cooling.h 4.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

/**
 * @file src/cooling/const/cooling.h
 * @brief Routines related to the "constant cooling" cooling function.
 *
 * This is the simplest possible cooling function. A constant cooling rate with
 * a minimal energy floor is applied.
 */

/* Some standard headers. */
#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 Properties of the cooling function.
 */
struct cooling_data {

  /*! Cooling rate in internal units. du_dt = -cooling_rate */
  float cooling_rate;

  /*! Minimally allowed internal energy of the particles */
  float min_energy;

  /*! Constant multiplication factor for time-step criterion */
  float cooling_tstep_mult;
};

/**
 * @brief Apply the cooling function to a particle.
 *
 * @param phys_const The physical constants in internal units.
 * @param us The internal system of units.
 * @param cooling The #cooling_data used in the run.
 * @param p Pointer to the particle data.
 * @param dt The time-step of this particle.
 */
__attribute__((always_inline)) INLINE static void cooling_cool_part(
    const struct phys_const* const phys_const, const struct UnitSystem* us,
    const struct cooling_data* cooling, struct part* p, float dt) {

  /* 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);
}

/**
 * @brief Computes the cooling time-step.
 *
 * @param cooling The #cooling_data used in the run.
 * @param phys_const The physical constants in internal units.
 * @param p Pointer to the particle data.
 */
__attribute__((always_inline)) INLINE static double cooling_timestep(
    const struct cooling_data* cooling,
    const struct phys_const* const phys_const, const struct part* const p) {

  const float cooling_rate = cooling->cooling_rate;
  const float internal_energy =
      hydro_get_internal_energy(p, 0);  // dt = 0 because using current entropy
  return cooling->cooling_tstep_mult * internal_energy / cooling_rate;
}

/**
 * @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
 */
INLINE void cooling_init(const struct swift_params* parameter_file,
                         const struct UnitSystem* us,
                         const struct phys_const* phys_const,
                         struct cooling_data* cooling) {

121
  cooling->cooling_rate =
122
      parser_get_param_double(parameter_file, "ConstCooling:cooling_rate");
Stefan Arridge's avatar
Stefan Arridge committed
123
  cooling->min_energy =
124
125
126
      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
127
128
129
130
131
132
133
134
135
136
137
138
139
}

/**
 * @brief Prints the properties of the cooling model to stdout.
 *
 * @param cooling The properties of the cooling function.
 */
INLINE void cooling_print(const struct cooling_data* cooling) {

  message("Cooling function is 'Constant cooling' with rate %f and floor %f",
          cooling->cooling_rate, cooling->min_energy);
}

140
#endif /* SWIFT_COOLING_CONST_DU_H */