-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnucleus.cpp
More file actions
executable file
·276 lines (246 loc) · 7.72 KB
/
Copy pathnucleus.cpp
File metadata and controls
executable file
·276 lines (246 loc) · 7.72 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// ~~~~~~~~~~~~~~~~~~~~~
// n u c l e u s . c p p
// ~~~~~~~~~~~~~~~~~~~~~
// ~~~~~~~~~~~~~~~~~~~~~
// T. Erdmann
// T.R. Sokolowski
// 2009 - 2012
// ~~~~~~~~~~~~~~~~~~~~~~
#include "nucleus.hpp"
using namespace std;
nucleus::nucleus()
{
// Create an empty dummy nucleus
// nucleus.init() has to be run from upstream functions
// to fully initialize the nuclear parameters.
no_reac = 0;
no_spec = 0;
no_obs = 0;
}
// Principal initialization: Assign number of reactions, species and observables
// for subsequent array creation
void nucleus::init(const int nr, const int ns, const int no)
{
no_reac = nr;
no_spec = ns;
no_obs = no;
}
// Initialize reaction network
void nucleus::init_reactions(const reaction_class* reaction_ext)
{
reaction = new reaction_class[no_reac];
for(unsigned int reac=0; reac<no_reac; reac++) reaction[reac] = reaction_ext[reac];
}
// Initialize diffusion parameters
void nucleus::init_diff_para(const double* diff_para_ext)
{
diff_para = new double[no_spec];
for(unsigned int spec = 0; spec < no_spec; spec++)
{
diff_para[spec] = diff_para_ext[spec];
}
}
// Initialize observables
void nucleus::init_observables(const observable* obs_ext)
{
obs = new observable[no_obs];
for(unsigned int s = 0; s < no_obs; s++)
{
obs[s] = obs_ext[s];
}
}
// Initialize configuration
void nucleus::init_config(const double* config_ext)
{
configuration = new double[no_spec];
for(unsigned int spec = 0; spec < no_spec; spec++)
{
configuration[spec] = config_ext[spec];
}
update_observables();
}
// Initialize reaction & diffusion propensities
void nucleus::init_propensities(MTRand &mtrand)
{
// Propensities
sum_reac_prop = 0.0;
reac_prop = new double[no_reac];
for(unsigned int reac = 0; reac < no_reac; reac++)
{
reac_prop[reac] = reaction[reac].prop(configuration);
sum_reac_prop += reac_prop[reac];
}
sum_diff_prop = 0.0;
diff_prop = new double[no_spec];
for(unsigned int spec = 0; spec < no_spec; spec++)
{
diff_prop[spec] = configuration[spec]*diff_para[spec];
sum_diff_prop += diff_prop[spec];
}
// Next reaction time
if((sum_reac_prop+sum_diff_prop) > 0)
{
next_reac_time = mtrand.randExp(1.0/(sum_reac_prop+sum_diff_prop));
}
else
{
next_reac_time = ARTIFICIAL_INFINITY;
}
}
// Set the neighbours of a nucleus
void nucleus::init_neighbours(const unsigned int no_neighbours)
{
no_neig = no_neighbours;
my_neig = new unsigned int[no_neig];
}
void nucleus::set_neighbour(const unsigned int index,const unsigned int position)
{
if(index >= no_neig)
{
cerr << "Tried to assign more neighours than possible! " << endl;
exit(EXIT_FAILURE);
}
my_neig[index] = position;
}
// Gillespie step
// This is the central routine called for subsequent Monte Carlo updates
int nucleus::gillespie_step(MTRand & mtrand,unsigned int & neig_nucl,unsigned int & neig_spec)
{
// Check if at the last update everything went fine
bool error = false;
for(unsigned int spec = 0; spec < no_spec; spec++)
{
if(configuration[spec]<0) error = true;
}
if(sum_reac_prop<=0.0) error = true;
if(error){
cerr << "NEGATIVE COPY NUMBER AND/OR NON-POSITIVE PROPENSITY SUM! Something is wrong!" << endl;
cerr << "sum_reac_prop = " << sum_reac_prop << endl;
cerr << "sum_diff_prop = " << sum_diff_prop << endl;
cerr << "last_reac = " << last_reac << endl;
cerr << "Configuration:" << endl;
for(unsigned int spec = 0; spec < no_spec; spec++)
{
cerr << configuration[spec] << " ";
}
cerr << endl;
exit(EXIT_FAILURE);
}
if(mtrand.randDblExc()*(sum_reac_prop+sum_diff_prop) <= sum_reac_prop)
{
// Reaction step
double rand_prop = mtrand.randDblExc()*sum_reac_prop;
unsigned int this_reac;
for(this_reac = 0; this_reac < no_reac; this_reac++)
{
rand_prop -= reac_prop[this_reac];
if(rand_prop < 0) break;
}
// Fire the reaction = update configuration
reaction[this_reac].fire(configuration);
// Update propensities
sum_reac_prop = 0.0;
for(unsigned int reac = 0; reac < no_reac; reac++)
{
reac_prop[reac] = reaction[reac].prop(configuration);
sum_reac_prop += reac_prop[reac];
}
sum_diff_prop = 0.0;
for(unsigned int spec = 0; spec < no_spec; spec++)
{
diff_prop[spec] = configuration[spec]*diff_para[spec];
sum_diff_prop += diff_prop[spec];
}
// Update next reaction time
if((sum_reac_prop+sum_diff_prop) > 0)
{
next_reac_time += mtrand.randExp(1.0/(sum_reac_prop+sum_diff_prop));
}
else
{
next_reac_time = ARTIFICIAL_INFINITY;
}
// Remember which reaction fired
last_reac = this_reac;
// no neighbouring nucleus was bothered
}
else
{ // Diffusion step
unsigned int this_spec;
double rand_prop = mtrand.randDblExc()*sum_diff_prop;
for(this_spec = 0; this_spec < no_spec; this_spec++)
{
rand_prop -= diff_prop[this_spec];
if(rand_prop < 0) break;
}
// Update configuration
// ( only in this nucleus; the update in the neighbouring
// nucleus receiving the molecule is done by the upstream
// routine using nucleus::add_molecule )
configuration[this_spec] -= 1;
// Update propensities
sum_reac_prop = 0.0;
for(unsigned int reac = 0; reac < no_reac; reac++)
{
reac_prop[reac] = reaction[reac].prop(configuration);
sum_reac_prop += reac_prop[reac];
}
sum_diff_prop = 0.0;
diff_prop[this_spec] = configuration[this_spec] * diff_para[this_spec];
for(unsigned int spec = 0; spec < no_spec; spec++)
{
sum_diff_prop += diff_prop[spec];
}
// Update next reaction time
if((sum_reac_prop+sum_diff_prop) > 0)
{
next_reac_time += mtrand.randExp(1.0/(sum_reac_prop+sum_diff_prop));
}
else
{
next_reac_time = ARTIFICIAL_INFINITY;
}
// Identify receiving neighbour (chosen randomly from the neighbours)
neig_nucl = my_neig[mtrand.randInt(no_neig-1)];
neig_spec = this_spec;
}
update_observables();
return(EXIT_SUCCESS);
}
int nucleus::add_molecule(const unsigned int neig_spec,const double this_time,MTRand &mtrand)
{
// Update configuration and observables
configuration[neig_spec] += 1;
update_observables();
// Update propensities
sum_reac_prop = 0.0;
for(unsigned int reac = 0; reac < no_reac; reac++)
{
reac_prop[reac] = reaction[reac].prop(configuration);
sum_reac_prop += reac_prop[reac];
}
sum_diff_prop = 0.0;
diff_prop[neig_spec] = configuration[neig_spec]*diff_para[neig_spec];
for(unsigned int spec = 0; spec < no_spec; spec++)
{
sum_diff_prop += diff_prop[spec];
}
// Update next reaction time
if((sum_reac_prop+sum_diff_prop) > 0)
{
next_reac_time = this_time + mtrand.randExp(1.0/(sum_reac_prop+sum_diff_prop));
}
else
{
next_reac_time = ARTIFICIAL_INFINITY;
}
return(EXIT_SUCCESS);
}
void nucleus::update_observables(void)
{
for(int s=0; s<no_obs; s++)
{
obs[s].value = 0.0;
for(int c=0; c<obs[s].no_components; c++) obs[s].value += 1.0*configuration[obs[s].component[c]];
}
}