www.mooseframework.org
PiecewiseMultiInterpolation.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 #include "GriddedData.h"
12 
15 {
17  params.addParam<FileName>(
18  "data_file",
19  "File holding data for use with PiecewiseMultiInterpolation. Format: any empty line and any "
20  "line "
21  "beginning with # are ignored, all other lines are assumed to contain relevant information. "
22  "The file must begin with specification of the grid. This is done through lines containing "
23  "the keywords: AXIS X; AXIS Y; AXIS Z; or AXIS T. Immediately following the keyword line "
24  "must be a space-separated line of real numbers which define the grid along the specified "
25  "axis. These data must be monotonically increasing. After all the axes and their grids "
26  "have been specified, there must be a line that is DATA. Following that line, function "
27  "values are given in the correct order (they may be on individual lines, or be "
28  "space-separated on a number of lines). When the function is evaluated, f[i,j,k,l] "
29  "corresponds to the i + j*Ni + k*Ni*Nj + l*Ni*Nj*Nk data value. Here i>=0 corresponding to "
30  "the index along the first AXIS, j>=0 corresponding to the index along the second AXIS, etc, "
31  "and Ni = number of grid points along the first AXIS, etc.");
32  return params;
33 }
34 
36  : Function(parameters),
37  _gridded_data(std::make_unique<GriddedData>(getParam<FileName>("data_file"))),
38  _dim(_gridded_data->getDim())
39 {
40  _gridded_data->getAxes(_axes);
41  _gridded_data->getGrid(_grid);
42 
43  // GriddedData does not require monotonicity of axes, but we do
44  for (unsigned int i = 0; i < _dim; ++i)
45  for (unsigned int j = 1; j < _grid[i].size(); ++j)
46  if (_grid[i][j - 1] >= _grid[i][j])
47  mooseError("PiecewiseMultiInterpolation needs monotonically-increasing axis data. Axis ",
48  i,
49  " contains non-monotonicity at value ",
50  _grid[i][j]);
51 
52  // GriddedData does not demand that each axis is independent, but we do
53  std::set<int> s(_axes.begin(), _axes.end());
54  if (s.size() != _dim)
55  mooseError(
56  "PiecewiseMultiInterpolation needs the AXES to be independent. Check the AXIS lines in "
57  "your data file.");
58 }
59 
61 
62 template <bool is_ad>
65  const MooseADWrapper<Point, is_ad> & p) const
66 {
67  // convert the inputs to an input to the sample function using _axes
69  for (unsigned int i = 0; i < _dim; ++i)
70  {
71  if (_axes[i] < 3)
72  point_in_grid[i] = p(_axes[i]);
73  else if (_axes[i] == 3) // the time direction
74  point_in_grid[i] = t;
75  }
76  return point_in_grid;
77 }
78 
80 PiecewiseMultiInterpolation::pointInGrid<false>(const Real &, const Point &) const;
82 PiecewiseMultiInterpolation::pointInGrid<true>(const ADReal &, const ADPoint &) const;
83 
84 Real
85 PiecewiseMultiInterpolation::value(Real t, const Point & p) const
86 {
87  return sample(pointInGrid<false>(t, p));
88 }
89 
90 ADReal
92 {
93  return sample(pointInGrid<true>(t, p));
94 }
95 
96 ADReal
98 {
99  mooseError("The AD variant of 'sample' needs to be implemented");
100 }
101 
102 void
104  Real x,
105  unsigned int & lower_x,
106  unsigned int & upper_x) const
107 {
108  int N = in_arr.size();
109  if (x <= in_arr[0])
110  {
111  lower_x = 0;
112  upper_x = 0;
113  }
114  else if (x >= in_arr[N - 1])
115  {
116  lower_x = N - 1;
117  upper_x = N - 1;
118  }
119  else
120  {
121  // returns up which points at the first element in inArr that is not less than x
122  std::vector<double>::iterator up = std::lower_bound(in_arr.begin(), in_arr.end(), x);
123 
124  // std::distance returns std::difference_type, which can be negative in theory, but
125  // in this context will always be >=0. Therefore the explicit cast is just to shut
126  // the compiler up.
127  upper_x = static_cast<unsigned int>(std::distance(in_arr.begin(), up));
128  if (in_arr[upper_x] == x)
129  lower_x = upper_x;
130  else
131  lower_x = upper_x - 1;
132  }
133 }
Container for holding a function defined on a grid of arbitrary dimension.
Definition: GriddedData.h:34
Base class for function objects.
Definition: Function.h:37
static InputParameters validParams()
Create new PiecewiseMultiInterpolation object.
std::unique_ptr< GriddedData > _gridded_data
object to provide function evaluations at points on the grid
std::vector< std::vector< Real > > _grid
the grid
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
Utility class template for a semidynamic vector with a maximum size N and a chosen dynamic size...
void getNeighborIndices(std::vector< Real > in_arr, Real x, unsigned int &lower_x, unsigned int &upper_x) const
Operates on monotonically increasing in_arr.
std::vector< int > _axes
_axes specifies how to embed the grid into the MOOSE coordinate frame if _axes[i] = 0 then the i_th a...
unsigned int _dim
dimension of the grid
DualReal ADReal
Definition: ADRealForward.h:14
MooseADWrapper< GridPoint, is_ad > pointInGrid(const MooseADWrapper< Real, is_ad > &t, const MooseADWrapper< Point, is_ad > &p) const
convert cartesian+time coordinates into grid coordinates
typename MooseADWrapperStruct< T, is_ad >::type MooseADWrapper
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
PiecewiseMultiInterpolation(const InputParameters &parameters)
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
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...
virtual Real value(Real t, const Point &pt) const override
Given t and p, return the interpolated value.
virtual Real sample(const GridPoint &pt) const =0
This does the core work.
static InputParameters validParams()
Class constructor.
Definition: Function.C:15