-
Notifications
You must be signed in to change notification settings - Fork 0
/
interp.c
442 lines (377 loc) · 13.9 KB
/
interp.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#include "interp.h"
#include "util.h"
PetscErrorCode Interp1dCreateAndSet( const char *filename, Interp1d *interp_ptr, PetscScalar xconst, PetscScalar yconst )
{
PetscErrorCode ierr;
Interp1d interp;
FILE *fp;
PetscInt i=0;
char string[PETSC_MAX_PATH_LEN];
#if (defined PETSC_USE_REAL___FLOAT128)
char xtemp[30], ytemp[30];
#endif
PetscScalar x, y, xscale=0.0, yscale=0.0;
PetscInt HEAD, NX;
PetscFunctionBeginUser;
ierr = PetscMalloc1(1,interp_ptr);CHKERRQ(ierr);
interp = *interp_ptr;
fp = fopen( filename, "r" );
if (!fp) {
SETERRQ1(PETSC_COMM_WORLD,PETSC_ERR_FILE_OPEN,"Could not open file %s",filename);
}
// fgets reads in string, sscanf processes it
while(fgets(string, sizeof(string), fp) != NULL) {
/* first header contains size of arrays to create */
/* if the header information is incorrect it can lead
to various memory errors. Add some safety checks? */
if( i==0 ){
/* remove # at start of line */
memmove( string, string+1, strlen(string) );
/* will break for 64 bit integers (but presumably
elsewhere in the code will also break) */
sscanf( string, "%d %d", &HEAD, &NX );
/* make arrays based on the sizes in the header */
ierr = PetscMalloc1( NX, &interp->xa ); CHKERRQ(ierr);
ierr = PetscMalloc1( NX, &interp->ya ); CHKERRQ(ierr);
}
/* get column scalings from last line of header */
else if( i==HEAD-1 ){
/* remove # at start of line */
memmove( string, string+1, strlen(string) );
#if (defined PETSC_USE_REAL___FLOAT128)
sscanf( string, "%s %s", xtemp, ytemp );
xscale = strtoflt128(xtemp, NULL);
yscale = strtoflt128(ytemp, NULL);
#else
sscanf(string, "%lf %lf", &xscale, &yscale );
#endif
}
else if( i>=HEAD ){
#if (defined PETSC_USE_REAL___FLOAT128)
sscanf( string, "%s %s", xtemp, ytemp );
x = strtoflt128(xtemp, NULL);
y = strtoflt128(ytemp, NULL);
#else
sscanf(string, "%lf %lf", &x, &y );
#endif
interp->xa[i-HEAD] = x;
interp->xa[i-HEAD] *= xscale;
interp->xa[i-HEAD] /= xconst;
interp->ya[i-HEAD] = y;
interp->ya[i-HEAD] *= yscale;
interp->ya[i-HEAD] /= yconst;
}
++i;
}
fclose( fp );
/* for debugging */
#if (defined DEBUGOUTPUT)
PetscErrorCode ierr;
PetscMPIInt rank;
ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
for (i=0; i<NX; i++ ){
ierr = PetscPrintf(PETSC_COMM_SELF,"[%D] %d %f\n", rank, i, (double) interp->xa[i]);CHKERRQ(ierr);
}
for (i=0; i<NX; i++ ){
ierr = PetscPrintf(PETSC_COMM_SELF,"[%D] %d %f\n", rank, i, (double) interp->ya[i]);CHKERRQ(ierr);
}
#endif
interp->xmin= interp->xa[0];
interp->xmax= interp->xa[NX-1];
/* ymin and ymax are not used at present, and it might be
dangerous to do so since for the middle-out solidus and
liquidus data the maximum entropy is not at the end of the
array since the maximum occurs around mid-mantle pressures */
interp->ymin= interp->ya[0];
interp->ymax= interp->ya[NX-1];
interp->NX = NX;
PetscFunctionReturn(0);
}
PetscErrorCode Interp2dCreateAndSet( const char * filename, Interp2d *interp_ptr, PetscScalar xconst, PetscScalar yconst, PetscScalar zconst )
{
PetscErrorCode ierr;
Interp2d interp;
FILE *fp;
PetscInt i=0, j=0, k=0;
char string[PETSC_MAX_PATH_LEN];
#if (defined PETSC_USE_REAL___FLOAT128)
char xtemp[30], ytemp[30], ztemp[30];
#endif
/* without assigning values below, the compiler warns about possible
uninitialised values for just the quadruple precision case */
PetscScalar xscale=0.0, yscale=0.0, zscale=0.0;
PetscScalar x, y, z;
PetscInt HEAD, NX, NY; // replaced by entries in first header line
PetscInt xind, yind;
PetscFunctionBeginUser;
// NEW - allocate memory for struct
ierr = PetscMalloc1(1, interp_ptr);CHKERRQ(ierr);
interp = *interp_ptr;
fp = fopen( filename, "r" );
if(fp==NULL) {
SETERRQ1(PETSC_COMM_WORLD,PETSC_ERR_FILE_OPEN,"Could not open file %s",filename);
}
// fgets reads in string, sscanf processes it
while(fgets(string, sizeof(string), fp) != NULL) {
/* first header contains size of arrays to create */
/* if the header information is incorrect it can lead
to various memory errors. Add some safety checks? */
if( i==0 ){
/* remove # at start of line */
memmove( string, string+1, strlen(string) );
/* will break for 64 bit integers (but presumably
elsewhere in the code will also break) */
sscanf( string, "%d %d %d", &HEAD, &NX, &NY );
/* make arrays based on the sizes in the header */
ierr = PetscMalloc1( NX, &interp->xa ); CHKERRQ(ierr);
ierr = PetscMalloc1( NY, &interp->ya ); CHKERRQ(ierr);
ierr = Make2DPetscScalarArray( NX, NY, &interp->za ); CHKERRQ(ierr);
}
/* get column scalings from last line of header */
else if( i==HEAD-1 ){
/* remove # at start of line */
memmove( string, string+1, strlen(string) );
#if (defined PETSC_USE_REAL___FLOAT128)
sscanf( string, "%s %s %s", xtemp, ytemp, ztemp );
xscale = strtoflt128(xtemp, NULL);
yscale = strtoflt128(ytemp, NULL);
zscale = strtoflt128(ztemp, NULL);
#else
sscanf( string, "%lf %lf %lf", &xscale, &yscale, &zscale );
#endif
}
else if( i>=HEAD ){
#if (defined PETSC_USE_REAL___FLOAT128)
sscanf( string, "%s %s %s", xtemp, ytemp, ztemp );
x = strtoflt128(xtemp, NULL);
y = strtoflt128(ytemp, NULL);
z = strtoflt128(ztemp, NULL);
#else
sscanf(string, "%lf %lf %lf", &x, &y, &z );
#endif
/* need to determine x and y indices to insert data in
correct position in the za (2-D) array */
xind = (i-HEAD) % NX; // e.g., repeats 0->2019, 0->2019, 0->2019, for each set
yind = (i-HEAD) / NX; // integer division gives correct yind (I think)
/* lookup value */
interp->za[xind][yind] = z;
interp->za[xind][yind] *= zscale;
interp->za[xind][yind] /= zconst;
/* x coordinate */
if( i<HEAD+NX ){
interp->xa[j] = x;
interp->xa[j] *= xscale;
interp->xa[j] /= xconst;
++j;
}
/* y coordinate */
if( (i-HEAD) % NX ==0 ){
interp->ya[k] = y;
interp->ya[k] *= yscale;
interp->ya[k] /= yconst;
++k;
}
}
++i;
}
fclose( fp );
/* for debugging */
#if (defined DEBUGOUTPUT)
PetscErrorCode ierr;
PetscMPIInt rank;
ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
for (i=0; i<NX; i++ ){
ierr = PetscPrintf(PETSC_COMM_SELF,"[%D] %d %f\n", rank, i, (double) interp->xa[i]);CHKERRQ(ierr);
}
for (j=0; j<NY; j++ ){
ierr = PetscPrintf(PETSC_COMM_SELF,"[%D] %d %f\n", rank, j, (double) interp->ya[j]);CHKERRQ(ierr);
}
for(i=0; i<NX; i++ ){
for(j=0; j<NY; j++ ){
ierr = PetscPrintf(PETSC_COMM_SELF,"[%D] %d %d %f\n", rank, i, j, (double) interp->za[i][j]);CHKERRQ(ierr);
}
}
#endif
interp->NX = NX;
interp->NY = NY;
interp->xmin= interp->xa[0];
interp->xmax= interp->xa[NX-1];
interp->ymin= interp->ya[0];
interp->ymax= interp->ya[NY-1];
/* if we store the x and y step, we can more quickly locate the
relevant indices in the arrays by direct calculation, if the
data has constant spacing */
interp->dx = interp->xa[1]-interp->xa[0];
interp->dy = interp->ya[1]-interp->ya[0];
PetscFunctionReturn(0);
}
PetscErrorCode SetInterp1dValue( const Interp1d interp, PetscScalar x, PetscScalar *val, PetscScalar *dval )
{
/* wrapper for evaluating a 1-D lookup
linear interpolation with truncation for values
that fall outside the data lookup range */
PetscScalar const *xa, *ya;
PetscScalar w1, xmin, xmax;
PetscInt ind, NX;
PetscFunctionBeginUser;
NX = interp->NX;
xa = interp->xa;
xmin = interp->xmin;
xmax = interp->xmax;
ya = interp->ya;
/* to reproduce the behaviour of scipy.interpolate.interp1d the
code should produce a ValueError if interpolation is
attempted on a value outside of the range of x (where
extrapolation is necessary). Here we truncate instead. */
if( x<xmin ){
//ierr = PetscPrintf(PETSC_COMM_WORLD,"Warning: get_val1d: x<xmin, %f<%f. Truncating\n",(double)x,(double)xmin);CHKERRQ(ierr);
ind = 0; // minimum index, max index is always +1
x = xmin;
}
else if( x>xmax ){
//ierr = PetscPrintf(PETSC_COMM_WORLD,"Warning: get_val1d: x>xmax, %f>%f. Truncating\n",(double)x,(double)xmax);CHKERRQ(ierr);
ind = NX-2; // minimum index, max index is always +1
x = xmax;
}
else{
// loop to find minimum index
/* trivial algorithm to find minimum index when x data
is not evenly spaced */
ind = 0;
while( (x-xa[ind])>=0) {
ind += 1;
}
/* loop exits when sign changes, meaning that previous index
is the minimum index */
ind -= 1;
}
/* interpolated value */
if( val != NULL ){
// w1 is 0 at leftmost (minimum) x, 1 at rightmost (maximum) x
w1 = (x-xa[ind]) / (xa[ind+1]-xa[ind]); // weighting
*val = ya[ind] * (1.0-w1) + ya[ind+1] * w1;
}
/* first-order derivative estimate */
if( dval != NULL ){
*dval = ( ya[ind+1] - ya[ind] ) / ( xa[ind+1] - xa[ind] );
}
PetscFunctionReturn(0);
}
PetscErrorCode SetInterp2dValue( const Interp2d interp, PetscScalar x, PetscScalar y, PetscScalar *val )
{
/* wrapper for evaluating a 2-D lookup using bilinear
interpolation.
Note that this assumes that x data (pressure) is evenly
spaced so we use a faster lookup approach by computing
indices directly rather than looping through data */
PetscScalar z1, z2, z3, z4;
PetscScalar w1, w2, w3, w4; // weights
PetscScalar const *xa, *ya;
PetscInt NX, NY;
PetscScalar dx, xmin, xmax, ymin, ymax;
// below only if y data is evenly spaced
//PetscScalar dy;
PetscInt indx, indy;
PetscFunctionBeginUser;
NX = interp->NX;
xa = interp->xa;
xmin = interp->xmin;
xmax = interp->xmax;
dx = interp->dx;
NY = interp->NY;
ya = interp->ya;
ymin = interp->ymin;
ymax = interp->ymax;
// below only if y data is evenly spaced
//dy = interp->dy;
/* to reproduce the behaviour of scipy.interpolate.RectBivariateSpline
the code should truncate if interpolation is attempted on a
value outside of the lookup data range */
/* for pressure (x), constant spacing assumed */
if( x<xmin ){
//ierr = PetscPrintf(PETSC_COMM_WORLD,"Warning: get_val2d: x<xmin, %f<%f. Truncating\n",(double)x,(double)xmin);CHKERRQ(ierr);
indx = 0; // minimum index, max index is always +1
x = xmin;
}
else if( x>xmax ){
//ierr = PetscPrintf(PETSC_COMM_WORLD,"Warning: get_val2d: x>xmax, %f>%f. Truncating\n",(double)x,(double)xmax);CHKERRQ(ierr);
indx = NX-2; // minimum index, max index is always +1
x = xmax;
}
else{
indx = PetscFloorReal( (x-xmin)/dx ); // minimum index
}
// x weights
w1 = x-xa[indx]; // x-x1
w2 = xa[indx+1]-x; // x2-x
/* for entropy (y), irregular spacing assumed */
if( y<ymin ){
//ierr = PetscPrintf(PETSC_COMM_WORLD,"Warning: get_val2d: y<ymin, %f<%f. Truncating\n",(double)y,(double)ymin);CHKERRQ(ierr);
indy = 0; // minimum index, max index is always +1
y = ymin;
}
else if( y>ymax ){
//ierr = PetscPrintf(PETSC_COMM_WORLD,"Warning: get_val2d: y>ymax, %f>%f. Truncating\n",(double)y,(double)ymax);CHKERRQ(ierr);
indy = NY-2; // minimum index, max index is always +1
y = ymax;
}
else{
// loop to find minimum index
/* trivial algorithm to find minimum index when y data
is not evenly spaced */
indy = 0;
while( (y-ya[indy])>=0) {
indy += 1;
}
/* loop exits when sign changes, meaning that previous index
is the minimum index */
indy -= 1;
}
// y weights
w3 = y-ya[indy]; // y-y1
w4 = ya[indy+1]-y; // y2-y
// min P (x), min S (y)
z1 = interp->za[indx][indy];
// max P (x), min S (y)
z2 = interp->za[indx+1][indy];
// min P (x), max S (y)
z3 = interp->za[indx][indy+1];
// max P (x), max S (y)
z4 = interp->za[indx+1][indy+1];
// bilinear interpolation
*val = z1 * w2 * w4;
*val += z2 * w1 * w4;
*val += z3 * w2 * w3;
*val += z4 * w1 * w3;
*val /= dx; // dx
*val /= ya[indy+1]-ya[indy]; // dy
PetscFunctionReturn(0);
}
PetscErrorCode Interp1dDestroy( Interp1d *interp_ptr )
{
PetscErrorCode ierr;
Interp1d interp = *interp_ptr;
PetscFunctionBeginUser;
ierr = PetscFree( interp->xa ); CHKERRQ(ierr);
ierr = PetscFree( interp->ya ); CHKERRQ(ierr);
ierr = PetscFree(*interp_ptr);CHKERRQ(ierr);
*interp_ptr = NULL;
PetscFunctionReturn(0);
}
PetscErrorCode Interp2dDestroy( Interp2d *interp_ptr )
{
PetscErrorCode ierr;
Interp2d interp = *interp_ptr;
PetscFunctionBeginUser;
PetscInt i;
PetscInt NX = interp->NX;
for(i=0; i<NX; i++) {
ierr = PetscFree(interp->za[i]);CHKERRQ(ierr);
}
ierr = PetscFree( interp->za );CHKERRQ(ierr);
PetscFree( interp->xa );
PetscFree( interp->ya );
ierr = PetscFree(*interp_ptr);CHKERRQ(ierr);
*interp_ptr = NULL;
PetscFunctionReturn(0);
}