-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcortex.cpp
More file actions
executable file
·246 lines (207 loc) · 7.45 KB
/
cortex.cpp
File metadata and controls
executable file
·246 lines (207 loc) · 7.45 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
// ~~~~~~~~~~~~~~~~~~~
// c o r t e x . c p p
// ~~~~~~~~~~~~~~~~~~~
// ~~~~~~~~~~~~~~~~~~
// T. Erdmann
// T.R. Sokolowski
// 2009 - 2012
// ~~~~~~~~~~~~~~~~~~~
#include "cortex.hpp"
using namespace std;
void cortex::init_cortex(MTRand & mtrand,const run_parameter input)
{
// Declare array of nuclei
no_r_nucl = input.no_r_nucl;
no_z_nucl = input.no_z_nucl;
no_nucl = no_r_nucl*no_z_nucl;
// Create spatial array of nuclei
nuclear_array = new nucleus[no_r_nucl*no_z_nucl];
for(int n=0; n<no_r_nucl*no_z_nucl; n++)
nuclear_array[n].init(input.no_reac,input.no_spec,input.no_obs);
// Find the neighbours and assign the parameters
assign_neighbours(input);
assign_reactions(mtrand,input);
assign_diff_para(mtrand,input);
assign_observables(mtrand,input);
assign_configuration(mtrand,input);
// Calculate propensities and next reaction time
for(unsigned int nucl = 0; nucl < no_nucl; nucl++)
{
nuclear_array[nucl].init_propensities(mtrand);
}
// Init the next-event-time scheduler
init_nuclear_queue();
}
int cortex::assign_neighbours(const run_parameter input)
{
no_neig = input.no_neig;
for(unsigned int r_nucl = 0; r_nucl < no_r_nucl; r_nucl++)
{
for(unsigned int z_nucl = 0; z_nucl < no_z_nucl; z_nucl++)
{
nuclear_array[z_nucl+r_nucl*no_z_nucl].init_neighbours(no_neig);
}
}
for(unsigned int r_nucl = 0; r_nucl < no_r_nucl; r_nucl++)
{
for(unsigned int z_nucl = 0; z_nucl < no_z_nucl; z_nucl++)
{
unsigned int this_nucl = z_nucl+r_nucl*no_z_nucl;
unsigned int neig_nucl;
unsigned int r_neig_nucl;
unsigned int z_neig_nucl;
r_neig_nucl = r_nucl;
if(z_nucl == 0)
z_neig_nucl = 0;
else
z_neig_nucl = z_nucl-1;
neig_nucl = z_neig_nucl+r_neig_nucl*no_z_nucl;
nuclear_array[this_nucl].set_neighbour(0,neig_nucl);
if(r_nucl == 0)
r_neig_nucl = no_r_nucl-1;
else
r_neig_nucl = r_nucl-1;
z_neig_nucl = z_nucl;
neig_nucl = z_neig_nucl+r_neig_nucl*no_z_nucl;
nuclear_array[this_nucl].set_neighbour(1,neig_nucl);
r_neig_nucl = r_nucl;
if(z_nucl == no_z_nucl-1)
z_neig_nucl = no_z_nucl-1;
else
z_neig_nucl = z_nucl+1;
neig_nucl = z_neig_nucl+r_neig_nucl*no_z_nucl;
nuclear_array[this_nucl].set_neighbour(2,neig_nucl);
if(r_nucl == no_r_nucl-1)
r_neig_nucl = 0;
else
r_neig_nucl = r_nucl+1;
z_neig_nucl = z_nucl;
neig_nucl = z_neig_nucl+r_neig_nucl*no_z_nucl;
nuclear_array[this_nucl].set_neighbour(3,neig_nucl);
}
}
return(EXIT_SUCCESS);
}
int cortex::assign_reactions(MTRand & mtrand,const run_parameter input)
{
unsigned int no_reac = input.no_reac;
if(no_reac != nuclear_array[mtrand.randInt(no_nucl-1)].get_no_reac())
{
cerr << "Parameter input.no_reac does not match no_reac in constructed nucleus!" << endl;
exit(EXIT_FAILURE);
}
for(unsigned int r_nucl = 0; r_nucl < no_r_nucl; r_nucl++)
{
for(unsigned int z_nucl = 0; z_nucl < no_z_nucl; z_nucl++)
{
nuclear_array[z_nucl+r_nucl*no_z_nucl].init_reactions(input.reaction);
}
}
return(EXIT_SUCCESS);
}
int cortex::assign_diff_para(MTRand & mtrand,const run_parameter input)
{
unsigned int no_spec = input.no_spec;
if(no_spec != nuclear_array[mtrand.randInt(no_nucl-1)].get_no_spec())
{
cerr << "Parameter input.no_spec does not match no_spec in constructed nucleus!" << endl;
exit(EXIT_FAILURE);
}
double * tmp_diff_para = new double[no_spec];
for(unsigned int r_nucl = 0; r_nucl < no_r_nucl; r_nucl++)
{
for(unsigned int z_nucl = 0; z_nucl < no_z_nucl; z_nucl++)
{
for(unsigned int spec = 0; spec < no_spec; spec++)
{
tmp_diff_para[spec] = input.diff_rate_const[spec+z_nucl*no_spec+r_nucl*no_z_nucl*no_spec];
}
nuclear_array[z_nucl+r_nucl*no_z_nucl].init_diff_para(tmp_diff_para);
}
}
delete [] tmp_diff_para;
return(EXIT_SUCCESS);
}
int cortex::assign_observables(MTRand & mtrand,const run_parameter input)
{
unsigned int no_obs = input.no_obs;
if(no_obs!= nuclear_array[mtrand.randInt(no_nucl-1)].get_no_obs())
{
cerr << "Parameter input.no_obs does not match no_obs in constructed nucleus!" << endl;
exit(EXIT_FAILURE);
}
for(unsigned int r_nucl = 0; r_nucl < no_r_nucl; r_nucl++)
{
for(unsigned int z_nucl = 0; z_nucl < no_z_nucl; z_nucl++)
{
nuclear_array[z_nucl+r_nucl*no_z_nucl].init_observables(input.obs);
}
}
return(EXIT_SUCCESS);
}
int cortex::assign_configuration(MTRand & mtrand,const run_parameter input)
{
unsigned int no_spec = input.no_spec;
if(no_spec != nuclear_array[mtrand.randInt(no_nucl-1)].get_no_spec())
{
cerr << "Parameter input.no_spec does not match no_spec in constructed nucleus!" << endl;
exit(EXIT_FAILURE);
}
double * tmp_config = new double[no_spec];
for(unsigned int r_nucl = 0; r_nucl < no_r_nucl; r_nucl++)
{
for(unsigned int z_nucl = 0; z_nucl < no_z_nucl; z_nucl++)
{
for(unsigned int spec = 0; spec < no_spec; spec++)
{
tmp_config[spec] = input.configuration[spec+z_nucl*no_spec+r_nucl*no_z_nucl*no_spec];
}
nuclear_array[z_nucl+r_nucl*no_z_nucl].init_config(tmp_config);
}
}
delete [] tmp_config;
return(EXIT_SUCCESS);
}
int cortex::init_nuclear_queue(void)
{
double * tmp_reac_time = new double[no_nucl];
for(unsigned int nucl = 0; nucl < no_nucl;nucl++)
{
tmp_reac_time[nucl] = nuclear_array[nucl].get_time();
}
nuclear_queue.init(no_nucl,tmp_reac_time);
delete [] tmp_reac_time;
return(EXIT_SUCCESS);
}
void cortex::gillespie_step(MTRand & mtrand)
{
unsigned int neig_nucl = no_nucl, neig_spec;
unsigned int this_nucl = nuclear_queue.get_index(0);
double this_time = nuclear_queue.get_value(0);
// Do the Gillespie step for the upmost nucleus in the tree
nuclear_array[this_nucl].gillespie_step(mtrand,neig_nucl,neig_spec);
// Get the updated nucleus' new next-event time
unsigned int next_pos = 0;
double new_time = nuclear_array[this_nucl].get_time();
// Put the new time into the tree
nuclear_queue.tree_updt(0,this_time,new_time);
// Now accordingly update the neighbours if a diffusion event happened
// (then neig_nucl is set to some value in [0,no_nucl), so the condition is true)
if(neig_nucl < no_nucl)
{
nuclear_array[neig_nucl].add_molecule(neig_spec,this_time,mtrand);
new_time = nuclear_array[neig_nucl].get_time();
for(unsigned int nucl = 0; nucl < no_nucl; nucl++)
{
if(nuclear_queue.get_index(nucl) == neig_nucl)
{
next_pos = nucl;
break;
}
}
// Put the new time into the tree again
nuclear_queue.tree_updt(next_pos,this_time,new_time);
}
// Rearrange the tree, putting the smallest next-time to the root
nuclear_queue.tree_rsrt(next_pos);
}