hydro_iact.h 10.6 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
/* Includes. */
24
#include "const.h"
25
#include "kernel.h"
26
#include "part.h"
27
28
#include "vector.h"

29

30
31
32
/**
 * @brief SPH interaction functions following the Gadget-2 version of SPH.
 *
33
34
35
36
37
38
 * 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
39
40
 * missed by the Gadget-2 tree-code neighbours search.
 *
41
42
 * The code uses internal energy instead of entropy as a thermodynamical
 *variable.
43
44
45
46
47
48
 */

/**
 * @brief Density loop
 */

49
50
51
__attribute__((always_inline)) INLINE static void runner_iact_density(
    float r2, float *dx, float hi, float hj, struct part *pi, struct part *pj) {

52
53
  float wi, wi_dx;
  float wj, wj_dx;
54
  float dv[3], curlvr[3];
55

56
  /* Get the masses. */
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  const float mi = pj->mass;
  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;
  pi->rho_dh -= mj * kernel_igamma * (3.f * wi + ui * wi_dx);
  
  /* 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;
  pj->rho_dh -= mi * kernel_igamma * (3.f * wj + uj * wj_dx);
  
  /* Compute contribution to the number of neighbours */
  pj->density.wcount += wj;
  pj->density.wcount_dh -= uj * wj_dx;
90

91
92
93
94
  
  const float faci = mj * wi_dx * r_inv;
  const float facj = mi * wj_dx * r_inv;
  
95
96
97
98
  /* 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];
99
100
101
102
  const float dvdr = dv[0] * dx[0] + dv[1] * dx[1] + dv[2] * dx[2];

  pi->div_v -= faci * dvdr;
  pj->div_v -= facj * dvdr;
103
104
105
106
107
108

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

109
110
111
  pi->rot_v[0] += faci * curlvr[0];
  pi->rot_v[1] += faci * curlvr[1];
  pi->rot_v[2] += faci * curlvr[2];
112

113
114
115
  pj->rot_v[0] += facj * curlvr[0];
  pj->rot_v[1] += facj * curlvr[1];
  pj->rot_v[2] += facj * curlvr[2];
116

117
118
  /* if(pi->id == 1000) */
  /*   message("Interacting with %lld. r=%f\n", pj->id, r); */
119

120
121
  /* if(pj->id == 1000) */
  /*   message("Interacting with %lld. r=%f\n", pi->id, r); */
122
123
124

}

125
126
127
128
129

/**
 * @brief Density loop (non-symmetric version)
 */

130
131
132
133
134
135
136
__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. */
137
  const float mj = pj->mass;
138
139

  /* Get r and r inverse. */
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
  const float r = sqrtf(r2);
  const float ri = 1.0f / r;

  if(pi->id == 1000 && pj->id == 1103) hi = 0.2234976 / 2.f;
  
  /* 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;
  pi->rho_dh -= mj * kernel_igamma * (3.f * wi + u * wi_dx);

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

158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
  const float ih3 = h_inv * h_inv * h_inv;
  const float ih4 = h_inv * h_inv * h_inv * h_inv;
  
  if(pi->id == 1000 && pj->id == 1103)
    message("Interacting with %lld. r=%e hi=%e u=%e W=%e dW/dx=%e dh_drho1=%e dh_drho2=%e %e\n",
	    pj->id,
	    r,
	    hi,
	    u,
	    wi * ih3,
	    wi_dx * ih4,
	    -mj * (3.f * kernel_igamma * wi) * ih4,
	    -mj * u * wi_dx * kernel_igamma * ih4,
	    kernel_igamma
	    );

  const float fac = mj * wi_dx * ri;
  
176
177
178
179
  /* 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];
180
181
  const float dvdr = dv[0] * dx[0] + dv[1] * dx[1] + dv[2] * dx[2];
  pi->div_v -= fac * dvdr;
182
183
184
185
186
187

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

188
189
190
  pi->rot_v[0] += fac * curlvr[0];
  pi->rot_v[1] += fac * curlvr[1];
  pi->rot_v[2] += fac * curlvr[2];  
191
192
}

193
194
195
196
197

/**
 * @brief Force loop
 */

