use opaque pointers
[sddekit.git] / test / test_solv.c
blob7478050f2c21c8c1de8cabd68fcf438f52670b39
1 /* Apache 2.0 INS-AMU 2015 */
3 #include "sddekit.h"
5 typedef struct {
6 int n_calls, nx, nc;
7 double t, *x, *c, *f, *g, *Jf, *Jg;
8 } sys_data;
10 static SK_DEFSYS(test_sys)
12 sys_data *d = data;
13 /* unused */ (void) Jce; (void) hist; (void) i;
14 d->n_calls++;
15 d->t = t;
16 d->nx = nx;
17 d->x = x;
18 d->nc = nc;
19 d->c = c;
20 d->f = f;
21 d->g = g;
22 d->Jf = Jf;
23 d->Jg = Jg;
24 return 0;
27 typedef struct {
28 int n_calls;
29 double dt;
30 rk_state *rng;
31 } sch_data;
33 static SK_DEFSCH(test_sch)
35 sch_data *d = data;
36 d->n_calls++;
37 d->dt = dt;
38 d->rng = rng;
39 (*sys)(sysd, hist, t, 0, nx, x, NULL, NULL, NULL, NULL, nc, c, NULL);
40 return 0;
43 typedef struct {
44 int nx;
45 double tf, *x;
46 } out_data;
48 static SK_DEFOUT(test_out)
50 out_data *d = data;
51 /* unused */ (void) nc; (void) c;
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 TEST(solv, simple) {
68 int vi[NC];
69 double x[NX], vd[NX], rand0;
70 sys_data sysd;
71 sch_data schd;
72 out_data outd;
73 sk_solv *solv;
74 rk_state rng;
76 rk_seed(SEED, &rng);
77 rand0 = rk_gauss(&rng);
79 sysd.n_calls = 0;
80 schd.n_calls = 0;
81 vi[0] = 0;
82 vi[1] = 1;
83 vd[0] = 2.1;
84 vd[1] = 0.42;
86 solv = sk_solv_alloc();
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 EXPECT_EQ(rk_gauss(sk_solv_get_rng(solv)), rand0);
94 outd.tf = T0 + DT;
96 sk_solv_cont(solv);
98 EXPECT_EQ(1,schd.n_calls);
99 EXPECT_EQ(DT,schd.dt);
100 EXPECT_EQ(sk_solv_get_rng(solv),schd.rng);
102 EXPECT_EQ(1,sysd.n_calls);
103 EXPECT_EQ(NX,sysd.nx);
104 EXPECT_EQ(NC,sysd.nc);
105 EXPECT_EQ(T0,sysd.t);
106 EXPECT_EQ(T0+DT,sk_solv_get_t(solv));
107 EXPECT_EQ(sk_solv_get_x(solv),sysd.x);
108 EXPECT_EQ(sk_solv_get_c(solv),sysd.c);
109 EXPECT_EQ(NULL,sysd.f);
110 EXPECT_EQ(NULL,sysd.g);
111 EXPECT_EQ(NULL,sysd.Jf);
112 EXPECT_EQ(NULL,sysd.Jg);
114 outd.tf = T0 + 17 * DT;
115 sk_solv_cont(solv);
116 EXPECT_EQ(17,sysd.n_calls);
117 ASSERT_NEAR(sysd.t+DT, outd.tf, 1e-14);
119 sk_solv_free(solv);