MP-Gadget  5.0.1.dev1-76bc7d4726-dirty
Macros | Functions | Variables
test_cooling_rates.c File Reference
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <math.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <libgadget/physconst.h>
#include <libgadget/cooling_rates.h>
#include <libgadget/config.h>
#include "stub.h"
Include dependency graph for test_cooling_rates.c:

Go to the source code of this file.

Macros

#define NEXACT   20
 

Functions

int during_helium_reionization (double redshift)
 
double recomb_alphaHp (double temp)
 
static struct cooling_params get_test_coolpar (void)
 
static void test_recomb_rates (void **state)
 
static void test_uvbg_loader (void **state)
 
static void test_rate_network (void **state)
 
static void test_heatingcooling_rate (void **state)
 
int main (void)
 

Variables

static const double f92g2 [NEXACT] = {5.758e-11, 2.909e-11, 1.440e-11, 6.971e-12,3.282e-12, 1.489e-12, 6.43e-13, 2.588e-13, 9.456e-14, 3.069e-14, 8.793e-15, 2.245e-15, 5.190e-16, 1.107e-16, 2.221e-17, 4.267e-18, 7.960e-19, 1.457e-19,2.636e-20, 4.737e-21}
 
static const double f92n1 [NEXACT] = {9.258e-12, 5.206e-12, 2.927e-12, 1.646e-12, 9.246e-13, 5.184e-13, 2.890e-13, 1.582e-13, 8.255e-14, 3.882e-14, 1.545e-14, 5.058e-15, 1.383e-15, 3.276e-16, 7.006e-17, 1.398e-17, 2.665e-18, 4.940e-19, 9.001e-20, 1.623e-20}
 
static const double tt [NEXACT] = {3.16227766e+00, 1.0e+01, 3.16227766e+01, 1.0e+02, 3.16227766e+02, 1.00e+03, 3.16227766e+03, 1.e+04, 3.16227766e+04, 1.e+05, 3.16227766e+05, 1.e+06, 3.16227766e+06, 1.0e+07, 3.16227766e+07, 1.0e+08, 3.16227766e+08, 1.0e+09, 3.16227766e+09, 1.0e+10}
 

Macro Definition Documentation

◆ NEXACT

#define NEXACT   20

Definition at line 25 of file test_cooling_rates.c.

Function Documentation

◆ during_helium_reionization()

int during_helium_reionization ( double  redshift)

Definition at line 18 of file test_cooling_rates.c.

19 {
20  return 0;
21 }

◆ get_test_coolpar()

static struct cooling_params get_test_coolpar ( void  )
static

Definition at line 32 of file test_cooling_rates.c.

35 {
36  static struct cooling_params coolpar;
37  coolpar.CMBTemperature = 2.7255;
38  coolpar.PhotoIonizeFactor = 1;
39  coolpar.SelfShieldingOn = 1;
40  coolpar.fBar = 0.17;
41  coolpar.PhotoIonizationOn = 1;
42  coolpar.recomb = Verner96;
43  coolpar.cooling = Sherwood;
44  coolpar.UVRedshiftThreshold = -1;
45  coolpar.MinGasTemp = 100;
46  coolpar.HeliumHeatOn = 0;
47  coolpar.HydrogenHeatAmp = 0;
48  return coolpar;
49 }
@ Sherwood
Definition: cooling_rates.h:19
@ Verner96
Definition: cooling_rates.h:12
double CMBTemperature
Definition: cooling_rates.h:41

Referenced by test_heatingcooling_rate(), test_rate_network(), test_recomb_rates(), and test_uvbg_loader().

Here is the caller graph for this function:

◆ main()

int main ( void  )

Definition at line 321 of file test_cooling_rates.c.

321  {
322  const struct CMUnitTest tests[] = {
323  cmocka_unit_test(test_recomb_rates),
324  cmocka_unit_test(test_rate_network),
325  cmocka_unit_test(test_heatingcooling_rate),
326  cmocka_unit_test(test_uvbg_loader)
327  };
328  return cmocka_run_group_tests_mpi(tests, NULL, NULL);
329 }
static void test_heatingcooling_rate(void **state)
static void test_recomb_rates(void **state)
static void test_rate_network(void **state)
static void test_uvbg_loader(void **state)

