From 41f29cbf3076db51b96f240d27d432ef31b8aa71 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Tue, 14 Apr 2015 16:00:24 +0200 Subject: A major rewrite. Split the code into multiple files, drop the GSL dependency, introduce configurable logging, random cleanups. --- init.c | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 init.c (limited to 'init.c') diff --git a/init.c b/init.c new file mode 100644 index 0000000..759088f --- /dev/null +++ b/init.c @@ -0,0 +1,133 @@ +/* + * 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; + } + + if (!bd->nb_coeffs_z) + bd->nb_coeffs_z = bd->nb_coeffs_rho; + + s->basis = &bdi_sb_even_basis; + + s->nb_coeffs = bd->nb_coeffs_rho * bd->nb_coeffs_z; + + s->nb_colloc_points_rho = bd->nb_coeffs_rho + bd->overdet_rho; + s->nb_colloc_points_z = bd->nb_coeffs_z + bd->overdet_z; + + s->nb_colloc_points = s->nb_colloc_points_rho * s->nb_colloc_points_z; + + s->colloc_grid_order_rho = s->nb_colloc_points_rho; + s->colloc_grid_order_z = s->nb_colloc_points_z; + + 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 = bd->nb_coeffs_rho; + + 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_rho = 80; + bd->nb_coeffs_z = 0; + + bd->basis_scale_factor_rho = 3.0; + bd->basis_scale_factor_z = 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; +} -- cgit v1.2.3