Convert configuration of model from wrappers camb and class into respectively params.ini and explanatory.ini

Use of Cobaya. camb, CLASS, cosmomc, compilers, etc.
Post Reply
Dournac Fabien
Posts: 70
Joined: May 18 2019
Affiliation: IRAP
Contact:

Convert configuration of model from wrappers camb and class into respectively params.ini and explanatory.ini

Post by Dournac Fabien » June 13 2022

Hello,

I use wrappers for CAMB and CLASS. I have the following configuration to generate Matter/Angular power scpectrum.

1) for wrapper CAMB

Code: Select all

 # Dictionary with cosmological parameters
cosmo = {'h':0.67810,
       'omega_b':0.02238280,
       'omega_cdm': 0.1201075,
       'A_s':2.100549e-09,
       'tau_reio': 0.05430842,
       'n_s':0.9660499}

kmin = 1e-4 # Mpc
kmax = 5 # Mpc
npoints = 1000 # Number of elements in the Pk array
lmax = 2500 # Maximum multiple calculated for the CLs

# Start a CAMB parameters object
pars = camb.CAMBparams()
# Specify the parameters
pars.set_cosmology(H0=cosmo['h']*100, ombh2=cosmo['omega_b'], omch2=cosmo['omega_cdm'], tau=cosmo['tau_reio'])
pars.InitPower.set_params(As=cosmo['A_s'], ns=cosmo['n_s'])
pars.set_matter_power(redshifts=[0.], kmax=kmax, nonlinear=True)
pars.set_for_lmax(lmax - 50) # CAMB calculates 50 more than you ask it to (probably a bug)

# Calculate non-linear power spectrum
#pars.NonLinear = camb.model.NonLinear_none
pars.NonLinear = camb.model.NonLinear_both
pars.NonLinearModel.set_params(halofit_version='casarini')
results = camb.get_results(pars)
kh, z, pk_camb = results.get_matter_power_spectrum(minkh=kmin, maxkh=kmax, npoints = npoints)
1) for wrapper CLASS :

Code: Select all

# create instance of the class "Class"
LambdaCDM = Class()
# pass input parameters
LambdaCDM.set(cosmo)
LambdaCDM.set({'output':'mPk, tCl, lCl','P_k_max_1/Mpc':3.0, 'lensing':'yes', 'non linear':'halofit'})
# run class
LambdaCDM.compute()

# get P(k) at redhsift z=0
pk_class = [] # P(k) in (Mpc/h)**3
for k in kh:
    pk_class.append(LambdaCDM.pk(k*cosmo['h'],0.)*cosmo['h']**3) # function .pk(k,z)
Is there a way to generate for CAMB and CLASS respectively a params.ini and explanotary.in from their configuration above in the wrapper.

It would be a kind of exporting the configuration from a wrapper by writting the standard files params.ini and explanotary.ini.

Best regards.

Dournac Fabien
Posts: 70
Joined: May 18 2019
Affiliation: IRAP
Contact:

Re: Convert configuration of model from wrappers camb and class into respectively params.ini and explanatory.ini

Post by Dournac Fabien » July 23 2022

Regarding the case of CAMB wrapper, I would like to reverse the generation of a " params.ini " file, that is generating the input file from parameters set into the camb wrapper. Currently, I can do inside python wrapper :

Code: Select all

>>> import camb
>>> pars=camb.read_ini(os.path.join(camb_path,'inifiles','planck_2018.ini'))
>>> print(pars)
and I get the following configuration :

Code: Select all

