www.mooseframework.org
LinearCombinationFunction.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
16 
17 template <>
20 {
22  params.addRequiredParam<std::vector<FunctionName>>(
23  "functions", "This function will return Sum_over_i(w_i * functions_i)");
24  params.addRequiredParam<std::vector<Real>>(
25  "w", "This function will return Sum_over_i(w_i * functions_i)");
26  params.addClassDescription("Returns the linear combination of the functions");
27  return params;
28 }
29 
31  : Function(parameters), FunctionInterface(this), _w(getParam<std::vector<Real>>("w"))
32 {
33 
34  const std::vector<FunctionName> & names = getParam<std::vector<FunctionName>>("functions");
35  const unsigned int len = names.size();
36  if (len != _w.size())
37  mooseError(
38  "LinearCombinationFunction: The number of functions must equal the number of w values");
39 
40  _f.resize(len);
41  for (unsigned i = 0; i < len; ++i)
42  {
43  if (name() == names[i])
44  mooseError("A LinearCombinationFunction must not reference itself");
45  Function * const f = &getFunctionByName(names[i]);
46  if (!f)
47  mooseError("LinearCombinationFunction: The function ",
48  names[i],
49  " (referenced by ",
50  name(),
51  ") cannot be found");
52  _f[i] = f;
53  }
54 }
55 
56 Real
57 LinearCombinationFunction::value(Real t, const Point & p)
58 {
59  Real val = 0;
60  for (unsigned i = 0; i < _f.size(); ++i)
61  val += _w[i] * _f[i]->value(t, p);
62  return val;
63 }
64 
66 LinearCombinationFunction::gradient(Real t, const Point & p)
67 {
68  RealGradient g;
69  for (unsigned i = 0; i < _f.size(); ++i)
70  g += _w[i] * _f[i]->gradient(t, p);
71  return g;
72 }
73 
76 {
78  for (unsigned i = 0; i < _f.size(); ++i)
79  v += _w[i] * _f[i]->vectorValue(t, p);
80  return v;
81 }
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:47
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...
RealVectorValue RealGradient
Definition: Assembly.h:43
Base class for function objects.
Definition: Function.h:46
VectorValue< Real > RealVectorValue
Definition: Assembly.h:40
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
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.
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...
void mooseError(Args &&...args) const
Definition: MooseObject.h:80
InputParameters validParams< Function >()
Definition: Function.C:19
std::vector< Function * > _f
Interface for objects that need to use functions.