sesame.h 18.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*******************************************************************************
 * This file is part of SWIFT.
 * Copyright (c) 2016   Matthieu Schaller (matthieu.schaller@durham.ac.uk).
 *               2018   Jacob Kegerreis (jacob.kegerreis@durham.ac.uk).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser 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 Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 ******************************************************************************/
20
#ifndef SWIFT_SESAME_EQUATION_OF_STATE_H
21
22
23
24
25
#define SWIFT_SESAME_EQUATION_OF_STATE_H

/**
 * @file equation_of_state/planetary/sesame.h
 *
26
 * Contains the SESAME and ANEOS-in-SESAME-style EOS functions for
27
28
29
30
31
32
 * equation_of_state/planetary/equation_of_state.h
 *
 */

/* Some standard headers. */
#include <math.h>
33
#include <float.h>
34
35
36
37

/* Local headers. */
#include "adiabatic_index.h"
#include "common_io.h"
Matthieu Schaller's avatar
Matthieu Schaller committed
38
#include "equation_of_state.h"
39
40
#include "inline.h"
#include "physical_constants.h"
Matthieu Schaller's avatar
Matthieu Schaller committed
41
#include "units.h"
42
#include "utilities.h"
43

44
// SESAME parameters
45
struct SESAME_params {
46
47
48
49
50
51
52
  float *table_log_rho;
  float *table_log_u_rho_T;
  float *table_P_rho_T;
  float *table_c_rho_T;
  float *table_s_rho_T;
  int num_rho, num_T;
  float P_tiny, c_tiny;
53
  enum eos_planetary_material_id mat_id;
54
55
56
};

// Parameter values for each material (cgs units)
57
58
INLINE static void set_SESAME_iron(struct SESAME_params *mat,
                                   enum eos_planetary_material_id mat_id) {
59
  // SESAME 2140
Matthieu Schaller's avatar
Matthieu Schaller committed
60
  mat->mat_id = mat_id;
61
}
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
INLINE static void set_SESAME_basalt(struct SESAME_params *mat,
                                     enum eos_planetary_material_id mat_id) {
  // SESAME 7530
  mat->mat_id = mat_id;
}
INLINE static void set_SESAME_water(struct SESAME_params *mat,
                                    enum eos_planetary_material_id mat_id) {
  // SESAME 7154
  mat->mat_id = mat_id;
}
INLINE static void set_SS08_water(struct SESAME_params *mat,
                                  enum eos_planetary_material_id mat_id) {
  // Senft & Stewart (2008)
  mat->mat_id = mat_id;
}
77
78
79
80
81
INLINE static void set_ANEOS_forsterite(struct SESAME_params *mat,
                                        enum eos_planetary_material_id mat_id) {
  // Stewart et al. (2019)
  mat->mat_id = mat_id;
}
82
83
84
85
86
87
88
89
90
91
INLINE static void set_ANEOS_iron(struct SESAME_params *mat,
                                  enum eos_planetary_material_id mat_id) {
  // Stewart (2020)
  mat->mat_id = mat_id;
}
INLINE static void set_ANEOS_Fe85Si15(struct SESAME_params *mat,
                                      enum eos_planetary_material_id mat_id) {
  // Stewart (2020)
  mat->mat_id = mat_id;
}
92
93
94
95
96
97
98

