hydro_properties.c 5.72 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

40
41
42
43
44
void hydro_props_init(struct hydro_props *p,
                      const struct swift_params *params) {

  /* Kernel properties */
  p->eta_neighbours = parser_get_param_float(params, "SPH:resolution_eta");
45
46
47
48
49
50

  /* 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 */
51
  p->target_neighbours = pow_dimension(p->eta_neighbours) * kernel_norm;
52
53
54
55
  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;
56

57
58
59
60
61
62
#ifdef SHADOWFAX_SPH
  /* change the meaning of target_neighbours and delta_neighbours */
  p->target_neighbours = 1.0f;
  p->delta_neighbours = 0.0f;
#endif

63
64
65
66
67
  /* 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 */
68
69
  p->max_smoothing_iterations = parser_get_opt_param_int(
      params, "SPH:max_ghost_iterations", hydro_props_default_max_iterations);
70
71
72

  /* Time integration properties */
  p->CFL_condition = parser_get_param_float(params, "SPH:CFL_condition");
73
74
  const float max_volume_change = parser_get_opt_param_float(
      params, "SPH:max_volume_change", hydro_props_default_volume_change);
75
  p->log_max_h_change = logf(powf(max_volume_change, hydro_dimension_inv));
76
77
78
79
}

void hydro_props_print(const struct hydro_props *p) {

80
81
  /* Print equation of state first */
  eos_print(&eos);
82

83
  /* Now SPH */
84
85
86
  message("Hydrodynamic scheme: %s in %dD.", SPH_IMPLEMENTATION,
          (int)hydro_dimension);

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

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

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

95
  message(
Matthieu Schaller's avatar
Matthieu Schaller committed
96
97
      "Hydrodynamic integration: Max change of volume: %.2f "
      "(max|dlog(h)/dt|=%f).",
98
      pow_dimension(expf(p->log_max_h_change)), p->log_max_h_change);
99

100
101
102
  if (p->h_max != hydro_props_default_h_max)
    message("Maximal smoothing length allowed: %.4f", p->h_max);

103
104
105
  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);
106
}
107
108
109
110

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

111
112
  eos_print_snapshot(h_grpsph, &eos);

113
114
115
116
117
118
  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);
119
  io_write_attribute_f(h_grpsph, "Smoothing length tolerance", p->h_tolerance);
120
  io_write_attribute_f(h_grpsph, "Maximal smoothing length", p->h_max);
121
122
123
124
125
126
127
  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);
128
129
}
#endif
130
131
132
133
134
135
136
137

/**
 * @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
138
  restart_write_blocks((void *)p, sizeof(struct hydro_props), 1, stream,
139
                       "hydroprops", "hydro props");
140
141
142
143
144
145
146
147
148
149
}

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