https://mooseframework.inl.gov
PiecewiseMultilinear.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
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 "PiecewiseMultilinear.h"
11 #include "GriddedData.h"
12 
14 
17 {
19  params.addClassDescription(
20  "PiecewiseMultilinear performs linear interpolation on 1D, 2D, 3D or 4D "
21  "data. The data_file specifies the axes directions and the function "
22  "values. If a point lies outside the data range, the appropriate end "
23  "value is used.");
24  params.addParam<Real>(
25  "epsilon", 1e-12, "Finite differencing parameter for gradient and time derivative");
26  return params;
27 }
28 
30  : PiecewiseMultiInterpolation(parameters), _epsilon(getParam<Real>("epsilon"))
31 {
32 }
33 
34 Real
36 {
37  return sampleInternal<false>(pt);
38 }
39 
40 ADReal
42 {
43  return sampleInternal<true>(pt);
44 }
45 
46 template <bool is_ad>
49 {
50  using std::abs;
51  /*
52  * left contains the indices of the point to the 'left', 'down', etc, of pt
53  * right contains the indices of the point to the 'right', 'up', etc, of pt
54  * Hence, left and right define the vertices of the hypercube containing pt
55  */
56  GridIndex left(_dim);
57  GridIndex right(_dim);
58  for (unsigned int i = 0; i < _dim; ++i)
59  getNeighborIndices(_grid[i], MetaPhysicL::raw_value(pt[i]), left[i], right[i]);
60 
61  /*
62  * The following just loops through all the vertices of the
63  * hypercube containing pt, evaluating the function at all
64  * those vertices, and weighting the contributions to the
65  * final result depending on the distance of pt from the vertex
66  */
69  GridIndex arg(_dim);
70  // number of points in hypercube = 2^_dim
71  for (unsigned int i = 0; i < (1u << _dim); ++i)
72  {
73  weight = 1;
74  for (unsigned int j = 0; j < _dim; ++j)
75  // shift i j-bits to the right and see if the result has a 0 as its right-most bit
76  if ((i >> j) % 2 == 0)
77  {
78  arg[j] = left[j];
79  if (left[j] != right[j])
80  weight *= abs(pt[j] - _grid[j][right[j]]);
81  else
82  // unusual "end condition" case. weight by 0.5 because we will encounter this twice
83  weight *= 0.5;
84  }
85  else
86  {
87  arg[j] = right[j];
88  if (left[j] != right[j])
89  weight *= abs(pt[j] - _grid[j][left[j]]);
90  else
91  // unusual "end condition" case. weight by 0.5 because we will encounter this twice
92  weight *= 0.5;
93  }
94  f += _gridded_data->evaluateFcn(arg) * weight;
95  }
96 
97  /*
98  * finally divide by the volume of the hypercube
99  */
100  weight = 1;
101  for (unsigned int dim = 0; dim < pt.size(); ++dim)
102  if (left[dim] != right[dim])
103  weight *= _grid[dim][right[dim]] - _grid[dim][left[dim]];
104  else
105  // unusual "end condition" case. weight by 1 to cancel the two 0.5 encountered previously
106  weight *= 1;
107 
108  return f / weight;
109 }
110 
112 PiecewiseMultilinear::gradient(Real t, const Point & p) const
113 {
114  RealGradient grad;
115 
116  // sample center point
117  auto s1 = sample(pointInGrid<false>(t, p));
118 
119  // sample epsilon steps in all directions
120  for (const auto dir : _axes)
121  if (dir < 3)
122  {
123  Point pp = p;
124  pp(dir) += _epsilon;
125  grad(dir) = (sample(pointInGrid<false>(t, pp)) - s1) / _epsilon;
126  }
127 
128  return grad;
129 }
130 
131 Real
132 PiecewiseMultilinear::timeDerivative(Real t, const Point & p) const
133 {
134  return (sample(pointInGrid<false>(t + _epsilon, p)) - sample(pointInGrid<false>(t, p))) /
135  _epsilon;
136 }
Moose::GenericType< Real, is_ad > sampleInternal(const Moose::GenericType< GridPoint, is_ad > pt) const
MetaPhysicL::DualNumber< V, D, asd > abs(const MetaPhysicL::DualNumber< V, D, asd > &a)
Definition: EigenADReal.h:50
Uses GriddedData to define data on a grid, and does linear interpolation on that data to provide func...
static InputParameters validParams()
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
Uses GriddedData to define data on a grid, and does linear interpolation on that data to provide func...
auto raw_value(const Eigen::Map< T > &in)
Definition: EigenADReal.h:100
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
static constexpr std::size_t dim
This is the dimension of all vector and tensor datastructures used in MOOSE.
Definition: Moose.h:162
DualNumber< Real, DNDerivativeType, true > ADReal
Definition: ADRealForward.h:42
typename std::conditional< is_ad, typename ADType< T >::type, T >::type GenericType
Definition: MooseTypes.h:689
void getNeighborIndices(std::vector< Real > in_arr, Real x, unsigned int &lower_x, unsigned int &upper_x) const
Operates on monotonically increasing in_arr.
virtual RealGradient gradient(Real t, const Point &p) const override
Function objects can optionally provide a gradient at a point.
dof_id_type weight(const MeshBase &mesh, const processor_id_type pid)
PiecewiseMultilinear(const InputParameters &parameters)
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
virtual Real timeDerivative(Real t, const Point &p) const override
Get the time derivative of the function.
GriddedData::ADGridPoint ADGridPoint
registerMooseObject("MooseApp", PiecewiseMultilinear)
virtual Real sample(const GridPoint &pt) const override
This does the core work.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
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 addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object...