198
199
200
__attribute__((always_inline)) INLINE static void runner_iact_force(
    float r2, float *dx, float hi, float hj, struct part *pi, struct part *pj) {

201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
  /* float r = sqrtf(r2), ri = 1.0f / r; */
  /* float xi, xj; */
  /* float hi_inv, hi2_inv; */
  /* float hj_inv, hj2_inv; */
  /* float wi, wj, wi_dx, wj_dx, wi_dr, wj_dr, w, dvdr; */
  /* float mi, mj, POrho2i, POrho2j, rhoi, rhoj; */
  /* float v_sig, omega_ij, Pi_ij; */
  /* float f; */
  /* int k; */

  /* /\* Get some values in local variables. *\/ */
  /* mi = pi->mass; */
  /* mj = pj->mass; */
  /* rhoi = pi->rho; */
  /* rhoj = pj->rho; */
  /* //POrho2i = pi->force.POrho2; */
  /* //POrho2j = pj->force.POrho2; */

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

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

  /* /\* Compute dv dot r. *\/ */
  /* 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]; */
  /* dvdr *= ri; */

  /* /\* Compute the relative velocity. (This is 0 if the particles move away from */
  /*  * each other and negative otherwise) *\/ */
  /* omega_ij = fminf(dvdr, 0.f); */

  /* /\* Compute signal velocity *\/ */
  /* v_sig = pi->force.c + pj->force.c - 3.0f * omega_ij; */

  /* /\* Compute viscosity tensor *\/ */
  /* Pi_ij = -const_viscosity_alpha * v_sig * omega_ij / (rhoi + rhoj); */

  /* /\* Apply balsara switch *\/ */
  /* Pi_ij *= (pi->force.balsara + pj->force.balsara); */

  /* /\* Get the common factor out. *\/ */
  /* w = ri * */
  /*     ((POrho2i * wi_dr + POrho2j * wj_dr) + 0.25f * Pi_ij * (wi_dr + wj_dr)); */

  /* /\* Use the force, Luke! *\/ */
  /* for (k = 0; k < 3; k++) { */
  /*   f = dx[k] * w; */
  /*   pi->a[k] -= mj * f; */
  /*   pj->a[k] += mi * f; */
  /* } */

  /* /\* Get the time derivative for u. *\/ */
  /* pi->force.u_dt += */
  /*     mj * dvdr * (POrho2i * wi_dr + 0.125f * Pi_ij * (wi_dr + wj_dr)); */
  /* pj->force.u_dt += */
  /*     mi * dvdr * (POrho2j * wj_dr + 0.125f * Pi_ij * (wi_dr + wj_dr)); */

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

  /* /\* Update the signal velocity. *\/ */
  /* pi->force.v_sig = fmaxf(pi->force.v_sig, v_sig); */
  /* pj->force.v_sig = fmaxf(pj->force.v_sig, v_sig); */
275
}
276
277
278
279
280
281


/**
 * @brief Force loop (non-symmetric version)
 */

282
283
284
__attribute__((always_inline)) INLINE static void runner_iact_nonsym_force(
    float r2, float *dx, float hi, float hj, struct part *pi, struct part *pj) {

285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
  /* float r = sqrtf(r2), ri = 1.0f / r; */
  /* float xi, xj; */
  /* float hi_inv, hi2_inv; */
  /* float hj_inv, hj2_inv; */
  /* float wi, wj, wi_dx, wj_dx, wi_dr, wj_dr, w, dvdr; */
  /* float /\*mi,*\/ mj, POrho2i, POrho2j, rhoi, rhoj; */
  /* float v_sig, omega_ij, Pi_ij; */
  /* float f; */
  /* int k; */

  /* /\* Get some values in local variables. *\/ */
  /* // mi = pi->mass; */
  /* mj = pj->mass; */
  /* rhoi = pi->rho; */
  /* rhoj = pj->rho; */
  /* POrho2i = pi->force.POrho2; */
  /* POrho2j = pj->force.POrho2; */

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

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

  /* /\* Compute dv dot r. *\/ */
  /* 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]; */
  /* dvdr *= ri; */

  /* /\* Compute the relative velocity. (This is 0 if the particles move away from */
  /*  * each other and negative otherwise) *\/ */
  /* omega_ij = fminf(dvdr, 0.f); */

  /* /\* Compute signal velocity *\/ */
  /* v_sig = pi->force.c + pj->force.c - 3.0f * omega_ij; */

  /* /\* Compute viscosity tensor *\/ */
  /* Pi_ij = -const_viscosity_alpha * v_sig * omega_ij / (rhoi + rhoj); */

  /* /\* Apply balsara switch *\/ */
  /* Pi_ij *= (pi->force.balsara + pj->force.balsara); */

  /* /\* Get the common factor out. *\/ */
  /* w = ri * */
  /*     ((POrho2i * wi_dr + POrho2j * wj_dr) + 0.25f * Pi_ij * (wi_dr + wj_dr)); */

  /* /\* Use the force, Luke! *\/ */
  /* for (k = 0; k < 3; k++) { */
  /*   f = dx[k] * w; */
  /*   pi->a[k] -= mj * f; */
  /* } */

  /* /\* Get the time derivative for u. *\/ */
  /* pi->force.u_dt += */
  /*     mj * dvdr * (POrho2i * wi_dr + 0.125f * Pi_ij * (wi_dr + wj_dr)); */

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

  /* /\* Update the signal velocity. *\/ */
  /* pi->force.v_sig = fmaxf(pi->force.v_sig, v_sig); */
  /* pj->force.v_sig = fmaxf(pj->force.v_sig, v_sig); */
355
}
356
357


358
#endif /* SWIFT_RUNNER_IACT_LEGACY_H */