17 return std::make_unique<ShapeVariableToSimulation>(states, parametrization);
18 else if (type ==
"elastic")
19 return std::make_unique<ElasticVariableToSimulation>(states, parametrization);
20 else if (type ==
"friction")
21 return std::make_unique<FrictionCoeffientVariableToSimulation>(states, parametrization);
22 else if (type ==
"damping")
23 return std::make_unique<DampingCoeffientVariableToSimulation>(states, parametrization);
24 else if (type ==
"initial")
25 return std::make_unique<InitialConditionVariableToSimulation>(states, parametrization);
26 else if (type ==
"dirichlet")
27 return std::make_unique<DirichletVariableToSimulation>(states, parametrization);
28 else if (type ==
"pressure")
29 return std::make_unique<PressureVariableToSimulation>(states, parametrization);
30 else if (type ==
"periodic-shape")
31 return std::make_unique<PeriodicShapeVariableToSimulation>(states, parametrization);
34 return std::unique_ptr<VariableToSimulation>();
39 const std::string composite_map_type = args[
"composite_map_type"];
41 if (composite_map_type ==
"none")
45 else if (composite_map_type ==
"indices")
47 if (args[
"composite_map_indices"].is_string())
53 else if (args[
"composite_map_indices"].is_array())
70 ind.setLinSpaced(out_size, 0, out_size - 1);
75 return Eigen::VectorXi();
86 return Eigen::VectorXd();
96 std::vector<ValueType>().swap(
L);
97 for (
const auto &arg : args)
103 Eigen::VectorXd adjoint_term = Eigen::VectorXd::Zero(
x.size());
104 for (
const auto &v2s :
L)
105 adjoint_term += v2s->compute_adjoint_term(
x);
111 for (
const auto &v2s :
L)
113 if (v2s->get_parameter_type() != type)
116 const Eigen::VectorXd var = v2s->get_parametrization().eval(
x);
117 for (
const auto &state : v2s->get_states())
119 if (state.get() != state_ptr)
122 state_variable(v2s->get_output_indexing(
x)) = var;
129 Eigen::VectorXd gradv = Eigen::VectorXd::Zero(
x.size());
130 for (
const auto &v2s :
L)
132 if (v2s->get_parameter_type() != type)
135 for (
const auto &state : v2s->get_states())
137 if (state.get() != state_ptr)
140 gradv += v2s->apply_parametrization_jacobian(grad(),
x);
150 const int dim = state->mesh->dimension();
153 for (
int i = 0; i < indices.size(); i += dim)
154 for (
int j = 0; j < dim; j++)
155 assert(indices(i + j) == indices(i) + j);
157 for (
int i = 0; i < indices.size(); i += dim)
158 state->set_mesh_vertex(indices(i) / dim, state_variable(Eigen::seqN(i, dim)));
163 Eigen::VectorXd term, cur_term;
166 if (state->problem->is_time_dependent())
170 if (!state->is_homogenization())
176 if (term.size() != cur_term.size())
185 const int dim =
states_[0]->mesh->dimension();
186 const int npts =
states_[0]->mesh->n_vertices();
191 if (indices.size() == 0)
192 indices.setLinSpaced(npts * dim, 0, npts * dim - 1);
196 if (indices.maxCoeff() >=
V.size())
204 const std::string composite_map_type = args[
"composite_map_type"];
206 if (composite_map_type ==
"interior")
211 else if (composite_map_type ==
"boundary")
216 else if (composite_map_type ==
"boundary_excluding_surface")
218 const std::vector<int> excluded_surfaces = args[
"surface_selection"];
230 const int n_elem = state->bases.size();
231 assert(n_elem * 2 == state_variable.size());
232 state->assembler->update_lame_params(state_variable.segment(0, n_elem), state_variable.segment(n_elem, n_elem));
237 Eigen::VectorXd term, cur_term;
240 if (state->problem->is_time_dependent())
245 if (term.size() != cur_term.size())
255 auto params_map = state.assembler->parameters();
257 auto search_lambda = params_map.find(
"lambda");
258 auto search_mu = params_map.find(
"mu");
259 if (search_lambda == params_map.end() || search_mu == params_map.end())
262 return Eigen::VectorXd();
265 Eigen::VectorXd lambdas(state.mesh->n_elements());
266 Eigen::VectorXd mus(state.mesh->n_elements());
267 for (
int e = 0; e < state.mesh->n_elements(); e++)
270 if (!state.mesh->is_volume())
272 const auto &mesh2d = *
dynamic_cast<mesh::Mesh2D *
>(state.mesh.get());
277 const auto &mesh3d = *
dynamic_cast<mesh::Mesh3D *
>(state.mesh.get());
280 lambdas(e) = search_lambda->second(RowVectorNd::Zero(state.mesh->dimension()), barycenter, 0., e);
281 mus(e) = search_mu->second(RowVectorNd::Zero(state.mesh->dimension()), barycenter, 0., e);
283 state.assembler->update_lame_params(lambdas, mus);
285 Eigen::VectorXd params(lambdas.size() + mus.size());
286 params << lambdas, mus;
293 assert(state_variable.size() == 1);
294 assert(state_variable(0) >= 0);
296 state->args[
"contact"][
"friction_coefficient"] = state_variable(0);
300 Eigen::VectorXd term, cur_term;
303 if (state->problem->is_time_dependent())
308 if (term.size() != cur_term.size())
318 return Eigen::VectorXd();
323 assert(state_variable.size() == 2);
324 json damping_param = {
325 {
"psi", state_variable(0)},
326 {
"phi", state_variable(1)},
330 if (!state->args[
"materials"].is_array())
332 state->args[
"materials"][
"psi"] = damping_param[
"psi"];
333 state->args[
"materials"][
"phi"] = damping_param[
"phi"];
337 for (
auto &arg : state->args[
"materials"])
339 arg[
"psi"] = damping_param[
"psi"];
340 arg[
"phi"] = damping_param[
"phi"];
344 if (state->damping_assembler)
345 state->damping_assembler->add_multimaterial(0, damping_param, state->units);
347 logger().info(
"[{}] Current params: {}, {}",
name(), state_variable(0), state_variable(1));
351 Eigen::VectorXd term, cur_term;
354 if (state->problem->is_time_dependent())
359 if (term.size() != cur_term.size())
369 return Eigen::VectorXd();
376 if (state_variable.size() != state->ndof() * 2)
378 log_and_throw_adjoint_error(
"[{}] Inconsistent number of parameters {} and number of dofs in forward {}!",
name(), state_variable.size(), state->ndof() * 2);
380 state->initial_sol_update = state_variable.head(state->ndof());
381 state->initial_vel_update = state_variable.tail(state->ndof());
386 Eigen::VectorXd term, cur_term;
389 if (state->problem->is_time_dependent())
394 if (term.size() != cur_term.size())
404 Eigen::MatrixXd sol, vel;
405 state.initial_solution(sol);
406 state.initial_velocity(vel);
408 Eigen::VectorXd
x(sol.size() + vel.size());
415 auto tensor_problem = std::dynamic_pointer_cast<polyfem::assembler::GenericTensorProblem>(
states_[0]->problem);
417 int dim =
states_[0]->mesh->dimension();
418 int num_steps = indices.size() / dim;
419 for (
int i = 0; i < num_steps; ++i)
421 tensor_problem->update_dirichlet_boundary(b, indices(i * dim) + 1, state_variable.segment(i * dim, dim));
428 Eigen::VectorXd term, cur_term;
431 if (state->problem->is_time_dependent())
436 if (term.size() != cur_term.size())
452 int dim =
states_[0]->mesh->dimension();
454 for (
const auto &b :
states_[0]->args[
"boundary_conditions"][
"dirichlet_boundary"])
457 auto value = b[
"value"];
458 if (value[0].is_array())
460 if (!
states_[0]->problem->is_time_dependent())
462 Eigen::MatrixXd dirichlet = value;
463 x.setZero(dirichlet.rows() * (dirichlet.cols() - 1));
464 for (
int j = 1; j < dirichlet.cols(); ++j)
465 x.segment((j - 1) * dim, dim) = dirichlet.col(j);
467 else if (value[0].is_number())
469 if (
states_[0]->problem->is_time_dependent())
474 else if (value.is_string())
483 const std::string composite_map_type = args[
"composite_map_type"];
485 if (composite_map_type ==
"time_step_indexing")
487 const int time_steps = state.
args[
"time"][
"time_steps"];
488 const int dim = state.
mesh->dimension();
491 for (
int i = 0; i < time_steps; ++i)
492 for (
int k = 0; k < dim; ++k)
503 auto tensor_problem = std::dynamic_pointer_cast<polyfem::assembler::GenericTensorProblem>(
states_[0]->problem);
505 for (
int i = 0; i < indices.size(); ++i)
507 tensor_problem->update_pressure_boundary(b, indices(i) + 1, state_variable(i));
514 Eigen::VectorXd term, cur_term;
517 if (state->problem->is_time_dependent())
519 Eigen::MatrixXd adjoint_nu, adjoint_p;
520 adjoint_nu = state->get_adjoint_mat(1);
521 adjoint_p = state->get_adjoint_mat(0);
528 if (term.size() != cur_term.size())
547 for (
const auto &b :
states_[0]->args[
"boundary_conditions"][
"pressure_boundary"])
550 auto value = b[
"value"];
551 if (value.is_array())
553 if (!
states_[0]->problem->is_time_dependent())
555 Eigen::VectorXd pressures = value;
556 x = pressures.segment(1, pressures.size() - 1);
558 else if (value.is_number())
560 if (
states_[0]->problem->is_time_dependent())
565 else if (value.is_string())
575 const std::string composite_map_type = args[
"composite_map_type"];
577 if (composite_map_type ==
"time_step_indexing")
579 const int time_steps = state.
args[
"time"][
"time_steps"];
581 for (
int i = 0; i < time_steps; ++i)
592 Eigen::VectorXd term, cur_term;
595 if (state->problem->is_time_dependent())
603 if (term.size() != cur_term.size())
612 const int dim =
states_[0]->mesh->dimension();
618 const int n_verts = state->mesh->n_vertices();
620 for (
int i = 0; i < n_verts; i++)
621 state->set_mesh_vertex(i,
V.row(i));
626 const auto &state = *(
states_[0]);
629 state.get_vertices(
V);
631 if (!state.periodic_bc->all_direction_periodic())
main class that contains the polyfem solver and all its state
std::unique_ptr< mesh::Mesh > mesh
current mesh, it can be a Mesh2D or Mesh3D
json args
main input arguments containing all defaults
std::string resolve_input_path(const std::string &path, const bool only_if_exists=false) const
Resolve input path relative to root_path() if the path is not absolute.
RowVectorNd face_barycenter(const int index) const override
face barycenter
virtual RowVectorNd cell_barycenter(const int c) const =0
cell barycenter
int size(const int x_size) const override
Eigen::VectorXd apply_jacobian(const Eigen::VectorXd &grad_full, const Eigen::VectorXd &x) const override
Eigen::VectorXd inverse_eval(const Eigen::VectorXd &y) override
Eigen::VectorXd eval(const Eigen::VectorXd &x) const override
Eigen::VectorXd compute_adjoint_term(const Eigen::VectorXd &x) const override
Eigen::VectorXd inverse_eval() override
void update_state(const Eigen::VectorXd &state_variable, const Eigen::VectorXi &indices) override
std::string name() const override
Eigen::VectorXd compute_adjoint_term(const Eigen::VectorXd &x) const override
std::string variable_to_string(const Eigen::VectorXd &variable)
std::vector< int > dirichlet_boundaries_
std::string name() const override
void update_state(const Eigen::VectorXd &state_variable, const Eigen::VectorXi &indices) override
Eigen::VectorXd inverse_eval() override
void set_dirichlet_boundaries(const std::vector< int > &dirichlet_boundaries)
void set_output_indexing(const json &args) override
std::string name() const override
Eigen::VectorXd compute_adjoint_term(const Eigen::VectorXd &x) const override
void update_state(const Eigen::VectorXd &state_variable, const Eigen::VectorXi &indices) override
Eigen::VectorXd inverse_eval() override
Eigen::VectorXd compute_adjoint_term(const Eigen::VectorXd &x) const override
std::string name() const override
Eigen::VectorXd inverse_eval() override
void update_state(const Eigen::VectorXd &state_variable, const Eigen::VectorXi &indices) override
Eigen::VectorXd inverse_eval() override
Eigen::VectorXd compute_adjoint_term(const Eigen::VectorXd &x) const override
std::string name() const override
void update_state(const Eigen::VectorXd &state_variable, const Eigen::VectorXi &indices) override
void update(const Eigen::VectorXd &x) override
Eigen::VectorXd compute_adjoint_term(const Eigen::VectorXd &x) const override
Eigen::VectorXd apply_parametrization_jacobian(const Eigen::VectorXd &term, const Eigen::VectorXd &x) const override
Eigen::VectorXd periodic_mesh_representation
Eigen::VectorXd inverse_eval() override
std::unique_ptr< PeriodicMeshToMesh > periodic_mesh_map
std::vector< int > pressure_boundaries_
void set_pressure_boundaries(const std::vector< int > &pressure_boundaries)
Eigen::VectorXd inverse_eval() override
std::string variable_to_string(const Eigen::VectorXd &variable)
void update_state(const Eigen::VectorXd &state_variable, const Eigen::VectorXi &indices) override
void set_output_indexing(const json &args) override
Eigen::VectorXd compute_adjoint_term(const Eigen::VectorXd &x) const override
virtual void update_state(const Eigen::VectorXd &state_variable, const Eigen::VectorXi &indices) override
void set_output_indexing(const json &args) override
Eigen::VectorXd compute_adjoint_term(const Eigen::VectorXd &x) const override
Eigen::VectorXd inverse_eval() override
const Eigen::VectorXi & get_output_indexing() const
void compute_state_variable(const ParameterType type, const State *state_ptr, const Eigen::VectorXd &x, Eigen::VectorXd &state_variable) const
Evaluate the variable to simulations and overwrite the state_variable based on x.
Eigen::VectorXd compute_adjoint_term(const Eigen::VectorXd &x) const
Computes the sum of adjoint terms for all VariableToSimulation.
virtual Eigen::VectorXd apply_parametrization_jacobian(const ParameterType type, const State *state_ptr, const Eigen::VectorXd &x, const std::function< Eigen::VectorXd()> &grad) const
Maps the partial gradient wrt.
void init(const json &args, const std::vector< std::shared_ptr< State > > &states, const std::vector< int > &variable_sizes)
std::vector< ValueType > L
virtual Eigen::VectorXd apply_parametrization_jacobian(const Eigen::VectorXd &term, const Eigen::VectorXd &x) const
CompositeParametrization parametrization_
virtual Eigen::VectorXd inverse_eval()
const std::vector< std::shared_ptr< State > > states_
virtual void update_state(const Eigen::VectorXd &state_variable, const Eigen::VectorXi &indices)
virtual void set_output_indexing(const json &args)
virtual std::string name() const =0
Eigen::VectorXi get_output_indexing(const Eigen::VectorXd &x) const
static std::unique_ptr< VariableToSimulation > create(const std::string &type, const std::vector< std::shared_ptr< State > > &states, CompositeParametrization &¶metrization)
Eigen::VectorXi output_indexing_
bool read_matrix(const std::string &path, Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &mat)
Reads a matrix from a file. Determines the file format based on the path's extension.
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 & logger()
Retrieves the current logger.
void log_and_throw_adjoint_error(const std::string &msg)
Eigen::Matrix< double, 1, Eigen::Dynamic, Eigen::RowMajor, 1, 3 > RowVectorNd
void log_and_throw_error(const std::string &msg)
static std::unique_ptr< VariableToSimulation > create_variable_to_simulation(const json &args, const std::vector< std::shared_ptr< State > > &states, const std::vector< int > &variable_sizes)