hydro_iact.h 33.3 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
21
#ifndef SWIFT_GADGET2_HYDRO_IACT_H
#define SWIFT_GADGET2_HYDRO_IACT_H
22
23

/**
24
 * @file Gadget2/hydro_iact.h
25
26
 * @brief SPH interaction functions following the Gadget-2 version of SPH.
 *
27
 * The interactions computed here are the ones presented in the Gadget-2 paper
28
29
 * Springel, V., MNRAS, Volume 364, Issue 4, pp. 1105-1134.
 * We use the same numerical coefficients as the Gadget-2 code. When used with
30
31
32
 * the Spline-3 kernel, the results should be equivalent to the ones obtained
 * with Gadget-2 up to the rounding errors and interactions missed by the
 * Gadget-2 tree-code neighbours search.
33
34
 */

35
#include "cache.h"
James Willis's avatar
James Willis committed
36
#include "minmax.h"
37

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

44
45
  float wi, wi_dx;
  float wj, wj_dx;
46
  float dv[3], curlvr[3];
47

48
  /* Get the masses. */
49
  const float mi = pi->mass;
50
51
52
53
54
55
56
57
58
59
60
61
62
  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;
63
  pi->density.rho_dh -= mj * (hydro_dimension * wi + ui * wi_dx);
64

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

  /* 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;
76
  pj->density.rho_dh -= mi * (hydro_dimension * wj + uj * wj_dx);
77

78
79
  /* Compute contribution to the number of neighbours */
  pj->density.wcount += wj;
80
  pj->density.wcount_dh -= (hydro_dimension * wj + uj * wj_dx);
81

82
83
  const float faci = mj * wi_dx * r_inv;
  const float facj = mi * wj_dx * r_inv;
84

85
86
87
88
  /* 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];
89
90
  const float dvdr = dv[0] * dx[0] + dv[1] * dx[1] + dv[2] * dx[2];

91
92
  pi->density.div_v -= faci * dvdr;
  pj->density.div_v -= facj * dvdr;
93
94
95
96
97
98

  /* 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];

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

103
104
105
  pj->density.rot_v[0] += facj * curlvr[0];
  pj->density.rot_v[1] += facj * curlvr[1];
  pj->density.rot_v[2] += facj * curlvr[2];
106
107
  
  /* Update ngb counters */
108
109
#ifdef DEBUG_INTERACTIONS_SPH
  pi->ids_ngbs_density[pi->num_ngb_density] = pj->id;
110
  ++pi->num_ngb_density;
111
  pj->ids_ngbs_density[pj->num_ngb_density] = pi->id;
112
113
114
  ++pj->num_ngb_density;
#endif

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
  const float r = sqrtf(r2);
131
  const float r_inv = 1.0f / r;
132

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

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

  /* Compute contribution to the number of neighbours */
  pi->density.wcount += wi;
144
  pi->density.wcount_dh -= (hydro_dimension * wi + ui * wi_dx);
145

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

148
149
150
151
152
  /* 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];
153
  pi->density.div_v -= fac * dvdr;
154

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
#ifdef DEBUG_INTERACTIONS_SPH
165
  /* Update ngb counters */
166
  pi->ids_ngbs_density[pi->num_ngb_density] = pj->id;
167
168
169
  ++pi->num_ngb_density;
#endif

170
171
}

172
#ifdef WITH_VECTORIZATION
173
174
175
176
177

/**
 * @brief Density interaction computed using 1 vector
 * (non-symmetric vectorized version).
 */
