www.mooseframework.org
LinearCombinationFunction.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 
11 
13 
14 template <>
17 {
19  params.addRequiredParam<std::vector<FunctionName>>(
20  "functions", "This function will return Sum_over_i(w_i * functions_i)");
21  params.addRequiredParam<std::vector<Real>>(
22  "w", "This function will return Sum_over_i(w_i * functions_i)");
23  params.addClassDescription("Returns the linear combination of the functions");
24  return params;
25 }
26 
28  : Function(parameters), FunctionInterface(this), _w(getParam<std::vector<Real>>("w"))
29 {
30 
31  const std::vector<FunctionName> & names = getParam<std::vector<FunctionName>>("functions");
32  const unsigned int len = names.size();
33  if (len != _w.size())
34  mooseError(
35  "LinearCombinationFunction: The number of functions must equal the number of w values");
36 
37  _f.resize(len);
38  for (unsigned i = 0; i < len; ++i)
39  {
40  if (name() == names[i])
41  mooseError("A LinearCombinationFunction must not reference itself");
42  Function * const f = &getFunctionByName(names[i]);
43  if (!f)
44  mooseError("LinearCombinationFunction: The function ",
45  names[i],
46  " (referenced by ",
47  name(),
48  ") cannot be found");
49  _f[i] = f;
50  }
51 }
52 
53 Real
54 LinearCombinationFunction::value(Real t, const Point & p)
55 {
56  Real val = 0;
57  for (unsigned i = 0; i < _f.size(); ++i)
58  val += _w[i] * _f[i]->value(t, p);
59  return val;
60 }
61 
62 RealGradient
63 LinearCombinationFunction::gradient(Real t, const Point & p)
64 {
65  RealGradient g;
66  for (unsigned i = 0; i < _f.size(); ++i)
67  g += _w[i] * _f[i]->gradient(t, p);
68  return g;
69 }
70 
73 {
75  for (unsigned i = 0; i < _f.size(); ++i)
76  v += _w[i] * _f[i]->vectorValue(t, p);
77  return v;
78 }
virtual RealVectorValue vectorValue(Real t, const Point &p) override
Override this to evaluate the vector function at a point (t,x,y,z), by default this returns a zero ve...
Sum_over_i (w_i * functions_i)
Base class for function objects.
Definition: Function.h:40
registerMooseObject("MooseApp", LinearCombinationFunction)
VectorValue< Real > RealVectorValue
Definition: Assembly.h:31
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
LinearCombinationFunction(const InputParameters &parameters)
void addRequiredParam(const std::string &name, const std::string &doc_string)
This method adds a parameter and documentation string to the InputParameters object that will be extr...
InputParameters validParams< LinearCombinationFunction >()
virtual RealGradient gradient(Real t, const Point &p) override
Function objects can optionally provide a gradient at a point.
virtual Real value(Real t, const Point &pt) override
Override this to evaluate the scalar function at point (t,x,y,z), by default this returns zero...
Function & getFunctionByName(const FunctionName &name)
Get a function with a given name.
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:59
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
InputParameters validParams< Function >()
Definition: Function.C:14
std::vector< Function * > _f
Interface for objects that need to use functions.