lesgourg / class_public

Public repository of the Cosmic Linear Anisotropy Solving System (master for the most recent version of the standard code; GW_CLASS to include Cosmic Gravitational Wave Background anisotropies; classnet branch for acceleration with neutral networks; ExoCLASS branch for exotic energy injection; class_matter branch for FFTlog)
223 stars 291 forks source link

Outputting primordial spectrum #325

Open NolanSmyth opened 4 years ago

NolanSmyth commented 4 years ago

I'm trying to find the result of CLASS's calculation of the primordial scalar spectrum. In the explanitory.ini, I see the option "write primordial". I tried setting this to 'yes', but nothing gets written (even after double checking the root path). Since I'm using the python wrapper in jupyter lab I tried using get_primordial(), but to no avail. I get

TypeError: a bytes-like object is required, not 'str'

When I call get_perturbations as another test, it just returns an empty dict.

Am I neglecting to input some required parameter? I'm using the default external power spectrum for now, but plan to change it to my own file later once I have this working. Thank you!

LambdaCDM_extPK = Class()

LambdaCDM_extPK.set({'omega_b':0.022032,'omega_cdm':0.12038,'h':0.67556,'tau_reio':0.0925})
LambdaCDM_extPK.set({'output':'tCl,pCl,lCl,mPk','lensing':'yes','P_k_max_1/Mpc':3.0})

LambdaCDM_extPK.set({'P_k_ini type':'external_Pk', 'command':'cat external_Pk/Pk_example.dat'})

LambdaCDM_extPK.compute()

LambdaCDM_extPK.get_primordial()
lesgourg commented 4 years ago

Dear Nolan, You are right, the classy function .get_primordial() had a compatibility issue with python3 that I had not noticed. The fix consists in inserting a line in L1467:

        tmp = <bytes> titles
        tmp = str(tmp.decode())      #<<< this is the new line that fixes the problem
        names = tmp.split("\t")[:-1]

The other similar functions get_background(), get_thermodynamics(), get_perturbations() do work, they include such a fix already. I checked it explicitly by running successfully the notebook

from classy import Class
LambdaCDM_extPK = Class()
LambdaCDM_extPK.set({'omega_b':0.022032,'omega_cdm':0.12038,'h':0.67556,'tau_reio':0.0925,'modes':'s,t'})
LambdaCDM_extPK.set({'output':'tCl,pCl,lCl,mPk','lensing':'yes','P_k_max_1/Mpc':3.0})
LambdaCDM_extPK.set({'write background':'yes'})
LambdaCDM_extPK.set({'write thermodynamics':'yes'})
LambdaCDM_extPK.set({'write primordial':'yes','P_k_ini type':'external_Pk', 'command':'cat ../external_Pk/Pk_example.dat'})
LambdaCDM_extPK.set({'k_output_values':'0.1,0.2'})
LambdaCDM_extPK.compute()
LambdaCDM_extPK.get_background()
LambdaCDM_extPK.get_thermodynamics()
LambdaCDM_extPK.get_primordial()
LambdaCDM_extPK.get_perturbations()

It gave me the expected dictionaries. You got an empty dictionary with get_perturbations() because you had not required something with 'k_output_values'. The missing line in .get_primordial() will be fixed in a forthcoming release. In the meantime, let us leave this issue open. many thanks for your help.

NolanSmyth commented 4 years ago

Beautiful. This fixed my issue after recompiling. Thank you so much!