// Read the tables from file
INLINE static void load_table_SESAME(struct SESAME_params *mat,
                                     char *table_file) {

  // Load table contents from file
  FILE *f = fopen(table_file, "r");
Jacob Kegerreis's avatar
Jacob Kegerreis committed
99
  if (f == NULL) error("Failed to open the SESAME EoS file '%s'", table_file);
100
101
102
103

  // Ignore header lines
  char buffer[100];
  for (int i = 0; i < 5; i++) {
Bert Vandenbroucke's avatar
Bert Vandenbroucke committed
104
    if (fgets(buffer, 100, f) == NULL)
Jacob Kegerreis's avatar
Jacob Kegerreis committed
105
      error("Failed to read the SESAME EoS file header %s", table_file);
106
107
108
109
  }
  float ignore;

  // Table properties
110
  int c = fscanf(f, "%d %d", &mat->num_rho, &mat->num_T);
Jacob Kegerreis's avatar
Jacob Kegerreis committed
111
  if (c != 2) error("Failed to read the SESAME EoS table %s", table_file);
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

  // Ignore the first elements of rho = 0, T = 0
  mat->num_rho--;
  mat->num_T--;

  // Allocate table memory
  mat->table_log_rho = (float *)malloc(mat->num_rho * sizeof(float));
  mat->table_log_u_rho_T =
      (float *)malloc(mat->num_rho * mat->num_T * sizeof(float));
  mat->table_P_rho_T =
      (float *)malloc(mat->num_rho * mat->num_T * sizeof(float));
  mat->table_c_rho_T =
      (float *)malloc(mat->num_rho * mat->num_T * sizeof(float));
  mat->table_s_rho_T =
      (float *)malloc(mat->num_rho * mat->num_T * sizeof(float));

  // Densities (not log yet)
  for (int i_rho = -1; i_rho < mat->num_rho; i_rho++) {
    // Ignore the first elements of rho = 0, T = 0
    if (i_rho == -1) {
      c = fscanf(f, "%f", &ignore);
Jacob Kegerreis's avatar
Jacob Kegerreis committed
133
      if (c != 1) error("Failed to read the SESAME EoS table %s", table_file);
134
135
    } else {
      c = fscanf(f, "%f", &mat->table_log_rho[i_rho]);
Jacob Kegerreis's avatar
Jacob Kegerreis committed
136
      if (c != 1) error("Failed to read the SESAME EoS table %s", table_file);
137
138
139
140
141
142
    }
  }

  // Temperatures (ignored)
  for (int i_T = -1; i_T < mat->num_T; i_T++) {
    c = fscanf(f, "%f", &ignore);
Jacob Kegerreis's avatar
Jacob Kegerreis committed
143
    if (c != 1) error("Failed to read the SESAME EoS table %s", table_file);
144
145
146
147
148
149
150
151
  }

  // Sp. int. energies (not log yet), pressures, sound speeds, and entropies
  for (int i_T = -1; i_T < mat->num_T; i_T++) {
    for (int i_rho = -1; i_rho < mat->num_rho; i_rho++) {
      // Ignore the first elements of rho = 0, T = 0
      if ((i_T == -1) || (i_rho == -1)) {
        c = fscanf(f, "%f %f %f %f", &ignore, &ignore, &ignore, &ignore);
Jacob Kegerreis's avatar
Jacob Kegerreis committed
152
        if (c != 4) error("Failed to read the SESAME EoS table %s", table_file);
153
154
155
156
157
158
      } else {
        c = fscanf(f, "%f %f %f %f",
                   &mat->table_log_u_rho_T[i_rho * mat->num_T + i_T],
                   &mat->table_P_rho_T[i_rho * mat->num_T + i_T],
                   &mat->table_c_rho_T[i_rho * mat->num_T + i_T],
                   &mat->table_s_rho_T[i_rho * mat->num_T + i_T]);
Jacob Kegerreis's avatar
Jacob Kegerreis committed
159
        if (c != 4) error("Failed to read the SESAME EoS table %s", table_file);
160
161
162
163
164
165
      }
    }
  }

  fclose(f);
}
166

