ampsci
c++ program for high-precision atomic structure calculations of single-valence systems
Grid.hpp
1 #pragma once
2 #include <string>
3 #include <vector>
4 
5 //==============================================================================
6 enum class GridType { loglinear, logarithmic, linear };
7 
9 struct GridParameters {
10  std::size_t num_points;
11  double r0, rmax, b;
12  GridType type;
13 
15  GridParameters(std::size_t innum_points = 1, double inr0 = 1.0,
16  double inrmax = 1.0, double inb = 4.0,
17  GridType intype = GridType::loglinear, double indu = 0);
18 
20  GridParameters(std::size_t innum_points, double inr0, double inrmax,
21  double inb, const std::string &str_type = "loglinear",
22  double indu = 0);
23 
24  static GridType parseType(const std::string &str_type);
25  static std::string parseType(GridType type);
26 };
27 
28 //==============================================================================
29 //==============================================================================
31 class Grid {
32 
33 private:
34  // Minimum grid value
35  double m_r0;
36  // Uniform (u) grid step size
37  double m_du;
38  // GridType: loglinear, logarithmic, linear [enum]
39  GridType gridtype;
40  // log-linear grid 'turning point'
41  double m_b;
42  // Grid values r[i] = r(u)
43  std::vector<double> m_r;
44  // Convinient: (1/r)*(dr/du)[i]
45  std::vector<double> m_drduor;
46  // Jacobian (dr/du)[i]
47  std::vector<double> m_drdu;
48 
49 public:
51  Grid(double in_r0, double in_rmax, std::size_t in_num_points,
52  GridType in_gridtype, double in_b = 0);
53 
55  Grid(const GridParameters &in);
56 
58  auto r0() const { return m_r.front(); }
59  auto front() const { return m_r.front(); }
61  auto rmax() const { return m_r.back(); }
62  auto back() const { return m_r.back(); }
64  auto num_points() const { return m_r.size(); }
66  auto size() const { return num_points(); }
68  auto du() const { return m_du; }
70  auto type() const { return gridtype; }
72  auto loglin_b() const { return m_b; }
73 
75  const std::vector<double> &r() const { return m_r; }
76  auto r(std::size_t i) const { return m_r.at(i); };
77  auto at(std::size_t i) const { return m_r.at(i); };
78  auto operator()(std::size_t i) const { return r(i); }
80  const std::vector<double> &drdu() const { return m_drdu; };
81  auto drdu(std::size_t i) const { return m_drdu.at(i); };
83  const std::vector<double> &drduor() const { return m_drduor; };
84  auto drduor(std::size_t i) const { return m_drduor.at(i); };
85 
87  std::size_t getIndex(double x, bool require_nearest = false) const;
88 
90  std::string gridParameters() const;
91 
93  std::vector<double> rpow(double k) const;
94 
96  auto begin() const { return m_r.cbegin(); }
97  auto end() const { return m_r.cend(); }
98  auto cbegin() const { return m_r.cbegin(); }
99  auto cend() const { return m_r.cend(); }
100  auto rbegin() const { return m_r.crbegin(); }
101  auto rend() const { return m_r.crend(); }
102  auto crbegin() const { return m_r.crbegin(); }
103  auto crend() const { return m_r.crend(); }
104 
107 
110  void extend_to(double new_rmax);
111 
114  return GridParameters{num_points(), m_r0, rmax(), m_b, gridtype, m_du};
115  }
116 
117  // Static functions: can be called outside of instantialised object
119  static double calc_du_from_num_points(double in_r0, double in_rmax,
120  std::size_t in_num_points,
121  GridType in_gridtype, double in_b = 0);
123  static std::size_t calc_num_points_from_du(double in_r0, double in_rmax,
124  double in_du, GridType in_gridtype,
125  double in_b = 0);
126 
127 private: // helper fns
128  static double next_r_loglin(double b, double u, double r_guess);
129  static std::vector<double> form_r(const GridType type, const double r0,
130  const std::size_t num_points,
131  const double du, const double b);
132  static std::vector<double> form_drduor(const GridType type,
133  const std::vector<double> &in_r,
134  const double b);
135  static std::vector<double> form_drdu(const GridType type,
136  const std::vector<double> &in_r,
137  const std::vector<double> &in_drduor);
138 };
Holds grid, including type + Jacobian (dr/du)
Definition: Grid.hpp:31
const std::vector< double > & drduor() const
Convinient: (1/r)*(dr/du)
Definition: Grid.hpp:83
auto size() const
Number of grid points.
Definition: Grid.hpp:66
auto r0() const
Minium (first) grid point.
Definition: Grid.hpp:58
auto begin() const
Provide iterators, but only onst ones.
Definition: Grid.hpp:96
std::vector< double > rpow(double k) const
Calculates+returns vector of 1/r.
Definition: Grid.cpp:120
auto du() const
Linear step size dr = (dr/dr)*du.
Definition: Grid.hpp:68
Grid(double in_r0, double in_rmax, std::size_t in_num_points, GridType in_gridtype, double in_b=0)
Manual constructor.
Definition: Grid.cpp:64
void extend_to(double new_rmax)
Extends grid to new_r_max. Note: This is the only non-const function; use with caution.
Definition: Grid.cpp:132
std::string gridParameters() const
String human-readable grid parameters.
Definition: Grid.cpp:102
static double calc_du_from_num_points(double in_r0, double in_rmax, std::size_t in_num_points, GridType in_gridtype, double in_b=0)
Given r0/rmax + num_points, calculates du.
Definition: Grid.cpp:257
static std::size_t calc_num_points_from_du(double in_r0, double in_rmax, double in_du, GridType in_gridtype, double in_b=0)
Given r0/rmax + du, calculates num_points.
Definition: Grid.cpp:277
const std::vector< double > & drdu() const
Jacobian (dr/du)[i].
Definition: Grid.hpp:80
auto type() const
Grid-type (linear, logarithmic, loglinear)
Definition: Grid.hpp:70
auto num_points() const
Number of grid points.
Definition: Grid.hpp:64
auto rmax() const
Maximum (final) grid point.
Definition: Grid.hpp:61
std::size_t getIndex(double x, bool require_nearest=false) const
Given value, returns grid index. if not require_nearest, returns lower.
Definition: Grid.cpp:76
const std::vector< double > & r() const
Grid points, r.
Definition: Grid.hpp:75
auto loglin_b() const
log-linear grid 'turning point' (~ roughly log for r<b, lin for r>b)
Definition: Grid.hpp:72
GridParameters params() const
Returns set of paramets; may be used to construct grid.
Definition: Grid.hpp:113
Parmaters used to create Grid.
Definition: Grid.hpp:9
GridParameters(std::size_t innum_points=1, double inr0=1.0, double inrmax=1.0, double inb=4.0, GridType intype=GridType::loglinear, double indu=0)
indu is optional. Only used if innum_points = 0 [then, uses du to find N]
Definition: Grid.cpp:24