178
__attribute__((always_inline)) INLINE static void
Matthieu Schaller's avatar
Matthieu Schaller committed
179
180
181
182
183
184
185
runner_iact_nonsym_1_vec_density(vector *r2, vector *dx, vector *dy, vector *dz,
                                 vector hi_inv, vector vix, vector viy,
                                 vector viz, float *Vjx, float *Vjy, float *Vjz,
                                 float *Mj, vector *rhoSum, vector *rho_dhSum,
                                 vector *wcountSum, vector *wcount_dhSum,
                                 vector *div_vSum, vector *curlvxSum,
                                 vector *curlvySum, vector *curlvzSum,
186
                                 mask_t mask) {
187

188
  vector r, ri, ui, wi, wi_dx;
189
190
191
192
193
  vector mj;
  vector dvx, dvy, dvz;
  vector vjx, vjy, vjz;
  vector dvdr;
  vector curlvrx, curlvry, curlvrz;
James Willis's avatar
James Willis committed
194

195
  /* Fill the vectors. */
196
197
198
199
  mj.v = vec_load(Mj);
  vjx.v = vec_load(Vjx);
  vjy.v = vec_load(Vjy);
  vjz.v = vec_load(Vjz);
200
201
202
203
204

  /* Get the radius and inverse radius. */
  ri = vec_reciprocal_sqrt(*r2);
  r.v = vec_mul(r2->v, ri.v);

205
  ui.v = vec_mul(r.v, hi_inv.v);
206
207

  /* Calculate the kernel for two particles. */
208
  kernel_deval_1_vec(&ui, &wi, &wi_dx);
209
210
211
212
213
214
215
216
217
218

  /* Compute dv. */
  dvx.v = vec_sub(vix.v, vjx.v);
  dvy.v = vec_sub(viy.v, vjy.v);
  dvz.v = vec_sub(viz.v, vjz.v);

  /* Compute dv dot r */
  dvdr.v = vec_fma(dvx.v, dx->v, vec_fma(dvy.v, dy->v, vec_mul(dvz.v, dz->v)));
  dvdr.v = vec_mul(dvdr.v, ri.v);

219
220
221
222
223
224
225
226
227
228
229
  /* Compute dv cross r */
  curlvrx.v =
      vec_fma(dvy.v, dz->v, vec_mul(vec_set1(-1.0f), vec_mul(dvz.v, dy->v)));
  curlvry.v =
      vec_fma(dvz.v, dx->v, vec_mul(vec_set1(-1.0f), vec_mul(dvx.v, dz->v)));
  curlvrz.v =
      vec_fma(dvx.v, dy->v, vec_mul(vec_set1(-1.0f), vec_mul(dvy.v, dx->v)));
  curlvrx.v = vec_mul(curlvrx.v, ri.v);
  curlvry.v = vec_mul(curlvry.v, ri.v);
  curlvrz.v = vec_mul(curlvrz.v, ri.v);

230
231
  vector wcount_dh_update;
  wcount_dh_update.v =
James Willis's avatar
James Willis committed
232
      vec_fma(vec_set1(hydro_dimension), wi.v, vec_mul(ui.v, wi_dx.v));
233

234
  /* Mask updates to intermediate vector sums for particle pi. */
235
  rhoSum->v = vec_mask_add(rhoSum->v, vec_mul(mj.v, wi.v), mask);
236
237
  rho_dhSum->v =
      vec_mask_sub(rho_dhSum->v, vec_mul(mj.v, wcount_dh_update.v), mask);
238
  wcountSum->v = vec_mask_add(wcountSum->v, wi.v, mask);
239
  wcount_dhSum->v = vec_mask_sub(wcount_dhSum->v, wcount_dh_update.v, mask);
James Willis's avatar
James Willis committed
240
241
242
243
244
245
246
247
  div_vSum->v =
      vec_mask_sub(div_vSum->v, vec_mul(mj.v, vec_mul(dvdr.v, wi_dx.v)), mask);
  curlvxSum->v = vec_mask_add(curlvxSum->v,
                              vec_mul(mj.v, vec_mul(curlvrx.v, wi_dx.v)), mask);
  curlvySum->v = vec_mask_add(curlvySum->v,
                              vec_mul(mj.v, vec_mul(curlvry.v, wi_dx.v)), mask);
  curlvzSum->v = vec_mask_add(curlvzSum->v,
                              vec_mul(mj.v, vec_mul(curlvrz.v, wi_dx.v)), mask);
248
249
}

250
/**
James Willis's avatar
James Willis committed
251
252
 * @brief Density interaction computed using 2 interleaved vectors
 * (non-symmetric vectorized version).
253
254
 */
