4.6.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:
- data: Any
- 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
- extract_field(field_id, label=None)[source]
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=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(interrupts=1, *, transformation=None)[source]
Create object that can be used as a tracker to fill this storage.
- Parameters:
interrupts (InterruptData) – 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:
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()
- view_field(field_id)[source]
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:
- write_mode: WriteModeType
- class StorageTracker(storage, interrupts=1, *, transformation=None)[source]
Bases:
TransformedTrackerBaseTracker 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 writteninterrupts (InterruptData) – 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.
- finalize(info=None)[source]
Finalize the tracker, supplying additional information.
- Parameters:
info (dict) – Extra information from the simulation
- Return type:
None
- class StorageView(storage, *, field)[source]
Bases:
objectRepresents a view into a storage that extracts a particular field.
- Parameters:
storage (
StorageBase) – The storage providing the basic datafield (int or str) – The index into the field collection determining 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.
- items()[source]
Iterate over all times and stored fields, returning pairs.
- Return type:
Iterator[tuple[float, DataFieldBase]]