class_public icon indicating copy to clipboard operation
class_public copied to clipboard

Error in initiating multiple instance of CLASS in DCDM

Open ShazAlvi opened this issue 3 years ago • 15 comments

Following the issue #414 , I have a similar issue when I consider DCDM.

Here is the CLASSY code that I am running.

from classy import Class
import numpy as np
import matplotlib.pyplot as plt

params = {'output': 'tCl', 'omega_b': 0.022032,'omega_ini_dcdm': 0.12038, 'Gamma_dcdm': 0.0, 'ln10^{10}A_s': 3.0980, 'n_s': 0.9619,
                '100*theta_s': 1.04119, 'tau_reio': 0.0925,'w0_fld':-1.0, 'wa_fld':0.0, 'Omega_Lambda': 0.0,
                'non linear': 'halofit'}
MyClass = Class()
MyClass.set(params)
MyClass.compute()
TT = MyClass.raw_cl()
factor = TT['ell'][2:]*(TT['ell'][2:] + 1)*(2*np.pi)**-1
plt.loglog(TT['ell'][2:], factor*TT['tt'][2:])
MyClass.empty()
params = {'output': 'tCl', 'omega_b': 0.022032,'omega_ini_dcdm': 0.12038, 'Gamma_dcdm': 20, 'ln10^{10}A_s': 3.0980, 'n_s': 0.9619,
                '100*theta_s': 1.04119, 'tau_reio': 0.0925,'w0_fld':-1.0, 'wa_fld':0.0, 'Omega_Lambda': 0.0,
                'non linear': 'halofit'}
MyClass = Class()
MyClass.set(params)
MyClass.compute()
TT = MyClass.raw_cl()
factor = TT['ell'][2:]*(TT['ell'][2:] + 1)*(2*np.pi)**-1
plt.loglog(TT['ell'][2:], factor*TT['tt'][2:], '--')
plt.show()

Running this piece of code gives the error in the second instance of CLASS that, Error in Class: Class did not read input parameter(s): Gamma_dcdm

However, one instance of CLASS does not give any error and the TT cls are plotted.

Any help in understanding why this or the related issue happens would be very useful.

ShazAlvi avatar May 02 '21 15:05 ShazAlvi

Hi @ShazAlvi , Can you check these two instances by numbering them differently. For example: after importing run first instance as param1 =..... Myclass1 = ... .. TT1= factor1= ... plt.loglog...

Then the 2nd instance as param2= .. ..

And then see if plt.show() gives anything. I hope this helps.

Best, Shan

shan1525 avatar May 03 '21 13:05 shan1525

Hi. Thanks for your comment. I tried this and the problem remains.

ShazAlvi avatar May 03 '21 14:05 ShazAlvi

@shan1525 Would it be possible for you to copy and check that this is indeed happening and it is not an environmental issue?

ShazAlvi avatar May 04 '21 06:05 ShazAlvi

Hi @ShazAlvi , I am sorry it didn't help. You can also try without python wrapper, make these Instances in class with the . ini file by setting up your params. Generate both the data and plot with the cpu.py programme meanwhile I will check with the python wrapper and see whether I can produce the output that you have gotten.

Best, Shan.

shan1525 avatar May 04 '21 16:05 shan1525

Quick update on this front: The error happens (in some way) because of the plot statement. If the plot statement is executed before the Class() call, then Class throws an error. Consequently, this code works:

from classy import Class
import numpy as np
import matplotlib.pyplot as plt

params = {'output': 'tCl', 'omega_b': 0.022032,'omega_ini_dcdm': 0.12038, 'Gamma_dcdm': 0.0, 'ln10^{10}A_s': 3.0980, 'n_s': 0.9619,
                '100*theta_s': 1.04119, 'tau_reio': 0.0925,'w0_fld':-1.0, 'wa_fld':0.0, 'Omega_Lambda': 0.0,
                'non linear': 'halofit'}
MyClass = Class()
MyClass.set(params)
MyClass.compute()
TT = MyClass.raw_cl()
factor = TT['ell'][2:]*(TT['ell'][2:] + 1)*(2*np.pi)**-1
MyClass.empty()
params = {'output': 'tCl', 'omega_b': 0.022032,'omega_ini_dcdm': 0.12038, 'Gamma_dcdm': 20.0, 'ln10^{10}A_s': 3.0980, 'n_s': 0.9619,
                '100*theta_s': 1.04119, 'tau_reio': 0.0925,'w0_fld':-1.0, 'wa_fld':0.0, 'Omega_Lambda': 0.0,
                'non linear': 'halofit'}
