| 1 | /* |
|---|
| 2 | Copyright (C) 2011 J. Alberdi, M. Oliveira, Y. Pouillon, and M. Verstraete |
|---|
| 3 | |
|---|
| 4 | This program is free software; you can redistribute it and/or modify |
|---|
| 5 | it under the terms of the GNU Lesser General Public License as published by |
|---|
| 6 | the Free Software Foundation; either version 3 of the License, or |
|---|
| 7 | (at your option) any later version. |
|---|
| 8 | |
|---|
| 9 | This program is distributed in the hope that it will be useful, |
|---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 | GNU Lesser General Public License for more details. |
|---|
| 13 | |
|---|
| 14 | You should have received a copy of the GNU Lesser General Public License |
|---|
| 15 | along with this program; if not, write to the Free Software |
|---|
| 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 17 | |
|---|
| 18 | $Id$ |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | #include "pspio_meshfunc.h" |
|---|
| 22 | |
|---|
| 23 | #include <stdlib.h> |
|---|
| 24 | |
|---|
| 25 | int pspio_meshfunc_alloc(pspio_meshfunc_t *func, pspio_mesh_t *mesh){ |
|---|
| 26 | int i; |
|---|
| 27 | |
|---|
| 28 | func = (pspio_meshfunc_t *) malloc (sizeof(pspio_meshfunc_t)); |
|---|
| 29 | |
|---|
| 30 | if (func == NULL) { |
|---|
| 31 | return PSPIO_ENOMEM; |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | func->f = (double *) malloc (mesh->np * sizeof(double)); |
|---|
| 35 | |
|---|
| 36 | if (func->f == NULL) { |
|---|
| 37 | free (func); |
|---|
| 38 | return PSPIO_ENOMEM; |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | for (i = 0; i < mesh->np; i++) |
|---|
| 42 | { |
|---|
| 43 | func->f[i] = 0; |
|---|
| 44 | } |
|---|
| 45 | func->mesh = mesh; |
|---|
| 46 | func->spl = gsl_spline_alloc(gsl_interp_cspline, mesh->np); |
|---|
| 47 | func->acc = gsl_interp_accel_alloc(); |
|---|
| 48 | |
|---|
| 49 | return PSPIO_SUCCESS; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | int pspio_meshfunc_set(pspio_meshfunc_t *func, double *f){ |
|---|
| 54 | int i; |
|---|
| 55 | |
|---|
| 56 | for (i = 0; i < func->mesh->np; i++) |
|---|
| 57 | { |
|---|
| 58 | func->f[i] = f[i]; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | gsl_spline_init(func->spl, func->mesh->r, func->f, func->mesh->np); |
|---|
| 62 | |
|---|
| 63 | return PSPIO_SUCCESS; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | void pspio_meshfunc_free(pspio_meshfunc_t *func){ |
|---|
| 68 | |
|---|
| 69 | if (func == NULL) return; |
|---|
| 70 | |
|---|
| 71 | free(func->f); |
|---|
| 72 | gsl_spline_free(func->spl); |
|---|
| 73 | gsl_interp_accel_free(func->acc); |
|---|
| 74 | func->mesh = NULL; |
|---|
| 75 | free(func); |
|---|
| 76 | |
|---|
| 77 | } |
|---|