www.mooseframework.org
BicubicSplineFunction.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 
10 #include "BicubicSplineFunction.h"
11 
13 
15 
18 {
20  MooseEnum normal_component("x=0 y=1 z=2", "z");
21  params.addParam<MooseEnum>(
22  "normal_component",
23  normal_component,
24  "The component of the geometry that is normal to the spline x1/x2 values");
25  params.addRequiredParam<std::vector<Real>>("x1", "The first independent coordinate.");
26  params.addRequiredParam<std::vector<Real>>("x2", "The second independent coordinate.");
27  params.addRequiredParam<std::vector<Real>>("y", "The dependent values");
28  params.addParam<std::vector<Real>>(
29  "yx11", "The values of the derivative wrt x1 on the lower interpolation grid points.");
30  params.addParam<std::vector<Real>>(
31  "yx1n", "The values of the derivative wrt x1 on the upper interpolation grid points.");
32  params.addParam<std::vector<Real>>(
33  "yx21", "The values of the derivative wrt x2 on the lower interpolation grid points.");
34  params.addParam<std::vector<Real>>(
35  "yx2n", "The values of the derivative wrt x2 on the upper interpolation grid points.");
36  params.addParam<FunctionName>(
37  "yx1", "1e30", "The functional form of the derivative with respect to x1.");
38  params.addParam<FunctionName>(
39  "yx2", "1e30", "The functional form of the derivative with respect to x2.");
40 
41  return params;
42 }
43 
45  : Function(parameters),
46  FunctionInterface(this),
47  _normal_component(getParam<MooseEnum>("normal_component")),
48  _yx1(getFunction("yx1")),
49  _yx2(getFunction("yx2"))
50 {
51  _x1 = getParam<std::vector<Real>>("x1");
52  _x2 = getParam<std::vector<Real>>("x2");
53  std::vector<Real> yvec = getParam<std::vector<Real>>("y");
54  if (isParamValid("yx11"))
55  _yx11 = getParam<std::vector<Real>>("yx11");
56  if (isParamValid("yx1n"))
57  _yx1n = getParam<std::vector<Real>>("yx1n");
58  if (isParamValid("yx21"))
59  _yx21 = getParam<std::vector<Real>>("yx21");
60  if (isParamValid("yx2n"))
61  _yx2n = getParam<std::vector<Real>>("yx2n");
62 
63  unsigned int m = _x1.size(), n = _x2.size(), mn = yvec.size();
64  if (m * n != mn)
65  mooseError("The length of the supplied y must be equal to the lengths of x1 and x2 multiplied "
66  "together");
67 
68  std::vector<std::vector<Real>> y(m, std::vector<Real>(n));
69  unsigned int k = 0;
70  for (unsigned int i = 0; i < m; ++i)
71  for (unsigned int j = 0; j < n; ++j)
72  y[i][j] = yvec[k++];
73 
74  if (_yx11.empty())
75  _yx11.resize(n, 1e30);
76  else if (_yx11.size() != n)
77  mooseError("The length of the vectors holding the first derivatives of y with respect to x1 "
78  "must match the length of x2.");
79 
80  if (_yx1n.empty())
81  _yx1n.resize(n, 1e30);
82  else if (_yx1n.size() != n)
83  mooseError("The length of the vectors holding the first derivatives of y with respect to x1 "
84  "must match the length of x2.");
85 
86  if (_yx21.empty())
87  _yx21.resize(m, 1e30);
88  else if (_yx21.size() != m)
89  mooseError("The length of the vectors holding the first derivatives of y with respect to x2 "
90  "must match the length of x1.");
91 
92  if (_yx2n.empty())
93  _yx2n.resize(m, 1e30);
94  else if (_yx2n.size() != m)
95  mooseError("The length of the vectors holding the first derivatives of y with respect to x2 "
96  "must match the length of x1.");
97 
99 
100  if (_normal_component == 0)
101  {
102  // YZ plane
103  _x1_index = 1;
104  _x2_index = 2;
105  }
106  else if (_normal_component == 1)
107  {
108  // ZX plane
109  _x1_index = 2;
110  _x2_index = 0;
111  }
112  else
113  {
114  // XY plane
115  _x1_index = 0;
116  _x2_index = 1;
117  }
118 }
119 
120 Real
121 BicubicSplineFunction::value(Real /*t*/, const Point & p) const
122 {
123  // Call yx11/yx1n with the correctly oriented points
124  Real x1_begin = _x1[0];
125  Real x1_end = p(_x2_index);
126  Real xn_begin = _x1.back();
127  Real xn_end = p(_x2_index);
128 
129  Point x1(0, 0, 0);
130  Point xn(0, 0, 0);
131 
132  x1(_x1_index) = x1_begin;
133  x1(_x2_index) = x1_end;
134  xn(_x1_index) = xn_begin;
135  xn(_x2_index) = xn_end;
136 
137  Real yx11 = _yx1.value(0, x1);
138  Real yx1n = _yx1.value(0, xn);
139 
140  return _ipol.sample(p(_x1_index), p(_x2_index), yx11, yx1n);
141 }
142 
143 Real
144 BicubicSplineFunction::derivative(const Point & p, unsigned int deriv_var) const
145 {
146  Real yp1, ypn;
147  Point x1(0, 0, 0);
148  Point xn(0, 0, 0);
149  if (deriv_var == 1)
150  {
151  // Call yx11/yx1n with the correctly oriented points
152  Real x1_begin = _x1[0];
153  Real x1_end = p(_x2_index);
154  Real xn_begin = _x1.back();
155  Real xn_end = p(_x2_index);
156 
157  x1(_x1_index) = x1_begin;
158  x1(_x2_index) = x1_end;
159  xn(_x1_index) = xn_begin;
160  xn(_x2_index) = xn_end;
161 
162  yp1 = _yx1.value(0, x1);
163  ypn = _yx1.value(0, xn);
164  }
165  else if (deriv_var == 2)
166  {
167  // Call yx11/yx1n with the correctly oriented points
168  Real x1_begin = p(_x1_index);
169  Real x1_end = _x2[0];
170  Real xn_begin = p(_x1_index);
171  Real xn_end = _x2.back();
172 
173  x1(_x1_index) = x1_begin;
174  x1(_x2_index) = x1_end;
175  xn(_x1_index) = xn_begin;
176  xn(_x2_index) = xn_end;
177 
178  yp1 = _yx2.value(0, x1);
179  ypn = _yx2.value(0, xn);
180  }
181  else
182  mooseError("deriv_var must equal 1 or 2");
183 
184  return _ipol.sampleDerivative(p(_x1_index), p(_x2_index), deriv_var, yp1, ypn);
185 }
186 
187 RealGradient
188 BicubicSplineFunction::gradient(Real /*t*/, const Point & p) const
189 {
190  RealGradient grad = RealGradient(0, 0, 0);
191 
192  Real dF_dx1 = derivative(p, 1);
193  Real dF_dx2 = derivative(p, 2);
194 
195  grad(_x1_index) = dF_dx1;
196  grad(_x2_index) = dF_dx2;
197 
198  return grad;
199 }
200 
201 Real
202 BicubicSplineFunction::secondDerivative(const Point & p, unsigned int deriv_var) const
203 {
204  Real yp1, ypn;
205  Point x1(0, 0, 0);
206  Point xn(0, 0, 0);
207  if (deriv_var == 1)
208  {
209  // Call yx11/yx1n with the correctly oriented points
210  Real x1_begin = _x1[0];
211  Real x1_end = p(_x2_index);
212  Real xn_begin = _x1.back();
213  Real xn_end = p(_x2_index);
214 
215  x1(_x1_index) = x1_begin;
216  x1(_x2_index) = x1_end;
217  xn(_x1_index) = xn_begin;
218  xn(_x2_index) = xn_end;
219 
220  yp1 = _yx1.value(0, x1);
221  ypn = _yx1.value(0, xn);
222  }
223  else if (deriv_var == 2)
224  {
225  // Call yx11/yx1n with the correctly oriented points
226  Real x1_begin = p(_x1_index);
227  Real x1_end = _x2[0];
228  Real xn_begin = p(_x1_index);
229  Real xn_end = _x2.back();
230 
231  x1(_x1_index) = x1_begin;
232  x1(_x2_index) = x1_end;
233  xn(_x1_index) = xn_begin;
234  xn(_x2_index) = xn_end;
235 
236  yp1 = _yx2.value(0, x1);
237  ypn = _yx2.value(0, xn);
238  }
239  else
240  mooseError("deriv_var must equal 1 or 2");
241 
242  return _ipol.sample2ndDerivative(p(_x1_index), p(_x2_index), deriv_var, yp1, ypn);
243 }
BicubicSplineFunction::_x2
std::vector< Real > _x2
Definition: BicubicSplineFunction.h:44
BicubicSplineFunction::secondDerivative
virtual Real secondDerivative(const Point &p, unsigned int deriv_var) const
Definition: BicubicSplineFunction.C:202
BicubicSplineFunction.h
MooseObject::mooseError
void mooseError(Args &&... args) const
Definition: MooseObject.h:141
Function::value
virtual Real value(Real t, const Point &p) const
Override this to evaluate the scalar function at point (t,x,y,z), by default this returns zero,...
Definition: Function.C:39
MooseObject::isParamValid
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseObject.h:100
BicubicSplineFunction::_x1
std::vector< Real > _x1
Definition: BicubicSplineFunction.h:43
BicubicSplineFunction::_yx1n
std::vector< Real > _yx1n
Definition: BicubicSplineFunction.h:46
MooseEnum
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:31
BicubicSplineFunction::_ipol
BicubicSplineInterpolation _ipol
Definition: BicubicSplineFunction.h:38
BicubicSplineFunction::_yx21
std::vector< Real > _yx21
Definition: BicubicSplineFunction.h:47
InputParameters::addParam
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.
Definition: InputParameters.h:1198
FunctionInterface
Interface for objects that need to use functions.
Definition: FunctionInterface.h:38
defineLegacyParams
defineLegacyParams(BicubicSplineFunction)
BicubicSplineInterpolation::sample
Real sample(Real x1, Real x2, Real yx11=_deriv_bound, Real yx1n=_deriv_bound)
Samples value at point (x1, x2)
Definition: BicubicSplineInterpolation.C:159
BicubicSplineInterpolation::setData
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 and y values, and first derivatives at the edges.
Definition: BicubicSplineInterpolation.C:46
BicubicSplineInterpolation::sampleDerivative
Real sampleDerivative(Real x1, Real x2, unsigned int deriv_var, Real yp1=_deriv_bound, Real ypn=_deriv_bound)
Samples first derivative at point (x1, x2)
Definition: BicubicSplineInterpolation.C:171
BicubicSplineFunction::derivative
virtual Real derivative(const Point &p, unsigned int deriv_var) const
Definition: BicubicSplineFunction.C:144
BicubicSplineFunction::_normal_component
unsigned int _normal_component
Definition: BicubicSplineFunction.h:41
InputParameters
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
Definition: InputParameters.h:53
BicubicSplineFunction::_yx2
const Function & _yx2
Definition: BicubicSplineFunction.h:51
BicubicSplineFunction::_x1_index
Real _x1_index
Definition: BicubicSplineFunction.h:54
BicubicSplineFunction::_x2_index
Real _x2_index
Definition: BicubicSplineFunction.h:55
BicubicSplineFunction::_yx1
const Function & _yx1
Definition: BicubicSplineFunction.h:50
registerMooseObject
registerMooseObject("MooseApp", BicubicSplineFunction)
BicubicSplineFunction::gradient
virtual RealGradient gradient(Real t, const Point &p) const override
Function objects can optionally provide a gradient at a point.
Definition: BicubicSplineFunction.C:188
BicubicSplineFunction::_yx11
std::vector< Real > _yx11
Definition: BicubicSplineFunction.h:45
BicubicSplineFunction::value
virtual Real value(Real t, const Point &p) const override
Override this to evaluate the scalar function at point (t,x,y,z), by default this returns zero,...
Definition: BicubicSplineFunction.C:121
BicubicSplineFunction
Function that uses spline interpolation.
Definition: BicubicSplineFunction.h:24
Function::validParams
static InputParameters validParams()
Class constructor.
Definition: Function.C:15
BicubicSplineFunction::_yx2n
std::vector< Real > _yx2n
Definition: BicubicSplineFunction.h:48
BicubicSplineFunction::BicubicSplineFunction
BicubicSplineFunction(const InputParameters &parameters)
Definition: BicubicSplineFunction.C:44
m
PetscInt m
Definition: PetscDMMoose.C:1504
Function
Base class for function objects.
Definition: Function.h:40
BicubicSplineFunction::validParams
static InputParameters validParams()
Definition: BicubicSplineFunction.C:17
BicubicSplineInterpolation::sample2ndDerivative
Real sample2ndDerivative(Real x1, Real x2, unsigned int deriv_var, Real yp1=_deriv_bound, Real ypn=_deriv_bound)
Samples second derivative at point (x1, x2)
Definition: BicubicSplineInterpolation.C:202
InputParameters::addRequiredParam
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...
Definition: InputParameters.h:1176
n
PetscInt n
Definition: PetscDMMoose.C:1504