QMCPACK
OneDimCubicSplineLinearGrid.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2022 QMCPACK developers.
6 //
7 // File developed by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
8 //
9 // File created by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 
13 #ifndef QMCPLUSPLUS_LINEAR_GRID_CUBIC_SPLINE_H
14 #define QMCPLUSPLUS_LINEAR_GRID_CUBIC_SPLINE_H
15 
16 #include "OneDimCubicSpline.h"
17 #include "OneDimCubicSpline.h"
19 
20 namespace qmcplusplus
21 {
22 /** combined OneDimCubicSpline and LinearGrid
23  * OneDimCubicSpline contains OneDimGridBase pointer and calls its virtual function members. This doesn't work well on a GPU.
24  * Since the use case is OneDimCubicSpline with LinearGrid. We fuse both classes and avoid any virtual functions.
25  * There are two splint functions. The one with one paramemter r is intended for testing or being called on the CPU.
26  * The static one with many parameters is intended to be used(inlined) inside a GPU kernel.
27  */
28 template<class T>
30 {
31 public:
33  {
34  auto& grid = dynamic_cast<const LinearGrid<T>&>(cublis_spliner.grid());
35  r_min_ = grid.rmin();
36  r_max_ = grid.rmax();
37  delta_inv_ = grid.DeltaInv;
38 
39  const size_t num_points = grid.size();
40  X_.resize(num_points);
41  for (size_t i = 0; i < num_points; i++)
42  X_[i] = *(grid.data() + i);
43 
44  first_deriv_ = cublis_spliner.first_deriv;
45  const_value_ = cublis_spliner.ConstValue;
46  m_Y_.resize(num_points);
47  m_Y2_.resize(num_points);
48  for (size_t i = 0; i < num_points; i++)
49  {
50  m_Y_[i] = cublis_spliner.m_Y[i];
51  m_Y2_[i] = cublis_spliner.m_Y2[i];
52  }
53  X_.updateTo();
54  m_Y_.updateTo();
55  m_Y2_.updateTo();
56  }
57 
58  /** compute the function value at r
59  */
60  T splint(T r) const
61  {
62  return splint(r_min_, r_max_, X_.data(), delta_inv_, m_Y_.data(), m_Y2_.data(), first_deriv_, const_value_, r);
63  }
64 
65  /** compute the function value at r.
66  * Need to pass in all the parameters.
67  */
68  static T splint(T r_min,
69  T r_max,
70  const T* X,
71  T delta_inv,
72  const T* m_Y,
73  const T* m_Y2,
74  T first_deriv,
75  T const_value,
76  T r)
77  {
78  if (r < r_min)
79  {
80  return m_Y[0] + first_deriv * (r - r_min);
81  }
82  else if (r >= r_max)
83  {
84  return const_value;
85  }
86 
87  const size_t loc = std::floor((r - r_min) * delta_inv);
88  const T dist = r - X[loc];
89  const T delta = X[loc + 1] - X[loc];
90  CubicSplineEvaluator<T> eval(dist, delta);
91  return eval.cubicInterpolate(m_Y[loc], m_Y[loc + 1], m_Y2[loc], m_Y2[loc + 1]);
92  }
93 
94  const auto& get_m_Y() const { return m_Y_; }
95  const auto& get_m_Y2() const { return m_Y2_; }
96  T get_first_deriv() const { return first_deriv_; }
97  T get_const_value() const { return const_value_; }
98 
99  T get_r_min() const { return r_min_; }
100  T get_r_max() const { return r_max_; }
101  const auto& get_X() const { return X_; }
102  double get_delta_inv() const { return delta_inv_; }
103 
104 private:
105  // spline related
106  /// data for the function on the grid
108  /// data for the function on the grid
110  /// first derivative for handling r < r_min_
112  /// const value for handling r > r_max_
114 
115  // grid related info
116  /// use spline above r_min_. If below, use first deriv extrapolation
118  /// use spline below r_min_. If above, use const value
120  /// the location of grid points
122  /// 1/grid space
123  double delta_inv_;
124 };
125 
126 } // namespace qmcplusplus
127 #endif
T first_deriv_
first derivative for handling r < r_min_
T1 cubicInterpolate(T1 y1, T1 y2, T1 d2y1, T1 d2y2) const
One-Dimensional linear-grid.
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
T r_max_
use spline below r_min_. If above, use const value
Vector< T, OffloadAllocator< T > > m_Y_
data for the function on the grid
Perform One-Dimensional Cubic Spline Interpolation using M-relation.
T const_value_
const value for handling r > r_max_
combined OneDimCubicSpline and LinearGrid OneDimCubicSpline contains OneDimGridBase pointer and calls...
OneDimCubicSplineLinearGrid(const OneDimCubicSpline< T > &cublis_spliner)
T splint(T r) const
compute the function value at r
Vector< T, OffloadAllocator< T > > X_
the location of grid points
T r_min_
use spline above r_min_. If below, use first deriv extrapolation
static T splint(T r_min, T r_max, const T *X, T delta_inv, const T *m_Y, const T *m_Y2, T first_deriv, T const_value, T r)
compute the function value at r.
const grid_type & grid() const
return the radial grid
Vector< T, OffloadAllocator< T > > m_Y2_
data for the function on the grid
MakeReturn< UnaryNode< FnFloor, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t floor(const Vector< T1, C1 > &l)
data_type m_Y
data for the function on the grid