4.6 pde.storage package

Module defining classes for storing simulation data.

StorageBase

Base class for storing time series of discretized fields.

FileStorage

Store discretized fields in a hdf5 file.

get_memory_storage

A context manager that can be used to create a MemoryStorage.

MemoryStorage

Store discretized fields in memory.

ModelrunnerStorage

Store discretized fields in a modelrunner storage.

MovieStorage

Store discretized fields in a movie file.

class FileStorage(filename: str | Path, *, info: dict[str, Any] | None = None, write_mode: Literal['append', 'readonly', 'truncate', 'truncate_once'] = 'truncate_once', max_length: int | None = None, compression: bool = True, keep_opened: bool = True, check_mpi: bool = True)[source]

Bases: StorageBase

Store discretized fields in a hdf5 file.

Parameters:
  • filename (str) – The path to the hdf5-file where the data is stored

  • info (dict) – Supplies extra information that is stored in the storage

  • write_mode (str) – Determines how new data is added to already existing data. Possible values are: ‘append’ (data is always appended), ‘truncate’ (data is cleared every time this storage is used for writing), or ‘truncate_once’ (data is cleared for the first writing, but appended subsequently). Alternatively, specifying ‘readonly’ will disable writing completely.

  • max_length (int, optional) – Maximal number of entries that will be stored in the file. This can be used to preallocate data, which can lead to smaller files, but is also less flexible. Giving max_length = None, allows for arbitrarily large data, which might lead to larger files.

  • compression (bool) – Whether to store the data in compressed form. Automatically enabled chunked storage.

  • keep_opened (bool) – Flag indicating whether the file should be kept opened after each writing. If False, the file will be closed after writing a dataset. This keeps the file in a consistent state, but also requires more work before data can be written.

  • check_mpi (bool) – If True, files will only be opened in the main node for an parallel simulation using MPI. This flag has no effect in serial code.

append(field: FieldBase, time: float | None = None) → None

Add field to the storage.

Parameters:
  • field (FieldBase) – The field that is added to the storage

  • time (float, optional) – The time point

apply(func: Callable, out: StorageBase | None = None, *, progress: bool = False) → StorageBase

Applies function to each field in a storage.

Parameters:
  • func (callable) – The function to apply to each stored field. The function must either take as a single argument the field or as two arguments the field and the associated time point. In both cases, it should return a field.

  • out (StorageBase) – Storage to which the output is written. If omitted, a new MemoryStorage is used and returned

  • progress (bool) – Flag indicating whether the progress is shown during the calculation

Returns:

The new storage that contains the data after the function func has been applied

Return type:

StorageBase

clear(clear_data_shape: bool = False)[source]

Truncate the storage by removing all stored data.

Parameters:

clear_data_shape (bool) – Flag determining whether the data shape is also deleted.

close() → None[source]

Close the currently opened file.

copy(out: StorageBase | None = None, *, progress: bool = False) → StorageBase

Copies all fields in a storage to a new one.

Parameters:
  • out (StorageBase) – Storage to which the output is written. If omitted, a new MemoryStorage is used and returned

  • progress (bool) – Flag indicating whether the progress is shown during the calculation

Returns:

The new storage that contains the copied data

Return type:

StorageBase

property data

The actual data for all time.

Type:

ndarray

property data_shape: tuple[int, ...]

The current data shape.

Raises:

RuntimeError – if data_shape was not set

property dtype: DTypeLike

The current data type.

Raises:

RuntimeError – if data_type was not set

end_writing() → None[source]

Finalize the storage after writing.

This makes sure the data is actually written to a file when self.keep_opened == False

extract_field(field_id: int | str, label: str | None = None) → MemoryStorage

Extract the time course of a single field from a collection.

This method makes a copy of the underlying data.

Parameters:
  • field_id (int or str) – The index into the field collection. This determines which field of the collection is returned. Instead of a numerical index, the field label can also be supplied. If there are multiple fields with the same label, only the first field is returned.

  • label (str) – The label of the returned field. If omitted, the stored label is used.

Returns:

a storage instance that contains the data for the single field

Return type:

MemoryStorage

extract_time_range(t_range: float | tuple[float, float] | None = None) → MemoryStorage

Extract a particular time interval.

Note

This might return a view into the original data, so modifying the returned data can also change the underlying original data.

Parameters:

t_range (float or tuple) – Determines the range of time points included in the result. If only a single number is given, all data up to this time point are included.

Returns:

a storage instance that contains the extracted data.

Return type:

MemoryStorage

property grid: GridBase | None

the grid associated with this storage

This returns None if grid was not stored in self.info.

Type:

GridBase

property has_collection: bool

whether the storage is storing a collection

Type:

bool

items() → Iterator[tuple[float, FieldBase]]

Iterate over all times and stored fields, returning pairs.

property shape: tuple[int, ...] | None

The shape of the stored data.

start_writing(field: FieldBase, info: InfoDict | None = None) → None[source]

Initialize the storage for writing data.

Parameters:
  • field (FieldBase) – An example of the data that will be written to extract the grid and the data_shape

  • info (dict) – Supplies extra information that is stored in the storage

property times

The times at which data is available.

Type:

ndarray

tracker(interrupts: InterruptData = 1, *, transformation: Callable[[FieldBase, float], FieldBase] | None = None) → StorageTracker

Create object that can be used as a tracker to fill this storage.

Parameters:
  • interrupts – Determines when the tracker interrupts the simulation. A single numbers determines an interval (measured in the simulation time unit) of regular interruption. A string is interpreted as a duration in real time assuming the format ‘hh:mm:ss’. A list of values is taken as explicit simulation time points. More fine- grained control is possible by passing an instance of classes defined in interrupts.

  • transformation (callable, optional) – A function that transforms the current state into a new field or field collection, which is then stored. This allows to store derived quantities of the field during calculations. The argument needs to be a callable function taking 1 or 2 arguments. The first argument always is the current field, while the optional second argument is the associated time.

Returns:

The tracker that fills the current storage

Return type:

StorageTracker

Example

The transformation argument allows storing additional fields:

def add_to_state(state):
    transformed_field = state.smooth(1)
    return field.append(transformed_field)


storage = pde.MemoryStorage()
tracker = storage.tracker(1, transformation=add_to_state)
eq.solve(..., tracker=tracker)

In this example, storage will contain a trajectory of the fields of the simulation as well as the smoothed fields. Other transformations are possible by defining appropriate add_to_state()

view_field(field_id: int | str) → StorageView

Returns a view into this storage focusing on a particular field.

Note

Modifying data returned by the view will modify the underlying storage

Parameters:

field_id (int or str) – The index into the field collection. This determines which field of the collection is returned. Instead of a numerical index, the field label can also be supplied. If there are multiple fields with the same label, only the first field is returned.

Returns:

A view into the storage only returning a single field

Return type:

StorageView

class MemoryStorage(times: Sequence[float] | None = None, data: list[NumericArray] | None = None, *, info: InfoDict | None = None, field_obj: FieldBase | None = None, write_mode: WriteModeType = 'truncate_once')[source]

Bases: StorageBase

Store discretized fields in memory.

Parameters:
  • times (ndarray) – Sequence of times for which data is known

  • data (list of ndarray) – The field data at the given times

  • field_obj (FieldBase) – An instance of the field class store data for a single time point.

  • info (dict) – Supplies extra information that is stored in the storage

  • write_mode (str) – Determines how new data is added to already existing data. Possible values are: ‘append’ (data is always appended), ‘truncate’ (data is cleared every time this storage is used for writing), or ‘truncate_once’ (data is cleared for the first writing, but appended subsequently). Alternatively, specifying ‘readonly’ will disable writing completely.

append(field: FieldBase, time: float | None = None) → None

Add field to the storage.

Parameters:
  • field (FieldBase) – The field that is added to the storage

  • time (float, optional) – The time point

apply(func: Callable, out: StorageBase | None = None, *, progress: bool = False) → StorageBase

Applies function to each field in a storage.