MyClass = Class()
MyClass.set(params)
MyClass.compute()
TT_2 = MyClass.raw_cl()
factor = TT['ell'][2:]*(TT['ell'][2:] + 1)*(2*np.pi)**-1
plt.loglog(TT['ell'][2:], factor*TT['tt'][2:])
plt.loglog(TT_2['ell'][2:], factor*TT_2['tt'][2:], '--')
plt.show()

ShazAlvi avatar May 05 '21 12:05 ShazAlvi

Hi

Thanks for investigating this, I am following this issue. However, I am not able to reproduce the issue on my machine, I tried both in a Jupyter notebook and running the script through Python. It would be great if others could try executing the code block in the first post to narrow down the cause.

Cheers, Thomas

ThomasTram avatar May 05 '21 12:05 ThomasTram

So, you can execute the code that I posted at the start of this thread without any issue, regardless of where you put the plot statement? That is strange indeed.

ShazAlvi avatar May 05 '21 13:05 ShazAlvi

~Just a quick thought:~

~If the segfault is really triggered by the line~ ~~ plt.loglog(TT_2['ell'][2:], factor*TT_2['tt'][2:], '--') ~~ ~I suppose that the TT_2 dictionary is faulty.~

~Have you checked whether the arrays in this dictionary have the correct length? Maybe something went wrong and the arrays are empty such that the index [2:] goes out of bounds (In C this is one of the classic reasons for a seggault). Normally numpy should catch this. At least in pure python code this is the case, but I am not sure sure whether this bound check is disabled as we use cython.~

EDIT: Nevermind what I wrote here. I have no idea why I had the impression that there is a segfault.

pstoecker avatar May 05 '21 13:05 pstoecker

Hi @ShazAlvi , I checked your query about the python wrapper. As @ThomasTram has also checked , it is working well in my system also. I don't know why it is giving an error in your system. I am attaching the output that it generated.

test_shaz Best, shan

shan1525 avatar May 06 '21 11:05 shan1525

Hi all,

I'm not sure it helps much, but I can attest that on my machine (Dell workstation) and distro (Linux Mint 20.3), I have the same two errors as @ShazAlvi: the BBN file error of #414 and the error in the present issue.

s-ilic avatar Jul 05 '22 21:07 s-ilic

Hi all, Thanks @s-ilic I still have not been able to find a solution to this problem. When running multiple instances my standard method is to collect all the observables in a dictionary and then plot them after I have run all the cases in CLASS.

ShazAlvi avatar Jul 06 '22 08:07 ShazAlvi

Hello @ShazAlvi ! Yeah, I also figured out that it's the best solution for now... This is quite a puzzling situation! But I think the two issues are one and a same, because if I remember correctly, when using the classy wrapper, there is a "behind-the-scenes" step somewhere akin to writing your input parameters into a temporary file, which is then read by the "main" CLASS code (@ThomasTram can confirm?), therefore both the "BBN file issue" and the "did not read param issue" are most likely due to a problem with "reading files"...

s-ilic avatar Jul 06 '22 08:07 s-ilic

Hey guys

This is a weird issue. There is no temporary file, but there is a dict which is being updated in the .set statement. Perhaps it has to do with some instance variables becoming/being Class variables unintentionally. However, I cannot immediately see such an error. It may also depend on the version of Cython?

Anyway, it would be interesting if one of you having the problem could see if it exists in my (experimental) C++ version of CLASS at https://github.com/AarhusCosmology/CLASSpp_public. I rewrote the initialisation of the wrapper in a different way, and if it solves the problem perhaps the new initialisation method can be backported..

Cheers, Thomas

ThomasTram avatar Jul 06 '22 09:07 ThomasTram

Thank you for your reply @ThomasTram, I'll give it a try later today! I'm currently working with a different laptop (running Linux Mint 19) which, strangely enough, doesn't have those issues...

s-ilic avatar Jul 06 '22 09:07 s-ilic

Some updates: my code still yields the same BBN file problem with your C++ version of CLASS, @ThomasTram ...

Interestingly enough, @ShazAlvi 's code gives me the "BBN error" with your C++ version of CLASS, whereas it gives me the "did not read param issue" with the standard C version of CLASS... Curioser and Curioser!

s-ilic avatar Jul 06 '22 23:07 s-ilic