__attribute__((always_inline)) INLINE static void
James Willis's avatar
James Willis committed
255
256
257
258
259
260
261
262
runner_iact_nonsym_2_vec_density(float *R2, float *Dx, float *Dy, float *Dz,
                                 vector hi_inv, vector vix, vector viy,
                                 vector viz, float *Vjx, float *Vjy, float *Vjz,
                                 float *Mj, vector *rhoSum, vector *rho_dhSum,
                                 vector *wcountSum, vector *wcount_dhSum,
                                 vector *div_vSum, vector *curlvxSum,
                                 vector *curlvySum, vector *curlvzSum,
                                 mask_t mask, mask_t mask2, short mask_cond) {
263

264
  vector r, ri, r2, ui, wi, wi_dx;
265
266
267
268
269
  vector mj;
  vector dx, dy, dz, dvx, dvy, dvz;
  vector vjx, vjy, vjz;
  vector dvdr;
  vector curlvrx, curlvry, curlvrz;
270
  vector r_2, ri2, r2_2, ui2, wi2, wi_dx2;
271
272
273
274
275
276
  vector mj2;
  vector dx2, dy2, dz2, dvx2, dvy2, dvz2;
  vector vjx2, vjy2, vjz2;
  vector dvdr2;
  vector curlvrx2, curlvry2, curlvrz2;

James Willis's avatar
James Willis committed
277
  /* Fill the vectors. */
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
  mj.v = vec_load(Mj);
  mj2.v = vec_load(&Mj[VEC_SIZE]);
  vjx.v = vec_load(Vjx);
  vjx2.v = vec_load(&Vjx[VEC_SIZE]);
  vjy.v = vec_load(Vjy);
  vjy2.v = vec_load(&Vjy[VEC_SIZE]);
  vjz.v = vec_load(Vjz);
  vjz2.v = vec_load(&Vjz[VEC_SIZE]);
  dx.v = vec_load(Dx);
  dx2.v = vec_load(&Dx[VEC_SIZE]);
  dy.v = vec_load(Dy);
  dy2.v = vec_load(&Dy[VEC_SIZE]);
  dz.v = vec_load(Dz);
  dz2.v = vec_load(&Dz[VEC_SIZE]);

  /* Get the radius and inverse radius. */
  r2.v = vec_load(R2);
  r2_2.v = vec_load(&R2[VEC_SIZE]);
296
297
  ri = vec_reciprocal_sqrt(r2);
  ri2 = vec_reciprocal_sqrt(r2_2);
298
299
300
  r.v = vec_mul(r2.v, ri.v);
  r_2.v = vec_mul(r2_2.v, ri2.v);

301
302
  ui.v = vec_mul(r.v, hi_inv.v);
  ui2.v = vec_mul(r_2.v, hi_inv.v);
303

James Willis's avatar
James Willis committed
304
  /* Calculate the kernel for two particles. */
305
  kernel_deval_2_vec(&ui, &wi, &wi_dx, &ui2, &wi2, &wi_dx2);
306
307
308
309
310
311
312
313
314
315
316

  /* Compute dv. */
  dvx.v = vec_sub(vix.v, vjx.v);
  dvx2.v = vec_sub(vix.v, vjx2.v);
  dvy.v = vec_sub(viy.v, vjy.v);
  dvy2.v = vec_sub(viy.v, vjy2.v);
  dvz.v = vec_sub(viz.v, vjz.v);
  dvz2.v = vec_sub(viz.v, vjz2.v);

  /* Compute dv dot r */
  dvdr.v = vec_fma(dvx.v, dx.v, vec_fma(dvy.v, dy.v, vec_mul(dvz.v, dz.v)));
James Willis's avatar
James Willis committed
317
318
  dvdr2.v =
      vec_fma(dvx2.v, dx2.v, vec_fma(dvy2.v, dy2.v, vec_mul(dvz2.v, dz2.v)));
319
320
321
322
  dvdr.v = vec_mul(dvdr.v, ri.v);
  dvdr2.v = vec_mul(dvdr2.v, ri2.v);

  /* Compute dv cross r */
James Willis's avatar
James Willis committed
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
  curlvrx.v =
      vec_fma(dvy.v, dz.v, vec_mul(vec_set1(-1.0f), vec_mul(dvz.v, dy.v)));
  curlvrx2.v =
      vec_fma(dvy2.v, dz2.v, vec_mul(vec_set1(-1.0f), vec_mul(dvz2.v, dy2.v)));
  curlvry.v =
      vec_fma(dvz.v, dx.v, vec_mul(vec_set1(-1.0f), vec_mul(dvx.v, dz.v)));
  curlvry2.v =
      vec_fma(dvz2.v, dx2.v, vec_mul(vec_set1(-1.0f), vec_mul(dvx2.v, dz2.v)));
  curlvrz.v =
      vec_fma(dvx.v, dy.v, vec_mul(vec_set1(-1.0f), vec_mul(dvy.v, dx.v)));
  curlvrz2.v =
      vec_fma(dvx2.v, dy2.v, vec_mul(vec_set1(-1.0f), vec_mul(dvy2.v, dx2.v)));
  curlvrx.v = vec_mul(curlvrx.v, ri.v);
  curlvrx2.v = vec_mul(curlvrx2.v, ri2.v);
  curlvry.v = vec_mul(curlvry.v, ri.v);
  curlvry2.v = vec_mul(curlvry2.v, ri2.v);
  curlvrz.v = vec_mul(curlvrz.v, ri.v);
  curlvrz2.v = vec_mul(curlvrz2.v, ri2.v);

342
343
  vector wcount_dh_update, wcount_dh_update2;
  wcount_dh_update.v =
James Willis's avatar
James Willis committed
344
      vec_fma(vec_set1(hydro_dimension), wi.v, vec_mul(ui.v, wi_dx.v));
345
  wcount_dh_update2.v =
James Willis's avatar
James Willis committed
346
      vec_fma(vec_set1(hydro_dimension), wi2.v, vec_mul(ui2.v, wi_dx2.v));
347

James Willis's avatar
James Willis committed
348
  /* Mask updates to intermediate vector sums for particle pi. */
349
  /* Mask only when needed. */
James Willis's avatar
James Willis committed
350
  if (mask_cond) {
351
352
    rhoSum->v = vec_mask_add(rhoSum->v, vec_mul(mj.v, wi.v), mask);
    rhoSum->v = vec_mask_add(rhoSum->v, vec_mul(mj2.v, wi2.v), mask2);
James Willis's avatar
James Willis committed
353
    rho_dhSum->v =
354
        vec_mask_sub(rho_dhSum->v, vec_mul(mj.v, wcount_dh_update.v), mask);
James Willis's avatar
James Willis committed
355
    rho_dhSum->v =
356
        vec_mask_sub(rho_dhSum->v, vec_mul(mj2.v, wcount_dh_update2.v), mask2);
357
358
    wcountSum->v = vec_mask_add(wcountSum->v, wi.v, mask);
    wcountSum->v = vec_mask_add(wcountSum->v, wi2.v, mask2);
359
360
    wcount_dhSum->v = vec_mask_sub(wcount_dhSum->v, wcount_dh_update.v, mask);
    wcount_dhSum->v = vec_mask_sub(wcount_dhSum->v, wcount_dh_update2.v, mask2);
James Willis's avatar
James Willis committed
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
    div_vSum->v = vec_mask_sub(div_vSum->v,
                               vec_mul(mj.v, vec_mul(dvdr.v, wi_dx.v)), mask);
    div_vSum->v = vec_mask_sub(
        div_vSum->v, vec_mul(mj2.v, vec_mul(dvdr2.v, wi_dx2.v)), mask2);
    curlvxSum->v = vec_mask_add(
        curlvxSum->v, vec_mul(mj.v, vec_mul(curlvrx.v, wi_dx.v)), mask);
    curlvxSum->v = vec_mask_add(
        curlvxSum->v, vec_mul(mj2.v, vec_mul(curlvrx2.v, wi_dx2.v)), mask2);
    curlvySum->v = vec_mask_add(
        curlvySum->v, vec_mul(mj.v, vec_mul(curlvry.v, wi_dx.v)), mask);
    curlvySum->v = vec_mask_add(
        curlvySum->v, vec_mul(mj2.v, vec_mul(curlvry2.v, wi_dx2.v)), mask2);
    curlvzSum->v = vec_mask_add(
        curlvzSum->v, vec_mul(mj.v, vec_mul(curlvrz.v, wi_dx.v)), mask);
    curlvzSum->v = vec_mask_add(
        curlvzSum->v, vec_mul(mj2.v, vec_mul(curlvrz2.v, wi_dx2.v)), mask2);
  } else {
378
379
    rhoSum->v = vec_add(rhoSum->v, vec_mul(mj.v, wi.v));
    rhoSum->v = vec_add(rhoSum->v, vec_mul(mj2.v, wi2.v));
380
381
    rho_dhSum->v = vec_sub(rho_dhSum->v, vec_mul(mj.v, wcount_dh_update.v));
    rho_dhSum->v = vec_sub(rho_dhSum->v, vec_mul(mj2.v, wcount_dh_update2.v));
382
383
    wcountSum->v = vec_add(wcountSum->v, wi.v);
    wcountSum->v = vec_add(wcountSum->v, wi2.v);
384
385
    wcount_dhSum->v = vec_sub(wcount_dhSum->v, wcount_dh_update.v);
    wcount_dhSum->v = vec_sub(wcount_dhSum->v, wcount_dh_update2.v);
386
    div_vSum->v = vec_sub(div_vSum->v, vec_mul(mj.v, vec_mul(dvdr.v, wi_dx.v)));
James Willis's avatar
James Willis committed
387
388
389
390
391
392
393
394
395
396
397
398
399
400
    div_vSum->v =
        vec_sub(div_vSum->v, vec_mul(mj2.v, vec_mul(dvdr2.v, wi_dx2.v)));
    curlvxSum->v =
        vec_add(curlvxSum->v, vec_mul(mj.v, vec_mul(curlvrx.v, wi_dx.v)));
    curlvxSum->v =
        vec_add(curlvxSum->v, vec_mul(mj2.v, vec_mul(curlvrx2.v, wi_dx2.v)));
    curlvySum->v =
        vec_add(curlvySum->v, vec_mul(mj.v, vec_mul(curlvry.v, wi_dx.v)));
    curlvySum->v =
        vec_add(curlvySum->v, vec_mul(mj2.v, vec_mul(curlvry2.v, wi_dx2.v)));
    curlvzSum->v =
        vec_add(curlvzSum->v, vec_mul(mj.v, vec_mul(curlvrz.v, wi_dx.v)));
    curlvzSum->v =
        vec_add(curlvzSum->v, vec_mul(mj2.v, vec_mul(curlvrz2.v, wi_dx2.v)));
401
  }
402
}
James Willis's avatar
James Willis committed
403
#endif
404

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

