swiftsimio.writer module

Contains functions and objects for creating SWIFT datasets.

Essentially all you want to do is use SWIFTWriterDataset and fill the attributes that are required for each particle type. More information is available in the README.

swiftsimio.writer.get_dimensions(dimension: <module 'unyt.dimensions' from '/home/docs/checkouts/readthedocs.org/user_builds/swiftsimio/envs/latest/lib/python3.8/site-packages/unyt/dimensions.py'>) dict[source]

Returns exponents corresponding to base dimensions for given unyt dimensions object

Parameters:

dimension (unyt.dimensions) – dimension for which we’re identifying the exponents

Returns:

exp_array – array of exponents corresponding to each base dimension

Return type:

np.ndarray

Examples

>>> get_dimensions(unyt.dimensions.velocity)
{
    "(mass)": 0,
    "(length)": 1,
    "(time)": -1,
    "(temperature)": 0,
    "(current)": 0,
}
swiftsimio.writer.generate_getter(name: str)[source]

Generates a function that gets the unyt array for name.

Parameters:

name (str) – name of data field

Returns:

getter – function that returns unyt array for name

Return type:

function

swiftsimio.writer.generate_setter(name: str, dimensions, unit_system: UnitSystem | str)[source]

Generates a function that sets self._name to the value that is passed to it.

Parameters:
  • name (str) – string to set self._name to

  • dimensions (unyt.dimensions) – physical dimension of self._name (for consistency check)

  • unit_system (unyt.UnitSystem or str) – unit system of self._name

Returns:

setter – function to set self._name

Return type:

function

swiftsimio.writer.generate_deleter(name: str)[source]

Generates a function that destroys self._name (sets it back to None).

Parameters:

name (str) – name of object to be destroyed

Returns:

deleter – function to delete self._name

Return type:

function

swiftsimio.writer.generate_dataset(unit_system: ~unyt.unit_systems.UnitSystem | str, particle_type: int, unit_fields_generate_units: ~typing.Callable[[...], dict] = <function generate_units>)[source]

Generates a SWIFTWriterParticleDataset _class_ that corresponds to the particle type given.

We _must_ do the following _outside_ of the class itself, as one can assign properties to a _class_ but not _within_ a class dynamically.

Here we loop through all of the possible properties in the metadata file. We then use the builtin property() function and some generators to create setters and getters for those properties. This will allow them to be accessed from outside by using SWIFTWriterParticleDataset.name, where the name is, for example, coordinates.

Parameters:
  • unit_system (unyt.UnitSystem or str) – unit system of the dataset

  • particle_type (int) – the particle type of the dataset. Numbering convention is the same as SWIFT, with 0 corresponding to gas, etc. as usual.

  • unit_fields_generate_units (callable, optional) – collection of properties in metadata file for which to create setters and getters

Returns:

an empty dataset for the given particle type

Return type:

SWIFTWriterParticleDataset

class swiftsimio.writer.SWIFTWriterDataset(unit_system: ~unyt.unit_systems.UnitSystem | str, box_size: list | ~unyt.array.unyt_quantity, dimension=3, compress=True, extra_header: None | dict = None, unit_fields_generate_units: ~typing.Callable[[...], dict] = <function generate_units>, scale_factor: ~numpy.float32 = 1.0)[source]

Bases: object

The SWIFT writer dataset. This is used to store all particle arrays and do some extra processing before writing a HDF5 file containing:

  • Fully consistent unit system

  • All required arrays for SWIFT to start

  • Required metadata (all automatic, apart from those required by __init__)

create_particle_datasets()[source]

Creates particle dataset for each particle type in the metadata with associated units

write(filename: str)[source]

Writes the information in the dataset to file.

Parameters:

filename (str) – file to write to