ASCOT5
Loading...
Searching...
No Matches
linint2D.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 "linint.h"
10
26 int n_x, int n_y,
27 int bc_x, int bc_y,
28 real x_min, real x_max,
29 real y_min, real y_max) {
30
31 real x_grid = (x_max - x_min) / ( n_x - 1 * (bc_x == NATURALBC) );
32 real y_grid = (y_max - y_min) / ( n_y - 1 * (bc_y == NATURALBC) );
33
34 str->n_x = n_x;
35 str->n_y = n_y;
36 str->bc_x = bc_x;
37 str->bc_y = bc_y;
38 str->x_min = x_min;
39 str->x_max = x_max;
40 str->x_grid = x_grid;
41 str->y_min = y_min;
42 str->y_max = y_max;
43 str->y_grid = y_grid;
44 str->c = c;
45}
46
61 real c00, c01, c10, c11;
62 real c0, c1;
63
64 /* Make sure periodic coordinates are within [max, min] region. */
65 if(str->bc_x == PERIODICBC) {
66 x = fmod(x - str->x_min, str->x_max - str->x_min) + str->x_min;
67 x = x + (x < str->x_min) * (str->x_max - str->x_min);
68 }
69 if(str->bc_y == PERIODICBC) {
70 y = fmod(y - str->y_min, str->y_max - str->y_min) + str->y_min;
71 y = y + (y < str->y_min) * (str->y_max - str->y_min);
72 }
73
74 /* Index for x variable */
75 int i_x = (x - str->x_min) / str->x_grid;
76 /* Normalized x coordinate in current cell */
77 real dx = ( x - (str->x_min + i_x*str->x_grid) ) / str->x_grid;
78
79 /* Index for y variable */
80 int i_y = (y - str->y_min) / str->y_grid;
81 /* Normalized y coordinate in current cell */
82 real dy = ( y - (str->y_min + i_y*str->y_grid) ) / str->y_grid;
83
84 int n = i_y*str->n_x + i_x; /* Index jump to cell */
85 int x1 = 1; /* Index jump one x forward */
86 int y1 = str->n_x; /* Index jump one y forward */
87
88 int err = 0;
89
90 /* Enforce periodic BC or check that the coordinate is within the grid. */
91 if( str->bc_x == PERIODICBC && i_x == str->n_x-1 ) {
92 x1 = -(str->n_x-1)*x1;
93 }
94 else if( str->bc_x == NATURALBC && !(x >= str->x_min && x <= str->x_max) ) {
95 err = 1;
96 }
97 if( str->bc_y == PERIODICBC && i_y == str->n_y-1 ) {
98 y1 = -(str->n_y-1)*y1;
99 }
100 else if( str->bc_y == NATURALBC && !(y >= str->y_min && y <= str->y_max) ) {
101 err = 1;
102 }
103
104 if(!err) {
105 /* Values at grid cell corners */
106 c00 = str->c[n];
107 c10 = str->c[n + x1];
108 c01 = str->c[n + y1];
109 c11 = str->c[n + y1 + x1];
110 /* Interpolate along x */
111 c0 = c00*(1 - dx) + c10*dx;
112 c1 = c01*(1 - dx) + c11*dx;
113 /* Finally we interpolate these values along y */
114 *f = c0*(1 - dy) + c1*dy;
115 }
116
117 return err;
118}
Main header file for ASCOT5.
double real
Definition ascot5.h:85
@ NATURALBC
Definition interp.h:37
@ PERIODICBC
Definition interp.h:38
Linear interpolation library.
void linint2D_init(linint2D_data *str, real *c, int n_x, int n_y, int bc_x, int bc_y, real x_min, real x_max, real y_min, real y_max)
Initialize linear interpolation struct for scalar 2D data.
Definition linint2D.c:25
DECLARE_TARGET_END int linint2D_eval_f(real *f, linint2D_data *str, real x, real y)
Evaluate interpolated value of 2D scalar field.
Definition linint2D.c:60
real fmod(real x, real y)
Compute the modulus of two real numbers.
Definition math.c:22
Header file for math.c.
2D interpolation struct.
Definition linint.h:33
real y_grid
Definition linint.h:43
real y_max
Definition linint.h:42
real x_max
Definition linint.h:39
real x_min
Definition linint.h:38
real y_min
Definition linint.h:41
real x_grid
Definition linint.h:40
real * c
Definition linint.h:44