https://mooseframework.inl.gov
QuadraticMinimize.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 "QuadraticMinimize.h"
11 #include "libmesh/petsc_vector.h"
12 
13 registerMooseObject("OptimizationTestApp", QuadraticMinimize);
14 
17 {
19  params.addRequiredParam<Real>("objective", "Desired value of objective function.");
20  params.addRequiredParam<std::vector<Real>>("solution", "Desired solution to optimization.");
21  return params;
22 }
23 
25  : OptimizationReporter(parameters),
26  _result(getParam<Real>("objective")),
27  _solution(getParam<std::vector<Real>>("solution"))
28 {
30  if (_solution.size() != _ndof)
31  paramError("solution", "Size not equal to number of degrees of freedom (", _ndof, ").");
32 }
33 
34 Real
36 {
37  Real obj = _result;
38  unsigned int i = 0;
39  for (const auto & param : _parameters)
40  for (const auto & val : *param)
41  {
42  Real tmp = val - _solution[i++];
43  obj += tmp * tmp;
44  }
45 
46  return obj;
47 }
48 
49 void
51 {
52  unsigned int i = 0;
53  for (const auto & param : _parameters)
54  for (const auto & val : *param)
55  {
56  gradient.set(i, 2.0 * (val - _solution[i]));
57  i++;
58  }
59  gradient.close();
60 }
Computes gradient and contains reporters for communicating between optimizeSolve and subapps...
const Real & _result
Input objective function value.
dof_id_type _ndof
Total number of parameters.
static InputParameters validParams()
void addRequiredParam(const std::string &name, const std::string &doc_string)
static InputParameters validParams()
registerMooseObject("OptimizationTestApp", QuadraticMinimize)
void paramError(const std::string &param, Args... args) const
std::vector< std::vector< Real > * > _parameters
Parameter values declared as reporter data.
virtual Real computeObjective() override
Function to compute objective.
virtual void setICsandBounds() override
Sets the initial conditions and bounds right before it is needed.
const std::vector< Real > & _solution
Desired solution to optimization.
virtual void set(const numeric_index_type i, const T value) override
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
This form function simply represents a quadratic objective function: f(x) = val + {i=1}^N (x_i - a_i)...
virtual void computeGradient(libMesh::PetscVector< Number > &gradient) const override
Function to compute gradient.
QuadraticMinimize(const InputParameters &parameters)
virtual void close() override