Advertisement
mabruzzo

sanity_check.py

Dec 1st, 2022
1,162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.74 KB | None | 0 0
  1. from pygrackle import chemistry_data
  2.  
  3. from pygrackle.grackle_wrapper import _test_dynamic_access
  4.  
  5.  
  6. # the following shows the old body of _set_default_chemistry_parameters(void).
  7. #   - I copied lines 41-136 from
  8. #      https://github.com/grackle-project/grackle/blob/d062c199c7d28011dd4ff3f7a6ba3ce727daaa90/src/clib/set_default_chemistry_parameters.c
  9. #   - and then I removed all comments
  10.  
  11. _set_default_chemistry_parameters_body = """
  12.  my_chemistry.Gamma                          = 5./3.;
  13.  my_chemistry.use_grackle                    = FALSE;
  14.  my_chemistry.with_radiative_cooling         = TRUE;
  15.  my_chemistry.primordial_chemistry           = 0;
  16.  my_chemistry.dust_chemistry                 = 0;
  17.  my_chemistry.metal_cooling                  = FALSE;
  18.  my_chemistry.h2_on_dust                     = FALSE;
  19.  my_chemistry.use_dust_density_field         = FALSE;
  20.  
  21.  my_chemistry.cmb_temperature_floor          = TRUE;
  22.  my_chemistry.grackle_data_file              = "";
  23.  
  24.  my_chemistry.three_body_rate                = 0;
  25.  my_chemistry.cie_cooling                    = 0;
  26.  my_chemistry.h2_optical_depth_approximation = 0;
  27.  
  28.  my_chemistry.h2_charge_exchange_rate        = 1;
  29.  
  30.  my_chemistry.h2_dust_rate                    = 1;
  31.  
  32.  my_chemistry.h2_h_cooling_rate              = 1;
  33.  
  34.  my_chemistry.collisional_excitation_rates   = 1;
  35.  my_chemistry.collisional_ionisation_rates   = 1;
  36.  my_chemistry.recombination_cooling_rates    = 1;
  37.  my_chemistry.bremsstrahlung_cooling_rates   = 1;
  38.  
  39.  my_chemistry.dust_recombination_cooling     = -1;
  40.  my_chemistry.photoelectric_heating          = -1;
  41.  
  42.  my_chemistry.photoelectric_heating_rate     = 8.5e-26;
  43.  my_chemistry.use_isrf_field                 = 0;
  44.  my_chemistry.interstellar_radiation_field   = 1.7;
  45.  
  46.  my_chemistry.use_volumetric_heating_rate    = 0;
  47.  my_chemistry.use_specific_heating_rate      = 0;
  48.  
  49.  my_chemistry.UVbackground                   = 0;
  50.  
  51.  my_chemistry.UVbackground_redshift_on      = FLOAT_UNDEFINED;
  52.  my_chemistry.UVbackground_redshift_off     = FLOAT_UNDEFINED;
  53.  my_chemistry.UVbackground_redshift_fullon  = FLOAT_UNDEFINED;
  54.  my_chemistry.UVbackground_redshift_drop    = FLOAT_UNDEFINED;
  55.  
  56.  my_chemistry.Compton_xray_heating   = 0;
  57.  
  58.  my_chemistry.LWbackground_intensity = 0.0;
  59.  my_chemistry.LWbackground_sawtooth_suppression = 0;
  60.  
  61.  my_chemistry.HydrogenFractionByMass       = 0.76;
  62.  my_chemistry.DeuteriumToHydrogenRatio     = 2.0*3.4e-5;
  63.  
  64.  my_chemistry.SolarMetalFractionByMass     = 0.01295;
  65.  
  66.  my_chemistry.local_dust_to_gas_ratio      = 0.009387;
  67.  
  68.  my_chemistry.NumberOfTemperatureBins      = 600;
  69.  my_chemistry.ih2co                        = 1;
  70.  my_chemistry.ipiht                        = 1;
  71.  my_chemistry.TemperatureStart             = 1.0;
  72.  my_chemistry.TemperatureEnd               = 1.0e9;
  73.  my_chemistry.CaseBRecombination           = 0;
  74.  my_chemistry.NumberOfDustTemperatureBins  = 250;
  75.  my_chemistry.DustTemperatureStart         = 1.0;
  76.  my_chemistry.DustTemperatureEnd           = 1500.0;
  77.  
  78.  my_chemistry.cloudy_electron_fraction_factor = 9.153959e-3;
  79.  
  80.  my_chemistry.use_radiative_transfer                 = 0;
  81.  my_chemistry.radiative_transfer_coupled_rate_solver = 0;
  82.  my_chemistry.radiative_transfer_intermediate_step   = 0;
  83.  my_chemistry.radiative_transfer_hydrogen_only       = 0;
  84.  
  85.  my_chemistry.self_shielding_method                  = 0;
  86.  my_chemistry.H2_self_shielding                      = 0;
  87.  my_chemistry.H2_custom_shielding                    = 0;
  88. """
  89.  
  90.  
  91. def _get_default_vals():
  92.  
  93.     # taken from grackle_macros.h
  94.     substitutions = {'FLOAT_UNDEFINED' : -99999.0, 'INT_UNDEFINED': -99999,
  95.                      'FALSE' : 0, 'TRUE' : 1}
  96.  
  97.     out = {}
  98.     for line in _set_default_chemistry_parameters_body.splitlines():
  99.         line = line.strip()
  100.         if line == '':
  101.             continue
  102.         assert line[:13] == 'my_chemistry.' and line[-1] == ';'
  103.         name, expression = [elem.strip() for elem in line[13:-1].split('=')]
  104.         out[name] = eval(expression, substitutions)
  105.     return out
  106.  
  107. ref_defaults = _get_default_vals()
  108.  
  109. my_chem = chemistry_data()
  110.  
  111. matching_names, mismatching_names = [], []
  112.  
  113. for name, ref_val in ref_defaults.items():
  114.     if name == 'grackle_data_file':
  115.         continue
  116.     actual_val = _test_dynamic_access(my_chem, name,
  117.                                       is_int_type = isinstance(ref_val, int))
  118.     assert actual_val is not None
  119.     if actual_val == ref_val:
  120.         matching_names.append(name)
  121.     else:
  122.         print(
  123.             f'mismatch in "{name}", actual = {actual_val!r}, ref = {ref_val!r}'
  124.         )
  125.         mismatching_names.append(name)
  126.    
  127. print('Number of mismatches: ', len(mismatching_names))
  128. print('Number of matches:', len(matching_names))
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement