forked from christophmschaefer/miluphcuda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaneos.cu
584 lines (521 loc) · 26.2 KB
/
aneos.cu
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/**
* @author Christoph Burger
*
* @section LICENSE
* Copyright (c) 2019 Christoph Burger, Christoph Schaefer
*
* This file is part of miluphcuda.
*
* miluphcuda 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.
*
* miluphcuda 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 miluphcuda. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include "aneos.h"
#include "timeintegration.h"
#include "parameter.h"
// global variables (on the host)
int *g_eos_is_aneos; // TRUE if eos of material is ANEOS
const char **g_aneos_tab_file;
int *g_aneos_n_rho;
int *g_aneos_n_e;
double *g_aneos_rho_0;
double *g_aneos_bulk_cs;
double **g_aneos_rho;
double **g_aneos_e;
double ***g_aneos_p;
#if MORE_ANEOS_OUTPUT
double ***g_aneos_T;
double ***g_aneos_cs;
double ***g_aneos_entropy;
int ***g_aneos_phase_flag;
#endif
#if MORE_ANEOS_OUTPUT
void initialize_aneos_eos_full(const char *aneos_tab_file, int n_rho, int n_e, double *rho, double *e, double **p, double **T, double **cs, double **entropy, int **phase_flag)
/* Fully initializes ANEOS EOS for one material by reading full lookup table from file.*/
{
int i,j;
FILE *f;
// open file containing ANEOS lookup table
if ( (f = fopen(aneos_tab_file,"r")) == NULL )
ERRORVAR("FILE ERROR! Cannot open %s for reading!\n", aneos_tab_file)
// read rho and e (vectors) and p, T, cs, entropy and phase-flag (matrices) from file
for(i=0; i<3; i++)
fscanf(f, "%*[^\n]\n"); // ignore first three lines
if ( fscanf(f, "%le %le %le %le %le %le %d%*[^\n]\n", rho, e, &p[0][0], &T[0][0], &cs[0][0], &entropy[0][0], &phase_flag[0][0] ) != 7 )
ERRORVAR("ERROR! Something's wrong with the ANEOS lookup table in %s\n", aneos_tab_file)
for(j=1; j<n_e; j++)
fscanf(f, "%*le %le %le %le %le %le %d%*[^\n]\n", &e[j], &p[0][j], &T[0][j], &cs[0][j], &entropy[0][j], &phase_flag[0][j] );
for(i=1; i<n_rho; i++) {
fscanf(f, "%le %*le %le %le %le %le %d%*[^\n]\n", &rho[i], &p[i][0], &T[i][0], &cs[i][0], &entropy[i][0], &phase_flag[i][0] );
for(j=1; j<n_e; j++)
fscanf(f, "%*le %*le %le %le %le %le %d%*[^\n]\n", &p[i][j], &T[i][j], &cs[i][j], &entropy[i][j], &phase_flag[i][j] );
}
fclose(f);
}
#else
void initialize_aneos_eos_basic(const char *aneos_tab_file, int n_rho, int n_e, double *rho, double *e, double **p)
/* Initializes basic quantities of the ANEOS EOS for one material by reading only these quantities from the lookup table file.*/
{
int i,j;
FILE *f;
// open file containing ANEOS lookup table
if ( (f = fopen(aneos_tab_file,"r")) == NULL )
ERRORVAR("FILE ERROR! Cannot open %s for reading!\n", aneos_tab_file)
// read rho and e (vectors) and p (matrix) from file
for(i=0; i<3; i++)
fscanf(f, "%*[^\n]\n"); // ignore first three lines
if ( fscanf(f, "%le %le %le%*[^\n]\n", rho, e, &p[0][0] ) != 3 )
ERRORVAR("ERROR! Something's wrong with the ANEOS lookup table in %s\n", aneos_tab_file)
for(j=1; j<n_e; j++)
fscanf(f, "%*le %le %le%*[^\n]\n", &e[j], &p[0][j] );
for(i=1; i<n_rho; i++) {
fscanf(f, "%le %*le %le%*[^\n]\n", &rho[i], &p[i][0] );
for(j=1; j<n_e; j++)
fscanf(f, "%*le %*le %le%*[^\n]\n", &p[i][j] );
}
fclose(f);
}
#endif
void free_aneos_memory()
/* Frees (global) ANEOS memory on the host */
{
int i,j;
for (i=0; i<numberOfMaterials; i++) {
if (g_eos_is_aneos[i]) {
free(g_aneos_rho[i]);
free(g_aneos_e[i]);
for(j = 0; j < g_aneos_n_rho[i]; j++) {
free(g_aneos_p[i][j]);
#if MORE_ANEOS_OUTPUT
free(g_aneos_T[i][j]);
free(g_aneos_cs[i][j]);
free(g_aneos_entropy[i][j]);
free(g_aneos_phase_flag[i][j]);
#endif
}
free(g_aneos_p[i]);
#if MORE_ANEOS_OUTPUT
free(g_aneos_T[i]);
free(g_aneos_cs[i]);
free(g_aneos_entropy[i]);
free(g_aneos_phase_flag[i]);
#endif
}
}
free(g_aneos_rho);
free(g_aneos_e);
free(g_aneos_p);
#if MORE_ANEOS_OUTPUT
free(g_aneos_T);
free(g_aneos_cs);
free(g_aneos_entropy);
free(g_aneos_phase_flag);
#endif
free(g_eos_is_aneos);
free(g_aneos_tab_file);
free(g_aneos_n_rho);
free(g_aneos_n_e);
free(g_aneos_rho_0);
free(g_aneos_bulk_cs);
}
__device__ int array_index(double x, double* array, int n)
/* Uses simple bisection to find the index i in an ordered array (length n)
* that satisfies 'array[i] <= x < array[i+1]'. If x lies outside the array-covered values it returns -1.
*/
{
int i,i1,i2; // current index and its lower and upper bound
// return -1 if x lies outside the array-covered values
if( x < array[0] || x >= array[n-1])
return(-1);
i1 = 0;
i2 = n-1;
do {
i = (int)( (double)(i1+i2)/2.0 );
if( array[i] <= x )
i1 = i; // 'i' becomes new lower bound
else
i2 = i; // 'i' becomes new upper bound
}
while( (i2-i1)>1 );
return(i1);
}
#if MORE_ANEOS_OUTPUT
int array_index_host(double x, double* array, int n)
/* Uses simple bisection to find the index i in an ordered array (length n)
* that satisfies 'array[i] <= x < array[i+1]'. If x lies outside the array-covered values it returns -1.
*/
{
int i,i1,i2; // current index and its lower and upper bound
// return -1 if x lies outside the array-covered values
if( x < array[0] || x >= array[n-1])
return(-1);
i1 = 0;
i2 = n-1;
do {
i = (int)( (double)(i1+i2)/2.0 );
if( array[i] <= x )
i1 = i; // 'i' becomes new lower bound
else
i2 = i; // 'i' becomes new upper bound
}
while( (i2-i1)>1 );
return(i1);
}
#endif
__device__ double bilinear_interpolation_from_linearized(double x, double y, double* table, double* xtab, double* ytab, int ix, int iy, int n_x, int n_y)
/* Performs bilinear interpolation (2d lin. interp.) of values in 'table' which correspond to x- and y-values in xtab and ytab.
* table is a linearized array where rows (connected y-values for a single x-value) are saved successively.
* The target values are x and y. ix holds the index that satisfies 'xtab[ix] <= x < xtab[ix+1]' (similar for iy).
* n_x holds the length of a row of x-values for a single y-value (similar for n_y).
* If (x,y) lies outside the table then ix<0 || iy<0 and the table values are (somewhat linearly) extrapolated.
*/
{
double normx, normy, a, b, p;
// if (x,y) lies outside table then extrapolate (somewhat linearly) and print a warning
if( ix < 0 || iy < 0 )
{
if( ix < 0 && iy < 0 ) // (x,y) lies in one of the 4 "corners"
{
if( x < xtab[0] && y < ytab[0] )
{
normx = (xtab[0]-x) / (xtab[1]-xtab[0]); // (always positive) distance from table end, normalized to x-spacing between 2 outermost table values
normy = (ytab[0]-y) / (ytab[1]-ytab[0]); // (always positive) distance from table end, normalized to y-spacing between 2 outermost table values
p = table[0] + normx*(table[0]-table[n_y]) + normy*(table[0]-table[1]);
}
else if( x < xtab[0] && y >= ytab[n_y-1] )
{
normx = (xtab[0]-x) / (xtab[1]-xtab[0]); // (always positive) distance from table end, normalized to x-spacing between 2 outermost table values
normy = (y-ytab[n_y-1]) / (ytab[n_y-1]-ytab[n_y-2]); // (always positive) distance from table end, normalized to y-spacing between 2 outermost table values
p = table[n_y-1] + normx*(table[n_y-1]-table[2*n_y-1]) + normy*(table[n_y-1]-table[n_y-2]);
}
else if( x >= xtab[n_x-1] && y < ytab[0] )
{
normx = (x-xtab[n_x-1]) / (xtab[n_x-1]-xtab[n_x-2]); // (always positive) distance from table end, normalized to x-spacing between 2 outermost table values
normy = (ytab[0]-y) / (ytab[1]-ytab[0]); // (always positive) distance from table end, normalized to y-spacing between 2 outermost table values
p = table[(n_x-1)*n_y] + normx*(table[(n_x-1)*n_y]-table[(n_x-2)*n_y]) + normy*(table[(n_x-1)*n_y]-table[(n_x-1)*n_y+1]);
}
else if( x >= xtab[n_x-1] && y >= ytab[n_y-1] )
{
normx = (x-xtab[n_x-1]) / (xtab[n_x-1]-xtab[n_x-2]); // (always positive) distance from table end, normalized to x-spacing between 2 outermost table values
normy = (y-ytab[n_y-1]) / (ytab[n_y-1]-ytab[n_y-2]); // (always positive) distance from table end, normalized to y-spacing between 2 outermost table values
p = table[n_x*n_y-1] + normx*(table[n_x*n_y-1]-table[(n_x-1)*n_y-1]) + normy*(table[n_x*n_y-1]-table[n_x*n_y-2]);
}
else
printf("WARNING: Some odd behavior during extrapolation from ANEOS table encountered for rho = %e and e = %e !\n", x, y);
}
else if( ix < 0 )
{
normy = (y-ytab[iy]) / (ytab[iy+1]-ytab[iy]);
if( x < xtab[0] )
{
// linear interpolation in y-direction at xtab[0] and xtab[1]
a = table[iy] + normy*(table[iy+1]-table[iy]);
b = table[n_y+iy] + normy*(table[n_y+iy+1]-table[n_y+iy]);
// linear extrapolation in x-direction from a and b
normx = (x-xtab[0]) / (xtab[1]-xtab[0]); // (always negative) distance from table end, normalized to x-spacing between 2 outermost table values
p = a + normx*(b-a);
}
else if( x >= xtab[n_x-1] )
{
// linear interpolation in y-direction at xtab[n_x-1] and xtab[n_x-2]
a = table[(n_x-1)*n_y+iy] + normy*(table[(n_x-1)*n_y+iy+1]-table[(n_x-1)*n_y+iy]);
b = table[(n_x-2)*n_y+iy] + normy*(table[(n_x-2)*n_y+iy+1]-table[(n_x-2)*n_y+iy]);
// linear extrapolation in x-direction from a and b
normx = (x-xtab[n_x-1]) / (xtab[n_x-1]-xtab[n_x-2]); // (always positive) distance from table end, normalized to x-spacing between 2 outermost table values
p = a + normx*(a-b);
}
else
printf("WARNING: Some odd behavior during extrapolation from ANEOS table encountered for rho = %e and e = %e !\n", x, y);
}
else if( iy < 0 )
{
normx = (x-xtab[ix]) / (xtab[ix+1]-xtab[ix]);
if( y < ytab[0] )
{
// linear interpolation in x-direction at ytab[0] and ytab[1]
a = table[ix*n_y] + normx*(table[(ix+1)*n_y]-table[ix*n_y]);
b = table[ix*n_y+1] + normx*(table[(ix+1)*n_y+1]-table[ix*n_y+1]);
// linear extrapolation in y-direction from a and b
normy = (y-ytab[0]) / (ytab[1]-ytab[0]); // (always negative) distance from table end, normalized to y-spacing between 2 outermost table values
p = a + normy*(b-a);
}
else if( y >= ytab[n_y-1] )
{
// linear interpolation in x-direction at ytab[n_y-1] and ytab[n_y-2]
a = table[(ix+1)*n_y-1] + normx*(table[(ix+2)*n_y-1]-table[(ix+1)*n_y-1]);
b = table[(ix+1)*n_y-2] + normx*(table[(ix+2)*n_y-2]-table[(ix+1)*n_y-2]);
// linear extrapolation in y-direction from a and b
normy = (y-ytab[n_y-1]) / (ytab[n_y-1]-ytab[n_y-2]); // (always positive) distance from table end, normalized to y-spacing between 2 outermost table values
p = a + normy*(a-b);
}
else
printf("WARNING: Some odd behavior during extrapolation from ANEOS table encountered for rho = %e and e = %e !\n", x, y);
}
else
printf("WARNING: Some odd behavior during extrapolation from ANEOS table encountered for rho = %e and e = %e !\n", x, y);
printf("WARNING: At least one of rho = %e and e = %e is out of ANEOS lookup table range! Use extrapolated f(rho,e) = %e\n", x, y, p);
return(p);
}
// calculate normalized distances of x and y from (lower) table values
normx = (x-xtab[ix]) / (xtab[ix+1]-xtab[ix]);
normy = (y-ytab[iy]) / (ytab[iy+1]-ytab[iy]);
// linear interpolation in x-direction at ytab[iy] and ytab[iy+1]
a = table[ix*n_y+iy] + normx*(table[(ix+1)*n_y+iy]-table[ix*n_y+iy]);
b = table[ix*n_y+iy+1] + normx*(table[(ix+1)*n_y+iy+1]-table[ix*n_y+iy+1]);
// linear interpolation in y-direction between a and b
return( a + normy*(b-a) );
}
#if MORE_ANEOS_OUTPUT
double bilinear_interpolation_from_matrix(double x, double y, double** table, double* xtab, double* ytab, int ix, int iy, int n_x, int n_y)
// Performs bilinear interpolation (2d lin. interp.) of values in 'table' which correspond to x- and y-values in 'xtab' and 'ytab'.
// The target values are 'x' and 'y'. 'ix' holds the index that satisfies 'xtab[ix] <= x < xtab[ix+1]' (similar for iy).
// 'n_x' holds the length of a row of x-values for a single y-value (similar for n_y).
// If (x,y) lies outside the table then ix<0 || iy<0 and the table values are (somewhat linearly) extrapolated.
{
double normx = -1.0, normy = -1.0;
double a, b, p = -1.0;
// FILE *f;
// if (x,y) lies outside table then extrapolate (somewhat linearly) and print a warning
if( ix < 0 || iy < 0 )
{
if( ix < 0 && iy < 0 ) // (x,y) lies in one of the 4 "corners"
{
if( x < xtab[0] && y < ytab[0] )
{
normx = (xtab[0]-x) / (xtab[1]-xtab[0]); // (always positive) distance from table end, normalized to x-spacing between 2 outermost table values
normy = (ytab[0]-y) / (ytab[1]-ytab[0]); // (always positive) distance from table end, normalized to y-spacing between 2 outermost table values
p = table[0][0] + normx*(table[0][0]-table[1][0]) + normy*(table[0][0]-table[0][1]);
}
else if( x < xtab[0] && y >= ytab[n_y-1] )
{
normx = (xtab[0]-x) / (xtab[1]-xtab[0]); // (always positive) distance from table end, normalized to x-spacing between 2 outermost table values
normy = (y-ytab[n_y-1]) / (ytab[n_y-1]-ytab[n_y-2]); // (always positive) distance from table end, normalized to y-spacing between 2 outermost table values
p = table[0][n_y-1] + normx*(table[0][n_y-1]-table[1][n_y-1]) + normy*(table[0][n_y-1]-table[0][n_y-2]);
}
else if( x >= xtab[n_x-1] && y < ytab[0] )
{
normx = (x-xtab[n_x-1]) / (xtab[n_x-1]-xtab[n_x-2]); // (always positive) distance from table end, normalized to x-spacing between 2 outermost table values
normy = (ytab[0]-y) / (ytab[1]-ytab[0]); // (always positive) distance from table end, normalized to y-spacing between 2 outermost table values
p = table[n_x-1][0] + normx*(table[n_x-1][0]-table[n_x-2][0]) + normy*(table[n_x-1][0]-table[n_x-1][1]);
}
else if( x >= xtab[n_x-1] && y >= ytab[n_y-1] )
{
normx = (x-xtab[n_x-1]) / (xtab[n_x-1]-xtab[n_x-2]); // (always positive) distance from table end, normalized to x-spacing between 2 outermost table values
normy = (y-ytab[n_y-1]) / (ytab[n_y-1]-ytab[n_y-2]); // (always positive) distance from table end, normalized to y-spacing between 2 outermost table values
p = table[n_x-1][n_y-1] + normx*(table[n_x-1][n_y-1]-table[n_x-2][n_y-1]) + normy*(table[n_x-1][n_y-1]-table[n_x-1][n_y-2]);
}
else
ERRORVAR2("ERROR: Some odd behavior during extrapolation from ANEOS table encountered for rho = %e and e = %e !\n", x, y)
}
else if( ix < 0 )
{
normy = (y-ytab[iy]) / (ytab[iy+1]-ytab[iy]);
if( x < xtab[0] )
{
// linear interpolation in y-direction at xtab[0] and xtab[1]
a = table[0][iy] + normy*(table[0][iy+1]-table[0][iy]);
b = table[1][iy] + normy*(table[1][iy+1]-table[1][iy]);
// linear extrapolation in x-direction from a and b
normx = (x-xtab[0]) / (xtab[1]-xtab[0]); // (always negative) distance from table end, normalized to x-spacing between 2 outermost table values
p = a + normx*(b-a);
}
else if( x >= xtab[n_x-1] )
{
// linear interpolation in y-direction at xtab[n_x-1] and xtab[n_x-2]
a = table[n_x-1][iy] + normy*(table[n_x-1][iy+1]-table[n_x-1][iy]);
b = table[n_x-2][iy] + normy*(table[n_x-2][iy+1]-table[n_x-2][iy]);
// linear extrapolation in x-direction from a and b
normx = (x-xtab[n_x-1]) / (xtab[n_x-1]-xtab[n_x-2]); // (always positive) distance from table end, normalized to x-spacing between 2 outermost table values
p = a + normx*(a-b);
}
else
ERRORVAR2("ERROR: Some odd behavior during extrapolation from ANEOS table encountered for rho = %e and e = %e !\n", x, y)
}
else if( iy < 0 )
{
normx = (x-xtab[ix]) / (xtab[ix+1]-xtab[ix]);
if( y < ytab[0] )
{
// linear interpolation in x-direction at ytab[0] and ytab[1]
a = table[ix][0] + normx*(table[ix+1][0]-table[ix][0]);
b = table[ix][1] + normx*(table[ix+1][1]-table[ix][1]);
// linear extrapolation in y-direction from a and b
normy = (y-ytab[0]) / (ytab[1]-ytab[0]); // (always negative) distance from table end, normalized to y-spacing between 2 outermost table values
p = a + normy*(b-a);
}
else if( y >= ytab[n_y-1] )
{
// linear interpolation in x-direction at ytab[n_y-1] and ytab[n_y-2]
a = table[ix][n_y-1] + normx*(table[ix+1][n_y-1]-table[ix][n_y-1]);
b = table[ix][n_y-2] + normx*(table[ix+1][n_y-2]-table[ix][n_y-2]);
// linear extrapolation in y-direction from a and b
normy = (y-ytab[n_y-1]) / (ytab[n_y-1]-ytab[n_y-2]); // (always positive) distance from table end, normalized to y-spacing between 2 outermost table values
p = a + normy*(a-b);
}
else
ERRORVAR2("ERROR: Some odd behavior during extrapolation from ANEOS table encountered for rho = %e and e = %e !\n", x, y)
}
else
ERRORVAR2("ERROR: Some odd behavior during extrapolation from ANEOS table encountered for rho = %e and e = %e !\n", x, y)
// write a warning to warnings file
// if ( (f = fopen("miluphcuda.warnings", "a")) == NULL )
// ERRORTEXT("FILE ERROR! Cannot open 'miluphcuda.warnings' for appending!\n")
// fprintf(f, "WARNING: At least one of rho = %e and e = %e is out of ANEOS lookup table range! Use extrapolated p(rho,e) = %e\n", x, y, p);
// fclose(f);
return(p);
}
// calculate normalized distances of x and y from (lower) table values
normx = (x-xtab[ix]) / (xtab[ix+1]-xtab[ix]);
normy = (y-ytab[iy]) / (ytab[iy+1]-ytab[iy]);
// linear interpolation in x-direction at ytab[iy] and ytab[iy+1]
a = table[ix][iy] + normx*(table[ix+1][iy]-table[ix][iy]);
b = table[ix][iy+1] + normx*(table[ix+1][iy+1]-table[ix][iy+1]);
// linear interpolation in y-direction between a and b
return( a + normy*(b-a) );
} // end function 'bilinear_interpolation()'
#endif
#if MORE_ANEOS_OUTPUT
int discrete_value_table_lookup_from_matrix(double x, double y, int** table, double* xtab, double* ytab, int ix, int iy, int n_x, int n_y)
// Discrete (int) values in 'table' correspond to x- and y-values (doubles) in 'xtab' and 'ytab'.
// This function finds the closest "corner" (in the x-y-plane) of the respective cell and returns the value of 'table' in that corner.
// The target values are 'x' and 'y'. 'ix' holds the index that satisfies 'xtab[ix] <= x < xtab[ix+1]' (similar for iy).
// 'n_x' holds the length of a row of x-values for a single y-value (similar for n_y).
// If (x,y) lies outside the table then ix<0 || iy<0 and the closest (in the x-y-plane) value of 'table' is returned.
{
int phase_flag = -1;
double normx = -1.0, normy = -1.0;
// FILE *f;
// if (x,y) lies outside table then find the closest value (in the x-y-plane) of 'table' and print a warning
if( ix < 0 || iy < 0 )
{
if( ix < 0 && iy < 0 ) // (x,y) lies in one of the 4 "corners"
{
if( x < xtab[0] && y < ytab[0] ) // "lower left" corner
{
phase_flag = table[0][0];
}
else if( x < xtab[0] && y >= ytab[n_y-1] ) // "upper left" corner
{
phase_flag = table[0][n_y-1];
}
else if( x >= xtab[n_x-1] && y < ytab[0] ) // "lower right" corner
{
phase_flag = table[n_x-1][0];
}
else if( x >= xtab[n_x-1] && y >= ytab[n_y-1] ) // "upper right" corner
{
phase_flag = table[n_x-1][n_y-1];
}
else
ERRORVAR2("ERROR: Some odd behavior during extrapolation from ANEOS table in 'discrete_value_table_lookup()' encountered for rho = %e and e = %e !\n", x, y)
}
else if( ix < 0 )
{
normy = (y-ytab[iy]) / (ytab[iy+1]-ytab[iy]);
if( normy >= 0.5 && normy <= 1.0 )
{
if( x < xtab[0] )
{
phase_flag = table[0][iy+1];
}
else if( x >= xtab[n_x-1] )
{
phase_flag = table[n_x-1][iy+1];
}
else
ERRORVAR2("ERROR: Some odd behavior during extrapolation from ANEOS table in 'discrete_value_table_lookup()' encountered for rho = %e and e = %e !\n", x, y)
}
else if( normy < 0.5 && normy >= 0.0 )
{
if( x < xtab[0] )
{
phase_flag = table[0][iy];
}
else if( x >= xtab[n_x-1] )
{
phase_flag = table[n_x-1][iy];
}
else
ERRORVAR2("ERROR: Some odd behavior during extrapolation from ANEOS table in 'discrete_value_table_lookup()' encountered for rho = %e and e = %e !\n", x, y)
}
else
ERRORVAR("ERROR! 'normy' = %e (is not in [0,1]) in 'discrete_value_table_lookup()' ...\n", normy)
}
else if( iy < 0 )
{
normx = (x-xtab[ix]) / (xtab[ix+1]-xtab[ix]);
if( normx >= 0.5 && normx <= 1.0 )
{
if( y < ytab[0] )
{
phase_flag = table[ix+1][0];
}
else if( y >= ytab[n_y-1] )
{
phase_flag = table[ix+1][n_y-1];
}
else
ERRORVAR2("ERROR: Some odd behavior during extrapolation from ANEOS table in 'discrete_value_table_lookup()' encountered for rho = %e and e = %e !\n", x, y)
}
else if( normx < 0.5 && normx >= 0.0 )
{
if( y < ytab[0] )
{
phase_flag = table[ix][0];
}
else if( y >= ytab[n_y-1] )
{
phase_flag = table[ix][n_y-1];
}
else
ERRORVAR2("ERROR: Some odd behavior during extrapolation from ANEOS table in 'discrete_value_table_lookup()' encountered for rho = %e and e = %e !\n", x, y)
}
else
ERRORVAR("ERROR! 'normx' = %e (is not in [0,1]) in 'discrete_value_table_lookup()' ...\n", normx)
}
else
ERRORVAR2("ERROR: Some odd behavior during extrapolation from ANEOS table in 'discrete_value_table_lookup()' encountered for rho = %e and e = %e !\n", x, y)
// write a warning to warnings file
// if ( (f = fopen("miluphcuda.warnings", "a")) == NULL )
// ERRORTEXT("FILE ERROR! Cannot open 'miluphcuda.warnings' for appending!\n")
// fprintf(f, "WARNING: At least one of rho = %e and e = %e is out of ANEOS lookup table range! Use extrapolated phase-flag = %d\n", x, y, phase_flag);
// fclose(f);
return(phase_flag);
}
// calculate normalized distances of x and y from (lower) table values
normx = (x-xtab[ix]) / (xtab[ix+1]-xtab[ix]);
normy = (y-ytab[iy]) / (ytab[iy+1]-ytab[iy]);
// find the closest "corner" (in the x-y-plane) and return respective value of 'table'
if( normx >= 0.5 && normx <= 1.0 && normy >= 0.5 && normy <= 1.0 ) // "upper right" quadrant of cell
{
phase_flag = table[ix+1][iy+1];
}
else if( normx >= 0.5 && normx <= 1.0 && normy < 0.5 && normy >= 0.0 ) // "lower right" quadrant of cell
{
phase_flag = table[ix+1][iy];
}
else if( normx < 0.5 && normx >= 0.0 && normy >= 0.5 && normy <= 1.0 ) // "upper left" quadrant of cell
{
phase_flag = table[ix][iy+1];
}
else if( normx < 0.5 && normx >= 0.0 && normy < 0.5 && normy >= 0.0 ) // "lower left" quadrant of cell
{
phase_flag = table[ix][iy];
}
else
ERRORVAR2("ERROR: Some odd behavior during \"discrete interpolation\" from ANEOS table in 'discrete_value_table_lookup()' encountered for rho = %e and e = %e !\n", x, y)
return( phase_flag );
} // end function 'discrete_value_table_lookup()'
#endif