https://mooseframework.inl.gov
OptimizationReporterBase.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #include "MooseTypes.h"
12 #include "OptUtils.h"
13 #include "libmesh/petsc_vector.h"
14 
17 {
19  params.registerBase("OptimizationReporterBase");
20  params.addRequiredParam<std::vector<ReporterValueName>>(
21  "parameter_names", "List of parameter names, one for each group of parameters.");
22  params.addRangeCheckedParam<Real>(
23  "tikhonov_coeff", 0.0, "tikhonov_coeff >= 0", "Coefficient for Tikhonov Regularization.");
24  params.addParam<std::vector<ReporterValueName>>(
25  "equality_names", std::vector<ReporterValueName>(), "List of equality names.");
26  params.addParam<std::vector<ReporterValueName>>(
27  "inequality_names", std::vector<ReporterValueName>(), "List of inequality names.");
28  params.registerBase("OptimizationReporterBase");
29  return params;
30 }
31 
33  : GeneralReporter(parameters),
34  _parameter_names(getParam<std::vector<ReporterValueName>>("parameter_names")),
35  _nparams(_parameter_names.size()),
36  _parameters(_nparams),
37  _gradients(_nparams),
38  _tikhonov_coeff(getParam<Real>("tikhonov_coeff")),
39  _equality_names(&getParam<std::vector<ReporterValueName>>("equality_names")),
40  _n_eq_cons(_equality_names->size()),
41  _eq_constraints(_n_eq_cons),
42  _eq_gradients(_n_eq_cons),
43  _inequality_names(&getParam<std::vector<ReporterValueName>>("inequality_names")),
44  _n_ineq_cons(_inequality_names->size()),
45  _ineq_constraints(_n_ineq_cons),
46  _ineq_gradients(_n_ineq_cons)
47 {
48  for (const auto & i : make_range(_nparams))
49  {
50  _parameters[i] =
51  &declareValueByName<std::vector<Real>>(_parameter_names[i], REPORTER_MODE_REPLICATED);
52  _gradients[i] = &declareValueByName<std::vector<Real>>("grad_" + _parameter_names[i],
54  }
55  for (const auto & i : make_range(_n_eq_cons))
56  {
57  _eq_constraints[i] =
58  &declareValueByName<std::vector<Real>>(_equality_names->at(i), REPORTER_MODE_REPLICATED);
59  _eq_gradients[i] = &declareValueByName<std::vector<Real>>("grad_" + _equality_names->at(i),
61  }
62  for (const auto & i : make_range(_n_ineq_cons))
63  {
65  &declareValueByName<std::vector<Real>>(_inequality_names->at(i), REPORTER_MODE_REPLICATED);
66  _ineq_gradients[i] = &declareValueByName<std::vector<Real>>("grad_" + _inequality_names->at(i),
68  }
69 }
70 
71 void
73 {
74  for (const auto & param_group_id : make_range(_nparams))
75  {
76  if (_gradients[param_group_id]->size() != _nvalues[param_group_id])
77  mooseError("The gradient for parameter ",
78  _parameter_names[param_group_id],
79  " has changed, expected ",
80  _nvalues[param_group_id],
81  " versus ",
82  _gradients[param_group_id]->size(),
83  ".");
84 
85  if (_tikhonov_coeff > 0.0)
86  {
87  auto params = _parameters[param_group_id];
88  auto grads = _gradients[param_group_id];
89  for (const auto & param_id : make_range(_nvalues[param_group_id]))
90  (*grads)[param_id] += (*params)[param_id] * _tikhonov_coeff;
91  }
92  }
93 
95 }
96 
97 void
99 {
100  setICsandBounds();
101  x.init(getNumParams());
103 }
104 
105 void
107 {
109 }
110 
111 Real
113 {
114  return _lower_bounds[i];
115 }
116 
117 Real
119 {
120  return _upper_bounds[i];
121 }
122 
123 std::vector<Real>
125  Real default_value,
126  unsigned int param_id) const
127 {
128  // fill with default values
129  std::vector<Real> parsed_data_id(_nvalues[param_id], default_value);
130  if (isParamValid(type))
131  {
132  std::vector<std::vector<Real>> parsed_data(getParam<std::vector<std::vector<Real>>>(type));
133  parsed_data_id.assign(parsed_data[param_id].begin(), parsed_data[param_id].end());
134  if (parsed_data.size() != _nvalues.size())
136  "There must be a vector of ",
137  type,
138  " per parameter group. The ",
139  type,
140  " input format is std::vector<std::vector<Real>> so each vector should be "
141  "seperated by \";\" even if it is a single value per group for a constant ",
142  type,
143  ".");
144  // The case when the initial condition is constant for each parameter group
145  if (parsed_data[param_id].size() == 1)
146  parsed_data_id.assign(_nvalues[param_id], parsed_data[param_id][0]);
147  else if (parsed_data[param_id].size() != _nvalues[param_id])
149  "When ",
150  type,
151  " are given in input file, there must either be a single value per parameter "
152  "group or a value for every parameter in the group.");
153  }
154 
155  return parsed_data_id;
156 }
157 
158 void
160  libMesh::PetscVector<Number> & eqs_constraints) const
161 {
163 }
164 
165 void
167  libMesh::PetscVector<Number> & ineqs_constraints) const
168 {
170 }
171 
172 void
174 {
175  for (const auto & p : make_range(_n_eq_cons))
176  if (_eq_gradients[p]->size() != _ndof)
177  mooseError("The equality jacobian for parameter ",
178  _parameter_names[p],
179  " has changed, expected ",
180  _ndof,
181  " versus ",
182  _eq_gradients[p]->size(),
183  ".");
185 }
186 
187 void
189 {
190  for (const auto & p : make_range(_n_ineq_cons))
191  if (_ineq_gradients[p]->size() != _ndof)
192  mooseError("The inequality jacobian for parameter ",
193  _parameter_names[p],
194  " has changed, expected ",
195  _ndof,
196  " versus ",
197  _ineq_gradients[p]->size(),
198  ".");
200 }
virtual void setICsandBounds()
Sets the initial conditions and bounds right before it is needed.
const std::vector< ReporterValueName > * _inequality_names
Inequality constraint names.
const std::vector< ReporterValueName > & _parameter_names
Parameter names.
const unsigned int _n_ineq_cons
Number of inequality constraint names.
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
virtual void computeInequalityConstraints(libMesh::PetscVector< Number > &ineqs_constraints) const
Function to compute the inequality constraints.
Real getLowerBound(dof_id_type i) const
std::vector< std::vector< Real > * > _ineq_gradients
Gradient values declared as reporter data.
dof_id_type _ndof
Total number of parameters.
void copyPetscVectorIntoReporter(const libMesh::PetscVector< Number > &x, std::vector< std::vector< Real > *> reporterVectors)
Definition: OptUtils.C:33
static InputParameters validParams()
virtual void computeEqualityGradient(libMesh::PetscMatrix< Number > &gradient) const
Function to compute the gradient of the equality constraints/ This is the last call of the equality c...
virtual dof_id_type getNumParams() const
Function to get the total number of parameters.
void addRequiredParam(const std::string &name, const std::string &doc_string)
const unsigned int _nparams
Number of parameter vectors.
std::vector< std::vector< Real > * > _ineq_constraints
Inequality values declared as reporter data.
bool isParamValid(const std::string &name) const
void registerBase(const std::string &value)
void setInitialCondition(libMesh::PetscVector< Number > &param)
Function to initialize petsc vectors from vpp data.
std::vector< std::vector< Real > * > _eq_gradients
Gradient values declared as reporter data.
const std::vector< double > x
std::vector< dof_id_type > _nvalues
Number of values for each parameter.
const std::string & type() const
const T & getParam(const std::string &name) 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.
std::vector< Real > _lower_bounds
Bounds of the parameters.
void paramError(const std::string &param, Args... args) const
std::vector< std::vector< Real > * > _parameters
Parameter values declared as reporter data.
std::vector< std::vector< Real > * > _eq_constraints
Equality values declared as reporter data.
const unsigned int _n_eq_cons
Number of equality constraint names.
const std::vector< ReporterValueName > * _equality_names
Equality constraint names.
virtual void computeInequalityGradient(libMesh::PetscMatrix< Number > &gradient) const
Function to compute the gradient of the inequality constraints/ This is the last call of the inequali...
OptimizationReporterBase(const InputParameters &parameters)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual void computeGradient(libMesh::PetscVector< Number > &gradient) const
Function to compute gradient.
void copyReporterIntoPetscVector(const std::vector< std::vector< Real > *> reporterVectors, libMesh::PetscVector< Number > &x)
Definition: OptUtils.C:21
std::vector< std::vector< Real > * > _gradients
Gradient values declared as reporter data.
IntRange< T > make_range(T beg, T end)
virtual void updateParameters(const libMesh::PetscVector< Number > &x)
Function to set parameters.
void mooseError(Args &&... args) const
void addRangeCheckedParam(const std::string &name, const T &value, const std::string &parsed_function, const std::string &doc_string)
const ReporterMode REPORTER_MODE_REPLICATED
static InputParameters validParams()
Real getUpperBound(dof_id_type i) const
Upper and lower bounds for each parameter being controlled.
const Real _tikhonov_coeff
Tikhonov Coefficient for regularization.
virtual void computeEqualityConstraints(libMesh::PetscVector< Number > &eqs_constraints) const
Function to compute the equality constraints.
void copyReporterIntoPetscMatrix(const std::vector< std::vector< Real > *> reporterVectors, libMesh::PetscMatrix< Number > &x)
Definition: OptUtils.C:42
uint8_t dof_id_type