15#include "libmesh/sparse_matrix.h"
24 params.
addClassDescription(
"Evaluates surrogate models and maps the results back to a full "
25 "solution field for given variables.");
26 params.
addParam<std::vector<UserObjectName>>(
27 "surrogate", std::vector<UserObjectName>(),
"The names of the surrogates for each variable.");
29 "mapping",
"The name of the mapping object which provides the inverse mapping function.");
32 "The names of the variables that this object is supposed to populate with the "
33 "reconstructed results.");
35 "variable_to_reconstruct",
36 "The names of the variables in the nonlinear system which we would like to approximate. This "
37 "is important for DoF information.");
40 "The input parameters for the surrogate. If no surrogate is supplied these are assumed to be "
41 "the coordinates in the latent space.");
51 _var_names_to_fill(getParam<
std::vector<VariableName>>(
"variable_to_fill")),
52 _var_names_to_reconstruct(getParam<
std::vector<VariableName>>(
"variable_to_reconstruct")),
53 _surrogate_model_names(getParam<
std::vector<UserObjectName>>(
"surrogate")),
54 _input_parameters(getParam<
std::vector<Real>>(
"parameters"))
58 "The number of variables to fill should be the same as the number of entries in "
59 "`variable_to_reconstruct`");
64 "The number of surrogates should match the number of variables which need to be "
86 if (std::find(mapping_variable_names.begin(),
87 mapping_variable_names.end(),
91 "! Double check the training process and make sure that the mapping includes "
92 "the given variable!");
101 if (fe_type_reconstruct != fe_type_fill)
103 "The FEtype should match the ones defined for `variable_to_reconstruct`");
105 if (fe_type_reconstruct.family == SCALAR)
106 paramError(
"variable_to_fill",
"InverseMapping does not support SCALAR variables!");
119 std::unique_ptr<NumericVector<Number>> temporary_vector =
122 std::vector<Real> reduced_coefficients;
130 DenseVector<Real> reconstructed_solution;
145 dof_id_type local_dof_begin = dof_map.first_dof();
146 dof_id_type local_dof_end = dof_map.end_dof();
149 for (
const auto & dof_i : index_range(dofs))
150 if (dofs[dof_i] >= local_dof_begin && dofs[dof_i] < local_dof_end)
151 temporary_vector->set(dofs[dof_i], reconstructed_solution(dof_i));
157 unsigned int from_sys_num = var_to_reconstruct.
sys().
system().
number();
158 unsigned int from_var_num =
165 for (
const auto & node : to_mesh.local_node_ptr_range())
167 const auto n_dofs = node->n_dofs(to_sys_num, to_var_num);
173 for (
auto dof_i : make_range(n_dofs))
176 const auto & to_dof_id = node->dof_number(to_sys_num, to_var_num, dof_i);
177 const auto & from_dof_id = node->dof_number(from_sys_num, from_var_num, dof_i);
180 var_to_fill.
sys().
solution().
set(to_dof_id, (*temporary_vector)(from_dof_id));
188 for (
auto & elem : as_range(to_mesh.local_elements_begin(), to_mesh.local_elements_end()))
191 const auto n_dofs = elem->n_dofs(to_sys_num, to_var_num);
196 for (
auto dof_i : make_range(n_dofs))
199 const auto & to_dof_id = elem->dof_number(to_sys_num, to_var_num, dof_i);
200 const auto & from_dof_id = elem->dof_number(from_sys_num, from_var_num, dof_i);
202 var_to_fill.
sys().
solution().
set(to_dof_id, (*temporary_vector)(from_dof_id));
registerMooseObject("StochasticToolsApp", InverseMapping)
T evaluate(Real, const Point &)
virtual const MooseVariableFieldBase & getVariable(const THREAD_ID tid, const std::string &var_name, Moose::VarKindType expected_var_type=Moose::VarKindType::VAR_ANY, Moose::VarFieldType expected_var_field_type=Moose::VarFieldType::VAR_FIELD_ANY) const override
virtual MooseMesh & mesh() override
static InputParameters validParams()
A user object which takes a surrogate (or just user supplied values) to determine coordinates in a la...
const std::vector< VariableName > & _var_names_to_reconstruct
The names of the variables in the nonlinear system whose reconstruction we are working on.
void initialSetup() override
std::vector< SurrogateModel * > _surrogate_models
Links to the surrogate models which provide functions to determine the coordinates in the latent spac...
static InputParameters validParams()
std::vector< MooseVariableFieldBase * > _variable_to_reconstruct
Links to the MooseVariables from the nonlinear system whose dof numbering we need to populate the var...
InverseMapping(const InputParameters ¶ms)
std::vector< MooseVariableFieldBase * > _variable_to_fill
Links to the MooseVariables of the requested variables.
VariableMappingBase * _mapping
Link to the mapping object which provides the inverse mapping function.
const std::vector< VariableName > & _var_names_to_fill
The names of the variables which serve as a container for the reconstructed solution.
const std::vector< UserObjectName > & _surrogate_model_names
The names of the surrogate models for each variable.
const std::vector< Real > & _input_parameters
Input parameters for the surrogate models.
An interface class that helps getting access to Mapping objects.
VariableMappingBase & getMapping(const std::string &name) const
Get the mapping using the parameters of the moose object.
const std::string & name() const
void paramError(const std::string ¶m, Args... args) const
Interface for objects that need to use samplers.
T & getSurrogateModelByName(const UserObjectName &name) const
Get a sampler with a given name.
const std::vector< dof_id_type > & getVariableGlobalDoFs()
void setVariableGlobalDoFs(const std::string &var_name)
NumericVector< Number > & solution()
virtual libMesh::System & system()=0
FEProblemBase & _fe_problem
virtual const std::vector< VariableName > & getVariableNames()
Get the available variable names in this mapping.
virtual void inverse_map(const VariableName &vname, const std::vector< Real > &reduced_order_vector, DenseVector< Real > &full_order_vector) const =0
Map a reduced-order vector (from the latent space) back to a full-order solution vector (in DenseVect...
virtual void set(const numeric_index_type i, const T value)=0
virtual std::unique_ptr< NumericVector< T > > zero_clone() const=0
unsigned int variable_number(std::string_view var) const
const DofMap & get_dof_map() const
unsigned int number() const