www.mooseframework.org
BicubicSplineFunction.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 
15 #include "BicubicSplineFunction.h"
16 
17 template <>
20 {
22  params.addRequiredParam<std::vector<Real>>("x1", "The first independent coordinate.");
23  params.addRequiredParam<std::vector<Real>>("x2", "The second independent coordinate.");
24  params.addRequiredParam<std::vector<Real>>("y", "The dependent values");
25  params.addParam<std::vector<Real>>(
26  "yx11", "The values of the derivative wrt x1 on the lower interpolation grid points.");
27  params.addParam<std::vector<Real>>(
28  "yx1n", "The values of the derivative wrt x1 on the upper interpolation grid points.");
29  params.addParam<std::vector<Real>>(
30  "yx21", "The values of the derivative wrt x2 on the lower interpolation grid points.");
31  params.addParam<std::vector<Real>>(
32  "yx2n", "The values of the derivative wrt x2 on the upper interpolation grid points.");
33  params.addParam<FunctionName>(
34  "yx1", "1e30", "The functional form of the derivative with respect to x1.");
35  params.addParam<FunctionName>(
36  "yx2", "1e30", "The functional form of the derivative with respect to x2.");
37 
38  return params;
39 }
40 
42  : Function(parameters),
43  FunctionInterface(this),
44  _yx1(getFunction("yx1")),
45  _yx2(getFunction("yx2"))
46 {
47  _x1 = getParam<std::vector<Real>>("x1");
48  _x2 = getParam<std::vector<Real>>("x2");
49  std::vector<Real> yvec = getParam<std::vector<Real>>("y");
50  if (isParamValid("yx11"))
51  _yx11 = getParam<std::vector<Real>>("yx11");
52  if (isParamValid("yx1n"))
53  _yx1n = getParam<std::vector<Real>>("yx1n");
54  if (isParamValid("yx21"))
55  _yx21 = getParam<std::vector<Real>>("yx21");
56  if (isParamValid("yx2n"))
57  _yx2n = getParam<std::vector<Real>>("yx2n");
58 
59  unsigned int m = _x1.size(), n = _x2.size(), mn = yvec.size();
60  if (m * n != mn)
61  mooseError("The length of the supplied y must be equal to the lengths of x1 and x2 multiplied "
62  "together");
63 
64  std::vector<std::vector<Real>> y(m, std::vector<Real>(n));
65  unsigned int k = 0;
66  for (unsigned int i = 0; i < m; ++i)
67  for (unsigned int j = 0; j < n; ++j)
68  y[i][j] = yvec[k++];
69 
70  if (_yx11.empty())
71  _yx11.resize(n, 1e30);
72  else if (_yx11.size() != n)
73  mooseError("The length of the vectors holding the first derivatives of y with respect to x1 "
74  "must match the length of x2.");
75 
76  if (_yx1n.empty())
77  _yx1n.resize(n, 1e30);
78  else if (_yx1n.size() != n)
79  mooseError("The length of the vectors holding the first derivatives of y with respect to x1 "
80  "must match the length of x2.");
81 
82  if (_yx21.empty())
83  _yx21.resize(m, 1e30);
84  else if (_yx21.size() != m)
85  mooseError("The length of the vectors holding the first derivatives of y with respect to x2 "
86  "must match the length of x1.");
87 
88  if (_yx2n.empty())
89  _yx2n.resize(m, 1e30);
90  else if (_yx2n.size() != m)
91  mooseError("The length of the vectors holding the first derivatives of y with respect to x2 "
92  "must match the length of x1.");
93 
94  _ipol.setData(_x1, _x2, y, _yx11, _yx1n, _yx21, _yx2n);
95 }
96 
97 Real
98 BicubicSplineFunction::value(Real /*t*/, const Point & p)
99 {
100  Point x1(_x1[0], p(1), 0);
101  Point xn(_x1.back(), p(1), 0);
102  Real yx11 = _yx1.value(0, x1);
103  Real yx1n = _yx1.value(0, xn);
104 
105  return _ipol.sample(p(0), p(1), yx11, yx1n);
106 }
107 
108 Real
109 BicubicSplineFunction::derivative(const Point & p, unsigned int deriv_var)
110 {
111  Real yp1, ypn;
112  if (deriv_var == 1)
113  {
114  Point x1(_x1[0], p(1), 0);
115  Point xn(_x1.back(), p(1), 0);
116  yp1 = _yx1.value(0, x1);
117  ypn = _yx1.value(0, xn);
118  }
119  else if (deriv_var == 2)
120  {
121  Point x1(p(0), _x2[0], 0);
122  Point xn(p(0), _x2.back(), 0);
123  yp1 = _yx2.value(0, x1);
124  ypn = _yx2.value(0, xn);
125  }
126  else
127  mooseError("deriv_var must equal 1 or 2");
128 
129  return _ipol.sampleDerivative(p(0), p(1), deriv_var, yp1, ypn);
130 }
131 
133 BicubicSplineFunction::gradient(Real /*t*/, const Point & p)
134 {
135  return RealGradient(derivative(p, 1), derivative(p, 2), 0);
136 }
137 
138 Real
139 BicubicSplineFunction::secondDerivative(const Point & p, unsigned int deriv_var)
140 {
141  Real yp1, ypn;
142  if (deriv_var == 1)
143  {
144  Point x1(_x1[0], p(1), 0);
145  Point xn(_x1.back(), p(1), 0);
146  yp1 = _yx1.value(0, x1);
147  ypn = _yx1.value(0, xn);
148  }
149  else if (deriv_var == 2)
150  {
151  Point x1(p(0), _x2[0], 0);
152  Point xn(p(0), _x2.back(), 0);
153  yp1 = _yx2.value(0, x1);
154  ypn = _yx2.value(0, xn);
155  }
156  else
157  mooseError("deriv_var must equal 1 or 2");
158 
159  return _ipol.sample2ndDerivative(p(0), p(1), deriv_var, yp1, ypn);
160 }
Real sample(Real x1, Real x2, Real yx11=_deriv_bound, Real yx1n=_deriv_bound)
RealVectorValue RealGradient
Definition: Assembly.h:43
virtual Real value(Real t, const Point &p)
Override this to evaluate the scalar function at point (t,x,y,z), by default this returns zero...
Definition: Function.C:43
Base class for function objects.
Definition: Function.h:46
InputParameters validParams< BicubicSplineFunction >()
BicubicSplineInterpolation _ipol
virtual Real value(Real t, const Point &p) override
Override this to evaluate the scalar function at point (t,x,y,z), by default this returns zero...
BicubicSplineFunction(const InputParameters &parameters)
virtual RealGradient gradient(Real t, const Point &p) override
Function objects can optionally provide a gradient at a point.
std::vector< Real > _x1
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
Real sampleDerivative(Real x1, Real x2, unsigned int deriv_var, Real yp1=_deriv_bound, Real ypn=_deriv_bound)
std::vector< Real > _yx2n
std::vector< Real > _yx1n
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...
virtual Real derivative(const Point &p, unsigned int deriv_var)
PetscInt m
std::vector< Real > _x2
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseObject.h:67
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition: MooseObject.h:122
virtual Real secondDerivative(const Point &p, unsigned int deriv_var)
std::vector< Real > _yx21
PetscInt n
std::vector< Real > _yx11
Real sample2ndDerivative(Real x1, Real x2, unsigned int deriv_var, Real yp1=_deriv_bound, Real ypn=_deriv_bound)
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an option parameter and a documentation string to the InputParameters object...
void mooseError(Args &&...args) const
Definition: MooseObject.h:80
InputParameters validParams< Function >()
Definition: Function.C:19
Interface for objects that need to use functions.
void setData(const std::vector< Real > &x1, const std::vector< Real > &x2, const std::vector< std::vector< Real >> &y, const std::vector< Real > &yx11=std::vector< Real >(), const std::vector< Real > &yx1n=std::vector< Real >(), const std::vector< Real > &yx21=std::vector< Real >(), const std::vector< Real > &yx2n=std::vector< Real >())
Set the x1-, x2, y- values and first derivatives.