ASCOT5
Loading...
Searching...
No Matches
interp1Dexpl.c
Go to the documentation of this file.
1
5#include <stdlib.h>
6#include <math.h>
7#include "../ascot5.h"
8#include "../math.h"
9#include "interp.h"
10#include "spline.h"
11
25int interp1Dexpl_init_coeff(real* c, real* f, int n_x, int bc_x,
26 real x_min, real x_max) {
27
28 if(c == NULL) {
29 return 1;
30 }
31
32 /* Calculate cubic spline coefficients. For each grid cell i_x, there are
33 four coefficients. Note how we account for normalized grid. */
34
35 /* Cubic spline along x, using f values to get a total of four
36 coefficients */
37 splineexpl(f, n_x, bc_x, c);
38
39 return 0;
40}
41
53 int n_x, int bc_x, real x_min, real x_max) {
54
55 /* Calculate grid interval. For periodic boundary condition, grid maximum
56 value and the last data point are not the same. Take this into account
57 in grid interval. */
58 real x_grid = (x_max - x_min) / ( n_x - 1 * (bc_x == NATURALBC) );
59
60 /* Initialize the interp1D_data struct */
61 str->n_x = n_x;
62 str->bc_x = bc_x;
63 str->x_min = x_min;
64 str->x_max = x_max;
65 str->x_grid = x_grid;
66 str->c = c;
67}
68
82
83 /* Make sure periodic coordinates are within [min, max] region. */
84 if(str->bc_x == PERIODICBC) {
85 x = fmod(x - str->x_min, str->x_max - str->x_min) + str->x_min;
86 x = x + (x < str->x_min) * (str->x_max - str->x_min);
87 }
88
89 /* Index for x variable. The -1 needed at exactly grid end. */
90 int i_x = (x-str->x_min)/str->x_grid - 1*(x==str->x_max);
91 /* Normalized x coordinate in current cell */
92 real dx = (x-(str->x_min+i_x*str->x_grid))/str->x_grid;
93 /* Helper variables */
94 real dx2 = dx*dx;
95 real dx3 = dx2*dx;
96
97 int n = i_x*4; /* Index jump to cell */
98
99 int err = 0;
100
101 /* Check that the coordinate is within the grid. */
102 if( str->bc_x == NATURALBC && !(x >= str->x_min && x <= str->x_max) ) {
103 err = 1;
104 }
105
106 if(!err) {
107 *f = str->c[n+0]+dx*str->c[n+1]+dx2*str->c[n+2]+dx3*str->c[n+3];
108 }
109
110 return err;
111}
112
132
133 /* Make sure periodic coordinates are within [min, max] region. */
134 if(str->bc_x == PERIODICBC) {
135 x = fmod(x - str->x_min, str->x_max - str->x_min) + str->x_min;
136 x = x + (x < str->x_min) * (str->x_max - str->x_min);
137 }
138
139 /* Index for x variable. The -1 needed at exactly grid end. */
140 int i_x = (x-str->x_min)/str->x_grid - 1*(x==str->x_max);
141 /* Normalized x coordinate in current cell */
142 real dx = (x-(str->x_min+i_x*str->x_grid))/str->x_grid;
143 /* Helper variables */
144 real dx2 = dx*dx;
145 real dx3 = dx2*dx;
146 real xgi = 1.0/str->x_grid;
147
148 int n = i_x*4; /* Index jump to cell */
149
150 int err = 0;
151
152 /* Check that the coordinate is within the grid. */
153 if( str->bc_x == NATURALBC && !(x >= str->x_min && x <= str->x_max) ) {
154 err = 1;
155 }
156
157 if(!err) {
158 /* f */
159 f_df[0] = str->c[n+0]+dx*str->c[n+1]+dx2*str->c[n+2]+dx3*str->c[n+3];
160
161 /* df/dx */
162 f_df[1] = xgi*(str->c[n+1]+2*dx*str->c[n+2]+3*dx2*str->c[n+3]);
163
164 /* d2f/dx2 */
165 f_df[2] = xgi*xgi*(2*str->c[n+2]+6*dx*str->c[n+3]);
166 }
167
168 return err;
169}
Main header file for ASCOT5.
double real
Definition ascot5.h:85
unsigned long int a5err
Simulation error flag.
Definition error.h:17
void interp1Dexpl_init_spline(interp1D_data *str, real *c, int n_x, int bc_x, real x_min, real x_max)
Initialize a cubic spline.
a5err interp1Dexpl_eval_df(real *f_df, interp1D_data *str, real x)
Evaluate interpolated value of 1D and its 1st and 2nd derivatives.
int interp1Dexpl_init_coeff(real *c, real *f, int n_x, int bc_x, real x_min, real x_max)
Calculate cubic spline interpolation coefficients for scalar 1D data.
a5err interp1Dexpl_eval_f(real *f, interp1D_data *str, real x)
Evaluate interpolated value of 1D scalar field.
Spline interpolation library.
@ NATURALBC
Definition interp.h:37
@ PERIODICBC
Definition interp.h:38
real fmod(real x, real y)
Compute the modulus of two real numbers.
Definition math.c:22
Header file for math.c.
Header file for splineexpl.c and splinecomp.c.
void splineexpl(real *f, int n, int bc, real *c)
Calculate explicit cubic spline interpolation coefficients in 1D.
Definition splineexpl.c:22
Cubic interpolation struct.
Definition interp.h:56
real x_min
Definition interp.h:59
real x_max
Definition interp.h:60
real x_grid
Definition interp.h:61
real * c
Definition interp.h:62