hydro.h 9.88 KB
Newer Older
1
2
/*******************************************************************************
 * This file is part of SWIFT.
3
 * Copyright (c) 2016 Matthieu Schaller (matthieu.schaller@durham.ac.uk)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * 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
#include "adiabatic_index.h"
21
#include "approx_math.h"
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "equation_of_state.h"

/**
 * @brief Returns the internal energy of a particle
 *
 * For implementations where the main thermodynamic variable
 * is not internal energy, this function computes the internal
 * energy from the thermodynamic variable.
 *
 * @param p The particle of interest
 * @param dt Time since the last kick
 */
__attribute__((always_inline)) INLINE static float hydro_get_internal_energy(
    const struct part *restrict p, float dt) {

  return p->u;
}

/**
 * @brief Returns the pressure of a particle
 *
 * @param p The particle of interest
 * @param dt Time since the last kick
 */
__attribute__((always_inline)) INLINE static float hydro_get_pressure(
    const struct part *restrict p, float dt) {

  return gas_pressure_from_internal_energy(p->rho, p->u);
}

/**
 * @brief Returns the entropy of a particle
 *
 * For implementations where the main thermodynamic variable
 * is not entropy, this function computes the entropy from
 * the thermodynamic variable.
 *
 * @param p The particle of interest
 * @param dt Time since the last kick
 */
__attribute__((always_inline)) INLINE static float hydro_get_entropy(
    const struct part *restrict p, float dt) {

  return gas_entropy_from_internal_energy(p->rho, p->u);
}

/**
 * @brief Returns the sound speed of a particle
 *
 * @param p The particle of interest
 * @param dt Time since the last kick
 */
__attribute__((always_inline)) INLINE static float hydro_get_soundspeed(
    const struct part *restrict p, float dt) {

  return gas_soundspeed_from_internal_energy(p->rho, p->u);
}
79

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
 * @brief Modifies the thermal state of a particle to the imposed internal
 * energy
 *
 * This overrides the current state of the particle but does *not* change its
 * time-derivatives
 *
 * @param p The particle
 * @param u The new internal energy
 */
__attribute__((always_inline)) INLINE static void hydro_set_internal_energy(
    struct part *restrict p, float u) {

  p->u = u;
}

/**
 * @brief Modifies the thermal state of a particle to the imposed entropy
 *
 * This overrides the current state of the particle but does *not* change its
 * time-derivatives
 *
 * @param p The particle
 * @param S The new entropy
 */
__attribute__((always_inline)) INLINE static void hydro_set_entropy(
    struct part *restrict p, float S) {

  p->u = gas_internal_energy_from_entropy(p->rho, S);
}

111
112
113
/**
 * @brief Computes the hydro time-step of a given particle
 *
114
115
116
 * This function returns the time-step of a particle given its hydro-dynamical
 * state. A typical time-step calculation would be the use of the CFL condition.
 *
117
118
 * @param p Pointer to the particle data
 * @param xp Pointer to the extended particle data
119
 * @param hydro_properties The SPH parameters
120
121
122
 *
 */
__attribute__((always_inline)) INLINE static float hydro_compute_timestep(
123
124
    const struct part *restrict p, const struct xpart *restrict xp,
    const struct hydro_props *restrict hydro_properties) {
125
126

  const float CFL_condition = hydro_properties->CFL_condition;
127
128

  /* CFL condition */
129
130
  const float dt_cfl =
      2.f * kernel_gamma * CFL_condition * p->h / p->force.v_sig;
131

132
  return dt_cfl;
133
134
}

135
136
137
138
/**
 * @brief Initialises the particles for the first time
 *
 * This function is called only once just after the ICs have been
139
140
 * read in to do some conversions or assignments between the particle
 * and extended particle fields.
141
142
143
144
 *
 * @param p The particle to act upon
 * @param xp The extended particle data to act upon
 */
145
__attribute__((always_inline)) INLINE static void hydro_first_init_part(
146
    struct part *restrict p, struct xpart *restrict xp) {
147
148
149
150

  xp->u_full = p->u;
}

151
152
153
154
/**
 * @brief Prepares a particle for the density calculation.
 *
 * Zeroes all the relevant arrays in preparation for the sums taking place in
155
156
 * the various density loop over neighbours. Typically, all fields of the
 * density sub-structure of a particle get zeroed in here.
157
158
159
 *
 * @param p The particle to act upon
 */
160
__attribute__((always_inline)) INLINE static void hydro_init_part(
161
    struct part *restrict p) {
162
163
164
165
166
167
168
169
170
171
172
  p->density.wcount = 0.f;
  p->density.wcount_dh = 0.f;
  p->rho = 0.f;
  p->rho_dh = 0.f;
}

/**
 * @brief Finishes the density calculation.
 *
 * Multiplies the density and number of neighbours by the appropiate constants
 * and add the self-contribution term.
173
174
175
 * Additional quantities such as velocity gradients will also get the final
 *terms
 * added to them here.
176
177
178
179
 *
 * @param p The particle to act upon
 * @param time The current time
 */