167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Misc. modifications
INLINE static void prepare_table_SESAME(struct SESAME_params *mat) {

  // Convert densities to log(density)
  for (int i_rho = 0; i_rho < mat->num_rho; i_rho++) {
    mat->table_log_rho[i_rho] = logf(mat->table_log_rho[i_rho]);
  }

  // Convert sp. int. energies to log(sp. int. energy)
  for (int i_rho = 0; i_rho < mat->num_rho; i_rho++) {
    for (int i_T = 0; i_T < mat->num_T; i_T++) {
      // If not positive then set very small for the log
      if (mat->table_log_u_rho_T[i_rho * mat->num_T + i_T] <= 0) {
        mat->table_log_u_rho_T[i_rho * mat->num_T + i_T] = 1.f;
      }

      mat->table_log_u_rho_T[i_rho * mat->num_T + i_T] =
          logf(mat->table_log_u_rho_T[i_rho * mat->num_T + i_T]);
    }
  }

188
189
190
  // Initialise tiny pressure and soundspeed
  mat->P_tiny = FLT_MAX;
  mat->c_tiny = FLT_MAX;
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

  // Enforce that the 1D arrays of u (at each rho) are monotonic
  // This is necessary because, for some high-density u slices at very low T,
  // u decreases (very slightly) with T, which makes the interpolation fail
  for (int i_rho = 0; i_rho < mat->num_rho; i_rho++) {
    for (int i_T = mat->num_T - 1; i_T > 0; i_T--) {

      // If the one-lower-T u is greater than this u
      if (mat->table_log_u_rho_T[i_rho * mat->num_T + i_T] <
          mat->table_log_u_rho_T[i_rho * mat->num_T + i_T - 1]) {

        // Replace it and all elements below it with that value
        for (int j_u = 0; j_u < i_T; j_u++) {
          mat->table_log_u_rho_T[i_rho * mat->num_T + j_u] =
              mat->table_log_u_rho_T[i_rho * mat->num_T + i_T];
        }
        break;
      }

      // Smallest positive pressure and sound speed
      if ((mat->table_P_rho_T[i_rho * mat->num_T + i_T] < mat->P_tiny) &&
          (mat->table_P_rho_T[i_rho * mat->num_T + i_T] > 0)) {
        mat->P_tiny = mat->table_P_rho_T[i_rho * mat->num_T + i_T];
      }
      if ((mat->table_c_rho_T[i_rho * mat->num_T + i_T] < mat->c_tiny) &&
          (mat->table_c_rho_T[i_rho * mat->num_T + i_T] > 0)) {
        mat->c_tiny = mat->table_c_rho_T[i_rho * mat->num_T + i_T];
      }
    }
  }

  // Tiny pressure to allow interpolation near non-positive values
  mat->P_tiny *= 1e-3f;
  mat->c_tiny *= 1e-3f;
}

// Convert to internal units
Matthieu Schaller's avatar
Matthieu Schaller committed
228
INLINE static void convert_units_SESAME(struct SESAME_params *mat,
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
                                        const struct unit_system *us) {

  struct unit_system si;
  units_init_si(&si);

  // All table values in SI
  // Densities (log)
  for (int i_rho = 0; i_rho < mat->num_rho; i_rho++) {
    mat->table_log_rho[i_rho] +=
        logf(units_cgs_conversion_factor(&si, UNIT_CONV_DENSITY) /
             units_cgs_conversion_factor(us, UNIT_CONV_DENSITY));
  }

  // Sp. Int. Energies (log), pressures, and sound speeds
  for (int i_rho = 0; i_rho < mat->num_rho; i_rho++) {
    for (int i_T = 0; i_T < mat->num_T; i_T++) {
      mat->table_log_u_rho_T[i_rho * mat->num_T + i_T] += logf(
          units_cgs_conversion_factor(&si, UNIT_CONV_ENERGY_PER_UNIT_MASS) /
          units_cgs_conversion_factor(us, UNIT_CONV_ENERGY_PER_UNIT_MASS));
      mat->table_P_rho_T[i_rho * mat->num_T + i_T] *=
          units_cgs_conversion_factor(&si, UNIT_CONV_PRESSURE) /
          units_cgs_conversion_factor(us, UNIT_CONV_PRESSURE);
      mat->table_c_rho_T[i_rho * mat->num_T + i_T] *=
          units_cgs_conversion_factor(&si, UNIT_CONV_SPEED) /
          units_cgs_conversion_factor(us, UNIT_CONV_SPEED);
      mat->table_s_rho_T[i_rho * mat->num_T + i_T] *=
          units_cgs_conversion_factor(&si, UNIT_CONV_ENERGY_PER_UNIT_MASS) /
          units_cgs_conversion_factor(us, UNIT_CONV_ENTROPY);
    }
  }

  // Tiny pressure and sound speed
  mat->P_tiny *= units_cgs_conversion_factor(&si, UNIT_CONV_PRESSURE) /
                 units_cgs_conversion_factor(us, UNIT_CONV_PRESSURE);
  mat->c_tiny *= units_cgs_conversion_factor(&si, UNIT_CONV_SPEED) /
                 units_cgs_conversion_factor(us, UNIT_CONV_SPEED);
}
266

267
268
// gas_internal_energy_from_entropy
INLINE static float SESAME_internal_energy_from_entropy(
269
    float density, float entropy, const struct SESAME_params *mat) {
270

Matthieu Schaller's avatar
Matthieu Schaller committed
271
  error("This EOS function is not yet implemented!");
272

273
  return 0.f;
274
275
276
}

