1 /* Apache 2.0 INS-AMU 2015 */
11 static void setup_buffer_structure(sk_hist
*h
)
16 SK_MALLOCHECK(h
->maxd
= malloc (sizeof(double) * h
->nu
));
17 SK_MALLOCHECK(h
->lim
= malloc (sizeof(double) * (h
->nu
+ 1)));
18 SK_MALLOCHECK(h
->len
= malloc (sizeof(double) * h
->nu
));
19 SK_MALLOCHECK(h
->pos
= malloc (sizeof(double) * h
->nu
));
21 /* vi2i requires max(uvi) then filling in vi2i[ui]=i */
23 for (i
=0; i
<h
->nu
; i
++)
24 if (h
->uvi
[i
] > h
->maxvi
)
27 SK_MALLOCHECK(h
->vi2i
= malloc (sizeof(int) * (h
->maxvi
+ 1)));
29 for (i
=0; i
<h
->nu
; i
++)
34 /* TODO double check */
35 for (j
=0; j
<h
->nd
; j
++)
36 if (h
->vi
[j
]==ui
&& h
->del
[j
]>maxd
)
39 h
->len
[i
] = ceil(maxd
/ h
->dt
) + 2;
44 h
->lim
[i
] = h
->pos
[i
-1] + h
->len
[i
-1];
46 h
->lim
[h
->nu
] = h
->lim
[h
->nu
-1] + h
->len
[h
->nu
-1];
49 void sk_hist_init(sk_hist
*h
, int nd
, int *vi
, double *vd
, double t0
, double dt
)
55 /* identify unique delayed variable indices
56 * NB: this allocates h->uci
58 sk_util_uniqi(nd
, vi
, &(h
->nu
), &(h
->uvi
));
60 /* alloc & copy the delay values */
61 SK_MALLOCHECK(h
->del
= malloc (sizeof(double) * nd
));
62 memcpy(h
->del
, vd
, nd
* sizeof(double));
65 SK_MALLOCHECK(h
->vi
= malloc (sizeof(int) * nd
));
66 memcpy(h
->vi
, vi
, nd
* sizeof(int));
68 /* setup maxd, off, len, pos */
69 setup_buffer_structure(h
);
72 SK_MALLOCHECK(h
->buf
= malloc (sizeof(double) * h
->lim
[h
->nu
]));
75 void sk_hist_free(sk_hist
*h
)
88 void sk_hist_fill(sk_hist
*h
, sk_hist_filler filler
, void *fill_data
)
90 int i
, j
, ui
, o
, n
, *vi
;
94 SK_MALLOCHECK(t
= malloc (sizeof(double) * n
));
95 SK_MALLOCHECK(vi
= malloc (sizeof(int) * n
));
97 /* expand indices per buffer element */
98 for (i
=0; i
<h
->nu
; i
++)
101 for (j
=h
->lim
[i
]; j
<h
->lim
[i
+1]; j
++)
105 /* evaluate time per buffer element */
106 for (i
=0; i
<h
->nu
; i
++)
108 for (j
=0; j
<(h
->len
[i
]-1); j
++)
110 o
= h
->pos
[i
]; /* current position in buffer */
111 o
+= j
; /* time step through buffer */
112 o
%= h
->len
[i
]; /* wrap around on section length */
113 o
+= h
->lim
[i
]; /* offset to start of section */
114 t
[o
] = h
->t
- j
* h
->dt
;
117 o
= h
->pos
[i
]; /* current position in buffer */
118 o
+= j
; /* time step through buffer */
119 o
%= h
->len
[i
]; /* wrap around on section length */
120 o
+= h
->lim
[i
]; /* offset to start of section */
121 t
[o
] = h
->t
+ h
->dt
; /* last point is next grid point */
124 filler(fill_data
, n
, t
, vi
, h
->buf
);
130 void sk_hist_get(sk_hist
*h
, double t
, double *c
)
134 for (i
=0; i
<h
->nd
; i
++)
136 ui
= h
->vi2i
[h
->vi
[i
]];
137 i0
= h
->pos
[ui
] - floor((t
- h
->del
[i
] - h
->t
) / h
->dt
);
139 i1
= i0
? i0
- 1 : h
->len
[ui
]-1;
144 if ((i0
< 0) || (i0
> h
->lim
[h
->nu
]))
145 fprintf(stderr
, "[sk_hist] oob i0=%d %s:%d\n", i0
, __FILE__
, __LINE__
);
146 if ((i1
< 0) || (i1
> h
->lim
[h
->nu
]))
147 fprintf(stderr
, "[sk_hist] oob i1=%d %s:%d\n", i1
, __FILE__
, __LINE__
);
149 c
[i
] = (h
->buf
[i1
] - h
->buf
[i0
]) / h
->dt
150 * fmod((t
+ h
->del
[i
]), h
->dt
) + h
->buf
[i0
];
154 static void update_time(sk_hist
*h
, double new_t
)
158 /* the current time must always be contained between
159 * pos and pos-1 in the buffer
162 while ((h
->t
+ h
->dt
) < new_t
)
168 /* if no steps to take, return early */
172 /* update positions */
173 for (i
=0; i
<h
->nu
; i
++)
175 h
->pos
[i
] -= n_steps
;
177 h
->pos
[i
] += h
->len
[i
];
181 void sk_hist_set(sk_hist
*h
, double t
, double *x
)
188 /* extrapolate from (x(h->t), x(t)) to next grid point*/
189 for (i
=0; i
<h
->nu
; i
++)
192 i1
= i0
? i0
- 1 : h
->len
[i
] - 1;
197 dx
= x
[h
->uvi
[i
]] - h
->buf
[i0
];
198 h
->buf
[i1
] = (dx
/ dt
) * h
->dt
+ x0
;
202 int sk_hist_nbytes(sk_hist
*h
)
205 nb
= sizeof(sk_hist
);
206 nb
+= sizeof(int) * ((h
->nu
+1) + 3*h
->nu
+ h
->nd
+ h
->maxvi
);
207 nb
+= sizeof(double) * (h
->lim
[h
->nu
] + h
->nu
+ h
->nd
);
211 void sk_hist_zero_filler(void *data
, int n
, double *t
, int *indices
, double *buf
)
214 /* suppress unused parameter warnings */
215 (void) data
; (void) n
; (void) t
; (void) indices
;