class: <CAMBparams>
 WantCls = True
 WantTransfer = True
 WantScalars = True
 WantTensors = False
 WantVectors = False
 WantDerivedParameters = True
 Want_cl_2D_array = True
 Want_CMB = True
 Want_CMB_lensing = True
 DoLensing = True
 NonLinear = NonLinear_both
 Transfer: <TransferParams>
   high_precision = True
   accurate_massive_neutrinos = False
   kmax = 1.3464234
   k_per_logint = 0
   PK_num_redshifts = 1
   PK_redshifts = [0.0]
 want_zstar = False
 want_zdrag = False
 min_l = 2
 max_l = 2700
 max_l_tensor = 600
 max_eta_k = 18000.0
 max_eta_k_tensor = 1200.0
 ombh2 = 0.0223828
 omch2 = 0.1201075
 omk = 0.0
 omnuh2 = 0.0006451439
 H0 = 67.32117
 TCMB = 2.7255
 YHe = 0.2454006
 num_nu_massless = 2.046
 num_nu_massive = 1
 nu_mass_eigenstates = 1
 share_delta_neff = True
 nu_mass_degeneracies = [0.0]
 nu_mass_fractions = [1.0]
 nu_mass_numbers = [1]
 InitPower: <InitialPowerLaw>
   tensor_parameterization = tensor_param_indeptilt
   ns = 0.9660499
   nrun = 0.0
   nrunrun = 0.0
   nt = 0.0
   ntrun = 0.0
   r = 0.0
   pivot_scalar = 0.05
   pivot_tensor = 0.05
   As = 2.100549e-09
   At = 0.0
 Recomb: <Recfast>
   min_a_evolve_Tm = 0.0011098779505118728
   RECFAST_fudge = 1.125
   RECFAST_fudge_He = 0.86
   RECFAST_Heswitch = 6
   RECFAST_Hswitch = True
   AGauss1 = -0.14
   AGauss2 = 0.079
   zGauss1 = 7.28
   zGauss2 = 6.73
   wGauss1 = 0.18
   wGauss2 = 0.33
 Reion: <TanhReionization>
   Reionization = True
   use_optical_depth = True
   redshift = 10.0
   optical_depth = 0.05430842
   delta_redshift = 0.5
   fraction = -1.0
   include_helium_fullreion = True
   helium_redshift = 3.5
   helium_delta_redshift = 0.5
   helium_redshiftstart = 6.0
   tau_solve_accuracy_boost = 1.0
   timestep_boost = 1.0
   max_redshift = 50.0
 DarkEnergy: <DarkEnergyPPF>
   w = -1.0
   wa = 0.0
   cs2 = 1.0
   use_tabulated_w = False
 NonLinearModel: <Halofit>
   Min_kh_nonlinear = 0.005
   halofit_version = mead2016
   HMCode_A_baryon = 3.13
   HMCode_eta_baryon = 0.603
   HMCode_logT_AGN = 7.8
 Accuracy: <AccuracyParams>
   AccuracyBoost = 1.0
   lSampleBoost = 1.0
   lAccuracyBoost = 1.0
   AccuratePolarization = True
   AccurateBB = False
   AccurateReionization = True
   TimeStepBoost = 1.0
   BackgroundTimeStepBoost = 1.0
   IntTolBoost = 1.0
   SourcekAccuracyBoost = 1.0
   IntkAccuracyBoost = 1.0
   TransferkBoost = 1.0
   NonFlatIntAccuracyBoost = 1.0
   BessIntBoost = 1.0
   LensingBoost = 1.0
   NonlinSourceBoost = 1.0
   BesselBoost = 1.0
   LimberBoost = 1.0
   SourceLimberBoost = 1.0
   KmaxBoost = 1.0
   neutrino_q_boost = 1.0
 SourceTerms: <SourceTermParams>
   limber_windows = True
   limber_phi_lmin = 100
   counts_density = True
   counts_redshift = True
   counts_lensing = False
   counts_velocity = True
   counts_radial = False
   counts_timedelay = True
   counts_ISW = True
   counts_potential = True
   counts_evolve = False
   line_phot_dipole = False
   line_phot_quadrupole = False
   line_basic = True
   line_distortions = True
   line_extra = False
   line_reionization = False
   use_21cm_mK = True
 z_outputs = []
 scalar_initial_condition = initial_adiabatic
 InitialConditionVector = []
 OutputNormalization = 1
 Alens = 1.0
 MassiveNuMethod = Nu_trunc
 DoLateRadTruncation = True
 Evolve_baryon_cs = False
 Evolve_delta_xe = False
 Evolve_delta_Ts = False
 Do21cm = False
 transfer_21cm_cl = False
 Log_lvalues = False
 use_cl_spline_template = True
 SourceWindows = []
 CustomSources: <CustomSources>
   num_custom_sources = 0
   c_source_func = None
   custom_source_ell_scales = []
