PolyFEM
Loading...
Searching...
No Matches
AMIPSForm.cpp
Go to the documentation of this file.
2
4
7
15
16namespace polyfem::solver
17{
18 namespace
19 {
20 void scaled_jacobian(const Eigen::MatrixXd &V, const Eigen::MatrixXi &F, Eigen::VectorXd &quality)
21 {
22 const int dim = F.cols() - 1;
23
24 quality.setZero(F.rows());
25 if (dim == 2)
26 {
27 for (int i = 0; i < F.rows(); i++)
28 {
29 Eigen::RowVector3d e0;
30 e0(2) = 0;
31 e0.head(2) = V.row(F(i, 2)) - V.row(F(i, 1));
32 Eigen::RowVector3d e1;
33 e1(2) = 0;
34 e1.head(2) = V.row(F(i, 0)) - V.row(F(i, 2));
35 Eigen::RowVector3d e2;
36 e2(2) = 0;
37 e2.head(2) = V.row(F(i, 1)) - V.row(F(i, 0));
38
39 double l0 = e0.norm();
40 double l1 = e1.norm();
41 double l2 = e2.norm();
42
43 double A = 0.5 * (e0.cross(e1)).norm();
44 double Lmax = std::max(l0 * l1, std::max(l1 * l2, l0 * l2));
45
46 quality(i) = 2 * A * (2 / sqrt(3)) / Lmax;
47 }
48 }
49 else
50 {
51 for (int i = 0; i < F.rows(); i++)
52 {
53 Eigen::RowVector3d e0 = V.row(F(i, 1)) - V.row(F(i, 0));
54 Eigen::RowVector3d e1 = V.row(F(i, 2)) - V.row(F(i, 1));
55 Eigen::RowVector3d e2 = V.row(F(i, 0)) - V.row(F(i, 2));
56 Eigen::RowVector3d e3 = V.row(F(i, 3)) - V.row(F(i, 0));
57 Eigen::RowVector3d e4 = V.row(F(i, 3)) - V.row(F(i, 1));
58 Eigen::RowVector3d e5 = V.row(F(i, 3)) - V.row(F(i, 2));
59
60 double l0 = e0.norm();
61 double l1 = e1.norm();
62 double l2 = e2.norm();
63 double l3 = e3.norm();
64 double l4 = e4.norm();
65 double l5 = e5.norm();
66
67 double J = std::abs((e0.cross(e3)).dot(e2));
68
69 double a1 = l0 * l2 * l3;
70 double a2 = l0 * l1 * l4;
71 double a3 = l1 * l2 * l5;
72 double a4 = l3 * l4 * l5;
73
74 double a = std::max({a1, a2, a3, a4, J});
75 quality(i) = J * sqrt(2) / a;
76 }
77 }
78 }
79 } // namespace
80
81 double MinJacobianForm::value_unweighted(const Eigen::VectorXd &x) const
82 {
83 const bool is_volume = state_->mesh->is_volume();
84 double min_jacs = std::numeric_limits<double>::max();
85 for (size_t e = 0; e < state_->geom_bases().size(); ++e)
86 {
87 if (state_->mesh->is_polytope(e))
88 continue;
89
90 const auto &gbasis = state_->geom_bases()[e];
91 const int n_local_bases = int(gbasis.bases.size());
92
94 gbasis.compute_quadrature(quad);
95
96 std::vector<assembler::AssemblyValues> tmp;
97
98 Eigen::MatrixXd dx = Eigen::MatrixXd::Zero(quad.points.rows(), quad.points.cols());
99 Eigen::MatrixXd dy = Eigen::MatrixXd::Zero(quad.points.rows(), quad.points.cols());
100 Eigen::MatrixXd dz;
101 if (is_volume)
102 dz = Eigen::MatrixXd::Zero(quad.points.rows(), quad.points.cols());
103
104 gbasis.evaluate_grads(quad.points, tmp);
105
106 for (int j = 0; j < n_local_bases; ++j)
107 {
108 const basis::Basis &b = gbasis.bases[j];
109
110 for (std::size_t ii = 0; ii < b.global().size(); ++ii)
111 {
112 dx += tmp[j].grad.col(0) * b.global()[ii].node * b.global()[ii].val;
113 dy += tmp[j].grad.col(1) * b.global()[ii].node * b.global()[ii].val;
114 if (is_volume)
115 dz += tmp[j].grad.col(2) * b.global()[ii].node * b.global()[ii].val;
116 }
117 }
118
119 for (long i = 0; i < dx.rows(); ++i)
120 {
121 if (is_volume)
122 {
123 Eigen::Matrix3d tmp;
124 tmp << dx.row(i), dy.row(i), dz.row(i);
125 min_jacs = std::min(min_jacs, tmp.determinant());
126 }
127 else
128 {
129 Eigen::Matrix2d tmp;
130 tmp << dx.row(i), dy.row(i);
131 min_jacs = std::min(min_jacs, tmp.determinant());
132 }
133 }
134 }
135
136 return min_jacs;
137 }
138
139 void MinJacobianForm::compute_partial_gradient(const Eigen::VectorXd &x, Eigen::VectorXd &gradv) const
140 {
141 log_and_throw_adjoint_error("{} is not differentiable!", name());
142 }
143
144 AMIPSForm::AMIPSForm(const VariableToSimulationGroup &variable_to_simulation, std::shared_ptr<const legacy::State> state)
145 : AdjointForm(variable_to_simulation),
146 state_(std::move(state))
147 {
149 amips_energy_->set_size(state_->mesh->dimension());
150
151 json use_rest = {};
152 use_rest["use_rest_pose"] = true;
153 amips_energy_->add_multimaterial(0, use_rest, state_->units, state_->root_path());
154
155 Eigen::MatrixXd V;
156 state_->get_vertices(V);
157 state_->get_elements(F);
159 init_geom_bases_ = state_->geom_bases();
160 }
161
162 double AMIPSForm::value_unweighted(const Eigen::VectorXd &x) const
163 {
164 Eigen::VectorXd X = get_updated_mesh_nodes(x);
165
166 return amips_energy_->assemble_energy(state_->mesh->is_volume(), init_geom_bases_, init_geom_bases_, init_ass_vals_cache_, 0, 0, AdjointTools::map_primitive_to_node_order(*state_, X - X_rest), Eigen::VectorXd());
167 }
168
169 void AMIPSForm::compute_partial_gradient(const Eigen::VectorXd &x, Eigen::VectorXd &gradv) const
170 {
172 const Eigen::VectorXd X = get_updated_mesh_nodes(x);
173 Eigen::MatrixXd grad;
174 amips_energy_->assemble_gradient(state_->mesh->is_volume(), state_->n_geom_bases, init_geom_bases_, init_geom_bases_, init_ass_vals_cache_, 0, 0, AdjointTools::map_primitive_to_node_order(*state_, X - X_rest), Eigen::VectorXd(), grad); // grad wrt. gbases
176 });
177 }
178
179 bool AMIPSForm::is_step_valid(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1) const
180 {
181 Eigen::VectorXd X = get_updated_mesh_nodes(x1);
182 Eigen::MatrixXd V1 = utils::unflatten(X, state_->mesh->dimension());
183 bool flipped = utils::is_flipped(V1, F);
184
185 if (flipped)
186 adjoint_logger().trace("[{}] Step flips elements.", name());
187
188 return !flipped;
189 }
190} // namespace polyfem::solver
int V
int x
static std::shared_ptr< Assembler > make_assembler(const std::string &formulation)
Represents one basis function and its gradient.
Definition Basis.hpp:44
double value_unweighted(const Eigen::VectorXd &x) const override
Compute the value of the form.
virtual std::string name() const override
Definition AMIPSForm.hpp:49
bool is_step_valid(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1) const override
Determine if a step from solution x0 to solution x1 is allowed.
Eigen::VectorXd get_updated_mesh_nodes(const Eigen::VectorXd &x) const
Definition AMIPSForm.hpp:56
std::vector< polyfem::basis::ElementBases > init_geom_bases_
Definition AMIPSForm.hpp:67
void compute_partial_gradient(const Eigen::VectorXd &x, Eigen::VectorXd &gradv) const override
std::shared_ptr< assembler::Assembler > amips_energy_
Definition AMIPSForm.hpp:70
assembler::AssemblyValsCache init_ass_vals_cache_
Definition AMIPSForm.hpp:68
AMIPSForm(const VariableToSimulationGroup &variable_to_simulation, std::shared_ptr< const legacy::State > state)
std::shared_ptr< const legacy::State > state_
Definition AMIPSForm.hpp:63
const VariableToSimulationGroup variable_to_simulations_
virtual double weight() const
Get the form's multiplicative constant weight.
Definition Form.hpp:128
virtual std::string name() const override
Definition AMIPSForm.hpp:35
void compute_partial_gradient(const Eigen::VectorXd &x, Eigen::VectorXd &gradv) const override
double value_unweighted(const Eigen::VectorXd &x) const override
Compute the value of the form.
Definition AMIPSForm.cpp:81
std::shared_ptr< const legacy::State > state_
Definition AMIPSForm.hpp:41
Eigen::VectorXd apply_parametrization_jacobian(ParameterType type, const legacy::State &target, const Eigen::VectorXd &x, const std::function< Eigen::VectorXd()> &grad) const
Compute parametrization jacobian for all var2sim matching parameter type and output to target state.
int norm
Definition p_bases.py:265
bool scaled_jacobian(Mesh3DStorage &hmi, Mesh_Quality &mq)
Eigen::VectorXd map_primitive_to_node_order(const legacy::State &state, const Eigen::VectorXd &primitives)
Eigen::VectorXd map_node_to_primitive_order(const legacy::State &state, const Eigen::VectorXd &nodes)
bool is_flipped(const Eigen::MatrixXd &V, const Eigen::MatrixXi &F)
Determine if any simplex is inverted or collapses.
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 & adjoint_logger()
Retrieves the current logger for adjoint.
Definition Logger.cpp:30
nlohmann::json json
Definition Common.hpp:9
void log_and_throw_adjoint_error(const std::string &msg)
Definition Logger.cpp:79