QMCPACK
CubicSplineCommon Class Reference

The CubicSplineCommon class is a third-order spline representation of a function. More...

+ Collaboration diagram for CubicSplineCommon:

Public Member Functions

Array< double, 1 > & Data ()
 
int size ()
 Returns the interpolated value. More...
 
double operator() (double x)
 
double Deriv (double x)
 Returns the interpolated first derivative. More...
 
double Deriv2 (double x)
 Returns the interpolated second derivative. More...
 
double Deriv3 (double x)
 Returns the interpolated third derivative. More...
 
void Update ()
 Recompute the second derivatives from the function values. More...
 
void Init (std::shared_ptr< Grid > &NewGrid, Array< double, 1 > NewYs, double startderiv, double endderiv)
 Initialize the cubic spline. More...
 
void Init (std::shared_ptr< Grid > &NewGrid, Array< double, 1 > NewYs)
 Simplified form which assumes that the second derivative at both boundaries are zero. More...
 
 CubicSplineCommon (std::shared_ptr< Grid > &NewGrid, Array< double, 1 > NewYs)
 Simplified constructor. More...
 
 CubicSplineCommon (std::shared_ptr< Grid > &NewGrid, Array< double, 1 > NewYs, double startderiv, double endderiv)
 Full constructor. More...
 
double operator() (int i) const
 Returns the value of the function at the ith grid point. More...
 
double & operator() (int i)
 Returns a reference to the value at the ith grid point. More...
 
double Params (int i) const
 Returns the value of the function at the ith grid point. More...
 
double & Params (int i)
 Returns a reference to the value at the ith grid point. More...
 
void Write (IOSectionClass &outSection)
 
void Read (IOSectionClass &inSection)
 
CubicSplineCommonoperator= (const CubicSplineCommon &spline)
 
 CubicSplineCommon ()
 Trivial constructor. More...
 

Public Attributes

int NumParams
 
std::shared_ptr< Gridgrid
 
double StartDeriv
 The values of the derivative of the represented function on the boundary. More...
 
double EndDeriv
 

Private Attributes

int UpToDate
 This flag records whether or not the stored second derivatives are in sync with the function values. More...
 
Array< double, 1 > y
 The function values on the grid points. More...
 
Array< double, 1 > d2y
 The second derivatives of the function. More...
 

Detailed Description

The CubicSplineCommon class is a third-order spline representation of a function.

It stores a pointer to a grid and the values of the function and its second derivative at the points defined by the grid.

Definition at line 28 of file CubicSplineCommon.h.

Constructor & Destructor Documentation

◆ CubicSplineCommon() [1/3]

CubicSplineCommon ( std::shared_ptr< Grid > &  NewGrid,
Array< double, 1 >  NewYs 
)
inline

Simplified constructor.

Definition at line 88 of file CubicSplineCommon.h.

References EndDeriv, Init(), and StartDeriv.

89  {
90  StartDeriv = EndDeriv = 5.0e30;
91  Init(NewGrid, NewYs, 5.0e30, 5.0e30);
92  }
double StartDeriv
The values of the derivative of the represented function on the boundary.
void Init(std::shared_ptr< Grid > &NewGrid, Array< double, 1 > NewYs, double startderiv, double endderiv)
Initialize the cubic spline.

◆ CubicSplineCommon() [2/3]

CubicSplineCommon ( std::shared_ptr< Grid > &  NewGrid,
Array< double, 1 >  NewYs,
double  startderiv,
double  endderiv 
)
inline

Full constructor.

Definition at line 95 of file CubicSplineCommon.h.

References Init(), and Update().

96  {
97  Init(NewGrid, NewYs, startderiv, endderiv);
98  Update();
99  }
void Update()
Recompute the second derivatives from the function values.
void Init(std::shared_ptr< Grid > &NewGrid, Array< double, 1 > NewYs, double startderiv, double endderiv)
Initialize the cubic spline.

