hydro_iact.h 12.1 KB
Newer Older
1
2
/*******************************************************************************
 * This file is part of SWIFT.
3
 * Copyright (c) 2012 Pedro Gonnet (pedro.gonnet@durham.ac.uk)
4
 *                    Matthieu Schaller (matthieu.schaller@durham.ac.uk)
5
 *
6
7
8
9
 * 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.
10
 *
11
12
13
14
 * 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.
15
 *
16
17
 * 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/>.
18
 *
19
 ******************************************************************************/
20
#ifndef SWIFT_RUNNER_IACT_LEGACY_H
21
#define SWIFT_RUNNER_IACT_LEGACY_H
22
23
24
25

/**
 * @brief SPH interaction functions following the Gadget-2 version of SPH.
 *
26
27
28
29
30
31
 * The interactions computed here are the ones presented in the Gadget-2 paper
 *and use the same
 * numerical coefficients as the Gadget-2 code. When used with the Spline-3
 *kernel, the results
 * should be equivalent to the ones obtained with Gadget-2 up to the rounding
 *errors and interactions
32
33
34
35
36
37
38
 * missed by the Gadget-2 tree-code neighbours search.
 *
 */

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

42
43
  float wi, wi_dx;
  float wj, wj_dx;
44
  float dv[3], curlvr[3];
45

46
  /* Get the masses. */
47
  const float mi = pi->mass;
48
49
50
51
52
53
54
55
56
57
58
59
60
  const float mj = pj->mass;

  /* Get r and r inverse. */
  const float r = sqrtf(r2);
  const float r_inv = 1.0f / r;

  /* Compute the kernel function for pi */
  const float hi_inv = 1.f / hi;
  const float ui = r * hi_inv;
  kernel_deval(ui, &wi, &wi_dx);

  /* Compute contribution to the density */
  pi->rho += mj * wi;
61
  pi->rho_dh -= mj * (3.f * wi + ui * wi_dx);
62

63
64
65
66
67
68
69
70
71
72
73
  /* Compute contribution to the number of neighbours */
  pi->density.wcount += wi;
  pi->density.wcount_dh -= ui * wi_dx;

  /* Compute the kernel function for pj */
  const float hj_inv = 1.f / hj;
  const float uj = r * hj_inv;
  kernel_deval(uj, &wj, &wj_dx);

  /* Compute contribution to the density */
  pj->rho += mi * wj;
74
  pj->rho_dh -= mi * (3.f * wj + uj * wj_dx);
75

76
77
78
  /* Compute contribution to the number of neighbours */
  pj->density.wcount += wj;
  pj->density.wcount_dh -= uj * wj_dx;
79

80
81
  const float faci = mj * wi_dx * r_inv;
  const float facj = mi * wj_dx * r_inv;
82

83
84
85
86
  /* Compute dv dot r */
  dv[0] = pi->v[0] - pj->v[0];
  dv[1] = pi->v[1] - pj->v[1];
  dv[2] = pi->v[2] - pj->v[2];
87
88
  const float dvdr = dv[0] * dx[0] + dv[1] * dx[1] + dv[2] * dx[2];

89
90
  pi->div_v -= faci * dvdr;
  pj->div_v -= facj * dvdr;
91
92
93
94
95
96

  /* Compute dv cross r */
  curlvr[0] = dv[1] * dx[2] - dv[2] * dx[1];
  curlvr[1] = dv[2] * dx[0] - dv[0] * dx[2];
  curlvr[2] = dv[0] * dx[1] - dv[1] * dx[0];

97
98
99
  pi->density.rot_v[0] += faci * curlvr[0];
  pi->density.rot_v[1] += faci * curlvr[1];
  pi->density.rot_v[2] += faci * curlvr[2];
100

101
102
103
  pj->density.rot_v[0] += facj * curlvr[0];
  pj->density.rot_v[1] += facj * curlvr[1];
  pj->density.rot_v[2] += facj * curlvr[2];
104
105
}

106
107
108
109
110
111
/**
 * @brief Density loop (Vectorized version)
 */
__attribute__((always_inline)) INLINE static void runner_iact_vec_density(
    float *R2, float *Dx, float *Hi, float *Hj, struct part **pi,
    struct part **pj) {
James Willis's avatar
James Willis committed
112
113
  error(
      "A vectorised version of the Gadget2 density interaction function does "
Matthieu Schaller's avatar
Matthieu Schaller committed
114
      "not exist yet!");
115
116
}

117
118
119
/**
 * @brief Density loop (non-symmetric version)
 */
120
121
122
123
124
125
126
__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;
  float dv[3], curlvr[3];

  /* Get the masses. */
127
  const float mj = pj->mass;
128
129

  /* Get r and r inverse. */
130
131
  const float r = sqrtf(r2);
  const float ri = 1.0f / r;
132

133
134
135
136
137
138
139
  /* Compute the kernel function */
  const float h_inv = 1.0f / hi;
  const float u = r * h_inv;
  kernel_deval(u, &wi, &wi_dx);

  /* Compute contribution to the density */
  pi->rho += mj * wi;
