void sd_err_handler(int err, char *file, int line, char *func, char *reason) { if (handler!=NULL) (*handler)(err, file, line, func, reason); #ifdef SDDEBUG else sd_log_debug("%s raised error at %s:%d `%s' (ignored by null handler).", func, file, line, reason); #endif }
static void get(sd_hist *hist, double t, double *aff) { uint32_t i; hist_data *h = hist->ptr; if (h==NULL) return; for (i = 0; i < h->nd; i++) { uint32_t ui = h->vi2i[h->vi[i]] /* unique variable index */ , p = h->pos[ui] /* rel pos in ui's var buf */ , l = h->len[ui] /* len of ui's var buf */ , o = h->lim[ui]; /* offset of ui's buf in h->buf */ /* TODO the following dt is not sol dt or hist dt, find better name */ double dt = (t - h->del[i]) - (h->t - (l - 2)*h->dt); /* relative buffer indices */ int64_t i0 = (int64_t) ceil((l - 2) - dt / h->dt); if (i0 < 0) i0 += l; int64_t i1 = i0 > 0 ? ((uint32_t) i0) - 1 : l - 1; /* absolute buffer indices */ uint32_t i0_ = (p + i0) % l + o , i1_ = (p + i1) % l + o; #ifdef SDDEBUG if ((i0_ < h->lim[ui]) || (i0_ >= h->lim[ui+1])) sd_log_debug("[sd_hist_get] oob: i0_=%d not in [%d, %d) at %s:%d\n", i0_, h->lim[ui], h->lim[ui+1], __FILE__, __LINE__ ); if ((i1_ < h->lim[ui]) || (i1_ >= h->lim[ui+1])) sd_log_debug("[sd_hist_get] oob: i0_=%d not in [%d, %d) at %s:%d\n", i1_, h->lim[ui], h->lim[ui+1], __FILE__, __LINE__ ); #endif double y0 = h->buf[i0_] /* consider sse read */ , y1 = h->buf[i1_] , m = (y1 - y0) / h->dt , dx = (t - h->del[i]) - (h->t - i0*h->dt); aff[i] = m * dx + y0; } }
static double get_buf_lin(sd_hist *hist, uint32_t index) { hist_data *h = hist->ptr; #ifdef SDDEBUG if (index >= h->lim[h->nu]) sd_log_debug( "[hist->get_buf_lin] oob index=%d not in [0, %d)\n", index, h->lim[h->nu] ); #endif return h->buf[index]; }
static enum sd_stat cont(struct sd_sol *sol) { clock_t tic = clock(); struct data *data = sol->data; struct sd_out *out = data->out; struct sd_sch *sch = data->sch; data->cont = SD_CONT; do { if (sch->apply(sch)!=SD_OK) { sd_err("scheme application failed."); return SD_ERR; } struct sd_out_sample sample = sch->sample(sch); data->cont = out->apply(out, &sample); } while (data->cont == SD_CONT); clock_t toc = clock(); double walltime = (double) (toc - tic) / CLOCKS_PER_SEC; sd_log_debug("continuation required %.3f s walltime", walltime); return SD_OK; }
static void set(sd_hist *hist, double t, double *eff) { uint32_t i, i0, i1; double x0, dx, dt; hist_data *h = hist->ptr; update_time(hist, t); for (i=0; i<h->nu; i++) { i0 = h->pos[i]; i1 = i0 ? i0 - 1 : h->len[i] - 1; i0 += h->lim[i]; i1 += h->lim[i]; #ifdef SDDEBUG if ((i0 < h->lim[i]) || (i0 > h->lim[i+1])) fprintf(stderr, "[sd_hist_set] t=%.3f ui=%d, i0=%d not in [%d,%d) %s:%d\n", t, i, i0, h->lim[i], h->lim[i+1], __FILE__, __LINE__); if ((i1 < h->lim[i]) || (i1 > h->lim[i+1])) fprintf(stderr, "[sd_hist_set] t=%.3f ui=%d, i1=%d not in [%d,%d) %s:%d\n", t, i, i1, h->lim[i], h->lim[i+1], __FILE__, __LINE__); #endif x0 = h->buf[i0]; dt = t - h->t; #ifdef SDDEBUG if (dt < 0) sd_log_debug( "[sd_hist] unhandled dt<0 at %s:%d\n", __FILE__, __LINE__ ); #endif if (dt > 0) { /* extrapolate from (x(h->t), x(t)) to next grid point*/ dx = eff[h->vi2i[h->uvi[i]]] - h->buf[i0]; h->buf[i1] = (dx / dt) * h->dt + x0; } else { /* reset grid point value */ h->buf[i0] = eff[h->vi2i[h->uvi[i]]]; } } }
static sd_stat cont(sd_sol *sol) { clock_t tic = clock(); sol_data *s = sol->ptr; sd_hist *h; h = s->nca ? s->hist : NULL; s->cont = SD_CONT; do { if (s->sch->apply(s->sch, h, s->rng, s->sys, s->t, s->dt, s->nx, s->x, s->nce, s->c)!=SD_OK) { sd_err("scheme failure."); return SD_ERR; } s->t += s->dt; s->cont = s->out->apply(s->out, s->t, s->nx, s->x, s->nce, s->c); } while (s->cont == SD_CONT); clock_t toc = clock(); double walltime = (double) (toc - tic) / CLOCKS_PER_SEC; sd_log_debug("continuation required %.3f s walltime", walltime); return SD_OK; }
void sd_err_default_handler(int err, char *file, int line, char *func, char *reason) { (void) err; sd_log_debug("%s raised error at %s:%d `%s'.", func, file, line, reason); }
void sd_err_set_handler(sd_err_handler_fp fp) { if (fp==NULL) sd_log_debug("set NULL error handler."); handler = fp; }