PolyFEM
Loading...
Searching...
No Matches
ContactForm.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "Form.hpp"
4
5#include <polyfem/Common.hpp>
7
8#include <ipc/collisions/collisions.hpp>
9#include <ipc/collision_mesh.hpp>
10#include <ipc/broad_phase/broad_phase.hpp>
11#include <ipc/potentials/barrier_potential.hpp>
12
13// map BroadPhaseMethod values to JSON as strings
14namespace ipc
15{
16 NLOHMANN_JSON_SERIALIZE_ENUM(
17 ipc::BroadPhaseMethod,
18 {{ipc::BroadPhaseMethod::HASH_GRID, "hash_grid"}, // also default
19 {ipc::BroadPhaseMethod::HASH_GRID, "HG"},
20 {ipc::BroadPhaseMethod::BRUTE_FORCE, "brute_force"},
21 {ipc::BroadPhaseMethod::BRUTE_FORCE, "BF"},
22 {ipc::BroadPhaseMethod::SPATIAL_HASH, "spatial_hash"},
23 {ipc::BroadPhaseMethod::SPATIAL_HASH, "SH"},
24 {ipc::BroadPhaseMethod::BVH, "bvh"},
25 {ipc::BroadPhaseMethod::BVH, "BVH"},
26 {ipc::BroadPhaseMethod::SWEEP_AND_PRUNE, "sweep_and_prune"},
27 {ipc::BroadPhaseMethod::SWEEP_AND_PRUNE, "SAP"},
28 {ipc::BroadPhaseMethod::SWEEP_AND_TINIEST_QUEUE, "sweep_and_tiniest_queue"},
29 {ipc::BroadPhaseMethod::SWEEP_AND_TINIEST_QUEUE, "STQ"}})
30} // namespace ipc
31
32namespace polyfem::solver
33{
35 class ContactForm : public Form
36 {
37 public:
47 ContactForm(const ipc::CollisionMesh &collision_mesh,
48 const double dhat,
49 const double avg_mass,
52 const bool is_time_dependent,
53 const bool enable_shape_derivatives,
54 const ipc::BroadPhaseMethod broad_phase_method,
55 const double ccd_tolerance,
56 const int ccd_max_iterations);
57 virtual ~ContactForm() = default;
58
59 std::string name() const override { return "contact"; }
60
63 void init(const Eigen::VectorXd &x) override;
64
65 virtual void force_shape_derivative(const ipc::Collisions &collision_set, const Eigen::MatrixXd &solution, const Eigen::VectorXd &adjoint_sol, Eigen::VectorXd &term);
66
67 protected:
71 virtual double value_unweighted(const Eigen::VectorXd &x) const override;
72
76 Eigen::VectorXd value_per_element_unweighted(const Eigen::VectorXd &x) const override;
77
81 virtual void first_derivative_unweighted(const Eigen::VectorXd &x, Eigen::VectorXd &gradv) const override;
82
86 virtual void second_derivative_unweighted(const Eigen::VectorXd &x, StiffnessMatrix &hessian) const override;
87
88 public:
92 void update_quantities(const double t, const Eigen::VectorXd &x) override;
93
98 double max_step_size(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1) const override;
99
103 void line_search_begin(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1) override;
104
106 void line_search_end() override;
107
110 void solution_changed(const Eigen::VectorXd &new_x) override;
111
115 void post_step(const polysolve::nonlinear::PostStepData &data) override;
116
119 bool is_step_collision_free(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1) const override;
120
123 virtual void update_barrier_stiffness(const Eigen::VectorXd &x, const Eigen::MatrixXd &grad_energy);
124
126 Eigen::MatrixXd compute_displaced_surface(const Eigen::VectorXd &x) const;
127
129 double barrier_stiffness() const { return barrier_stiffness_; }
135 bool use_convergent_formulation() const { return collision_set_.use_convergent_formulation(); }
136
138
139 double weight() const override { return weight_ * barrier_stiffness_; }
140
143
144 double dhat() const { return dhat_; }
145 const ipc::Collisions &collision_set() const { return collision_set_; }
146 const ipc::BarrierPotential &barrier_potential() const { return barrier_potential_; }
147
148 protected:
151 void update_collision_set(const Eigen::MatrixXd &displaced_surface);
152
154 const ipc::CollisionMesh &collision_mesh_;
155
157 const double dhat_;
158
160 const double dmin_ = 0;
161
168
170 const double avg_mass_;
171
174
177
179 const ipc::BroadPhaseMethod broad_phase_method_;
181 const double ccd_tolerance_;
184
187
191 ipc::Collisions collision_set_;
193 ipc::Candidates candidates_;
194
195 const ipc::BarrierPotential barrier_potential_;
196 };
197} // namespace polyfem::solver
int x
Form representing the contact potential and forces.
ipc::Collisions collision_set_
Cached constraint set for the current solution.
const double avg_mass_
Average mass of the mesh (used for adaptive barrier stiffness)
virtual ~ContactForm()=default
const bool use_adaptive_barrier_stiffness_
If true, use an adaptive barrier stiffness.
virtual void force_shape_derivative(const ipc::Collisions &collision_set, const Eigen::MatrixXd &solution, const Eigen::VectorXd &adjoint_sol, Eigen::VectorXd &term)
const double dmin_
Minimum distance between elements.
const ipc::BarrierPotential & barrier_potential() const
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.
std::string name() const override
Eigen::MatrixXd compute_displaced_surface(const Eigen::VectorXd &x) const
Compute the displaced positions of the surface nodes.
double barrier_stiffness() const
Get the current barrier stiffness.
void set_barrier_stiffness(const double barrier_stiffness)
Get the current barrier stiffness.
void post_step(const polysolve::nonlinear::PostStepData &data) override
Update fields after a step in the optimization.
virtual void second_derivative_unweighted(const Eigen::VectorXd &x, StiffnessMatrix &hessian) const override
Compute the second derivative of the value wrt x.
double max_step_size(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1) const override
Determine the maximum step size allowable between the current and next solution.
double barrier_stiffness_
Barrier stiffness.
double max_barrier_stiffness_
Maximum barrier stiffness to use when using adaptive barrier stiffness.
void solution_changed(const Eigen::VectorXd &new_x) override
Update cached fields upon a change in the solution.
double prev_distance_
Previous minimum distance between all elements.
double weight() const override
Get the form's multiplicative constant weight.
const bool enable_shape_derivatives_
Enable shape derivatives computation.
const ipc::BarrierPotential barrier_potential_
const ipc::BroadPhaseMethod broad_phase_method_
Broad phase method to use for distance and CCD evaluations.
void line_search_end() override
Clear 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.
virtual double value_unweighted(const Eigen::VectorXd &x) const override
Compute the contact barrier potential value.
const ipc::Collisions & collision_set() const
const ipc::CollisionMesh & collision_mesh_
Collision mesh.
void line_search_begin(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1) override
Initialize variables used during the line search.
bool use_adaptive_barrier_stiffness() const
Get use_adaptive_barrier_stiffness.
ipc::Candidates candidates_
Cached candidate set for the current solution.
const int ccd_max_iterations_
Continuous collision detection maximum iterations.
virtual void update_barrier_stiffness(const Eigen::VectorXd &x, const Eigen::MatrixXd &grad_energy)
Update the barrier stiffness based on the current elasticity energy.
void update_collision_set(const Eigen::MatrixXd &displaced_surface)
Update the cached candidate set for the current solution.
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 ccd_tolerance_
Continuous collision detection tolerance.
void update_quantities(const double t, const Eigen::VectorXd &x) override
Update time-dependent fields.
bool save_ccd_debug_meshes
If true, output debug files.
const double dhat_
Barrier activation distance.
bool is_step_collision_free(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1) const override
Checks if the step is collision free.
bool use_convergent_formulation() const
Get use_convergent_formulation.
const bool is_time_dependent_
Is the simulation time dependent?
double weight_
weight of the form (e.g., AL penalty weight or Δt²)
Definition Form.hpp:143
Eigen::SparseMatrix< double, Eigen::ColMajor > StiffnessMatrix
Definition Types.hpp:22