7#include <polysolve/linear/FEMSolver.hpp>
8#include <polysolve/nonlinear/Solver.hpp>
15#include <unsupported/Eigen/SparseExtra>
25 using namespace assembler;
27 using namespace solver;
28 using namespace utils;
33 const int dim =
mesh->dimension();
48 args.value(
"/time/quasistatic"_json_pointer,
true),
mass,
51 args[
"solver"][
"advanced"][
"lagged_regularization_weight"],
52 args[
"solver"][
"advanced"][
"lagged_regularization_iterations"],
58 args[
"solver"][
"contact"][
"barrier_stiffness"],
59 args[
"solver"][
"contact"][
"CCD"][
"broad_phase"],
60 args[
"solver"][
"contact"][
"CCD"][
"tolerance"],
61 args[
"solver"][
"contact"][
"CCD"][
"max_iterations"],
68 args[
"contact"][
"friction_coefficient"],
69 args[
"contact"][
"epsv"],
70 args[
"solver"][
"contact"][
"friction_iterations"],
72 args[
"solver"][
"rayleigh_damping"]);
76 if (name ==
"augmented_lagrangian_lagr" || name ==
"augmented_lagrangian_penalty")
83 bool solve_symmetric_flag =
false;
86 for (
int i = 0; i < dim; i++)
88 for (
int j = 0; j < i; j++)
90 if (std::find(fixed_entry.data(), fixed_entry.data() + fixed_entry.size(), i + j * dim) == fixed_entry.data() + fixed_entry.size() && std::find(fixed_entry.data(), fixed_entry.data() + fixed_entry.size(), j + i * dim) == fixed_entry.data() + fixed_entry.size())
92 logger().info(
"Strain entry [{},{}] and [{},{}] are not fixed, solve for symmetric strain...", i, j, j, i);
93 solve_symmetric_flag =
true;
97 if (solve_symmetric_flag)
102 std::shared_ptr<NLHomoProblem> homo_problem = std::make_shared<NLHomoProblem>(
108 *
this, t, forms, solve_symmetric_flag);
117 solve_data.
nl_problem->init(Eigen::VectorXd::Zero(homo_problem->reduced_size() + homo_problem->macro_reduced_size()));
118 solve_data.
nl_problem->update_quantities(t, Eigen::VectorXd::Zero(homo_problem->reduced_size() + homo_problem->macro_reduced_size()));
123 const int dim =
mesh->dimension();
128 Eigen::VectorXd extended_sol;
129 extended_sol.setZero(
ndof + dim * dim);
131 if (sol.size() == extended_sol.size())
135 homo_problem->set_fixed_entry({});
137 std::shared_ptr<polysolve::nonlinear::Solver> nl_solver =
make_nl_solver(
true);
139 Eigen::VectorXi al_indices = fixed_entry.array() + homo_problem->full_size();
147 const double initial_weight =
args[
"solver"][
"augmented_lagrangian"][
"initial_weight"];
148 const double max_weight =
args[
"solver"][
"augmented_lagrangian"][
"max_weight"];
149 const double eta_tol =
args[
"solver"][
"augmented_lagrangian"][
"eta"];
150 const double scaling =
args[
"solver"][
"augmented_lagrangian"][
"scaling"];
151 double al_weight = initial_weight;
153 Eigen::VectorXd tmp_sol = homo_problem->extended_to_reduced(extended_sol);
154 const Eigen::VectorXd initial_sol = tmp_sol;
155 const double initial_error = al_form->compute_error(extended_sol);
156 double current_error = initial_error;
159 extended_sol(al_indices) = al_values;
160 Eigen::VectorXd reduced_sol = homo_problem->extended_to_reduced(extended_sol);
162 homo_problem->line_search_begin(tmp_sol, reduced_sol);
164 bool force_al =
true;
166 || !std::isfinite(homo_problem->value(reduced_sol))
167 || !homo_problem->is_step_valid(tmp_sol, reduced_sol)
168 || !homo_problem->is_step_collision_free(tmp_sol, reduced_sol))
171 homo_problem->line_search_end();
173 al_form->set_weight(al_weight);
174 logger().info(
"Solving AL Problem with weight {}", al_weight);
176 homo_problem->init(tmp_sol);
179 nl_solver->minimize(*homo_problem, tmp_sol);
181 catch (
const std::runtime_error &e)
183 logger().error(
"AL solve failed!");
184 export_data(homo_problem->reduced_to_full(tmp_sol), Eigen::MatrixXd());
187 extended_sol = homo_problem->reduced_to_extended(tmp_sol);
188 logger().debug(
"Current macro strain: {}", extended_sol.tail(dim * dim));
190 current_error = al_form->compute_error(extended_sol);
191 const double eta = 1 - sqrt(current_error / initial_error);
193 logger().info(
"Current eta = {}, current error = {}, initial error = {}", eta, current_error, initial_error);
195 if (eta < eta_tol && al_weight < max_weight)
196 al_weight *= scaling;
198 lagr_form->update_lagrangian(extended_sol, al_weight);
202 if (adaptive_initial_weight)
204 args[
"solver"][
"augmented_lagrangian"][
"initial_weight"] =
args[
"solver"][
"augmented_lagrangian"][
"initial_weight"].get<
double>() * scaling;
206 json tmp = json::object();
207 tmp[
"/solver/augmented_lagrangian/initial_weight"_json_pointer] =
args[
"solver"][
"augmented_lagrangian"][
"initial_weight"];
209 logger().warn(
"AL weight too small, increase weight and revert solution, new initial weight is {}",
args[
"solver"][
"augmented_lagrangian"][
"initial_weight"].get<double>());
211 tmp_sol = initial_sol;
215 extended_sol(al_indices) = al_values;
216 reduced_sol = homo_problem->extended_to_reduced(extended_sol);
218 homo_problem->line_search_begin(tmp_sol, reduced_sol);
220 homo_problem->line_search_end();
222 lagr_form->disable();
225 homo_problem->set_fixed_entry(fixed_entry);
227 Eigen::VectorXd reduced_sol = homo_problem->extended_to_reduced(extended_sol);
229 homo_problem->init(reduced_sol);
230 std::shared_ptr<polysolve::nonlinear::Solver> nl_solver =
make_nl_solver(
false);
231 nl_solver->minimize(*homo_problem, reduced_sol);
233 logger().info(
"Macro Strain: {}", extended_sol.tail(dim * dim).transpose());
237 json linear_args =
args[
"solver"][
"linear"];
238 std::string solver_name = linear_args[
"solver"];
239 if (solver_name.find(
"Pardiso") != std::string::npos)
241 linear_args[
"solver"] =
"Eigen::PardisoLLT";
242 std::unique_ptr<polysolve::linear::Solver> solver =
243 polysolve::linear::Solver::create(linear_args,
logger());
246 homo_problem->hessian(reduced_sol, A);
247 Eigen::VectorXd
x, b = Eigen::VectorXd::Zero(A.rows());
251 *solver, A, b, {},
x, A.rows(),
args[
"output"][
"data"][
"stiffness_mat"],
false,
false,
false);
253 catch (
const std::runtime_error &error)
255 logger().error(
"The solution is a saddle point!");
260 sol = homo_problem->reduced_to_extended(reduced_sol);
261 if (
args[
"/boundary_conditions/periodic_boundary/force_zero_mean"_json_pointer].get<bool>())
265 for (
int d = 0; d < dim; d++)
266 sol(Eigen::seqN(d,
n_bases, dim), 0).array() -= integral(d) / area;
268 reduced_sol = homo_problem->extended_to_reduced(sol);
278 if (!is_static && !
args[
"time"][
"quasistatic"])
283 const int dim =
mesh->dimension();
284 Eigen::MatrixXd extended_sol;
285 for (
int t = 0; t <= time_steps; ++t)
287 double forward_solve_time = 0, remeshing_time = 0, global_relaxation_time = 0;
299 save_timestep(t0 + dt * t, t, t0, dt, sol, Eigen::MatrixXd());
312 logger().info(
"{}/{} t={}", t, time_steps, t0 + dt * t);
#define POLYFEM_SCOPED_TIMER(...)
assembler::MacroStrainValue macro_strain_constraint
int n_bases
number of bases
void cache_transient_adjoint_quantities(const int current_step, const Eigen::MatrixXd &sol, const Eigen::MatrixXd &disp_grad)
int n_pressure_bases
number of pressure bases
assembler::AssemblyValsCache ass_vals_cache
used to store assembly values for small problems
int n_boundary_samples() const
quadrature used for projecting boundary conditions
const std::vector< basis::ElementBases > & geom_bases() const
Get a constant reference to the geometry mapping bases.
std::vector< mesh::LocalBoundary > local_pressure_boundary
mapping from elements to nodes for pressure boundary conditions
mesh::Obstacle obstacle
Obstacles used in collisions.
std::shared_ptr< assembler::Assembler > assembler
assemblers
ipc::CollisionMesh periodic_collision_mesh
IPC collision mesh under periodic BC.
ipc::CollisionMesh collision_mesh
IPC collision mesh.
solver::CacheLevel optimization_enabled
void solve_homogenization_step(Eigen::MatrixXd &sol, const int t=0, bool adaptive_initial_weight=false)
In Elasticity PDE, solve for "min W(disp_grad + \grad u)" instead of "min W(\grad u)".
void save_timestep(const double time, const int t, const double t0, const double dt, const Eigen::MatrixXd &sol, const Eigen::MatrixXd &pressure)
saves a timestep
std::shared_ptr< polysolve::nonlinear::Solver > make_nl_solver(bool for_al) const
factory to create the nl solver depending on input
std::shared_ptr< assembler::Mass > mass_matrix_assembler
std::unique_ptr< mesh::Mesh > mesh
current mesh, it can be a Mesh2D or Mesh3D
std::shared_ptr< polyfem::mesh::MeshNodes > mesh_nodes
Mapping from input nodes to FE nodes.
StiffnessMatrix mass
Mass matrix, it is computed only for time dependent problems.
void solve_homogenization(const int time_steps, const double t0, const double dt, Eigen::MatrixXd &sol)
json args
main input arguments containing all defaults
void save_restart_json(const double t0, const double dt, const int t) const
Save a JSON sim file for restarting the simulation at time t.
Eigen::VectorXi periodic_collision_mesh_to_basis
index mapping from periodic 2x2 collision mesh to FE periodic mesh
std::vector< basis::ElementBases > bases
FE bases, the size is #elements.
std::vector< mesh::LocalBoundary > local_boundary
mapping from elements to nodes for dirichlet boundary conditions
double avg_mass
average system mass, used for contact with IPC
void export_data(const Eigen::MatrixXd &sol, const Eigen::MatrixXd &pressure)
saves all data on the disk according to the input params
assembler::AssemblyValsCache mass_ass_vals_cache
std::vector< int > boundary_nodes
list of boundary nodes
solver::SolveData solve_data
timedependent stuff cached
std::vector< mesh::LocalBoundary > local_neumann_boundary
mapping from elements to nodes for neumann boundary conditions
std::shared_ptr< assembler::PressureAssembler > elasticity_pressure_assembler
Eigen::MatrixXd rhs
System right-hand side.
std::unordered_map< int, std::vector< mesh::LocalBoundary > > local_pressure_cavity
mapping from elements to nodes for pressure boundary conditions
void init_homogenization_solve(const double t)
Eigen::MatrixXd eval(const double t) const
const Eigen::VectorXi & get_fixed_entry() const
static Eigen::MatrixXd generate_linear_field(const int n_bases, const std::shared_ptr< mesh::MeshNodes > mesh_nodes, const Eigen::MatrixXd &grad)
static Eigen::VectorXd integrate_function(const std::vector< basis::ElementBases > &bases, const std::vector< basis::ElementBases > &gbases, const Eigen::MatrixXd &fun, const int dim, const int actual_dim)
std::shared_ptr< solver::PeriodicContactForm > periodic_contact_form
void update_dt()
updates the dt inside the different forms
std::shared_ptr< solver::NLProblem > nl_problem
std::shared_ptr< solver::MacroStrainLagrangianForm > strain_al_lagr_form
std::shared_ptr< solver::MacroStrainALForm > strain_al_pen_form
std::vector< std::pair< std::string, std::shared_ptr< solver::Form > > > named_forms() const
std::vector< std::shared_ptr< Form > > init_forms(const Units &units, const int dim, const double t, const int n_bases, const std::vector< basis::ElementBases > &bases, const std::vector< basis::ElementBases > &geom_bases, const assembler::Assembler &assembler, const assembler::AssemblyValsCache &ass_vals_cache, const assembler::AssemblyValsCache &mass_ass_vals_cache, const int n_pressure_bases, const std::vector< int > &boundary_nodes, const std::vector< mesh::LocalBoundary > &local_boundary, const std::vector< mesh::LocalBoundary > &local_neumann_boundary, const int n_boundary_samples, const Eigen::MatrixXd &rhs, const Eigen::MatrixXd &sol, const assembler::Density &density, const std::vector< mesh::LocalBoundary > &local_pressure_boundary, const std::unordered_map< int, std::vector< mesh::LocalBoundary > > &local_pressure_cavity, const std::shared_ptr< assembler::PressureAssembler > pressure_assembler, const bool ignore_inertia, const StiffnessMatrix &mass, const std::shared_ptr< assembler::ViscousDamping > damping_assembler, const double lagged_regularization_weight, const int lagged_regularization_iterations, const size_t obstacle_ndof, const bool contact_enabled, const ipc::CollisionMesh &collision_mesh, const double dhat, const double avg_mass, const bool use_convergent_contact_formulation, const json &barrier_stiffness, const ipc::BroadPhaseMethod broad_phase, const double ccd_tolerance, const long ccd_max_iterations, const bool enable_shape_derivatives, const assembler::MacroStrainValue ¯o_strain_constraint, const bool periodic_contact, const Eigen::VectorXi &tiled_to_single, const double friction_coefficient, const double epsv, const int friction_iterations, const json &rayleigh_damping)
Initialize the forms and return a vector of pointers to them.
void update_barrier_stiffness(const Eigen::VectorXd &x)
update the barrier stiffness for the forms
std::shared_ptr< assembler::RhsAssembler > rhs_assembler
bool is_param_valid(const json ¶ms, const std::string &key)
Determine if a key exists and is non-null in a json object.
Eigen::MatrixXd unflatten(const Eigen::VectorXd &x, int dim)
Unflatten rowwises, so every dim elements in x become a row.
Eigen::VectorXd flatten(const Eigen::MatrixXd &X)
Flatten rowwises.
spdlog::logger & logger()
Retrieves the current logger.
void log_and_throw_error(const std::string &msg)
Eigen::SparseMatrix< double, Eigen::ColMajor > StiffnessMatrix