I tried to store this output by np.savetxt('params.ini', pars) :

Code: Select all

>>> np.savetxt('param.ini',pars)
Traceback (most recent call last):
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.9/site-packages/numpy/core/_internal.py", line 597, in _dtype_from_pep3118
    dtype, align = __dtype_from_pep3118(stream, is_subdtype=False)
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.9/site-packages/numpy/core/_internal.py", line 651, in __dtype_from_pep3118
    value, align = __dtype_from_pep3118(
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.9/site-packages/numpy/core/_internal.py", line 651, in __dtype_from_pep3118
    value, align = __dtype_from_pep3118(
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.9/site-packages/numpy/core/_internal.py", line 674, in __dtype_from_pep3118
    raise ValueError("Unknown PEP 3118 data type specifier %r" % stream.s)
ValueError: Unknown PEP 3118 data type specifier 'P:allocatable:}:_InitPower:T{(16)<P:allocatable:}:_Recomb:T{(16)<P:allocatable:}:_Reion:T{(16)<P:allocatable:}:_DarkEnergy:T{(16)<P:allocatable:}:_NonLinearModel:T{<d:AccuracyBoost:<d:lSampleBoost:<d:lAccuracyBoost:<i:_AccuratePolarization:<i:_AccurateBB:<i:_AccurateReionization:<d:TimeStepBoost:<d:BackgroundTimeStepBoost:<d:IntTolBoost:<d:SourcekAccuracyBoost:<d:IntkAccuracyBoost:<d:TransferkBoost:<d:NonFlatIntAccuracyBoost:<d:BessIntBoost:<d:LensingBoost:<d:NonlinSourceBoost:<d:BesselBoost:<d:LimberBoost:<d:SourceLimberBoost:<d:KmaxBoost:<d:neutrino_q_boost:}:Accuracy:T{<i:_limber_windows:<i:limber_phi_lmin:<i:_counts_density:<i:_counts_redshift:<i:_counts_lensing:<i:_counts_velocity:<i:_counts_radial:<i:_counts_timedelay:<i:_counts_ISW:<i:_counts_potential:<i:_counts_evolve:<i:_line_phot_dipole:<i:_line_phot_quadrupole:<i:_line_basic:<i:_line_distortions:<i:_line_extra:<i:_line_reionization:<i:_use_21cm_mK:}:SourceTerms:T{(9)<P:allocatable:}:_z_outputs:<i:_scalar_initial_condition:T{(9)<P:allocatable:}:_InitialConditionVector:<i:OutputNormalization:<d:Alens:<i:_MassiveNuMethod:<i:_DoLateRadTruncation:<i:_Evolve_baryon_cs:<i:_Evolve_delta_xe:<i:_Evolve_delta_Ts:<i:_Do21cm:<i:_transfer_21cm_cl:<i:_Log_lvalues:<i:_use_cl_spline_template:T{(9)<P:allocatable:}:_SourceWindows:T{<i:num_custom_sources:<P:c_source_func:T{(9)<P:allocatable:}:_custom_source_ell_scales:}:CustomSources:}'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<__array_function__ internals>", line 5, in savetxt
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.9/site-packages/numpy/lib/npyio.py", line 1378, in savetxt
    X = np.asarray(X)
ValueError: 'T{<i:_WantCls:<i:_WantTransfer:<i:_WantScalars:<i:_WantTensors:<i:_WantVectors:<i:_WantDerivedParameters:<i:_Want_cl_2D_array:<i:_Want_CMB:<i:_Want_CMB_lensing:<i:_DoLensing:<i:_NonLinear:T{<i:_high_precision:<i:_accurate_massive_neutrinos:<d:kmax:<i:k_per_logint:<i:PK_num_redshifts:(150)<d:_PK_redshifts:}:Transfer:<i:_want_zstar:<i:_want_zdrag:<i:min_l:<i:max_l:<i:max_l_tensor:<d:max_eta_k:<d:max_eta_k_tensor:<d:ombh2:<d:omch2:<d:omk:<d:omnuh2:<d:H0:<d:TCMB:<d:YHe:<d:num_nu_massless:<i:num_nu_massive:<i:nu_mass_eigenstates:<i:_share_delta_neff:(5)<d:_nu_mass_degeneracies:(5)<d:_nu_mass_fractions:(5)<i:_nu_mass_numbers:T{(16)<P:allocatable:}:_InitPower:T{(16)<P:allocatable:}:_Recomb:T{(16)<P:allocatable:}:_Reion:T{(16)<P:allocatable:}:_DarkEnergy:T{(16)<P:allocatable:}:_NonLinearModel:T{<d:AccuracyBoost:<d:lSampleBoost:<d:lAccuracyBoost:<i:_AccuratePolarization:<i:_AccurateBB:<i:_AccurateReionization:<d:TimeStepBoost:<d:BackgroundTimeStepBoost:<d:IntTolBoost:<d:SourcekAccuracyBoost:<d:IntkAccuracyBoost:<d:TransferkBoost:<d:NonFlatIntAccuracyBoost:<d:BessIntBoost:<d:LensingBoost:<d:NonlinSourceBoost:<d:BesselBoost:<d:LimberBoost:<d:SourceLimberBoost:<d:KmaxBoost:<d:neutrino_q_boost:}:Accuracy:T{<i:_limber_windows:<i:limber_phi_lmin:<i:_counts_density:<i:_counts_redshift:<i:_counts_lensing:<i:_counts_velocity:<i:_counts_radial:<i:_counts_timedelay:<i:_counts_ISW:<i:_counts_potential:<i:_counts_evolve:<i:_line_phot_dipole:<i:_line_phot_quadrupole:<i:_line_basic:<i:_line_distortions:<i:_line_extra:<i:_line_reionization:<i:_use_21cm_mK:}:SourceTerms:T{(9)<P:allocatable:}:_z_outputs:<i:_scalar_initial_condition:T{(9)<P:allocatable:}:_InitialConditionVector:<i:OutputNormalization:<d:Alens:<i:_MassiveNuMethod:<i:_DoLateRadTruncation:<i:_Evolve_baryon_cs:<i:_Evolve_delta_xe:<i:_Evolve_delta_Ts:<i:_Do21cm:<i:_transfer_21cm_cl:<i:_Log_lvalues:<i:_use_cl_spline_template:T{(9)<P:allocatable:}:_SourceWindows:T{<i:num_custom_sources:<P:c_source_func:T{(9)<P:allocatable:}:_custom_source_ell_scales:}:CustomSources:}' is not a valid PEP 3118 buffer format string
Is there a way to store in a text file (like the default " params.ini " file) the content of variable " pars " ?

Regards

Antony Lewis
Posts: 1790
Joined: September 23 2004
Affiliation: University of Sussex
Contact:

Re: Convert configuration of model from wrappers camb and class into respectively params.ini and explanatory.ini

Post by Antony Lewis » July 23 2022

No. A PR would be welcome to support full round trip python __repr__ of the params object.

Dournac Fabien
Posts: 70
Joined: May 18 2019
Affiliation: IRAP
Contact:

Re: Convert configuration of model from wrappers camb and class into respectively params.ini and explanatory.ini

Post by Dournac Fabien » July 24 2022

Hello,

I have just noticed with version '1.3.5' a discrepancy between the values into planck_2018.ini and the values loaded from this file inside CAMB wrapper :

For example, in planck_2018.ini, one has :

Code: Select all

#If re_use_optical_depth = F then use following, otherwise ignored
re_redshift          = 11
And if we loaded this file with CAMB wrapper, we get :

Code: Select all

>>> import camb
>>> pars=camb.read_ini(os.path.join(camb_path,'inifiles','planck_2018.ini'))
>>> pars

Reion: <TanhReionization>
   Reionization = True
   use_optical_depth = True
   redshift = 10.0
 
As you can see, re_redshift is equal to 11 for the first case and 10 for pars loaded into wrapper.

Could anyone explain me this difference ?

Regards

Antony Lewis
Posts: 1790
Joined: September 23 2004
Affiliation: University of Sussex
Contact:

Re: Convert configuration of model from wrappers camb and class into respectively params.ini and explanatory.ini

Post by Antony Lewis » July 25 2022

The value is not used because

Code: Select all

use_topical_depth=True

Post Reply