GamR  0.0.0
GammaROOT
Loading...
Searching...
No Matches
Scheme.cc
Go to the documentation of this file.
1#include "Scheme.hh"
2#include "Solver.hh"
3
4namespace GamR {
5 namespace Bateman {
6 void Scheme::ReadScheme(std::string f) {
7 FILE *file = fopen(f.c_str(), "ra");
8 if (file == NULL) { std::cout << "Scheme file " << f << " does not exist!" << std::endl; return; }
9 std::stringstream ss;
10 char cline[2048];
11
12 int ind = 0;
13 double branches[100][100];
14 double branch_errs[100][100];
15 for (int i=0; i<100; ++i) {
16 for (int j=0; j<100; ++j) {
17 branches[i][j] = 0;
18 branch_errs[i][j] = 0;
19 }
20 }
21 while(std::fgets(cline, sizeof cline, file)!=NULL) {
22 std::string line(cline);
23 if (line.size() == 0) { continue; }
24 if (line[0] == '#') { continue; }
25 if (line[0] == ';') { continue; }
26
27 ss.clear();
28 ss.str(line);
29
30 std::string type;
31 ss >> type;
32 if (type == "N") {
33 int id;
34 std::string name;
35 double tau;
36 double tau_err;
37 double eff;
38 double eff_err;
39 double N0;
40 ss >> id >> name >> tau >> tau_err >> eff >> eff_err >> N0;
41 AddNucleus(name, tau, eff, tau_err, eff_err, N0);
42 }
43 else if (type == "B") {
44 int id1, id2;
45 double br, br_err;
46 ss >> id1 >> id2 >> br >> br_err;
47 branches[id1][id2] = br;
48 branch_errs[id1][id2] = br_err;
49 }
50 }
51
52 for (int i=0; i<100; ++i) {
53 for (int j=0; j<100; ++j) {
54 if (branches[i][j] == 0) { continue; }
55 nuclei[j]->feeders.push_back(nuclei[i]);
56 nuclei[j]->branches.push_back(branches[i][j]);
57 nuclei[j]->branch_errs.push_back(branch_errs[i][j]);
58 }
59 }
60
62 NormN0();
63
64 fclose(file);
65 }
66
68 double sumN0 = 0;
69 for (int j=0; j<nuclei.size(); ++j) {
70 sumN0 += nuclei[j]->N0;
71 }
72 for (int j=0; j<nuclei.size(); ++j) {
73 nuclei[j]->N0 = nuclei[j]->N0/sumN0;
74 }
75 }
76
78 double branches[100][100];
79 double branch_errs[100][100];
80 for (int i=0; i<100; ++i) {
81 for (int j=0; j<100; ++j) {
82 branches[i][j] = 0;
83 branch_errs[i][j] = 0;
84 }
85 }
86
87 for (int j=0; j<nuclei.size(); ++j) {
88 for (int i=0; i<nuclei[j]->feeders.size(); ++i) {
89 branches[nuclei[j]->feeders[i]->index][nuclei[j]->index] = nuclei[j]->branches[i];
90 branch_errs[nuclei[j]->feeders[i]->index][nuclei[j]->index] = nuclei[j]->branch_errs[i];
91 }
92 }
93
94 for (int j=0; j<nuclei.size(); ++j) {
95 nuclei[j]->feeders.clear();
96 nuclei[j]->branches.clear();
97 nuclei[j]->branch_errs.clear();
98 }
99
100 for (int i=0; i<100; ++i) {
101 double br_tot = 0;
102 for (int j=0; j<100; ++j) {
103 if (branches[i][j] == 0) { continue; }
104 br_tot += branches[i][j];
105 }
106
107
108 for (int j=0; j<100; ++j) {
109 if (branches[i][j] == 0) { continue; }
110 double br = branches[i][j]/br_tot;
111 double err_tot = 0;
112 for (int k=0; k<100; ++k) {
113 if (branches[i][k] == 0) { continue; }
114 if (j == k ) { continue; }
115 err_tot += std::pow(branches[i][j]/(br_tot*br_tot) * branch_errs[i][k], 2);
116 }
117
118 double br_err = std::sqrt(std::pow((br_tot - branches[i][j])/(br_tot*br_tot) * branch_errs[i][j], 2) + err_tot);
119 nuclei[j]->feeders.push_back(nuclei[i]);
120 nuclei[j]->branches.push_back(br);
121 nuclei[j]->branch_errs.push_back(br_err);
122 }
123 }
124
125 }
126
127 void Scheme::operator()(const std::vector<double> &x, std::vector<double> &dxdt, const double) {
128 for (int i=0; i<x.size(); ++i) {
129 dxdt[i] = -(1.0/nuclei[i]->lifetime) * x[i];
130 for (int j=0; j<nuclei[i]->feeders.size(); ++j) {
131 dxdt[i] += nuclei[i]->branches[j] * (1.0/nuclei[i]->feeders[j]->lifetime) * x[nuclei[i]->feeders[j]->index];
132 }
133 }
134 }
135
136 void Scheme::PrintSummary(std::ostream &stream) {
137 for (int i=0; i<nuclei.size(); ++i) {
138 nuclei[i]->Print(stream);
139 }
140 }
141
142 void Scheme::PrintPops(std::ostream &stream, Solver &solver) {
143 double time = 0;
144 int i = 0;
145 while (time < solver.max_time) {
146 stream << time << " ";
147 for (int j=0; j<nuclei.size(); ++j) {
148 stream << nuclei[j]->population[i] << " ";
149 }
150 stream << std::endl;
151 ++i;
152 time += solver.stepsize;
153 }
154 }
155
156 void Scheme::PrintDecays(std::ostream &stream, Solver &solver) {
157 double time = 0;
158 int i = 0;
159 while (time < solver.max_time) {
160 stream << time << " ";
161 for (int j=0; j<nuclei.size(); ++j) {
162 stream << nuclei[j]->population[i] * 1.0/nuclei[j]->lifetime << " ";
163 }
164 stream << std::endl;
165 ++i;
166 time += solver.stepsize;
167 }
168 }
169
170 double Scheme::GetTotal(double t, Solver &solver) {
171 int i=0;
172 double time = 0.0;
173 if (t >= solver.max_time) { std::cout << "Evaluation ouside bounds!" << std::endl; return -1; }
174 if (t < 0 ) { std::cout << "Evaluation ouside bounds!" << std::endl; return -1; }
175 while (time < solver.max_time) {
176 if (t >= time && t < time+solver.stepsize) { break; }
177 ++i;
178 time += solver.stepsize;
179 }
180
181 //now we interpolate between i and i+1
182 double n0 = 0.0;
183 double n1 = 0.0;
184 for (int j=0; j<nuclei.size(); ++j) {
185 n0 += nuclei[j]->efficiency * nuclei[j]->population[i] * 1.0/nuclei[j]->lifetime;
186 n1 += nuclei[j]->efficiency * nuclei[j]->population[i+1] * 1.0/nuclei[j]->lifetime;
187 }
188 double frac = (t - time)/solver.stepsize;
189 return n0+frac*(n1-n0);
190 }
191
192 double Scheme::Get(int indx, double t, Solver &solver) {
193 int i=0;
194 double time = 0.0;
195 if (t >= solver.max_time) { std::cout << "Evaluation ouside bounds!" << std::endl; return -1; }
196 if (t < 0 ) { std::cout << "Evaluation ouside bounds!" << std::endl; return -1; }
197 while (time < solver.max_time) {
198 if (t >= time && t < time+solver.stepsize) { break; }
199 ++i;
200 time += solver.stepsize;
201 }
202
203 //now we interpolate between i and i+1
204 double n0 = 0.0;
205 double n1 = 0.0;
206 n0 = nuclei[indx]->efficiency * nuclei[indx]->population[i] * 1.0/nuclei[indx]->lifetime;
207 n1 += nuclei[indx]->efficiency * nuclei[indx]->population[i+1] * 1.0/nuclei[indx]->lifetime;
208 double frac = (t - time)/solver.stepsize;
209 return n0+frac*(n1-n0);
210 }
211
212 }
213}
void ss(std::vector< int > indexes, TCanvas *canvas, Option_t *option)
std::vector< Nucleus * > nuclei
Definition Scheme.hh:15
void PrintDecays(std::ostream &stream, Solver &solver)
Definition Scheme.cc:156
void PrintSummary(std::ostream &stream)
Definition Scheme.cc:136
void ReadScheme(std::string f)
Definition Scheme.cc:6
void AddNucleus(std::string n, double lt, double eff, double lt_err, double eff_err, double N0)
Definition Scheme.hh:17
void operator()(const std::vector< double > &x, std::vector< double > &dxdt, const double)
Definition Scheme.cc:127
double Get(int indx, double t, Solver &solver)
Definition Scheme.cc:192
double GetTotal(double t, Solver &solver)
Definition Scheme.cc:170
void PrintPops(std::ostream &stream, Solver &solver)
Definition Scheme.cc:142
Definition Gain.cc:19