dynamodels 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dynamodels-0.1.0/LICENSE +21 -0
- dynamodels-0.1.0/MANIFEST.in +1 -0
- dynamodels-0.1.0/PKG-INFO +87 -0
- dynamodels-0.1.0/README.md +61 -0
- dynamodels-0.1.0/dynamodels/__init__.py +30 -0
- dynamodels-0.1.0/dynamodels/history.py +216 -0
- dynamodels-0.1.0/dynamodels/integrator.py +499 -0
- dynamodels-0.1.0/dynamodels/model.py +1152 -0
- dynamodels-0.1.0/dynamodels/physical/__init__.py +15 -0
- dynamodels-0.1.0/dynamodels/physical/annular.py +186 -0
- dynamodels-0.1.0/dynamodels/physical/kuramoto_sivashinsky.py +659 -0
- dynamodels-0.1.0/dynamodels/physical/lorenz63.py +199 -0
- dynamodels-0.1.0/dynamodels/physical/lorenz96.py +264 -0
- dynamodels-0.1.0/dynamodels/physical/rijke.py +373 -0
- dynamodels-0.1.0/dynamodels/physical/van_der_pol.py +81 -0
- dynamodels-0.1.0/dynamodels/utils.py +234 -0
- dynamodels-0.1.0/dynamodels.egg-info/PKG-INFO +87 -0
- dynamodels-0.1.0/dynamodels.egg-info/SOURCES.txt +24 -0
- dynamodels-0.1.0/dynamodels.egg-info/dependency_links.txt +1 -0
- dynamodels-0.1.0/dynamodels.egg-info/requires.txt +9 -0
- dynamodels-0.1.0/dynamodels.egg-info/top_level.txt +1 -0
- dynamodels-0.1.0/pyproject.toml +56 -0
- dynamodels-0.1.0/setup.cfg +4 -0
- dynamodels-0.1.0/tests/conftest.py +3 -0
- dynamodels-0.1.0/tests/test_models_history.py +85 -0
- dynamodels-0.1.0/tests/test_physical.py +65 -0
dynamodels-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Andrea Nóvoa
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
recursive-include tests *.py
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: dynamodels
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Dynamical-system models with history tracking and pluggable integrators (Lorenz, Van der Pol, Rijke, annular combustor, KS)
|
|
5
|
+
Author-email: Andrea Nóvoa <a.novoa@imperial.ac.uk>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/andreanovoa/dynamodels
|
|
8
|
+
Project-URL: Repository, https://github.com/andreanovoa/dynamodels
|
|
9
|
+
Project-URL: Issues, https://github.com/andreanovoa/dynamodels/issues
|
|
10
|
+
Keywords: dynamical-systems,chaos,thermoacoustics,reduced-order-modelling
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Science/Research
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Requires-Python: >=3.10
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Requires-Dist: matplotlib<4.0,>=3.9
|
|
18
|
+
Requires-Dist: multiprocess>=0.70
|
|
19
|
+
Requires-Dist: numpy<3.0,>=2.0
|
|
20
|
+
Requires-Dist: scipy<2.0,>=1.13
|
|
21
|
+
Requires-Dist: typeguard
|
|
22
|
+
Provides-Extra: dev
|
|
23
|
+
Requires-Dist: pytest>=8.4; extra == "dev"
|
|
24
|
+
Requires-Dist: ruff; extra == "dev"
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# dynamodels
|
|
28
|
+
|
|
29
|
+
Dynamical-system models with pre-allocated history tracking and pluggable time
|
|
30
|
+
integrators — the modelling core split out of
|
|
31
|
+
[romda](https://github.com/andreanovoa/real-time-bias-aware-DA) so it can be
|
|
32
|
+
reused on its own. Torch-free; depends only on numpy, scipy, matplotlib,
|
|
33
|
+
typeguard and multiprocess.
|
|
34
|
+
|
|
35
|
+
Tutorial: [`tutorial_dynamodels.ipynb`](tutorial_dynamodels.ipynb) | Interface
|
|
36
|
+
documentation: [model protocol](https://andreanovoa.github.io/ntsa/protocol/)
|
|
37
|
+
|
|
38
|
+
## Install
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pip install dynamodels # once released; until then:
|
|
42
|
+
pip install "dynamodels @ git+https://github.com/andreanovoa/dynamodels"
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Quickstart
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
from dynamodels.physical import Lorenz63
|
|
49
|
+
|
|
50
|
+
model = Lorenz63(rho=28., dt=0.01)
|
|
51
|
+
psi, t = model.time_integrate(Nt=1000) # (Nt, Nphi, m) states, (Nt,) times
|
|
52
|
+
model.update_history(psi, t)
|
|
53
|
+
y = model.get_observable_hist() # (Nt, Nq, m) observables
|
|
54
|
+
model.visualize_history()
|
|
55
|
+
model.close() # release the integrator's pool
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
A model composes a `HistoryTracker` (state/time buffer behind `hist`, `hist_t`,
|
|
59
|
+
`update_history()`) with an `Integrator` strategy; subclasses supply
|
|
60
|
+
`obs_labels` plus either `time_derivative(t, psi, **params)` (continuous) or
|
|
61
|
+
`time_step(Nt)` (discrete map). `m`-member ensembles are supported natively
|
|
62
|
+
(`init_ensemble`). Included physical models (`dynamodels.physical`): `Lorenz63`,
|
|
63
|
+
`Lorenz96`, `VdP`, `Rijke`, `Annular`, `KS` — the `lorenz63`, `lorenz96` and `rijke`
|
|
64
|
+
modules carry measured dominant-Lyapunov tables (module-level `_LAM1_MEASURED`;
|
|
65
|
+
`_LAM1_MEASURED_NX10` for Lorenz96) from which instances set `t_lyap`.
|
|
66
|
+
|
|
67
|
+
## Ecosystem
|
|
68
|
+
|
|
69
|
+
- [`ntsa`](https://github.com/andreanovoa/ntsa) — nonlinear time-series analysis
|
|
70
|
+
for any dynamodels-style model ([docs](https://andreanovoa.github.io/ntsa/)).
|
|
71
|
+
- [romda](https://github.com/andreanovoa/real-time-bias-aware-DA) — bias-aware
|
|
72
|
+
ensemble data assimilation built on top.
|
|
73
|
+
|
|
74
|
+
## Development
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
pip install -e ".[dev]"
|
|
78
|
+
python -m pytest tests/
|
|
79
|
+
ruff check dynamodels/ tests/
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Releases publish to PyPI via trusted publishing on version tags
|
|
83
|
+
(`.github/workflows/release.yml`).
|
|
84
|
+
|
|
85
|
+
## License
|
|
86
|
+
|
|
87
|
+
MIT
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# dynamodels
|
|
2
|
+
|
|
3
|
+
Dynamical-system models with pre-allocated history tracking and pluggable time
|
|
4
|
+
integrators — the modelling core split out of
|
|
5
|
+
[romda](https://github.com/andreanovoa/real-time-bias-aware-DA) so it can be
|
|
6
|
+
reused on its own. Torch-free; depends only on numpy, scipy, matplotlib,
|
|
7
|
+
typeguard and multiprocess.
|
|
8
|
+
|
|
9
|
+
Tutorial: [`tutorial_dynamodels.ipynb`](tutorial_dynamodels.ipynb) | Interface
|
|
10
|
+
documentation: [model protocol](https://andreanovoa.github.io/ntsa/protocol/)
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pip install dynamodels # once released; until then:
|
|
16
|
+
pip install "dynamodels @ git+https://github.com/andreanovoa/dynamodels"
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Quickstart
|
|
20
|
+
|
|
21
|
+
```python
|
|
22
|
+
from dynamodels.physical import Lorenz63
|
|
23
|
+
|
|
24
|
+
model = Lorenz63(rho=28., dt=0.01)
|
|
25
|
+
psi, t = model.time_integrate(Nt=1000) # (Nt, Nphi, m) states, (Nt,) times
|
|
26
|
+
model.update_history(psi, t)
|
|
27
|
+
y = model.get_observable_hist() # (Nt, Nq, m) observables
|
|
28
|
+
model.visualize_history()
|
|
29
|
+
model.close() # release the integrator's pool
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
A model composes a `HistoryTracker` (state/time buffer behind `hist`, `hist_t`,
|
|
33
|
+
`update_history()`) with an `Integrator` strategy; subclasses supply
|
|
34
|
+
`obs_labels` plus either `time_derivative(t, psi, **params)` (continuous) or
|
|
35
|
+
`time_step(Nt)` (discrete map). `m`-member ensembles are supported natively
|
|
36
|
+
(`init_ensemble`). Included physical models (`dynamodels.physical`): `Lorenz63`,
|
|
37
|
+
`Lorenz96`, `VdP`, `Rijke`, `Annular`, `KS` — the `lorenz63`, `lorenz96` and `rijke`
|
|
38
|
+
modules carry measured dominant-Lyapunov tables (module-level `_LAM1_MEASURED`;
|
|
39
|
+
`_LAM1_MEASURED_NX10` for Lorenz96) from which instances set `t_lyap`.
|
|
40
|
+
|
|
41
|
+
## Ecosystem
|
|
42
|
+
|
|
43
|
+
- [`ntsa`](https://github.com/andreanovoa/ntsa) — nonlinear time-series analysis
|
|
44
|
+
for any dynamodels-style model ([docs](https://andreanovoa.github.io/ntsa/)).
|
|
45
|
+
- [romda](https://github.com/andreanovoa/real-time-bias-aware-DA) — bias-aware
|
|
46
|
+
ensemble data assimilation built on top.
|
|
47
|
+
|
|
48
|
+
## Development
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
pip install -e ".[dev]"
|
|
52
|
+
python -m pytest tests/
|
|
53
|
+
ruff check dynamodels/ tests/
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Releases publish to PyPI via trusted publishing on version tags
|
|
57
|
+
(`.github/workflows/release.yml`).
|
|
58
|
+
|
|
59
|
+
## License
|
|
60
|
+
|
|
61
|
+
MIT
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"""dynamodels — dynamical-system models with history tracking and pluggable integrators.
|
|
2
|
+
|
|
3
|
+
The `Model` base class composes a pre-allocated state/time history
|
|
4
|
+
(`HistoryTracker`) with an `Integrator` strategy (`IVPIntegrator` for
|
|
5
|
+
continuous ODEs via scipy `solve_ivp`, `DiscreteIntegrator` for maps,
|
|
6
|
+
`ConstantIntegrator` for frozen states). A model subclass supplies
|
|
7
|
+
`obs_labels` plus either `time_derivative(t, psi, **params)` or
|
|
8
|
+
`time_step(Nt)`. Ready-made physical models live in `dynamodels.physical`:
|
|
9
|
+
Lorenz63, Lorenz96, Van der Pol, Rijke, Annular, KS.
|
|
10
|
+
|
|
11
|
+
Originally the `romda.models` core (real-time bias-aware data assimilation);
|
|
12
|
+
split out so the modelling layer is reusable on its own.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from . import physical
|
|
16
|
+
from .history import HistoryTracker
|
|
17
|
+
from .integrator import ConstantIntegrator, DiscreteIntegrator, Integrator, IVPIntegrator
|
|
18
|
+
from .model import Model
|
|
19
|
+
|
|
20
|
+
__version__ = "0.1.0"
|
|
21
|
+
|
|
22
|
+
__all__ = [
|
|
23
|
+
"Model",
|
|
24
|
+
"HistoryTracker",
|
|
25
|
+
"Integrator",
|
|
26
|
+
"IVPIntegrator",
|
|
27
|
+
"DiscreteIntegrator",
|
|
28
|
+
"ConstantIntegrator",
|
|
29
|
+
"physical",
|
|
30
|
+
]
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
|
|
5
|
+
__all__ = ['HistoryTracker']
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class HistoryTracker:
|
|
9
|
+
"""Pre-allocated, growable storage for a state (and time) history.
|
|
10
|
+
|
|
11
|
+
Backs `dynamodels.model.Model` and bias estimators in downstream packages:
|
|
12
|
+
states are written into two pre-allocated arrays, `_hist` (shape
|
|
13
|
+
``(capacity, N, m)``) and `_hist_t` (shape ``(capacity,)``), so that
|
|
14
|
+
appending new states (the common case, one per forecast step) does not
|
|
15
|
+
reallocate on every call. `current_ti` tracks the index of the next empty
|
|
16
|
+
slot; `hist` / `hist_t` expose only the ``[:current_ti]`` valid prefix.
|
|
17
|
+
When the buffer fills up, `_increase_hist_size` grows it in bulk.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
# ________________________ History accessors ________________________ #
|
|
21
|
+
|
|
22
|
+
@property
|
|
23
|
+
def hist(self):
|
|
24
|
+
"""ndarray: The valid (non-empty) portion of the state buffer,
|
|
25
|
+
`_hist[:current_ti]`, shape ``(current_ti, N, m)``."""
|
|
26
|
+
return self._hist[:self.current_ti]
|
|
27
|
+
|
|
28
|
+
@property
|
|
29
|
+
def hist_t(self):
|
|
30
|
+
"""ndarray: The valid portion of the time buffer,
|
|
31
|
+
`_hist_t[:current_ti]`, shape ``(current_ti,)``."""
|
|
32
|
+
return self._hist_t[:self.current_ti]
|
|
33
|
+
|
|
34
|
+
@property
|
|
35
|
+
def capacity(self):
|
|
36
|
+
"""int: Total number of pre-allocated slots, ``_hist_t.shape[0]``
|
|
37
|
+
(>= `current_ti`)."""
|
|
38
|
+
return self._hist_t.shape[0]
|
|
39
|
+
|
|
40
|
+
@property
|
|
41
|
+
def current_state(self):
|
|
42
|
+
"""ndarray: Most recent state, ``hist[current_ti - 1]``."""
|
|
43
|
+
return self.hist[self.current_ti - 1]
|
|
44
|
+
|
|
45
|
+
@property
|
|
46
|
+
def current_time(self):
|
|
47
|
+
"""float: Time stamp of `current_state`."""
|
|
48
|
+
return self.hist_t[self.current_ti - 1]
|
|
49
|
+
|
|
50
|
+
@property
|
|
51
|
+
def current_ti(self):
|
|
52
|
+
"""int: Index of the next empty slot in the buffer (i.e. the number
|
|
53
|
+
of valid entries currently stored)."""
|
|
54
|
+
return self._ti
|
|
55
|
+
|
|
56
|
+
@current_ti.setter
|
|
57
|
+
def current_ti(self, value: int):
|
|
58
|
+
self._ti = value
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def __init__(self, initial_capacity=1000):
|
|
62
|
+
"""Initialise an empty tracker (the storage arrays themselves are
|
|
63
|
+
allocated lazily, by the first ``update_history(..., reset=True)``
|
|
64
|
+
call).
|
|
65
|
+
|
|
66
|
+
Parameters
|
|
67
|
+
----------
|
|
68
|
+
initial_capacity : int
|
|
69
|
+
Initial capacity of the history arrays.
|
|
70
|
+
"""
|
|
71
|
+
|
|
72
|
+
self._initial_capacity = initial_capacity
|
|
73
|
+
self.current_ti = 0 # Current time index in history
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _reset_history(self, new_history, t_reset=None):
|
|
77
|
+
"""Resets the history arrays to the provided new_history and t_reset.
|
|
78
|
+
|
|
79
|
+
Parameters
|
|
80
|
+
----------
|
|
81
|
+
new_history : ndarray, shape (Nt, N, m)
|
|
82
|
+
New state history to set.
|
|
83
|
+
t_reset : ndarray, shape (Nt,), optional
|
|
84
|
+
New time history to set. Defaults to zeros, i.e. a reset restarts
|
|
85
|
+
the clock, matching `Model.__init__`.
|
|
86
|
+
"""
|
|
87
|
+
|
|
88
|
+
if t_reset is None:
|
|
89
|
+
t_reset = np.zeros(new_history.shape[0])
|
|
90
|
+
|
|
91
|
+
Nt = max(new_history.shape[0], self._initial_capacity)
|
|
92
|
+
|
|
93
|
+
# Initialize the history arrays
|
|
94
|
+
# Preserve dtype: complex-state models (e.g. KS Fourier modes) must not
|
|
95
|
+
# be truncated to their real part.
|
|
96
|
+
self._hist = np.empty((Nt, new_history.shape[1], new_history.shape[2]),
|
|
97
|
+
dtype=new_history.dtype)
|
|
98
|
+
self._hist_t = np.empty((Nt,))
|
|
99
|
+
# Store the reset history
|
|
100
|
+
|
|
101
|
+
self._hist[:new_history.shape[0]] = new_history
|
|
102
|
+
self._hist_t[:t_reset.shape[0]] = t_reset
|
|
103
|
+
self.current_ti = new_history.shape[0]
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _reset_last_states(self, new_state, t=None):
|
|
107
|
+
"""Resets only the last state in the history arrays to the provided new_state and t."""
|
|
108
|
+
|
|
109
|
+
# A single state may arrive as (N, m) -- e.g. the analysis ensemble from
|
|
110
|
+
# Estimator.analysis_step. Promote it, otherwise Nt below picks up the state
|
|
111
|
+
# dimension and clobbers that many timesteps.
|
|
112
|
+
if new_state.ndim == 2:
|
|
113
|
+
new_state = new_state[np.newaxis, ...]
|
|
114
|
+
if t is not None:
|
|
115
|
+
t = np.atleast_1d(t)
|
|
116
|
+
|
|
117
|
+
Nt = new_state.shape[0]
|
|
118
|
+
|
|
119
|
+
# current_ti points at the next empty slot, so the last Nt stored states
|
|
120
|
+
# occupy [current_ti - Nt, current_ti) -- the same window that the `hist`
|
|
121
|
+
# property returns as hist[-Nt:]. Writing one slot earlier leaves the
|
|
122
|
+
# newest state stale and shifts every other state back by one step.
|
|
123
|
+
ti_0 = self.current_ti - Nt
|
|
124
|
+
ti_now = self.current_ti
|
|
125
|
+
|
|
126
|
+
assert ti_0 >= 0, f"Cannot reset last {Nt} states because the new time goes to zero. Consider resetting the full history instead."
|
|
127
|
+
|
|
128
|
+
self._hist[ti_0:ti_now] = new_state[:]
|
|
129
|
+
|
|
130
|
+
if t is not None:
|
|
131
|
+
assert t.shape[0] == Nt, f"Length of t ({t.shape}) must match number of time steps in new_state ({new_state.shape})."
|
|
132
|
+
self._hist_t[ti_0:ti_now] = t[:]
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def update_history(self,
|
|
138
|
+
state: np.ndarray, t: np.ndarray | None,
|
|
139
|
+
reset=False, modify_saved_states=False):
|
|
140
|
+
"""Write `state` into the history buffer.
|
|
141
|
+
|
|
142
|
+
Three mutually exclusive modes, selected by `reset` /
|
|
143
|
+
`modify_saved_states`:
|
|
144
|
+
|
|
145
|
+
- `reset=True`: replace the entire buffer with `state` (via
|
|
146
|
+
`_reset_history`), re-allocating storage sized to
|
|
147
|
+
``max(state.shape[0], initial_capacity)``.
|
|
148
|
+
- `modify_saved_states=True`: overwrite the most recent
|
|
149
|
+
``state.shape[0]`` already-stored entries in place (via
|
|
150
|
+
`_reset_last_states`), without advancing `current_ti`.
|
|
151
|
+
- otherwise (default): append `state` as new entries starting at
|
|
152
|
+
`current_ti`, growing the buffer first (via `_increase_hist_size`)
|
|
153
|
+
if it would not fit.
|
|
154
|
+
|
|
155
|
+
Parameters
|
|
156
|
+
----------
|
|
157
|
+
state : ndarray
|
|
158
|
+
State(s) to write. In append mode (the default) must have shape
|
|
159
|
+
``(Nt, N, m)`` with ``N`` matching the existing buffer.
|
|
160
|
+
t : ndarray or None
|
|
161
|
+
Time stamp(s) matching `state`. Required in append mode; optional
|
|
162
|
+
when resetting (defaults to zeros) or modifying saved states.
|
|
163
|
+
reset : bool
|
|
164
|
+
If True, replace the entire history (see above).
|
|
165
|
+
modify_saved_states : bool
|
|
166
|
+
If True, overwrite recent entries in place instead of appending
|
|
167
|
+
(see above); ignored if `reset` is True.
|
|
168
|
+
"""
|
|
169
|
+
if t is not None and state.ndim == 3:
|
|
170
|
+
t = np.atleast_1d(t)
|
|
171
|
+
assert state.shape[0] == t.shape[0], f"Length of t ({t.shape}) must match number of time steps in state ({state.shape})."
|
|
172
|
+
if reset: # Reset the full history
|
|
173
|
+
self._reset_history(state, t)
|
|
174
|
+
|
|
175
|
+
elif modify_saved_states: # Update only the last state in history
|
|
176
|
+
self._reset_last_states(new_state=state, t=t)
|
|
177
|
+
else:
|
|
178
|
+
assert t is not None, "Time array t must be provided when adding new states to history."
|
|
179
|
+
assert state.ndim == 3, f"State must have shape (Nt, N, m), but got {state.shape}."
|
|
180
|
+
assert state.shape[1] == self._hist.shape[1], f"State N dimension ({state.shape[1]}) must match history N dimension ({self._hist.shape[1]})."
|
|
181
|
+
|
|
182
|
+
t0 = self.current_ti
|
|
183
|
+
t1 = t0 + state.shape[0]
|
|
184
|
+
|
|
185
|
+
if t1 > self.capacity:
|
|
186
|
+
self._increase_hist_size(Nt=state.shape[0]*10)
|
|
187
|
+
|
|
188
|
+
self._hist[t0:t1] = state
|
|
189
|
+
self._hist_t[t0:t1] = t
|
|
190
|
+
self.current_ti = t1
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def _increase_hist_size(self, Nt=None):
|
|
194
|
+
"""
|
|
195
|
+
With this I avoid np.concatenate every time I want to add new data to history.
|
|
196
|
+
"""
|
|
197
|
+
|
|
198
|
+
if Nt is None:
|
|
199
|
+
Nt = self._initial_capacity
|
|
200
|
+
|
|
201
|
+
new_capacity = self.capacity + Nt
|
|
202
|
+
|
|
203
|
+
# Create new, larger arrays
|
|
204
|
+
new_hist = np.empty((new_capacity, self._hist.shape[1], self._hist.shape[2]),
|
|
205
|
+
dtype=self._hist.dtype)
|
|
206
|
+
new_hist_t = np.empty((new_capacity,))
|
|
207
|
+
|
|
208
|
+
# Copy existing data (expensive operation, but done rarely)
|
|
209
|
+
new_hist[:self.capacity] = self._hist
|
|
210
|
+
new_hist_t[:self.capacity] = self._hist_t
|
|
211
|
+
|
|
212
|
+
# Update attributes
|
|
213
|
+
self._hist = new_hist
|
|
214
|
+
self._hist_t = new_hist_t
|
|
215
|
+
|
|
216
|
+
|