140
  pi->rho_dh -= mj * (3.f * wi + u * wi_dx);
141
142
143
144

  /* Compute contribution to the number of neighbours */
  pi->density.wcount += wi;
  pi->density.wcount_dh -= u * wi_dx;
145

146
  const float fac = mj * wi_dx * ri;
147

148
149
150
151
152
153
154
  /* Compute dv dot r */
  dv[0] = pi->v[0] - pj->v[0];
  dv[1] = pi->v[1] - pj->v[1];
  dv[2] = pi->v[2] - pj->v[2];
  const float dvdr = dv[0] * dx[0] + dv[1] * dx[1] + dv[2] * dx[2];
  pi->div_v -= fac * dvdr;

155
156
157
158
159
  /* Compute dv cross r */
  curlvr[0] = dv[1] * dx[2] - dv[2] * dx[1];
  curlvr[1] = dv[2] * dx[0] - dv[0] * dx[2];
  curlvr[2] = dv[0] * dx[1] - dv[1] * dx[0];

160
161
162
  pi->density.rot_v[0] += fac * curlvr[0];
  pi->density.rot_v[1] += fac * curlvr[1];
  pi->density.rot_v[2] += fac * curlvr[2];
163
164
}

165
166
167
168
169
170
/**
 * @brief Density loop (non-symmetric vectorized version)
 */
__attribute__((always_inline)) INLINE static void
runner_iact_nonsym_vec_density(float *R2, float *Dx, float *Hi, float *Hj,
                               struct part **pi, struct part **pj) {
James Willis's avatar
James Willis committed
171
172
  error(
      "A vectorised version of the Gadget2 non-symmetric density interaction "
Matthieu Schaller's avatar
Matthieu Schaller committed
173
      "function does not exist yet!");
174
175
}

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

182
183
184
  float wi, wj, wi_dx, wj_dx;

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

186
187
188
189
  const float r = sqrtf(r2);
  const float r_inv = 1.0f / r;

  /* Get some values in local variables. */
190
  const float mi = pi->mass;
191
192
193
  const float mj = pj->mass;
  const float rhoi = pi->rho;
  const float rhoj = pj->rho;
194
195
  const float pressurei = pi->force.pressure;
  const float pressurej = pj->force.pressure;
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215

  /* Get the kernel for hi. */
  const float hi_inv = 1.0f / hi;
  const float hi2_inv = hi_inv * hi_inv;
  const float ui = r * hi_inv;
  kernel_deval(ui, &wi, &wi_dx);
  const float wi_dr = hi2_inv * hi2_inv * wi_dx;

  /* Get the kernel for hj. */
  const float hj_inv = 1.0f / hj;
  const float hj2_inv = hj_inv * hj_inv;
  const float xj = r * hj_inv;
  kernel_deval(xj, &wj, &wj_dx);
  const float wj_dr = hj2_inv * hj2_inv * wj_dx;

  /* Compute gradient terms */
  const float P_over_rho_i = pressurei / (rhoi * rhoi) * pi->rho_dh;
  const float P_over_rho_j = pressurej / (rhoj * rhoj) * pj->rho_dh;

  /* Compute sound speeds */
216
217
  const float ci = pi->force.soundspeed;
  const float cj = pj->force.soundspeed;
218

219
  /* Compute dv dot r. */
220
221
222
  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];
223

224
225
226
  /* Balsara term */
  const float balsara_i =
      fabsf(pi->div_v) /
227
      (fabsf(pi->div_v) + pi->force.curl_v + 0.0001f * ci / fac_mu / hi);
228
229
  const float balsara_j =
      fabsf(pj->div_v) /
230
      (fabsf(pj->div_v) + pj->force.curl_v + 0.0001f * cj / fac_mu / hj);
231
232
233
234
235
236
237
238
239
240
241
242

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

  /* Signal velocity */
  const float v_sig = ci + cj - 3.f * mu_ij;

  /* Now construct the full viscosity term */
  const float rho_ij = 0.5f * (rhoi + rhoj);
  const float visc = -0.25f * const_viscosity_alpha * v_sig * mu_ij *
                     (balsara_i + balsara_j) / rho_ij;
243
244

  /* Now, convolve with the kernel */
245
246
  const float visc_term = 0.5f * visc * (wi_dr + wj_dr) * r_inv;
  const float sph_term = (P_over_rho_i * wi_dr + P_over_rho_j * wj_dr) * r_inv;
247
248
249
250
251

  /* Eventually got the acceleration */
  const float acc = visc_term + sph_term;

  /* Use the force Luke ! */
252
253
254
  pi->a_hydro[0] -= mj * acc * dx[0];
  pi->a_hydro[1] -= mj * acc * dx[1];
  pi->a_hydro[2] -= mj * acc * dx[2];
255

256
257
258
  pj->a_hydro[0] += mi * acc * dx[0];
  pj->a_hydro[1] += mi * acc * dx[1];
  pj->a_hydro[2] += mi * acc * dx[2];
259

260
  /* Get the time derivative for h. */