References test_heatingcooling_rate(), test_rate_network(), test_recomb_rates(), and test_uvbg_loader().

Here is the call graph for this function:

◆ recomb_alphaHp()

double recomb_alphaHp ( double  temp)

Definition at line 347 of file cooling_rates.c.

348 {
349  switch(CoolingParams.recomb)
350  {
351  case Cen92:
352  return 8.4e-11 / sqrt(temp) / pow(temp/1000, 0.2) / (1+ pow(temp/1e6, 0.7));
353  case Verner96:
354  /*The V&F 96 fitting formula is accurate to < 1% in the worst case.
355  See line 1 of V&F96 table 1.*/
356  return _Verner96Fit(temp, 7.982e-11, 0.748, 3.148, 7.036e+05);
357  case Badnell06:
358  /*Slightly different fit coefficients*/
359  return _Verner96Fit(temp, 8.318e-11, 0.7472, 2.965, 7.001e5);
360  default:
361  endrun(3, "Recombination rate not supported\n");
362  }
363 }
static double _Verner96Fit(double temp, double aa, double bb, double temp0, double temp1)
static struct cooling_params CoolingParams
Definition: cooling_rates.c:68
@ Cen92
Definition: cooling_rates.h:11
@ Badnell06
Definition: cooling_rates.h:13
void endrun(int where, const char *fmt,...)
Definition: endrun.c:147
enum RecombType recomb
Definition: cooling_rates.h:26

References _Verner96Fit(), Badnell06, Cen92, CoolingParams, endrun(), cooling_params::recomb, and Verner96.

Referenced by cool_RecombHp(), init_cooling_rates(), nH0_internal(), recomb_alphaHepp(), and test_recomb_rates().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_heatingcooling_rate()

static void test_heatingcooling_rate ( void **  state)
static

Definition at line 174 of file test_cooling_rates.c.

