PolyFEM
Loading...
Searching...
No Matches
NormalAdhesionForm.cpp
Go to the documentation of this file.
2
10
12
13#include <ipc/barrier/adaptive_stiffness.hpp>
14#include <ipc/utils/world_bbox_diagonal_length.hpp>
15
16#include <igl/writePLY.h>
17
18namespace polyfem::solver
19{
20 NormalAdhesionForm::NormalAdhesionForm(const ipc::CollisionMesh &collision_mesh,
21 const double dhat_p,
22 const double dhat_a,
23 const double Y,
24 const bool is_time_dependent,
25 const bool enable_shape_derivatives,
26 const ipc::BroadPhaseMethod broad_phase_method,
27 const double ccd_tolerance,
28 const int ccd_max_iterations)
29 : collision_mesh_(collision_mesh),
30 dhat_p_(dhat_p),
31 dhat_a_(dhat_a),
32 Y_(Y),
33 is_time_dependent_(is_time_dependent),
34 enable_shape_derivatives_(enable_shape_derivatives),
35 broad_phase_method_(broad_phase_method),
36 broad_phase_(ipc::build_broad_phase(broad_phase_method)),
37 tight_inclusion_ccd_(ccd_tolerance, ccd_max_iterations),
38 normal_adhesion_potential_(dhat_p, dhat_a, Y, 1)
39 {
40 assert(dhat_p > 0);
41 assert(dhat_a > dhat_p);
42 assert(ccd_tolerance > 0);
43
44 prev_distance_ = -1;
45 collision_set_.set_enable_shape_derivatives(enable_shape_derivatives);
46 }
47
48 void NormalAdhesionForm::init(const Eigen::VectorXd &x)
49 {
51 }
52
53 void NormalAdhesionForm::force_shape_derivative(const ipc::NormalCollisions &collision_set, const Eigen::MatrixXd &solution, const Eigen::VectorXd &adjoint_sol, Eigen::VectorXd &term)
54 {
55 // Eigen::MatrixXd U = collision_mesh_.vertices(utils::unflatten(solution, collision_mesh_.dim()));
56 // Eigen::MatrixXd X = collision_mesh_.vertices(boundary_nodes_pos_);
57 const Eigen::MatrixXd displaced_surface = compute_displaced_surface(solution);
58
59 StiffnessMatrix dq_h = collision_mesh_.to_full_dof(normal_adhesion_potential_.shape_derivative(collision_set, collision_mesh_, displaced_surface));
60 term = dq_h.transpose() * adjoint_sol;
61 }
62
63 void NormalAdhesionForm::update_quantities(const double t, const Eigen::VectorXd &x)
64 {
66 }
67
68 Eigen::MatrixXd NormalAdhesionForm::compute_displaced_surface(const Eigen::VectorXd &x) const
69 {
70 return collision_mesh_.displace_vertices(utils::unflatten(x, collision_mesh_.dim()));
71 }
72
73 void NormalAdhesionForm::update_collision_set(const Eigen::MatrixXd &displaced_surface)
74 {
75 // Store the previous value used to compute the constraint set to avoid duplicate computation.
76 static Eigen::MatrixXd cached_displaced_surface;
77 if (cached_displaced_surface.size() == displaced_surface.size() && cached_displaced_surface == displaced_surface)
78 return;
79
81 collision_set_.build(
82 candidates_, collision_mesh_, displaced_surface, dhat_a_);
83 else
84 collision_set_.build(
85 collision_mesh_, displaced_surface, dhat_a_, dmin_, broad_phase_);
86 cached_displaced_surface = displaced_surface;
87 }
88
93
94 Eigen::VectorXd NormalAdhesionForm::value_per_element_unweighted(const Eigen::VectorXd &x) const
95 {
96 const Eigen::MatrixXd V = compute_displaced_surface(x);
97 assert(V.rows() == collision_mesh_.num_vertices());
98
99 const size_t num_vertices = collision_mesh_.num_vertices();
100
101 if (collision_set_.empty())
102 {
103 return Eigen::VectorXd::Zero(collision_mesh_.full_num_vertices());
104 }
105
106 const Eigen::MatrixXi &E = collision_mesh_.edges();
107 const Eigen::MatrixXi &F = collision_mesh_.faces();
108
109 auto storage = utils::create_thread_storage<Eigen::VectorXd>(Eigen::VectorXd::Zero(num_vertices));
110
111 utils::maybe_parallel_for(collision_set_.size(), [&](int start, int end, int thread_id) {
112 Eigen::VectorXd &local_storage = utils::get_local_thread_storage(storage, thread_id);
113
114 for (size_t i = start; i < end; i++)
115 {
116 // Quadrature weight is premultiplied by compute_potential
117 const double potential = normal_adhesion_potential_(collision_set_[i], collision_set_[i].dof(V, E, F));
118
119 const int n_v = collision_set_[i].num_vertices();
120 const auto vis = collision_set_[i].vertex_ids(E, F);
121 for (int j = 0; j < n_v; j++)
122 {
123 assert(0 <= vis[j] && vis[j] < num_vertices);
124 local_storage[vis[j]] += potential / n_v;
125 }
126 }
127 });
128
129 Eigen::VectorXd out = Eigen::VectorXd::Zero(num_vertices);
130 for (const auto &local_potential : storage)
131 {
132 out += local_potential;
133 }
134
135 Eigen::VectorXd out_full = Eigen::VectorXd::Zero(collision_mesh_.full_num_vertices());
136 for (int i = 0; i < out.size(); i++)
137 out_full[collision_mesh_.to_full_vertex_id(i)] = out[i];
138
139 assert(std::abs(value_unweighted(x) - out_full.sum()) < std::max(1e-10 * out_full.sum(), 1e-10));
140
141 return out_full;
142 }
143
144 void NormalAdhesionForm::first_derivative_unweighted(const Eigen::VectorXd &x, Eigen::VectorXd &gradv) const
145 {
147 gradv = collision_mesh_.to_full_dof(gradv);
148 }
149
150 void NormalAdhesionForm::second_derivative_unweighted(const Eigen::VectorXd &x, StiffnessMatrix &hessian) const
151 {
152 POLYFEM_SCOPED_TIMER("normal adhesion hessian");
153
154 ipc::PSDProjectionMethod psd_projection_method;
155
156 if (project_to_psd_) {
157 psd_projection_method = ipc::PSDProjectionMethod::CLAMP;
158 } else {
159 psd_projection_method = ipc::PSDProjectionMethod::NONE;
160 }
161
162 hessian = normal_adhesion_potential_.hessian(collision_set_, collision_mesh_, compute_displaced_surface(x), psd_projection_method);
163 hessian = collision_mesh_.to_full_dof(hessian);
164 }
165
166 void NormalAdhesionForm::solution_changed(const Eigen::VectorXd &new_x)
167 {
169 }
170
171 void NormalAdhesionForm::line_search_begin(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1)
172 {
173 candidates_.build(
177 /*inflation_radius=*/dhat_a_ / 2,
179
181 }
182
184 {
185 candidates_.clear();
187 }
188
189 void NormalAdhesionForm::post_step(const polysolve::nonlinear::PostStepData &data)
190 {
191 if (data.iter_num == 0)
192 return;
193
194 const Eigen::MatrixXd displaced_surface = compute_displaced_surface(data.x);
195
196 const double curr_distance = collision_set_.compute_minimum_distance(collision_mesh_, displaced_surface);
197
198 prev_distance_ = curr_distance;
199 }
200
201
202} // namespace polyfem::solver
int V
int x
#define POLYFEM_SCOPED_TIMER(...)
Definition Timer.hpp:10
bool project_to_psd_
If true, the form's second derivative is projected to be positive semidefinite.
Definition Form.hpp:147
void init(const Eigen::VectorXd &x) override
Initialize the form.
bool use_cached_candidates_
If true, use the cached candidate set for the current solution.
const ipc::CollisionMesh & collision_mesh_
Collision mesh.
const std::shared_ptr< ipc::BroadPhase > broad_phase_
ipc::NormalCollisions collision_set_
Cached constraint set for the current solution.
virtual void second_derivative_unweighted(const Eigen::VectorXd &x, StiffnessMatrix &hessian) const override
Compute the second derivative of the value wrt x.
const ipc::NormalAdhesionPotential normal_adhesion_potential_
double prev_distance_
Previous minimum distance between all elements.
ipc::Candidates candidates_
Cached candidate set for the current solution.
const double dhat_a_
Adhesion activation distance.
virtual void first_derivative_unweighted(const Eigen::VectorXd &x, Eigen::VectorXd &gradv) const override
Compute the first derivative of the value wrt x.
const double dmin_
Minimum distance between elements.
NormalAdhesionForm(const ipc::CollisionMesh &collision_mesh, const double dhat_p, const double dhat_a, const double Y, const bool is_time_dependent, const bool enable_shape_derivatives, const ipc::BroadPhaseMethod broad_phase_method, const double ccd_tolerance, const int ccd_max_iterations)
Construct a new NormalAdhesion Form object.
void update_quantities(const double t, const Eigen::VectorXd &x) override
Update time-dependent fields.
virtual double value_unweighted(const Eigen::VectorXd &x) const override
Compute the normal adhesion potential value.
virtual void force_shape_derivative(const ipc::NormalCollisions &collision_set, const Eigen::MatrixXd &solution, const Eigen::VectorXd &adjoint_sol, Eigen::VectorXd &term)
void line_search_begin(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1) override
Initialize variables used during the line search.
Eigen::VectorXd value_per_element_unweighted(const Eigen::VectorXd &x) const override
Compute the value of the form multiplied per element.
const ipc::NormalCollisions & collision_set() const
void line_search_end() override
Clear variables used during the line search.
Eigen::MatrixXd compute_displaced_surface(const Eigen::VectorXd &x) const
Compute the displaced positions of the surface nodes.
void solution_changed(const Eigen::VectorXd &new_x) override
Update cached fields upon a change in the solution.
void update_collision_set(const Eigen::MatrixXd &displaced_surface)
Update the cached candidate set for the current solution.
void post_step(const polysolve::nonlinear::PostStepData &data) override
Update fields after a step in the optimization.
Eigen::MatrixXd unflatten(const Eigen::VectorXd &x, int dim)
Unflatten rowwises, so every dim elements in x become a row.
void maybe_parallel_for(int size, const std::function< void(int, int, int)> &partial_for)
Eigen::SparseMatrix< double, Eigen::ColMajor > StiffnessMatrix
Definition Types.hpp:22