ASCOT5
Toggle main menu visibility
Loading...
Searching...
No Matches
spline
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
25
int
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
52
void
interp1Dexpl_init_spline
(
interp1D_data
* str,
real
* c,
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
81
a5err
interp1Dexpl_eval_f
(
real
* f,
interp1D_data
* str,
real
x) {
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
131
a5err
interp1Dexpl_eval_df
(
real
* f_df,
interp1D_data
* str,
real
x) {
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
}
ascot5.h
Main header file for ASCOT5.
real
double real
Definition
ascot5.h:85
a5err
unsigned long int a5err
Simulation error flag.
Definition
error.h:17
interp.h
Spline interpolation library.
interp1Dexpl_init_spline
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.
Definition
interp1Dexpl.c:52
NATURALBC
@ NATURALBC
Definition
interp.h:37
PERIODICBC
@ PERIODICBC
Definition
interp.h:38
interp1Dexpl_eval_df
DECLARE_TARGET_END a5err interp1Dexpl_eval_df(real *f_df, interp1D_data *str, real x)
Evaluate interpolated value of 1D and its 1st and 2nd derivatives.
Definition
interp1Dexpl.c:131
interp1Dexpl_init_coeff
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.
Definition
interp1Dexpl.c:25
interp1Dexpl_eval_f
DECLARE_TARGET_END a5err interp1Dexpl_eval_f(real *f, interp1D_data *str, real x)
Evaluate interpolated value of 1D scalar field.
Definition
interp1Dexpl.c:81
fmod
real fmod(real x, real y)
Compute the modulus of two real numbers.
Definition
math.c:22
math.h
Header file for math.c.
spline.h
Header file for splineexpl.c and splinecomp.c.
splineexpl
void splineexpl(real *f, int n, int bc, real *c)
Calculate explicit cubic spline interpolation coefficients in 1D.
Definition
splineexpl.c:22
interp1D_data
Cubic interpolation struct.
Definition
interp.h:56
interp1D_data::x_min
real x_min
Definition
interp.h:59
interp1D_data::x_max
real x_max
Definition
interp.h:60
interp1D_data::x_grid
real x_grid
Definition
interp.h:61
interp1D_data::bc_x
int bc_x
Definition
interp.h:58
interp1D_data::c
real * c
Definition
interp.h:62
interp1D_data::n_x
int n_x
Definition
interp.h:57
Generated on
for ASCOT5 by
1.18.0