aboutsummaryrefslogtreecommitdiff
path: root/init.c
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2015-04-14 16:00:24 +0200
committerAnton Khirnov <anton@khirnov.net>2015-04-14 16:02:58 +0200
commit41f29cbf3076db51b96f240d27d432ef31b8aa71 (patch)
treeaedc8f140385f4c94054b561e09358a04ffbc296 /init.c
parent307d35ed2bc4e073ae3a6ceac3dcb7a74f25e98a (diff)
A major rewrite.
Split the code into multiple files, drop the GSL dependency, introduce configurable logging, random cleanups.
Diffstat (limited to 'init.c')
-rw-r--r--init.c133
1 files changed, 133 insertions, 0 deletions
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 <anton@khirnov.net>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <errno.h>
+#include <math.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+
+#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;
+}