-
Notifications
You must be signed in to change notification settings - Fork 6
/
eph_beta.h
220 lines (165 loc) · 5.46 KB
/
eph_beta.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* Authors of the extension Artur Tamm, Alfredo Correa
* e-mail: [email protected]
*/
#ifndef EPH_BETA
#define EPH_BETA
// external headers
#include <memory>
#include <vector>
#include <string>
#include <sstream>
#include <cassert>
#include <fstream>
#include <cstddef>
// internal headers
#include "eph_spline.h"
/*
* Stripped down version of beta(rho) class
*
*/
// TODO: consider storing data as alpha instead of beta to reduce the number of sqrt
template<typename Float = double, template<typename> class Allocator = std::allocator, template <typename _F = Float, typename _A = Allocator<Float>> class Container = std::vector>
class EPH_Beta {
public:
using Spline = EPH_Spline<Float, Allocator, Container>;
using Container_Float = Container<Float, Allocator<Float>>;
EPH_Beta() :
n_elements {0},
r_cutoff {0},
rho_cutoff {0}
{}
EPH_Beta(const char* file) {
std::ifstream fd(file);
assert(fd.is_open());
char line[max_line_length];
// read first three lines
// these are comments so we ignore them
fd.getline(line, max_line_length);
fd.getline(line, max_line_length);
fd.getline(line, max_line_length);
// read the header
fd >> n_elements;
assert(n_elements > 0);
element_name.resize(n_elements);
element_number.resize(n_elements);
rho.resize(n_elements);
alpha.resize(n_elements);
beta.resize(n_elements);
rho_r_sq.resize(n_elements);
// read the number of elements and their names
fd.getline(line, max_line_length);
std::string str(line);
std::istringstream strstream(str);
for(size_t i = 0; i < n_elements; ++i) {
std::string elem;
strstream >> elem;
element_name[i] = elem;
}
// read spline parameters
size_t n_points_rho;
size_t n_points_beta;
Float dr;
Float dr_sq;
Float drho;
fd >> n_points_rho;
fd >> dr;
fd >> n_points_beta;
fd >> drho;
fd >> r_cutoff;
r_cutoff_sq = r_cutoff * r_cutoff;
rho_cutoff = drho * (n_points_beta - 1);
dr_sq = r_cutoff_sq / (n_points_rho - 1);
// read spline knots for rho and beta for each element
for(size_t i = 0; i < n_elements; ++i) {
// workaround to read an uint8_t
unsigned short val; // TODO: change this;
fd >> val;
element_number[i] = val;
Container_Float l_rho(n_points_rho);
for(size_t j = 0; j != n_points_rho; ++j)
fd >> l_rho[j];
rho[i] = Spline(dr, l_rho);
// create square version
for(size_t j = 0; j != n_points_rho; ++j)
l_rho[j] = rho[i](sqrt(j * dr_sq));
rho_r_sq[i] = Spline(dr_sq, l_rho);
Container_Float l_beta(n_points_beta);
for(size_t j = 0; j != n_points_beta; ++j)
fd >> l_beta[j];
beta[i] = Spline(drho, l_beta);
// create alpha from beta
for(size_t j = 0; j != n_points_beta; ++j)
l_beta[j] = sqrt(l_beta[j]);
alpha[i] = Spline(drho, l_beta);
}
fd.close();
}
size_t get_n_elements() const {
return n_elements;
}
Float get_r_cutoff() const {
return r_cutoff;
}
Float get_r_cutoff_sq() const {
return r_cutoff_sq;
}
Float get_rho_cutoff() const {
return rho_cutoff;
}
uint8_t get_element_number(size_t index) const {
assert(index < n_elements);
return element_number[index];
}
std::string get_element_name(size_t index) const {
assert(index < n_elements);
return element_name[index];
}
Float get_rho(size_t index, Float r) const {
assert(index < n_elements);
assert(r < r_cutoff);
return rho[index](r);
}
Float get_rho_r_sq(size_t index, Float r_sq) const {
assert(index < n_elements);
assert(r_sq < r_cutoff_sq);
return rho_r_sq[index](r_sq);
}
Float get_beta(size_t index, Float rho_i) {
assert(index < n_elements && "eph_beta::get_beta() ERROR: index out of range");
if(rho_i > rho_cutoff) {
if(!beta_zero_warning) {
std::cout << "WARNING: function get_beta() in eph_beta.h: rho_i > rho_cutoff, beta set to zero\n";
beta_zero_warning = true;
}
return 0.;
}
return beta[index](rho_i);
}
Float get_alpha(size_t index, Float rho_i) {
assert(index < n_elements && "eph_beta::get_alpha() ERROR: index out of range");
if(rho_i > rho_cutoff) {
if(!beta_zero_warning) {
std::cout << "WARNING: function get_alpha() in eph_beta.h: rho_i > rho_cutoff, alpha set to zero\n";
beta_zero_warning = true;
}
return 0.;
}
return alpha[index](rho_i);
}
protected:
static constexpr unsigned int max_line_length = 1024; // this is for parsing
Float r_cutoff; // cutoff for locality
Float r_cutoff_sq; // cutoff sq for locality mostly unused
Float rho_cutoff; // cutoff for largest site density
size_t n_elements; // number of elements
Container<uint8_t, Allocator<uint8_t>> element_number;
Container<std::string, Allocator<std::string>> element_name;
Container<Spline, Allocator<Spline>> rho;
Container<Spline, Allocator<Spline>> rho_r_sq;
Container<Spline, Allocator<Spline>> alpha;
Container<Spline, Allocator<Spline>> beta;
bool beta_zero_warning {false}; // this t
};
using Beta = EPH_Beta<Float, Allocator, Container>;
#endif