dynamodels 0.1.0__py3-none-any.whl
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/__init__.py +30 -0
- dynamodels/history.py +216 -0
- dynamodels/integrator.py +499 -0
- dynamodels/model.py +1152 -0
- dynamodels/physical/__init__.py +15 -0
- dynamodels/physical/annular.py +186 -0
- dynamodels/physical/kuramoto_sivashinsky.py +659 -0
- dynamodels/physical/lorenz63.py +199 -0
- dynamodels/physical/lorenz96.py +264 -0
- dynamodels/physical/rijke.py +373 -0
- dynamodels/physical/van_der_pol.py +81 -0
- dynamodels/utils.py +234 -0
- dynamodels-0.1.0.dist-info/METADATA +87 -0
- dynamodels-0.1.0.dist-info/RECORD +17 -0
- dynamodels-0.1.0.dist-info/WHEEL +5 -0
- dynamodels-0.1.0.dist-info/licenses/LICENSE +21 -0
- dynamodels-0.1.0.dist-info/top_level.txt +1 -0
dynamodels/__init__.py
ADDED
|
@@ -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
|
+
]
|
dynamodels/history.py
ADDED
|
@@ -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
|
+
|