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 
13 template <>
16 {
18  params.addParam<FileName>(
19  "data_file",
20  "File holding data for use with PiecewiseMultiInterpolation. Format: any empty line and any "
21  "line "
22  "beginning with # are ignored, all other lines are assumed to contain relevant information. "
23  "The file must begin with specification of the grid. This is done through lines containing "
24  "the keywords: AXIS X; AXIS Y; AXIS Z; or AXIS T. Immediately following the keyword line "
25  "must be a space-separated line of real numbers which define the grid along the specified "
26  "axis. These data must be monotonically increasing. After all the axes and their grids "
27  "have been specified, there must be a line that is DATA. Following that line, function "
28  "values are given in the correct order (they may be on individual lines, or be "
29  "space-separated on a number of lines). When the function is evaluated, f[i,j,k,l] "
30  "corresponds to the i + j*Ni + k*Ni*Nj + l*Ni*Nj*Nk data value. Here i>=0 corresponding to "
31  "the index along the first AXIS, j>=0 corresponding to the index along the second AXIS, etc, "
32  "and Ni = number of grid points along the first AXIS, etc.");
33  return params;
34 }
35 
37  : Function(parameters),
38  _gridded_data(libmesh_make_unique<GriddedData>(getParam<FileName>("data_file"))),
39  _dim(_gridded_data->getDim())
40 {
41  _gridded_data->getAxes(_axes);
42  _gridded_data->getGrid(_grid);
43 
44  // GriddedData does not require monotonicity of axes, but we do
45  for (unsigned int i = 0; i < _dim; ++i)
46  for (unsigned int j = 1; j < _grid[i].size(); ++j)
47  if (_grid[i][j - 1] >= _grid[i][j])
48  mooseError("PiecewiseMultiInterpolation needs monotonically-increasing axis data. Axis ",
49  i,
50  " contains non-monotonicity at value ",
51  _grid[i][j]);
52 
53  // GriddedData does not demand that each axis is independent, but we do
54  std::set<int> s(_axes.begin(), _axes.end());
55  if (s.size() != _dim)
56  mooseError(
57  "PiecewiseMultiInterpolation needs the AXES to be independent. Check the AXIS lines in "
58  "your data file.");
59 }
60 
62 
63 Real
64 PiecewiseMultiInterpolation::value(Real t, const Point & p)
65 {
66  // convert the inputs to an input to the sample function using _axes
67  std::vector<Real> pt_in_grid(_dim);
68  for (unsigned int i = 0; i < _dim; ++i)
69  {
70  if (_axes[i] < 3)
71  pt_in_grid[i] = p(_axes[i]);
72  else if (_axes[i] == 3) // the time direction
73  pt_in_grid[i] = t;
74  }
75  return sample(pt_in_grid);
76 }
77 
78 void
80  Real x,
81  unsigned int & lower_x,
82  unsigned int & upper_x)
83 {
84  int N = in_arr.size();
85  if (x <= in_arr[0])
86  {
87  lower_x = 0;
88  upper_x = 0;
89  }
90  else if (x >= in_arr[N - 1])
91  {
92  lower_x = N - 1;
93  upper_x = N - 1;
94  }
95  else
96  {
97  // returns up which points at the first element in inArr that is not less than x
98  std::vector<double>::iterator up = std::lower_bound(in_arr.begin(), in_arr.end(), x);
99 
100  // std::distance returns std::difference_type, which can be negative in theory, but
101  // in this context will always be >=0. Therefore the explicit cast is just to shut
102  // the compiler up.
103  upper_x = static_cast<unsigned int>(std::distance(in_arr.begin(), up));
104  if (in_arr[upper_x] == x)
105  lower_x = upper_x;
106  else
107  lower_x = upper_x - 1;
108  }
109 }
virtual Real value(Real t, const Point &pt) override
Given t and p, return the interpolated value.
Container for holding a function defined on a grid of arbitrary dimension.
Definition: GriddedData.h:33
Base class for function objects.
Definition: Function.h:40
PetscInt N
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...
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
static PetscErrorCode Vec x
virtual Real sample(const std::vector< Real > &pt)=0
This does the core work.
void getNeighborIndices(std::vector< Real > in_arr, Real x, unsigned int &lower_x, unsigned int &upper_x)
Operates on monotonically increasing in_arr.
InputParameters validParams< PiecewiseMultiInterpolation >()
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
PiecewiseMultiInterpolation(const InputParameters &parameters)
Create new PiecewiseMultiInterpolation object.
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...
InputParameters validParams< Function >()
Definition: Function.C:14