15 #include "libmesh/string_to_enum.h" 29 "Computes objective function, gradient and contains reporters for communicating between " 30 "optimizeSolve and subapps using mesh-based parameter definition.");
33 "parameter_meshes",
"Exodus file containing meshes describing parameters.");
40 "Specifies the family of FE shape functions for each group of parameters. If a single value " 42 "specified, then that value is used for all groups of parameters.");
48 "Specifies the order of FE shape functions for each group of parameters. If a single value " 50 "specified, then that value is used for all groups of parameters.");
53 "num_parameter_times", 1,
"The number of time points the parameters represent.");
55 params.
addParam<std::vector<std::string>>(
56 "initial_condition_mesh_variable",
57 "Name of variable on parameter mesh to use as initial condition.");
58 params.
addParam<std::vector<std::string>>(
59 "lower_bound_mesh_variable",
"Name of variable on parameter mesh to use as lower bound.");
60 params.
addParam<std::vector<std::string>>(
61 "upper_bound_mesh_variable",
"Name of variable on parameter mesh to use as upper bound.");
62 params.
addParam<std::vector<unsigned int>>(
63 "exodus_timesteps_for_parameter_mesh_variable",
64 "Timesteps to read all parameter group bounds and initial conditions from Exodus mesh. The " 65 "options are to give no timestep, a single timestep or \"num_parameter_times\" timesteps. " 66 "No timestep results in the final timestep from the mesh being used. A single timestep " 67 "results in values at that timestep being used for all timesteps. \"num_parameter_times\" " 68 "timesteps results in values from the mesh at those steps being used. The same timesteps " 69 "are used for all parameter groups and all meshes, the capability to define different " 70 "timesteps for different meshes is not supported.");
75 "regularization_types",
77 "Types of regularization to apply. Multiple types can be specified.");
79 params.
addParam<std::vector<Real>>(
"regularization_coeffs",
81 "Coefficients for each regularization type. Must match the " 82 "number of regularization_types specified.");
92 _regularization_coeffs(getParam<
std::vector<
Real>>(
"regularization_coeffs")),
93 _regularization_types(getParam<
MultiMooseEnum>(
"regularization_types")
99 "Number of regularization coefficients (",
101 ") must match number of regularization types (",
108 const FileName mesh_file_name,
109 const std::vector<unsigned int> & exodus_timestep,
110 const std::string & mesh_var_name)
const 114 std::vector<Real> parsed_data;
116 for (
auto const & step : exodus_timestep)
119 parsed_data.insert(parsed_data.end(), data.begin(), data.end());
129 paramError(
"num_values_name or num_values should not be used with ParameterMeshOptimization. " 130 "Instead the number of dofs is set by the parameter meshes.");
134 const auto & meshes = getParam<std::vector<FileName>>(
"parameter_meshes");
135 const auto & families = getParam<MultiMooseEnum>(
"parameter_families");
136 const auto & orders = getParam<MultiMooseEnum>(
"parameter_orders");
137 const auto & ntimes = getParam<unsigned int>(
"num_parameter_times");
140 std::vector<std::string> initial_condition_mesh_variable;
141 std::vector<std::string> lower_bound_mesh_variable;
142 std::vector<std::string> upper_bound_mesh_variable;
144 initial_condition_mesh_variable =
145 getParam<std::vector<std::string>>(
"initial_condition_mesh_variable");
147 lower_bound_mesh_variable =
getParam<std::vector<std::string>>(
"lower_bound_mesh_variable");
149 upper_bound_mesh_variable =
getParam<std::vector<std::string>>(
"upper_bound_mesh_variable");
151 std::vector<unsigned int> exodus_timestep;
152 if (
isParamValid(
"exodus_timesteps_for_parameter_mesh_variable"))
154 getParam<std::vector<unsigned int>>(
"exodus_timesteps_for_parameter_mesh_variable");
156 exodus_timestep = {std::numeric_limits<unsigned int>::max()};
162 "There must be a mesh associated with each group of parameters.");
163 if (families.size() > 1 && families.size() !=
_nparams)
165 "There must be a family associated with each group of parameters.");
166 if (orders.size() > 1 && orders.size() !=
_nparams)
168 "There must be an order associated with each group of parameters.");
173 "Initial conditions for all parameter groups can only be defined by " 174 "initial_condition_mesh_variable or " 175 "initial_condition but not both.");
178 "lower_bound_mesh_variable",
179 "Lower bounds for all parameter groups can only be defined by lower_bound_mesh_variable or " 180 "lower_bounds but not both.");
183 "upper_bound_mesh_variable",
184 "Upper bounds for all parameter groups can only be defined by upper_bound_mesh_variable or " 185 "upper_bounds but not both.");
188 if (
isParamValid(
"exodus_timesteps_for_parameter_mesh_variable") &&
192 paramError(
"\"exodus_timesteps_for_parameter_mesh_variable\" should only be specified if " 193 "reading values from a mesh.");
194 else if (exodus_timestep.size() != ntimes && exodus_timestep.size() != 1)
195 paramError(
"exodus_timesteps_for_parameter_mesh_variable",
196 "Number of timesteps to read mesh data specified by " 197 "\"exodus_timesteps_for_parameter_mesh_variable\" incorrect. " 198 "\"exodus_timesteps_for_parameter_mesh_variable\" can specify a single timestep or " 199 "\"num_parameter_times\" timesteps.");
205 const std::string family = families.size() > 1 ? families[param_id] : families[0];
206 const std::string order = orders.size() > 1 ? orders[param_id] : orders[0];
207 const FEType fetype(Utility::string_to_enum<Order>(order),
208 Utility::string_to_enum<FEFamily>(family));
210 _parameter_meshes[param_id] = std::make_unique<ParameterMesh>(fetype, meshes[param_id]);
219 fetype, meshes[param_id], exodus_timestep, initial_condition_mesh_variable[param_id]);
228 std::vector<Real> lower_bound;
231 fetype, meshes[param_id], exodus_timestep, lower_bound_mesh_variable[param_id]);
233 lower_bound =
parseInputData(
"lower_bounds", std::numeric_limits<Real>::lowest(), param_id);
240 std::vector<Real> upper_bound;
243 fetype, meshes[param_id], exodus_timestep, upper_bound_mesh_variable[param_id]);
245 upper_bound =
parseInputData(
"upper_bounds", std::numeric_limits<Real>::max(), param_id);
265 Real regularization_value = 0.0;
273 const auto & param_values = *
_parameters[param_id];
276 regularization_value +=
277 _parameter_meshes[param_id]->computeRegularizationObjective(param_values, reg_type);
301 const auto & param_values = *
_parameters[param_id];
305 std::vector<Real> reg_grad =
306 _parameter_meshes[param_id]->computeRegularizationGradient(param_values, reg_type);
309 for (
unsigned int i = 0; i < param_values.size(); ++i)
std::vector< Real > getParameterValues(const unsigned int timestep) const
Initializes parameter data and sets bounds in the main optmiization application getParameterValues is...
virtual void computeGradient(libMesh::PetscVector< Number > &gradient) const override
Function to compute gradient.
RegularizationType
Enumerations for regularization computations.
virtual Real computeObjective() override
Function to compute objective.
Utility class to use an Exodus mesh to define controllable parameters for optimization problems This ...
void paramError(const std::string ¶m, Args... args) const
const T & getParam(const std::string &name) const
const std::vector< ParameterMesh::RegularizationType > _regularization_types
Regularization types to apply.
static InputParameters validParams()
Utility function to read a single variable off an Exodus mesh for optimization problem This class wil...
dof_id_type _ndof
Total number of parameters.
registerMooseObject("OptimizationApp", ParameterMeshOptimization)
The following methods are specializations for using the Parallel::packed_range_* routines for a vecto...
std::string getRawNames() const
std::vector< std::unique_ptr< ParameterMesh > > _parameter_meshes
Store parameter meshes for regularization computation.
static MooseEnum getNonlinearVariableFamilies()
const unsigned int _nparams
Number of parameter vectors.
static MooseEnum getNonlinearVariableOrders()
ParameterMeshOptimization(const InputParameters ¶meters)
std::vector< dof_id_type > _nvalues
Number of values for each parameter.
Number initial_condition(const Point &p, const Parameters ¶meters, const std::string &, const std::string &)
Mesh-based parameter optimization.
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.
std::vector< Real > _lower_bounds
Bounds of the parameters.
std::vector< std::vector< Real > * > _parameters
Parameter values declared as reporter data.
const std::vector< Real > _regularization_coeffs
Vector of regularization coefficients corresponding to each type.
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.
static InputParameters validParams()
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual void computeGradient(libMesh::PetscVector< Number > &gradient) const
Function to compute gradient.
std::vector< std::vector< Real > * > _gradients
Gradient values declared as reporter data.
IntRange< T > make_range(T beg, T end)
virtual Real computeObjective() override
Function to compute objective.
std::vector< Real > _upper_bounds
bool isParamValid(const std::string &name) const
auto index_range(const T &sizable)
virtual void setICsandBounds() override
Sets the initial conditions and bounds right before it is needed.
Optimization reporter that interfaces with TAO.