add em color
[sddekit.git] / test_solv.c
blobaf9f6a24b662daffe486cd417db01009b8830b28
1 /* Apache 2.0 INS-AMU 2015 */
3 #include "sk_test.h"
4 #include "sk_solv.h"
5 #include "randomkit.h"
7 typedef struct {
8 int n_calls, nx, nc;
9 double t, *x, *c, *f, *g, *Jf, *Jg;
10 } sys_data;
12 static SK_DEFSYS(test_sys)
14 sys_data *d = data;
15 d->n_calls++;
16 d->t = t;
17 d->nx = nx;
18 d->x = x;
19 d->nc = nc;
20 d->c = c;
21 d->f = f;
22 d->g = g;
23 d->Jf = Jf;
24 d->Jg = Jg;
25 return 0;
28 typedef struct {
29 int n_calls;
30 double dt;
31 rk_state *rng;
32 } sch_data;
34 static SK_DEFSCH(test_sch)
36 sch_data *d = data;
37 d->n_calls++;
38 d->dt = dt;
39 d->rng = rng;
40 (*sys)(sysd, t, nx, x, NULL, NULL, NULL, NULL, nc, c);
41 return 0;
44 typedef struct {
45 int nx;
46 double tf, *x;
47 } out_data;
49 static SK_DEFOUT(test_out)
51 out_data *d = data;
52 d->nx = nx;
53 d->x = x;
54 return t < d->tf;
57 static void test_hist_filler()
61 #define SEED 42
62 #define T0 2.34
63 #define DT 0.12
64 #define NX 3
65 #define NC 2
67 int test_solv()
69 int vi[NC];
70 double x[NX], vd[NX], rand0;
71 sys_data sysd;
72 sch_data schd;
73 out_data outd;
74 sk_solv solv;
75 rk_state rng;
77 rk_seed(SEED, &rng);
78 rand0 = rk_gauss(&rng);
80 sysd.n_calls = 0;
81 schd.n_calls = 0;
82 vi[0] = 0;
83 vi[1] = 1;
84 vd[0] = 2.1;
85 vd[1] = 0.42;
87 sk_solv_init(&solv, &test_sys, &sysd,
88 &test_sch, &schd, &test_out, &outd,
89 &test_hist_filler, NULL, SEED, NX, x, NC, vi, vd,
90 T0, DT);
92 sk_test_true(rand0==rk_gauss(&solv.rng));
94 outd.tf = T0 + DT;
96 sk_solv_cont(&solv);
98 sk_test_true(schd.n_calls==1);
99 sk_test_true(schd.dt==DT);
100 sk_test_true(schd.rng==&solv.rng);
102 sk_test_true(sysd.n_calls==1);
103 sk_test_true(sysd.nx==NX);
104 sk_test_true(sysd.nc==NC);
105 sk_test_true(sysd.t==T0+DT);
106 sk_test_true(sysd.x==solv.x);
107 sk_test_true(sysd.c==solv.c);
108 sk_test_true(sysd.f==NULL);
109 sk_test_true(sysd.g==NULL);
110 sk_test_true(sysd.Jf==NULL);
111 sk_test_true(sysd.Jg==NULL);
113 outd.tf = T0 + 17 * DT;
114 sk_solv_cont(&solv);
115 sk_test_true(sysd.n_calls==17);
116 sk_test_tol(sysd.t, outd.tf, 1e-15);
118 sk_solv_free(&solv);
119 return 0;