cooling.h 7.01 KB
Newer Older
Stefan Arridge's avatar
Stefan Arridge committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*******************************************************************************
 * This file is part of SWIFT.
 * Copyright (c) 2016 Matthieu Schaller (matthieu.schaller@durham.ac.uk)
 *                    Richard Bower (r.g.bower@durham.ac.uk)
 *                    Stefan Arridge  (stefan.arridge@durham.ac.uk)
 *
 * 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/>.
 *
 ******************************************************************************/
21
22
#ifndef SWIFT_COOLING_CONST_DU_H
#define SWIFT_COOLING_CONST_DU_H
Stefan Arridge's avatar
Stefan Arridge committed
23
24

/**
25
 * @file src/cooling/const_du/cooling.h
Stefan Arridge's avatar
Stefan Arridge committed
26
27
28
 * @brief Routines related to the "constant cooling" cooling function.
 *
 * This is the simplest possible cooling function. A constant cooling rate with
29
30
 * a minimal energy floor is applied. Should be used as a template for more
 * realistic functions.
Stefan Arridge's avatar
Stefan Arridge committed
31
32
33
34
35
36
37
 */

/* Some standard headers. */
#include <math.h>

/* Local includes. */
#include "const.h"
38
#include "cooling_struct.h"
Stefan Arridge's avatar
Stefan Arridge committed
39
40
#include "error.h"
#include "hydro.h"
41
#include "io_properties.h"
Stefan Arridge's avatar
Stefan Arridge committed
42
43
44
45
46
#include "parser.h"
#include "part.h"
#include "physical_constants.h"
#include "units.h"

47
48
#ifdef HAVE_HDF5

49
50
51
52
/**
 * @brief Writes the current model of SPH to the file
 * @param h_grpsph The HDF5 group in which to write
 */
53
__attribute__((always_inline)) INLINE static void cooling_write_flavour(
54
    hid_t h_grpsph) {
55

56
  io_write_attribute_s(h_grpsph, "Cooling Model", "Constant du/dt");
57
}
58
#endif
59

Stefan Arridge's avatar
Stefan Arridge committed
60
61
62
/**
 * @brief Apply the cooling function to a particle.
 *
63
64
65
 * In this simple example we just apply the const cooling rate
 * and check that we don't go below the given floor.
 *
Stefan Arridge's avatar
Stefan Arridge committed
66
67
 * @param phys_const The physical constants in internal units.
 * @param us The internal system of units.
68
 * @param cosmo The current cosmological model.
69
 * @param cooling The #cooling_function_data used in the run.
Stefan Arridge's avatar
Stefan Arridge committed
70
 * @param p Pointer to the particle data.
71
 * @param xp Pointer to the extended particle data.
Stefan Arridge's avatar
Stefan Arridge committed
72
73
74
 * @param dt The time-step of this particle.
 */
__attribute__((always_inline)) INLINE static void cooling_cool_part(
75
    const struct phys_const* restrict phys_const,
76
    const struct unit_system* restrict us,
77
    const struct cosmology* restrict cosmo,
78
    const struct cooling_function_data* restrict cooling,
79
    struct part* restrict p, struct xpart* restrict xp, float dt) {
Stefan Arridge's avatar
Stefan Arridge committed
80

81
82
83
84
  /* Internal energy floor */
  const float u_floor = cooling->min_energy;

  /* Get current internal energy */
85
  const float u_old = hydro_get_physical_internal_energy(p, cosmo);
Stefan Arridge's avatar
Stefan Arridge committed
86

87
88
89
  /* Current du_dt */
  const float hydro_du_dt = hydro_get_internal_energy_dt(p);

Stefan Arridge's avatar
Stefan Arridge committed
90
  /* Get cooling function properties */
91
  float cooling_du_dt = -cooling->cooling_rate;
Stefan Arridge's avatar
Stefan Arridge committed
92

93
94
95
96
97
  /* Integrate cooling equation to enforce energy floor */
  if (u_old + hydro_du_dt * dt < u_floor) {
    cooling_du_dt = 0.f;
  } else if (u_old + (hydro_du_dt + cooling_du_dt) * dt < u_floor) {
    cooling_du_dt = (u_old + dt * hydro_du_dt - u_floor) / dt;
Stefan Arridge's avatar
Stefan Arridge committed
98
99
  }

100
101
  /* Update the internal energy time derivative */
  hydro_set_internal_energy_dt(p, hydro_du_dt + cooling_du_dt);
102
103

  /* Store the radiated energy */
104
  xp->cooling_data.radiated_energy += -hydro_get_mass(p) * cooling_du_dt * dt;
Stefan Arridge's avatar
Stefan Arridge committed
105
106
107
108
109
}

