CAMB icon indicating copy to clipboard operation
CAMB copied to clipboard

camb.model.*Params attributes default to zero

Open tilmantroester opened this issue 6 years ago • 3 comments

When creating a new instance of one of the camb.model.*Params classes, the attributes are set to zero by default instead of the sensible defaults in model.f90. For example, in case of camb.model.AccuracyParams:

>>> import camb
>>> acc = camb.model.AccuracyParams()
>>> print(acc)
class: <AccuracyParams>
 AccuracyBoost = 0.0
 lSampleBoost = 0.0
 lAccuracyBoost = 0.0
 AccuratePolarization = False
 AccurateBB = False
 AccurateReionization = False
 TimeStepBoost = 0.0
 BackgroundTimeStepBoost = 0.0
 IntTolBoost = 0.0
 SourcekAccuracyBoost = 0.0
 IntkAccuracyBoost = 0.0
 TransferkBoost = 0.0
 NonFlatIntAccuracyBoost = 0.0
 BessIntBoost = 0.0
 LensingBoost = 0.0
 NonlinSourceBoost = 0.0
 BesselBoost = 0.0
 LimberBoost = 0.0
 SourceLimberBoost = 0.0
 KmaxBoost = 0.0
 neutrino_q_boost = 0.0```

This then leads to obscure errors when computing anything. It would be nice if the python class gets populated with the Fortran defaults or at least that an error is thrown if not all attributes are specified.

tilmantroester avatar Jul 22 '19 14:07 tilmantroester

I agree it is odd. But currently only objects descended from F2003Class can be expected to initialize as the Fortran in python generated instances. It wasn't really intended that people make separate instances of CAMBstructure types as these are basically just standard ctypes memory and only used as elements of containing F2003Class instances. I think the only obvious solution is to convert all CAMBstructure types to F2003Class or add custom init methods for each subclass declared. For me this is not a priority, but happy to consider pull requests if you think it would be useful.

cmbant avatar Jul 22 '19 21:07 cmbant

For now I've just avoided the use of the CAMBstructure derived classes. At some point it would be nice to be able to create instances to pass to camb.model.CAMBparams without having to maintain a list of default values (which might diverge from the CAMB ones over time).

Would replacing CAMBstructure with F2003Class and adding the @fortran_class decorator and a _fortran_class_name_ class attribute be all that needs to be done?

tilmantroester avatar Jul 25 '19 13:07 tilmantroester

You’d have to modify the corresponding fortran class as well adding required defining methods, and make the component allocatable.

cmbant avatar Jul 25 '19 13:07 cmbant