-
Notifications
You must be signed in to change notification settings - Fork 5
/
hyrectools.c
269 lines (213 loc) · 8.33 KB
/
hyrectools.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/******************************* HYRECTOOLS.C ********************************
Multidimensional array creation and freeing functions.
Also, function to make linear arrays and interpolation routines.
January 2015 - added cubic interpolation for non-evenly spaced table)
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "hyrectools.h"
/******************************************************************************************************
Square and cube, often used
******************************************************************************************************/
double square(double x) {
return x*x;
}
double cube(double x) {
return x*x*x;
}
/**************************************************************************
Creates a [n1] array.
***************************************************************************/
double *create_1D_array(unsigned n1, int *error, char error_message[SIZE_ErrorM]){
double *matrix = (double *) calloc(n1, sizeof(double));
char sub_message[128];
if (*error == 1) return matrix;
if (matrix == NULL) {
sprintf(sub_message, "unable to allocate memory in create_1D_array \n");
strcat(error_message, sub_message);
*error = 1;
}
return matrix;
}
/**************************************************************************
Creates a [n1][n2] array.
***************************************************************************/
double **create_2D_array(unsigned n1, unsigned n2, int *error, char error_message[SIZE_ErrorM]){
unsigned i;
double **matrix = (double **) calloc(n1, sizeof(double *));
char sub_message[128];
if (*error == 1) return matrix;
if (matrix == NULL){
sprintf(sub_message, "unable to allocate memory in create_2D_array \n");
strcat(error_message, sub_message);
*error = 1;
}
for (i = 0; i < n1; i++) matrix[i] = create_1D_array(n2, error, error_message);
return matrix;
}
/*******************************************************************************
Frees the memory of a [n1][] array.
********************************************************************************/
void free_2D_array(double **matrix, unsigned n1){
unsigned i;
for (i = 0; i < n1; i++) free(matrix[i]);
free(matrix);
}
/*********************************************************************************
Creates a [n1][n2][n3] matrix.
**********************************************************************************/
double ***create_3D_array(unsigned n1, unsigned n2, unsigned n3, int *error, char error_message[SIZE_ErrorM]){
unsigned i;
double ***matrix = (double ***) calloc(n1, sizeof(double **));
char sub_message[128];
if (*error == 1) return matrix;
if (matrix == NULL) {
sprintf(sub_message, "unable to allocate memory in create_3D_array \n");
strcat(error_message, sub_message);
*error = 1;
}
for (i = 0; i < n1; i++) matrix[i] = create_2D_array(n2, n3, error, error_message);
return matrix;
}
/***********************************************************************************
Frees memory of a [n1][n2][] matrix
***********************************************************************************/
void free_3D_array(double ***matrix, unsigned n1, unsigned n2) {
unsigned i;
for (i = 0; i < n1; i++) free_2D_array(matrix[i], n2);
free(matrix);
}
/********************************************************************************************
Making an evenly spaced array.
Input: xmin, xmax, Nx, xtab.
xtab is changed, s.t. xtab[0] = xmin, xtab[Nx-1] = xmax, evenly spaced.
ATTENTION: there will be Nx points, and Nx-1 intervals.
**********************************************************************************************/
void maketab(double xmin, double xmax, unsigned Nx, double *xtab){
unsigned i;
double h = (xmax - xmin)/(Nx - 1.0);
for (i = 0; i < Nx; i++) xtab[i] = xmin + i * h;
}
/************************************************************************************
Interpolation routine for 1-D table. Uses cubic interpolation assuming
uniformly spaced x-values, x0 ... x0+(Nx-1)*dx.
The table is assumed to have dimension Nx >= 4
*************************************************************************************/
double rec_interp1d(double x0, double dx, double *ytab, unsigned int Nx, double x, int *error, char error_message[SIZE_ErrorM]) {
long ix;
double frac;
char sub_message[128];
if (*error == 1) return 0.;
/* Check if in range */
if (dx > 0 && (x<x0 || x>x0+dx*(Nx-1))) {
sprintf(sub_message,"x-value out of range in interpolation in rec_interp1d.\n");
strcat(error_message, sub_message);
*error = 1;
return 0.;
}
if (dx < 0 && (x>x0 || x<x0+dx*(Nx-1))) {
sprintf(sub_message,"x-value out of range in interpolation in rec_interp1d.\n");
strcat(error_message, sub_message);
*error = 1;
return 0.;
}
/* Identify location to interpolate */
ix = (long)floor((x-x0)/dx);
if (ix<1) ix=1;
if (ix>Nx-3) ix=Nx-3;
frac = (x-x0)/dx-ix;
ytab += ix-1;
/* Return value */
return(
-ytab[0]*frac*(1.-frac)*(2.-frac)/6.
+ytab[1]*(1.+frac)*(1.-frac)*(2.-frac)/2.
+ytab[2]*(1.+frac)*frac*(2.-frac)/2.
-ytab[3]*(1.+frac)*frac*(1.-frac)/6.
);
}
/************************************************************************************
Interpolation routine for 2-D table. Uses bicubic interpolation assuming
uniformly spaced x1 and x2-values.
*************************************************************************************/
double rec_interp2d(double x10, double dx1, double x20, double dx2, double **ytab,
unsigned int Nx1, unsigned int Nx2, double x1, double x2, int *error, char error_message[SIZE_ErrorM]) {
int j;
long ix1;
double frac1;
double temp[4];
char sub_message[128];
if (*error == 1) return 0.;
/* Check if in range in x1 */
if (x1<x10 || x1>x10+dx1*(Nx1-1)) {
sprintf(sub_message,"x-value out of range in interpolation in rec_interp2d.\n");
strcat(error_message, sub_message);
*error = 1;
return 0.;
}
/* Identify location to interpolate in x1 */
ix1 = (long)floor((x1-x10)/dx1);
if (ix1<1) ix1=1;
if (ix1>Nx1-3) ix1=Nx1-3;
frac1 = (x1-x10)/dx1-ix1;
ytab += ix1-1;
/* Get values interpolated over x2 at the 4 neighboring points in x1 */
for(j=0;j<4;j++) temp[j] = rec_interp1d(x20,dx2,ytab[j],Nx2,x2, error, error_message);
return(
-temp[0]*frac1*(1.-frac1)*(2.-frac1)/6.
+temp[1]*(1.+frac1)*(1.-frac1)*(2.-frac1)/2.
+temp[2]*(1.+frac1)*frac1*(2.-frac1)/2.
+temp[3]*(1.+frac1)*frac1*(frac1-1.)/6.
);
}
/********************************************************************************
Cubic interpolation for non-regular grid
*******************************************************************************/
double rec_interpol_G(double x, double *xtab, double *ytab, unsigned int Nx, int *error, char error_message[SIZE_ErrorM]) {
char sub_message[128];
if (*error == 1) return 0.;
if (Nx < 4) {
sprintf(sub_message, "Table needs to be of dimension 4 at least in rec_interpol_G.\n");
strcat(error_message, sub_message);
*error = 1;
return 0.;
}
if (xtab[0] > xtab[1]) {
sprintf(sub_message, "Array does not seem to be increasing in rec_interpol_G.\n");
strcat(error_message, sub_message);
*error = 1;
return 0.;
}
if (x < xtab[0] || x >= xtab[Nx-1]) {
sprintf(sub_message, "x-value out of range in rec_interpol_G.\n");
strcat(error_message, sub_message);
*error = 1;
return 0.;
}
long int ix;
long int ilo = 0;
long int ihi = Nx-1;
long int imid;
while (ihi - ilo > 1) {
imid = (ihi + ilo)/2;
if (x >= xtab[imid]) ilo = imid;
if (x < xtab[imid]) ihi = imid;
}
ix = ilo;
if (ix<1) ix = 1;
if (ix>Nx-3) ix = Nx-3;
xtab += ix-1;
ytab += ix-1;
/* Return value */
return(
ytab[0] *(x-xtab[1])*(x-xtab[2])*(x-xtab[3])
/(xtab[0]-xtab[1])/(xtab[0]-xtab[2])/(xtab[0]-xtab[3])
+ ytab[1] *(x-xtab[0])*(x-xtab[2])*(x-xtab[3])
/(xtab[1]-xtab[0])/(xtab[1]-xtab[2])/(xtab[1]-xtab[3])
+ ytab[2] *(x-xtab[0])*(x-xtab[1])*(x-xtab[3])
/(xtab[2]-xtab[0])/(xtab[2]-xtab[1])/(xtab[2]-xtab[3])
+ ytab[3] *(x-xtab[0])*(x-xtab[1])*(x-xtab[2])
/(xtab[3]-xtab[0])/(xtab[3]-xtab[1])/(xtab[3]-xtab[2])
);
}