hydro_iact.h 9.24 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
 *
 * 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/>.
 *
 ******************************************************************************/
19
20
#ifndef SWIFT_MINIMAL_HYDRO_IACT_H
#define SWIFT_MINIMAL_HYDRO_IACT_H
21

22
/**
23
24
 * @file Minimal/hydro_iact.h
 * @brief Minimal conservative implementation of SPH (Neighbour loop equations)
25
 *
26
 * The thermal variable is the internal energy (u). Simple constant
27
28
29
30
 * viscosity term without switches is implemented. No thermal conduction
 * term is implemented.
 *
 * This corresponds to equations (43), (44), (45), (101), (103)  and (104) with
31
32
 * \f$\beta=3\f$ and \f$\alpha_u=0\f$ of Price, D., Journal of Computational
 * Physics, 2012, Volume 231, Issue 3, pp. 759-794.
33
34
 */

35
#include "adiabatic_index.h"
36
#include "minmax.h"
37

38
39
40
41
42
43
44
/**
 * @brief Density loop
 */
__attribute__((always_inline)) INLINE static void runner_iact_density(
    float r2, float *dx, float hi, float hj, struct part *pi, struct part *pj) {

  float wi, wj, wi_dx, wj_dx;
45
46

  const float r = sqrtf(r2);
47
48

  /* Get the masses. */
49
50
  const float mi = pi->mass;
  const float mj = pj->mass;
51
52

  /* Compute density of pi. */
53
  const float hi_inv = 1.f / hi;
54
55
  const float ui = r * hi_inv;
  kernel_deval(ui, &wi, &wi_dx);
56
57

  pi->rho += mj * wi;
58
  pi->density.rho_dh -= mj * (hydro_dimension * wi + ui * wi_dx);
59
  pi->density.wcount += wi;
60
  pi->density.wcount_dh -= (hydro_dimension * wi + ui * wi_dx);
61
62

  /* Compute density of pj. */
63
  const float hj_inv = 1.f / hj;
64
65
  const float uj = r * hj_inv;
  kernel_deval(uj, &wj, &wj_dx);
66
67

  pj->rho += mi * wj;
68
  pj->density.rho_dh -= mi * (hydro_dimension * wj + uj * wj_dx);
69
  pj->density.wcount += wj;
70
  pj->density.wcount_dh -= (hydro_dimension * wj + uj * wj_dx);
71
72
73
74
75
76
77
78
79
80
81
}

/**
 * @brief Density loop (non-symmetric version)
 */
__attribute__((always_inline)) INLINE static void runner_iact_nonsym_density(
    float r2, float *dx, float hi, float hj, struct part *pi, struct part *pj) {

  float wi, wi_dx;

  /* Get the masses. */
82
  const float mj = pj->mass;
83
84

  /* Get r and r inverse. */
85
  const float r = sqrtf(r2);
86

87
  const float h_inv = 1.f / hi;
88
89
  const float ui = r * h_inv;
  kernel_deval(ui, &wi, &wi_dx);
90
91

  pi->rho += mj * wi;
92
  pi->density.rho_dh -= mj * (hydro_dimension * wi + ui * wi_dx);
93
  pi->density.wcount += wi;
94
  pi->density.wcount_dh -= (hydro_dimension * wi + ui * wi_dx);
95
96
97
98
99
100
101
102
}

/**
 * @brief Force loop
 */
