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