gravity.h 6.96 KB
Newer Older
1
2
3
/*******************************************************************************
 * This file is part of SWIFT.
 * Coypright (c) 2015 Matthieu Schaller (matthieu.schaller@durham.ac.uk)
4
 *               2016 Tom Theuns (tom.theuns@durham.ac.uk)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * 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/>.
 *
 ******************************************************************************/
20
21
#ifndef SWIFT_DEFAULT_GRAVITY_H
#define SWIFT_DEFAULT_GRAVITY_H
22
23

#include <float.h>
24

25
#include "cosmology.h"
26
#include "gravity_properties.h"
27
#include "kernel_gravity.h"
28
#include "minmax.h"
29

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
 * @brief Returns the mass of a particle
 *
 * @param gp The particle of interest
 */
__attribute__((always_inline)) INLINE static float gravity_get_mass(
    const struct gpart* restrict gp) {

  return gp->mass;
}

/**
 * @brief Returns the softening of a particle
 *
 * @param gp The particle of interest
45
 * @param grav_props The global gravity properties.
46
47
 */
__attribute__((always_inline)) INLINE static float gravity_get_softening(
48
    const struct gpart* gp, const struct gravity_props* restrict grav_props) {
49

50
  return grav_props->epsilon_cur;
51
52
53
}

/**
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
 * @brief Add a contribution to this particle's potential.
 *
 * Here we do nothing as this version does not accumulate potential.
 *
 * @param gp The particle.
 * @param pot The contribution to add.
 */
__attribute__((always_inline)) INLINE static void
gravity_add_comoving_potential(struct gpart* restrict gp, float pot) {}

/**
 * @brief Returns the comoving potential of a particle.
 *
 * This returns 0 as this flavour of gravity does not compute the
 * particles' potential.
69
70
71
 *
 * @param gp The particle of interest
 */
72
73
__attribute__((always_inline)) INLINE static float
gravity_get_comoving_potential(const struct gpart* restrict gp) {
74

75
  return 0.f;
76
77
}

78
79
80
/**
 * @brief Returns the physical potential of a particle
 *
81
82
83
 * This returns 0 as this flavour of gravity does not compute the
 * particles' potential.
 *
84
85
86
87
88
89
90
 * @param gp The particle of interest.
 * @param cosmo The cosmological model.
 */
__attribute__((always_inline)) INLINE static float
gravity_get_physical_potential(const struct gpart* restrict gp,
                               const struct cosmology* cosmo) {

91
  return 0.f;
92
93
}

94
95
96
/**
 * @brief Computes the gravity time-step of a given particle due to self-gravity
 *
97
98
 * We use Gadget-2's type 0 time-step criterion.
 *
99
 * @param gp Pointer to the g-particle data.
100
 * @param a_hydro The accelerations coming from the hydro scheme (can be 0).
101
 * @param grav_props Constants used in the gravity scheme.
102
 * @param cosmo The current cosmological model.
103
 */
104
__attribute__((always_inline)) INLINE static float
105
gravity_compute_timestep_self(const struct gpart* const gp,
106
                              const float a_hydro[3],
107
108
                              const struct gravity_props* restrict grav_props,
                              const struct cosmology* cosmo) {
109

110
111
112
113
114
115
116
117
118
119
120
121
  /* Get physical acceleration (gravity contribution) */
  float a_phys_x = gp->a_grav[0] * cosmo->a_factor_grav_accel;
  float a_phys_y = gp->a_grav[1] * cosmo->a_factor_grav_accel;
  float a_phys_z = gp->a_grav[2] * cosmo->a_factor_grav_accel;

  /* Get physical acceleration (hydro contribution) */
  a_phys_x += a_hydro[0] * cosmo->a_factor_hydro_accel;
  a_phys_y += a_hydro[1] * cosmo->a_factor_hydro_accel;
  a_phys_z += a_hydro[2] * cosmo->a_factor_hydro_accel;

  const float ac2 =
      a_phys_x * a_phys_x + a_phys_y * a_phys_y + a_phys_z * a_phys_z;
122

Matthieu Schaller's avatar
Matthieu Schaller committed
123
  const float ac_inv = (ac2 > 0.f) ? 1.f / sqrtf(ac2) : FLT_MAX;
124

125
  const float epsilon = gravity_get_softening(gp, grav_props);
126

127
128
  const float dt = sqrtf(2. * kernel_gravity_softening_plummer_equivalent_inv *
                         cosmo->a * grav_props->eta * epsilon * ac_inv);
129
130
131
132

  return dt;
}