__attribute__((always_inline)) INLINE static void runner_iact_force(
    float r2, float *dx, float hi, float hj, struct part *pi, struct part *pj) {

103
104
  const float fac_mu = 1.f; /* Will change with cosmological integration */

105
106
107
108
109
110
111
112
113
114
  const float r = sqrtf(r2);
  const float r_inv = 1.0f / r;

  /* Recover some data */
  const float mi = pi->mass;
  const float mj = pj->mass;
  const float rhoi = pi->rho;
  const float rhoj = pj->rho;
  const float pressurei = pi->force.pressure;
  const float pressurej = pj->force.pressure;
115
116

  /* Get the kernel for hi. */
117
  const float hi_inv = 1.0f / hi;
118
  const float hid_inv = pow_dimension_plus_one(hi_inv); /* 1/h^(d+1) */
119
120
  const float xi = r * hi_inv;
  float wi, wi_dx;
121
  kernel_deval(xi, &wi, &wi_dx);
122
  const float wi_dr = hid_inv * wi_dx;
123
124

  /* Get the kernel for hj. */
125
  const float hj_inv = 1.0f / hj;
126
  const float hjd_inv = pow_dimension_plus_one(hj_inv); /* 1/h^(d+1) */
127
128
  const float xj = r * hj_inv;
  float wj, wj_dx;
129
  kernel_deval(xj, &wj, &wj_dx);
130
  const float wj_dr = hjd_inv * wj_dx;
131
132

  /* Compute gradient terms */
133
134
  const float P_over_rho2_i = pressurei / (rhoi * rhoi) * pi->force.f;
  const float P_over_rho2_j = pressurej / (rhoj * rhoj) * pj->force.f;
135
136

  /* Compute dv dot r. */
137
138
139
  const float dvdr = (pi->v[0] - pj->v[0]) * dx[0] +
                     (pi->v[1] - pj->v[1]) * dx[1] +
                     (pi->v[2] - pj->v[2]) * dx[2];
140

141
  /* Are the particles moving towards each others ? */
142
  const float omega_ij = min(dvdr, 0.f);
143
144
  const float mu_ij = fac_mu * r_inv * omega_ij; /* This is 0 or negative */

145
  /* Compute sound speeds and signal velocity */
146
147
  const float ci = pi->force.soundspeed;
  const float cj = pj->force.soundspeed;
148
149
150
151
  const float v_sig = ci + cj - 3.f * mu_ij;

  /* Construct the full viscosity term */
  const float rho_ij = 0.5f * (rhoi + rhoj);
152
  const float visc = -0.5f * const_viscosity_alpha * v_sig * mu_ij / rho_ij;
153
154
155

  /* Convolve with the kernel */
  const float visc_acc_term = 0.5f * visc * (wi_dr + wj_dr) * r_inv;
156

157
  /* SPH acceleration term */
158
159
160
161
162
  const float sph_acc_term =
      (P_over_rho2_i * wi_dr + P_over_rho2_j * wj_dr) * r_inv;

  /* Assemble the acceleration */
  const float acc = sph_acc_term + visc_acc_term;
163

164
  /* Use the force Luke ! */
165
166
167
  pi->a_hydro[0] -= mj * acc * dx[0];
  pi->a_hydro[1] -= mj * acc * dx[1];
  pi->a_hydro[2] -= mj * acc * dx[2];
168

169
170
171
  pj->a_hydro[0] += mi * acc * dx[0];
  pj->a_hydro[1] += mi * acc * dx[1];
  pj->a_hydro[2] += mi * acc * dx[2];
172
173

  /* Get the time derivative for u. */
174
  const float sph_du_term_i = P_over_rho2_i * dvdr * r_inv * wi_dr;
175
  const float sph_du_term_j = P_over_rho2_j * dvdr * r_inv * wj_dr;
176
177
178
179
180
181
182
183
184
185
186

  /* Viscosity term */
  const float visc_du_term = 0.5f * visc_acc_term * dvdr;

  /* Assemble the energy equation term */
  const float du_dt_i = sph_du_term_i + visc_du_term;
  const float du_dt_j = sph_du_term_j + visc_du_term;

  /* Internal energy time derivatibe */
  pi->u_dt += du_dt_i * mj;
  pj->u_dt += du_dt_j * mi;
187
188

  /* Get the time derivative for h. */
189
190
  pi->force.h_dt -= mj * dvdr * r_inv / rhoj * wi_dr;
  pj->force.h_dt -= mi * dvdr * r_inv / rhoi * wj_dr;
191
192

  /* Update the signal velocity. */
193
194
  pi->force.v_sig = max(pi->force.v_sig, v_sig);
  pj->force.v_sig = max(pj->force.v_sig, v_sig);
195
196
197
198
199
200
201
202
}

/**
 * @brief Force loop (non-symmetric version)
 */
