stars_io.h 7.62 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*******************************************************************************
 * This file is part of SWIFT.
 * Coypright (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/>.
 *
 ******************************************************************************/
Loic Hausammann's avatar
Loic Hausammann committed
19
20
#ifndef SWIFT_DEFAULT_STARS_IO_H
#define SWIFT_DEFAULT_STARS_IO_H
21
22

#include "io_properties.h"
23
#include "stars_part.h"
24
25
26
27
28
29
30
31

/**
 * @brief Specifies which s-particle fields to read from a dataset
 *
 * @param sparts The s-particle array.
 * @param list The list of i/o properties to read.
 * @param num_fields The number of i/o fields to read.
 */
32
33
34
INLINE static void stars_read_particles(struct spart *sparts,
                                        struct io_props *list,
                                        int *num_fields) {
35
36

  /* Say how much we want to read */
Loic Hausammann's avatar
Loic Hausammann committed
37
  *num_fields = 5;
38
39
40
41
42
43
44
45
46
47

  /* List what we want to read */
  list[0] = io_make_input_field("Coordinates", DOUBLE, 3, COMPULSORY,
                                UNIT_CONV_LENGTH, sparts, x);
  list[1] = io_make_input_field("Velocities", FLOAT, 3, COMPULSORY,
                                UNIT_CONV_SPEED, sparts, v);
  list[2] = io_make_input_field("Masses", FLOAT, 1, COMPULSORY, UNIT_CONV_MASS,
                                sparts, mass);
  list[3] = io_make_input_field("ParticleIDs", LONGLONG, 1, COMPULSORY,
                                UNIT_CONV_NO_UNITS, sparts, id);
Loic Hausammann's avatar
Loic Hausammann committed
48
  list[4] = io_make_input_field("SmoothingLength", FLOAT, 1, OPTIONAL,
Loic Hausammann's avatar
Loic Hausammann committed
49
                                UNIT_CONV_LENGTH, sparts, h);
50
51
52
53
54
55
56
57
58
}

/**
 * @brief Specifies which s-particle fields to write to a dataset
 *
 * @param sparts The s-particle array.
 * @param list The list of i/o properties to write.
 * @param num_fields The number of i/o fields to write.
 */
59
60
61
INLINE static void stars_write_particles(const struct spart *sparts,
                                         struct io_props *list,
                                         int *num_fields) {
62
63

  /* Say how much we want to read */
Loic Hausammann's avatar
Loic Hausammann committed
64
  *num_fields = 5;
65
66
67

  /* List what we want to read */
  list[0] = io_make_output_field("Coordinates", DOUBLE, 3, UNIT_CONV_LENGTH,
lhausamm's avatar
lhausamm committed
68
                                 sparts, x);
lhausamm's avatar
lhausamm committed
69
70
  list[1] =
      io_make_output_field("Velocities", FLOAT, 3, UNIT_CONV_SPEED, sparts, v);
71
  list[2] =
lhausamm's avatar
lhausamm committed
72
      io_make_output_field("Masses", FLOAT, 1, UNIT_CONV_MASS, sparts, mass);
73
  list[3] = io_make_output_field("ParticleIDs", LONGLONG, 1, UNIT_CONV_NO_UNITS,
lhausamm's avatar
lhausamm committed
74
                                 sparts, id);
Loic Hausammann's avatar
Loic Hausammann committed
75
76
  list[4] = io_make_output_field("SmoothingLength", FLOAT, 1, UNIT_CONV_LENGTH,
                                 sparts, h);
77
78
}

Loic Hausammann's avatar
Loic Hausammann committed
79
80
81
/**
 * @brief Initialize the global properties of the stars scheme.
 *
82
83
 * By default, takes the values provided by the hydro.
 *
Loic Hausammann's avatar
Loic Hausammann committed
84
85
86
87
88
89
 * @param p The #stars_props.
 * @param phys_const The physical constants in the internal unit system.
 * @param us The internal unit system.
 * @param params The parsed parameters.
 */
INLINE static void stars_props_init(struct stars_props *sp,
90
91
92
93
                                    const struct phys_const *phys_const,
                                    const struct unit_system *us,
                                    struct swift_params *params,
                                    const struct hydro_props *p) {
Loic Hausammann's avatar
Loic Hausammann committed
94
95

  /* Kernel properties */
96
97
  sp->eta_neighbours = parser_get_opt_param_float(
      params, "Stars:resolution_eta", p->eta_neighbours);
Loic Hausammann's avatar
Loic Hausammann committed
98
99

  /* Tolerance for the smoothing length Newton-Raphson scheme */
100
101
  sp->h_tolerance =
      parser_get_opt_param_float(params, "Stars:h_tolerance", p->h_tolerance);
Loic Hausammann's avatar
Loic Hausammann committed
102
103
104
105
106
107
108
109
110

  /* Get derived properties */
  sp->target_neighbours = pow_dimension(sp->eta_neighbours) * kernel_norm;
  const float delta_eta = sp->eta_neighbours * (1.f + sp->h_tolerance);
  sp->delta_neighbours =
      (pow_dimension(delta_eta) - pow_dimension(sp->eta_neighbours)) *
      kernel_norm;

  /* Maximal smoothing length */
111
  sp->h_max = parser_get_opt_param_float(params, "Stars:h_max", p->h_max);
Loic Hausammann's avatar
Loic Hausammann committed
112
113
114
115
116
117

  /* Number of iterations to converge h */
  sp->max_smoothing_iterations = parser_get_opt_param_int(
      params, "Stars:max_ghost_iterations", p->max_smoothing_iterations);

  /* Time integration properties */
118
119
  const float max_volume_change =
      parser_get_opt_param_float(params, "Stars:max_volume_change", -1);
Loic Hausammann's avatar
Loic Hausammann committed
120
121
122
123
124
125
126
127
128
129
130
  if (max_volume_change == -1)
    sp->log_max_h_change = p->log_max_h_change;
  else
    sp->log_max_h_change = logf(powf(max_volume_change, hydro_dimension_inv));
}

/**
 * @brief Print the global properties of the stars scheme.
 *
 * @param p The #stars_props.
 */
Loic Hausammann's avatar
Loic Hausammann committed
131
INLINE static void stars_props_print(const struct stars_props *sp) {
Loic Hausammann's avatar
Loic Hausammann committed
132
133
134
135
136
137
138
139
140
141
142
143
144

  /* Now stars */
  message("Stars kernel: %s with eta=%f (%.2f neighbours).", kernel_name,
          sp->eta_neighbours, sp->target_neighbours);

  message("Stars relative tolerance in h: %.5f (+/- %.4f neighbours).",
          sp->h_tolerance, sp->delta_neighbours);

  message(
      "Stars integration: Max change of volume: %.2f "
      "(max|dlog(h)/dt|=%f).",
      pow_dimension(expf(sp->log_max_h_change)), sp->log_max_h_change);

145
146
  if (sp->h_max != FLT_MAX)
    message("Maximal smoothing length allowed: %.4f", sp->h_max);
Loic Hausammann's avatar
Loic Hausammann committed
147
148

  message("Maximal iterations in ghost task set to %d",
149
          sp->max_smoothing_iterations);
Loic Hausammann's avatar
Loic Hausammann committed
150
151
152
}

#if defined(HAVE_HDF5)
153
154
INLINE static void stars_props_print_snapshot(hid_t h_grpstars,
                                              const struct stars_props *sp) {
Loic Hausammann's avatar
Loic Hausammann committed
155
156

  io_write_attribute_s(h_grpstars, "Kernel function", kernel_name);
157
158
  io_write_attribute_f(h_grpstars, "Kernel target N_ngb",
                       sp->target_neighbours);
Loic Hausammann's avatar
Loic Hausammann committed
159
160
  io_write_attribute_f(h_grpstars, "Kernel delta N_ngb", sp->delta_neighbours);
  io_write_attribute_f(h_grpstars, "Kernel eta", sp->eta_neighbours);
161
162
  io_write_attribute_f(h_grpstars, "Smoothing length tolerance",
                       sp->h_tolerance);
Loic Hausammann's avatar
Loic Hausammann committed
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
  io_write_attribute_f(h_grpstars, "Maximal smoothing length", sp->h_max);
  io_write_attribute_f(h_grpstars, "Volume log(max(delta h))",
                       sp->log_max_h_change);
  io_write_attribute_f(h_grpstars, "Volume max change time-step",
                       pow_dimension(expf(sp->log_max_h_change)));
  io_write_attribute_i(h_grpstars, "Max ghost iterations",
                       sp->max_smoothing_iterations);
}
#endif

/**
 * @brief Write a #stars_props struct to the given FILE as a stream of bytes.
 *
 * @param p the struct
 * @param stream the file stream
 */
179
180
INLINE static void stars_props_struct_dump(const struct stars_props *p,
                                           FILE *stream) {
Loic Hausammann's avatar
Loic Hausammann committed
181
182
183
184
185
186
187
188
189
190
191
  restart_write_blocks((void *)p, sizeof(struct stars_props), 1, stream,
                       "starsprops", "stars props");
}

/**
 * @brief Restore a stars_props struct from the given FILE as a stream of
 * bytes.
 *
 * @param p the struct
 * @param stream the file stream
 */
192
193
INLINE static void stars_props_struct_restore(const struct stars_props *p,
                                              FILE *stream) {
Loic Hausammann's avatar
Loic Hausammann committed
194
195
196
197
  restart_read_blocks((void *)p, sizeof(struct stars_props), 1, stream, NULL,
                      "stars props");
}

198
#endif /* SWIFT_DEFAULT_STAR_IO_H */