py21cmfast.wrapper.inputs.InputStruct

class py21cmfast.wrapper.inputs.InputStruct

A convenient interface to create a C structure with defaults specified.

It is provided for the purpose of creating C structures in Python to be passed to C functions, where sensible defaults are available. Structures which are created within C and passed back do not need to be wrapped.

This provides a fully initialised structure, and will fail if not all fields are specified with defaults.

Note

The actual C structure is gotten by calling an instance. This is auto-generated when called, based on the parameters in the class.

Warning

This class will not deal well with parameters of the struct which are pointers. All parameters should be primitive types, except for strings, which are dealt with specially.

Parameters:

ffi (cffi object) – The ffi object from any cffi-wrapped library.

asdict() dict

Return a dict representation of the instance.

Examples

This dict should be such that doing the following should work, i.e. it can be used exactly to construct a new instance of the same object:

>>> inp = InputStruct(**params)
>>> newinp =InputStruct(**inp.asdict())
>>> inp == newinp
Return type:

dict

clone(**kwargs)

Make a fresh copy of the instance with arbitrary parameters updated.

classmethod from_subdict(dct: dict[str, Any], safe: bool = True) Self

Construct an instance of this class from a dictionary.

Parameters:
  • dct (dict[str, Any])

  • safe (bool)

Return type:

Self

classmethod new(x: dict | Self | None = None, **kwargs)

Create a new instance of the struct.

Parameters:

x (dict | InputStruct | None) – Initial values for the struct. If x is a dictionary, it should map field names to their corresponding values. If x is an instance of this class, its attributes will be used as initial values. If x is None, the struct will be initialised with default values.

Other Parameters:
  • All other parameters should be passed as if directly to the class constructor

  • (i.e. as parameter names).

Parameters:

x (dict | Self | None)

Examples

>>> params = SimulationOptions.new({'HII_DIM': 250})
>>> params.HII_DIM
250
>>> params = SimulationOptions.new(params)
>>> params.HII_DIM
250
>>> params = SimulationOptions.new()
>>> params.HII_DIM
200
>>> params = SimulationOptions.new(HII_DIM=256)
>>> params.HII_DIM
256
property cdict: dict

A python dictionary containing the properties of the wrapped C-struct.

The memory pointed to by this dictionary is not owned by the wrapped C-struct, but is rather just a python dict. However, in contrast to asdict(), this method transforms the properties to what they should be in C (e.g. linear space vs. log-space) before putting them into the dict.

This dict also contains only the properties of the wrapped C-struct, rather than all properties of the InputStruct instance (some attributes of the python instance are there only to guide setting of defaults, and don’t appear in the C-struct at all).

Return type:

dict