Parameters:
  • func (callable) – The function to apply to each stored field. The function must either take as a single argument the field or as two arguments the field and the associated time point. In both cases, it should return a field.

  • out (StorageBase) – Storage to which the output is written. If omitted, a new MemoryStorage is used and returned

  • progress (bool) – Flag indicating whether the progress is shown during the calculation

Returns:

The new storage that contains the data after the function func has been applied

Return type:

StorageBase

clear(clear_data_shape: bool = False) → None[source]

Truncate the storage by removing all stored data.

Parameters:

clear_data_shape (bool) – Flag determining whether the data shape is also deleted.

copy(out: StorageBase | None = None, *, progress: bool = False) → StorageBase

Copies all fields in a storage to a new one.

Parameters:
  • out (StorageBase) – Storage to which the output is written. If omitted, a new MemoryStorage is used and returned

  • progress (bool) – Flag indicating whether the progress is shown during the calculation

Returns:

The new storage that contains the copied data

Return type:

StorageBase

property data_shape: tuple[int, ...]

The current data shape.

Raises:

RuntimeError – if data_shape was not set

property dtype: DTypeLike

The current data type.

Raises:

RuntimeError – if data_type was not set

end_writing() → None

Finalize the storage after writing.

extract_field(field_id: int | str, label: str | None = None) → MemoryStorage

Extract the time course of a single field from a collection.

This method makes a copy of the underlying data.

Parameters:
  • field_id (int or str) – The index into the field collection. This determines which field of the collection is returned. Instead of a numerical index, the field label can also be supplied. If there are multiple fields with the same label, only the first field is returned.

  • label (str) – The label of the returned field. If omitted, the stored label is used.

Returns:

a storage instance that contains the data for the single field

Return type:

MemoryStorage

extract_time_range(t_range: float | tuple[float, float] | None = None) → MemoryStorage

Extract a particular time interval.

Note

This might return a view into the original data, so modifying the returned data can also change the underlying original data.

Parameters:

t_range (float or tuple) – Determines the range of time points included in the result. If only a single number is given, all data up to this time point are included.

Returns:

a storage instance that contains the extracted data.

Return type:

MemoryStorage

classmethod from_collection(storages: Sequence[StorageBase], label: str | None = None, *, rtol: float = 1e-05, atol: float = 1e-08) → MemoryStorage[source]

Combine multiple memory storages into one.

This method can be used to combine multiple time series of different fields into a single representation. This requires that all time series contain data at the same time points.

Parameters:
  • storages (list) – A collection of instances of StorageBase whose data will be concatenated into a single MemoryStorage

  • label (str, optional) – The label of the instances of FieldCollection that represent the concatenated data

  • rtol (float) – Relative tolerance used when checking times for merging

  • atol (float) – Absolute tolerance used when checking times for merging

Returns:

Storage containing all the data.

Return type:

MemoryStorage

classmethod from_fields(times: Sequence[float] | None = None, fields: Sequence[FieldBase] | None = None, info: InfoDict | None = None, write_mode: WriteModeType = 'truncate_once') → MemoryStorage[source]

Create MemoryStorage from a list of fields.

Parameters:
  • times (ndarray) – Sequence of times for which data is known

  • fields (list of FieldBase) – The fields at all given time points

  • info (dict) – Supplies extra information that is stored in the storage

  • write_mode (str) – Determines how new data is added to already existing data. Possible values are: ‘append’ (data is always appended), ‘truncate’ (data is cleared every time this storage is used for writing), or ‘truncate_once’ (data is cleared for the first writing, but appended subsequently). Alternatively, specifying ‘readonly’ will disable writing completely.

property grid: GridBase | None

the grid associated with this storage

This returns None if grid was not stored in self.info.

Type:

GridBase

property has_collection: bool

whether the storage is storing a collection

Type:

bool

items() → Iterator[tuple[float, FieldBase]]

Iterate over all times and stored fields, returning pairs.

property shape: tuple[int, ...] | None

The shape of the stored data.

start_writing(field: FieldBase, info: InfoDict | None = None) → None[source]

