forked from Conedy/Conedy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegrator.h
222 lines (150 loc) · 4.49 KB
/
integrator.h
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
#ifndef integrator_h
#define integrator_h integrator_h
#include "baseType.h"
#include "gslNoise.h"
using namespace std;
#ifndef M_PI
#define M_PI 3.14159265358979323846f
#endif
class odeIntegrator
{
protected:
baseType * dxdt;
public:
odeIntegrator (unsigned int size) {
dxdt = (baseType *) calloc (size, sizeof(baseType));
for (unsigned int i = 0; i < size; i++)
{
dxdt[i] = 0;
}
}
virtual ~ odeIntegrator () {
free (dxdt);
}
};
class sdeIntegrator
{
protected:
baseType *dxdt;
baseType *s;
baseType *dsdx;
public:
sdeIntegrator ( unsigned int size) {
dxdt = (baseType *) calloc (size, sizeof(baseType));
s = (baseType *) calloc (size, sizeof(baseType));
dsdx = (baseType *) calloc (size, sizeof(baseType));
for (unsigned int i = 0; i < size; i++)
{
s[i] = 0;
dsdx[i] = 0;
}
}
virtual ~sdeIntegrator () {
free (dxdt);
free (s);
free (dsdx);
}
};
class euler : public odeIntegrator
{
public:
euler (unsigned int size) : odeIntegrator( size) { }
template <typename dgl>
void step (double dt, baseType *state, dgl &func, unsigned int size) {
func.dgl(state,dxdt);
for (unsigned int i = 0; i < size; i++)
state[i] = state[i] + dt * dxdt[i];
}
};
class eulerMaruyama : public sdeIntegrator
{
public:
eulerMaruyama( unsigned int size) : sdeIntegrator (size) {}
template <typename dgl>
void step (double dt, baseType *state, dgl &func, unsigned int size) {
func.dgl (state, dxdt, s, dsdx);
for (unsigned int i = 0; i < size ; i++)
state[i] = state[i] + dxdt[i] * dt + s[i] * sqrt (dt) * gslNoise::getGaussian();
}
};
class milsteinIto : public sdeIntegrator
{
public:
milsteinIto( unsigned int size) : sdeIntegrator (size){}
template <typename dgl>
void step (double dt, baseType *state, dgl &func, unsigned int size) {
double W;
func.dgl (state, dxdt, s, dsdx);
for (unsigned int i = 0; i < size ; i++) {
W = gslNoise::getGaussian();
state [i] = state [i] + dxdt[i] * dt + s[i] * sqrt ( dt) * W + 0.5 * s[i] * dsdx[i] * ( dt * W *W - dt);
}
}
};
class milsteinStrato : public sdeIntegrator
{
public:
milsteinStrato( unsigned int size) : sdeIntegrator (size) {}
template <typename dgl>
void step (double dt, baseType *state, dgl &func, unsigned int size)
{
double W;
func.dgl (state, dxdt, s, dsdx);
for (unsigned int i = 0; i < size ; i++)
{
W = gslNoise::getGaussian();
state [i] = state [i] + dxdt[i] * dt + s[i] * sqrt ( dt) * W + 0.5 * s[i] * dsdx[i] * ( dt * W *W );
}
}
};
class strongTaylor : public sdeIntegrator
{
baseType dW, dZ;
baseType zeta;
baseType dt2, sqdt, sqdt2, a10, rho;
baseType * tmp2, * dxdt, *dyt, *dym, *dydW;
const unsigned int approxOrder;
public:
strongTaylor (unsigned int size) : sdeIntegrator (size), approxOrder(20) {
tmp2 = (baseType *) calloc (size, sizeof(baseType));
dxdt = (baseType *) calloc (size, sizeof(baseType));
dyt = (baseType *) calloc (size, sizeof(baseType));
dym = (baseType *) calloc (size, sizeof(baseType));
dydW = (baseType *) calloc (size, sizeof(baseType));
double r2, pi2=1.0/(2.0*pow(M_PI, 2)), tp1=0.0;
rho=1.0/12.0;
for (unsigned short i = 1; i <= approxOrder; i++) {
r2 = pow(1.0/(double)i, 2.0);
tp1 += r2;
}
rho -= pi2*tp1;
}
template <typename dgl>
void step (double dt, baseType *state, dgl &func, unsigned int size) {
dt2=dt/2.0; sqdt=sqrt(dt); sqdt2 = sqdt/2.0;
//first step
zeta = gslNoise::getGaussian();
dW = sqdt*zeta;
double r;
// Berrechne a10 und b1
a10=0.0;
for (unsigned short j = 0; j < approxOrder; j++) {
r=(double)(j+1);
a10 -= gslNoise::getGaussian()/r;
}
a10 = a10*sqrt(2.0*dt)/M_PI - 2*sqrt(dt*rho)*gslNoise::getGaussian();
dZ = 0.5*dt*(dW + a10);
func.dgl(state, dxdt, dydW);
for (unsigned int i = 0; i < size; i++)
tmp2[i] = state[i] + dxdt[i]*dt + dydW[i]*sqdt;
//second step
func.dgl (tmp2, dyt, dydW);
for (unsigned int i = 0; i < size; i++)
tmp2[i] = state[i] + dxdt[i]*dt - dydW[i]*sqdt;
//third step
func.dgl(tmp2, dym, dydW);
for (unsigned int i = 0; i < size; i++)
state[i] += dydW[i]*dW + (dyt[i] - dym[i])/((baseType)2.0*sqdt)*dZ + (dyt[i] + dym[i] + (baseType)2.0*dxdt[i])/(baseType)4.0*dt;
}
};
#endif