-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
189 lines (135 loc) · 4.75 KB
/
utils.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
// Tom O'Shea 2024
// utilities to load datafiles etc.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <math.h>
#include <filesystem>
#include <bits/stdc++.h>
using namespace std;
// constants
double pi = 3.14159265359; // fine structure constant
double alpha = 1/137.035999084;
double me = 510998.950; // e- mass [eV]
double Mpl = 2e27; // planck mass [eV]
double zeta3 = 1.202056903159594; // Riemann zeta(3)
double amu = 931.494e6; // amu in eV
// conversion factors
double s2eV = (6.582119569e-16); // Hz to eV
double J2eV = (1. / 1.602176634e-19); // Joules to eV (1 / e)
double m2eV = (1.973269804e-7); // m-1 to eV
double K2eV = (8.617333262e-5); // Kelvin to eV
double kg2eV = 5.609588604e35; // from hbar/c2
double T2eV = 2e2; // Tesla to eV2 conversion [eV2/T]
// solar parameters
double dSolar = 149.5978707e9/m2eV; // mean earth-sun distance [eV-1]
double rSolar = 6.957e8/m2eV; // solar radius [eV-1]
// read in datafiles in csv form
vector<double> read( string name ) {
//cout << "Reading file " << name << "..." << endl;
// open file defined in argument
fstream file;
file.open(name,ios::in);
char delim('\n'); // define delimiter for file parsing
if (file.is_open()){ // checking whether the file is open
string temp; // define temporary storage string
vector<double> row; // define vector to store input values and return
while(getline(file, temp, delim)){ // read data from file object and put it into string
double item = stod(temp); // convert string to double
row.push_back(item); // append double to vector
}
file.close(); // close the file object.
return row; // returns vector of values
}
else{ cout << "some error i guess..." << endl ; vector<double> err = {69.} ; return err ; }
}
// write out 2 column datafile
void write2D( string name, vector<double> data1, vector<double> data2) {
// delete old file if extant
if ( remove(name.c_str()) == 0 ) {
cout << "file " << name << " deleted." << endl;
}
// create new file
fstream fout;
fout.open(name, ios::out | ios::trunc);
// get size of vectors
int len1 = data1.size();
int len2 = data2.size();
// check vectors of equal length
if ( len1 == len2 ) {
// Read the input from vector
for ( int c = 0; c < len1; c++ ) {
// Insert the data to file
fout << data1[c] << " " << data2[c] << endl;
}
cout << "file " << name << " created succesfully." << endl;
}
// if vectors not equal return error
else { cout << "ERROR - ensure vectors are of equal length" << endl; }
fout.close();
}
// read in gaunt factors from matlab matrix files
vector<vector<double>> readGaunt( string name ) {
//cout << "Reading file " << name << "..." << endl;
// open file defined in argument
fstream file;
file.open(name,ios::in);
char delim(' '); // define delimiter for file parsing
if (file.is_open()){ // checking whether the file is open
string temp; // define temporary storage string
vector<vector<double>> g2D; // 2D matrix to store rows
vector<double> row; // define vector to store input values and return
vector<double> x; // define vector to store input values and return
int c = 0; // define counter for elements
int line = 0; // counter for lines
while( !file.eof() ) { // loop until end of file
getline(file, temp, delim); // get one row
// check data is not empty
if( temp == "\n" ) { continue; }
double item = stod(temp); // convert string to double
// add a zero on first line
if( c == 0 ) { row.push_back(0.); }
row.push_back(item); // append double to vector
c++;
// when row is full append to 2D vector and wipe row
if( row.size() == 201 ) {
g2D.push_back(row);
row.clear();
line++;
}
if( line == 501 ) { break; }
}
file.close(); // close the file object.
return g2D; // returns vector of values
}
else{ cout << "couldn't find file: " << name << endl ; vector<vector<double>> err = {{69.}} ; return err ; }
}
// write big matrices
void writeREST( string name, vector<double> data, int line ){ // data vector needs values of w for constant r
//int piece = 0; // for newlining
int len = data.size();
//cout<<len<<endl;
// delete old file if extant
if ( line == 0 ) {
if ( remove(name.c_str()) == 0 ) {
cout << "file " << name << " deleted." << endl;
}
}
// file pointer
fstream fout;
// creates new csv file
fout.open(name, ios::out | ios::app);
// Read the input from vector
int c = 0;
for ( double item : data ) {
// Insert the data to file
//cout<<item<<endl;
fout << item;
if(c == len-1) { fout<<endl; break; }
else { fout << " "; c++; }
}
if( line == 0 ){ cout << "writing to file " << name << "...\n" << endl; }
fout.close();
}