cooling.h 7.95 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 21 22
/*******************************************************************************
 * This file is part of SWIFT.
 * Copyright (c) 2016 Tom Theuns (tom.theuns@durham.ac.uk)
 *                    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/>.
 *
 ******************************************************************************/

23 24
#ifndef SWIFT_COOLING_CONST_LAMBDA_H
#define SWIFT_COOLING_CONST_LAMBDA_H
Stefan Arridge's avatar
Stefan Arridge committed
25

26 27 28
/* Config parameters. */
#include "../config.h"

Stefan Arridge's avatar
Stefan Arridge committed
29
/* Some standard headers. */
30
#include <float.h>
Stefan Arridge's avatar
Stefan Arridge committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include <math.h>

/* Local includes. */
#include "const.h"
#include "error.h"
#include "hydro.h"
#include "parser.h"
#include "part.h"
#include "physical_constants.h"
#include "units.h"

/**
 * @brief Calculates du/dt in code units for a particle.
 *
 * @param phys_const The physical constants in internal units.
 * @param us The internal system of units.
47
 * @param cosmo The current cosmological model.
48
 * @param cooling The #cooling_function_data used in the run.
Stefan Arridge's avatar
Stefan Arridge committed
49 50 51
 * @param p Pointer to the particle data..
 */
__attribute__((always_inline)) INLINE static float cooling_rate(
52
    const struct phys_const* const phys_const, const struct unit_system* us,
53
    const struct cosmology* restrict cosmo,
54
    const struct cooling_function_data* cooling, const struct part* p) {
Stefan Arridge's avatar
Stefan Arridge committed
55

56
  /* Get particle density */
57
  const float rho = hydro_get_physical_density(p, cosmo);
Matthieu Schaller's avatar
Matthieu Schaller committed
58

Stefan Arridge's avatar
Stefan Arridge committed
59 60 61 62
  /* Get cooling function properties */
  const float X_H = cooling->hydrogen_mass_abundance;

  /* Calculate du_dt */
Matthieu Schaller's avatar
Matthieu Schaller committed
63 64 65
  const float du_dt = -cooling->lambda *
                      (X_H * rho / phys_const->const_proton_mass) *
                      (X_H * rho / phys_const->const_proton_mass) / rho;
Stefan Arridge's avatar
Stefan Arridge committed
66 67 68 69 70 71 72 73
  return du_dt;
}

/**
 * @brief Apply the cooling function to a particle.
 *
 * @param phys_const The physical constants in internal units.
 * @param us The internal system of units.
74
 * @param cosmo The current cosmological model.
75
 * @param cooling The #cooling_function_data used in the run.
Stefan Arridge's avatar
Stefan Arridge committed
76 77 78 79
 * @param p Pointer to the particle data.
 * @param dt The time-step of this particle.
 */
__attribute__((always_inline)) INLINE static void cooling_cool_part(
80
    const struct phys_const* restrict phys_const,
81
    const struct unit_system* restrict us,
82
    const struct cosmology* restrict cosmo,
83
    const struct cooling_function_data* restrict cooling,
84
    struct part* restrict p, struct xpart* restrict xp, float dt) {
Stefan Arridge's avatar
Stefan Arridge committed
85 86 87 88

  /* Internal energy floor */
  const float u_floor = cooling->min_energy;

89
  /* Current energy */
90
  const float u_old = hydro_get_physical_internal_energy(p, cosmo);
91

92 93 94 95
  /* Current du_dt */
  const float hydro_du_dt = hydro_get_internal_energy_dt(p);

  /* Calculate cooling du_dt */
96
  float cooling_du_dt = cooling_rate(phys_const, us, cosmo, cooling, p);
97

98
  /* Integrate cooling equation to enforce energy floor */
99 100
  /* Factor of 1.5 included since timestep could potentially double */
  if (u_old + (hydro_du_dt + cooling_du_dt) * 1.5f * dt < u_floor) {
Matthieu Schaller's avatar
Matthieu Schaller committed
101
    cooling_du_dt = -(u_old + 1.5f * dt * hydro_du_dt - u_floor) / (1.5f * dt);
102
  }
103

104 105
  /* Update the internal energy time derivative */
  hydro_set_internal_energy_dt(p, hydro_du_dt + cooling_du_dt);
106

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

/**
 * @brief Computes the time-step due to cooling
 *
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.
Stefan Arridge's avatar
Stefan Arridge committed
117
 * @param us The internal system of units.
Matthieu Schaller's avatar
Matthieu Schaller committed
118
 * @param p Pointer to the particle data.
Stefan Arridge's avatar
Stefan Arridge committed
119 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
  /* Get current internal energy */
127 128
  const float u = hydro_get_physical_internal_energy(p, cosmo);
  const float du_dt = cooling_rate(phys_const, us, cosmo, cooling, p);
Stefan Arridge's avatar
Stefan Arridge committed
129

130
  /* If we are close to (or below) the energy floor, we ignore the condition */
131 132 133 134
  if (u < 1.01f * cooling->min_energy)
    return FLT_MAX;
  else
    return cooling->cooling_tstep_mult * u / fabsf(du_dt);
Stefan Arridge's avatar
Stefan Arridge committed
135 136
}