/**
 * @brief Computes the cooling time-step.
 *
110
111
112
113
 * In this simple example, we return \f$ \alpha \frac{u}{du/dt} \f$.
 * This is used to compute the time-step of the particle. Cooling functions
 * that are solved implicitly can simply return FLT_MAX here.
 *
114
 * @param cooling The #cooling_function_data used in the run.
Stefan Arridge's avatar
Stefan Arridge committed
115
 * @param phys_const The physical constants in internal units.
116
 * @param cosmo The current cosmological model.
117
 * @param us The internal system of units.
Stefan Arridge's avatar
Stefan Arridge committed
118
119
 * @param p Pointer to the particle data.
 */
120
__attribute__((always_inline)) INLINE static float cooling_timestep(
121
    const struct cooling_function_data* restrict cooling,
122
    const struct phys_const* restrict phys_const,
123
    const struct cosmology* restrict cosmo,
124
    const struct unit_system* restrict us, const struct part* restrict p) {
Stefan Arridge's avatar
Stefan Arridge committed
125
126

  const float cooling_rate = cooling->cooling_rate;
127
  const float internal_energy = hydro_get_physical_internal_energy(p, cosmo);
128
  return cooling->cooling_tstep_mult * internal_energy / fabsf(cooling_rate);
Stefan Arridge's avatar
Stefan Arridge committed
129
130
131
}

/**
132
133
134
135
136
137
138
139
140
 * @brief Sets the cooling properties of the (x-)particles to a valid start
 * state.
 *
 * In this case, we set the total radiated energy to 0. Note that the particle
 * structure is just passed in for cases where information needs to be read
 * from there.
 *
 * @param p Pointer to the particle data.
 * @param xp Pointer to the extended particle data.
141
 * @param cooling The properties of the cooling function.
142
 */
143
144
145
__attribute__((always_inline)) INLINE static void cooling_first_init_part(
    const struct part* restrict p, struct xpart* restrict xp,
    const struct cooling_function_data* cooling) {
146
147
148
149

  xp->cooling_data.radiated_energy = 0.f;
}

150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
 * @brief Returns the total radiated energy by this particle.
 *
 * In this simple example we jsut return the quantity accumulated in the
 * #cooling_xpart_data of this particle.
 *
 * @param xp The extended particle data
 */
__attribute__((always_inline)) INLINE static float cooling_get_radiated_energy(
    const struct xpart* restrict xp) {

  return xp->cooling_data.radiated_energy;
}

164
165
166
167
168
/**
 * @brief Initialises the cooling function properties from the parameter file
 *
 * In this example, we just read in the values from the YAML file without
 * doing any conversions or multiplying any constants in.
Stefan Arridge's avatar
Stefan Arridge committed
169
170
171
172
173
174
 *
 * @param parameter_file The parsed parameter file.
 * @param us The current internal system of units.
 * @param phys_const The physical constants in internal units.
 * @param cooling The cooling properties to initialize
 */
175
static INLINE void cooling_init_backend(
176
    const struct swift_params* parameter_file, const struct unit_system* us,
177
178
    const struct phys_const* phys_const,
    struct cooling_function_data* cooling) {
Stefan Arridge's avatar
Stefan Arridge committed
179

180
  cooling->cooling_rate =
181
      parser_get_param_double(parameter_file, "ConstCooling:cooling_rate");
Stefan Arridge's avatar
Stefan Arridge committed
182
  cooling->min_energy =
183
184
185
      parser_get_param_double(parameter_file, "ConstCooling:min_energy");
  cooling->cooling_tstep_mult = parser_get_param_double(
      parameter_file, "ConstCooling:cooling_tstep_mult");
Stefan Arridge's avatar
Stefan Arridge committed
186
187
188
189
190
191
192
}

/**
 * @brief Prints the properties of the cooling model to stdout.
 *
 * @param cooling The properties of the cooling function.
 */
193
194
static INLINE void cooling_print_backend(
    const struct cooling_function_data* cooling) {
Stefan Arridge's avatar
Stefan Arridge committed
195

196
  message("Cooling function is 'Constant cooling' with rate %f and floor %f.",
Stefan Arridge's avatar
Stefan Arridge committed
197
198
199
          cooling->cooling_rate, cooling->min_energy);
}

200
#endif /* SWIFT_COOLING_CONST_DU_H */