Cobaya: error adding new parameter to CAMB

Use of Cobaya. camb, CLASS, cosmomc, compilers, etc.
Post Reply
joby pk
Posts: 12
Joined: February 26 2016
Affiliation: Asia Pacific Institute for Theoretical Physics

Cobaya: error adding new parameter to CAMB

Post by joby pk » November 18 2022

Hello!

I am trying to add a new parameter to CAMB (nc_alpha) which modifies the primordial scalar power spectra. I have not made any modifications to Cobaya. I am using the editable version of CAMB 1.3.5. I follow the instructions given in the cobaya/camb modification section:

1) add the new parameter in CAMBParams type in model.f90 in the CAMB/fortran directory
2) add the new parameter in CAMBParams class in model.py in the CAMB/camb directory
3) compile camb using "make all" in the CAMB/fortran directory
4) run "python setup.py build" in the CAMB directory.
5) run cobaya with modified CAMB

I get the following error:

File "/Users/joby/software/camb_edit/CAMB/build/lib/camb/model.py", line 191, in CAMBparams
("nc_alpha", nc_alpha, "noncommutativity parameter alpha"),
NameError: name 'nc_alpha' is not defined

Any reply or suggestions for fixing the error is appreciated.

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

Re: Cobaya: error adding new parameter to CAMB

Post by Antony Lewis » November 18 2022

The second parameter is supposed to be a ctype, following the pattern of other variables

joby pk
Posts: 12
Joined: February 26 2016
Affiliation: Asia Pacific Institute for Theoretical Physics

Re: Cobaya: error adding new parameter to CAMB

Post by joby pk » November 18 2022

Thank you Antony! I corrected the type definition for my new parameter and that fixed the issue. However, when I try to run cobaya to estimate LCDM parameters with my modification to the primordial power spectra in Initialpower.f90, I get the following error:

"cobaya.log.LoggedError: Could not find random point giving finite posterior after 1320 tries"

At first I thought it might have to do with how the new parameter modifies the initial power spectra. So I gave a test run with a simple modification to the initial power as: scalar_power = A_s * ((k/k0)^(n_s-1)) * nc_alpha, with nc_alpha fixed to 1.0 so that it is effectively the same as the default initial power spectra. I still get the same error about cobaya not being able to find the initial random point with finite posterior. I have tried some other modifications to the initial power and I keep getting the same error.

Is there anything I could check to figure out the source of this error?

joby pk
Posts: 12
Joined: February 26 2016
Affiliation: Asia Pacific Institute for Theoretical Physics

Re: Cobaya: error adding new parameter to CAMB

Post by joby pk » November 19 2022

I noticed that extra parameter I added in CAMBtypes in model.f90 and model.py, is not taking the value I set in the .yaml file of cobaya.

I have added the extra parameter nc_alpha in the Tinitialpowerlaw_scalarpower in the Initialpower.f90 file, and set the same parameter value in the .yaml file of Cobaya. However when I run Cobaya, it seems that nc_alpha does not take the specified value in the .yaml file, and instead just stays at it's initialized value of 1.0.

Is there any way I can tell CAMB to use the nc_alpha value specified in the .yaml file instead of the default initialization value?

Any comments or suggestions are appreciated.

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

Re: Cobaya: error adding new parameter to CAMB

Post by Antony Lewis » November 19 2022

Did you add to InitialPowerLaw.set_params?

joby pk
Posts: 12
Joined: February 26 2016
Affiliation: Asia Pacific Institute for Theoretical Physics

Re: Cobaya: error adding new parameter to CAMB

Post by joby pk » November 20 2022

Sorry I don't know how to do that. Is it done by modifying the following lines in model.py?

def set_initial_power(self, initial_power_params):
"""
Set the InitialPower primordial power spectrum parameters

:param initial_power_params: :class:`.initialpower.InitialPowerLaw`
or :class:`.initialpower.SplinedInitialPower` instance
:return: self
"""
self.InitPower = initial_power_params
return self

If I want to tell CAMB that the value stored in the CAMBtype object this%nc_alpha must be passed to the initialpower parameter nc_alpha, how do I make the modification?

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

Re: Cobaya: error adding new parameter to CAMB

Post by Antony Lewis » November 20 2022

in initialpower.py

joby pk
Posts: 12
Joined: February 26 2016
Affiliation: Asia Pacific Institute for Theoretical Physics

Re: Cobaya: error adding new parameter to CAMB

Post by joby pk » November 20 2022

Antony Lewis wrote:
November 20 2022
in initialpower.py
I suppose you mean the following lines in initialpower.py, class InitialPowerLaw(InitialPower):

def __init__(self, **kwargs):
self.set_params(**kwargs)

def set_params(self, As=2e-9, ns=0.96, nrun=0, nrunrun=0.0, r=0.0, nt=None, ntrun=0.0,
pivot_scalar=0.05, pivot_tensor=0.05, nc_alpha=1.0, parameterization="tensor_param_rpivot"):

I have modified set_params to add the extra parameter nc_alpha, but I don't know how to the pass the param value "nc_alpha" mentioned in the .yaml file, to this variable in initialpower.py. When I check the execution of camb, it is still using nc_alpha = 1.0 instead of the value mentioned in the .yaml file used with Cobaya.

Would really appreciate if you could clarify this :)

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

Re: Cobaya: error adding new parameter to CAMB

Post by Antony Lewis » November 21 2022

You need to the variable to the InitialPowerLaw class, in both python and fortran, add the argument to InitialPowerLaw.set_params in python and assign it in the method code. If you have a problem with a specific implementation that you cannot debug yourself, best to provide a github link to the full modified code - preferable with all changes in one commit.

joby pk
Posts: 12
Joined: February 26 2016
Affiliation: Asia Pacific Institute for Theoretical Physics

Re: Cobaya: error adding new parameter to CAMB

Post by joby pk » November 21 2022

Hi Antony,

I want to check whether I have made the modifications correctly. If this is a bug, then I can share the modified code on github.
I have added the extra variable nc_alpha to the InitialPowerLaw class in Initialpower.f90 and also to initialpower.py in the same order of the arguments:
Antony Lewis wrote:
November 21 2022
add the argument to InitialPowerLaw.set_params in python and assign it in the method code.
Could you please elaborate on this a little bit? Does it mean to modify the following lines in the InitialPowerLaw class in initialpower.py:

def set_params(self, As=2e-9, ns=0.96, nrun=0, nrunrun=0.0, r=0.0, nt=None, ntrun=0.0,
pivot_scalar=0.05, pivot_tensor=0.05, nc_alpha=1.0, parameterization="tensor_param_rpivot"):

If you could give an example of how to do this last step regarding adding the parameter to InitialPowerLaw.set_params and assigning it to the method code, then I could use it to make my own modifications. Eventually, I want nc_alpha to be this extra parameter in the primordial power spectra for which I will estimate the posterior using Cobaya.

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

Re: Cobaya: error adding new parameter to CAMB

Post by Antony Lewis » November 22 2022

Yes, just follow the pattern of the existing code for As.

joby pk
Posts: 12
Joined: February 26 2016
Affiliation: Asia Pacific Institute for Theoretical Physics

Re: Cobaya: error adding new parameter to CAMB

Post by joby pk » November 23 2022

Hi Antony,

I followed your instructions and I was able to get the modified code working! Thank you so much :)

Post Reply