cooling.h 5.71 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
41
42
43
44
45
46
47
48
#include "error.h"
#include "hydro.h"
#include "parser.h"
#include "part.h"
#include "physical_constants.h"
#include "units.h"

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

  /* Get current internal energy (dt=0) */
  const float u_old = hydro_get_internal_energy(p, 0.f);

  /* Get cooling function properties */
  const float du_dt = -cooling->cooling_rate;
  const float u_floor = cooling->min_energy;

  /* Constant cooling with a minimal floor */
  float u_new;
  if (u_old - du_dt * dt > u_floor) {
    u_new = u_old + du_dt * dt;
  } else {
    u_new = u_floor;
  }

  /* Update the internal energy */
  hydro_set_internal_energy(p, u_new);
82
83
84

  /* Store the radiated energy */
  xp->cooling_data.radiated_energy += u_new - u_old;
Stefan Arridge's avatar
Stefan Arridge committed
85
86
87
88
89
}

/**
 * @brief Computes the cooling time-step.
 *
90
91
92
93
 * 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.
 *
94
 * @param cooling The #cooling_function_data used in the run.
Stefan Arridge's avatar
Stefan Arridge committed
95
 * @param phys_const The physical constants in internal units.
96
 * @param us The internal system of units.
Stefan Arridge's avatar
Stefan Arridge committed
97
98
 * @param p Pointer to the particle data.
 */
99
__attribute__((always_inline)) INLINE static float cooling_timestep(
100
    const struct cooling_function_data* restrict cooling,
101
102
    const struct phys_const* restrict phys_const,
    const struct UnitSystem* restrict us, const struct part* restrict p) {
Stefan Arridge's avatar
Stefan Arridge committed
103
104

  const float cooling_rate = cooling->cooling_rate;
105
  const float internal_energy = hydro_get_internal_energy(p, 0);
Stefan Arridge's avatar
Stefan Arridge committed
106
107
108
109
  return cooling->cooling_tstep_mult * internal_energy / cooling_rate;
}

/**
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
 * @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;
}

/**
 * @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
131
132
133
134
135
136
 *
 * @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
 */
137
138
static INLINE void cooling_init_backend(
    const struct swift_params* parameter_file, const struct UnitSystem* us,
139
140
    const struct phys_const* phys_const,
    struct cooling_function_data* cooling) {
Stefan Arridge's avatar
Stefan Arridge committed
141

142
  cooling->cooling_rate =
143
      parser_get_param_double(parameter_file, "ConstCooling:cooling_rate");
Stefan Arridge's avatar
Stefan Arridge committed
144
  cooling->min_energy =
145
146
147
      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
148
149
150
151
152
153
154
}

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

158
  message("Cooling function is 'Constant cooling' with rate %f and floor %f.",
Stefan Arridge's avatar
Stefan Arridge committed
159
160
161
          cooling->cooling_rate, cooling->min_energy);
}

162
#endif /* SWIFT_COOLING_CONST_DU_H */