Skip to content

Commit 67e7d78

Browse files
Merge pull request #1072 from OceanParcels/reducing_interpolationerrors_JIT
Reducing interpolation errors in JIT
2 parents d6218d8 + b8aaa05 commit 67e7d78

File tree

3 files changed

+49
-41
lines changed

3 files changed

+49
-41
lines changed

parcels/grid.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ def __init__(self, lon, lat, time, time_origin, mesh):
275275
if self.ydim > 1 and self.lat[-1] < self.lat[0]:
276276
self.lat = np.flip(self.lat, axis=0)
277277
self.lat_flipped = True
278-
logger.warning_once("Flipping lat data from North-South to South-North")
278+
logger.warning_once("Flipping lat data from North-South to South-North. "
279+
"Note that this may lead to wrong sign for meridional velocity, so tread very carefully")
279280

280281
def add_periodic_halo(self, zonal, meridional, halosize=5):
281282
"""Add a 'halo' to the Grid, through extending the Grid (and lon/lat)

parcels/include/index_search.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ extern "C" {
99
#include <math.h>
1010

1111
#define CHECKSTATUS(res) do {if (res != SUCCESS) return res;} while (0)
12+
#define rtol 1.e-5
13+
#define atol 1.e-8
1214

1315
#ifdef DOUBLE_COORD_VARIABLES
1416
typedef double type_coord;
@@ -56,6 +58,44 @@ typedef enum
5658
RECTILINEAR_Z_GRID=0, RECTILINEAR_S_GRID=1, CURVILINEAR_Z_GRID=2, CURVILINEAR_S_GRID=3
5759
} GridCode;
5860

61+
// equal/closeness comparison that is equal to numpy (double)
62+
static inline bool is_close_dbl(double a, double b) {
63+
return (fabs(a-b) <= (atol + rtol * fabs(b)));
64+
}
65+
66+
// customisable equal/closeness comparison (double)
67+
static inline bool is_close_dbl_tol(double a, double b, double tolerance) {
68+
return (fabs(a-b) <= (tolerance + fabs(b)));
69+
}
70+
71+
// numerically accurate equal/closeness comparison (double)
72+
static inline bool is_equal_dbl(double a, double b) {
73+
return (fabs(a-b) <= (DBL_EPSILON * fabs(b)));
74+
}
75+
76+
// customisable equal/closeness comparison (float)
77+
static inline bool is_close_flt_tol(float a, float b, float tolerance) {
78+
return (fabs(a-b) <= (tolerance + fabs(b)));
79+
}
80+
81+
// equal/closeness comparison that is equal to numpy (float)
82+
static inline bool is_close_flt(float a, float b) {
83+
return (fabs(a-b) <= ((float)(atol) + (float)(rtol) * fabs(b)));
84+
}
85+
86+
// numerically accurate equal/closeness comparison (float)
87+
static inline bool is_equal_flt(float a, float b) {
88+
return (fabs(a-b) <= (FLT_EPSILON * fabs(b)));
89+
}
90+
91+
static inline bool is_zero_dbl(double a) {
92+
return (fabs(a) <= DBL_EPSILON * fabs(a));
93+
}
94+
95+
static inline bool is_zero_flt(float a) {
96+
return (fabs(a) <= FLT_EPSILON * fabs(a));
97+
}
98+
5999
static inline StatusCode search_indices_vertical_z(type_coord z, int zdim, float *zvals, int *zi, double *zeta, int gridindexingtype)
60100
{
61101
if (zvals[zdim-1] > zvals[0]){
@@ -260,6 +300,13 @@ static inline StatusCode search_indices_rectilinear(type_coord x, type_coord y,
260300
else
261301
*zeta = 0;
262302

303+
if ( (*xsi < 0) && (is_zero_dbl(*xsi)) ) {*xsi = 0.;}
304+
if ( (*xsi > 1) && (is_close_dbl(*xsi, 1.)) ) {*xsi = 1.;}
305+
if ( (*eta < 0) && (is_zero_dbl(*eta)) ) {*eta = 0.;}
306+
if ( (*eta > 1) && (is_close_dbl(*eta, 1.)) ) {*eta = 1.;}
307+
if ( (*zeta < 0) && (is_zero_dbl(*zeta)) ) {*zeta = 0.;}
308+
if ( (*zeta > 1) && (is_close_dbl(*zeta, 1.)) ) {*zeta = 1.;}
309+
263310
if ( (*xsi < 0) || (*xsi > 1) ) return ERROR_INTERPOLATION;
264311
if ( (*eta < 0) || (*eta > 1) ) return ERROR_INTERPOLATION;
265312
if ( (*zeta < 0) || (*zeta > 1) ) return ERROR_INTERPOLATION;

parcels/include/parcels.h

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ extern "C" {
1515

1616
#define min(X, Y) (((X) < (Y)) ? (X) : (Y))
1717
#define max(X, Y) (((X) > (Y)) ? (X) : (Y))
18-
#define rtol 1.e-5
19-
#define atol 1.e-8
2018

2119
typedef struct
2220
{
@@ -25,44 +23,6 @@ typedef struct
2523
CGrid *grid;
2624
} CField;
2725

28-
// customisable equal/closeness comparison (double)
29-
static inline bool is_close_dbl_tol(double a, double b, double tolerance) {
30-
return (fabs(a-b) <= (tolerance + fabs(b)));
31-
}
32-
33-
// equal/closeness comparison that is equal to numpy (double)
34-
static inline bool is_close_dbl(double a, double b) {
35-
return (fabs(a-b) <= (atol + rtol * fabs(b)));
36-
}
37-
38-
// numerically accurate equal/closeness comparison (double)
39-
static inline bool is_equal_dbl(double a, double b) {
40-
return (fabs(a-b) <= (DBL_EPSILON * fabs(b)));
41-
}
42-
43-
// customisable equal/closeness comparison (float)
44-
static inline bool is_close_flt_tol(float a, float b, float tolerance) {
45-
return (fabs(a-b) <= (tolerance + fabs(b)));
46-
}
47-
48-
// equal/closeness comparison that is equal to numpy (float)
49-
static inline bool is_close_flt(float a, float b) {
50-
return (fabs(a-b) <= ((float)(atol) + (float)(rtol) * fabs(b)));
51-
}
52-
53-
// numerically accurate equal/closeness comparison (float)
54-
static inline bool is_equal_flt(float a, float b) {
55-
return (fabs(a-b) <= (FLT_EPSILON * fabs(b)));
56-
}
57-
58-
static inline bool is_zero_dbl(double a) {
59-
return (fabs(a) <= DBL_EPSILON * fabs(a));
60-
}
61-
62-
static inline bool is_zero_flt(float a) {
63-
return (fabs(a) <= FLT_EPSILON * fabs(a));
64-
}
65-
6626
/* Bilinear interpolation routine for 2D grid */
6727
static inline StatusCode spatial_interpolation_bilinear(double xsi, double eta, float data[2][2], float *value)
6828
{

0 commit comments

Comments
 (0)