◆ CubicSplineCommon() [3/3]

CubicSplineCommon ( )
inline

Trivial constructor.

Definition at line 140 of file CubicSplineCommon.h.

References EndDeriv, grid, NumParams, StartDeriv, and UpToDate.

141  {
142  UpToDate = 0;
143  NumParams = 0;
144  StartDeriv = 0;
145  EndDeriv = 0;
146  grid = NULL;
147  }
double StartDeriv
The values of the derivative of the represented function on the boundary.
int UpToDate
This flag records whether or not the stored second derivatives are in sync with the function values...
std::shared_ptr< Grid > grid

Member Function Documentation

◆ Data()

Array< double, 1 > & Data ( )
inline

Definition at line 151 of file CubicSplineCommon.h.

References UpToDate, and y.

152 {
153  UpToDate = false;
154  return y;
155 }
int UpToDate
This flag records whether or not the stored second derivatives are in sync with the function values...
Array< double, 1 > y
The function values on the grid points.

◆ Deriv()

double Deriv ( double  x)
inline

Returns the interpolated first derivative.

Definition at line 200 of file CubicSplineCommon.h.

References d2y, grid, Grid::NumPoints, Grid::ReverseMap(), Update(), UpToDate, and y.

201 {
202  if (!UpToDate)
203  Update();
204 
205  Grid& X = *grid;
206  int hi = X.ReverseMap(x) + 1;
207  int low = hi - 1;
208  if (low < 0)
209  {
210  low = 0;
211  hi = 1;
212  }
213  if (hi > (X.NumPoints - 1))
214  {
215  hi = (X.NumPoints - 1);
216  low = hi - 1;
217  }
218 
219  double h = X(hi) - X(low);
220  double hinv = 1.0 / h;
221  double a = (X(hi) - x) * hinv;
222  double b = (x - X(low)) * hinv;
223  double sixinv = 0.1666666666666666666;
224 
225  return ((y(hi) - y(low)) * hinv + (h * sixinv) * ((3.0 * b * b - 1.0) * d2y(hi) - (3.0 * a * a - 1.0) * d2y(low)));
226 }
Array< double, 1 > d2y
The second derivatives of the function.
Parent class for all grids.
Definition: Grid.h:43
int UpToDate
This flag records whether or not the stored second derivatives are in sync with the function values...
std::shared_ptr< Grid > grid
int NumPoints
Number of points in the grid.
Definition: Grid.h:54
Array< double, 1 > y
The function values on the grid points.
void Update()
Recompute the second derivatives from the function values.
virtual int ReverseMap(double r)=0
Returns the index of the nearest point below r.

◆ Deriv2()

double Deriv2 ( double  x)
inline

Returns the interpolated second derivative.

Definition at line 228 of file CubicSplineCommon.h.

References d2y, grid, Grid::NumPoints, Grid::ReverseMap(), Update(), and UpToDate.

229 {
230  if (!UpToDate)
231  Update();
232  Grid& X = *grid;
233  int hi = X.ReverseMap(x) + 1;
234  int low = hi - 1;
235  if (low < 0)
236  {
237  low = 0;
238  hi = 1;
239  }
240  if (hi > (X.NumPoints - 1))
241  {
242  hi = (X.NumPoints - 1);
243  low = hi - 1;
244  }
245 
246 
247  double h = X(hi) - X(low);
248  double hinv = 1.0 / h;
249  double a = (X(hi) - x) * hinv;
250  double b = (x - X(low)) * hinv;
251  return (a * d2y(low) + b * d2y(hi));
252 }
Array< double, 1 > d2y
The second derivatives of the function.
Parent class for all grids.
Definition: Grid.h:43
int UpToDate
This flag records whether or not the stored second derivatives are in sync with the function values...
std::shared_ptr< Grid > grid
int NumPoints
Number of points in the grid.
Definition: Grid.h:54
void Update()
Recompute the second derivatives from the function values.
virtual int ReverseMap(double r)=0
Returns the index of the nearest point below r.

