-
Notifications
You must be signed in to change notification settings - Fork 0
/
nrk3m.c
158 lines (146 loc) · 5.34 KB
/
nrk3m.c
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
/*
* Copyright (c) 2013 Pau Rué <[email protected]>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "methods.h"
#include "time.h"
#include<unistd.h>
#define NRK3_A21 9.302165557301426e-02
#define NRK3_A32 2.753884901801833e-01
#ifdef PRINT_RUNTIME
clock_t start, end;
#endif
void sim_nrk3m(Model_t * m, double tt, double tau){
int i, j, step;
long seed;
int nreactions, nspecies;
int **rs, **stoich, **as;
double *L, *d, *y, *f;
double *state;
propensityFunc * prop;
double **params;
int nsteps;
double *rates;
const gsl_rng_type * type = gsl_rng_default;
gsl_rng * r;
/* Get the pointers */
nreactions = m->Nreactions;
nspecies = m->nspecies;
stoich = imatrix(nspecies, nreactions);
rs = m->rstoichiometry;
for(i=0; i< nspecies;i++){
for(j=0; j< nreactions; j++){
stoich[i][j] = (m->pstoichiometry[j][i] - rs[j][i]);
}
}
prop = m->prop;
params = m->params;
state = dzeros(nspecies);
rates = dzeros(nreactions);
for(i=0; i<nspecies; i++) state[i] = (double) m->istate[i];
as = m->acting_species;
gsl_rng_env_setup();
r = gsl_rng_alloc (type);
seed = time(NULL) * getpid();
gsl_rng_set (r, seed); // set seed
L = dvector(nreactions);
d = dvector(nspecies);
f = dvector(nspecies);
y = dvector(nspecies);
#ifdef OUTPUT_SPECIES
/* Header: column names */
printf("#time ");
for(i=0; i<nspecies; i++) printf("%s ", m->species[i]);
printf("\n");
printf("0 ");
for(i=0; i<nspecies; i++) printf("%ld ", (long) state[i]);
printf("\n");
#endif
if(tau == 0){
report_error("Tau-leap requires a strictly positive time step\n");
exit(1);
} else {
#ifdef PRINT_RUNTIME
start = clock();
#endif
nsteps = (int) ceil(tt / tau);
for(step=0; step < nsteps; step++) {
/* Step 0: Compute propensities and L(tau,x) = Pois(tau*x) -tau*x */
for(j=0; j< nreactions; j++){
rates[j] = prop[j](state, nspecies, rs[j], params[j], as[j]);
L[j] = gsl_ran_poisson (r, tau * rates[j]) - tau * rates[j];
}
/* Step 1: compute d = stoichiometry * L
* f(y) = stoich. * propensities and Y2
*/
for(i=0; i<nspecies; i++){
d[i] = 0;
f[i] = 0;
for(j=0; j<nreactions; j++) {
d[i] += (stoich[i][j]) * L[j];
f[i] += (stoich[i][j]) * rates[j];
}
/* Y2 = y + A21 * (tau * f(y) + d) */
y[i] = state[i] + NRK3_A21 * (tau * f[i] + d[i]);
}
/* Step 2: Compute propensities for Y2 */
for(j=0; j< nreactions; j++){
rates[j] = prop[j](y, nspecies, rs[j], params[j], as[j]);
}
/* Step 3: compute f(Y2) = stoich. * propensities
* and Y3
*/
for(i=0; i<nspecies; i++){
f[i] = 0;
for(j=0; j<nreactions; j++) {
f[i] += (stoich[i][j]) * rates[j];
}
/* Y3 = y + A31 * (tau * f(Y2) + d) */
y[i] = state[i] + NRK3_A32 * (tau * f[i] + d[i]);
}
/* Step 4: Compute propensities for Y3 */
for(j=0; j< nreactions; j++){
rates[j] = prop[j](y, nspecies, rs[j], params[j], as[j]);
}
/* Step 5: compute f(Y3) = stoich. * propensities and states[n+1]
*/
for(i=0; i<nspecies; i++){
f[i] = 0;
for(j=0; j<nreactions; j++) {
f[i] += (stoich[i][j]) * rates[j];
}
/* y = ROUND(y + tau * f(Y3) + d) */
state[i] += (tau * f[i] + d[i]);
if(state[i]<0) state[i] = 0;
}
#ifdef OUTPUT_SPECIES
printf("%g ", tau * (step+1));
for(i=0; i<nspecies; i++) printf("%ld ", (long) state[i]);
printf("\n");
#endif
}
#ifdef PRINT_RUNTIME
end = clock();
printf("%g ", (double) (end - start)/CLOCKS_PER_SEC);
#endif
#ifdef OUTPUT_SPECIES
printf("%g ", tau * (step+1));
for(i=0; i<nspecies; i++) printf("%ld ", (long) state[i]);
printf("\n");
#endif
}
gsl_rng_free(r);
return;
}