https://mooseframework.inl.gov
Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Attributes | List of all members
OptimizationFunctionInnerProductHelper Class Reference

#include <OptimizationFunctionInnerProductHelper.h>

Inheritance diagram for OptimizationFunctionInnerProductHelper:
[legend]

Public Member Functions

 OptimizationFunctionInnerProductHelper (const InputParameters &parameters)
 

Static Public Member Functions

static InputParameters validParams ()
 

Protected Member Functions

void setCurrentTime (Real time, Real dt)
 This function sets up member variables for the inner product accumulation at certain time. More...
 
void update (const Point &q_point, Real q_inner_product)
 Accumulates integration for inner product by multiplying the given value by the function's parameterGradient. More...
 
void add (const OptimizationFunctionInnerProductHelper &other)
 Accumulates inner product integration in _curr_time_ip vector from another object. More...
 
void getVector (std::vector< Real > &result)
 Gathers _curr_time_ip from other processors and performs time integration. More...
 

Private Attributes

FEProblemBase_ip_problem
 FEProblem used for getting system quantities. More...
 
const OptimizationFunction *const _function
 Function used in optimization. More...
 
const Real_reverse_time_end
 The final time when we want to reverse the time index in function evaluation. More...
 
Real _simulation_time
 Time the simulation is at. More...
 
Real _actual_time
 Time the actual problem is at, defined by _reverse_time_end. More...
 
std::vector< std::pair< Real, std::vector< Real > > > _time_ip
 Vector holding data for each time. More...
 
std::vector< Real > * _curr_time_ip = nullptr
 Vector for current time. More...
 

Detailed Description

Definition at line 24 of file OptimizationFunctionInnerProductHelper.h.

Constructor & Destructor Documentation

◆ OptimizationFunctionInnerProductHelper()

OptimizationFunctionInnerProductHelper::OptimizationFunctionInnerProductHelper ( const InputParameters parameters)

Definition at line 32 of file OptimizationFunctionInnerProductHelper.C.

34  : _ip_problem(*parameters.getCheckedPointerParam<FEProblemBase *>("_fe_problem_base")),
35  _function(dynamic_cast<const OptimizationFunction *>(&_ip_problem.getFunction(
36  parameters.get<FunctionName>("function"), parameters.get<THREAD_ID>("_tid")))),
37  _reverse_time_end(parameters.get<Real>("reverse_time_end")),
39 {
40  if (!_function)
41  mooseError("Function requested by ",
42  parameters.get<std::string>("_type"),
43  " must be an OptimizationFunction.");
44 }
void mooseError(Args &&... args)
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
T getCheckedPointerParam(const std::string &name, const std::string &error_string="") const
Real getStartTime() const
const OptimizationFunction *const _function
Function used in optimization.
FEProblemBase & _ip_problem
FEProblem used for getting system quantities.
const Real & _reverse_time_end
The final time when we want to reverse the time index in function evaluation.
MooseApp & getMooseApp() const
virtual Function & getFunction(const std::string &name, const THREAD_ID tid=0)
unsigned int THREAD_ID

Member Function Documentation

◆ add()

void OptimizationFunctionInnerProductHelper::add ( const OptimizationFunctionInnerProductHelper other)
protected

Accumulates inner product integration in _curr_time_ip vector from another object.

This is used for thread joining.

Definition at line 79 of file OptimizationFunctionInnerProductHelper.C.

Referenced by ElementOptimizationFunctionInnerProduct::threadJoin(), and SideOptimizationFunctionInnerProduct::threadJoin().

80 {
81  _curr_time_ip->resize(std::max(_curr_time_ip->size(), other._curr_time_ip->size()), 0.0);
82  for (const auto & i : index_range(*other._curr_time_ip))
83  (*_curr_time_ip)[i] += (*other._curr_time_ip)[i];
84 }
std::vector< Real > * _curr_time_ip
Vector for current time.
auto index_range(const T &sizable)

