QMCPACK
OneDimGridBase.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) 2016 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
8 // Miguel Morales, moralessilva2@llnl.gov, Lawrence Livermore National Laboratory
9 // Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
10 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
11 //
12 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
13 //////////////////////////////////////////////////////////////////////////////////////
14 
15 
16 #ifndef QMCPLUSPLUS_ONEDIMGRID_BASE
17 #define QMCPLUSPLUS_ONEDIMGRID_BASE
18 
19 /**@file OneDimGridBase.h
20  *@brief Decalaration of One-Dimesional grids
21  */
22 
23 #include <algorithm>
24 #include <cmath>
25 #include <memory>
26 #include "OhmmsPETE/OhmmsVector.h"
27 #include "Numerics/GridTraits.h"
28 #include "einspline/bspline_base.h"
29 
30 namespace qmcplusplus
31 {
32 /** An abstract base class to implement a One-Dimensional grid
33  */
34 template<class T, class CT = Vector<T>>
36 {
37  using value_type = T;
38  using Array_t = CT;
39 
40 
41  int GridTag;
45  ///differential spacing of the grid
47  double DeltaInv;
48 
49  ///array to store the radial grid data
51 
52 
53  inline OneDimGridBase() : GridTag(0), num_points(0), lower_bound(0), upper_bound(0), Delta(0), DeltaInv(0.) {}
54 
55  virtual std::unique_ptr<OneDimGridBase<T, CT>> makeClone() const = 0;
56 
57  virtual ~OneDimGridBase() = default;
58 
59  inline int getGridTag() const { return GridTag; }
60 
61  inline int getIndex(T r) const
62  {
63  int k;
64  int klo = 0;
65  int khi = this->size();
66  while (khi - klo > 1)
67  {
68  k = (khi + klo) >> 1;
69  if (X[k] > r)
70  khi = k;
71  else
72  klo = k;
73  }
74  return klo;
75  }
76 
77  ///assign a value
78  inline T& operator[](int i) { return X[i]; }
79  ///assign a value
80  inline T& operator()(int i) { return X[i]; }
81  ///return a value
82  inline T operator[](int i) const { return X[i]; }
83  ///return a value
84  inline T operator()(int i) const { return X[i]; }
85 
86  inline const T* data() const { return &(X[0]); }
87  inline T* data() { return &(X[0]); }
88 
89  ///return the differential spacing of the grid
90  inline T dh() const { return Delta; }
91  ///returns \f$ r(i)\f$
92  inline T r(int i) const { return X[i]; }
93  ///returns \f$ r(i+1)-r(i)\f$
94  inline T dr(int i) const { return X[i + 1] - X[i]; }
95  ///returns the size of the grid
96  inline int size() const { return num_points; }
97  ///return the first grid point
98  inline T rmin() const { return lower_bound; }
99  ///return the last grid point
100  inline T rmax() const { return upper_bound; }
101 
102  template<typename T1>
103  inline int getIndexAndDistanceFromGridPoint(T r, T1& dist) const
104  {
105  //Find Loc
106  int Loc = locate(r);
107  dist = (r - X[Loc]);
108  return Loc;
109  }
110 
111  /** evaluate the index of r
112  * @param r current position
113  *
114  * The grid index satisfies \f$ X[Loc] \ge r < X[Loc+1]\f$.
115  */
116  virtual int locate(T r) const = 0;
117 
118  /** Set the grid given the parameters.
119  *@param ri initial grid point
120  *@param rf final grid point
121  *@param n number of grid points
122  */
123  virtual void set(T ri, T rf, int n) = 0;
124 };
125 
126 /** One-Dimensional linear-grid.
127  *
128  * The analytic form \f$ r_i = r_0 +
129  * i\left( \frac{r_f - r_0}{N-1} \right), \f$
130  * where \f$ N \f$ is the number of points and the index
131  * \f$ i \f$ runs from 0 to \f$ N-1 \f$.
132  */
133 template<class T, class CT = Vector<T>>
134 struct LinearGrid : public OneDimGridBase<T, CT>
135 {
143 
144  std::unique_ptr<OneDimGridBase<T, CT>> makeClone() const override
145  {
146  return std::make_unique<LinearGrid<T, CT>>(*this);
147  }
148 
149  inline int locate(T r) const override { return static_cast<int>((static_cast<double>(r) - X[0]) * DeltaInv); }
150 
151  inline void set(T ri, T rf, int n) override
152  {
154  lower_bound = ri;
155  upper_bound = rf;
156  num_points = n;
157  // Delta is the differential spacing
158  X.resize(n);
159  double Delta_DP = (static_cast<double>(rf) - static_cast<double>(ri)) / static_cast<double>(n - 1);
160  Delta = Delta_DP;
161  DeltaInv = 1.0 / Delta_DP;
162  for (int i = 0; i < n; i++)
163  X[i] = ri + Delta_DP * i;
164  }
165 
166  inline Ugrid einspline_grid()
167  {
168  Ugrid grid;
169  grid.start = lower_bound;
170  grid.end = upper_bound;
171  grid.num = num_points;
172  grid.delta = Delta;
173  grid.delta_inv = DeltaInv;
174 
175  return grid;
176  }
177 };
178 
179 /** One-Dimensional logarithmic-grid.
180  *
181  * The analytic form \f$ r_i = r_0
182  * \left( \frac{r_f}{r_0} \right) ^{\frac{i}{N-1}}, \f$
183  * where \f$ N \f$ is the number of points and the index
184  * \f$ i \f$ runs from 0 to \f$ N-1 \f$
185  */
186 template<class T, class CT = Vector<T>>
187 struct LogGrid : public OneDimGridBase<T, CT>
188 {
195  // T Delta;
197 
198  std::unique_ptr<OneDimGridBase<T, CT>> makeClone() const override { return std::make_unique<LogGrid<T, CT>>(*this); }
199 
200  inline int locate(T r) const override { return static_cast<int>(std::log(r / X[0]) * OneOverLogDelta); }
201 
202  inline void set(T ri, T rf, int n) override
203  {
205  lower_bound = ri;
206  upper_bound = rf;
207  num_points = n;
208  // r(i) = ri*(rf/ri)^(i/(n-1))
209  // this expression is equal to:
210  // r(i) = ri*exp(dlog_ratio*i)
211  // where dlog_ratio = (1/(n-1))*log(rf/ri)
212  // dlog_ratio is the differential spacing
213  double ratio = rf / ri;
214  double log_ratio = std::log(ratio);
215  double dlog_ratio = log_ratio / static_cast<double>(n - 1);
216  X.resize(n);
217  X[0] = ri;
218  for (int i = 1; i < n; i++)
219  X[i] = ri * std::exp(i * dlog_ratio);
220  Delta = dlog_ratio;
221  OneOverLogDelta = 1.0 / dlog_ratio;
222  }
223 };
224 
225 /** One-Dimensional logarithmic-grid starting at the origin (Used in Siesta).
226  *
227  * The analytic form \f$ r_i = B
228  * \left[ \exp(Ai)-1 \right] , \f$
229  * where the number of points is \f$ N \f$ and the index
230  * \f$ i \f$ runs from 0 to \f$ N-1 \f$
231  */
232 template<class T, class CT = Vector<T>>
233 struct LogGridZero : public OneDimGridBase<T, CT>
234 {
243 
244  std::unique_ptr<OneDimGridBase<T, CT>> makeClone() const override
245  {
246  return std::make_unique<LogGridZero<T, CT>>(*this);
247  }
248 
249  inline int locate(T r) const override { return static_cast<int>(std::log(r * OneOverB + 1.0) * OneOverA); }
250 
251  /** the meaing of ri/rf are different from the convetions of other classes
252  * @param ri ratio
253  * @param rf norm
254  * @param n number of grid, [0,n)
255  */
256  inline void set(T ri, T rf, int n) override
257  {
259  lower_bound = ri;
260  upper_bound = rf;
261  num_points = n;
262  OneOverA = 1.0 / ri;
263  OneOverB = 1.0 / rf;
264  X.resize(n);
265  for (int i = 0; i < n; i++)
266  X[i] = rf * (std::exp(ri * i) - 1.0);
267  Delta = 0.0;
268  }
269 };
270 
271 /** One-Dimensional numerical grid with arbitrary grid spacings.
272  *
273  * Non-Analytic grid, uses an array of values
274  * (typically read in from a file).
275  */
276 template<class T, class CT = Vector<T>>
277 struct NumericalGrid : public OneDimGridBase<T, CT>
278 {
285 
287 
288  template<class VA>
289  NumericalGrid(const VA& nv)
290  {
292  assign(nv.begin(), nv.end());
293  }
294 
295  std::unique_ptr<OneDimGridBase<T, CT>> makeClone() const override
296  {
297  return std::make_unique<NumericalGrid<T, CT>>(*this);
298  }
299 
300 
301  template<class IT>
302  void assign(IT g_first, IT g_last)
303  {
304  num_points = g_last - g_first;
305  X.resize(num_points);
306  copy(g_first, g_last, X.begin());
307  lower_bound = X[0];
308  upper_bound = X[num_points - 1];
309  int nf = num_points / 2;
310  Delta = X[nf] - X[nf - 1];
311  }
312 
313  inline void resize(int n)
314  {
315  if (X.size() != n)
316  X.resize(n);
317  }
318 
319  inline int locate(T r) const override
320  {
321  int k;
322  int klo = 0;
323  int khi = num_points;
324  //int khi=this->size()-1;
325  while (khi - klo > 1)
326  {
327  k = (khi + klo) >> 1;
328  if (X[k] > r)
329  khi = k;
330  else
331  klo = k;
332  }
333  return klo;
334  }
335 
336  inline void set(T ri, T rf, int n) override
337  {
338  lower_bound = ri;
339  upper_bound = rf;
340  num_points = n;
341  //X.resize(n);
342  //Delta = 0.0;
343  }
344 };
345 
346 template<class T>
347 std::ostream& operator<<(std::ostream& out, const OneDimGridBase<T>& rhs)
348 {
349  for (int i = 0; i < rhs.size(); i++)
350  out << i << " " << rhs.r(i) << " " << rhs.dr(i) << std::endl;
351  return out;
352 }
353 
354 } // namespace qmcplusplus
355 #endif
T operator()(int i) const
return a value
T rmin() const
return the first grid point
One-Dimensional linear-grid.
std::unique_ptr< OneDimGridBase< T, CT > > makeClone() const override
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
int locate(T r) const override
evaluate the index of r
T & operator[](int i)
assign a value
std::unique_ptr< OneDimGridBase< T, CT > > makeClone() const override
One-Dimensional logarithmic-grid starting at the origin (Used in Siesta).
Array_t X
array to store the radial grid data
int locate(T r) const override
evaluate the index of r
std::unique_ptr< OneDimGridBase< T, CT > > makeClone() const override
T dh() const
return the differential spacing of the grid
void copy(const Array< T1, 3 > &src, Array< T2, 3 > &dest)
Definition: Blitz.h:639
An abstract base class to implement a One-Dimensional grid.
int locate(T r) const override
evaluate the index of r
void assign(IT g_first, IT g_last)
virtual ~OneDimGridBase()=default
std::unique_ptr< OneDimGridBase< T, CT > > makeClone() const override
int locate(T r) const override
evaluate the index of r
One-Dimensional logarithmic-grid.
MakeReturn< UnaryNode< FnExp, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t exp(const Vector< T1, C1 > &l)
value_type Delta
differential spacing of the grid
One-Dimensional numerical grid with arbitrary grid spacings.
Define data types for any GridType.
MakeReturn< UnaryNode< FnLog, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t log(const Vector< T1, C1 > &l)
int size() const
returns the size of the grid
Declaraton of Vector<T,Alloc> Manage memory through Alloc directly and allow referencing an existing ...
T operator[](int i) const
return a value
virtual int locate(T r) const =0
evaluate the index of r
T rmax() const
return the last grid point
virtual std::unique_ptr< OneDimGridBase< T, CT > > makeClone() const =0
T dr(int i) const
returns
T & operator()(int i)
assign a value
T r(int i) const
returns
int getIndexAndDistanceFromGridPoint(T r, T1 &dist) const