4.6.3 pde.storage.memory module
Defines a class storing data in memory.
- 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:
StorageBaseStore discretized fields in memory.
- Parameters:
times (
ndarray) – Sequence of times for which data is knowndata (list of
ndarray) – The field data at the given timesfield_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.
- 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 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: 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 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: DTypeLike
The current data type.
- Raises:
RuntimeError – if data_type was not set
- 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:
- 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.
- 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
StorageBasewhose data will be concatenated into a single MemoryStoragelabel (str, optional) – The label of the instances of
FieldCollectionthat represent the concatenated datartol (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:
- 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 knownfields (list of
FieldBase) – The fields at all given time pointsinfo (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:
- items() Iterator[tuple[float, FieldBase]]
Iterate over all times and stored fields, returning pairs.
- 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,
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: 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: