add em color
[sddekit.git] / sk_solv.c
blobc9bad155c293e3ee610e41953480056be429edcf
1 /* Apache 2.0 INS-AMU 2015 */
3 #include <string.h>
4 #include <stdlib.h>
6 #include "sk_solv.h"
7 #include "sk_util.h"
10 int sk_solv_init(
11 sk_solv *s,
12 sk_sys sys, void *sys_data,
13 sk_sch scheme, void *scheme_data,
14 sk_out out, void *out_data,
15 sk_hist_filler hf, void *hfill_data,
16 int seed,
17 int nx, double *x0,
18 int nc, int *vi, double *vd,
19 double t0, double dt
22 s->nx = nx;
23 s->nc = nc;
24 s->cont = 1;
25 s->sys = sys;
26 s->sysd = sys_data;
27 s->sch = scheme;
28 s->schd = scheme_data;
29 s->out = out;
30 s->outd = out_data;
31 s->hf = hf;
32 s->hfd = hfill_data;
33 SK_MALLOCHECK(s->x = malloc (sizeof(double) * nx));
34 SK_MALLOCHECK(s->x0 = malloc (sizeof(double) * nx));
35 if (nc > 0 && vi!=NULL && vd!=NULL) {
36 sk_hist_init(&s->hist, nc, vi, vd, t0, dt);
37 sk_hist_fill(&s->hist, hf, hfill_data);
38 SK_MALLOCHECK(s->c = malloc(sizeof(double) * s->nc));
39 } else {
40 s->nc = 0;
41 s->c = NULL;
43 rk_seed(seed, &(s->rng));
44 memcpy(s->x, x0, sizeof(double) * s->nx);
45 memcpy(s->x0, x0, sizeof(double) * s->nx);
46 s->t = t0;
47 s->dt = dt;
48 return 0;
51 void sk_solv_free(sk_solv *s)
53 free(s->x);
54 free(s->x0);
55 if (s->c != NULL) {
56 free(s->c);
57 sk_hist_free(&s->hist);
61 int sk_solv_cont(sk_solv *s)
63 s->cont = 1;
64 if (s->nc)
65 do {
66 s->t += s->dt;
67 sk_hist_get(&s->hist, s->t, s->c);
68 s->sch(s->schd, &(s->rng), s->sys, s->sysd, s->t, s->dt,
69 s->nx, s->x, s->nc, s->c);
70 sk_hist_set(&s->hist, s->t, s->x);
71 s->cont = s->out(s->outd, s->t, s->nx, s->x);
72 } while (s->cont);
73 else
74 do {
75 s->t += s->dt;
76 s->sch(s->schd, &(s->rng), s->sys, s->sysd, s->t, s->dt,
77 s->nx, s->x, s->nc, s->c);
78 s->cont = s->out(s->outd, s->t, s->nx, s->x);
79 } while (s->cont);
81 return 0;