hydro_properties.c 16.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*******************************************************************************
 * This file is part of SWIFT.
 * Copyright (c) 2016 Matthieu Schaller (matthieu.schaller@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/>.
 *
 ******************************************************************************/

/* This object's header. */
#include "hydro_properties.h"

/* Standard headers */
24
#include <float.h>
25
26
27
#include <math.h>

/* Local headers. */
28
#include "adiabatic_index.h"
29
#include "common_io.h"
30
#include "dimension.h"
31
#include "equation_of_state.h"
32
#include "error.h"
33
#include "gravity_properties.h"
34
#include "hydro.h"
35
#include "kernel_hydro.h"
36
37
#include "parser.h"
#include "units.h"
38

39
#define hydro_props_default_max_iterations 30
40
#define hydro_props_default_volume_change 1.4f
41
#define hydro_props_default_h_max FLT_MAX
42
#define hydro_props_default_h_min_ratio 0.f
43
#define hydro_props_default_h_tolerance 1e-4
44
#define hydro_props_default_init_temp 0.f
45
#define hydro_props_default_min_temp 0.f
46
#define hydro_props_default_H_ionization_temperature 1e4
Josh Borrow's avatar
Josh Borrow committed
47
#define hydro_props_default_viscosity_alpha 0.8f
Josh Borrow's avatar
Josh Borrow committed
48
49
50
51

#ifdef ANARCHY_PU_SPH
/* This nasty #ifdef is only temporary until we separate the viscosity
 * and hydro components. If it is not removed by July 2019, shout at JB. */
Josh Borrow's avatar
Josh Borrow committed
52
#define hydro_props_default_viscosity_alpha_min \
Josh Borrow's avatar
Josh Borrow committed
53
54
55
56
57
58
59
60
  0.01f /* values taken from Schaller+ 2015 */
#define hydro_props_default_viscosity_alpha_max \
  2.0f /* values taken from Schaller+ 2015 */
#define hydro_props_default_viscosity_length \
  0.01f /* values taken from Schaller+ 2015 */
#else
#define hydro_props_default_viscosity_alpha_min \
  0.1f /* values taken from (price,2004), not used in legacy gadget mode */
Josh Borrow's avatar
Josh Borrow committed
61
#define hydro_props_default_viscosity_alpha_max \
Josh Borrow's avatar
Josh Borrow committed
62
  2.0f /* values taken from (price,2004), not used in legacy gadget mode */
Josh Borrow's avatar
Josh Borrow committed
63
64
#define hydro_props_default_viscosity_length \
  0.1f /* Values taken from (Price,2004), not used in legacy gadget mode */
Josh Borrow's avatar
Josh Borrow committed
65
66
67
68
69
70
71
#endif /* ANARCHY_PU_SPH */

/* Following values taken directly from the ANARCHY paper (Schaller+ 2015) */
#define hydro_props_default_diffusion_alpha 0.0f
#define hydro_props_default_diffusion_beta 0.01f
#define hydro_props_default_diffusion_alpha_max 1.0f
#define hydro_props_default_diffusion_alpha_min 0.0f
72

73
74
75
76
77
78
79
80
/**
 * @brief Initialize the global properties of the hydro scheme.
 *
 * @param p The #hydro_props.
 * @param phys_const The physical constants in the internal unit system.
 * @param us The internal unit system.
 * @param params The parsed parameters.
 */
81
void hydro_props_init(struct hydro_props *p,
82
83
                      const struct phys_const *phys_const,
                      const struct unit_system *us,
lhausamm's avatar
lhausamm committed
84
                      struct swift_params *params) {
85
86
87

  /* Kernel properties */
  p->eta_neighbours = parser_get_param_float(params, "SPH:resolution_eta");
88
89
90
91
92
93

  /* Tolerance for the smoothing length Newton-Raphson scheme */
  p->h_tolerance = parser_get_opt_param_float(params, "SPH:h_tolerance",
                                              hydro_props_default_h_tolerance);

  /* Get derived properties */
94
  p->target_neighbours = pow_dimension(p->eta_neighbours) * kernel_norm;
95
96
97
98
  const float delta_eta = p->eta_neighbours * (1.f + p->h_tolerance);
  p->delta_neighbours =
      (pow_dimension(delta_eta) - pow_dimension(p->eta_neighbours)) *
      kernel_norm;
99

100
101
102
103
#ifdef SHADOWFAX_SPH
  /* change the meaning of target_neighbours and delta_neighbours */
  p->target_neighbours = 1.0f;
  p->delta_neighbours = 0.0f;
104
  p->eta_neighbours = 1.0f;
105
106
#endif

107
108
109
110
  /* Maximal smoothing length */
  p->h_max = parser_get_opt_param_float(params, "SPH:h_max",
                                        hydro_props_default_h_max);

111
112
113
114
115
116
117
  /* Minimal smoothing length ratio to softening */
  p->h_min_ratio = parser_get_opt_param_float(params, "SPH:h_min_ratio",
                                              hydro_props_default_h_min_ratio);

  /* Temporarily set the minimal softening to 0. */
  p->h_min = 0.f;

118
  /* Number of iterations to converge h */
119
120
  p->max_smoothing_iterations = parser_get_opt_param_int(
      params, "SPH:max_ghost_iterations", hydro_props_default_max_iterations);
121

122
123
124
  if (p->max_smoothing_iterations <= 10)
    error("The number of smoothing length iterations should be > 10");

125
126
  /* Time integration properties */
  p->CFL_condition = parser_get_param_float(params, "SPH:CFL_condition");
127
128
  const float max_volume_change = parser_get_opt_param_float(
      params, "SPH:max_volume_change", hydro_props_default_volume_change);
129
  p->log_max_h_change = logf(powf(max_volume_change, hydro_dimension_inv));
130
131
132
133
134

  /* Initial temperature */
  p->initial_temperature = parser_get_opt_param_float(
      params, "SPH:initial_temperature", hydro_props_default_init_temp);

135
  /* Minimal temperature */
136
137
138
  p->minimal_temperature = parser_get_opt_param_float(
      params, "SPH:minimal_temperature", hydro_props_default_min_temp);

139
140
  if ((p->initial_temperature != 0.) &&
      (p->initial_temperature < p->minimal_temperature))
141
142
    error("Initial temperature lower than minimal allowed temperature!");

143
144
145
146
147
  /* Neutral to ionized Hydrogen transition temperature */
  p->hydrogen_ionization_temperature =
      parser_get_opt_param_double(params, "SPH:H_ionization_temperature",
                                  hydro_props_default_H_ionization_temperature);

148
  /* Hydrogen mass fraction */
149
150
  const float default_H_fraction =
      1. - phys_const->const_primordial_He_fraction;
151
  p->hydrogen_mass_fraction = parser_get_opt_param_double(
152
      params, "SPH:H_mass_fraction", default_H_fraction);
153

154
155
156
157
158
159
  /* Mean molecular mass for neutral gas */
  p->mu_neutral = 4. / (1. + 3. * p->hydrogen_mass_fraction);

  /* Mean molecular mass for fully ionised gas */
  p->mu_ionised = 4. / (8. - 5. * (1. - p->hydrogen_mass_fraction));

Josh Borrow's avatar
Josh Borrow committed
160
161
  /* Read the artificial viscosity parameters from the file, if they exist */
  p->viscosity.alpha = parser_get_opt_param_float(
Matthieu Schaller's avatar
Matthieu Schaller committed
162
      params, "SPH:viscosity_alpha", hydro_props_default_viscosity_alpha);
Josh Borrow's avatar
Josh Borrow committed
163

Matthieu Schaller's avatar
Matthieu Schaller committed
164
165
166
167
168
169
170
  p->viscosity.alpha_max =
      parser_get_opt_param_float(params, "SPH:viscosity_alpha_max",
                                 hydro_props_default_viscosity_alpha_max);

  p->viscosity.alpha_min =
      parser_get_opt_param_float(params, "SPH:viscosity_alpha_min",
                                 hydro_props_default_viscosity_alpha_min);
Josh Borrow's avatar
Josh Borrow committed
171
172

  p->viscosity.length = parser_get_opt_param_float(
Matthieu Schaller's avatar
Matthieu Schaller committed
173
      params, "SPH:viscosity_length", hydro_props_default_viscosity_length);
Josh Borrow's avatar
Josh Borrow committed
174

Josh Borrow's avatar
Josh Borrow committed
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
  /* Same for the thermal diffusion parameters */
  p->diffusion.alpha = parser_get_opt_param_float(
      params, "SPH:diffusion_alpha", hydro_props_default_diffusion_alpha);

  p->diffusion.beta = parser_get_opt_param_float(
      params, "SPH:diffusion_beta", hydro_props_default_diffusion_beta);

  p->diffusion.alpha_max =
      parser_get_opt_param_float(params, "SPH:diffusion_alpha_max",
                                 hydro_props_default_diffusion_alpha_max);

  p->diffusion.alpha_min =
      parser_get_opt_param_float(params, "SPH:diffusion_alpha_min",
                                 hydro_props_default_diffusion_alpha_min);

190
  /* Compute the initial energy (Note the temp. read is in internal units) */
191
  /* u_init = k_B T_init / (mu m_p (gamma - 1)) */
192
193
194
195
  double u_init = phys_const->const_boltzmann_k / phys_const->const_proton_mass;
  u_init *= p->initial_temperature;
  u_init *= hydro_one_over_gamma_minus_one;

196
  /* Correct for hydrogen mass fraction (mu) */
197
  double mean_molecular_weight;
198
  if (p->initial_temperature > p->hydrogen_ionization_temperature)
199
200
201
202
203
    mean_molecular_weight = 4. / (8. - 5. * (1. - p->hydrogen_mass_fraction));
  else
    mean_molecular_weight = 4. / (1. + 3. * p->hydrogen_mass_fraction);

  p->initial_internal_energy = u_init / mean_molecular_weight;
204
205

  /* Compute the minimal energy (Note the temp. read is in internal units) */
206
  /* u_min = k_B T_min / (mu m_p (gamma - 1)) */
207
  double u_min = phys_const->const_boltzmann_k / phys_const->const_proton_mass;
208
  u_min *= p->minimal_temperature;
209
210
  u_min *= hydro_one_over_gamma_minus_one;

211
  /* Correct for hydrogen mass fraction (mu) */
212
  if (p->minimal_temperature > p->hydrogen_ionization_temperature)
213
214
215
216
217
    mean_molecular_weight = 4. / (8. - 5. * (1. - p->hydrogen_mass_fraction));
  else
    mean_molecular_weight = 4. / (1. + 3. * p->hydrogen_mass_fraction);

  p->minimal_internal_energy = u_min / mean_molecular_weight;
218
219
}

220
221
222
223
224
/**
 * @brief Print the global properties of the hydro scheme.
 *
 * @param p The #hydro_props.
 */
225
226
void hydro_props_print(const struct hydro_props *p) {

227
228
  /* Print equation of state first */
  eos_print(&eos);
229

230
  /* Now SPH */
231
232
233
  message("Hydrodynamic scheme: %s in %dD.", SPH_IMPLEMENTATION,
          (int)hydro_dimension);

234
235
236
  message("Hydrodynamic kernel: %s with eta=%f (%.2f neighbours).", kernel_name,
          p->eta_neighbours, p->target_neighbours);

237
  message("Hydrodynamic relative tolerance in h: %.5f (+/- %.4f neighbours).",
238
          p->h_tolerance, p->delta_neighbours);
239

Peter W. Draper's avatar
Peter W. Draper committed
240
  message("Hydrodynamic integration: CFL parameter: %.4f.", p->CFL_condition);
241

Matthieu Schaller's avatar
Matthieu Schaller committed
242
243
244
245
246
  message(
      "Artificial viscosity parameters set to alpha: %.3f, max: %.3f, "
      "min: %.3f, length: %.3f.",
      p->viscosity.alpha, p->viscosity.alpha_max, p->viscosity.alpha_min,
      p->viscosity.length);
Josh Borrow's avatar
Josh Borrow committed
247

248
  message(
Matthieu Schaller's avatar
Matthieu Schaller committed
249
250
      "Hydrodynamic integration: Max change of volume: %.2f "
      "(max|dlog(h)/dt|=%f).",
251
      pow_dimension(expf(p->log_max_h_change)), p->log_max_h_change);
252

253
254
255
  if (p->h_max != hydro_props_default_h_max)
    message("Maximal smoothing length allowed: %.4f", p->h_max);

256
257
258
  if (p->max_smoothing_iterations != hydro_props_default_max_iterations)
    message("Maximal iterations in ghost task set to %d (default is %d)",
            p->max_smoothing_iterations, hydro_props_default_max_iterations);
259
260
261

  if (p->initial_temperature != hydro_props_default_init_temp)
    message("Initial gas temperature set to %f", p->initial_temperature);
262
263

  if (p->minimal_temperature != hydro_props_default_min_temp)
264
    message("Minimal gas temperature set to %f", p->minimal_temperature);
265

Peter W. Draper's avatar
Peter W. Draper committed
266
    // Matthieu: Temporary location for this i/o business.
267

268
#ifdef PLANETARY_SPH
269
270
#ifdef PLANETARY_SPH_NO_BALSARA
  message("Planetary SPH: Balsara switch DISABLED");
271
#else
272
273
274
  message("Planetary SPH: Balsara switch ENABLED");
#endif
#endif
275
}
276
277
278
279

#if defined(HAVE_HDF5)
void hydro_props_print_snapshot(hid_t h_grpsph, const struct hydro_props *p) {

280
281
  eos_print_snapshot(h_grpsph, &eos);

282
283
284
285
286
287
  io_write_attribute_i(h_grpsph, "Dimension", (int)hydro_dimension);
  io_write_attribute_s(h_grpsph, "Scheme", SPH_IMPLEMENTATION);
  io_write_attribute_s(h_grpsph, "Kernel function", kernel_name);
  io_write_attribute_f(h_grpsph, "Kernel target N_ngb", p->target_neighbours);
  io_write_attribute_f(h_grpsph, "Kernel delta N_ngb", p->delta_neighbours);
  io_write_attribute_f(h_grpsph, "Kernel eta", p->eta_neighbours);
288
  io_write_attribute_f(h_grpsph, "Smoothing length tolerance", p->h_tolerance);
289
290
  io_write_attribute_f(h_grpsph, "Maximal smoothing length [internal units]",
                       p->h_max);
291
292
293
294
295
296
297
  io_write_attribute_f(h_grpsph, "CFL parameter", p->CFL_condition);
  io_write_attribute_f(h_grpsph, "Volume log(max(delta h))",
                       p->log_max_h_change);
  io_write_attribute_f(h_grpsph, "Volume max change time-step",
                       pow_dimension(expf(p->log_max_h_change)));
  io_write_attribute_i(h_grpsph, "Max ghost iterations",
                       p->max_smoothing_iterations);
298
  io_write_attribute_f(h_grpsph, "Minimal temperature", p->minimal_temperature);
299
300
301
  io_write_attribute_f(h_grpsph,
                       "Minimal energy per unit mass [internal units]",
                       p->minimal_internal_energy);
302
  io_write_attribute_f(h_grpsph, "Initial temperature", p->initial_temperature);
303
304
  io_write_attribute_f(h_grpsph,
                       "Initial energy per unit mass [internal units]",
305
306
307
                       p->initial_internal_energy);
  io_write_attribute_f(h_grpsph, "Hydrogen mass fraction",
                       p->hydrogen_mass_fraction);
308
  io_write_attribute_f(h_grpsph, "Hydrogen ionization transition temperature",
309
                       p->hydrogen_ionization_temperature);
Josh Borrow's avatar
Josh Borrow committed
310
311
312
313
314
  io_write_attribute_f(h_grpsph, "Alpha viscosity", p->viscosity.alpha);
  io_write_attribute_f(h_grpsph, "Alpha viscosity (max)",
                       p->viscosity.alpha_max);
  io_write_attribute_f(h_grpsph, "Alpha viscosity (min)",
                       p->viscosity.alpha_min);
315
316
  io_write_attribute_f(h_grpsph, "Viscosity decay length [internal units]",
                       p->viscosity.length);
Josh Borrow's avatar
Josh Borrow committed
317
  io_write_attribute_f(h_grpsph, "Beta viscosity", const_viscosity_beta);
318
319
  io_write_attribute_f(h_grpsph, "Max v_sig ratio (limiter)",
                       const_limiter_max_v_sig_ratio);
320
321
}
#endif
322

Josh Borrow's avatar
Josh Borrow committed
323
324
325
326
/**
 * @brief Initialises a hydro_props struct with somewhat useful values for
 *        the automated test suite. This is not intended for production use,
 *        but rather to fill for the purposes of mocking.
Matthieu Schaller's avatar
Matthieu Schaller committed
327
 *
Josh Borrow's avatar
Josh Borrow committed
328
329
330
 * @param p the struct
 */
void hydro_props_init_no_hydro(struct hydro_props *p) {
331

Josh Borrow's avatar
Josh Borrow committed
332
333
334
335
336
337
338
339
  p->eta_neighbours = 1.2348;
  p->h_tolerance = hydro_props_default_h_tolerance;
  p->target_neighbours = pow_dimension(p->eta_neighbours) * kernel_norm;
  const float delta_eta = p->eta_neighbours * (1.f + p->h_tolerance);
  p->delta_neighbours =
      (pow_dimension(delta_eta) - pow_dimension(p->eta_neighbours)) *
      kernel_norm;
  p->h_max = hydro_props_default_h_max;
340
341
  p->h_min = 0.f;
  p->h_min_ratio = hydro_props_default_h_min_ratio;
Josh Borrow's avatar
Josh Borrow committed
342
343
344
  p->max_smoothing_iterations = hydro_props_default_max_iterations;
  p->CFL_condition = 0.1;
  p->log_max_h_change = logf(powf(1.4, hydro_dimension_inv));
Matthieu Schaller's avatar
Matthieu Schaller committed
345

Josh Borrow's avatar
Josh Borrow committed
346
347
348
349
350
351
352
353
354
  /* These values are inconsistent and in a production run would probably lead
     to a crash. Again, this function is intended for mocking use in unit tests
     and is _not_ to be used otherwise! */
  p->minimal_temperature = hydro_props_default_min_temp;
  p->minimal_internal_energy = 0.f;
  p->initial_temperature = hydro_props_default_init_temp;
  p->initial_internal_energy = 0.f;

  p->hydrogen_mass_fraction = 0.755;
Matthieu Schaller's avatar
Matthieu Schaller committed
355
356
  p->hydrogen_ionization_temperature =
      hydro_props_default_H_ionization_temperature;
Josh Borrow's avatar
Josh Borrow committed
357
358

  p->viscosity.alpha = hydro_props_default_viscosity_alpha;
Matthieu Schaller's avatar
Matthieu Schaller committed
359
360
361
  p->viscosity.alpha_max = hydro_props_default_viscosity_alpha_max;
  p->viscosity.alpha_min = hydro_props_default_viscosity_alpha_min;
  p->viscosity.length = hydro_props_default_viscosity_length;
Josh Borrow's avatar
Josh Borrow committed
362
363
364
365
366

  p->diffusion.alpha = hydro_props_default_diffusion_alpha;
  p->diffusion.beta = hydro_props_default_diffusion_beta;
  p->diffusion.alpha_max = hydro_props_default_diffusion_alpha_max;
  p->diffusion.alpha_min = hydro_props_default_diffusion_alpha_min;
Josh Borrow's avatar
Josh Borrow committed
367
368
}

369
370
371
372
373
374
375
376
377
378
379
380
381
382
/**
 * @brief Update the global properties of the hydro scheme for that time-step.
 *
 * @param p The properties to update.
 * @param gp The properties of the gravity scheme.
 * @param cosmo The cosmological model.
 */
void hydro_props_update(struct hydro_props *p, const struct gravity_props *gp,
                        const struct cosmology *cosmo) {

  /* Update the minimal allowed smoothing length */
  p->h_min = p->h_min_ratio * gp->epsilon_cur;
}

383
384
385
386
387
388
389
/**
 * @brief Write a hydro_props struct to the given FILE as a stream of bytes.
 *
 * @param p the struct
 * @param stream the file stream
 */
void hydro_props_struct_dump(const struct hydro_props *p, FILE *stream) {
Peter W. Draper's avatar
Peter W. Draper committed
390
  restart_write_blocks((void *)p, sizeof(struct hydro_props), 1, stream,
391
                       "hydroprops", "hydro props");
392
393
394
395
396
397
398
399
400
401
}

/**
 * @brief Restore a hydro_props struct from the given FILE as a stream of
 * bytes.
 *
 * @param p the struct
 * @param stream the file stream
 */
void hydro_props_struct_restore(const struct hydro_props *p, FILE *stream) {
Peter W. Draper's avatar
Peter W. Draper committed
402
403
  restart_read_blocks((void *)p, sizeof(struct hydro_props), 1, stream, NULL,
                      "hydro props");
404
}