175 {
176  struct cooling_params coolpar = get_test_coolpar();
177  coolpar.recomb = Cen92;
178  coolpar.cooling = KWH92;
179  coolpar.SelfShieldingOn = 0;
180 
181  const char * TreeCool = GADGET_TESTDATA_ROOT "/examples/TREECOOL_ep_2018p";
182  const char * MetalCool = "";
183 
184  /*unit system*/
185  double HubbleParam = 0.697;
186  double UnitDensity_in_cgs = 6.76991e-22;
187  double UnitTime_in_s = 3.08568e+16;
188  double UnitMass_in_g = 1.989e+43;
189  double UnitLength_in_cm = 3.08568e+21;
190  double UnitEnergy_in_cgs = UnitMass_in_g * pow(UnitLength_in_cm, 2) / pow(UnitTime_in_s, 2);
191  Cosmology CP = {0};
192  CP.OmegaCDM = 0.3;
193  CP.OmegaBaryon = coolpar.fBar * CP.OmegaCDM;
194  CP.HubbleParam = HubbleParam;
195 
196  set_coolpar(coolpar);
197  init_cooling_rates(TreeCool, MetalCool, &CP);
198 
199  struct cooling_units coolunits;
200  coolunits.CoolingOn = 1;
201  coolunits.density_in_phys_cgs = UnitDensity_in_cgs * HubbleParam * HubbleParam;
202  coolunits.uu_in_cgs = UnitEnergy_in_cgs / UnitMass_in_g;
203  coolunits.tt_in_s = UnitTime_in_s / HubbleParam;
204 
205  /*Default values from sfr_eff.c. Some dependence on HubbleParam, so don't change it.*/
206  double egyhot = 2104.92;
207  egyhot *= coolunits.uu_in_cgs;
208 
209  /* convert to physical cgs units */
210  double dens = 0.027755;
212  double ne = 1.0;
213 
214  struct UVBG uvbg = {0};
215  /* XXX: We set the threshold without metal cooling
216  * and with zero ionization at z=0.
217  * It probably make sense to set the parameters with
218  * a metalicity dependence. */
219  double LambdaNet = get_heatingcooling_rate(dens, egyhot, 1 - HYDROGEN_MASSFRAC, 0, 0, &uvbg, &ne);
220 
221  double tcool = egyhot / (- LambdaNet);
222 
223  /*Convert back to internal units*/
224  tcool /= coolunits.tt_in_s;
225 
226  //message(1, "tcool = %g LambdaNet = %g ne=%g\n", tcool, LambdaNet, ne);
227  /* This differs by 0.13% from the old cooling code number,
228  * apparently just because of rounding errors. The excitation cooling
229  * numbers from Cen are not accurate to better than 1% anyway, so don't worry about it*/
230  assert_true(fabs(tcool / 4.68906e-06 - 1) < 1e-3);
231 
232  /*Now check that we get the desired cooling rate with a UVB*/
233  uvbg = get_global_UVBG(0);
234 
235  assert_true(uvbg.epsHep > 0);
236  assert_true(uvbg.gJHe0 > 0);
237 
238  dens /= 100;
239  LambdaNet = get_heatingcooling_rate(dens, egyhot/10., 1 - HYDROGEN_MASSFRAC, 0, 0, &uvbg, &ne);
240  //message(1, "LambdaNet = %g, uvbg=%g\n", LambdaNet, uvbg.epsHep);
241  assert_true(fabs(LambdaNet / (-0.0410059) - 1) < 1e-3);
242 
243  LambdaNet = get_heatingcooling_rate(dens/2.5, egyhot/10., 1 - HYDROGEN_MASSFRAC, 0, 0, &uvbg, &ne);
244  assert_true(LambdaNet > 0);
245  /*Check self-shielding affects the cooling rates*/
246  coolpar.SelfShieldingOn = 1;
247  set_coolpar(coolpar);
248  init_cooling_rates(TreeCool, MetalCool, &CP);
249  LambdaNet = get_heatingcooling_rate(dens*1.5, egyhot/10., 1 - HYDROGEN_MASSFRAC, 0, 0, &uvbg, &ne);
250  //message(1, "LambdaNet = %g, uvbg=%g\n", LambdaNet, uvbg.epsHep);
251  assert_false(LambdaNet > 0);
252  assert_true(fabs(LambdaNet/ (-1.64834) - 1) < 1e-3);
253 }
static struct cooling_units coolunits
Definition: cooling.c:33
struct UVBG get_global_UVBG(double redshift)
double get_heatingcooling_rate(double density, double ienergy, double helium, double redshift, double metallicity, const struct UVBG *uvbg, double *ne_equilib)
void set_coolpar(struct cooling_params cp)
void init_cooling_rates(const char *TreeCoolFile, const char *MetalCoolFile, Cosmology *CP)
@ KWH92
Definition: cooling_rates.h:17
struct @1 MetalCool
#define HYDROGEN_MASSFRAC
Definition: physconst.h:37
#define PROTONMASS
Definition: physconst.h:21
static Cosmology * CP
Definition: power.c:27
static double UnitLength_in_cm
Definition: power.c:26
double HubbleParam
Definition: cosmology.h:20
double OmegaCDM
Definition: cosmology.h:11
double OmegaBaryon
Definition: cosmology.h:19
Definition: cooling.h:8
double epsHep
Definition: cooling.h:13
double gJHe0
Definition: cooling.h:11
enum CoolingType cooling
Definition: cooling_rates.h:28
int CoolingOn
Definition: cooling.h:23
double density_in_phys_cgs
Definition: cooling.h:25
double tt_in_s
Definition: cooling.h:29
double uu_in_cgs
Definition: cooling.h:27
static struct cooling_params get_test_coolpar(void)

References Cen92, cooling_params::cooling, cooling_units::CoolingOn, coolunits, CP, cooling_units::density_in_phys_cgs, UVBG::epsHep, cooling_params::fBar, get_global_UVBG(), get_heatingcooling_rate(), get_test_coolpar(), UVBG::gJHe0, Cosmology::HubbleParam, HYDROGEN_MASSFRAC, init_cooling_rates(), KWH92, MetalCool, Cosmology::OmegaBaryon, Cosmology::OmegaCDM, PROTONMASS, cooling_params::recomb, cooling_params::SelfShieldingOn, set_coolpar(), cooling_units::tt_in_s, UnitLength_in_cm, and cooling_units::uu_in_cgs.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_rate_network()

static void test_rate_network ( void **  state)
static

Definition at line 119 of file test_cooling_rates.c.

120 {
121  struct cooling_params coolpar = get_test_coolpar();
122  const char * TreeCool = GADGET_TESTDATA_ROOT "/examples/TREECOOL_ep_2018p";
123  const char * MetalCool = "";
124 
125  set_coolpar(coolpar);
126  Cosmology CP = {0};
127  CP.OmegaCDM = 0.3;
128  CP.OmegaBaryon = coolpar.fBar * CP.OmegaCDM;
129  CP.HubbleParam = 0.7;
130 
131  init_cooling_rates(TreeCool, MetalCool, &CP);
132 
133  struct UVBG uvbg = get_global_UVBG(2);
134 
135 
136  //Complete ionisation at low density
137  double logt;
138  assert_true( fabs(get_equilib_ne(1e-6, 200.*1e10, 0.24, &logt, &uvbg, 1) / (1e-6*0.76) - (1 + 2* 0.24/(1-0.24)/4)) < 3e-5);
139  assert_true( fabs(get_equilib_ne(1e-6, 200.*1e10, 0.12, &logt, &uvbg, 1) / (1e-6*0.88) - (1 + 2* 0.12/(1-0.12)/4)) < 3e-5);
140  assert_true( fabs(get_equilib_ne(1e-5, 200.*1e10, 0.24, &logt, &uvbg, 1) / (1e-5*0.76) - (1 + 2* 0.24/(1-0.24)/4)) < 3e-4);
141  assert_true( fabs(get_equilib_ne(1e-4, 200.*1e10, 0.24, &logt, &uvbg, 1) / (1e-4*0.76) - (1 + 2* 0.24/(1-0.24)/4)) < 2e-3);
142 
143  double ne = 1.;
144  double temp = get_temp(1e-4, 200.*1e10,0.24, &uvbg, &ne);
145  assert_true(9500 < temp);
146  assert_true(temp < 9510);
147  //Roughly prop to internal energy when ionised
148  assert_true(fabs(get_temp(1e-4, 400.*1e10,0.24, &uvbg, &ne) / get_temp(1e-4, 200.*1e10,0.24, &uvbg, &ne) - 2.) < 1e-3);
149 
150  assert_true(fabs(get_temp(1, 200.*1e10,0.24, &uvbg, &ne) - 14700) < 200);
151 
152  //Neutral fraction prop to density.
153  double dens[3] = {1e-4, 1e-5, 1e-6};
154  int i;
155  for(i = 0; i < 3; i++) {
156  assert_true(fabs(get_neutral_fraction_phys_cgs(dens[i], 200.*1e10,0.24, &uvbg, &ne) / dens[i] - 0.3113) < 1e-3);
157  }
158  //Neutral (self-shielded) at high density:
159  assert_true(get_neutral_fraction_phys_cgs(1, 100.,0.24, &uvbg, &ne) > 0.95);
160  assert_true(0.75 > get_neutral_fraction_phys_cgs(0.1, 100.*1e10,0.24, &uvbg, &ne));
161  assert_true(get_neutral_fraction_phys_cgs(0.1, 100.*1e10,0.24, &uvbg, &ne) > 0.735);
162 
163  //Check self-shielding is working.
164  coolpar.SelfShieldingOn = 0;
165  set_coolpar(coolpar);
166  init_cooling_rates(TreeCool, MetalCool, &CP);
167 
168  assert_true( get_neutral_fraction_phys_cgs(1, 100.*1e10,0.24, &uvbg, &ne) < 0.25);
169  assert_true( get_neutral_fraction_phys_cgs(0.1, 100.*1e10,0.24, &uvbg, &ne) <0.05);
170 }
double get_temp(double density, double ienergy, double helium, const struct UVBG *uvbg, double *ne_init)
double get_equilib_ne(double density, double ienergy, double helium, double *logt, const struct UVBG *uvbg, double ne_init)
double get_neutral_fraction_phys_cgs(double density, double ienergy, double helium, const struct UVBG *uvbg, double *ne_init)