Initialize the storage for writing data.

Parameters:
  • field (FieldBase) – An instance of the field class store data for a single time point.

  • info (dict) – Supplies extra information that is stored in the storage

tracker(interrupts: InterruptData = 1, *, transformation: Callable[[FieldBase, float], FieldBase] | None = None) → StorageTracker

Create object that can be used as a tracker to fill this storage.

Parameters:
  • interrupts – Determines when the tracker interrupts the simulation. A single numbers determines an interval (measured in the simulation time unit) of regular interruption. A string is interpreted as a duration in real time assuming the format ‘hh:mm:ss’. A list of values is taken as explicit simulation time points. More fine- grained control is possible by passing an instance of classes defined in interrupts.

  • transformation (callable, optional) – A function that transforms the current state into a new field or field collection, which is then stored. This allows to store derived quantities of the field during calculations. The argument needs to be a callable function taking 1 or 2 arguments. The first argument always is the current field, while the optional second argument is the associated time.

Returns:

The tracker that fills the current storage

Return type:

StorageTracker

Example

The transformation argument allows storing additional fields:

def add_to_state(state):
    transformed_field = state.smooth(1)
    return field.append(transformed_field)


storage = pde.MemoryStorage()
tracker = storage.tracker(1, transformation=add_to_state)
eq.solve(..., tracker=tracker)

In this example, storage will contain a trajectory of the fields of the simulation as well as the smoothed fields. Other transformations are possible by defining appropriate add_to_state()

view_field(field_id: int | str) → StorageView

Returns a view into this storage focusing on a particular field.

Note

Modifying data returned by the view will modify the underlying storage

Parameters:

field_id (int or str) – The index into the field collection. This determines which field of the collection is returned. Instead of a numerical index, the field label can also be supplied. If there are multiple fields with the same label, only the first field is returned.

Returns:

A view into the storage only returning a single field

Return type:

StorageView

class ModelrunnerStorage(storage: mr.storage.StorageGroup, *, loc: mr.storage.Location = 'trajectory', info: InfoDict | None = None, write_mode: WriteModeType = 'truncate_once')[source]

Bases: StorageBase

Store discretized fields in a modelrunner storage.

This storage class acts as a wrapper for the trajectory module, which allows handling time-dependent data in modelrunner storages. In principle, all backends are supported, but it is advisable to use binary formats like HDFStorage or ZarrStorage to write large amounts of data.

from modelrunner import Result

r = Result.from_file("data.hdf5")
r.result.plot()  # plots the final state
r.storage["trajectory"]  # allows accessing the stored trajectory
Parameters:
  • storage (StorageGroup) – Modelrunner storage used for storing the trajectory

  • loc (str or list of str) – The location in the storage where the trajectory data is written.

  • info (dict) – Supplies extra information that is stored in the storage

  • write_mode (str) – Determines how new data is added to already existing data. Possible values are: ‘append’ (data is always appended), ‘truncate’ (data is cleared every time this storage is used for writing), or ‘truncate_once’ (data is cleared for the first writing, but appended subsequently). Alternatively, specifying ‘readonly’ will disable writing completely.

append(field: FieldBase, time: float | None = None) → None

Add field to the storage.

Parameters:
  • field (FieldBase) – The field that is added to the storage

  • time (float, optional) – The time point

apply(func: Callable, out: StorageBase | None = None, *, progress: bool = False) → StorageBase

Applies function to each field in a storage.

Parameters:
  • func (callable) – The function to apply to each stored field. The function must either take as a single argument the field or as two arguments the field and the associated time point. In both cases, it should return a field.

  • out (StorageBase) – Storage to which the output is written. If omitted, a new MemoryStorage is used and returned

  • progress (bool) – Flag indicating whether the progress is shown during the calculation

Returns:

The new storage that contains the data after the function func has been applied

Return type:

StorageBase

clear(clear_data_shape: bool = False)[source]

Truncate the storage by removing all stored data.

Parameters:

clear_data_shape (bool) – Flag determining whether the data shape is also deleted.

close() → None[source]

Close the currently opened trajectory writer.

