-
Notifications
You must be signed in to change notification settings - Fork 10
/
utilities.cc
139 lines (125 loc) · 3.9 KB
/
utilities.cc
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
/**
* @file utilities.cc
* @author Luca Maccione
* @email luca.maccione@desy.de
* @brief Implementation of useful functions
*/
#include "utilities.h"
#include <iostream>
#include <fstream>
#include <math.h>
#include <algorithm>
#include "constants.h"
#include "input.h"
#include "grid.h"
using namespace std;
void Utility::solve_tridag(vector<double>& a, vector<double>& b, vector<double>& c, vector<double>& r, vector<double>& u, int n) {
int j = 0;
double bet = 0.0;
vector<double> gam(n,0.); //double gam[n];
// One vector of workspace, gam, is needed.
if (b[0] == 0.0) cerr << "Error 1 in tridag: the first diagonal term is 0!! " << endl;
//If this happens, then you should rewrite your equations as a set of order N-1, with u1 trivially eliminated.
bet = b[0];
u[0] = r[0] / bet;
for (j = 1; j < n; j++) { //Decomposition and forward substitution.
//double* gm = gam+j;
//(*gm) = c[j-1]/bet;
gam[j] = c[j-1]/bet;
//bet = b[j] - a[j]*(*gm);
bet = b[j] - a[j]*gam[j];
if (bet == 0.0){
cout << "j = 0 " << " --> diagonal term b[0] = " << b[0] << " off diagonal term a[0] = " << a[0] << " c[0] = " << c[0] << " u[0] = " << u[0] << " bet = b[0] " << endl;
cout << "j = " << j << " --> diagonal term b[j] = " << b[j] << " off diagonal term a[j] = " << a[j] << " gam[j] = " << gam[j] << " bet = b[j] - a[j]*c[j-1]/bet " << bet << endl;
cerr << "Error 2 in tridag: bet = 0!" << endl;
}
u[j] = (r[j] - a[j]*u[j-1])/bet;
}
for (j = (n-2); j >= 0; j--)
u[j] -= gam[j+1]*u[j+1]; //Backsubstitution.
return ;
}
/*
void Utility::solve_tridag(double* a, double* b, double* c, double* r, double* u, int n) {
int j = 0;
double bet = 0.0;
double gam[n];
// One vector of workspace, gam, is needed.
if (b[0] == 0.0) cerr << "Error 1 in tridag" << endl;
//If this happens, then you should rewrite your equations as a set of order N-1, with u1 trivially eliminated.
u[0] = r[0] / (bet = b[0]);
double* gm;
for (j = 1; j < n; j++) { //Decomposition and forward substitution.
a++;
r++;
u++;
b++;
gm = gam+j;
(*gm) = (*c)/bet;
c++;
bet = *b - (*a)*(*gm);
if (bet == 0.0) cerr << "Error 2 in tridag" << endl;
*(u) = (*r - (*a)*(*(u-1)))/bet;
}
u--;
for (j = (n-2); j >= 0; j--) {
*u -= (*gm)*(*(u+1)); //Backsubstitution.
u--;
gm--;
}
return ;
}
*/
void Utility::insert_data(
int i,
double E,
double BC, double sigma_BC,
double CO, double sigma_CO,
double NO, double sigma_NO,
double phi,
int experiment,
Utility::data* data_set
)
{
data_set[i].E = E;
data_set[i].BC = BC;
data_set[i].sigma_BC = sigma_BC;
data_set[i].CO = CO;
data_set[i].sigma_CO = sigma_CO;
data_set[i].NO = NO;
data_set[i].sigma_NO = sigma_NO;
data_set[i].phi = phi;
data_set[i].experiment = experiment;
return;
}
void Utility::insert_data_be(
int i,
double E,
double Be, double sigma_Be,
double phi,
int experiment,
Utility::data_be* data_set
)
{
data_set[i].E = E;
data_set[i].Be = Be;
data_set[i].sigma_Be = sigma_Be;
data_set[i].phi = phi;
data_set[i].experiment = experiment;
return;
}
void Utility::id_nuc(int uid, int& A, int& Z) {
if (uid == -999) {
A = 1;
Z = -1;
return ;
}
if (uid == -998) {
A = 2;
Z = -1;
return ;
}
A = int(uid%1000);
Z = int(uid/1000);
return ;
}