411
412
413
  float wi, wj, wi_dx, wj_dx;

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

415
416
417
418
  const float r = sqrtf(r2);
  const float r_inv = 1.0f / r;

  /* Get some values in local variables. */
419
  const float mi = pi->mass;
420
421
422
423
424
425
  const float mj = pj->mass;
  const float rhoi = pi->rho;
  const float rhoj = pj->rho;

  /* Get the kernel for hi. */
  const float hi_inv = 1.0f / hi;
426
  const float hid_inv = pow_dimension_plus_one(hi_inv); /* 1/h^(d+1) */
427
428
  const float ui = r * hi_inv;
  kernel_deval(ui, &wi, &wi_dx);
429
  const float wi_dr = hid_inv * wi_dx;
430
431
432

  /* Get the kernel for hj. */
  const float hj_inv = 1.0f / hj;
433
  const float hjd_inv = pow_dimension_plus_one(hj_inv); /* 1/h^(d+1) */
434
435
  const float xj = r * hj_inv;
  kernel_deval(xj, &wj, &wj_dx);
436
  const float wj_dr = hjd_inv * wj_dx;
437

438
439
440
441
442
  /* Compute h-gradient terms */
  const float f_i = pi->force.f;
  const float f_j = pj->force.f;

  /* Compute pressure terms */
443
444
  const float P_over_rho2_i = pi->force.P_over_rho2;
  const float P_over_rho2_j = pj->force.P_over_rho2;
445
446

  /* Compute sound speeds */
447
448
  const float ci = pi->force.soundspeed;
  const float cj = pj->force.soundspeed;
449

450
  /* Compute dv dot r. */
451
452
453
  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];
454

455
  /* Balsara term */
456
457
  const float balsara_i = pi->force.balsara;
  const float balsara_j = pj->force.balsara;
Matthieu Schaller's avatar
Matthieu Schaller committed
458

459
  /* Are the particles moving towards each others ? */
460
  const float omega_ij = (dvdr < 0.f) ? dvdr : 0.f;
461
462
463
464
465
466
467
468
469
  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;
470
471

  /* Now, convolve with the kernel */
472
  const float visc_term = 0.5f * visc * (wi_dr + wj_dr) * r_inv;
473
  const float sph_term =
474
      (f_i * P_over_rho2_i * wi_dr + f_j * P_over_rho2_j * wj_dr) * r_inv;
475
476
477
478
479

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

  /* Use the force Luke ! */
480
481
482
  pi->a_hydro[0] -= mj * acc * dx[0];
  pi->a_hydro[1] -= mj * acc * dx[1];
  pi->a_hydro[2] -= mj * acc * dx[2];
483

484
485
486
  pj->a_hydro[0] += mi * acc * dx[0];
  pj->a_hydro[1] += mi * acc * dx[1];
  pj->a_hydro[2] += mi * acc * dx[2];
487

488
  /* Get the time derivative for h. */
