hydro_properties.c 8.42 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
32
#include "error.h"
#include "hydro.h"
33
#include "kernel_hydro.h"
34

35
#define hydro_props_default_max_iterations 30
36
#define hydro_props_default_volume_change 1.4f
37
#define hydro_props_default_h_max FLT_MAX
38
#define hydro_props_default_h_tolerance 1e-4
39
#define hydro_props_default_init_temp 0.f
40
#define hydro_props_default_min_temp 0.f
41
#define hydro_props_default_H_fraction 0.76
42

43
void hydro_props_init(struct hydro_props *p,
44
45
                      const struct phys_const *phys_const,
                      const struct unit_system *us,
46
47
48
49
                      const struct swift_params *params) {

  /* Kernel properties */
  p->eta_neighbours = parser_get_param_float(params, "SPH:resolution_eta");
50
51
52
53
54
55

  /* 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 */
56
  p->target_neighbours = pow_dimension(p->eta_neighbours) * kernel_norm;
57
58
59
60
  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;
61

62
63
64
65
66
67
#ifdef SHADOWFAX_SPH
  /* change the meaning of target_neighbours and delta_neighbours */
  p->target_neighbours = 1.0f;
  p->delta_neighbours = 0.0f;
#endif

68
69
70
71
72
  /* Maximal smoothing length */
  p->h_max = parser_get_opt_param_float(params, "SPH:h_max",
                                        hydro_props_default_h_max);

  /* Number of iterations to converge h */
73
74
  p->max_smoothing_iterations = parser_get_opt_param_int(
      params, "SPH:max_ghost_iterations", hydro_props_default_max_iterations);
75
76
77

  /* Time integration properties */
  p->CFL_condition = parser_get_param_float(params, "SPH:CFL_condition");
78
79
  const float max_volume_change = parser_get_opt_param_float(
      params, "SPH:max_volume_change", hydro_props_default_volume_change);
80
  p->log_max_h_change = logf(powf(max_volume_change, hydro_dimension_inv));
81
82
83
84
85

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

86
87
88
89
90
91
92
  /* Initial temperature */
  p->minimal_temperature = parser_get_opt_param_float(
      params, "SPH:minimal_temperature", hydro_props_default_min_temp);

  if (p->initial_temperature < p->minimal_temperature)
    error("Initial temperature lower than minimal allowed temperature!");

93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  /* Hydrogen mass fraction */
  p->hydrogen_mass_fraction = parser_get_opt_param_double(
      params, "SPH:H_mass_fraction", hydro_props_default_H_fraction);

  /* Compute the initial energy (Note the temp. read is in internal units) */
  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;

  /* Correct for hydrogen mass fraction */
  double mean_molecular_weight;
  if (p->initial_temperature *
          units_cgs_conversion_factor(us, UNIT_CONV_TEMPERATURE) >
      1e4)
    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;
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

  /* Compute the minimal energy (Note the temp. read is in internal units) */
  double u_min = phys_const->const_boltzmann_k / phys_const->const_proton_mass;
  u_min *= p->initial_temperature;
  u_min *= hydro_one_over_gamma_minus_one;

  /* Correct for hydrogen mass fraction */
  if (p->minimal_temperature *
          units_cgs_conversion_factor(us, UNIT_CONV_TEMPERATURE) >
      1e4)
    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;
127
128
129
130
}

void hydro_props_print(const struct hydro_props *p) {

131
132
  /* Print equation of state first */
  eos_print(&eos);
133

134
  /* Now SPH */
135
136
137
  message("Hydrodynamic scheme: %s in %dD.", SPH_IMPLEMENTATION,
          (int)hydro_dimension);

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

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

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

146
  message(
Matthieu Schaller's avatar
Matthieu Schaller committed
147
148
      "Hydrodynamic integration: Max change of volume: %.2f "
      "(max|dlog(h)/dt|=%f).",
149
      pow_dimension(expf(p->log_max_h_change)), p->log_max_h_change);
150

151
152
153
  if (p->h_max != hydro_props_default_h_max)
    message("Maximal smoothing length allowed: %.4f", p->h_max);

154
155
156
  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);
157
158
159

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

  if (p->minimal_temperature != hydro_props_default_min_temp)
    message("Minimal gas temperature set to %f", p->initial_temperature);
163
}
164
165
166
167

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

168
169
  eos_print_snapshot(h_grpsph, &eos);

170
171
172
173
174
175
  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);
176
  io_write_attribute_f(h_grpsph, "Smoothing length tolerance", p->h_tolerance);
177
  io_write_attribute_f(h_grpsph, "Maximal smoothing length", p->h_max);
178
179
180
181
182
183
184
  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);
185
  io_write_attribute_f(h_grpsph, "Minimal temperature", p->minimal_temperature);
186
187
188
189
190
  io_write_attribute_f(h_grpsph, "Initial temperature", p->initial_temperature);
  io_write_attribute_f(h_grpsph, "Initial energy per unit mass",
                       p->initial_internal_energy);
  io_write_attribute_f(h_grpsph, "Hydrogen mass fraction",
                       p->hydrogen_mass_fraction);
191
192
}
#endif
193
194
195
196
197
198
199
200

/**
 * @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
201
  restart_write_blocks((void *)p, sizeof(struct hydro_props), 1, stream,
202
                       "hydroprops", "hydro props");
203
204
205
206
207
208
209
210
211
212
}

/**
 * @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
213
214
  restart_read_blocks((void *)p, sizeof(struct hydro_props), 1, stream, NULL,
                      "hydro props");
215
}