◆ getVector()

void OptimizationFunctionInnerProductHelper::getVector ( std::vector< Real > &  result)
protected

Gathers _curr_time_ip from other processors and performs time integration.

Parameters
resultReturn vector of inner products

Definition at line 87 of file OptimizationFunctionInnerProductHelper.C.

Referenced by ElementOptimizationFunctionInnerProduct::finalize(), and SideOptimizationFunctionInnerProduct::finalize().

88 {
89  std::size_t nvar = _curr_time_ip->size();
90  _ip_problem.comm().max(nvar);
91  _curr_time_ip->resize(nvar, 0.0);
93 
94  if (!_ip_problem.isTransient())
95  result = (*_curr_time_ip);
96  else
97  {
98  // Make sure everything is the same size
99  for (const auto & it : _time_ip)
100  nvar = std::max(nvar, it.second.size());
101  for (auto & it : _time_ip)
102  it.second.resize(nvar);
103  result.assign(nvar, 0.0);
104 
105  // Integrate in time
106  std::sort(_time_ip.begin(),
107  _time_ip.end(),
108  [](const std::pair<Real, std::vector<Real>> & a,
109  const std::pair<Real, std::vector<Real>> & b) { return a.first < b.first; });
110  // We are integrating over the entire history here. Technically this can be done in an
111  // accumulative manner by storing the integration over previous time steps. However, this
112  // is a relatively cheap operation and considering all cases where time steps may be
113  // repeated would be difficult.
114  for (const auto & ti : _time_ip)
115  for (const auto & i : make_range(nvar))
116  result[i] += ti.second[i];
117  }
118 }
std::vector< std::pair< Real, std::vector< Real > > > _time_ip
Vector holding data for each time.
const Parallel::Communicator & comm() const
std::vector< Real > * _curr_time_ip
Vector for current time.
FEProblemBase & _ip_problem
FEProblem used for getting system quantities.
void max(const T &r, T &o, Request &req) const
IntRange< T > make_range(T beg, T end)
virtual bool isTransient() const override

◆ setCurrentTime()

void OptimizationFunctionInnerProductHelper::setCurrentTime ( Real  time,
Real  dt 
)
protected

This function sets up member variables for the inner product accumulation at certain time.

The time step size is needed in order to calculate the actual simulation time (for adjoint calculations)

Parameters
timeCurrent simulation time, the actual time is computed via _reverse_time_end
dtThe current time step size

Definition at line 47 of file OptimizationFunctionInnerProductHelper.C.

Referenced by ElementOptimizationFunctionInnerProduct::initialize(), and SideOptimizationFunctionInnerProduct::initialize().