__attribute__((always_inline)) INLINE static void runner_iact_nonsym_force(
    float r2, float *dx, float hi, float hj, struct part *pi, struct part *pj) {

203
204
  const float fac_mu = 1.f; /* Will change with cosmological integration */

205
206
207
208
209
210
211
212
213
214
  const float r = sqrtf(r2);
  const float r_inv = 1.0f / r;

  /* Recover some data */
  // const float mi = pi->mass;
  const float mj = pj->mass;
  const float rhoi = pi->rho;
  const float rhoj = pj->rho;
  const float pressurei = pi->force.pressure;
  const float pressurej = pj->force.pressure;
215
216

  /* Get the kernel for hi. */
217
  const float hi_inv = 1.0f / hi;
218
  const float hid_inv = pow_dimension_plus_one(hi_inv); /* 1/h^(d+1) */
219
220
  const float xi = r * hi_inv;
  float wi, wi_dx;
221
  kernel_deval(xi, &wi, &wi_dx);
222
  const float wi_dr = hid_inv * wi_dx;
223
224

  /* Get the kernel for hj. */
225
  const float hj_inv = 1.0f / hj;
226
  const float hjd_inv = pow_dimension_plus_one(hj_inv); /* 1/h^(d+1) */
227
228
  const float xj = r * hj_inv;
  float wj, wj_dx;
229
  kernel_deval(xj, &wj, &wj_dx);
230
  const float wj_dr = hjd_inv * wj_dx;
231
232

  /* Compute gradient terms */
233
234
  const float P_over_rho2_i = pressurei / (rhoi * rhoi) * pi->force.f;
  const float P_over_rho2_j = pressurej / (rhoj * rhoj) * pj->force.f;
235
236

  /* Compute dv dot r. */
237
238
239
  const float dvdr = (pi->v[0] - pj->v[0]) * dx[0] +
                     (pi->v[1] - pj->v[1]) * dx[1] +
                     (pi->v[2] - pj->v[2]) * dx[2];
240

241
  /* Are the particles moving towards each others ? */
242
  const float omega_ij = min(dvdr, 0.f);
243
244
245
  const float mu_ij = fac_mu * r_inv * omega_ij; /* This is 0 or negative */

  /* Compute sound speeds and signal velocity */
246
247
  const float ci = pi->force.soundspeed;
  const float cj = pj->force.soundspeed;
248
249
250
251
  const float v_sig = ci + cj - 3.f * mu_ij;

  /* Construct the full viscosity term */
  const float rho_ij = 0.5f * (rhoi + rhoj);
252
  const float visc = -0.5f * const_viscosity_alpha * v_sig * mu_ij / rho_ij;
253
254
255

  /* Convolve with the kernel */
  const float visc_acc_term = 0.5f * visc * (wi_dr + wj_dr) * r_inv;
256

257
  /* SPH acceleration term */
258
259
260
261
262
  const float sph_acc_term =
      (P_over_rho2_i * wi_dr + P_over_rho2_j * wj_dr) * r_inv;

  /* Assemble the acceleration */
  const float acc = sph_acc_term + visc_acc_term;
263

264
  /* Use the force Luke ! */
265
266
267
  pi->a_hydro[0] -= mj * acc * dx[0];
  pi->a_hydro[1] -= mj * acc * dx[1];
  pi->a_hydro[2] -= mj * acc * dx[2];
268
269

  /* Get the time derivative for u. */
270
271
272
273
274
275
276
277
278
279
  const float sph_du_term_i = P_over_rho2_i * dvdr * r_inv * wi_dr;

  /* Viscosity term */
  const float visc_du_term = 0.5f * visc_acc_term * dvdr;

  /* Assemble the energy equation term */
  const float du_dt_i = sph_du_term_i + visc_du_term;

  /* Internal energy time derivatibe */
  pi->u_dt += du_dt_i * mj;
280
281

  /* Get the time derivative for h. */
282
  pi->force.h_dt -= mj * dvdr * r_inv / rhoj * wi_dr;
283
284

  /* Update the signal velocity. */
285
  pi->force.v_sig = max(pi->force.v_sig, v_sig);
286
287
}

288
#endif /* SWIFT_MINIMAL_HYDRO_IACT_H */