-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImplicitEulerSolver.c
325 lines (297 loc) · 8.86 KB
/
ImplicitEulerSolver.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include "ImplicitEulerSolver.h"
#include <stdbool.h>
#include <math.h>
#include <stdio.h>
void vector_implicit_euler(
int N,
int n,
int NS,
double *pt,
double *px,
double *pdW,
double *workspace_lf,
int *workspace_d,
int max_iterations,
double tolerance,
functiontype f_func,
functiontype g_func,
functiontype J_func,
double *pu,
double *pd,
void *pP,
double *px0
){
unsigned int row, col, sim, i,j,k;
double h; // temporal step
double *pF = &workspace_lf[0];
double *pG = &workspace_lf[n];
double *ppsi = &workspace_lf[n+n];
double *workspace_inner = &workspace_lf[n+n+n];
int len2dims_X = (N+1)*n;
int len2dims_dW = len2dims_X-n;
int index_X = 0;
int index_dW = 0;
for (sim = 0; sim < NS;sim++){
// Imposing initial condition
i = index_X;
for (row = 0; row < n; row++) {
px[i] = px0[row];
i++;
}
// Initializing indexes
i = index_X;
j = index_X+n;
k = index_dW;
for (col = 0; col < N; col++) {
// Invoking drift and diffusion terms
f_func(&pt[col],&px[i],pu,pd,pP,pF);
g_func(&pt[col],&px[i],pu,pd,pP,pG);
// Calculating time step
h = pt[col+1]-pt[col];
// Initial guess for Newton solver
for (row = 0; row < n; row++) {
ppsi[row] = px[i] + (pG[row]*pdW[k]);
px[j] = ppsi[row]+ (h*pF[row]);
i++;
j++;
k++;
}
// Invoking newton solver
newton_solver(
f_func,
J_func,
max_iterations,
tolerance,
n,
h,
&pt[col],
&px[i], // since i has been incremented to what was j
ppsi,
workspace_inner,
workspace_d,
pu,
pd,
pP
);
}
index_X += len2dims_X;
index_dW += len2dims_dW;
}
}
void newton_solver(
functiontype f_func,
functiontype J_func,
int max_iterations,
double tolerance,
int n,
double dt,
double *pt,
double *px,
double *ppsi,
double *workspace_lf,
int *workspace_d,
double *pu,
double *pd,
void *pP
){
double *pftemp = &workspace_lf[0];
double *pjacobian = &workspace_lf[n];
double *pdRdX = &workspace_lf[n*(n+1)];
double *pR = &workspace_lf[n*(n+n+1)];
f_func(pt, px,pu,pd,pP,pftemp);
J_func(pt, px,pu,pd,pP,pjacobian);
// Initializing residuals
int i = 0;
for (i=0;i<n;i++){
pR[i] = px[i]-pftemp[i]*dt-ppsi[i];
}
// Parameters for DGESV
int N = n;
int NRHS = 1;
int LDA = N;
int LDB = N;
int INFO;
// Minimizing residuals
int iterations = 0; // of iterations
// Boolean to check if the residuals meet the tolerance
bool has_converged;
// Index to check when a diagonal element is reached
int diagonal_index = 0;
// Size of square matrix
int n_square = n*n;
for (iterations = 0;iterations<max_iterations;iterations++){
// Initializing system matrix to solve
for (i=0;i<n_square;i++){
if (i == diagonal_index){ // Handling diagonal elements
pdRdX[i] = 1 - pjacobian[i]*dt;
diagonal_index += n+1;
}
else {
pdRdX[i] = -pjacobian[i]*dt;
}
}
// Minimizing residuals
dgesv_(
&N,
&NRHS,
pdRdX,
&LDA,
workspace_d,
pR,
&LDB,
&INFO
);
// Updating the solution x
for (i=0;i<n;i++){
px[i] -= pR[i];
}
// Updating the residuals and calculating the infinity norm
f_func(pt, px,pu,pd,pP,pftemp);
has_converged = true;
for (i=0;i<n;i++){
pR[i] = px[i]-pftemp[i]*dt-ppsi[i];
has_converged &= fabs(pR[i]) < tolerance;
}
// If the infinity norm is less than the tolerance, the method terminates
if (has_converged){
break;
}
// Saving one call to the jacobian if convergence is obtained
J_func(pt, px,pu,pd,pP,pjacobian);
// Resetting the diagonal index
diagonal_index = 0;
}
}
void vector_implicit_euler_final_step(
int N,
int n,
int NS,
double *pt,
double *px,
double *pdW,
double *workspace_lf,
int *workspace_d,
int max_iterations,
double tolerance,
functiontype f_func,
functiontype g_func,
functiontype J_func,
double *pu,
double *pd,
void *pP,
double *px0
){
unsigned int row, col, sim;
unsigned int k = 0;
double h; // temporal step
double *pF = &workspace_lf[0];
double *pG = &workspace_lf[n];
double *ppsi = &workspace_lf[n+n];
double *pxtemp_1 = &workspace_lf[n+n+n]; // temporary x values for current step
double *pxtemp_2 = &workspace_lf[n+n+n+n]; // temporary x values for last step
double *workspace_inner = &workspace_lf[n+n+n+n+n];
double *pxtemp; // used to switch current x step to previous x step
int result_index = 0;
for (sim = 0; sim < NS;sim++){
// Running Euler Maruyama algorithm
for (col = 0; col < N; col++) {
// Invoking drift and diffusion terms
if (col >0 ){
f_func(&pt[col],pxtemp_1,pu,pd,pP,pF);
g_func(&pt[col],pxtemp_1,pu,pd,pP,pG);
}
// Imposing initial condition
else {
f_func(&pt[col],px0,pu,pd,pP,pF);
g_func(&pt[col],px0,pu,pd,pP,pG);
}
// Calculating time step
h = pt[col+1]-pt[col];
// There are now three cases:
// 1. Either we impose the initial condition stored in px0
if (col == 0){
for (row = 0; row < n; row++) {
ppsi[row] = px0[row] + (pG[row]*pdW[k]);
pxtemp_2[row] = ppsi[row]+ (h*pF[row]);
k++;
}
// Invoking newton solver
newton_solver(
f_func,
J_func,
max_iterations,
tolerance,
n,
h,
&pt[col],
pxtemp_2, // the result will be written here
ppsi,
workspace_inner,
workspace_d,
pu,
pd,
pP
);
// Updating pointers such that current time step is the previous time step
pxtemp = pxtemp_2;
pxtemp_2 = pxtemp_1;
pxtemp_1 = pxtemp;
}
// 2. We iterate over all temporary solutions which are not stored
else if (col < N-1){
for (row = 0; row < n; row++) {
ppsi[row] = pxtemp_1[row] + (pG[row]*pdW[k]);
pxtemp_2[row] = ppsi[row]+ (h*pF[row]);
k++;
}
// Invoking newton solver
newton_solver(
f_func,
J_func,
max_iterations,
tolerance,
n,
h,
&pt[col],
pxtemp_2, // the result will be written here
ppsi,
workspace_inner,
workspace_d,
pu,
pd,
pP
);
// Updating pointers such that current time step is the previous time step
pxtemp = pxtemp_2;
pxtemp_2 = pxtemp_1;
pxtemp_1 = pxtemp;
}
// 3. Or we store the final solution computed in step N+1
else {
for (row = 0; row < n; row++) {
ppsi[row] = pxtemp_1[row] + (pG[row]*pdW[k]);
px[result_index] = ppsi[row]+ (h*pF[row]);
k++;
result_index++;
}
// Invoking newton solver
newton_solver(
f_func,
J_func,
max_iterations,
tolerance,
n,
h,
&pt[col],
&px[result_index-n], // The result is written to the final solution
ppsi,
workspace_inner,
workspace_d,
pu,
pd,
pP
);
}
}
}
}