// gas_pressure_from_entropy
277
278
INLINE static float SESAME_pressure_from_entropy(
    float density, float entropy, const struct SESAME_params *mat) {
279

Matthieu Schaller's avatar
Matthieu Schaller committed
280
  error("This EOS function is not yet implemented!");
281

282
  return 0.f;
283
284
285
}

// gas_entropy_from_pressure
286
287
INLINE static float SESAME_entropy_from_pressure(
    float density, float pressure, const struct SESAME_params *mat) {
288

Matthieu Schaller's avatar
Matthieu Schaller committed
289
  error("This EOS function is not yet implemented!");
290

291
  return 0.f;
292
293
294
}

// gas_soundspeed_from_entropy
295
296
INLINE static float SESAME_soundspeed_from_entropy(
    float density, float entropy, const struct SESAME_params *mat) {
297

Matthieu Schaller's avatar
Matthieu Schaller committed
298
  error("This EOS function is not yet implemented!");
299

300
  return 0.f;
301
302
303
304
}

// gas_entropy_from_internal_energy
INLINE static float SESAME_entropy_from_internal_energy(
305
    float density, float u, const struct SESAME_params *mat) {
306

307
  return 0.f;
308
309
}

310
// gas_pressure_from_internal_energy
311
INLINE static float SESAME_pressure_from_internal_energy(
312
    float density, float u, const struct SESAME_params *mat) {
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
  float P, P_1, P_2, P_3, P_4;

  if (u <= 0.f) {
    return 0.f;
  }

  int idx_rho, idx_u_1, idx_u_2;
  float intp_rho, intp_u_1, intp_u_2;
  const float log_rho = logf(density);
  const float log_u = logf(u);

  // 2D interpolation (bilinear with log(rho), log(u)) to find P(rho, u)
  // Density index
  idx_rho =
      find_value_in_monot_incr_array(log_rho, mat->table_log_rho, mat->num_rho);

  // Sp. int. energy at this and the next density (in relevant slice of u array)
  idx_u_1 = find_value_in_monot_incr_array(
      log_u, mat->table_log_u_rho_T + idx_rho * mat->num_T, mat->num_T);
  idx_u_2 = find_value_in_monot_incr_array(
      log_u, mat->table_log_u_rho_T + (idx_rho + 1) * mat->num_T, mat->num_T);

  // If outside the table then extrapolate from the edge and edge-but-one values
  if (idx_rho <= -1) {
    idx_rho = 0;
  } else if (idx_rho >= mat->num_rho) {
    idx_rho = mat->num_rho - 2;
  }
  if (idx_u_1 <= -1) {
    idx_u_1 = 0;
  } else if (idx_u_1 >= mat->num_T) {
    idx_u_1 = mat->num_T - 2;
  }
  if (idx_u_2 <= -1) {
    idx_u_2 = 0;
  } else if (idx_u_2 >= mat->num_T) {
    idx_u_2 = mat->num_T - 2;
  }

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
  // Check for duplicates in SESAME tables before interpolation
  if (mat->table_log_rho[idx_rho + 1] != mat->table_log_rho[idx_rho]) {
    intp_rho = (log_rho - mat->table_log_rho[idx_rho]) /
               (mat->table_log_rho[idx_rho + 1] - mat->table_log_rho[idx_rho]);
  } else {
    intp_rho = 1.;
  }

  // Check for duplicates in SESAME tables before interpolation
  if (mat->table_log_u_rho_T[idx_rho * mat->num_T + (idx_u_1 + 1)] !=
      mat->table_log_u_rho_T[idx_rho * mat->num_T + idx_u_1]) {
    intp_u_1 =
        (log_u - mat->table_log_u_rho_T[idx_rho * mat->num_T + idx_u_1]) /
        (mat->table_log_u_rho_T[idx_rho * mat->num_T + (idx_u_1 + 1)] -
         mat->table_log_u_rho_T[idx_rho * mat->num_T + idx_u_1]);
  } else {
    intp_u_1 = 1.;
  }

  // Check for duplicates in SESAME tables before interpolation
  if (mat->table_log_u_rho_T[(idx_rho + 1) * mat->num_T + (idx_u_2 + 1)] !=
      mat->table_log_u_rho_T[(idx_rho + 1) * mat->num_T + idx_u_2]) {
    intp_u_2 =
        (log_u - mat->table_log_u_rho_T[(idx_rho + 1) * mat->num_T + idx_u_2]) /
        (mat->table_log_u_rho_T[(idx_rho + 1) * mat->num_T + (idx_u_2 + 1)] -
         mat->table_log_u_rho_T[(idx_rho + 1) * mat->num_T + idx_u_2]);
  } else {
    intp_u_2 = 1.;
  }
382
383
384
385
386
387

  // Table values
  P_1 = mat->table_P_rho_T[idx_rho * mat->num_T + idx_u_1];
  P_2 = mat->table_P_rho_T[idx_rho * mat->num_T + idx_u_1 + 1];
  P_3 = mat->table_P_rho_T[(idx_rho + 1) * mat->num_T + idx_u_2];
  P_4 = mat->table_P_rho_T[(idx_rho + 1) * mat->num_T + idx_u_2 + 1];
388
389
390
391
392
393
394
395
  
  // If below the minimum u at this rho then just use the lowest table values
  if ((idx_rho > 0.f) && (
      (intp_u_1 < 0.f) || (intp_u_2 < 0.f) || (P_1 > P_2) || (P_3 > P_4)
  )) {
      intp_u_1 = 0;
      intp_u_2 = 0;
  }
396
397
398
399
400
401
402

  // If more than two table values are non-positive then return zero
  int num_non_pos = 0;
  if (P_1 <= 0.f) num_non_pos++;
  if (P_2 <= 0.f) num_non_pos++;
  if (P_3 <= 0.f) num_non_pos++;
  if (P_4 <= 0.f) num_non_pos++;
403
404
  if (num_non_pos > 0) {
    // If just one or two are non-positive then replace them with a tiny value
405
    // Unless already trying to extrapolate in which case return zero
406
407
    if ((num_non_pos > 2) || (mat->P_tiny == 0.f) || (intp_rho < 0.f) || 
        (intp_u_1 < 0.f) || (intp_u_2 < 0.f)) {
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
      return 0.f;
    }
    if (P_1 <= 0.f) P_1 = mat->P_tiny;
    if (P_2 <= 0.f) P_2 = mat->P_tiny;
    if (P_3 <= 0.f) P_3 = mat->P_tiny;
    if (P_4 <= 0.f) P_4 = mat->P_tiny;
  }

  // Interpolate with the log values
  P_1 = logf(P_1);
  P_2 = logf(P_2);
  P_3 = logf(P_3);
  P_4 = logf(P_4);

  P = (1.f - intp_rho) * ((1.f - intp_u_1) * P_1 + intp_u_1 * P_2) +
      intp_rho * ((1.f - intp_u_2) * P_3 + intp_u_2 * P_4);

  // Convert back from log
  P = expf(P);

  return P;
429
430
431
432
}