copy(out: StorageBase | None = None, *, progress: bool = False) → StorageBase

Copies all fields in a storage to a new one.

Parameters:
  • out (StorageBase) – Storage to which the output is written. If omitted, a new MemoryStorage is used and returned

  • progress (bool) – Flag indicating whether the progress is shown during the calculation

Returns:

The new storage that contains the copied data

Return type:

StorageBase

property data

The actual data for all time.

Type:

ndarray

property data_shape: tuple[int, ...]

The current data shape.

Raises:

RuntimeError – if data_shape was not set

property dtype: DTypeLike

The current data type.

Raises:

RuntimeError – if data_type was not set

end_writing() → None[source]

Finalize the storage after writing.

This makes sure the data is actually written to a file when self.keep_opened == False

extract_field(field_id: int | str, label: str | None = None) → MemoryStorage

Extract the time course of a single field from a collection.

This method makes a copy of the underlying data.

Parameters:
  • field_id (int or str) – The index into the field collection. This determines which field of the collection is returned. Instead of a numerical index, the field label can also be supplied. If there are multiple fields with the same label, only the first field is returned.

  • label (str) – The label of the returned field. If omitted, the stored label is used.

Returns:

a storage instance that contains the data for the single field

Return type:

MemoryStorage

extract_time_range(t_range: float | tuple[float, float] | None = None) → MemoryStorage

Extract a particular time interval.

Note

This might return a view into the original data, so modifying the returned data can also change the underlying original data.

Parameters:

t_range (float or tuple) – Determines the range of time points included in the result. If only a single number is given, all data up to this time point are included.

Returns:

a storage instance that contains the extracted data.

Return type:

MemoryStorage

property grid: GridBase | None

the grid associated with this storage

This returns None if grid was not stored in self.info.

Type:

GridBase

property has_collection: bool

whether the storage is storing a collection

Type:

bool

items() → Iterator[tuple[float, FieldBase]]

Iterate over all times and stored fields, returning pairs.

property shape: tuple[int, ...] | None

The shape of the stored data.

start_writing(field: FieldBase, info: InfoDict | None = None) → None[source]

Initialize the storage for writing data.

Parameters:
  • field (FieldBase) – An example of the data that will be written to extract the grid and the data_shape

  • info (dict) – Supplies extra information that is stored in the storage

property times

The times at which data is available.

Type:

ndarray

tracker(interrupts: InterruptData = 1, *, transformation: Callable[[FieldBase, float], FieldBase] | None = None) → StorageTracker

Create object that can be used as a tracker to fill this storage.

Parameters:
  • interrupts – Determines when the tracker interrupts the simulation. A single numbers determines an interval (measured in the simulation time unit) of regular interruption. A string is interpreted as a duration in real time assuming the format ‘hh:mm:ss’. A list of values is taken as explicit simulation time points. More fine- grained control is possible by passing an instance of classes defined in interrupts.

  • transformation (callable, optional) – A function that transforms the current state into a new field or field collection, which is then stored. This allows to store derived quantities of the field during calculations. The argument needs to be a callable function taking 1 or 2 arguments. The first argument always is the current field, while the optional second argument is the associated time.

Returns:

The tracker that fills the current storage

Return type:

StorageTracker

Example

The transformation argument allows storing additional fields:

def add_to_state(state):
    transformed_field = state.smooth(1)
    return field.append(transformed_field)


storage = pde.MemoryStorage()
tracker = storage.tracker(1, transformation=add_to_state)
eq.solve(..., tracker=tracker)

In this example, storage will contain a trajectory of the fields of the simulation as well as the smoothed fields. Other transformations are possible by defining appropriate add_to_state()

view_field(field_id: int | str) → StorageView

Returns a view into this storage focusing on a particular field.

Note

Modifying data returned by the view will modify the underlying storage

Parameters:

field_id (int or str) – The index into the field collection. This determines which field of the collection is returned. Instead of a numerical index, the field label can also be supplied. If there are multiple fields with the same label, only the first field is returned.

Returns:

A view into the storage only returning a single field

Return type:

StorageView

class MovieStorage(filename: str | Path, *, vmin: float | ArrayLike = 0, vmax: float | ArrayLike = 1, bits_per_channel: int = 16, video_format: str = 'auto', bitrate: int = -1, info: InfoDict | None = None, write_mode: WriteModeType = 'truncate_once', write_times: bool = False, loglevel: str = 'warning')[source]

Bases: StorageBase

Store discretized fields in a movie file.

This storage only works when the ffmpeg program and ffmpeg is installed. The default codec is FFV1, which supports lossless compression for various configurations. Not all video players support this codec, but VLC usually works quite well.

Note that important meta information is stored as a comment in the movie, so this data must not be deleted or altered if the video should be read again.

Warning

This storage potentially compresses data and can thus lead to loss of some information. The data quality depends on many parameters, but most important are the bits per channel of the video format and the range that is encoded (determined by vmin and vmax).

Note also that selecting individual time points might be quite slow since the video needs to be read from the beginning each time. Instead, it is much more efficient to process entire videos (by iterating over them or using items()).

Parameters:
  • filename (str) – The path where the movie is stored. The file extension determines the container format of the movie. The standard codec FFV1 plays well with the “.avi”, “.mkv”, and “.mov” container format.

  • vmin (float or array) – Lowest values that are encoded (per field). Smaller values are clipped to this value.

  • vmax (float or array) – Highest values that are encoded (per field). Larger values are clipped to this value.

  • bits_per_channel (int) – The number of bits used per color channel. Typical values are 8 and 16. The relative accuracy of stored values is 0.01 and 0.0001, respectively.

  • video_format (str) – Identifier for a video format from formats, which determines the number of channels, the bit depth of individual colors, and the codec. The special value auto tries to find a suitable format automatically, taking bits_per_channel into account.

  • bitrate (float) – The bitrate of the movie (in kilobits per second). The default value of -1 let’s the encoder choose an appropriate bit rate.

  • info (dict) – Supplies extra information that is stored in the storage alongside additional information necessary to reconstruct fields and grids.

  • write_mode (str) – Determines how new data is added to already existing data. Possible values are: ‘append’ (data is always appended), ‘truncate’ (data is cleared every time this storage is used for writing), or ‘truncate_once’ (data is cleared for the first writing, but appended subsequently). Alternatively, specifying ‘readonly’ will disable writing completely.

  • write_times (bool) – Flag determining whether timestamps are written to a file. If True, a separate file with name filename + ".times" is created where the times are written as plain text. Without these timestamps, the time information might be inaccurate.

  • loglevel (str) – FFmpeg log level determining the amount of data sent to stdout. The default only emits warnings and errors, but setting this to “info” can be useful to get additional information about the encoding.

append(field: FieldBase, time: float | None = None) → None

Add field to the storage.

Parameters:
  • field (FieldBase) – The field that is added to the storage

  • time (float, optional) – The time point

apply(func: Callable, out: StorageBase | None = None, *, progress: bool = False) → StorageBase

Applies function to each field in a storage.

Parameters:
  • func (callable) – The function to apply to each stored field. The function must either take as a single argument the field or as two arguments the field and the associated time point. In both cases, it should return a field.

  • out (StorageBase) – Storage to which the output is written. If omitted, a new MemoryStorage is used and returned

  • progress (bool) – Flag indicating whether the progress is shown during the calculation

Returns:

The new storage that contains the data after the function func has been applied

Return type:

StorageBase

clear()[source]

Truncate the storage by removing all stored data.

close() → None[source]

Close the currently opened file.

copy(out: StorageBase | None = None, *, progress: bool = False) → StorageBase

Copies all fields in a storage to a new one.

Parameters:
  • out (StorageBase) – Storage to which the output is written. If omitted, a new MemoryStorage is used and returned

  • progress (bool) – Flag indicating whether the progress is shown during the calculation

Returns:

The new storage that contains the copied data

Return type:

StorageBase

property data

The actual data for all times.

Type:

ndarray

property data_shape: tuple[int, ...]

The current data shape.

Raises:

RuntimeError – if data_shape was not set

property dtype: DTypeLike

The current data type.

Raises:

RuntimeError – if data_type was not set

end_writing() → None[source]

Finalize the storage after writing.

extract_field(field_id: int | str, label: str | None = None) → MemoryStorage

Extract the time course of a single field from a collection.

This method makes a copy of the underlying data.

Parameters:
  • field_id (int or str) – The index into the field collection. This determines which field of the collection is returned. Instead of a numerical index, the field label can also be supplied. If there are multiple fields with the same label, only the first field is returned.

  • label (str) – The label of the returned field. If omitted, the stored label is used.

Returns:

a storage instance that contains the data for the single field

Return type:

MemoryStorage

extract_time_range(t_range: float | tuple[float, float] | None = None) → MemoryStorage

Extract a particular time interval.

Note

This might return a view into the original data, so modifying the returned data can also change the underlying original data.

Parameters:

t_range (float or tuple) – Determines the range of time points included in the result. If only a single number is given, all data up to this time point are included.

Returns:

a storage instance that contains the extracted data.

Return type:

MemoryStorage

property grid: GridBase | None

the grid associated with this storage

This returns None if grid was not stored in self.info.

Type:

GridBase

property has_collection: bool

whether the storage is storing a collection

Type:

bool

items() → Iterator[tuple[float, FieldBase]][source]

Iterate over all times and stored fields, returning pairs.

property shape: tuple[int, ...] | None

The shape of the stored data.

start_writing(field: FieldBase, info: InfoDict | None = None) → None[source]

Initialize the storage for writing data.

Parameters:
  • field (FieldBase) – An example of the data that will be written to extract the grid and the data_shape

  • info (dict) – Supplies extra information that is stored in the storage

times

The times at which data is available.

Type:

ndarray

tracker(interrupts: InterruptData = 1, *, transformation: Callable[[FieldBase, float], FieldBase] | None = None) → StorageTracker[source]

Create object that can be used as a tracker to fill this storage.

Parameters:
  • interrupts – Determines when the tracker interrupts the simulation. A single numbers determines an interval (measured in the simulation time unit) of regular interruption. A string is interpreted as a duration in real time assuming the format ‘hh:mm:ss’. A list of values is taken as explicit simulation time points. More fine- grained control is possible by passing an instance of classes defined in interrupts.

  • transformation (callable, optional) – A function that transforms the current state into a new field or field collection, which is then stored. This allows to store derived quantities of the field during calculations. The argument needs to be a callable function taking 1 or 2 arguments. The first argument always is the current field, while the optional second argument is the associated time.

Returns:

The tracker that fills the current storage

Return type:

StorageTracker

Example

The transformation argument allows storing additional fields:

def add_to_state(state):
    transformed_field = state.smooth(1)
    return field.append(transformed_field)


storage = pde.MemoryStorage()
tracker = storage.tracker(1, transformation=add_to_state)
eq.solve(..., tracker=tracker)

In this example, storage will contain a trajectory of the fields of the simulation as well as the smoothed fields. Other transformations are possible by defining appropriate add_to_state()

view_field(field_id: int | str) → StorageView

Returns a view into this storage focusing on a particular field.

Note

Modifying data returned by the view will modify the underlying storage

Parameters:

field_id (int or str) – The index into the field collection. This determines which field of the collection is returned. Instead of a numerical index, the field label can also be supplied. If there are multiple fields with the same label, only the first field is returned.

Returns:

A view into the storage only returning a single field

Return type:

StorageView

get_memory_storage(field: FieldBase, info: InfoDict | None = None)[source]

A context manager that can be used to create a MemoryStorage.

Example

This can be used to quickly store data:

with get_memory_storage(field_class) as storage:
    storage.append(numpy_array0, 0)
    storage.append(numpy_array1, 1)

# use storage thereafter
Parameters:
  • field (FieldBase) – An instance of the field class store data for a single time point.

  • info (dict) – Supplies extra information that is stored in the storage

Yields:

MemoryStorage