swiftsimio.reader module

This file contains four major objects:

  • SWIFTUnits, which is a unit system that can be queried for units (and converts arrays to relevant unyt arrays when read from the HDF5 file)

  • SWIFTMetadata, which contains all of the metadata from the file

  • __SWIFTParticleDataset, which contains particle information but should never be directly accessed. Use generate_dataset to create one of these. The reasoning here is that properties can only be added to the class afterwards, and not directly in an _instance_ of the class.

  • SWIFTDataset, a container class for all of the above.

class swiftsimio.reader.MassTable(base_mass_table: array, mass_units: unyt_quantity)[source]

Bases: object

Extracts a mass table to local variables based on the particle type names.

class swiftsimio.reader.MappingTable(data: ndarray, named_columns_x: List[str], named_columns_y: List[str], named_columns_x_name: str, named_columns_y_name: str)[source]

Bases: object

A mapping table from one named column instance to the other. Initially designed for the mapping between dust and elements.

class swiftsimio.reader.SWIFTUnits(filename)[source]

Bases: object

Generates a unyt system that can then be used with the SWIFT data.

These give the unit mass, length, time, current, and temperature as unyt unit variables in simulation units. I.e. you can take any value that you get out of the code and multiply it by the appropriate values to get it ‘unyt-ified’ with the correct units.

mass

unit for mass used

Type:

float

length

unit for length used

Type:

float

time

unit for time used

Type:

float

current

unit for current used

Type:

float

temperature

unit for temperature used

Type:

float

get_unit_dictionary()[source]

Store unit data and metadata

Length 1 arrays are used to store the unit data. This dictionary also contains the metadata information that connects the unyt objects to the names that are stored in the SWIFT snapshots.

class swiftsimio.reader.SWIFTMetadata(filename, units: SWIFTUnits)[source]

Bases: object

Loads all metadata (apart from Units, those are handled by SWIFTUnits) into dictionaries.

This also does some extra parsing on some well-used metadata.

header: dict
filename: str
units: SWIFTUnits
get_metadata()[source]

Loads the metadata as specified in metadata.metadata_fields.

get_named_column_metadata()[source]

Loads the custom named column metadata (if it exists) from SubgridScheme/NamedColumns.

get_mapping_metadata()[source]

Gets the mappings based on the named columns (must have already been read), from the form:

SubgridScheme/{X}To{Y}Mapping.

Includes a hack of Dust -> Grains that will be deprecated.

postprocess_header()[source]

Some minor postprocessing on the header to local variables.

load_particle_types()[source]

Loads the particle types and metadata into objects:

metadata.<type>_properties

This contains six arrays,

metadata.<type>_properties.field_names metadata.<type>_properties.field_paths metadata.<type>_properties.field_units metadata.<type>_properties.field_cosmologies metadata.<type>_properties.field_descriptions metadata.<type>_properties.field_compressions

As well as some more information about the particle type.

extract_cosmology()[source]

Creates an astropy.cosmology object from the internal cosmology system.

This will be saved as self.cosmology.

property present_particle_types

The particle types that are present in the file.

property present_particle_names

The particle _names_ that are present in the simulation.

property code_info

Gets a nicely printed set of code information with:

Name (Git Branch) Git Revision Git Date

property compiler_info

Gets information about the compiler and formats it as:

Compiler Name (Compiler Version) MPI library

property library_info

Gets information about the libraries used and formats it as:

FFTW vFFTW library version GSL vGSL library version HDF5 vHDF5 library version

property hydro_info

Gets information about the hydro scheme and formats it as:

Scheme Kernel function in DimensionD $eta$ = Kernel eta (Kernel target N_ngb $N_{ngb}$) $C_{rm CFL}$ = CFL parameter

property viscosity_info

Gets information about the viscosity scheme and formats it as:

Viscosity Model $alpha_{V, 0}$ = Alpha viscosity, $ell_V$ = Viscosity decay length [internal units], $beta_V$ = Beta viscosity Alpha viscosity (min) < $alpha_V$ < Alpha viscosity (max)

property diffusion_info

Gets information about the diffusion scheme and formats it as:

$lpha_{D, 0}$ = Diffusion alpha, $eta_D$ = Diffusion beta Diffusion alpha (min) < $lpha_D$ < Diffusion alpha (max)

class swiftsimio.reader.SWIFTParticleTypeMetadata(particle_type: int, particle_name: str, metadata: SWIFTMetadata, scale_factor: float)[source]

Bases: object

Object that contains the metadata for one particle type.

This, for instance, could be part type 0, or ‘gas’. This will load in the names of all particle datasets, their units, possible named fields, and their cosmology, and present them for use in the actual i/o routines.

load_metadata(self):

Loads the required metadata.

load_field_names(self):

Loads in the field names.

load_field_units(self):

Loads in the units from each dataset.

load_field_descriptions(self):

Loads in descriptions of the fields for each dataset.

load_field_compressions(self):

Loads in compressions of the fields for each dataset.

load_cosmology(self):

Loads in the field cosmologies.

load_named_columns(self):

Loads the named column data for relevant fields.

load_metadata()[source]

Loads the required metadata.

This includes loading the field names, units and descriptions, as well as the cosmology metadata and any custom named columns

load_field_names()[source]

Loads in only the field names.

load_field_units()[source]

Loads in the units from each dataset.

load_field_descriptions()[source]

Loads in the text descriptions of the fields for each dataset.

load_field_compressions()[source]

Loads in the string describing the compression filters of the fields for each dataset.

load_cosmology()[source]

Loads in the field cosmologies.

load_named_columns()[source]

Loads the named column data for relevant fields.

swiftsimio.reader.generate_getter(filename, name: str, field: str, unit: unyt_quantity, mask: None | ndarray, mask_size: int, cosmo_factor: cosmo_factor, description: str, compression: str, columns: None | IndexExpression = None)[source]

Generates a function that:

  1. If self._`name` exists, return it

  2. If not, open filename

  3. Reads filename[field]

  4. Set self._`name`

  5. Return self._`name`.

Parameters:
  • filename (str) – Filename of the HDF5 file that everything will be read from. Used to generate the HDF5 dataset.

  • name (str) – Output name (snake_case) of the field.

  • field (str) – Full path of field, including e.g. particle type. Examples include /PartType0/Velocities.

  • unit (unyt.unyt_quantity) – Output unit of the resultant cosmo_array

  • mask (None or np.ndarray) – Mask to be used with accelerated.read_ranges_from_file, i.e. an array of integers that describe ranges to be read from the file.

  • mask_size (int) – Size of the mask if present.

  • cosmo_factor (cosmo_factor) – Cosmology factor object corresponding to this array.

  • description (str) – Description (read from HDF5 file) of the data.

  • compression (str) – String describing the lossy compression filters that were applied to the data (read from the HDF5 file).

  • columns (np.lib.index_tricks.IndexEpression, optional) – Index expression corresponding to which columns to read from the numpy array. If not provided, we read all columns and return an n-dimensional array.

Returns:

getter – A callable object that gets the value of the array that has been saved to _name. This function takes only self from the :obj:__SWIFTParticleDataset class.

Return type:

callable

Notes

The major use of this function is for its side effect of setting _name as a member of the class on first read. When the attribute is accessed, it will be dynamically read from the file, to keep initial memory usage as minimal as possible.

If the resultant array is modified, it will not be re-read from the file.

swiftsimio.reader.generate_setter(name: str)[source]

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

Parameters:

name (str) – the name of the attribute to set

Returns:

setter – a callable object that sets the attribute specified by name to the value passed to it.

Return type:

callable

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

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

Parameters:

name (str) – the name of the field to be destroyed

Returns:

deleter – callable that destroys name field

Return type:

callable

swiftsimio.reader.generate_dataset(particle_metadata: SWIFTParticleTypeMetadata, mask)[source]

Generates a SWIFTParticleDataset _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 SWIFTParticleDataset.name, where the name is, for example, coordinates.

Parameters:
class swiftsimio.reader.SWIFTDataset(filename, mask=None)[source]

Bases: object

A collection object for:

  • SWIFTUnits,

  • SWIFTMetadata,

  • SWIFTParticleDataset

This object, in essence, completely represents a SWIFT snapshot. You can access the different particles as follows:

  • SWIFTDataset.gas.particle_ids

  • SWIFTDataset.dark_matter.masses

  • SWIFTDataset.gas.smoothing_lengths

These arrays all have units that are determined by the unit system in the file.

The unit system is available as SWIFTDataset.units and the metadata as SWIFTDataset.metadata.

def get_units(self):

Loads the units from the SWIFT snapshot.

def get_metadata(self):

Loads the metadata from the SWIFT snapshot.

def create_particle_datasets(self):

Creates particle datasets for whatever particle types and names are specified in metadata.particle_types.

get_units()[source]

Loads the units from the SWIFT snapshot.

Ordinarily this happens automatically, but you can call this function again if you mess things up.

get_metadata()[source]

Loads the metadata from the SWIFT snapshot.

Ordinarily this happens automatically, but you can call this function again if you mess things up.

create_particle_datasets()[source]

Creates particle datasets for whatever particle types and names are specified in metadata.particle_types.

These can then be accessed using their underscore names, e.g. gas.