489
490
  pi->force.h_dt -= mj * dvdr * r_inv / rhoj * wi_dr;
  pj->force.h_dt -= mi * dvdr * r_inv / rhoi * wj_dr;
491

492
  /* Update the signal velocity. */
493
494
  pi->force.v_sig = (pi->force.v_sig > v_sig) ? pi->force.v_sig : v_sig;
  pj->force.v_sig = (pj->force.v_sig > v_sig) ? pj->force.v_sig : v_sig;
495

496
  /* Change in entropy */
497
498
  pi->entropy_dt += mj * visc_term * dvdr;
  pj->entropy_dt += mi * visc_term * dvdr;
499
  
500
#ifdef DEBUG_INTERACTIONS_SPH
501
  /* Update ngb counters */
502
  pi->ids_ngbs_force[pi->num_ngb_force] = pj->id;
503
  ++pi->num_ngb_force;
504
  pj->ids_ngbs_force[pj->num_ngb_force] = pi->id;
505
506
507
  ++pj->num_ngb_force;
#endif

508
}
509
510
511
512

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

516
517
518
  float wi, wj, wi_dx, wj_dx;

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

520
521
522
523
  const float r = sqrtf(r2);
  const float r_inv = 1.0f / r;

  /* Get some values in local variables. */
524
  // const float mi = pi->mass;
525
526
527
528
529
530
  const float mj = pj->mass;
  const float rhoi = pi->rho;
  const float rhoj = pj->rho;

  /* Get the kernel for hi. */
  const float hi_inv = 1.0f / hi;
531
  const float hid_inv = pow_dimension_plus_one(hi_inv); /* 1/h^(d+1) */
532
533
  const float ui = r * hi_inv;
  kernel_deval(ui, &wi, &wi_dx);
534
  const float wi_dr = hid_inv * wi_dx;
535
536
537

  /* Get the kernel for hj. */
  const float hj_inv = 1.0f / hj;
538
  const float hjd_inv = pow_dimension_plus_one(hj_inv); /* 1/h^(d+1) */
539
540
  const float xj = r * hj_inv;
  kernel_deval(xj, &wj, &wj_dx);
541
  const float wj_dr = hjd_inv * wj_dx;
542

543
544
545
546
547
  /* Compute h-gradient terms */
  const float f_i = pi->force.f;
  const float f_j = pj->force.f;

  /* Compute pressure terms */
548
549
  const float P_over_rho2_i = pi->force.P_over_rho2;
  const float P_over_rho2_j = pj->force.P_over_rho2;
550
551

  /* Compute sound speeds */
552
553
  const float ci = pi->force.soundspeed;
  const float cj = pj->force.soundspeed;
554

555
  /* Compute dv dot r. */
556
557
558
  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];
559

560
  /* Balsara term */
561
562
  const float balsara_i = pi->force.balsara;
  const float balsara_j = pj->force.balsara;
563
564

  /* Are the particles moving towards each others ? */
565
  const float omega_ij = (dvdr < 0.f) ? dvdr : 0.f;
566
567
568
569
570
571
572
573
574
  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;
575
576

  /* Now, convolve with the kernel */
577
  const float visc_term = 0.5f * visc * (wi_dr + wj_dr) * r_inv;
578
  const float sph_term =
579
      (f_i * P_over_rho2_i * wi_dr + f_j * P_over_rho2_j * wj_dr) * r_inv;
580
581
582

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

584
  /* Use the force Luke ! */
585
586
587
  pi->a_hydro[0] -= mj * acc * dx[0];
  pi->a_hydro[1] -= mj * acc * dx[1];
  pi->a_hydro[2] -= mj * acc * dx[2];
588

589
  /* Get the time derivative for h. */
590
  pi->force.h_dt -= mj * dvdr * r_inv / rhoj * wi_dr;
591

592
  /* Update the signal velocity. */
593
  pi->force.v_sig = (pi->force.v_sig > v_sig) ? pi->force.v_sig : v_sig;
594

595
  /* Change in entropy */
596
  pi->entropy_dt += mj * visc_term * dvdr;
597
  
598
#ifdef DEBUG_INTERACTIONS_SPH
599
  /* Update ngb counters */
600
  pi->ids_ngbs_force[pi->num_ngb_force] = pj->id;
601
602
603
  ++pi->num_ngb_force;
#endif

604
}
605

606
#ifdef WITH_VECTORIZATION
James Willis's avatar
James Willis committed
607
608
static const vector const_viscosity_alpha_fac =
    FILL_VEC(-0.25f * const_viscosity_alpha);
609

James Willis's avatar
James Willis committed
610
611
612
613
/**
 * @brief Force interaction computed using 1 vector
 * (non-symmetric vectorized version).
 */