180
__attribute__((always_inline)) INLINE static void hydro_end_density(
181
    struct part *restrict p, float time) {
182
183
184
185
186
187
188

  /* Some smoothing length multiples. */
  const float h = p->h;
  const float ih = 1.0f / h;
  const float ih2 = ih * ih;
  const float ih4 = ih2 * ih2;

189
190
  /* Final operation on the density (add self-contribution). */
  p->rho += p->mass * kernel_root;
191
  p->rho_dh -= 3.0f * p->mass * kernel_root;
192
193
194
195
196
  p->density.wcount += kernel_root;

  /* Finish the calculation by inserting the missing h-factors */
  p->rho *= ih * ih2;
  p->rho_dh *= ih4;
197
198
  p->density.wcount *= kernel_norm;
  p->density.wcount_dh *= ih * kernel_gamma * kernel_norm;
199
200
201
202
203

  const float irho = 1.f / p->rho;

  /* Compute the derivative term */
  p->rho_dh = 1.f / (1.f + 0.33333333f * p->h * p->rho_dh * irho);
204
205
206
207
208
}

/**
 * @brief Prepare a particle for the force calculation.
 *
209
210
211
212
213
214
 * This function is called in the ghost task to convert some quantities coming
 * from the density loop over neighbours into quantities ready to be used in the
 * force loop over neighbours. Quantities are typically read from the density
 * sub-structure and written to the force sub-structure.
 * Examples of calculations done here include the calculation of viscosity term
 * constants, thermal conduction terms, hydro conversions, etc.
215
216
217
 *
 * @param p The particle to act upon
 * @param xp The extended particle data to act upon
218
219
 * @param ti_current The current time (on the timeline)
 * @param timeBase The minimal time-step size
220
 */
221
__attribute__((always_inline)) INLINE static void hydro_prepare_force(
222
223
    struct part *restrict p, struct xpart *restrict xp, int ti_current,
    double timeBase) {
224

225
  p->force.pressure = p->rho * p->u * hydro_gamma_minus_one;
226
227
228
229
230
231
}

/**
 * @brief Reset acceleration fields of a particle
 *
 * Resets all hydro acceleration and time derivative fields in preparation
232
 * for the sums taking  place in the various force tasks.
233
234
235
 *
 * @param p The particle to act upon
 */
236
__attribute__((always_inline)) INLINE static void hydro_reset_acceleration(
237
    struct part *restrict p) {
238
239
240
241
242
243
244

  /* Reset the acceleration. */
  p->a_hydro[0] = 0.0f;
  p->a_hydro[1] = 0.0f;
  p->a_hydro[2] = 0.0f;

  /* Reset the time derivatives. */
245
  p->u_dt = 0.0f;
246
  p->force.h_dt = 0.0f;
247
248
249
250
251
252
  p->force.v_sig = 0.0f;
}

/**
 * @brief Predict additional particle fields forward in time when drifting
 *
253
254
255
 * Additional hydrodynamic quantites are drifted forward in time here. These
 * include thermal quantities (thermal energy or total energy or entropy, ...).
 *
256
257
 * @param p The particle
 * @param xp The extended data of the particle
258
259
260
 * @param t0 The time at the start of the drift (on the timeline)
 * @param t1 The time at the end of the drift (on the timeline)
 * @param timeBase The minimal time-step size
261
262
 */
__attribute__((always_inline)) INLINE static void hydro_predict_extra(
263
264
    struct part *restrict p, const struct xpart *restrict xp, int t0, int t1,
    double timeBase) {
265

266
  p->u = xp->u_full;
267

268
  /* Need to recompute the pressure as well */
269
  p->force.pressure = p->rho * p->u * hydro_gamma_minus_one;
270
271
272
273
274
}

/**
 * @brief Finishes the force calculation.
 *
275
276
277
 * Multiplies the force and accelerations by the appropiate constants
 * and add the self-contribution term. In most cases, there is nothing
 * to do here.
278
279
280
 *
 * @param p The particle to act upon
 */
281
__attribute__((always_inline)) INLINE static void hydro_end_force(
282
283
284
285
    struct part *restrict p) {

  p->force.h_dt *= p->h * 0.333333333f;
}
286
287
288
289

/**
 * @brief Kick the additional variables
 *
290
291
292
 * Additional hydrodynamic quantites are kicked forward in time here. These
 * include thermal quantities (thermal energy or total energy or entropy, ...).
 *
293
 * @param p The particle to act upon
294
 * @param xp The particle extended data to act upon
295
 * @param dt The time-step for this kick
296
 * @param half_dt The half time-step for this kick
297
 */
298
__attribute__((always_inline)) INLINE static void hydro_kick_extra(
299
300
    struct part *restrict p, struct xpart *restrict xp, float dt,
    float half_dt) {
301
302
303
304
305
306
307

  /* Kick in momentum space */
  xp->u_full += p->u_dt * dt;

  /* Get the predicted internal energy */
  p->u = xp->u_full - half_dt * p->u_dt;
}
308
309

/**
310
 * @brief Converts hydro quantity of a particle at the start of a run
311
 *
312
313
314
315
 * This function is called once at the end of the engine_init_particle()
 * routine (at the start of a calculation) after the densities of
 * particles have been computed.
 * This can be used to convert internal energy into entropy for instance.
316
317
318
 *
 * @param p The particle to act upon
 */
319
__attribute__((always_inline)) INLINE static void hydro_convert_quantities(
320
    struct part *restrict p) {}