notify gitter on travis build failures
[sddekit.git] / test / test_exc.c
blob699c6acfce664f6092104429a7ef144d9cd829c1
1 /* copyright 2016 Apache 2 sddekit authors */
3 #include <stdio.h>
5 #include "sddekit.h"
6 #include "test.h"
8 typedef struct out_data {
9 int crossed;
10 double tf;
11 FILE *fd;
12 } out_data;
14 static sd_stat test_out_apply(sd_out *out, double t,
15 uint32_t nx, double * restrict x,
16 uint32_t nc, double * restrict c)
18 out_data *d = out->ptr;
19 (void) nx; (void) nc; (void) c;
20 fprintf(d->fd, "%f\t%f\t%f\n", t, x[0], x[1]);
21 if (x[0] < 0.0)
22 d->crossed = 1;
23 return t < d->tf ? SD_CONT : SD_STOP;
26 static void test_out_free(sd_out *out) { sd_free(out->ptr); sd_free(out); }
28 sd_out test_out_defaults = {.free=&test_out_free,
29 .apply=&test_out_apply,
30 .ptr=NULL};
32 sd_out * test_out_new()
34 sd_out *out = sd_malloc(sizeof(sd_out));;
35 *out = test_out_defaults;
36 out->ptr = sd_malloc(sizeof(out_data));
37 return out;
40 static double x0[2] = {1.010403, 0.030870};
42 static int for_scheme(sd_sch *sch, char *name)
44 sd_sys_exc *sys = sd_sys_exc_new();
45 sd_sol *sol;
46 sd_out *out = test_out_new();
47 sd_hfill *hf = sd_hfill_new_val(0.0);
48 out_data *outd = out->ptr;
49 char dat_name[100];
50 uint32_t vi[1];
51 double vd[1];
53 /* init solver */
54 vi[0] = 0;
55 vd[0] = 25.0;
56 sol = sd_sol_new_default(sys->sys(sys), sch, out, hf, 42, 2, x0,
57 1, 1, vi, vd, 0.0, 0.05);
59 /* fill in data */
60 outd->tf = 20.0;
61 sprintf(dat_name, "test_exc_%s.dat", name);
62 outd->fd = fopen(dat_name, "w");
63 sys->set_a(sys, 1.01);
64 sys->set_tau(sys, 3.0);
65 sys->set_k(sys, -1e-3);
67 /* deterministic sub-thresh, no crossing */
68 outd->crossed = 0;
69 sys->set_D(sys, 0.0);
70 sol->cont(sol);
71 EXPECT_TRUE(!outd->crossed);
73 /* stochastic sub-thresh, crossing */
74 outd->crossed = 0;
75 outd->tf = 40.0;
76 sys->set_D(sys, 0.05);
77 sol->cont(sol);
78 EXPECT_TRUE(outd->crossed);
80 /* clean up */
81 fclose(outd->fd);
82 out->free(out);
83 sd_sys *exc_sys_if = sys->sys(sys);
84 exc_sys_if->free(exc_sys_if);
85 sol->free(sol);
86 hf->free(hf);
88 return 0;
92 TEST(exc, em) {
93 sd_sch *sch = sd_sch_new_em(2);
94 for_scheme(sch, "em");
95 sch->free(sch);
98 TEST(exc, heun){
99 sd_sch *sch = sd_sch_new_heun(2);
100 for_scheme(sch, "heun");
101 sch->free(sch);
104 TEST(exc, emcolor){
105 sd_sch *sch = sd_sch_new_emc(2, 1.0);
106 for_scheme(sch, "emc");
107 sch->free(sch);