References CP, cooling_params::fBar, get_equilib_ne(), get_global_UVBG(), get_neutral_fraction_phys_cgs(), get_temp(), get_test_coolpar(), Cosmology::HubbleParam, init_cooling_rates(), MetalCool, Cosmology::OmegaBaryon, Cosmology::OmegaCDM, cooling_params::SelfShieldingOn, and set_coolpar().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_recomb_rates()

static void test_recomb_rates ( void **  state)
static

Definition at line 52 of file test_cooling_rates.c.

53 {
54  struct cooling_params coolpar = get_test_coolpar();
55  int i;
56  const char * TreeCool = GADGET_TESTDATA_ROOT "/examples/TREECOOL_ep_2018p";
57  const char * MetalCool = "";
58 
59  Cosmology CP = {0};
60  CP.OmegaCDM = 0.3;
61  CP.OmegaBaryon = coolpar.fBar * CP.OmegaCDM;
62  CP.HubbleParam = 0.7;
63 
64  set_coolpar(coolpar);
65  init_cooling_rates(TreeCool, MetalCool, &CP);
66  for(i=0; i< NEXACT; i++) {
67  assert_true(fabs(recomb_alphaHp(tt[i])/(f92g2[i]+f92n1[i])-1.) < 1e-2);
68  }
69 
70  coolpar.recomb = Cen92;
71  set_coolpar(coolpar);
72  init_cooling_rates(TreeCool, MetalCool, &CP);
73  /*Cen rates are not very accurate.*/
74  for(i=4; i< 12; i++) {
75  assert_true(fabs(recomb_alphaHp(tt[i])/(f92g2[i]+f92n1[i])-1.) < 0.5);
76  }
77 }
double recomb_alphaHp(double temp)
static const double tt[NEXACT]
static const double f92n1[NEXACT]
#define NEXACT
static const double f92g2[NEXACT]

References Cen92, CP, f92g2, f92n1, cooling_params::fBar, get_test_coolpar(), Cosmology::HubbleParam, init_cooling_rates(), MetalCool, NEXACT, Cosmology::OmegaBaryon, Cosmology::OmegaCDM, cooling_params::recomb, recomb_alphaHp(), set_coolpar(), and tt.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_uvbg_loader()

static void test_uvbg_loader ( void **  state)
static

Definition at line 80 of file test_cooling_rates.c.