◆ Deriv3()

double Deriv3 ( double  x)
inline

Returns the interpolated third derivative.

Definition at line 255 of file CubicSplineCommon.h.

References d2y, grid, Grid::NumPoints, Grid::ReverseMap(), Update(), and UpToDate.

256 {
257  if (!UpToDate)
258  Update();
259  Grid& X = *grid;
260  int hi = X.ReverseMap(x) + 1;
261  int low = hi - 1;
262  if (low < 0)
263  {
264  low = 0;
265  hi = 1;
266  }
267  if (hi > (X.NumPoints - 1))
268  {
269  hi = (X.NumPoints - 1);
270  low = hi - 1;
271  }
272 
273  double h = X(hi) - X(low);
274 
275  return ((d2y(hi) - d2y(low)) / h);
276 }
Array< double, 1 > d2y
The second derivatives of the function.
Parent class for all grids.
Definition: Grid.h:43
int UpToDate
This flag records whether or not the stored second derivatives are in sync with the function values...
std::shared_ptr< Grid > grid
int NumPoints
Number of points in the grid.
Definition: Grid.h:54
void Update()
Recompute the second derivatives from the function values.
virtual int ReverseMap(double r)=0
Returns the index of the nearest point below r.

◆ Init() [1/2]

void Init ( std::shared_ptr< Grid > &  NewGrid,
Array< double, 1 >  NewYs,
double  startderiv,
double  endderiv 
)
inline

Initialize the cubic spline.

See notes about start and end deriv above.

Definition at line 64 of file CubicSplineCommon.h.

References d2y, EndDeriv, grid, NumParams, Array< T, D, ALLOC >::resize(), Array< T, D, ALLOC >::rows(), StartDeriv, Update(), and y.

Referenced by CubicSplineCommon().

65  {
66  StartDeriv = startderiv;
67  EndDeriv = endderiv;
68  if (NewGrid->NumPoints != NewYs.rows())
69  {
70  std::cerr << "Size mismatch in CubicSplineCommon.\n";
71  std::cerr << "Grid Points = " << NewGrid->NumPoints << std::endl;
72  std::cerr << "Y points = " << NewYs.rows() << std::endl;
73  exit(1);
74  }
75  grid = NewGrid;
76  NumParams = grid->NumPoints;
77  y.resize(grid->NumPoints);
78  d2y.resize(grid->NumPoints);
79  y = NewYs;
80  Update();
81  }
Array< double, 1 > d2y
The second derivatives of the function.
double StartDeriv
The values of the derivative of the represented function on the boundary.
std::shared_ptr< Grid > grid
void resize(const std::array< SIZET, D > &dims)
Resize the container.
Definition: OhmmsArray.h:65
Array< double, 1 > y
The function values on the grid points.
void Update()
Recompute the second derivatives from the function values.
auto rows() const
Definition: Blitz.h:89

◆ Init() [2/2]

void Init ( std::shared_ptr< Grid > &  NewGrid,
Array< double, 1 >  NewYs 
)
inline

Simplified form which assumes that the second derivative at both boundaries are zero.

Definition at line 85 of file CubicSplineCommon.h.

References Init().

Referenced by Init().

85 { Init(NewGrid, NewYs, 5.0e30, 5.0e30); }
void Init(std::shared_ptr< Grid > &NewGrid, Array< double, 1 > NewYs, double startderiv, double endderiv)
Initialize the cubic spline.

◆ operator()() [1/3]

double operator() ( double  x)
inline

Definition at line 158 of file CubicSplineCommon.h.

References d2y, Grid::End, grid, Grid::NumPoints, Grid::ReverseMap(), Update(), UpToDate, and y.

