/* * Copyright 2014-2015 Anton Khirnov * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include #include #include #include #include #include "brill_data.h" #include "internal.h" static int brill_init_check_options(BDContext *bd) { BDPriv *s = bd->priv; switch (bd->q_func_type) { case BD_Q_FUNC_GUNDLACH: s->q_func = &bdi_q_func_gundlach; break; case BD_Q_FUNC_EPPLEY: if (bd->eppley_n < 4) { bdi_log(bd, 0, "Invalid n: %d < 4\n", bd->eppley_n); return -EINVAL; } s->q_func = &bdi_q_func_eppley; break; default: bdi_log(bd, 0, "Unknown q function type: %d\n", bd->q_func_type); return -EINVAL; } s->basis[0] = &bdi_sb_even_basis; s->basis[1] = &bdi_sb_even_basis; s->nb_colloc_points[0] = bd->nb_coeffs[0]; s->nb_colloc_points[1] = bd->nb_coeffs[1]; s->nb_coeffs[0] = bd->nb_coeffs[0]; s->nb_coeffs[1] = bd->nb_coeffs[1]; return 0; } int bd_solve(BDContext *bd) { BDPriv *s = bd->priv; int ret; if (bd->psi_minus1_coeffs) { bdi_log(bd, 0, "Solve called multiple times on the same context.\n"); return -EINVAL; } ret = brill_init_check_options(bd); if (ret < 0) return ret; ret = bdi_solve(bd); if (ret < 0) return ret; bd->psi_minus1_coeffs = s->coeffs; bd->stride = s->coeffs_stride; return 0; } BDContext *bd_context_alloc(void) { BDContext *bd = calloc(1, sizeof(*bd)); if (!bd) return NULL; bd->q_func_type = BD_Q_FUNC_GUNDLACH; bd->amplitude = 1.0; bd->eppley_n = 5; bd->nb_coeffs[0] = 80; bd->nb_coeffs[1] = 80; bd->basis_scale_factor[0] = 3.0; bd->basis_scale_factor[1] = 3.0; bd->log_callback = bdi_log_default_callback; bd->priv = calloc(1, sizeof(BDPriv)); if (!bd->priv) { free(bd); return NULL; } return bd; } void bd_context_free(BDContext **pbd) { BDContext *bd = *pbd; BDPriv *s; if (!bd) return; s = bd->priv; free(s->coeffs); free(bd->priv); free(bd); *pbd = NULL; }