MP-Gadget  5.0.1.dev1-76bc7d4726-dirty
gravity.c
Go to the documentation of this file.
1 #include <mpi.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <math.h>
6 
7 #include "utils.h"
8 #include "gravity.h"
9 
10 /*
11  * This table is computed by comparing with brute force calculation it matches the full PM exact up to 10 mesh sizes
12  * for a point source. it is copied to a tighter array for better cache performance (hopefully)
13  *
14  * Generated with split = 1.25; check with the assertion above!
15  * */
16 #include "shortrange-kernel.c"
17 #define NTAB (sizeof(shortrange_force_kernels) / sizeof(shortrange_force_kernels[0]))
18 
21 
22 void
24 {
26  if(Asmth != 1.5) {
27  endrun(0, "The short range force window is calibrated for Asmth = 1.5, but running with %g\n", Asmth);
28  }
29  }
30 
31  size_t i;
32  for(i = 0; i < NTAB; i++)
33  {
34  /* force_kernels is in units of mesh points; */
35  double u = shortrange_force_kernels[i][0] * 0.5 / Asmth;
36  switch (ShortRangeForceWindowType) {
38  /* Notice that the table is only calibrated for smth of 1.25*/
39  shortrange_table[i] = shortrange_force_kernels[i][2]; /* ~ erfc(u) + 2.0 * u / sqrt(M_PI) * exp(-u * u); */
40  /* The potential of the calibrated kernel is a bit off, so we still use erfc here; we do not use potential anyways.*/
42  break;
44  shortrange_table[i] = erfc(u) + 2.0 * u / sqrt(M_PI) * exp(-u * u);
45  shortrange_table_potential[i] = erfc(u);
46  break;
47  }
48  /* we don't have a table for that and don't use it anyways. */
49  shortrange_table_tidal[i] = 4.0 * u * u * u / sqrt(M_PI) * exp(-u * u);
50  }
51 }
52 
53 /* multiply force factor (*fac) and potential (*pot) by the shortrange force window function*/
54 int
55 grav_apply_short_range_window(double r, double * fac, double * pot, const double cellsize)
56 {
57  const double dx = shortrange_force_kernels[1][0];
58  double i = (r / cellsize / dx);
59  size_t tabindex = floor(i);
60  if(tabindex >= NTAB - 1)
61  return 1;
62  /* use a linear interpolation; */
63  *fac *= (tabindex + 1 - i) * shortrange_table[tabindex] + (i - tabindex) * shortrange_table[tabindex + 1];
64  *pot *= (tabindex + 1 - i) * shortrange_table_potential[tabindex] + (i - tabindex) * shortrange_table_potential[tabindex];
65  return 0;
66 }
67 
void endrun(int where, const char *fmt,...)
Definition: endrun.c:147
#define NTAB
Definition: gravity.c:17
static float shortrange_table[NTAB]
Definition: gravity.c:20
void gravshort_fill_ntab(const enum ShortRangeForceWindowType ShortRangeForceWindowType, const double Asmth)
Definition: gravity.c:23
static float shortrange_table_tidal[NTAB]
Definition: gravity.c:20
static float shortrange_table_potential[NTAB]
Definition: gravity.c:20
int grav_apply_short_range_window(double r, double *fac, double *pot, const double cellsize)
Definition: gravity.c:55
ShortRangeForceWindowType
Definition: gravity.h:23
@ SHORTRANGE_FORCE_WINDOW_TYPE_ERFC
Definition: gravity.h:25
@ SHORTRANGE_FORCE_WINDOW_TYPE_EXACT
Definition: gravity.h:24
const double shortrange_force_kernels[][5]