81 {
82  struct cooling_params coolpar = get_test_coolpar();
83  coolpar.SelfShieldingOn = 1;
84  const char * TreeCool = GADGET_TESTDATA_ROOT "/examples/TREECOOL_ep_2018p";
85  const char * MetalCool = "";
86  set_coolpar(coolpar);
87  Cosmology CP = {0};
88  CP.OmegaCDM = 0.3;
89  CP.OmegaBaryon = coolpar.fBar * CP.OmegaCDM;
90  CP.HubbleParam = 0.7;
91  init_cooling_rates(TreeCool, MetalCool, &CP);
92  /*Test sensible rates at high redshift*/
93  struct UVBG uvbg = get_global_UVBG(16);
94  assert_true(uvbg.epsH0 == 0);
95  assert_true(uvbg.self_shield_dens > 1e8);
96  assert_true(uvbg.gJH0 == 0);
97  /*Test at zero redshift*/
98  uvbg = get_global_UVBG(0);
99  assert_true(fabs(uvbg.epsH0/3.65296e-25 -1) < 1e-5);
100  assert_true(fabs(uvbg.epsHe0/3.98942e-25 -1) < 1e-5);
101  assert_true(fabs(uvbg.epsHep/3.33253e-26 -1) < 1e-5);
102  assert_true(fabs(uvbg.gJH0/6.06e-14 -1) < 1e-5);
103  assert_true(fabs(uvbg.gJHe0/3.03e-14 -1) < 1e-5);
104  assert_true(fabs(uvbg.gJHep/1.1e-15 -1) < 1e-5);
105  assert_true(fabs(uvbg.self_shield_dens/0.0010114161149989826 - 1) < 1e-5);
106  /*Test at intermediate redshift*/
107  uvbg = get_global_UVBG(3.);
108  //message(0, "uvbg %g %g %g %g %g %g %g\n", uvbg.gJH0, uvbg.gJHe0, uvbg.gJHep, uvbg.epsH0, uvbg.epsHe0, uvbg.epsHep, uvbg.self_shield_dens);
109  assert_true(fabs(uvbg.epsH0/5.96570906168362e-24 -1) < 1e-5);
110  assert_true(fabs(uvbg.epsHe0/4.466976578202419e-24 -1) < 1e-5);
111  assert_true(fabs(uvbg.epsHep/2.758535690259892e-26 -1) < 1e-5);
112  assert_true(fabs(uvbg.gJH0/1.0549960730284017e-12 -1) < 1e-5);
113  assert_true(fabs(uvbg.gJHe0/4.759025257653999e-13 -1) < 1e-5);
114  assert_true(fabs(uvbg.gJHep/2.270599708640625e-16 -1) < 1e-5);
115  assert_true(fabs(uvbg.self_shield_dens/0.007691709693529007 - 1) < 1e-5);
116 }
double epsH0
Definition: cooling.h:12
double gJHep
Definition: cooling.h:10
double gJH0
Definition: cooling.h:9
double self_shield_dens
Definition: cooling.h:15
double epsHe0
Definition: cooling.h:14

References CP, UVBG::epsH0, UVBG::epsHe0, UVBG::epsHep, cooling_params::fBar, get_global_UVBG(), get_test_coolpar(), UVBG::gJH0, UVBG::gJHe0, UVBG::gJHep, Cosmology::HubbleParam, init_cooling_rates(), MetalCool, Cosmology::OmegaBaryon, Cosmology::OmegaCDM, UVBG::self_shield_dens, cooling_params::SelfShieldingOn, and set_coolpar().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ f92g2

const double f92g2[NEXACT] = {5.758e-11, 2.909e-11, 1.440e-11, 6.971e-12,3.282e-12, 1.489e-12, 6.43e-13, 2.588e-13, 9.456e-14, 3.069e-14, 8.793e-15, 2.245e-15, 5.190e-16, 1.107e-16, 2.221e-17, 4.267e-18, 7.960e-19, 1.457e-19,2.636e-20, 4.737e-21}
static

Definition at line 29 of file test_cooling_rates.c.

Referenced by test_recomb_rates().

◆ f92n1

const double f92n1[NEXACT] = {9.258e-12, 5.206e-12, 2.927e-12, 1.646e-12, 9.246e-13, 5.184e-13, 2.890e-13, 1.582e-13, 8.255e-14, 3.882e-14, 1.545e-14, 5.058e-15, 1.383e-15, 3.276e-16, 7.006e-17, 1.398e-17, 2.665e-18, 4.940e-19, 9.001e-20, 1.623e-20}
static

Definition at line 31 of file test_cooling_rates.c.

Referenced by test_recomb_rates().

◆ tt

const double tt[NEXACT] = {3.16227766e+00, 1.0e+01, 3.16227766e+01, 1.0e+02, 3.16227766e+02, 1.00e+03, 3.16227766e+03, 1.e+04, 3.16227766e+04, 1.e+05, 3.16227766e+05, 1.e+06, 3.16227766e+06, 1.0e+07, 3.16227766e+07, 1.0e+08, 3.16227766e+08, 1.0e+09, 3.16227766e+09, 1.0e+10}
static

Definition at line 32 of file test_cooling_rates.c.

Referenced by init_cooling_rates(), and test_recomb_rates().