261
262
  pi->h_dt -= mj * dvdr * r_inv / rhoj * wi_dr;
  pj->h_dt -= mi * dvdr * r_inv / rhoi * wj_dr;
263

264
  /* Update the signal velocity. */
265
266
  pi->force.v_sig = fmaxf(pi->force.v_sig, v_sig);
  pj->force.v_sig = fmaxf(pj->force.v_sig, v_sig);
267

268
  /* Change in entropy */
269
270
  pi->entropy_dt += 0.5f * mj * visc_term * dvdr;
  pj->entropy_dt -= 0.5f * mi * visc_term * dvdr;
271
}
272

273
274
275
276
277
278
/**
 * @brief Force loop (Vectorized version)
 */
__attribute__((always_inline)) INLINE static void runner_iact_vec_force(
    float *R2, float *Dx, float *Hi, float *Hj, struct part **pi,
    struct part **pj) {
James Willis's avatar
James Willis committed
279
280
  error(
      "A vectorised version of the Gadget2 force interaction function does not "
Matthieu Schaller's avatar
Matthieu Schaller committed
281
      "exist yet!");
282
283
}

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

290
291
292
  float wi, wj, wi_dx, wj_dx;

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

294
295
296
297
  const float r = sqrtf(r2);
  const float r_inv = 1.0f / r;

  /* Get some values in local variables. */
298
  // const float mi = pi->mass;
299
300
301
  const float mj = pj->mass;
  const float rhoi = pi->rho;
  const float rhoj = pj->rho;
302
303
  const float pressurei = pi->force.pressure;
  const float pressurej = pj->force.pressure;
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323

  /* Get the kernel for hi. */
  const float hi_inv = 1.0f / hi;
  const float hi2_inv = hi_inv * hi_inv;
  const float ui = r * hi_inv;
  kernel_deval(ui, &wi, &wi_dx);
  const float wi_dr = hi2_inv * hi2_inv * wi_dx;

  /* Get the kernel for hj. */
  const float hj_inv = 1.0f / hj;
  const float hj2_inv = hj_inv * hj_inv;
  const float xj = r * hj_inv;
  kernel_deval(xj, &wj, &wj_dx);
  const float wj_dr = hj2_inv * hj2_inv * wj_dx;

  /* Compute gradient terms */
  const float P_over_rho_i = pressurei / (rhoi * rhoi) * pi->rho_dh;
  const float P_over_rho_j = pressurej / (rhoj * rhoj) * pj->rho_dh;

  /* Compute sound speeds */
324
325
  const float ci = pi->force.soundspeed;
  const float cj = pj->force.soundspeed;
326

327
  /* Compute dv dot r. */
328
329
330
  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];
331

332
333
334
  /* Balsara term */
  const float balsara_i =
      fabsf(pi->div_v) /
335
      (fabsf(pi->div_v) + pi->force.curl_v + 0.0001f * ci / fac_mu / hi);
336
337
  const float balsara_j =
      fabsf(pj->div_v) /
338
      (fabsf(pj->div_v) + pj->force.curl_v + 0.0001f * cj / fac_mu / hj);
339
340
341
342
343
344
345
346
347
348
349
350

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

  /* Signal velocity */
  const float v_sig = ci + cj - 3.f * mu_ij;

  /* Now construct the full viscosity term */
  const float rho_ij = 0.5f * (rhoi + rhoj);
  const float visc = -0.25f * const_viscosity_alpha * v_sig * mu_ij *
                     (balsara_i + balsara_j) / rho_ij;
351
352

  /* Now, convolve with the kernel */
353
354
  const float visc_term = 0.5f * visc * (wi_dr + wj_dr) * r_inv;
  const float sph_term = (P_over_rho_i * wi_dr + P_over_rho_j * wj_dr) * r_inv;
355
356
357

  /* Eventually got the acceleration */
  const float acc = visc_term + sph_term;
358

359
  /* Use the force Luke ! */
360
361
362
  pi->a_hydro[0] -= mj * acc * dx[0];
  pi->a_hydro[1] -= mj * acc * dx[1];
  pi->a_hydro[2] -= mj * acc * dx[2];
363

364
  /* Get the time derivative for h. */
365
  pi->h_dt -= mj * dvdr * r_inv / rhoj * wi_dr;
366

367
  /* Update the signal velocity. */
368
  pi->force.v_sig = fmaxf(pi->force.v_sig, v_sig);
369

370
  /* Change in entropy */
371
  pi->entropy_dt += 0.5f * mj * visc_term * dvdr;
372
}
373

374
375
376
377
378
379
/**
 * @brief Force loop (Vectorized non-symmetric version)
 */
__attribute__((always_inline)) INLINE static void runner_iact_nonsym_vec_force(
    float *R2, float *Dx, float *Hi, float *Hj, struct part **pi,
    struct part **pj) {
James Willis's avatar
James Willis committed
380
  error(
Matthieu Schaller's avatar
Matthieu Schaller committed
381
382
      "A vectorised version of the Gadget2 non-symmetric force interaction "
      "function does not exist yet!");
383
384
}

385
#endif /* SWIFT_RUNNER_IACT_LEGACY_H */