159 {
160  if (!UpToDate)
161  Update();
162 
163 
164  Grid& X = *grid;
165 #ifdef BZ_DEBUG
166  if (x > X.End)
167  {
168  if (x < (X.End * 1.000000001))
169  x = X.End;
170  else
171  {
172  std::cerr << "x outside grid in CubicSplineCommon.\n";
173  std::cerr << "x = " << x << " X.End = " << X.End << "\n";
174  abort();
175  }
176  }
177 #endif
178  int hi = X.ReverseMap(x) + 1;
179  int low = hi - 1;
180  if (low < 0)
181  {
182  low = 0;
183  hi = 1;
184  }
185  if (hi > (X.NumPoints - 1))
186  {
187  hi = (X.NumPoints - 1);
188  low = hi - 1;
189  }
190 
191  double h = X(hi) - X(low);
192  double hinv = 1.0 / h;
193  double a = (X(hi) - x) * hinv;
194  double b = (x - X(low)) * hinv;
195  double sixinv = 0.1666666666666666666;
196 
197  return (a * y(low) + b * y(hi) + ((a * a * a - a) * d2y(low) + (b * b * b - b) * d2y(hi)) * (h * h * sixinv));
198 }
Array< double, 1 > d2y
The second derivatives of the function.
Parent class for all grids.
Definition: Grid.h:43
int UpToDate
This flag records whether or not the stored second derivatives are in sync with the function values...
std::shared_ptr< Grid > grid
int NumPoints
Number of points in the grid.
Definition: Grid.h:54
Array< double, 1 > y
The function values on the grid points.
void Update()
Recompute the second derivatives from the function values.
virtual int ReverseMap(double r)=0
Returns the index of the nearest point below r.
double End
Definition: Grid.h:51

◆ operator()() [2/3]

double operator() ( int  i) const
inline

Returns the value of the function at the ith grid point.

Definition at line 102 of file CubicSplineCommon.h.

References y.

102 { return (y(i)); }
Array< double, 1 > y
The function values on the grid points.

◆ operator()() [3/3]

double& operator() ( int  i)
inline

Returns a reference to the value at the ith grid point.

Definition at line 104 of file CubicSplineCommon.h.

References UpToDate, and y.

105  {
106  UpToDate = 0;
107  return (y(i));
108  }
int UpToDate
This flag records whether or not the stored second derivatives are in sync with the function values...
Array< double, 1 > y
The function values on the grid points.

◆ operator=()

CubicSplineCommon& operator= ( const CubicSplineCommon spline)

◆ Params() [1/2]

double Params ( int  i) const
inline

Returns the value of the function at the ith grid point.

Definition at line 111 of file CubicSplineCommon.h.

References y.

111 { return (y(i)); }
Array< double, 1 > y
The function values on the grid points.

◆ Params() [2/2]

double& Params ( int  i)
inline

Returns a reference to the value at the ith grid point.

Definition at line 113 of file CubicSplineCommon.h.

References y.

113 { return (y(i)); }
Array< double, 1 > y
The function values on the grid points.

◆ Read()

void Read ( IOSectionClass inSection)
inline

Definition at line 124 of file CubicSplineCommon.h.

References IOSectionClass::CloseSection(), d2y, EndDeriv, grid, NumParams, IOSectionClass::OpenSection(), ReadGrid(), IOSectionClass::ReadVar(), Array< T, D, ALLOC >::resize(), Array< T, D, ALLOC >::size(), StartDeriv, Update(), and y.

