cooling.h 6.66 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
49
50
51
52
53
54
55
56
57
58
/**
 * @brief Writes the current model of SPH to the file
 * @param h_grpsph The HDF5 group in which to write
 */
void writeCoolingFlavor(hid_t h_grpsph) {

  /* Viscosity and thermal conduction */
  io_write_attribute_s(
      h_grpsph, "Cooling Model",
      "const_du");
}

Stefan Arridge's avatar
Stefan Arridge committed
59
60
61
/**
 * @brief Apply the cooling function to a particle.
 *
62
63
64
 * 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
65
66
 * @param phys_const The physical constants in internal units.
 * @param us The internal system of units.
67
 * @param cooling The #cooling_function_data used in the run.
Stefan Arridge's avatar
Stefan Arridge committed
68
 * @param p Pointer to the particle data.
69
 * @param xp Pointer to the extended particle data.
Stefan Arridge's avatar
Stefan Arridge committed
70
71
72
 * @param dt The time-step of this particle.
 */
__attribute__((always_inline)) INLINE static void cooling_cool_part(
73
    const struct phys_const* restrict phys_const,
74
    const struct unit_system* restrict us,
75
    const struct cooling_function_data* restrict cooling,
76
    struct part* restrict p, struct xpart* restrict xp, float dt) {
Stefan Arridge's avatar
Stefan Arridge committed
77

78
79
80
81
  /* Internal energy floor */
  const float u_floor = cooling->min_energy;

  /* Get current internal energy */
82
  const float u_old = hydro_get_internal_energy(p);
Stefan Arridge's avatar
Stefan Arridge committed
83

84
85
86
  /* Current du_dt */
  const float hydro_du_dt = hydro_get_internal_energy_dt(p);

Stefan Arridge's avatar
Stefan Arridge committed
87
  /* Get cooling function properties */
88
  float cooling_du_dt = -cooling->cooling_rate;
Stefan Arridge's avatar
Stefan Arridge committed
89

90
91
92
93
94
  /* 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
95
96
  }

97
98
  /* Update the internal energy time derivative */
  hydro_set_internal_energy_dt(p, hydro_du_dt + cooling_du_dt);
99
100

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

/**
 * @brief Computes the cooling time-step.
 *
107
108
109
110
 * 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.
 *
111
 * @param cooling The #cooling_function_data used in the run.
Stefan Arridge's avatar
Stefan Arridge committed
112
 * @param phys_const The physical constants in internal units.
113
 * @param us The internal system of units.
Stefan Arridge's avatar
Stefan Arridge committed
114
115
 * @param p Pointer to the particle data.
 */
116
__attribute__((always_inline)) INLINE static float cooling_timestep(
117
    const struct cooling_function_data* restrict cooling,
118
    const struct phys_const* restrict phys_const,
119
    const struct unit_system* restrict us, const struct part* restrict p) {
Stefan Arridge's avatar
Stefan Arridge committed
120
121

  const float cooling_rate = cooling->cooling_rate;
122
  const float internal_energy = hydro_get_internal_energy(p);
123
  return cooling->cooling_tstep_mult * internal_energy / fabsf(cooling_rate);
Stefan Arridge's avatar
Stefan Arridge committed
124
125
126
}

/**
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
 * @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.
 */
__attribute__((always_inline)) INLINE static void cooling_init_part(
    const struct part* restrict p, struct xpart* restrict xp) {

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

143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
 * @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;
}

157
158
159
160
161
/**
 * @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
162
163
164
165
166
167
 *
 * @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
 */
168
static INLINE void cooling_init_backend(
169
    const struct swift_params* parameter_file, const struct unit_system* us,
170
171
    const struct phys_const* phys_const,
    struct cooling_function_data* cooling) {
Stefan Arridge's avatar
Stefan Arridge committed
172

173
  cooling->cooling_rate =
174
      parser_get_param_double(parameter_file, "ConstCooling:cooling_rate");
Stefan Arridge's avatar
Stefan Arridge committed
175
  cooling->min_energy =
176
177
178
      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
179
180
181
182
183
184
185
}

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

189
  message("Cooling function is 'Constant cooling' with rate %f and floor %f.",
Stefan Arridge's avatar
Stefan Arridge committed
190
191
192
          cooling->cooling_rate, cooling->min_energy);
}

193
#endif /* SWIFT_COOLING_CONST_DU_H */