4.5.1. pde.storage.base module
Base classes for storing data
- class StorageBase(info=None, write_mode='truncate_once')[source]
Bases:
objectbase class for storing time series of discretized fields
These classes store time series of
FieldBase, i.e., they store the values of the fields at particular time points. Iterating of the storage will return the fields in order and individual time points can also be accessed.- Parameters:
info (dict) – Supplies extra information that is stored in the storage
write_mode (str) – Determines how new data is added to already existing one. 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 subsequent data using the same instances are appended). Alternatively, specifying ‘readonly’ will disable writing completely.
- apply(func, out=None, *, progress=False)[source]
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 newMemoryStorageis used and returnedprogress (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:
- clear(clear_data_shape=False)[source]
truncate the storage by removing all stored data.
- Parameters:
clear_data_shape (bool) – Flag determining whether the data shape is also deleted.
- Return type:
None
- copy(out=None, *, progress=False)[source]
copies all fields in a storage to a new one
- Parameters:
out (
StorageBase) – Storage to which the output is written. If omitted, a newMemoryStorageis used and returnedprogress (bool) – Flag indicating whether the progress is shown during the calculation
- Returns:
The new storage that contains the copied data
- Return type:
- property data_shape: Tuple[int, ...]
the current data shape.
- Raises:
RuntimeError – if data_shape was not set
- property dtype: dtype[Any] | None | Type[Any] | _SupportsDType[dtype[Any]] | str | Tuple[Any, int] | Tuple[Any, SupportsIndex | Sequence[SupportsIndex]] | List[Any] | _DTypeDict | Tuple[Any, Any]
the current data type.
- Raises:
RuntimeError – if data_type was not set
- extract_field(field_id, label=None)[source]
extract the time course of a single field from a collection
Note
This might return a view into the original data, so modifying the returned data can also change the underlying original 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=None)[source]
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.
- property grid: GridBase | None
the grid associated with this storage
This returns None if grid was not stored in self.info.
- Type:
- start_writing(field, info=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_shapeinfo (dict) – Supplies extra information that is stored in the storage
- Return type:
None
- tracker(interval=1, *, transformation=None)[source]
create object that can be used as a tracker to fill this storage
- Parameters:
interval (int | float | InterruptsBase) – Determines how often the tracker interrupts the simulation. Simple numbers are interpreted as durations measured in the simulation time variable. Alternatively, a string using the format ‘hh:mm:ss’ can be used to give durations in real time. Finally, instances of the classes defined in
interruptscan be given for more control.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:
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,
storagewill contain a trajectory of the fields of the simulation as well as the smoothed fields. Other transformations are possible by defining appropriateadd_to_state()
- class StorageTracker(storage, interval=1, *, transformation=None)[source]
Bases:
TrackerBaseTracker that stores data in special storage classes
- storage
The underlying storage class through which the data can be accessed
- Type:
- Parameters:
storage (
StorageBase) – Storage instance to which the data is writteninterval (IntervalData) – Determines how often the tracker interrupts the simulation. Simple numbers are interpreted as durations measured in the simulation time variable. Alternatively, a string using the format ‘hh:mm:ss’ can be used to give durations in real time. Finally, instances of the classes defined in
interruptscan be given for more control.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.
- finalize(info=None)[source]
finalize the tracker, supplying additional information
- Parameters:
info (dict) – Extra information from the simulation
- Return type:
None