-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_equations.hpp
553 lines (408 loc) · 11 KB
/
test_equations.hpp
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
Rehuel: a simple C++ library for solving ODEs
Copyright 2017-2019, Stefan Paquay (stefanpaquay@gmail.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
============================================================================= */
/**
\file test_equations.hpp
\brief Contains some typical ODEs that are good to test solvers on.
*/
#ifndef TEST_EQUATIONS_HPP
#define TEST_EQUATIONS_HPP
#include <cassert>
#include "functor.hpp"
namespace test_equations {
// Exponential function:
struct exponential : public functor
{
typedef mat_type jac_type;
exponential() : l(1.0) {}
explicit exponential( double l ) : l(l) {}
vec_type sol( double t )
{
return { exp( l*t ) };
}
vec_type fun( double t, const vec_type &y )
{
return l*y;
}
jac_type jac( double t, const vec_type &y )
{
return {l};
}
double l;
};
// Harmonic oscillator function:
struct harmonic : public functor
{
typedef mat_type jac_type;
explicit harmonic( double w ) : w(w) {}
vec_type sol( double t )
{
return { sin(w*t), cos(w*t) };
}
vec_type fun( double t, const vec_type &y )
{
return { w*y[1], -w*y[0] };
}
jac_type jac( double t, const vec_type &y )
{
return { { 0, w }, { -w, 0 } };
}
double w;
};
// Van der Pol oscillator:
struct vdpol : public functor
{
typedef mat_type jac_type;
vdpol() : mu(0.5) {}
vdpol( double mu ) : mu(mu){}
vec_type fun( double t, const vec_type &y )
{
return { y[1], ((1 - y[0]*y[0]) * y[1] - y[0]) / mu };
}
jac_type jac( double t, const vec_type &y )
{
jac_type J(2,2);
J(0,0) = 0.0;
J(0,1) = 1.0;
J(1,0) = -(2.0 * y[0] * y[1] + 1.0) / mu;
J(1,1) = ( 1.0 - y[0]*y[0] ) / mu;
return J;
}
double mu;
};
// Brusselator:
struct bruss : public functor
{
typedef mat_type jac_type;
bruss() : a(1.0), b(2.5) {}
bruss( double a, double b ) : a(a), b(b) {}
vec_type fun( double t, const vec_type &y )
{
return { a + y[0]*y[0]*y[1] - b*y[0] - y[0],
b*y[0] - y[0]*y[0]*y[1] };
}
jac_type jac( double t, const vec_type &y )
{
jac_type J(2,2);
J(0,0) = 2*y[0]*y[1] - b - 1;
J(0,1) = y[0]*y[0];
J(1,0) = b - 2*y[0]*y[1];
J(1,1) = -y[0]*y[0];
return J;
}
double a, b;
};
// Robertson oscillator:
struct rober : public functor {
typedef mat_type jac_type;
vec_type fun( double t, const vec_type &y )
{
return { -0.04*y[0] + 1e4 * y[1]*y[2],
0.04*y[0] - 1e4 * y[1]*y[2] - 3e7*y[1]*y[1],
3e7*y[1]*y[1] };
}
jac_type jac( double t, const vec_type &y )
{
jac_type J(3,3);
J(0,0) = -0.04;
J(0,1) = 1e4*y[2];
J(0,2) = 1e4*y[1];
J(1,0) = 0.04;
J(1,1) = -1e4*y[2] - 6e7*y[1];
J(1,2) = -1e4*y[1];
J(2,0) = J(2,2) = 0.0;
J(2,1) = 6e7*y[1];
return J;
}
};
// Simple dimerization reaction:
struct dimer : public functor {
typedef mat_type jac_type;
explicit dimer( double rate ) : rate(rate), irate(1.0/rate) {}
vec_type fun( double t, const vec_type &y )
{
return { -2*rate*y[0]*y[0] + 2*irate*y[1],
rate*y[0]*y[0] - irate*y[1] };
}
jac_type jac( double t, const vec_type &y )
{
jac_type J(2,2);
J(0,0) = -4*rate*y[0];
J(0,1) = 2*irate;
J(1,0) = 2*rate*y[0];
J(1,1) = -irate;
return J;
}
double rate, irate;
};
struct stiff_eq : public functor {
// y'' + 1001 y' + 1000 x = 0,
// y'(0) = 0, y(0) = 1.0;
// u := y' -->
// y' = u
// u' = -1001 u - 1000 y
// --> Y' = A*Y, Y = ( y, u )
//
typedef mat_type jac_type;
stiff_eq() : A(2,2)
{
A(0,0) = 0.0;
A(0,1) = 1.0;
A(1,0) = -1000.0;
A(1,1) = -1001.0;
}
vec_type sol( double t ) const
{
double ysol = -exp(-1000*t)/999.0 + 1000*exp(-t)/999.0;
double usol = ( 1000.0*exp(-1000*t) - 1000.0*exp(-t) ) / 999.0;
vec_type solution(2);
solution(0) = ysol;
solution(1) = usol;
return solution;
}
vec_type fun( double t, const vec_type &y )
{
return A*y;
}
jac_type jac( double t, const vec_type &y )
{
return A;
}
private:
jac_type A;
};
struct three_body : public functor
{
typedef mat_type jac_type;
three_body() : m1(1.0), m2(1.0), m3(1.0), m1r(1.0), m2r(1.0) {}
three_body( double m1, double m2, double m3 )
: m1(m1), m2(m2), m3(m3), m1r(m1/m3), m2r(m2/m3) {}
virtual vec_type fun( double t, const vec_type &y )
{
// y contains ( q0, q1, q2, q3, q4, q5,
// ( p0, p1, p2, p3, p4, p5 )
//
// (q0, q1, q2, q3, q4, q5) are (x0, y0, x1, y1, x2, y2)
vec_type dydt(12);
dydt[0] = y[6] / m1;
dydt[1] = y[7] / m1;
dydt[2] = y[8] / m2;
dydt[3] = y[9] / m2;
dydt[4] = y[10] / m3;
dydt[5] = y[11] / m3;
double x2mx1 = y[2] - y[0];
double y2my1 = y[3] - y[1];
double x3mx1 = y[4] - y[0];
double y3my1 = y[5] - y[1];
double x3mx2 = y[4] - y[2];
double y3my2 = y[5] - y[3];
double r12_2 = x2mx1*x2mx1 + y2my1*y2my1;
double r13_2 = x3mx1*x3mx1 + y3my1*y3my1;
double r23_2 = x3mx2*x3mx2 + y3my2*y3my2;
double r12_1 = sqrt(r12_2);
double r13_1 = sqrt(r13_2);
double r23_1 = sqrt(r23_2);
double r12_3 = r12_2*r12_1;
double r13_3 = r13_2*r13_1;
double r23_3 = r23_2*r23_1;
dydt[ 6] = m1*m3*x3mx1 / r13_3 + m1*m2*x2mx1 / r12_3;
dydt[ 7] = m1*m3*y3my1 / r13_3 + m1*m2*y2my1 / r12_3;
dydt[ 8] = -m1*m2*x2mx1 / r12_3 + m2*m3*x3mx2 / r23_3;
dydt[ 9] = -m1*m2*y2my1 / r12_3 + m2*m3*x3mx2 / r23_3;
dydt[10] = -m2*m3*x3mx2 / r23_3 - m1*m3*x3mx1 / r13_3;
dydt[11] = -m2*m3*y3my2 / r23_3 - m1*m3*y3my1 / r13_3;
return dydt;
}
virtual jac_type jac( double t, const vec_type &y )
{
mat_type J(12,12);
double x2mx1 = y[2] - y[0];
double y2my1 = y[3] - y[1];
double x3mx1 = y[4] - y[0];
double y3my1 = y[5] - y[1];
double x3mx2 = y[4] - y[2];
double y3my2 = y[5] - y[3];
double x2mx1_2 = x2mx1*x2mx1;
double y2my1_2 = y2my1*y2my1;
double x3mx1_2 = x3mx1*x3mx1;
double y3my1_2 = y3my1*y3my1;
double x3mx2_2 = x3mx2*x3mx2;
double y3my2_2 = y3my2*y3my2;
double r12_2 = x2mx1_2 + y2my1_2;
double r13_2 = x3mx1_2 + y3my1_2;
double r23_2 = x3mx2_2 + y3my2_2;
double r12_1 = sqrt(r12_2);
double r13_1 = sqrt(r13_2);
double r23_1 = sqrt(r23_2);
double r12_3 = r12_2*r12_1;
double r13_3 = r13_2*r13_1;
double r23_3 = r23_2*r23_1;
double r12_5 = r12_3*r12_2;
double r13_5 = r13_3*r13_2;
double r23_5 = r23_3*r23_2;
J = arma::zeros(12,12);
// [ 0..5 ] is p, [ 6..11 ] is x.
J( 6, 6) = -m1*m3 / r13_3 + 3*m1*m3*x3mx1_2 / r13_5
- m1*m2 / r12_3 + 3*m1*m2*x2mx1_2 / r12_5;
J( 8, 8) = -m2*m3 / r23_3 + 3*m2*m3*x3mx2_2 / r23_5
- m1*m2 / r12_3 + 3*m1*m2*x2mx1_2 / r12_5;
J(10,10) = -m1*m3 / r13_3 + 3*m1*m3*x3mx1_2 / r13_5
- m2*m3 / r23_3 + 3*m2*m3*x3mx2_2 / r23_5;
J( 7, 7) = -m1*m3 / r13_3 + 3*m1*m3*y3my1_2 / r13_5
- m1*m2 / r12_3 + 3*m1*m2*y2my1_2 / r12_5;
J( 9, 9) = -m2*m3 / r23_3 + 3*m2*m3*y3my2_2 / r23_5
- m1*m2 / r13_3 + 3*m1*m2*y2my1_2 / r12_5;
J(11,11) = -m1*m3 / r13_3 + 3*m1*m3*y3my1_2 / r13_5
- m2*m3 / r23_3 + 3*m2*m3*y3my2_2 / r23_5;
// Cross terms:
J( 7, 6) = 3*m1*m3*x3mx1*y3my1 / r13_5
+3*m1*m2*x2mx1*y2my1 / r12_5;
J( 8, 6) = m1*m2/r12_3 - 3*m1*m2*x2mx1_2 / r12_5;
J( 9, 6) = -3*m1*m2*x2mx1*y2my1 / r12_5;
J(10, 6) = m1*m3/r13_3 - 3*m1*m3*x3mx1_2 / r13_5;
J(11, 6) = -3*m1*m3*x3mx1*y3my1 / r13_5;
J( 8, 7) = -3*m1*m2*x2mx1*y2my1 / r12_5;
J( 9, 7) = m1*m2/r12_3 - 3*m1*m2*y2my1_2 / r12_5;
J(10, 7) = -3*m1*m3*x3mx1*y3my1 / r13_5;
J(11, 7) = m1*m3/r13_2 - 3*m1*m3*y3my1_2 / r13_5;
J( 9, 8) = 3*m2*m3*x3mx2*y3my2 / r23_5
+ 3*m1*m2*x2mx1*y2my1 / r12_5;
J(10, 8) = m2*m3 / r23_3 - 3*m2*m3*x3mx2_2 / r23_5;
J(11, 8) = -3*m2*m3*x3mx2*y3my2 / r23_5;
J(10, 9) = -3*m2*m3*x3mx2*y3my2 / r23_5;
J(11, 9) = m2*m3/r23_3 - 3*m2*m3*y3my2_2 / r23_5;
J(11,10) = 3*m2*m3*x3mx2*y3my2 / r23_5
+ 3*m1*m3*x3mx1*y3my1 / r13_5;
// symmetry:
J( 6, 7) = J( 7, 6);
J( 6, 8) = J( 8, 6);
J( 7, 8) = J( 8, 7);
J( 6, 9) = J( 9, 6);
J( 7, 9) = J( 9, 7);
J( 8, 9) = J( 9, 8);
J( 6,10) = J(10, 6);
J( 7,10) = J(10, 7);
J( 8,10) = J(10, 8);
J( 9,10) = J(10, 9);
J( 6,11) = J(11, 6);
J( 7,11) = J(11, 7);
J( 8,11) = J(11, 8);
J( 9,11) = J(11, 9);
J(10,11) = J(11,10);
// x to x:
J(0,0) = 1.0 / m1;
J(1,1) = 1.0 / m1;
J(2,2) = 1.0 / m2;
J(3,3) = 1.0 / m2;
J(4,4) = 1.0 / m3;
J(5,5) = 1.0 / m3;
// x to p and p to x are 0.
return -J;
}
double kin_energy( const vec_type &y )
{
double px0 = y[6];
double py0 = y[7];
double px1 = y[8];
double py1 = y[9];
double px2 = y[10];
double py2 = y[11];
double T = px0*px0 + py0*py0;
T += px1*px1 + py1*py1;
T += px2*px2 + py2*py2;
return 0.5*T;
}
double pot_energy( const vec_type &y )
{
double r12_x = y[2] - y[0];
double r12_y = y[3] - y[1];
double r13_x = y[4] - y[0];
double r13_y = y[5] - y[1];
double r23_x = y[4] - y[2];
double r23_y = y[5] - y[3];
double r12_2 = r12_x*r12_x + r12_y*r12_y;
double r13_2 = r13_x*r13_x + r13_y*r13_y;
double r23_2 = r23_x*r23_x + r23_y*r23_y;
double r12 = sqrt(r12_2);
double r13 = sqrt(r13_2);
double r23 = sqrt(r23_2);
double V = -m1*m2/r12 - m1*m3/r13 - m2*m3/r23;
return V;
}
void set_m1(double new_m1)
{
m1 = new_m1;
m1r = m1/m3;
}
void set_m2(double new_m2)
{
m2 = new_m2;
m2r = m2/m3;
}
void set_m3(double new_m3)
{
m3 = new_m3;
m1r = m1 / m3;
m2r = m2 / m3;
}
double m1, m2, m3;
double m1r, m2r;
};
struct kinetic_4 : public functor
{
typedef mat_type jac_type;
kinetic_4( double b2, double b3, double b4 )
: b2(b2), b3(b3), b4(b4) {}
virtual vec_type fun( double t, const vec_type &y )
{
vec_type rhs(4);
rhs(0) = -2*y(0)*y(0) - y(0)*(y(1) + y(2));
rhs(0) += 2*b2*y(1) + b3*y(2) + b4*y(3);
rhs(1) = y(0)*y(0) - y(0)*y(1) + b3*y(2) - b2*y(1);
rhs(2) = y(0)*y(1) - y(0)*y(2) + b4*y(3) - b3*y(2);
rhs(3) = y(0)*y(2) - b4*y(3);
return rhs;
}
virtual jac_type jac( double t, const vec_type &y )
{
jac_type J = { { -4*y(0) - y(1) - y(2), -y(0) + 2*b2, -y(0) + b3, b4 },
{ 2*y(0) - y(1), -y(0) - b2, b3, 0.0 },
{ y(1) - y(2), y(0), -y(0) - b3, b4 },
{ y(2), 0, y(0), -b4} };
return J;
}
double b2, b3, b4;
};
struct lorenz : public functor
{
typedef mat_type jac_type;
lorenz(double sigma = 10, double rho = 28, double beta = 8.0/3.0)
: s(sigma), r(rho), b(beta) {}
virtual vec_type fun(double t, const vec_type &y)
{
return { s*(y(1) - y(0)),
y(0)*(r - y(2)) - y(1),
y(0)*y(1) - b*y(2)
};
}
virtual jac_type jac(double t, const vec_type &y)
{
return { { -s, s, 0 },
{ r-y(2), -1.0, -y(0) },
{y(1), y(0), -b}
};
}
double s, r, b;
};
} // test_equations
#endif // TEST_EQUATIONS_HPP