// gas_internal_energy_from_pressure
INLINE static float SESAME_internal_energy_from_pressure(
433
    float density, float P, const struct SESAME_params *mat) {
434

Matthieu Schaller's avatar
Matthieu Schaller committed
435
  error("This EOS function is not yet implemented!");
436

437
  return 0.f;
438
439
440
}

// gas_soundspeed_from_internal_energy
441
INLINE static float SESAME_soundspeed_from_internal_energy(
442
    float density, float u, const struct SESAME_params *mat) {
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
  float c, c_1, c_2, c_3, c_4;

  if (u <= 0.f) {
    return 0.f;
  }

  int idx_rho, idx_u_1, idx_u_2;
  float intp_rho, intp_u_1, intp_u_2;
  const float log_rho = logf(density);
  const float log_u = logf(u);

  // 2D interpolation (bilinear with log(rho), log(u)) to find c(rho, u)
  // Density index
  idx_rho =
      find_value_in_monot_incr_array(log_rho, mat->table_log_rho, mat->num_rho);

  // Sp. int. energy at this and the next density (in relevant slice of u array)
  idx_u_1 = find_value_in_monot_incr_array(
      log_u, mat->table_log_u_rho_T + idx_rho * mat->num_T, mat->num_T);
  idx_u_2 = find_value_in_monot_incr_array(
      log_u, mat->table_log_u_rho_T + (idx_rho + 1) * mat->num_T, mat->num_T);

  // If outside the table then extrapolate from the edge and edge-but-one values
  if (idx_rho <= -1) {
    idx_rho = 0;
  } else if (idx_rho >= mat->num_rho) {
    idx_rho = mat->num_rho - 2;
  }
  if (idx_u_1 <= -1) {
    idx_u_1 = 0;
  } else if (idx_u_1 >= mat->num_T) {
    idx_u_1 = mat->num_T - 2;
  }
  if (idx_u_2 <= -1) {
    idx_u_2 = 0;
  } else if (idx_u_2 >= mat->num_T) {
    idx_u_2 = mat->num_T - 2;
  }

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
  // Check for duplicates in SESAME tables before interpolation
  if (mat->table_log_rho[idx_rho + 1] != mat->table_log_rho[idx_rho]) {
    intp_rho = (log_rho - mat->table_log_rho[idx_rho]) /
               (mat->table_log_rho[idx_rho + 1] - mat->table_log_rho[idx_rho]);
  } else {
    intp_rho = 1.;
  }

  // Check for duplicates in SESAME tables before interpolation
  if (mat->table_log_u_rho_T[idx_rho * mat->num_T + (idx_u_1 + 1)] !=
      mat->table_log_u_rho_T[idx_rho * mat->num_T + idx_u_1]) {
    intp_u_1 =
        (log_u - mat->table_log_u_rho_T[idx_rho * mat->num_T + idx_u_1]) /
        (mat->table_log_u_rho_T[idx_rho * mat->num_T + (idx_u_1 + 1)] -
         mat->table_log_u_rho_T[idx_rho * mat->num_T + idx_u_1]);
  } else {
    intp_u_1 = 1.;
  }

  // Check for duplicates in SESAME tables before interpolation
  if (mat->table_log_u_rho_T[(idx_rho + 1) * mat->num_T + (idx_u_2 + 1)] !=
      mat->table_log_u_rho_T[(idx_rho + 1) * mat->num_T + idx_u_2]) {
    intp_u_2 =
        (log_u - mat->table_log_u_rho_T[(idx_rho + 1) * mat->num_T + idx_u_2]) /
        (mat->table_log_u_rho_T[(idx_rho + 1) * mat->num_T + (idx_u_2 + 1)] -
         mat->table_log_u_rho_T[(idx_rho + 1) * mat->num_T + idx_u_2]);
  } else {
    intp_u_2 = 1.;
  }
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

  // Table values
  c_1 = mat->table_c_rho_T[idx_rho * mat->num_T + idx_u_1];
  c_2 = mat->table_c_rho_T[idx_rho * mat->num_T + idx_u_1 + 1];
  c_3 = mat->table_c_rho_T[(idx_rho + 1) * mat->num_T + idx_u_2];
  c_4 = mat->table_c_rho_T[(idx_rho + 1) * mat->num_T + idx_u_2 + 1];

  // If more than two table values are non-positive then return zero
  int num_non_pos = 0;
  if (c_1 <= 0.f) num_non_pos++;
  if (c_2 <= 0.f) num_non_pos++;
  if (c_3 <= 0.f) num_non_pos++;
  if (c_4 <= 0.f) num_non_pos++;
  if (num_non_pos > 2) {
    return mat->c_tiny;
  }
  // If just one or two are non-positive then replace them with a tiny value
  else if (num_non_pos > 0) {
    // Unless already trying to extrapolate in which case return zero
    if ((intp_rho < 0.f) || (intp_u_1 < 0.f) || (intp_u_2 < 0.f)) {
      return mat->c_tiny;
    }
    if (c_1 <= 0.f) c_1 = mat->c_tiny;
    if (c_2 <= 0.f) c_2 = mat->c_tiny;
    if (c_3 <= 0.f) c_3 = mat->c_tiny;
    if (c_4 <= 0.f) c_4 = mat->c_tiny;
  }

  // Interpolate with the log values
  c_1 = logf(c_1);
  c_2 = logf(c_2);
  c_3 = logf(c_3);
  c_4 = logf(c_4);

  c = (1.f - intp_rho) * ((1.f - intp_u_1) * c_1 + intp_u_1 * c_2) +
      intp_rho * ((1.f - intp_u_2) * c_3 + intp_u_2 * c_4);

  // Convert back from log
  c = expf(c);

  return c;
553
554
555
}

// gas_soundspeed_from_pressure
556
557
INLINE static float SESAME_soundspeed_from_pressure(
    float density, float P, const struct SESAME_params *mat) {
558

Matthieu Schaller's avatar
Matthieu Schaller committed
559
  error("This EOS function is not yet implemented!");
560

561
  return 0.f;
562
563
564
}

#endif /* SWIFT_SESAME_EQUATION_OF_STATE_H */