48 {
49  // We are saving the integral for each time step.
50  // So create or grab the vector if the time has changed or this is our first time here.
51  _curr_time_ip = nullptr;
52  for (auto & pr : _time_ip)
53  if (MooseUtils::relativeFuzzyEqual(time, pr.first))
54  _curr_time_ip = &pr.second;
55  if (!_curr_time_ip)
56  {
57  _time_ip.emplace_back(time, std::vector<Real>());
58  _curr_time_ip = &_time_ip.back().second;
59  }
60  _curr_time_ip->clear();
61 
62  _actual_time =
64 }
std::vector< std::pair< Real, std::vector< Real > > > _time_ip
Vector holding data for each time.
bool absoluteFuzzyEqual(const T &var1, const T2 &var2, const T3 &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
std::vector< Real > * _curr_time_ip
Vector for current time.
const Real & _reverse_time_end
The final time when we want to reverse the time index in function evaluation.
Real _actual_time
Time the actual problem is at, defined by _reverse_time_end.
bool relativeFuzzyEqual(const T &var1, const T2 &var2, const T3 &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)

◆ update()

void OptimizationFunctionInnerProductHelper::update ( const Point &  q_point,
Real  q_inner_product 
)
protected

Accumulates integration for inner product by multiplying the given value by the function's parameterGradient.

Parameters
q_pointThe quadrature point location
q_inner_productThe inner product value for the current quadrature point, which is multiplied by the function parameter gradient.

Definition at line 67 of file OptimizationFunctionInnerProductHelper.C.

Referenced by ElementOptimizationFunctionInnerProduct::execute(), and SideOptimizationFunctionInnerProduct::execute().

68 {
69  if (!_curr_time_ip)
70  mooseError("Internal error, 'setCurrentTime' needs to be called before calling 'update'.");
71 
72  const std::vector<Real> pg = _function->parameterGradient(_actual_time, q_point);
73  _curr_time_ip->resize(std::max(pg.size(), _curr_time_ip->size()), 0.0);
74  for (const auto & i : index_range(pg))
75  (*_curr_time_ip)[i] += q_inner_product * pg[i];
76 }
void mooseError(Args &&... args)
std::vector< Real > * _curr_time_ip
Vector for current time.
const OptimizationFunction *const _function
Function used in optimization.
virtual std::vector< Real > parameterGradient(Real t, const Point &pt) const =0
Real _actual_time
Time the actual problem is at, defined by _reverse_time_end.
auto index_range(const T &sizable)

◆ validParams()

InputParameters OptimizationFunctionInnerProductHelper::validParams ( )
static

Definition at line 21 of file OptimizationFunctionInnerProductHelper.C.

Referenced by ElementOptimizationFunctionInnerProduct::validParams(), and SideOptimizationFunctionInnerProduct::validParams().

22 {
24  params.addRequiredParam<FunctionName>("function", "Optimization function.");
25  params.addParam<Real>(
26  "reverse_time_end",
27  0.0,
28  "End time used for reversing the time integration when evaluating function derivative.");
29  return params;
30 }
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
void addRequiredParam(const std::string &name, const std::string &doc_string)
InputParameters emptyInputParameters()
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

Member Data Documentation

◆ _actual_time

Real OptimizationFunctionInnerProductHelper::_actual_time
private

Time the actual problem is at, defined by _reverse_time_end.

Definition at line 75 of file OptimizationFunctionInnerProductHelper.h.

Referenced by setCurrentTime(), and update().

◆ _curr_time_ip

std::vector<Real>* OptimizationFunctionInnerProductHelper::_curr_time_ip = nullptr
private

Vector for current time.

Definition at line 80 of file OptimizationFunctionInnerProductHelper.h.

Referenced by add(), getVector(), setCurrentTime(), and update().

◆ _function

const OptimizationFunction* const OptimizationFunctionInnerProductHelper::_function
private

Function used in optimization.

Definition at line 68 of file OptimizationFunctionInnerProductHelper.h.

Referenced by OptimizationFunctionInnerProductHelper(), and update().

◆ _ip_problem

FEProblemBase& OptimizationFunctionInnerProductHelper::_ip_problem
private

FEProblem used for getting system quantities.

Definition at line 65 of file OptimizationFunctionInnerProductHelper.h.

Referenced by getVector().

◆ _reverse_time_end

const Real& OptimizationFunctionInnerProductHelper::_reverse_time_end
private

The final time when we want to reverse the time index in function evaluation.

Definition at line 71 of file OptimizationFunctionInnerProductHelper.h.

Referenced by setCurrentTime().

◆ _simulation_time

Real OptimizationFunctionInnerProductHelper::_simulation_time
private

Time the simulation is at.

Definition at line 73 of file OptimizationFunctionInnerProductHelper.h.

◆ _time_ip

std::vector<std::pair<Real, std::vector<Real> > > OptimizationFunctionInnerProductHelper::_time_ip
private

Vector holding data for each time.

Definition at line 78 of file OptimizationFunctionInnerProductHelper.h.

Referenced by getVector(), and setCurrentTime().


The documentation for this class was generated from the following files: