-
Notifications
You must be signed in to change notification settings - Fork 0
/
heun.c
101 lines (94 loc) · 3.61 KB
/
heun.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
/*
* 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>
void sim_heun(Model_t * m, double tt, double tau){
int i, j, step;
int nreactions, nspecies;
int **rs, **ps, **as;
double *y2, *f, *f2;
double *state;
propensityFunc * prop;
double **params;
int nsteps;
double *rates;
/* Get the pointers */
nreactions = m->Nreactions;
nspecies = m->nspecies;
prop = m->prop;
params = m->params;
state = dzeros(nspecies);
rates = dzeros(nreactions);
for(i=0; i<nspecies; i++) state[i] = (double) m->istate[i];
rs = m->rstoichiometry;
ps = m->pstoichiometry;
as = m->acting_species;
f = dvector(nspecies);
f2 = dvector(nspecies);
y2 = dvector(nspecies);
/* 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");
if(tau == 0){
report_error("Heun method requires a strictly positive time step\n");
exit(1);
} else {
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]);
}
/* Step 1: compute d = stoichiometry * L
* f(y) = stoich. * propensities
* and Y2
*/
for(i=0; i<nspecies; i++){
f[i] = 0;
for(j=0; j<nreactions; j++) {
f[i] += (ps[j][i] - rs[j][i]) * rates[j];
}
/* Y2 = y + A21 * (tau * f(y) + d) */
y2[i] = state[i] + tau * f[i];
}
/* Step 2: Compute propensities for Y2 */
for(j=0; j< nreactions; j++){
rates[j] = prop[j](y2, nspecies, rs[j], params[j], as[j]);
}
/* Step 3: compute f(Y2) = stoich. * propensities
* and state[n+1]
*/
for(i=0; i<nspecies; i++){
f2[i] = 0;
for(j=0; j<nreactions; j++) {
f2[i] += (ps[j][i] - rs[j][i]) * rates[j];
}
/* y_{n+1} = y_{n} + h/2 * (f(t, Y1) + f(t + h, Y2)) */
state[i] = state[i] + 0.5 * tau * (f[i] + f2[i]);
}
printf("%g ", tau * (step+1));
for(i=0; i<nspecies; i++) printf("%g ", state[i]);
printf("\n");
}
}
return;
}