ampsci
High-precision calculations for one- and two-valence atomic systems
DiracSpinor.hpp
1#pragma once
2#include <cstdint>
3#include <memory>
4#include <string>
5#include <utility>
6#include <vector>
7class Grid;
8
9/*!
10@brief Stores radial Dirac spinor: F_nk = (f, g)
11@details
12\f[
13\psi_{n\kappa m} = \frac{1}{r}
14\begin{pmatrix}
15 f_{n\kappa}(r)\,\Omega_{\kappa m}\\
16 g_{n\kappa}(r)\,\Omega_{-\kappa m}
17\end{pmatrix},
18\quad
19F_{n\kappa} =
20\begin{pmatrix}
21 f_{n\kappa}(r)\\
22 g_{n\kappa}(r)
23\end{pmatrix}
24\f]
25
26\par Construction.
27Takes in constant n and k=kappa values + grid
28 - A shared pointer to the Grid is stored (to avoid many copies of Grid)
29
30\par Operator Overloads
31 - Intuative operator overloads are provided (Fa, Fb are DiracSpinors):
32 - v * Fa, where v is a double or vector works the obvious way
33 - Fa * Fb = <Fa|Fb>
34 - Fa == Fb returns true if {na,ka}=={nb,kb}
35 - Fa > Fb : first compares n, and then kappa (via kappa_index)
36 - Fa +/- Fb : Adds/subtracts the two spinors (and updates p0/pinf)
37 - You can make copies: auto Fnew = Fa
38 - And you can re-asign: Fb = Fa (provided Fa and Fb have same n and kappa!)
39 - n and kappa are constant, cannot be changed. Avoids angular errors.
40 - all 'set_' functions return mutable references to variables
41*/
43
44public:
45 //! Constructor: Requires n (PQN), kappa (Dirac QN), and grid (shared pointer,
46 //! as it's a shared resource)
47 DiracSpinor(int in_n, int in_kappa, std::shared_ptr<const Grid> in_rgrid);
48 using Index = uint16_t;
49
50private:
51 // Radial Grid; links F[i] to F(r)
52 std::shared_ptr<const Grid> m_rgrid;
53 // Principal quantum number
54 int m_n;
55 // Dirac quantum number, kappa
56 int m_kappa;
57 // Single-particle energy, not including rest energy
58 double m_en = 0.0;
59 // Upper (large) radial component
60 std::vector<double> m_f;
61 // Lower (small) radial component
62 std::vector<double> m_g;
63 // `practical zero': p0 is first non-zero point for f(r) [usually p0=0]
64 std::size_t m_p0 = 0;
65 // `practical infinity': pinf is last non-zero point for f(r)
66 std::size_t m_pinf;
67 // Number of iterations until energy convergence (for latest routine only)
68 int m_its = -1;
69 // Fractional energy convergence: eps = |(en'-en)/en|
70 double m_eps = -1.0;
71 // Occupation fraction. =1 for closed shells. =1/(2j+1) for valence
72 double m_occ_frac = 0.0;
73
74 // 2j, l, pi, kappa_index (for convenience): these are defined by n and kappa
75 int m_twoj;
76 int m_l;
77 int m_parity;
78 std::size_t m_kappa_index;
79 Index m_nkappa_index;
80
81 // flag for regular electron, or "exotic"
82 bool m_exotic{false};
83
84public:
85 //! Principal quantum number, n
86 int n() const { return m_n; }
87 //! Dirac quantum number, kappa
88 int kappa() const { return m_kappa; }
89 //! Orbital angular momentum Q number
90 int l() const { return m_l; }
91 //! j(j+1)
92 double jjp1() const { return 0.25 * double(m_twoj * (m_twoj + 2)); }
93 int twoj() const { return m_twoj; }
94 //! 2j+1
95 int twojp1() const { return m_twoj + 1; }
96 //! (-1)^l, returns +/- 1
97 int parity() const { return m_parity; }
98 //! kappa index (see AtomData)
99 std::size_t k_index() const { return m_kappa_index; }
100 //! (n,kappa) index (see AtomData)
101 Index nk_index() const { return m_nkappa_index; }
102 //! Single-electron term symbol (e.g., 6s_1/2). Gnuplot=true => 6s_{1/2}
103 std::string symbol(bool gnuplot = false) const;
104 //! e.g., 6p_1/2 => 6p-, 6p_3/2 => 6p+
105 std::string shortSymbol() const;
106
107 //! Checks if spinor is for "exotic" lepton, or regular electron
108 bool &exotic() { return m_exotic; }
109 //! Checks if spinor is for "exotic" lepton, or regular electron
110 bool exotic() const { return m_exotic; }
111
112 //! Changes 'kappa' angular quantum number. Use with caution!
113 void set_new_kappa(int new_kappa);
114
115 //! Resturns a const reference to the radial grid
116 const Grid &grid() const { return *m_rgrid; };
117 //! Resturns copy of shared_ptr to grid [shared resource] - used when we want
118 //! to construct a new DiracSpinor that shares this grid
119 std::shared_ptr<const Grid> grid_sptr() const { return m_rgrid; };
120
121 //! Single-particle energy, not including rest energy
122 double en() const { return m_en; }
123 double &en() { return m_en; }
124
125 //! Upper (large) radial component function, f
126 const std::vector<double> &f() const { return m_f; }
127 std::vector<double> &f() { return m_f; }
128 //! Upper (large) radial component, f(r_i)
129 double f(std::size_t i) const { return m_f.at(i); }
130 double &f(std::size_t i) { return m_f.at(i); }
131
132 //! Lower (small) radial component function, g
133 const std::vector<double> &g() const { return m_g; }
134 std::vector<double> &g() { return m_g; }
135 //! Lower (small) radial component, g(r_i)
136 double g(std::size_t i) const { return m_g.at(i); }
137 double &g(std::size_t i) { return m_g.at(i); }
138
139 //! First non-zero point (index for f[i])
140 // XXX Kill this?
141 auto min_pt() const { return m_p0; }
142 auto &min_pt() { return m_p0; }
143
144 //! Effective size(); index _after_ last non-zero point (index for f[i])
145 auto max_pt() const { return m_pinf; }
146 auto &max_pt() { return m_pinf; }
147
148 //! r0 = r[min_pt] (in atomic units) XXX Kill this?
149 double r0() const;
150 //! rinf = r[max_pt]
151 double rinf() const;
152
153 //! Number of iterations until energy convergence (for latest routine only)
154 auto its() const { return m_its; }
155 auto &its() { return m_its; }
156
157 //! Occupation fraction. =1 for closed shells. =1/(2j+1) for valence
158 auto occ_frac() const { return m_occ_frac; }
159 auto &occ_frac() { return m_occ_frac; }
160
161 //! Fractional energy convergence: eps = |(en'-en)/en|
162 auto eps() const { return m_eps; }
163 auto &eps() { return m_eps; }
164
165 //! Scales DiracSpinor (f and g) by constant
166 const DiracSpinor &scale(const double factor);
167 //! Scales DiracSpinor (f and g) by function of r
168 const DiracSpinor &scale(const std::vector<double> &v);
169 //! By default normalises to 1, but can normalise to other number.
170 void normalise(double norm_to = 1.0);
171 //! Forces f(r) and g(r) to be zero outside of [p0,pinf)
172 void zero_boundaries();
173
174 //! Returns the norm, defined: Norm = Sqrt[<a|a>]
175 double norm() const;
176 //! Returns the norm^2, defined: Norm2 = <a|a>
177 double norm2() const;
178
179 //! Returns [f[p0]/f_max , f[pinf]/f_max] - for tests
180 std::pair<double, double> r0pinfratio() const;
181
182 //! rho(r) = sum_m |Psi^2|(r) = (2j+1) * x_occ * |F^2|(r)
183 std::vector<double> rho() const;
184
185 //! Number of occupied electrons: (2j+1)*occ_frac
186 int num_electrons() const;
187
188public:
189 // Operator overloads
190
191 //! Returns radial integral (Fa,Fb) = Int(fa*fb + ga*gb)
192 friend double operator*(const DiracSpinor &Fa, const DiracSpinor &Fb);
193
194 //! Addition of two DiracSpinors - must have same kappa
196 DiracSpinor &operator-=(const DiracSpinor &rhs);
197 friend DiracSpinor operator+(DiracSpinor lhs, const DiracSpinor &rhs);
198 friend DiracSpinor operator-(DiracSpinor lhs, const DiracSpinor &rhs);
199
200 //! Scalar multiplication
201 DiracSpinor &operator*=(const double x);
202 friend DiracSpinor operator*(DiracSpinor Fa, const double x);
203 friend DiracSpinor operator*(const double x, DiracSpinor Fa);
204
205 //! Multiplication by array (function)
206 DiracSpinor &operator*=(const std::vector<double> &v);
207 friend DiracSpinor operator*(const std::vector<double> &v, DiracSpinor Fa);
208
209 //! Comparitor overloads (compares n, then kappa):
210 friend bool operator==(const DiracSpinor &lhs, const DiracSpinor &rhs);
211 friend bool operator!=(const DiracSpinor &lhs, const DiracSpinor &rhs);
212 friend bool operator<(const DiracSpinor &lhs, const DiracSpinor &rhs);
213 friend bool operator>(const DiracSpinor &lhs, const DiracSpinor &rhs);
214 friend bool operator<=(const DiracSpinor &lhs, const DiracSpinor &rhs);
215 friend bool operator>=(const DiracSpinor &lhs, const DiracSpinor &rhs);
216
217 //! Custom comparitors (for sorting): l, j, kappa_index, energy
218 static bool comp_l(const DiracSpinor &lhs, const DiracSpinor &rhs) {
219 return lhs.m_l < rhs.m_l;
220 }
221 static bool comp_j(const DiracSpinor &lhs, const DiracSpinor &rhs) {
222 return lhs.m_twoj < rhs.m_twoj;
223 }
224 static bool comp_ki(const DiracSpinor &lhs, const DiracSpinor &rhs) {
225 return lhs.m_kappa_index < rhs.m_kappa_index;
226 }
227 static bool comp_n(const DiracSpinor &lhs, const DiracSpinor &rhs) {
228 return lhs.m_n < rhs.m_n;
229 }
230 static bool comp_en(const DiracSpinor &lhs, const DiracSpinor &rhs) {
231 return lhs.en() < rhs.en();
232 }
233
234 //! Writes short symbol to ostream
235 friend std::ostream &operator<<(std::ostream &ostr, const DiracSpinor &Fa) {
236 return ostr << Fa.shortSymbol();
237 }
238
239 // Static (helper) functions:
240
241 //! Returns worst |<a|b>| (or |<a|b>-1| for a=b) {val, state_names}
242 static std::pair<double, std::string>
243 check_ortho(const std::vector<DiracSpinor> &a,
244 const std::vector<DiracSpinor> &b);
245
246 //! (approximately) OrthoNormalises a set of any orbitals.
247 //! @details Note: only updates orbs, not energies
248 static void orthonormaliseOrbitals(std::vector<DiracSpinor> &orbs,
249 int num_its = 1);
250
251 //! Forces Fin to be orthogonal to orbs. If Fn (i.e., {n,k}) in in orbs, replaces Fin with that from orbs.
252 [[nodiscard]] static DiracSpinor
253 orthogonaliseWrt(const DiracSpinor &Fin,
254 const std::vector<DiracSpinor> &orbs);
255
256 //! As orthogonaliseWrt(), but normalises Fin afterwards
257 [[nodiscard]] static DiracSpinor
259 const std::vector<DiracSpinor> &orbs);
260
261 //! A more correct way to force orthogonality than normal
262 void orthog(const DiracSpinor &rhs);
263
264 //! e.g., 6p_1/2 => 6p-, 6p_3/2 => 6p+
265 static std::string shortSymbol(int n, int kappa);
266
267 //! Returns formatted states string (e.g., '7sp5d') given list of orbs
268 static std::string state_config(const std::vector<DiracSpinor> &orbs);
269
270 //! Constructs H-like (pointlike) DiracSpinor - mainly for testing
271 static DiracSpinor exactHlike(int n, int k, std::shared_ptr<const Grid> rgrid,
272 double zeff, double alpha = 0.0);
273
274 //! Searches for {n,k} in list of orbitals, returns pointer (may be null)
275 static const DiracSpinor *find(int n, int k,
276 const std::vector<DiracSpinor> &orbs);
277
278 //! Returns maximum (2j) found in {orbs}
279 static int max_tj(const std::vector<DiracSpinor> &orbs);
280 //! Returns maximum l found in {orbs}
281 static int max_l(const std::vector<DiracSpinor> &orbs);
282 //! Returns maximum n found in {orbs}
283 static int max_n(const std::vector<DiracSpinor> &orbs);
284 //! Returns maximum n found in {orbs}, for given kappa
285 static int max_n(const std::vector<DiracSpinor> &orbs, int kappa);
286 //! Returns maximum kappa_index found in {orbs}
287 static std::size_t max_kindex(const std::vector<DiracSpinor> &orbs);
288
289 //! Returns maximum Energy found in {orbs}
290 static double max_En(const std::vector<DiracSpinor> &orbs);
291 //! Returns maximum Energy found in {orbs}
292 static double min_En(const std::vector<DiracSpinor> &orbs);
293
294 //! Splits orbitals into two groups (i.e., core, excited) by energy
295 //! @details
296 //! - The first group contains orbitals with energy < energy
297 //! - The second group contains orbitals with energy > energy
298 //! - The first group is also limited to have n >= n_min_core (i.e., exclude
299 //! deep core states)
300 static std::pair<std::vector<DiracSpinor>, std::vector<DiracSpinor>>
301 split_by_energy(const std::vector<DiracSpinor> &orbitals, double Fermi_energy,
302 int n_min_core = 1, int n_max_excited = 9999,
303 bool positrons_are_excited = true);
304
305 //! Splits orbitals into two groups (i.e., core, excited).
306 //! @details
307 //! - The first group contains orbitals with {n,kappa} in the given "core"
308 //! - The second group contains all of the rest
309 //! - The first group is also limited to have n >= n_min_core (i.e., exclude
310 //! deep core states)
311 static std::pair<std::vector<DiracSpinor>, std::vector<DiracSpinor>>
312 split_by_core(const std::vector<DiracSpinor> &orbitals,
313 const std::vector<DiracSpinor> &core, int n_min_core = 1);
314
315 //! Takes a subset of an input basis (by copy), according to subset_string
316 //! @details
317 //! - Includes only states matching the subset_string
318 static std::vector<DiracSpinor> subset(const std::vector<DiracSpinor> &basis,
319 const std::string &subset_string);
320};
Stores radial Dirac spinor: F_nk = (f, g)
Definition DiracSpinor.hpp:42
std::pair< double, double > r0pinfratio() const
Returns [f[p0]/f_max , f[pinf]/f_max] - for tests.
Definition DiracSpinor.cpp:106
void normalise(double norm_to=1.0)
By default normalises to 1, but can normalise to other number.
Definition DiracSpinor.cpp:91
static DiracSpinor orthonormaliseWrt(const DiracSpinor &Fin, const std::vector< DiracSpinor > &orbs)
As orthogonaliseWrt(), but normalises Fin afterwards.
Definition DiracSpinor.cpp:434
static int max_tj(const std::vector< DiracSpinor > &orbs)
Returns maximum (2j) found in {orbs}.
Definition DiracSpinor.cpp:266
int parity() const
(-1)^l, returns +/- 1
Definition DiracSpinor.hpp:97
double norm() const
Returns the norm, defined: Norm = Sqrt[<a|a>].
Definition DiracSpinor.cpp:65
static std::pair< std::vector< DiracSpinor >, std::vector< DiracSpinor > > split_by_energy(const std::vector< DiracSpinor > &orbitals, double Fermi_energy, int n_min_core=1, int n_max_excited=9999, bool positrons_are_excited=true)
Splits orbitals into two groups (i.e., core, excited) by energy.
Definition DiracSpinor.cpp:461
static std::pair< double, std::string > check_ortho(const std::vector< DiracSpinor > &a, const std::vector< DiracSpinor > &b)
Returns worst |<a|b>| (or |<a|b>-1| for a=b) {val, state_names}.
Definition DiracSpinor.cpp:328
auto max_pt() const
Effective size(); index after last non-zero point (index for f[i])
Definition DiracSpinor.hpp:145
auto eps() const
Fractional energy convergence: eps = |(en'-en)/en|.
Definition DiracSpinor.hpp:162
const std::vector< double > & f() const
Upper (large) radial component function, f.
Definition DiracSpinor.hpp:126
int twojp1() const
2j+1
Definition DiracSpinor.hpp:95
int kappa() const
Dirac quantum number, kappa.
Definition DiracSpinor.hpp:88
static std::pair< std::vector< DiracSpinor >, std::vector< DiracSpinor > > split_by_core(const std::vector< DiracSpinor > &orbitals, const std::vector< DiracSpinor > &core, int n_min_core=1)
Splits orbitals into two groups (i.e., core, excited).
Definition DiracSpinor.cpp:487
double norm2() const
Returns the norm^2, defined: Norm2 = <a|a>
Definition DiracSpinor.cpp:66
auto occ_frac() const
Occupation fraction. =1 for closed shells. =1/(2j+1) for valence.
Definition DiracSpinor.hpp:158
const Grid & grid() const
Resturns a const reference to the radial grid.
Definition DiracSpinor.hpp:116
friend bool operator==(const DiracSpinor &lhs, const DiracSpinor &rhs)
Comparitor overloads (compares n, then kappa):
Definition DiracSpinor.cpp:206
int n() const
Principal quantum number, n.
Definition DiracSpinor.hpp:86
static DiracSpinor orthogonaliseWrt(const DiracSpinor &Fin, const std::vector< DiracSpinor > &orbs)
Forces Fin to be orthogonal to orbs. If Fn (i.e., {n,k}) in in orbs, replaces Fin with that from orbs...
Definition DiracSpinor.cpp:417
std::shared_ptr< const Grid > grid_sptr() const
Resturns copy of shared_ptr to grid [shared resource] - used when we want to construct a new DiracSpi...
Definition DiracSpinor.hpp:119
static int max_l(const std::vector< DiracSpinor > &orbs)
Returns maximum l found in {orbs}.
Definition DiracSpinor.cpp:272
std::string symbol(bool gnuplot=false) const
Single-electron term symbol (e.g., 6s_1/2). Gnuplot=true => 6s_{1/2}.
Definition DiracSpinor.cpp:32
static double min_En(const std::vector< DiracSpinor > &orbs)
Returns maximum Energy found in {orbs}.
Definition DiracSpinor.cpp:320
DiracSpinor & operator*=(const double x)
Scalar multiplication.
Definition DiracSpinor.cpp:187
static int max_n(const std::vector< DiracSpinor > &orbs)
Returns maximum n found in {orbs}.
Definition DiracSpinor.cpp:277
double en() const
Single-particle energy, not including rest energy.
Definition DiracSpinor.hpp:122
Index nk_index() const
(n,kappa) index (see AtomData)
Definition DiracSpinor.hpp:101
void zero_boundaries()
Forces f(r) and g(r) to be zero outside of [p0,pinf)
Definition DiracSpinor.cpp:94
static const DiracSpinor * find(int n, int k, const std::vector< DiracSpinor > &orbs)
Searches for {n,k} in list of orbitals, returns pointer (may be null)
Definition DiracSpinor.cpp:301
double f(std::size_t i) const
Upper (large) radial component, f(r_i)
Definition DiracSpinor.hpp:129
static std::size_t max_kindex(const std::vector< DiracSpinor > &orbs)
Returns maximum kappa_index found in {orbs}.
Definition DiracSpinor.cpp:294
double r0() const
r0 = r[min_pt] (in atomic units) XXX Kill this?
Definition DiracSpinor.cpp:135
double g(std::size_t i) const
Lower (small) radial component, g(r_i)
Definition DiracSpinor.hpp:136
void orthog(const DiracSpinor &rhs)
A more correct way to force orthogonality than normal.
Definition DiracSpinor.cpp:349
static std::vector< DiracSpinor > subset(const std::vector< DiracSpinor > &basis, const std::string &subset_string)
Takes a subset of an input basis (by copy), according to subset_string.
Definition DiracSpinor.cpp:507
double jjp1() const
j(j+1)
Definition DiracSpinor.hpp:92
std::string shortSymbol() const
e.g., 6p_1/2 => 6p-, 6p_3/2 => 6p+
Definition DiracSpinor.cpp:43
static DiracSpinor exactHlike(int n, int k, std::shared_ptr< const Grid > rgrid, double zeff, double alpha=0.0)
Constructs H-like (pointlike) DiracSpinor - mainly for testing.
Definition DiracSpinor.cpp:442
static bool comp_l(const DiracSpinor &lhs, const DiracSpinor &rhs)
Custom comparitors (for sorting): l, j, kappa_index, energy.
Definition DiracSpinor.hpp:218
bool & exotic()
Checks if spinor is for "exotic" lepton, or regular electron.
Definition DiracSpinor.hpp:108
auto min_pt() const
First non-zero point (index for f[i])
Definition DiracSpinor.hpp:141
static void orthonormaliseOrbitals(std::vector< DiracSpinor > &orbs, int num_its=1)
(approximately) OrthoNormalises a set of any orbitals.
Definition DiracSpinor.cpp:362
static double max_En(const std::vector< DiracSpinor > &orbs)
Returns maximum Energy found in {orbs}.
Definition DiracSpinor.cpp:314
int num_electrons() const
Number of occupied electrons: (2j+1)*occ_frac.
Definition DiracSpinor.cpp:130
friend std::ostream & operator<<(std::ostream &ostr, const DiracSpinor &Fa)
Writes short symbol to ostream.
Definition DiracSpinor.hpp:235
const DiracSpinor & scale(const double factor)
Scales DiracSpinor (f and g) by constant.
Definition DiracSpinor.cpp:69
int l() const
Orbital angular momentum Q number.
Definition DiracSpinor.hpp:90
DiracSpinor & operator+=(const DiracSpinor &rhs)
Addition of two DiracSpinors - must have same kappa.
Definition DiracSpinor.cpp:150
friend double operator*(const DiracSpinor &Fa, const DiracSpinor &Fb)
Returns radial integral (Fa,Fb) = Int(fa*fb + ga*gb)
Definition DiracSpinor.cpp:140
bool exotic() const
Checks if spinor is for "exotic" lepton, or regular electron.
Definition DiracSpinor.hpp:110
void set_new_kappa(int new_kappa)
Changes 'kappa' angular quantum number. Use with caution!
Definition DiracSpinor.cpp:55
std::size_t k_index() const
kappa index (see AtomData)
Definition DiracSpinor.hpp:99
double rinf() const
rinf = r[max_pt]
Definition DiracSpinor.cpp:136
std::vector< double > rho() const
rho(r) = sum_m |Psi^2|(r) = (2j+1) * x_occ * |F^2|(r)
Definition DiracSpinor.cpp:119
auto its() const
Number of iterations until energy convergence (for latest routine only)
Definition DiracSpinor.hpp:154
static std::string state_config(const std::vector< DiracSpinor > &orbs)
Returns formatted states string (e.g., '7sp5d') given list of orbs.
Definition DiracSpinor.cpp:233
const std::vector< double > & g() const
Lower (small) radial component function, g.
Definition DiracSpinor.hpp:133
Holds grid, including type + Jacobian (dr/du)
Definition Grid.hpp:31