15#include "libmesh/string_to_enum.h"
27 "Computes objective function, gradient and contains reporters for communicating between "
28 "optimizeSolve and subapps using mesh-based parameter definition.");
31 "parameter_meshes",
"Exodus file containing meshes describing parameters.");
38 "Specifies the family of FE shape functions for each group of parameters. If a single value "
40 "specified, then that value is used for all groups of parameters.");
46 "Specifies the order of FE shape functions for each group of parameters. If a single value "
48 "specified, then that value is used for all groups of parameters.");
51 "num_parameter_times", 1,
"The number of time points the parameters represent.");
53 params.
addParam<std::vector<std::string>>(
54 "initial_condition_mesh_variable",
55 "Name of variable on parameter mesh to use as initial condition.");
56 params.
addParam<std::vector<std::string>>(
57 "lower_bound_mesh_variable",
"Name of variable on parameter mesh to use as lower bound.");
58 params.
addParam<std::vector<std::string>>(
59 "upper_bound_mesh_variable",
"Name of variable on parameter mesh to use as upper bound.");
60 params.
addParam<std::vector<unsigned int>>(
61 "exodus_timesteps_for_parameter_mesh_variable",
62 "Timesteps to read all parameter group bounds and initial conditions from Exodus mesh. The "
63 "options are to give no timestep, a single timestep or \"num_parameter_times\" timesteps. "
64 "No timestep results in the final timestep from the mesh being used. A single timestep "
65 "results in values at that timestep being used for all timesteps. \"num_parameter_times\" "
66 "timesteps results in values from the mesh at those steps being used. The same timesteps "
67 "are used for all parameter groups and all meshes, the capability to define different "
68 "timesteps for different meshes is not supported.");
73 "regularization_types",
75 "Types of regularization to apply. Multiple types can be specified.");
77 params.
addParam<std::vector<Real>>(
"regularization_coeffs",
79 "Coefficients for each regularization type. Must match the "
80 "number of regularization_types specified.");
90 _regularization_coeffs(getParam<
std::vector<Real>>(
"regularization_coeffs")),
91 _regularization_types(getParam<
MultiMooseEnum>(
"regularization_types")
97 "Number of regularization coefficients (",
99 ") must match number of regularization types (",
106 const FileName mesh_file_name,
107 const std::vector<unsigned int> & exodus_timestep,
108 const std::string & mesh_var_name)
const
112 std::vector<Real> parsed_data;
114 for (
auto const & step : exodus_timestep)
117 parsed_data.insert(parsed_data.end(), data.begin(), data.end());
127 paramError(
"num_values_name or num_values should not be used with ParameterMeshOptimization. "
128 "Instead the number of dofs is set by the parameter meshes.");
132 const auto & meshes = getParam<std::vector<FileName>>(
"parameter_meshes");
133 const auto & families = getParam<MultiMooseEnum>(
"parameter_families");
134 const auto & orders = getParam<MultiMooseEnum>(
"parameter_orders");
135 const auto & ntimes = getParam<unsigned int>(
"num_parameter_times");
138 std::vector<std::string> initial_condition_mesh_variable;
139 std::vector<std::string> lower_bound_mesh_variable;
140 std::vector<std::string> upper_bound_mesh_variable;
142 initial_condition_mesh_variable =
143 getParam<std::vector<std::string>>(
"initial_condition_mesh_variable");
145 lower_bound_mesh_variable =
getParam<std::vector<std::string>>(
"lower_bound_mesh_variable");
147 upper_bound_mesh_variable =
getParam<std::vector<std::string>>(
"upper_bound_mesh_variable");
149 std::vector<unsigned int> exodus_timestep;
150 if (
isParamValid(
"exodus_timesteps_for_parameter_mesh_variable"))
152 getParam<std::vector<unsigned int>>(
"exodus_timesteps_for_parameter_mesh_variable");
154 exodus_timestep = {std::numeric_limits<unsigned int>::max()};
160 "There must be a mesh associated with each group of parameters.");
161 if (families.size() > 1 && families.size() !=
_nparams)
163 "There must be a family associated with each group of parameters.");
164 if (orders.size() > 1 && orders.size() !=
_nparams)
166 "There must be an order associated with each group of parameters.");
171 "Initial conditions for all parameter groups can only be defined by "
172 "initial_condition_mesh_variable or "
173 "initial_condition but not both.");
176 "lower_bound_mesh_variable",
177 "Lower bounds for all parameter groups can only be defined by lower_bound_mesh_variable or "
178 "lower_bounds but not both.");
181 "upper_bound_mesh_variable",
182 "Upper bounds for all parameter groups can only be defined by upper_bound_mesh_variable or "
183 "upper_bounds but not both.");
186 if (
isParamValid(
"exodus_timesteps_for_parameter_mesh_variable") &&
190 paramError(
"\"exodus_timesteps_for_parameter_mesh_variable\" should only be specified if "
191 "reading values from a mesh.");
192 else if (exodus_timestep.size() != ntimes && exodus_timestep.size() != 1)
193 paramError(
"exodus_timesteps_for_parameter_mesh_variable",
194 "Number of timesteps to read mesh data specified by "
195 "\"exodus_timesteps_for_parameter_mesh_variable\" incorrect. "
196 "\"exodus_timesteps_for_parameter_mesh_variable\" can specify a single timestep or "
197 "\"num_parameter_times\" timesteps.");
201 for (
const auto & param_id : make_range(
_nparams))
203 const std::string family = families.size() > 1 ? families[param_id] : families[0];
204 const std::string order = orders.size() > 1 ? orders[param_id] : orders[0];
205 const FEType fetype(Utility::string_to_enum<Order>(order),
206 Utility::string_to_enum<FEFamily>(family));
208 _parameter_meshes[param_id] = std::make_unique<ParameterMesh>(fetype, meshes[param_id]);
217 fetype, meshes[param_id], exodus_timestep, initial_condition_mesh_variable[param_id]);
226 std::vector<Real> lower_bound;
229 fetype, meshes[param_id], exodus_timestep, lower_bound_mesh_variable[param_id]);
231 lower_bound =
parseInputData(
"lower_bounds", std::numeric_limits<Real>::lowest(), param_id);
238 std::vector<Real> upper_bound;
241 fetype, meshes[param_id], exodus_timestep, upper_bound_mesh_variable[param_id]);
243 upper_bound =
parseInputData(
"upper_bounds", std::numeric_limits<Real>::max(), param_id);
263 Real regularization_value = 0.0;
268 for (
const auto & param_id : make_range(
_nparams))
271 const auto & param_values = *
_parameters[param_id];
274 regularization_value +=
275 _parameter_meshes[param_id]->computeRegularizationObjective(param_values, reg_type);
296 for (
const auto & param_id : make_range(
_nparams))
299 const auto & param_values = *
_parameters[param_id];
303 std::vector<Real> reg_grad =
304 _parameter_meshes[param_id]->computeRegularizationGradient(param_values, reg_type);
307 for (
unsigned int i = 0; i < param_values.size(); ++i)
registerMooseObject("OptimizationApp", ParameterMeshOptimization)
static MooseEnum getNonlinearVariableFamilies()
static MooseEnum getNonlinearVariableOrders()
Optimization reporter that interfaces with TAO.
virtual Real computeObjective() override
Function to compute objective.
static InputParameters validParams()
void paramError(const std::string ¶m, Args... args) const
const T & getParam(const std::string &name) const
bool isParamValid(const std::string &name) const
std::string getRawNames() const
std::vector< Real > parseInputData(std::string type, Real default_value, unsigned int param_id) const
Function to to parse bounds and initial conditions from input file.
const unsigned int _nparams
Number of parameter vectors.
std::vector< std::vector< Real > * > _gradients
Gradient values declared as reporter data.
virtual void computeGradient(libMesh::PetscVector< Number > &gradient) const
Function to compute gradient.
std::vector< Real > _lower_bounds
Bounds of the parameters.
dof_id_type _ndof
Total number of parameters.
std::vector< std::vector< Real > * > _parameters
Parameter values declared as reporter data.
std::vector< Real > _upper_bounds
std::vector< dof_id_type > _nvalues
Number of values for each parameter.
Mesh-based parameter optimization.
const std::vector< Real > _regularization_coeffs
Vector of regularization coefficients corresponding to each type.
ParameterMeshOptimization(const InputParameters ¶meters)
const std::vector< ParameterMesh::RegularizationType > _regularization_types
Regularization types to apply.
std::vector< Real > parseExodusData(const FEType fetype, const FileName mesh_file_name, const std::vector< unsigned int > &exodus_timestep, const std::string &mesh_var_name) const
Read initialization data off of parameter mesh and error check.
virtual void setICsandBounds() override
Sets the initial conditions and bounds right before it is needed.
std::vector< std::unique_ptr< ParameterMesh > > _parameter_meshes
Store parameter meshes for regularization computation.
virtual Real computeObjective() override
Function to compute objective.
static InputParameters validParams()
virtual void computeGradient(libMesh::PetscVector< Number > &gradient) const override
Function to compute gradient.
Utility class to use an Exodus mesh to define controllable parameters for optimization problems This ...
RegularizationType
Enumerations for regularization computations.
Utility function to read a single variable off an Exodus mesh for optimization problem This class wil...
std::vector< Real > getParameterValues(const unsigned int timestep) const
Initializes parameter data and sets bounds in the main optmiization application getParameterValues is...
Number initial_condition(const Point &p, const Parameters ¶meters, const std::string &, const std::string &)