125  {
126  assert(inSection.ReadVar("StartDeriv", StartDeriv));
127  assert(inSection.ReadVar("EndDeriv", EndDeriv));
128  assert(inSection.ReadVar("y", y));
129  NumParams = y.size();
131  assert(inSection.OpenSection("Grid"));
132  grid = ReadGrid(inSection);
133  inSection.CloseSection();
134  Update();
135  }
Array< double, 1 > d2y
The second derivatives of the function.
std::shared_ptr< Grid > ReadGrid(IOSectionClass &inSection)
Definition: Grid.h:633
bool OpenSection(std::string name, int num=0)
Opens the num&#39;th section with the given name.
double StartDeriv
The values of the derivative of the represented function on the boundary.
std::shared_ptr< Grid > grid
void CloseSection()
Closes the current section.
void resize(const std::array< SIZET, D > &dims)
Resize the container.
Definition: OhmmsArray.h:65
Array< double, 1 > y
The function values on the grid points.
void Update()
Recompute the second derivatives from the function values.
size_t size() const
Definition: OhmmsArray.h:57
bool ReadVar(std::string name, T &var)
Template function which reads a variable in the present section into the passed-by-reference T variab...
Definition: IO.h:168

◆ size()

int size ( void  )
inline

Returns the interpolated value.

Definition at line 51 of file CubicSplineCommon.h.

References grid.

51 { return grid->NumPoints; }
std::shared_ptr< Grid > grid

◆ Update()

void Update ( )

Recompute the second derivatives from the function values.

Referenced by CubicSplineCommon(), Deriv(), Deriv2(), Deriv3(), Init(), operator()(), and Read().

◆ Write()

void Write ( IOSectionClass outSection)
inline

Definition at line 114 of file CubicSplineCommon.h.

References IOSectionClass::CloseSection(), EndDeriv, grid, IOSectionClass::NewSection(), StartDeriv, IOSectionClass::WriteVar(), and y.

115  {
116  outSection.WriteVar("StartDeriv", StartDeriv);
117  outSection.WriteVar("EndDeriv", EndDeriv);
118  outSection.WriteVar("y", y);
119 
120  outSection.NewSection("Grid");
121  grid->Write(outSection);
122  outSection.CloseSection();
123  }
double StartDeriv
The values of the derivative of the represented function on the boundary.
std::shared_ptr< Grid > grid
bool WriteVar(std::string name, T val)
Writes a variable under the current section.
Definition: IO.h:184
void CloseSection()
Closes the current section.
void NewSection(std::string name)
Creates a new section of the same type as currentSection under currentSection.
Definition: IO.h:153
Array< double, 1 > y
The function values on the grid points.

Member Data Documentation

◆ d2y

Array<double, 1> d2y
private

The second derivatives of the function.

Definition at line 38 of file CubicSplineCommon.h.

Referenced by Deriv(), Deriv2(), Deriv3(), Init(), operator()(), and Read().

◆ EndDeriv

double EndDeriv

Definition at line 47 of file CubicSplineCommon.h.

Referenced by CubicSplineCommon(), Init(), Read(), and Write().

◆ grid

std::shared_ptr<Grid> grid

Definition at line 42 of file CubicSplineCommon.h.

Referenced by CubicSplineCommon(), Deriv(), Deriv2(), Deriv3(), Init(), operator()(), Read(), size(), and Write().

◆ NumParams

int NumParams

Definition at line 41 of file CubicSplineCommon.h.

Referenced by CubicSplineCommon(), Init(), and Read().

◆ StartDeriv

double StartDeriv

The values of the derivative of the represented function on the boundary.

If each value is greater that 1e30, we compute boundary conditions assuming that the second derivative is zero at that boundary.

Definition at line 47 of file CubicSplineCommon.h.

Referenced by CubicSplineCommon(), Init(), Read(), and Write().

◆ UpToDate

int UpToDate
private

This flag records whether or not the stored second derivatives are in sync with the function values.

It is used to determine whether the second derivatives need recomputation.

Definition at line 34 of file CubicSplineCommon.h.

Referenced by CubicSplineCommon(), Data(), Deriv(), Deriv2(), Deriv3(), and operator()().

◆ y

Array<double, 1> y
private

The function values on the grid points.

Definition at line 36 of file CubicSplineCommon.h.

Referenced by Data(), Deriv(), Init(), operator()(), Params(), Read(), and Write().


The documentation for this class was generated from the following file: