PolyFEM
Loading...
Searching...
No Matches
L2Projection.cpp
Go to the documentation of this file.
1#include "L2Projection.hpp"
2
11
12#include <ipc/ipc.hpp>
13
14#include <polysolve/linear/Solver.hpp>
15
16namespace polyfem::mesh
17{
19 const Eigen::SparseMatrix<double> &M,
20 const Eigen::SparseMatrix<double> &A,
21 const Eigen::Ref<const Eigen::MatrixXd> &y)
22 {
23 // Construct a linear solver for M
24 std::unique_ptr<polysolve::linear::Solver> solver;
25#ifdef POLYSOLVE_WITH_MKL
26 solver = polysolve::linear::Solver::create("Eigen::PardisoLDLT", "");
27#elif defined(POLYSOLVE_WITH_CHOLMOD)
28 solver = polysolve::linear::Solver::create("Eigen::CholmodSimplicialLDLT", "");
29#else
30 solver = polysolve::linear::Solver::create("Eigen::SimplicialLDLT", "");
31#endif
32
33 solver->analyze_pattern(M, 0);
34 solver->factorize(M);
35
36 const Eigen::MatrixXd rhs = A * y;
37 Eigen::MatrixXd x(rhs.rows(), rhs.cols());
38 for (int i = 0; i < x.cols(); ++i)
39 solver->solve(rhs.col(i), x.col(i));
40
41 double residual_error = (M * x - rhs).norm();
42 logger().debug("residual error in L2 projection: {}", residual_error);
43 assert(residual_error < std::max(1e-12 * rhs.norm(), 1e-12));
44
45 return x;
46 }
47
49 const Eigen::MatrixXd &M,
50 const Eigen::MatrixXd &A,
51 const Eigen::Ref<const Eigen::MatrixXd> &y,
52 const std::vector<int> &boundary_nodes,
53 Eigen::Ref<Eigen::MatrixXd> x)
54 {
55 std::vector<int> free_nodes;
56 for (int i = 0, j = 0; i < y.rows(); ++i)
57 {
58 if (boundary_nodes[j] == i)
59 ++j;
60 else
61 free_nodes.push_back(i);
62 }
63
64 const Eigen::MatrixXd H = M(free_nodes, free_nodes);
65 const Eigen::MatrixXd g = -((M * x - A * y)(free_nodes, Eigen::all));
66 const Eigen::MatrixXd sol = H.llt().solve(g);
67 x(free_nodes, Eigen::all) += sol;
68 }
69
71 // Nonlinear solver
72 std::shared_ptr<polysolve::nonlinear::Solver> nl_solver,
73 // L2 projection form
74 const Eigen::SparseMatrix<double> &M,
75 const Eigen::SparseMatrix<double> &A,
76 const Eigen::VectorXd &y,
77 // Inversion-free form
78 const Eigen::MatrixXd &rest_positions,
79 const Eigen::MatrixXi &elements,
80 const int dim,
81 // Contact form
82 const ipc::CollisionMesh &collision_mesh,
83 const double dhat,
84 const double barrier_stiffness,
85 const bool use_convergent_formulation,
86 const ipc::BroadPhaseMethod broad_phase_method,
87 const double ccd_tolerance,
88 const int ccd_max_iterations,
89 // Augmented lagrangian form
90 const std::vector<int> &boundary_nodes,
91 const size_t obstacle_ndof,
92 const Eigen::VectorXd &target_x,
93 // Initial guess
94 const Eigen::VectorXd &x0)
95 {
96 using namespace polyfem::solver;
97
98 assert(M.rows() == M.cols());
99 assert(A.rows() == M.rows());
100 assert(A.cols() == y.size());
101
102 std::vector<std::shared_ptr<Form>> forms;
103
104 forms.push_back(std::make_shared<L2ProjectionForm>(M, A, y));
105
106 forms.push_back(std::make_shared<InversionBarrierForm>(
107 rest_positions, elements, dim, /*vhat=*/1e-12));
108 forms.back()->set_weight(barrier_stiffness); // use same weight as barrier stiffness
109 assert(forms.back()->is_step_valid(x0, x0));
110
111 if (collision_mesh.num_vertices() != 0)
112 {
113 forms.push_back(std::make_shared<ContactForm>(
114 collision_mesh, dhat, /*avg_mass=*/1.0, use_convergent_formulation,
115 /*use_adaptive_barrier_stiffness=*/false, /*is_time_dependent=*/true,
116 /*enable_shape_derivatives=*/false, broad_phase_method, ccd_tolerance,
117 ccd_max_iterations));
118 forms.back()->set_weight(barrier_stiffness);
119 assert(!ipc::has_intersections(collision_mesh, collision_mesh.displace_vertices(utils::unflatten(x0, dim))));
120 }
121
122 const int ndof = x0.size();
123 std::shared_ptr<BCPenaltyForm> bc_penalty_form = std::make_shared<BCPenaltyForm>(
124 ndof, boundary_nodes, M, obstacle_ndof, target_x);
125 forms.push_back(bc_penalty_form);
126
127 std::shared_ptr<BCLagrangianForm> bc_lagrangian_form = std::make_shared<BCLagrangianForm>(
128 ndof, boundary_nodes, M, obstacle_ndof, target_x);
129 forms.push_back(bc_lagrangian_form);
130
131 // --------------------------------------------------------------------
132
133 StaticBoundaryNLProblem problem(ndof, boundary_nodes, target_x, forms);
134
135 // --------------------------------------------------------------------
136
137 // Create augmented Lagrangian solver
138 // AL parameters
139 constexpr double al_initial_weight = 1e6;
140 constexpr double al_scaling = 2.0;
141 constexpr int al_max_weight = 100 * al_initial_weight;
142 constexpr double al_eta_tol = 0.99;
143 constexpr size_t al_max_solver_iter = 1000;
144 ALSolver al_solver(
145 bc_lagrangian_form, bc_penalty_form, al_initial_weight,
146 al_scaling, al_max_weight, al_eta_tol,
147 /*update_barrier_stiffness=*/[&](const Eigen::MatrixXd &x) {});
148
149 Eigen::MatrixXd sol = x0;
150
151 const size_t default_max_iterations = nl_solver->stop_criteria().iterations;
152 nl_solver->stop_criteria().iterations = al_max_solver_iter;
153 al_solver.solve_al(nl_solver, problem, sol);
154
155 nl_solver->stop_criteria().iterations = default_max_iterations;
156 al_solver.solve_reduced(nl_solver, problem, sol);
157
158#ifndef NDEBUG
159 assert(forms[1]->is_step_valid(sol, sol)); // inversion-free
160 if (collision_mesh.num_vertices() != 0)
161 assert(!ipc::has_intersections(collision_mesh, collision_mesh.displace_vertices(utils::unflatten(sol, dim))));
162#endif
163
164 return sol;
165 }
166} // namespace polyfem::mesh
int y
int x
void solve_al(std::shared_ptr< NLSolver > nl_solver, NLProblem &nl_problem, Eigen::MatrixXd &sol)
Definition ALSolver.cpp:25
Eigen::MatrixXd unconstrained_L2_projection(const Eigen::SparseMatrix< double > &M, const Eigen::SparseMatrix< double > &A, const Eigen::Ref< const Eigen::MatrixXd > &y)
Eigen::VectorXd constrained_L2_projection(std::shared_ptr< polysolve::nonlinear::Solver > nl_solver, const Eigen::SparseMatrix< double > &M, const Eigen::SparseMatrix< double > &A, const Eigen::VectorXd &y, const Eigen::MatrixXd &rest_positions, const Eigen::MatrixXi &elements, const int dim, const ipc::CollisionMesh &collision_mesh, const double dhat, const double barrier_stiffness, const bool use_convergent_formulation, const ipc::BroadPhaseMethod broad_phase_method, const double ccd_tolerance, const int ccd_max_iterations, const std::vector< int > &boundary_nodes, const size_t obstacle_ndof, const Eigen::VectorXd &target_x, const Eigen::VectorXd &x0)
void reduced_L2_projection(const Eigen::MatrixXd &M, const Eigen::MatrixXd &A, const Eigen::Ref< const Eigen::MatrixXd > &y, const std::vector< int > &boundary_nodes, Eigen::Ref< Eigen::MatrixXd > x)
Eigen::MatrixXd unflatten(const Eigen::VectorXd &x, int dim)
Unflatten rowwises, so every dim elements in x become a row.
spdlog::logger & logger()
Retrieves the current logger.
Definition Logger.cpp:42