133
134
135
136
137
138
139
140
/**
 * @brief Prepares a g-particle for the gravity calculation
 *
 * Zeroes all the relevant arrays in preparation for the sums taking place in
 * the variaous tasks
 *
 * @param gp The particle to act upon
 */
141
__attribute__((always_inline)) INLINE static void gravity_init_gpart(
142
    struct gpart* gp) {
143

144
145
146
147
  /* Zero the acceleration */
  gp->a_grav[0] = 0.f;
  gp->a_grav[1] = 0.f;
  gp->a_grav[2] = 0.f;
148

149
150
151
152
153
154
155
#ifdef SWIFT_GRAVITY_FORCE_CHECKS
  gp->potential_PM = 0.f;
  gp->a_grav_PM[0] = 0.f;
  gp->a_grav_PM[1] = 0.f;
  gp->a_grav_PM[2] = 0.f;
#endif

156
#ifdef SWIFT_DEBUG_CHECKS
157
  gp->num_interacted = 0;
158
  gp->initialised = 1;
159
#endif
160
}
Tom Theuns's avatar
Tom Theuns committed
161

Matthieu Schaller's avatar
Matthieu Schaller committed
162
163
164
/**
 * @brief Finishes the gravity calculation.
 *
165
166
 * Multiplies the forces and accelerations by the appropiate constants.
 * Applies cosmological correction for periodic BCs.
Matthieu Schaller's avatar
Matthieu Schaller committed
167
 *
Matthieu Schaller's avatar
Matthieu Schaller committed
168
169
170
 * No need to apply the potential normalisation correction for periodic
 * BCs here since we do not compute the potential.
 *
Matthieu Schaller's avatar
Matthieu Schaller committed
171
 * @param gp The particle to act upon
172
173
 * @param const_G Newton's constant in internal units.
 * @param potential_normalisation Term to be added to all the particles.
174
 * @param periodic Are we using periodic BCs?
Matthieu Schaller's avatar
Matthieu Schaller committed
175
 */
176
__attribute__((always_inline)) INLINE static void gravity_end_force(
177
178
    struct gpart* gp, float const_G, const float potential_normalisation,
    const int periodic) {
179
180
181
182
183

  /* Let's get physical... */
  gp->a_grav[0] *= const_G;
  gp->a_grav[1] *= const_G;
  gp->a_grav[2] *= const_G;
184
185
186
187
188
189
190

#ifdef SWIFT_GRAVITY_FORCE_CHECKS
  gp->potential_PM *= const_G;
  gp->a_grav_PM[0] *= const_G;
  gp->a_grav_PM[1] *= const_G;
  gp->a_grav_PM[2] *= const_G;
#endif
191
192
193
194

#ifdef SWIFT_DEBUG_CHECKS
  gp->initialised = 0; /* Ready for next step */
#endif
195
}
Matthieu Schaller's avatar
Matthieu Schaller committed
196

197
198
199
200
201
202
203
/**
 * @brief Kick the additional variables
 *
 * @param gp The particle to act upon
 * @param dt The time-step for this kick
 */
__attribute__((always_inline)) INLINE static void gravity_kick_extra(
204
    struct gpart* gp, float dt) {}
205

206
207
208
209
210
211
/**
 * @brief Sets the values to be predicted in the drifts to their values at a
 * kick time
 *
 * @param gp The particle.
 */
212
213
__attribute__((always_inline)) INLINE static void
gravity_reset_predicted_values(struct gpart* gp) {}
214

215
216
217
218
219
220
221
/**
 * @brief Initialises the g-particles for the first time
 *
 * This function is called only once just after the ICs have been
 * read in to do some conversions.
 *
 * @param gp The particle to act upon
222
 * @param grav_props The global properties of the gravity calculation.
223
224
 */
__attribute__((always_inline)) INLINE static void gravity_first_init_gpart(
225
    struct gpart* gp, const struct gravity_props* grav_props) {
226
227
228
229

  gp->time_bin = 0;

  gravity_init_gpart(gp);
Loikki's avatar
Loikki committed
230
231
232
233
234

#ifdef WITH_LOGGER
  gp->last_output = 0;
  gp->last_offset = 0;
#endif
235
236
}

237
#endif /* SWIFT_DEFAULT_GRAVITY_H */