-
Notifications
You must be signed in to change notification settings - Fork 11
/
bilinear.c
182 lines (171 loc) · 6.3 KB
/
bilinear.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* This file is part of interp2d, a GSL-compatible two-dimensional
* interpolation library. <http://www.ellipsix.net/interp2d.html>
*
* Copyright 2012 David Zaslavsky
* Portions based on GNU GSL interpolation code,
* copyright 1996, 1997, 1998, 1999, 2000, 2004 Gerard Jungman
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <gsl/gsl_errno.h>
#include <gsl/gsl_interp.h>
#include "interp2d.h"
static int bilinear_init(void* state, const double xa[], const double ya[], const double za[], size_t xsize, size_t ysize) {
return GSL_SUCCESS;
}
static int bilinear_eval(const void* state, const double xarr[], const double yarr[], const double zarr[], size_t xsize, size_t ysize, double x, double y, gsl_interp_accel* xa, gsl_interp_accel* ya, double* z) {
double xmin, xmax, ymin, ymax, zminmin, zminmax, zmaxmin, zmaxmax;
double dx, dy;
double t, u;
size_t xi, yi;
if (xa != NULL) {
xi = gsl_interp_accel_find(xa, xarr, xsize, x);
}
else {
xi = gsl_interp_bsearch(xarr, x, 0, xsize - 1);
}
if (ya != NULL) {
yi = gsl_interp_accel_find(ya, yarr, ysize, y);
}
else {
yi = gsl_interp_bsearch(yarr, y, 0, ysize - 1);
}
xmin = xarr[xi];
xmax = xarr[xi + 1];
ymin = yarr[yi];
ymax = yarr[yi + 1];
zminmin = zarr[INDEX_2D(xi, yi, xsize, ysize)];
zminmax = zarr[INDEX_2D(xi, yi + 1, xsize, ysize)];
zmaxmin = zarr[INDEX_2D(xi + 1, yi, xsize, ysize)];
zmaxmax = zarr[INDEX_2D(xi + 1, yi + 1, xsize, ysize)];
dx = xmax - xmin;
dy = ymax - ymin;
t = (x - xmin)/dx;
u = (y - ymin)/dy;
*z = (1.-t)*(1.-u)*zminmin + t*(1.-u)*zmaxmin + (1.-t)*u*zminmax + t*u*zmaxmax;
return GSL_SUCCESS;
}
static int bilinear_deriv_x(const void* state, const double xarr[], const double yarr[], const double zarr[], size_t xsize, size_t ysize, double x, double y, gsl_interp_accel* xa, gsl_interp_accel* ya, double* z_p) {
double xmin, xmax, ymin, ymax, zminmin, zminmax, zmaxmin, zmaxmax;
double dx, dy;
double dt, u;
size_t xi, yi;
if (xa != NULL) {
xi = gsl_interp_accel_find(xa, xarr, xsize, x);
}
else {
xi = gsl_interp_bsearch(xarr, x, 0, xsize - 1);
}
if (ya != NULL) {
yi = gsl_interp_accel_find(ya, yarr, ysize, y);
}
else {
yi = gsl_interp_bsearch(yarr, y, 0, ysize - 1);
}
xmin = xarr[xi];
xmax = xarr[xi + 1];
ymin = yarr[yi];
ymax = yarr[yi + 1];
zminmin = zarr[INDEX_2D(xi, yi, xsize, ysize)];
zminmax = zarr[INDEX_2D(xi, yi + 1, xsize, ysize)];
zmaxmin = zarr[INDEX_2D(xi + 1, yi, xsize, ysize)];
zmaxmax = zarr[INDEX_2D(xi + 1, yi + 1, xsize, ysize)];
dx = xmax - xmin;
dy = ymax - ymin;
dt = 1./dx; // partial t / partial x
u = (y - ymin)/dy;
*z_p = dt*(-(1.-u)*zminmin + (1.-u)*zmaxmin - u*zminmax + u*zmaxmax);
return GSL_SUCCESS;
}
static int bilinear_deriv_y(const void* state, const double xarr[], const double yarr[], const double zarr[], size_t xsize, size_t ysize, double x, double y, gsl_interp_accel* xa, gsl_interp_accel* ya, double* z_p) {
double xmin, xmax, ymin, ymax, zminmin, zminmax, zmaxmin, zmaxmax;
double dx, dy;
double t, du;
size_t xi, yi;
if (xa != NULL) {
xi = gsl_interp_accel_find(xa, xarr, xsize, x);
}
else {
xi = gsl_interp_bsearch(xarr, x, 0, xsize - 1);
}
if (ya != NULL) {
yi = gsl_interp_accel_find(ya, yarr, ysize, y);
}
else {
yi = gsl_interp_bsearch(yarr, y, 0, ysize - 1);
}
xmin = xarr[xi];
xmax = xarr[xi + 1];
ymin = yarr[yi];
ymax = yarr[yi + 1];
zminmin = zarr[INDEX_2D(xi, yi, xsize, ysize)];
zminmax = zarr[INDEX_2D(xi, yi + 1, xsize, ysize)];
zmaxmin = zarr[INDEX_2D(xi + 1, yi, xsize, ysize)];
zmaxmax = zarr[INDEX_2D(xi + 1, yi + 1, xsize, ysize)];
dx = xmax - xmin;
dy = ymax - ymin;
t = (x - xmin)/dx;
du = 1./dy; // partial u / partial y
*z_p = du*(-(1.-t)*zminmin - t*zmaxmin + (1.-t)*zminmax + t*zmaxmax);
return GSL_SUCCESS;
}
static int bilinear_deriv2(const void* state, const double xarr[], const double yarr[], const double zarr[], size_t xsize, size_t ysize, double x, double y, gsl_interp_accel* xa, gsl_interp_accel* ya, double* z_pp) {
*z_pp = 0.0;
return GSL_SUCCESS;
}
static int bilinear_derivxy(const void* state, const double xarr[], const double yarr[], const double zarr[], size_t xsize, size_t ysize, double x, double y, gsl_interp_accel* xa, gsl_interp_accel* ya, double* z_pp) {
double xmin, xmax, ymin, ymax, zminmin, zminmax, zmaxmin, zmaxmax;
double dx, dy;
double dt, du;
size_t xi, yi;
if (xa != NULL) {
xi = gsl_interp_accel_find(xa, xarr, xsize, x);
}
else {
xi = gsl_interp_bsearch(xarr, x, 0, xsize - 1);
}
if (ya != NULL) {
yi = gsl_interp_accel_find(ya, yarr, ysize, y);
}
else {
yi = gsl_interp_bsearch(yarr, y, 0, ysize - 1);
}
xmin = xarr[xi];
xmax = xarr[xi + 1];
ymin = yarr[yi];
ymax = yarr[yi + 1];
zminmin = zarr[INDEX_2D(xi, yi, xsize, ysize)];
zminmax = zarr[INDEX_2D(xi, yi + 1, xsize, ysize)];
zmaxmin = zarr[INDEX_2D(xi + 1, yi, xsize, ysize)];
zmaxmax = zarr[INDEX_2D(xi + 1, yi + 1, xsize, ysize)];
dx = xmax - xmin;
dy = ymax - ymin;
dt = 1./dx; // partial t / partial x
du = 1./dy; // partial u / partial y
*z_pp = dt*du*(zminmin-zmaxmin-zminmax+zmaxmax);
return GSL_SUCCESS;
}
static const interp2d_type bilinear_type = {
"bilinear",
2,
NULL,
&bilinear_init,
&bilinear_eval,
&bilinear_deriv_x,
&bilinear_deriv_y,
&bilinear_deriv2,
&bilinear_derivxy,
&bilinear_deriv2,
NULL
};
const interp2d_type* interp2d_bilinear = &bilinear_type;