James Willis's avatar
James Willis committed
614
615
__attribute__((always_inline)) INLINE static void
runner_iact_nonsym_1_vec_force(
616
    vector *r2, vector *dx, vector *dy, vector *dz, vector vix, vector viy,
James Willis's avatar
James Willis committed
617
618
619
    vector viz, vector pirho, vector grad_hi, vector piPOrho2, vector balsara_i,
    vector ci, float *Vjx, float *Vjy, float *Vjz, float *Pjrho, float *Grad_hj,
    float *PjPOrho2, float *Balsara_j, float *Cj, float *Mj, vector hi_inv,
620
    vector hj_inv, vector *a_hydro_xSum, vector *a_hydro_ySum,
James Willis's avatar
James Willis committed
621
622
    vector *a_hydro_zSum, vector *h_dtSum, vector *v_sigSum,
    vector *entropy_dtSum, mask_t mask) {
623
624
625

#ifdef WITH_VECTORIZATION

626
  vector r, ri;
627
  vector vjx, vjy, vjz, dvx, dvy, dvz;
628
  vector pjrho, grad_hj, pjPOrho2, balsara_j, cj, mj;
629
630
  vector xi, xj;
  vector hid_inv, hjd_inv;
631
  vector wi_dx, wj_dx, wi_dr, wj_dr, dvdr;
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
  vector piax, piay, piaz;
  vector pih_dt;
  vector v_sig;
  vector omega_ij, mu_ij, fac_mu, balsara;
  vector rho_ij, visc, visc_term, sph_term, acc, entropy_dt;

  /* Fill vectors. */
  vjx.v = vec_load(Vjx);
  vjy.v = vec_load(Vjy);
  vjz.v = vec_load(Vjz);
  mj.v = vec_load(Mj);

  pjrho.v = vec_load(Pjrho);
  grad_hj.v = vec_load(Grad_hj);
  pjPOrho2.v = vec_load(PjPOrho2);
  balsara_j.v = vec_load(Balsara_j);
  cj.v = vec_load(Cj);

  fac_mu.v = vec_set1(1.f); /* Will change with cosmological integration */

James Willis's avatar
James Willis committed
652
  /* Load stuff. */
653
  balsara.v = vec_add(balsara_i.v, balsara_j.v);
654
655

  /* Get the radius and inverse radius. */
656
  ri = vec_reciprocal_sqrt(*r2);
657
  r.v = vec_mul(r2->v, ri.v);
658
659

  /* Get the kernel for hi. */
660
  hid_inv = pow_dimension_plus_one_vec(hi_inv);
661
  xi.v = vec_mul(r.v, hi_inv.v);
662
  kernel_eval_dWdx_force_vec(&xi, &wi_dx);
663
  wi_dr.v = vec_mul(hid_inv.v, wi_dx.v);
664
665
666

  /* Get the kernel for hj. */
  hjd_inv = pow_dimension_plus_one_vec(hj_inv);
667
  xj.v = vec_mul(r.v, hj_inv.v);
James Willis's avatar
James Willis committed
668

669
  /* Calculate the kernel. */
James Willis's avatar
James Willis committed
670
  kernel_eval_dWdx_force_vec(&xj, &wj_dx);
James Willis's avatar
James Willis committed
671

672
673
674
675
676
677
  wj_dr.v = vec_mul(hjd_inv.v, wj_dx.v);

  /* Compute dv. */
  dvx.v = vec_sub(vix.v, vjx.v);
  dvy.v = vec_sub(viy.v, vjy.v);
  dvz.v = vec_sub(viz.v, vjz.v);
678
679

  /* Compute dv dot r. */
680
  dvdr.v = vec_fma(dvx.v, dx->v, vec_fma(dvy.v, dy->v, vec_mul(dvz.v, dz->v)));
681
682
683

  /* Compute the relative velocity. (This is 0 if the particles move away from
   * each other and negative otherwise) */
684
  omega_ij.v = vec_fmin(dvdr.v, vec_setzero());
James Willis's avatar
James Willis committed
685
686
  mu_ij.v =
      vec_mul(fac_mu.v, vec_mul(ri.v, omega_ij.v)); /* This is 0 or negative */
687
688

  /* Compute signal velocity */
689
  v_sig.v = vec_fnma(vec_set1(3.f), mu_ij.v, vec_add(ci.v, cj.v));
690
691

  /* Now construct the full viscosity term */
692
  rho_ij.v = vec_mul(vec_set1(0.5f), vec_add(pirho.v, pjrho.v));
James Willis's avatar
James Willis committed
693
694
695
  visc.v = vec_div(vec_mul(const_viscosity_alpha_fac.v,
                           vec_mul(v_sig.v, vec_mul(mu_ij.v, balsara.v))),
                   rho_ij.v);
696
697

  /* Now, convolve with the kernel */
James Willis's avatar
James Willis committed
698
699
700
  visc_term.v =
      vec_mul(vec_set1(0.5f),
              vec_mul(visc.v, vec_mul(vec_add(wi_dr.v, wj_dr.v), ri.v)));
James Willis's avatar
James Willis committed
701

702
  sph_term.v =
James Willis's avatar
James Willis committed
703
704
705
      vec_mul(vec_fma(vec_mul(grad_hi.v, piPOrho2.v), wi_dr.v,
                      vec_mul(grad_hj.v, vec_mul(pjPOrho2.v, wj_dr.v))),
              ri.v);
James Willis's avatar
James Willis committed
706

707
  /* Eventually get the acceleration */
708
  acc.v = vec_add(visc_term.v, sph_term.v);
709
710

  /* Use the force, Luke! */
711
712
713
  piax.v = vec_mul(mj.v, vec_mul(dx->v, acc.v));
  piay.v = vec_mul(mj.v, vec_mul(dy->v, acc.v));
  piaz.v = vec_mul(mj.v, vec_mul(dz->v, acc.v));
714
715

  /* Get the time derivative for h. */
James Willis's avatar
James Willis committed
716
717
  pih_dt.v =
      vec_div(vec_mul(mj.v, vec_mul(dvdr.v, vec_mul(ri.v, wi_dr.v))), pjrho.v);
718
719

  /* Change in entropy */
720
  entropy_dt.v = vec_mul(mj.v, vec_mul(visc_term.v, dvdr.v));
721

722
  /* Store the forces back on the particles. */
723
724
725
726
  a_hydro_xSum->v = vec_mask_sub(a_hydro_xSum->v, piax.v, mask);
  a_hydro_ySum->v = vec_mask_sub(a_hydro_ySum->v, piay.v, mask);
  a_hydro_zSum->v = vec_mask_sub(a_hydro_zSum->v, piaz.v, mask);
  h_dtSum->v = vec_mask_sub(h_dtSum->v, pih_dt.v, mask);
727
  v_sigSum->v = vec_fmax(v_sigSum->v, vec_and_mask(v_sig.v, mask));
728
  entropy_dtSum->v = vec_mask_add(entropy_dtSum->v, entropy_dt.v, mask);
729
730
731
732
733
734
735
736
737
738

#else

  error(
      "The Gadget2 serial version of runner_iact_nonsym_force was called when "
      "the vectorised version should have been used.");

#endif
}

