PolyFEM
Loading...
Searching...
No Matches
PeriodicShapeVariableToSimulation.cpp
Go to the documentation of this file.
2
3#include <polyfem/Common.hpp>
8
9#include <Eigen/Core>
10
11#include <cassert>
12#include <string>
13#include <utility>
14
15namespace polyfem::solver
16{
18 StatePtrs states,
19 DiffCachePtrs diff_caches,
20 CompositeParametrization parametrizations)
21 : dim_(states[0]->mesh->dimension()),
22 vertex_num_(states[0]->mesh->n_vertices()),
23 states_(std::move(states)),
24 diff_caches_(std::move(diff_caches)),
25 parametrization_(std::move(parametrizations))
26 {
27 assert(!states_.empty());
28 assert(states_.size() == diff_caches_.size());
29
30 for (const auto &s : states_)
31 {
32 if (s->mesh->dimension() != dim_)
33 {
34 log_and_throw_adjoint_error("Fail to construct periodic shape variable to simulation. Reason: mesh dimension mismatch between states.");
35 }
36 if (s->mesh->n_vertices() != vertex_num_)
37 {
38 log_and_throw_adjoint_error("Fail to construct periodic shape variable to simulation. Reason: mesh vertex num mismatch between states.");
39 }
40 if (s->problem->is_time_dependent())
41 {
42 log_and_throw_adjoint_error("Fail to construct periodic shape variable to simulation. Reason: transient simulations are not supported.");
43 }
44 if (!s->has_periodic_bc() || s->periodic_bc == nullptr)
45 {
46 log_and_throw_adjoint_error("Fail to construct periodic shape variable to simulation. Reason: periodic boundary conditions are not enabled.");
47 }
48 if (!s->periodic_bc->all_direction_periodic())
49 {
50 log_and_throw_adjoint_error("Fail to construct periodic shape variable to simulation. Reason: partial periodicity is not supported.");
51 }
52 if (!s->is_homogenization())
53 {
54 log_and_throw_adjoint_error("Fail to construct periodic shape variable to simulation. Reason: only homogenization problems are supported.");
55 }
56 }
57
58 Eigen::MatrixXd V;
59 states_[0]->get_vertices(V);
60 periodic_mesh_map_ = std::make_unique<PeriodicMeshToMesh>(V);
61 }
62
64 {
65 return "periodic-shape";
66 }
67
72
74 {
75 for (auto &s : states_)
76 {
77 if (s.get() == &target)
78 return true;
79 }
80 return false;
81 }
82
83 void PeriodicShapeVariableToSimulation::update(const Eigen::VectorXd &x)
84 {
85 Eigen::VectorXd y = parametrization_.eval(x);
86 assert(y.size() == para_out_dof());
87
88 Eigen::MatrixXd V = utils::unflatten(periodic_mesh_map_->eval(y), dim_);
89
90 for (auto &s : states_)
91 {
92 for (int i = 0; i < vertex_num_; ++i)
93 s->mesh->set_point(i, V.row(i));
94 }
95 }
96
97 void PeriodicShapeVariableToSimulation::update_state_variables(const Eigen::VectorXd &x, Eigen::VectorXd &state_variables) const
98 {
99 assert(state_variables.size() == para_out_dof());
100 state_variables = parametrization_.eval(x);
101 }
102
103 Eigen::VectorXd PeriodicShapeVariableToSimulation::compute_adjoint_term(const Eigen::VectorXd &x) const
104 {
105 Eigen::VectorXd y = parametrization_.eval(x);
106 assert(y.size() == para_out_dof());
107
108 Eigen::VectorXd term, cur_term;
109 for (int i = 0; i < states_.size(); ++i)
110 {
111 auto &state = states_[i];
112 auto &diff_cache = diff_caches_[i];
113
114 Eigen::MatrixXd adjoint_p = get_adjoint_mat(*state, *diff_cache, 0);
115
117 *state,
118 *diff_cache,
120 y,
121 diff_cache->u(0),
122 adjoint_p,
123 cur_term);
124
125 if (term.size() != cur_term.size())
126 {
127 term = cur_term;
128 }
129 else
130 {
131 term += cur_term;
132 }
133 }
134
135 assert(term.size() == para_out_dof());
136 return parametrization_.apply_jacobian(term, x);
137 }
138
143
145 {
146 Eigen::MatrixXd V;
147 states_[0]->get_vertices(V);
148
149 Eigen::VectorXd y = periodic_mesh_map_->inverse_eval(utils::flatten(V));
151 }
152
153 Eigen::VectorXd PeriodicShapeVariableToSimulation::apply_parametrization_jacobian(const Eigen::VectorXd &term, const Eigen::VectorXd &x) const
154 {
155 assert(term.size() == vertex_num_ * dim_);
156
157 const Eigen::VectorXd y = parametrization_.eval(x);
158 assert(y.size() == para_out_dof());
159
160 const Eigen::VectorXd reduced_term = periodic_mesh_map_->apply_jacobian(term, y);
161 assert(reduced_term.size() == para_out_dof());
162
163 return parametrization_.apply_jacobian(reduced_term, x);
164 }
165
167 {
168 return periodic_mesh_map_->input_size();
169 }
170
171} // namespace polyfem::solver
int V
int y
int x
main class that contains the polyfem solver and all its state
Definition State.hpp:114
Eigen::VectorXd apply_jacobian(const Eigen::VectorXd &grad_full, const Eigen::VectorXd &x) const override
Apply jacobian for chain rule.
Eigen::VectorXd inverse_eval(const Eigen::VectorXd &y) const override
Eval x = f^-1 (y).
int inverse_size(int y_size) const override
Compute DOF of x given DOF of y.
Eigen::VectorXd eval(const Eigen::VectorXd &x) const override
Eval y = f(x).
void update(const Eigen::VectorXd &x) override
Update forward simulation states from optimization variables.
Eigen::VectorXd compute_adjoint_term(const Eigen::VectorXd &x) const override
Compute adjoint contribution of objective gradient.
void update_state_variables(const Eigen::VectorXd &x, Eigen::VectorXd &state_variables) const override
Update state variables from optimization variables.
Eigen::VectorXd apply_parametrization_jacobian(const Eigen::VectorXd &term, const Eigen::VectorXd &x) const override
Apply parametrization jacobian to compute the gradient w.r.t.
int inverse_dof() const override
Compute optimization variables dof.
PeriodicShapeVariableToSimulation(StatePtrs states, DiffCachePtrs diff_caches, CompositeParametrization parametrizations)
Construct PeriodicShapeVariableToSimulation.
bool affect_state(const legacy::State &target) const override
Return true if current var2sim maps to target state.
Eigen::VectorXd inverse_eval() const override
Compute optimization variables from forward simulation legacy::State.
std::vector< std::shared_ptr< legacy::State > > StatePtrs
void dJ_periodic_shape_adjoint_term(const legacy::State &state, const DiffCache &diff_cache, const PeriodicMeshToMesh &periodic_mesh_map, const Eigen::VectorXd &periodic_mesh_representation, const Eigen::MatrixXd &sol, const Eigen::MatrixXd &adjoint, Eigen::VectorXd &one_form)
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.
void log_and_throw_adjoint_error(const std::string &msg)
Definition Logger.cpp:79
Eigen::MatrixXd get_adjoint_mat(const legacy::State &state, const DiffCache &diff_cache, int type)
Get adjoint parameter nu or p.