PolyFEM
Loading...
Searching...
No Matches
StateHomogenization.cpp
Go to the documentation of this file.
1#include <polyfem/State.hpp>
7#include <polysolve/linear/FEMSolver.hpp>
8#include <polysolve/nonlinear/Solver.hpp>
9
13
14#include <unsupported/Eigen/SparseExtra>
15
18
19#include <ipc/ipc.hpp>
20
21namespace polyfem
22{
23
24 using namespace assembler;
25 using namespace mesh;
26 using namespace solver;
27 using namespace utils;
28 using namespace quadrature;
29
31 {
32 const int dim = mesh->dimension();
33 const int ndof = n_bases * dim;
34
35 const std::vector<std::shared_ptr<Form>> forms = solve_data.init_forms(
36 // General
37 units,
38 mesh->dimension(), t,
39 // Elastic form
41 args["solver"]["advanced"]["jacobian_threshold"], args["solver"]["advanced"]["check_inversion"],
42 // Body form
44 n_boundary_samples(), rhs, Eigen::VectorXd::Zero(ndof) /* only to set neumann BC, not used*/, mass_matrix_assembler->density(),
45 // Pressure form
47 // Inertia form
48 args.value("/time/quasistatic"_json_pointer, true), mass,
49 nullptr,
50 // Lagged regularization form
51 args["solver"]["advanced"]["lagged_regularization_weight"],
52 args["solver"]["advanced"]["lagged_regularization_iterations"],
53 // Augmented lagrangian form
54 obstacle.ndof(),
55 // Contact form
56 args["contact"]["enabled"], args["contact"]["periodic"].get<bool>() ? periodic_collision_mesh : collision_mesh, args["contact"]["dhat"],
57 avg_mass, args["contact"]["use_convergent_formulation"],
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"],
63 // Homogenization
65 // Periodic contact
66 args["contact"]["periodic"], periodic_collision_mesh_to_basis, periodic_bc,
67 // Friction form
68 args["contact"]["friction_coefficient"],
69 args["contact"]["epsv"],
70 args["solver"]["contact"]["friction_iterations"],
71 // Rayleigh damping form
72 args["solver"]["rayleigh_damping"]);
73
74 for (const auto &[name, form] : solve_data.named_forms())
75 {
76 if (name == "augmented_lagrangian")
77 {
78 form->set_weight(0);
79 form->disable();
80 }
81 }
82
83 bool solve_symmetric_flag = false;
84 {
85 const auto &fixed_entry = macro_strain_constraint.get_fixed_entry();
86 for (int i = 0; i < dim; i++)
87 {
88 for (int j = 0; j < i; j++)
89 {
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())
91 {
92 logger().info("Strain entry [{},{}] and [{},{}] are not fixed, solve for symmetric strain...", i, j, j, i);
93 solve_symmetric_flag = true;
94 break;
95 }
96 }
97 if (solve_symmetric_flag)
98 break;
99 }
100 }
101
102 std::shared_ptr<NLHomoProblem> homo_problem = std::make_shared<NLHomoProblem>(
103 ndof,
105 *this, t, forms, solve_data.al_form, solve_symmetric_flag);
107 homo_problem->add_form(solve_data.periodic_contact_form);
109 homo_problem->add_form(solve_data.strain_al_lagr_form);
110
111 solve_data.nl_problem = homo_problem;
112 solve_data.nl_problem->init(Eigen::VectorXd::Zero(homo_problem->reduced_size() + homo_problem->macro_reduced_size()));
113 solve_data.nl_problem->update_quantities(t, Eigen::VectorXd::Zero(homo_problem->reduced_size() + homo_problem->macro_reduced_size()));
114 }
115
116 void State::solve_homogenization_step(Eigen::MatrixXd &sol, const int t, bool adaptive_initial_weight)
117 {
118 const int dim = mesh->dimension();
119 const int ndof = n_bases * dim;
120
121 auto homo_problem = std::dynamic_pointer_cast<NLHomoProblem>(solve_data.nl_problem);
122
123 Eigen::VectorXd extended_sol;
124 extended_sol.setZero(ndof + dim * dim);
125
126 if (sol.size() == extended_sol.size())
127 extended_sol = sol;
128
129 const auto &fixed_entry = macro_strain_constraint.get_fixed_entry();
130 homo_problem->set_fixed_entry({});
131 {
132 std::shared_ptr<polysolve::nonlinear::Solver> nl_solver = make_nl_solver(true);
133
134 Eigen::VectorXi al_indices = fixed_entry.array() + homo_problem->full_size();
135 Eigen::VectorXd al_values = utils::flatten(macro_strain_constraint.eval(t))(fixed_entry);
136
137 std::shared_ptr<MacroStrainLagrangianForm> lagr_form = solve_data.strain_al_lagr_form;
138 lagr_form->enable();
139
140 const double initial_weight = args["solver"]["augmented_lagrangian"]["initial_weight"];
141 const double max_weight = args["solver"]["augmented_lagrangian"]["max_weight"];
142 const double eta_tol = args["solver"]["augmented_lagrangian"]["eta"];
143 const double scaling = args["solver"]["augmented_lagrangian"]["scaling"];
144 double al_weight = initial_weight;
145
146 Eigen::VectorXd tmp_sol = homo_problem->extended_to_reduced(extended_sol);
147 const Eigen::VectorXd initial_sol = tmp_sol;
148 const double initial_error = lagr_form->compute_error(extended_sol);
149 double current_error = initial_error;
150
151 // try to enforce fixed values on macro strain
152 extended_sol(al_indices) = al_values;
153 Eigen::VectorXd reduced_sol = homo_problem->extended_to_reduced(extended_sol);
154
155 homo_problem->line_search_begin(tmp_sol, reduced_sol);
156 int al_steps = 0;
157 bool force_al = true;
158
159 lagr_form->set_initial_weight(al_weight);
160
161 while (force_al
162 || !std::isfinite(homo_problem->value(reduced_sol))
163 || !homo_problem->is_step_valid(tmp_sol, reduced_sol)
164 || !homo_problem->is_step_collision_free(tmp_sol, reduced_sol))
165 {
166 force_al = false;
167 homo_problem->line_search_end();
168
169 logger().info("Solving AL Problem with weight {}", al_weight);
170
171 homo_problem->init(tmp_sol);
172 try
173 {
174 nl_solver->minimize(*homo_problem, tmp_sol);
175 }
176 catch (const std::runtime_error &e)
177 {
178 logger().error("AL solve failed!");
179 }
180
181 extended_sol = homo_problem->reduced_to_extended(tmp_sol);
182 logger().debug("Current macro strain: {}", extended_sol.tail(dim * dim));
183
184 current_error = lagr_form->compute_error(extended_sol);
185 const double eta = 1 - sqrt(current_error / initial_error);
186
187 logger().info("Current eta = {}, current error = {}, initial error = {}", eta, current_error, initial_error);
188
189 if (eta < eta_tol && al_weight < max_weight)
190 al_weight *= scaling;
191 else
192 lagr_form->update_lagrangian(extended_sol, al_weight);
193
194 if (eta <= 0)
195 {
196 if (adaptive_initial_weight)
197 {
198 args["solver"]["augmented_lagrangian"]["initial_weight"] = args["solver"]["augmented_lagrangian"]["initial_weight"].get<double>() * scaling;
199 {
200 json tmp = json::object();
201 tmp["/solver/augmented_lagrangian/initial_weight"_json_pointer] = args["solver"]["augmented_lagrangian"]["initial_weight"];
202 }
203 logger().warn("AL weight too small, increase weight and revert solution, new initial weight is {}", args["solver"]["augmented_lagrangian"]["initial_weight"].get<double>());
204 }
205 tmp_sol = initial_sol;
206 }
207
208 // try to enforce fixed values on macro strain
209 extended_sol(al_indices) = al_values;
210 reduced_sol = homo_problem->extended_to_reduced(extended_sol);
211
212 homo_problem->line_search_begin(tmp_sol, reduced_sol);
213 }
214 homo_problem->line_search_end();
215 lagr_form->disable();
216 }
217
218 homo_problem->set_fixed_entry(fixed_entry);
219
220 Eigen::VectorXd reduced_sol = homo_problem->extended_to_reduced(extended_sol);
221
222 homo_problem->init(reduced_sol);
223 std::shared_ptr<polysolve::nonlinear::Solver> nl_solver = make_nl_solver(false);
224 nl_solver->minimize(*homo_problem, reduced_sol);
225
226 logger().info("Macro Strain: {}", extended_sol.tail(dim * dim).transpose());
227
228 // check saddle point
229 {
230 json linear_args = args["solver"]["linear"];
231 std::string solver_name = linear_args["solver"];
232 if (solver_name.find("Pardiso") != std::string::npos)
233 {
234 linear_args["solver"] = "Eigen::PardisoLLT";
235 std::unique_ptr<polysolve::linear::Solver> solver =
236 polysolve::linear::Solver::create(linear_args, logger());
237
239 homo_problem->hessian(reduced_sol, A);
240 Eigen::VectorXd x, b = Eigen::VectorXd::Zero(A.rows());
241 try
242 {
243 dirichlet_solve(
244 *solver, A, b, {}, x, A.rows(), args["output"]["data"]["stiffness_mat"], false, false, false);
245 }
246 catch (const std::runtime_error &error)
247 {
248 logger().error("The solution is a saddle point!");
249 }
250 }
251 }
252
253 sol = homo_problem->reduced_to_extended(reduced_sol);
254 if (args["/boundary_conditions/periodic_boundary/force_zero_mean"_json_pointer].get<bool>())
255 {
256 Eigen::VectorXd integral = io::Evaluator::integrate_function(bases, geom_bases(), sol, dim, dim);
257 double area = io::Evaluator::integrate_function(bases, geom_bases(), Eigen::VectorXd::Ones(n_bases), dim, 1)(0);
258 for (int d = 0; d < dim; d++)
259 sol(Eigen::seqN(d, n_bases, dim), 0).array() -= integral(d) / area;
260
261 reduced_sol = homo_problem->extended_to_reduced(sol);
262 }
263
265 cache_transient_adjoint_quantities(t, homo_problem->reduced_to_full(reduced_sol), utils::unflatten(sol.bottomRows(dim * dim), dim));
266 }
267
268 void State::solve_homogenization(const int time_steps, const double t0, const double dt, Eigen::MatrixXd &sol)
269 {
270 bool is_static = !is_param_valid(args, "time");
271 if (!is_static && !args["time"]["quasistatic"])
272 log_and_throw_error("Transient homogenization can only do quasi-static!");
273
275
276 const int dim = mesh->dimension();
277 Eigen::MatrixXd extended_sol;
278 for (int t = 0; t <= time_steps; ++t)
279 {
280 double forward_solve_time = 0, remeshing_time = 0, global_relaxation_time = 0;
281
282 {
283 POLYFEM_SCOPED_TIMER(forward_solve_time);
284 solve_homogenization_step(extended_sol, t, false);
285 }
286 sol = extended_sol.topRows(extended_sol.size() - dim * dim) + io::Evaluator::generate_linear_field(n_bases, mesh_nodes, utils::unflatten(extended_sol.bottomRows(dim * dim), dim));
287
288 if (is_static)
289 return;
290
291 // Always save the solution for consistency
292 save_timestep(t0 + dt * t, t, t0, dt, sol, Eigen::MatrixXd()); // no pressure
293
294 {
295 POLYFEM_SCOPED_TIMER("Update quantities");
296
297 // solve_data.time_integrator->update_quantities(sol);
298
299 solve_data.nl_problem->update_quantities(t0 + (t + 1) * dt, sol);
300
303 }
304
305 logger().info("{}/{} t={}", t, time_steps, t0 + dt * t);
306
307 // const std::string rest_mesh_path = args["output"]["data"]["rest_mesh"].get<std::string>();
308 // if (!rest_mesh_path.empty())
309 // {
310 // Eigen::MatrixXd V;
311 // Eigen::MatrixXi F;
312 // build_mesh_matrices(V, F);
313 // io::MshWriter::write(
314 // resolve_output_path(fmt::format(args["output"]["data"]["rest_mesh"], t)),
315 // V, F, mesh->get_body_ids(), mesh->is_volume(), /*binary=*/true);
316 // }
317
318 // const std::string &state_path = resolve_output_path(fmt::format(args["output"]["data"]["state"], t));
319 // if (!state_path.empty())
320 // solve_data.time_integrator->save_state(state_path);
321
322 // save restart file
323 save_restart_json(t0, dt, t);
324 // stats_csv.write(t, forward_solve_time, remeshing_time, global_relaxation_time, sol);
325 }
326 }
327
328} // namespace polyfem
Quadrature quadrature
int x
#define POLYFEM_SCOPED_TIMER(...)
Definition Timer.hpp:10
assembler::MacroStrainValue macro_strain_constraint
Definition State.hpp:705
std::shared_ptr< utils::PeriodicBoundary > periodic_bc
periodic BC and periodic mesh utils
Definition State.hpp:389
int n_bases
number of bases
Definition State.hpp:178
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
Definition State.hpp:180
assembler::AssemblyValsCache ass_vals_cache
used to store assembly values for small problems
Definition State.hpp:196
int n_boundary_samples() const
quadrature used for projecting boundary conditions
Definition State.hpp:263
const std::vector< basis::ElementBases > & geom_bases() const
Get a constant reference to the geometry mapping bases.
Definition State.hpp:223
std::vector< mesh::LocalBoundary > local_pressure_boundary
mapping from elements to nodes for pressure boundary conditions
Definition State.hpp:437
mesh::Obstacle obstacle
Obstacles used in collisions.
Definition State.hpp:468
std::shared_ptr< assembler::Assembler > assembler
assemblers
Definition State.hpp:155
ipc::CollisionMesh periodic_collision_mesh
IPC collision mesh under periodic BC.
Definition State.hpp:518
ipc::CollisionMesh collision_mesh
IPC collision mesh.
Definition State.hpp:515
solver::CacheLevel optimization_enabled
Definition State.hpp:647
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
Definition State.hpp:157
std::unique_ptr< mesh::Mesh > mesh
current mesh, it can be a Mesh2D or Mesh3D
Definition State.hpp:466
std::shared_ptr< polyfem::mesh::MeshNodes > mesh_nodes
Mapping from input nodes to FE nodes.
Definition State.hpp:193
StiffnessMatrix mass
Mass matrix, it is computed only for time dependent problems.
Definition State.hpp:202
void solve_homogenization(const int time_steps, const double t0, const double dt, Eigen::MatrixXd &sol)
json args
main input arguments containing all defaults
Definition State.hpp:101
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
Definition State.hpp:520
std::vector< basis::ElementBases > bases
FE bases, the size is #elements.
Definition State.hpp:171
std::vector< mesh::LocalBoundary > local_boundary
mapping from elements to nodes for dirichlet boundary conditions
Definition State.hpp:433
double avg_mass
average system mass, used for contact with IPC
Definition State.hpp:204
int ndof() const
Definition State.hpp:653
assembler::AssemblyValsCache mass_ass_vals_cache
Definition State.hpp:197
std::vector< int > boundary_nodes
list of boundary nodes
Definition State.hpp:427
solver::SolveData solve_data
timedependent stuff cached
Definition State.hpp:324
std::vector< mesh::LocalBoundary > local_neumann_boundary
mapping from elements to nodes for neumann boundary conditions
Definition State.hpp:435
std::shared_ptr< assembler::PressureAssembler > elasticity_pressure_assembler
Definition State.hpp:162
Eigen::MatrixXd rhs
System right-hand side.
Definition State.hpp:207
std::unordered_map< int, std::vector< mesh::LocalBoundary > > local_pressure_cavity
mapping from elements to nodes for pressure boundary conditions
Definition State.hpp:439
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::vector< std::shared_ptr< Form > > init_forms(const Units &units, const int dim, const double t, const int n_bases, std::vector< basis::ElementBases > &bases, const std::vector< basis::ElementBases > &geom_bases, const assembler::Assembler &assembler, assembler::AssemblyValsCache &ass_vals_cache, const assembler::AssemblyValsCache &mass_ass_vals_cache, const double jacobian_threshold, const solver::ElementInversionCheck check_inversion, 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 &macro_strain_constraint, const bool periodic_contact, const Eigen::VectorXi &tiled_to_single, const std::shared_ptr< utils::PeriodicBoundary > &periodic_bc, 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.
Definition SolveData.cpp:27
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::vector< std::pair< std::string, std::shared_ptr< solver::Form > > > named_forms() const
std::vector< std::shared_ptr< solver::AugmentedLagrangianForm > > al_form
void update_barrier_stiffness(const Eigen::VectorXd &x)
update the barrier stiffness for the forms
bool is_param_valid(const json &params, 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.
Definition Logger.cpp:42
nlohmann::json json
Definition Common.hpp:9
void log_and_throw_error(const std::string &msg)
Definition Logger.cpp:71
Eigen::SparseMatrix< double, Eigen::ColMajor > StiffnessMatrix
Definition Types.hpp:22