James Willis's avatar
James Willis committed
739
740
741
742
/**
 * @brief Force interaction computed using 2 interleaved vectors
 * (non-symmetric vectorized version).
 */
James Willis's avatar
James Willis committed
743
744
__attribute__((always_inline)) INLINE static void
runner_iact_nonsym_2_vec_force(
745
    float *R2, float *Dx, float *Dy, float *Dz, vector vix, vector viy,
James Willis's avatar
James Willis committed
746
747
748
749
750
751
    vector viz, vector pirho, vector grad_hi, vector piPOrho2, vector balsara_i,
    vector ci, float *Vjx, float *Vjy, float *Vjz, float *Pjrho, float *Grad_hj,
    float *PjPOrho2, float *Balsara_j, float *Cj, float *Mj, vector hi_inv,
    float *Hj_inv, vector *a_hydro_xSum, vector *a_hydro_ySum,
    vector *a_hydro_zSum, vector *h_dtSum, vector *v_sigSum,
    vector *entropy_dtSum, mask_t mask, mask_t mask_2, short mask_cond) {
752
753
754
755

#ifdef WITH_VECTORIZATION

  vector r, r2, ri;
756
  vector dx, dy, dz, dvx, dvy, dvz;
757
758
  vector vjx, vjy, vjz;
  vector pjrho, grad_hj, pjPOrho2, balsara_j, cj, mj, hj_inv;
759
  vector ui, uj;
760
  vector hid_inv, hjd_inv;
761
  vector wi_dx, wj_dx, wi_dr, wj_dr, dvdr;
762
763
764
765
766
767
768
  vector piax, piay, piaz;
  vector pih_dt;
  vector v_sig;
  vector omega_ij, mu_ij, fac_mu, balsara;
  vector rho_ij, visc, visc_term, sph_term, acc, entropy_dt;

  vector r_2, r2_2, ri_2;
769
  vector dx_2, dy_2, dz_2, dvx_2, dvy_2, dvz_2;
770
771
  vector vjx_2, vjy_2, vjz_2;
  vector pjrho_2, grad_hj_2, pjPOrho2_2, balsara_j_2, cj_2, mj_2, hj_inv_2;
772
  vector ui_2, uj_2;
773
  vector hjd_inv_2;
774
  vector wi_dx_2, wj_dx_2, wi_dr_2, wj_dr_2, dvdr_2;
775
776
777
778
779
780
781
  vector piax_2, piay_2, piaz_2;
  vector pih_dt_2;
  vector v_sig_2;
  vector omega_ij_2, mu_ij_2, balsara_2;
  vector rho_ij_2, visc_2, visc_term_2, sph_term_2, acc_2, entropy_dt_2;

  /* Fill vectors. */
782
783
  mj.v = vec_load(Mj);
  mj_2.v = vec_load(&Mj[VEC_SIZE]);
784
  vjx.v = vec_load(Vjx);
785
  vjx_2.v = vec_load(&Vjx[VEC_SIZE]);
786
  vjy.v = vec_load(Vjy);
787
  vjy_2.v = vec_load(&Vjy[VEC_SIZE]);
788
  vjz.v = vec_load(Vjz);
789
790
  vjz_2.v = vec_load(&Vjz[VEC_SIZE]);
  dx.v = vec_load(Dx);
791
  dx_2.v = vec_load(&Dx[VEC_SIZE]);
792
  dy.v = vec_load(Dy);
793
  dy_2.v = vec_load(&Dy[VEC_SIZE]);
794
  dz.v = vec_load(Dz);
795
  dz_2.v = vec_load(&Dz[VEC_SIZE]);
James Willis's avatar
James Willis committed
796

797
798
799
800
801
802
803
  /* Get the radius and inverse radius. */
  r2.v = vec_load(R2);
  r2_2.v = vec_load(&R2[VEC_SIZE]);
  ri = vec_reciprocal_sqrt(r2);
  ri_2 = vec_reciprocal_sqrt(r2_2);
  r.v = vec_mul(r2.v, ri.v);
  r_2.v = vec_mul(r2_2.v, ri_2.v);
804

805
806
  /* Get remaining properties. */
  pjrho.v = vec_load(Pjrho);
807
  pjrho_2.v = vec_load(&Pjrho[VEC_SIZE]);
808
  grad_hj.v = vec_load(Grad_hj);
809
  grad_hj_2.v = vec_load(&Grad_hj[VEC_SIZE]);
810
  pjPOrho2.v = vec_load(PjPOrho2);
811
  pjPOrho2_2.v = vec_load(&PjPOrho2[VEC_SIZE]);
812
  balsara_j.v = vec_load(Balsara_j);
813
  balsara_j_2.v = vec_load(&Balsara_j[VEC_SIZE]);
814
  cj.v = vec_load(Cj);
815
  cj_2.v = vec_load(&Cj[VEC_SIZE]);
816
  hj_inv.v = vec_load(Hj_inv);
817
818
  hj_inv_2.v = vec_load(&Hj_inv[VEC_SIZE]);

819
  fac_mu.v = vec_set1(1.f); /* Will change with cosmological integration */
820

821
822
823
  /* Find the balsara switch. */
  balsara.v = vec_add(balsara_i.v, balsara_j.v);
  balsara_2.v = vec_add(balsara_i.v, balsara_j_2.v);
824
825

  /* Get the kernel for hi. */
826
  hid_inv = pow_dimension_plus_one_vec(hi_inv);
827
828
829
830
831
832
  ui.v = vec_mul(r.v, hi_inv.v);
  ui_2.v = vec_mul(r_2.v, hi_inv.v);
  kernel_eval_dWdx_force_vec(&ui, &wi_dx);
  kernel_eval_dWdx_force_vec(&ui_2, &wi_dx_2);
  wi_dr.v = vec_mul(hid_inv.v, wi_dx.v);
  wi_dr_2.v = vec_mul(hid_inv.v, wi_dx_2.v);
833
834
835
836

  /* Get the kernel for hj. */
  hjd_inv = pow_dimension_plus_one_vec(hj_inv);
  hjd_inv_2 = pow_dimension_plus_one_vec(hj_inv_2);
837
838
  uj.v = vec_mul(r.v, hj_inv.v);
  uj_2.v = vec_mul(r_2.v, hj_inv_2.v);
James Willis's avatar
James Willis committed
839

840
  /* Calculate the kernel for two particles. */
841
842
  kernel_eval_dWdx_force_vec(&uj, &wj_dx);
  kernel_eval_dWdx_force_vec(&uj_2, &wj_dx_2);
James Willis's avatar
James Willis committed
843

844
845
  wj_dr.v = vec_mul(hjd_inv.v, wj_dx.v);
  wj_dr_2.v = vec_mul(hjd_inv_2.v, wj_dx_2.v);
846

847
848
849
850
851
852
853
  /* Compute dv. */
  dvx.v = vec_sub(vix.v, vjx.v);
  dvx_2.v = vec_sub(vix.v, vjx_2.v);
  dvy.v = vec_sub(viy.v, vjy.v);
  dvy_2.v = vec_sub(viy.v, vjy_2.v);
  dvz.v = vec_sub(viz.v, vjz.v);
  dvz_2.v = vec_sub(viz.v, vjz_2.v);
854
855

  /* Compute dv dot r. */
856
  dvdr.v = vec_fma(dvx.v, dx.v, vec_fma(dvy.v, dy.v, vec_mul(dvz.v, dz.v)));
James Willis's avatar
James Willis committed
857
858
  dvdr_2.v = vec_fma(dvx_2.v, dx_2.v,
                     vec_fma(dvy_2.v, dy_2.v, vec_mul(dvz_2.v, dz_2.v)));
859
860
861
862
863

  /* Compute the relative velocity. (This is 0 if the particles move away from
   * each other and negative otherwise) */
  omega_ij.v = vec_fmin(dvdr.v, vec_setzero());
  omega_ij_2.v = vec_fmin(dvdr_2.v, vec_setzero());
James Willis's avatar
James Willis committed
864
865
866
867
  mu_ij.v =
      vec_mul(fac_mu.v, vec_mul(ri.v, omega_ij.v)); /* This is 0 or negative */
  mu_ij_2.v = vec_mul(
      fac_mu.v, vec_mul(ri_2.v, omega_ij_2.v)); /* This is 0 or negative */
868
869

  /* Compute signal velocity */
870
871
  v_sig.v = vec_fnma(vec_set1(3.f), mu_ij.v, vec_add(ci.v, cj.v));
  v_sig_2.v = vec_fnma(vec_set1(3.f), mu_ij_2.v, vec_add(ci.v, cj_2.v));
872
873

  /* Now construct the full viscosity term */
874
875
876
  rho_ij.v = vec_mul(vec_set1(0.5f), vec_add(pirho.v, pjrho.v));
  rho_ij_2.v = vec_mul(vec_set1(0.5f), vec_add(pirho.v, pjrho_2.v));

James Willis's avatar
James Willis committed
877
878
879
880
881
882
883
  visc.v = vec_div(vec_mul(const_viscosity_alpha_fac.v,
                           vec_mul(v_sig.v, vec_mul(mu_ij.v, balsara.v))),
                   rho_ij.v);
  visc_2.v =
      vec_div(vec_mul(const_viscosity_alpha_fac.v,
                      vec_mul(v_sig_2.v, vec_mul(mu_ij_2.v, balsara_2.v))),
              rho_ij_2.v);
884
885

  /* Now, convolve with the kernel */
James Willis's avatar
James Willis committed
886
887
888
889
890
891
892
  visc_term.v =
      vec_mul(vec_set1(0.5f),
              vec_mul(visc.v, vec_mul(vec_add(wi_dr.v, wj_dr.v), ri.v)));
  visc_term_2.v = vec_mul(
      vec_set1(0.5f),
      vec_mul(visc_2.v, vec_mul(vec_add(wi_dr_2.v, wj_dr_2.v), ri_2.v)));

893
894
895
  vector grad_hi_mul_piPOrho2;
  grad_hi_mul_piPOrho2.v = vec_mul(grad_hi.v, piPOrho2.v);