137 138 139 140 141 142
/**
 * @brief Sets the cooling properties of the (x-)particles to a valid start
 * state.
 *
 * @param p Pointer to the particle data.
 * @param xp Pointer to the extended particle data.
143
 * @param cooling The properties of the cooling function.
144
 */
145
__attribute__((always_inline)) INLINE static void cooling_first_init_part(
146 147 148 149 150
    const struct phys_const* restrict phys_const,
    const struct unit_system* restrict us,
    const struct cosmology* restrict cosmo,
    const struct cooling_function_data* restrict cooling,
    const struct part* restrict p, struct xpart* restrict xp) {
151 152 153 154

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

155 156 157 158 159 160 161 162 163 164 165
/**
 * @brief Returns the total radiated energy by 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;
}

Stefan Arridge's avatar
Stefan Arridge committed
166 167 168 169 170 171 172 173
/**
 * @brief Initialises the cooling properties.
 *
 * @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
 */
174
static INLINE void cooling_init_backend(
175
    const struct swift_params* parameter_file, const struct unit_system* us,
176 177
    const struct phys_const* phys_const,
    struct cooling_function_data* cooling) {
Stefan Arridge's avatar
Stefan Arridge committed
178

179 180 181
  const double lambda_cgs =
      parser_get_param_double(parameter_file, "LambdaCooling:lambda_cgs");
  const float min_temperature = parser_get_param_double(
182
      parameter_file, "LambdaCooling:minimum_temperature");
183
  cooling->hydrogen_mass_abundance = parser_get_param_double(
184 185 186 187 188
      parameter_file, "LambdaCooling:hydrogen_mass_abundance");
  cooling->mean_molecular_weight = parser_get_param_double(
      parameter_file, "LambdaCooling:mean_molecular_weight");
  cooling->cooling_tstep_mult = parser_get_param_double(
      parameter_file, "LambdaCooling:cooling_tstep_mult");
Stefan Arridge's avatar
Stefan Arridge committed
189

190
  /* convert minimum temperature into minimum internal energy */
Stefan Arridge's avatar
Stefan Arridge committed
191
  const float u_floor =
192
      phys_const->const_boltzmann_k * min_temperature /
193 194
      (hydro_gamma_minus_one * cooling->mean_molecular_weight *
       phys_const->const_proton_mass);
Stefan Arridge's avatar
Stefan Arridge committed
195 196

  cooling->min_energy = u_floor;
197 198 199

  /* convert lambda to code units */
  cooling->lambda = lambda_cgs *
Matthieu Schaller's avatar
Matthieu Schaller committed
200 201 202
                    units_cgs_conversion_factor(us, UNIT_CONV_TIME) /
                    (units_cgs_conversion_factor(us, UNIT_CONV_ENERGY) *
                     units_cgs_conversion_factor(us, UNIT_CONV_VOLUME));
Stefan Arridge's avatar
Stefan Arridge committed
203 204 205 206 207 208 209
}

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

213 214
  message(
      "Cooling function is 'Constant lambda' with "
215
      "(lambda,min_energy,hydrogen_mass_abundance,mean_molecular_weight) "
216
      "=  (%g,%g,%g,%g)",
Matthieu Schaller's avatar
Matthieu Schaller committed
217 218
      cooling->lambda, cooling->min_energy, cooling->hydrogen_mass_abundance,
      cooling->mean_molecular_weight);
Stefan Arridge's avatar
Stefan Arridge committed
219 220
}

221
#endif /* SWIFT_COOLING_CONST_LAMBDA_H */