cooling.h 7.72 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*******************************************************************************
 * This file is part of SWIFT.
 * Copyright (c) 2016 Matthieu Schaller (matthieu.schaller@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/>.
 *
 ******************************************************************************/
#ifndef SWIFT_COOLING_GRACKLE_H
#define SWIFT_COOLING_GRACKLE_H

/**
 * @file src/cooling/none/cooling.h
 * @brief Empty infrastructure for the cases without cooling function
 */

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

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

/* include the grackle wrapper */
#include "grackle_wrapper.h"

42
43
44
45
46
47
48
49
50
51
/**
 * @brief Compute the cooling rate
 *
 * We do nothing.
 *
 * @param phys_const The physical constants in internal units.
 * @param us The internal system of units.
 * @param cooling The #cooling_function_data used in the run.
 * @param p Pointer to the particle data.
 * @param dt The time-step of this particle.
52
53
 *
 * @return du / dt
54
 */
55
56
57
58
59
__attribute__((always_inline)) INLINE static double cooling_rate(
    const struct phys_const* restrict phys_const,
    const struct unit_system* restrict us,
    const struct cooling_function_data* restrict cooling,
    struct part* restrict p, float dt) {
60

Matthieu Schaller's avatar
Matthieu Schaller committed
61
  if (cooling->GrackleRedshift == -1) error("TODO time dependant redshift");
62

63
64
  /* Get current internal energy (dt=0) */
  double u_old = hydro_get_internal_energy(p);
65
  double u_new = u_old;
66
67
68
69
  /* Get current density */
  const float rho = hydro_get_density(p);
  /* Actual scaling fractor */
  const float a_now = 1. / (1. + cooling->GrackleRedshift);
lhausamm's avatar
lhausamm committed
70

71
72
73
74
  /* 0.02041 (= 1 Zsun in Grackle v2.0, but = 1.5761 Zsun in
     Grackle v2.1) */
  double Z = 0.02041;

75
  if (wrap_do_cooling(rho, &u_new, dt, Z, a_now) == 0) {
lhausamm's avatar
lhausamm committed
76
    error("Error in do_cooling.\n");
77
78
79
    return 0;
  }

80
  return (u_new - u_old) / dt;
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
}

/**
 * @brief Apply the cooling function to a particle.
 *
 * We do nothing.
 *
 * @param phys_const The physical constants in internal units.
 * @param us The internal system of units.
 * @param cooling The #cooling_function_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* restrict phys_const,
lhausamm's avatar
lhausamm committed
96
    const struct unit_system* restrict us,
97
98
99
    const struct cooling_function_data* restrict cooling,
    struct part* restrict p, struct xpart* restrict xp, float dt) {

Matthieu Schaller's avatar
Matthieu Schaller committed
100
  if (dt == 0.) return;
101
102
103

  /* Current du_dt */
  const float hydro_du_dt = hydro_get_internal_energy_dt(p);
104

105
  float du_dt;
106
107
  float delta_u;

108
  du_dt = cooling_rate(phys_const, us, cooling, p, dt);
109

110
  delta_u = du_dt * dt;
111
112

  /* record energy lost */
lhausamm's avatar
lhausamm committed
113
114
  xp->cooling_data.radiated_energy += -delta_u * hydro_get_mass(p);

115
  /* Update the internal energy */
116
  hydro_set_internal_energy_dt(p, hydro_du_dt + du_dt);
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
}

/**
 * @brief Computes the cooling time-step.
 *
 * We return FLT_MAX so as to impose no limit on the time-step.
 *
 * @param cooling The #cooling_function_data used in the run.
 * @param phys_const The physical constants in internal units.
 * @param us The internal system of units.
 * @param p Pointer to the particle data.
 */
__attribute__((always_inline)) INLINE static float cooling_timestep(
    const struct cooling_function_data* restrict cooling,
    const struct phys_const* restrict phys_const,
lhausamm's avatar
lhausamm committed
132
    const struct unit_system* restrict us, const struct part* restrict p) {
133
134
135
136
137
138
139
140
141
142
143
144
145
146

  return FLT_MAX;
}

/**
 * @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.
 */
__attribute__((always_inline)) INLINE static void cooling_init_part(
    const struct part* restrict p, struct xpart* restrict xp) {

lhausamm's avatar
lhausamm committed
147
  xp->cooling_data.radiated_energy = 0.f;
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
}

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

/**
 * @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
 */
static INLINE void cooling_init_backend(
lhausamm's avatar
lhausamm committed
170
    const struct swift_params* parameter_file, const struct unit_system* us,
171
172
173
174
175
176
177
    const struct phys_const* phys_const,
    struct cooling_function_data* cooling) {

  double units_density, units_length, units_time;
  int grackle_chemistry;
  int UVbackground;

lhausamm's avatar
lhausamm committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
  parser_get_param_string(parameter_file, "GrackleCooling:GrackleCloudyTable",
                          cooling->GrackleCloudyTable);
  cooling->UVbackground =
      parser_get_param_int(parameter_file, "GrackleCooling:UVbackground");
  cooling->GrackleRedshift =
      parser_get_param_double(parameter_file, "GrackleCooling:GrackleRedshift");
  cooling->GrackleHSShieldingDensityThreshold = parser_get_param_double(
      parameter_file, "GrackleCooling:GrackleHSShieldingDensityThreshold");

  UVbackground = cooling->UVbackground;
  grackle_chemistry = 0; /* forced to be zero : read table */

  units_density = us->UnitMass_in_cgs / pow(us->UnitLength_in_cgs, 3);
  units_length = us->UnitLength_in_cgs;
  units_time = us->UnitTime_in_cgs;

#ifdef SWIFT_DEBUG_CHECKS
  float threshold = cooling->GrackleHSShieldingDensityThreshold;

  threshold /= phys_const->const_proton_mass;
  threshold /= pow(us->UnitLength_in_cgs, 3);

  message("***************************************");
  message("initializing grackle cooling function");
  message("");
  message("CloudyTable                        = %s",
          cooling->GrackleCloudyTable);
  message("UVbackground                       = %d", UVbackground);
  message("GrackleRedshift                    = %g", cooling->GrackleRedshift);
207
  message("GrackleHSShieldingDensityThreshold = %g atom/cm3", threshold);
lhausamm's avatar
lhausamm committed
208
209
210
211
212
213
214
#endif

  if (wrap_init_cooling(cooling->GrackleCloudyTable, UVbackground,
                        units_density, units_length, units_time,
                        grackle_chemistry) != 1) {
    error("Error in initialize_chemistry_data.");
  }
215

lhausamm's avatar
lhausamm committed
216
#ifdef SWIFT_DEBUG_CHECKS
lhausamm's avatar
lhausamm committed
217
  grackle_print_data();
lhausamm's avatar
lhausamm committed
218
219
220
  message("");
  message("***************************************");
#endif
221
222
223
224
225
226
227
228
229
230
231
}

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

  message("Cooling function is 'Grackle'.");
lhausamm's avatar
lhausamm committed
232
233
234
235
236
  message("CloudyTable                        = %s",
          cooling->GrackleCloudyTable);
  message("UVbackground                       = %d", cooling->UVbackground);
  message("GrackleRedshift                    = %g", cooling->GrackleRedshift);
  message("GrackleHSShieldingDensityThreshold = %g atom/cm3",
Matthieu Schaller's avatar
Matthieu Schaller committed
237
          cooling->GrackleHSShieldingDensityThreshold);
238
239
240
}

#endif /* SWIFT_COOLING_GRACKLE_H */