forked from PyDMD/PyDMD
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathparamdmd.py
More file actions
595 lines (492 loc) · 21.3 KB
/
paramdmd.py
File metadata and controls
595 lines (492 loc) · 21.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
"""
Module for the parametric Dynamic Mode Decomposition.
References:
- A Dynamic Mode Decomposition Extension for the Forecasting of Parametric
Dynamical Systems, F. Andreuzzi, N. Demo, G. Rozza, 2023, SIAM Journal on
Applied Dynamical Systems
"""
import pickle
import numpy as np
# roll by one position the shape of X. if X.shape == (a,b,c), the returned
# NumPy array's shape is (b,c,a)
def back_roll_shape(X):
"""
Roll by one position the shape of `X`. if `X.shape == (a,b,c)`, the returned
NumPy array's shape is `(b,c,a)`.
"""
return np.swapaxes(np.swapaxes(X, 0, 1), 1, 2)
def roll_shape(X):
"""
Roll by one position the shape of `X`. if `X.shape == (a,b,c)`, the returned
NumPy array's shape is `(c,a,b)`.
"""
return np.swapaxes(np.swapaxes(X, 0, 2), 1, 2)
class ParametricDMD:
"""
Implementation of the parametric Dynamic Mode Decomposition proposed in
arXiv:2110.09155v1. Both the *monolithic* and *partitioned* methods are
available, see the documentation of the parameter `dmd` for more details.
:param dmd: Instance(s) of :class:`dmdbase.DMDBase`, used by the
paramtric DMD for the prediction of future spatial modal coefficients.
If `dmd` is a `list` the *partitioned* approach is selected, in this
case the number of parameters in the training set should be equal to
the number of DMD instances provided. If `dmd` is not a list, we employ
the monolithic approach.
:type dmd: DMDBase or list
:param spatial_pod: Instance of an object usable for the generation of a
ROM of the dataset (see for instance the class
`POD <https://mathlab.github.io/EZyRB/pod.html>`_ from the Python
library `EZyRB <https://github.com/mathLab/EZyRB>`_).
:param approximation: An interpolator following the standard
learning-prediction pattern (`fit()` -> `predict()`). For some
convenient wrappers see those implemented in
`EZyRB <https://github.com/mathLab/EZyRB>`_).
:param bool light: Whether this instance should be light or not. A light
instance uses less memory since it caches a smaller number of resources.
Setting `light=True` might invalidate several properties (see also
:meth:`training_modal_coefficients`).
:param dmd_fit_args: Positional arguments to be passed to the `fit` method
of the given DMD instance.
:param dmd_fit_kwargs: Keyword arguments to be passed to the `fit` method
of the given DMD instance.
"""
def __init__(
self,
dmd,
spatial_pod,
approximation,
light=False,
dmd_fit_args=None,
dmd_fit_kwargs=None,
):
self._dmd = dmd
self._spatial_pod = spatial_pod
self._approximation = approximation
if dmd_fit_args is None:
dmd_fit_args = tuple()
if not isinstance(dmd_fit_args, (list, tuple)):
raise TypeError("Expected list, tuple or None for dmd_fit_args")
self._dmd_fit_args = dmd_fit_args
if dmd_fit_kwargs is None:
dmd_fit_kwargs = {}
if not isinstance(dmd_fit_kwargs, dict):
raise TypeError("Expected dict or None for dmd_fit_kwargs")
self._dmd_fit_kwargs = dmd_fit_kwargs
self._training_parameters = None
self._parameters = None
self._ntrain = None
self._time_instants = None
self._space_dim = None
self._light = light
self._training_modal_coefficients = None
@property
def is_partitioned(self):
"""
Return `True` if this instance is partitioned, `False` if it is
monolithic.
:type: bool
"""
return self._dmd is not None and isinstance(self._dmd, list)
@property
def _reference_dmd(self):
"""
An object used as a reference for several properties like
:func:`dmd_time` and :func:`dmd_timesteps`. If this instance is
monolithic the returned value is `self._dmd`, otherwise it is the first
item of the list `self._dmd`.
:return: The object used as a reference.
:rtype: pydmd.DMDBase
"""
if self.is_partitioned:
return self._dmd[0]
return self._dmd
@property
def dmd_time(self):
"""
The time dictionary used by the reference DMD instance (see also
:func:`_reference_dmd`). Note that when you set this attribute the
value is set only for the reference DMD (see :func:`_reference_dmd`),
however when :func:`_predict_modal_coefficients` is called the values
of all DMDs become consistent.
:getter: Return the time dictionary used by the reference DMD instance.
:setter: Set the given time dictionary in the field `dmd_time` for all
DMD instances.
:type: pydmd.dmdbase.DMDTimeDict
"""
return self._reference_dmd.dmd_time
@dmd_time.setter
def dmd_time(self, value):
self._reference_dmd.dmd_time = value
@property
def dmd_timesteps(self):
"""
The timesteps in the output of this instance, which coincides with the
timesteps in the output of the reference of this instance (see
:func:`_reference_dmd`).
:return: The timesteps in the output of this instance.
:rtype: list
"""
return self._reference_dmd.dmd_timesteps
@property
def original_time(self):
"""
The original time dictionary used by this instance, which coincides
with the original dictionary used by the reference of this instance
(see :func:`_reference_dmd`).
:return: The original time dictionary used by this instance.
:rtype: dict
"""
return self._reference_dmd.original_time
@property
def original_timesteps(self):
"""
The original timesteps in the input fed to this instance, which
coincides with the original timesteps in the input fed to the reference
of this instance (see :func:`_reference_dmd`).
:return: The original timesteps in the input fed to this instance.
:rtype: list
"""
return self._reference_dmd.original_timesteps
@property
def training_parameters(self):
"""
The original parameters passed when `self.fit` was called, represented
as a 2D array (the index of the parameter vary along the first
dimension).
:type: numpy.ndarray
"""
return self._training_parameters
def _set_training_parameters(self, params):
"""
Set the value of `self._original_parameters`, while checking that the
value provided is a 2D array.
:param numpy.ndarray: A 2D array which contains the original
parameters.
"""
if isinstance(params, list):
params = np.array(params)
if params.ndim == 1:
params = params[:, None]
if params.ndim > 2:
raise ValueError("Parameters must be stored in 2D arrays.")
self._training_parameters = params
@property
def parameters(self):
"""
The new parameters to be used in `reconstructed_data`, represented
as a 2D array (the index of the parameter vary along the first
dimension). For, instance, the following feeds a set of four 3D
parameters to `ParametricDMD`:
>>> from pydmd import ParametricDMD
>>> pdmd = ParametricDMD(...)
>>> pdmd.fit(...)
>>> p0 = [0.1, 0.2, 0.1]
>>> p1 = [0.1, 0.2, 0.3],
>>> p2 = [0.2, 0.2, 0.2],
>>> p3 = [0.1, 0.2, 0.2]
>>> pdmd.parameters = np.array([p0,p1,p2,p3])
Therefore, when we collect the results from `reconstructed_data`:
>>> result = pdmd.reconstructed_data
>>> # reconstruction corresponding to p0
>>> rec_p0 = result[0]
>>> # reconstruction corresponding to p1
>>> rec_p1 = result[1]
>>> ...
:getter: Return the current parameters.
:setter: Change the current parameters.
:type: numpy.ndarray
"""
return self._parameters if hasattr(self, "_parameters") else None
@parameters.setter
def parameters(self, value):
if isinstance(value, list):
value = np.array(value)
if value.ndim == 1:
value = value[:, None]
elif value.ndim > 2:
raise ValueError("Parameters must be stored in 2D arrays.")
self._parameters = value
def _arrange_parametric_snapshots(self, X):
"""
Arrange the given parametric snapshots (see :func:`fit` for an overview
of the shape of `X`) into a 2D matrix such that the shape is distributed
as follows:
- 0: Space;
- 1: Time/Parameter.
Time varies faster than the parameter along the columns of the matrix.
An overview of the shape of the resulting matrix:
.. math::
M = \\begin{bmatrix}
x_1(t_1,\\mu_1) & \dots & x_1(t_n,\\mu_1) & x_1(t_1,\\mu_1)
& \dots & x_1(t_{n-1},\\mu_k) & x_1(t_n,\\mu_k)\\\\
\\vdots & \\dots & \\vdots & \\vdots & \\dots & \\vdots
& \\dots\\\\
x_m(t_1,\\mu_1) & \dots & x_m(t_n,\\mu_1) & x_m(t_1,\\mu_1)
& \dots & x_m(t_{n-1},\\mu_k) & x_m(t_n,\\mu_k)
\\end{bmatrix}
:math:`x(t, \mu) \in \mathbb{R}^m` is the functon which represents the
parametric system at time :math:`t` with the parameter :math:`\\mu`.
:param X: Parametric snapshots (distribition of axes like in
:func:`fit`).
:type X: numpy.ndarray
:return: Parametric snapshots arranged in a 2D matrix like explained
above.
:rtype: numpy.ndarray
"""
# swap parameters dimension and space dimension
X = np.swapaxes(X, 0, 1)
return X.reshape((X.shape[0], -1), order="C")
def _compute_training_modal_coefficients(self, space_timemu):
"""
Compute the POD modal coefficient from the given matrix, and put
the resulting coefficients (along with their time evolution in matrix
form) into a list.
In symbols, from the given matrix :math:`X^x_{t,\mu} \in
\mathbb{R}^{m \\times nk}` we compute the modal
coefficients corresponding to its columns. At this point we have
something like this:
.. math::
\\widetilde{X}^s_{t,\mu} = \\begin{bmatrix}
\\widetilde{x}_1(t_1,\\mu_1), & \dots &
\\widetilde{x}_1(t_n,\\mu_1), &
\\widetilde{x}_1(t_1,\\mu_1), & \dots &
\\widetilde{x}_1(t_{n-1},\\mu_k), &
\\widetilde{x}_1(t_n,\\mu_k)\\\\
\\vdots & \\dots & \\vdots & \\vdots & \\dots & \\vdots &
\\dots\\\\
\\widetilde{x}_p(t_1,\\mu_1), & \dots & x_p(t_n,\\mu_1) &
\\widetilde{x}_p(t_1,\\mu_1), & \dots &
\\widetilde{x}_p(t_{n-1},\\mu_k), &
\\widetilde{x}_p(t_n,\\mu_k)
\\end{bmatrix} \in \mathbb{R}^{p \\times nk}
Detecting the sub-matrices corresponding to the time evolution of the
POD modal coefficients corresponding to a particular realization of the
system for some parameter :math:`\\mu_i`, we may rewrite this matrix as
follows:
.. math::
\\widetilde{X}^s_{t,\mu} = \\begin{bmatrix}
\\widetilde{X}_{\\mu_1}, & \dots & \\widetilde{X}_{\\mu_1}
\\end{bmatrix}
The returned list contains the matrices
:math:`\\widetilde{X}_{\\mu_i} \in \\mathbb{p \\times n}`.
:param space_timemu: A matrix containing parametric/time snapshots like
the matrix returned by :func:`_arrange_parametric_snapshots`. The
input size should be `p x nk` where `p` is the dimensionality of
the full-dimensional space, `k` is the number of training parameters
and `n` is the number of time instants used for the training.
:type space_timemu: numpy.ndarray
:return: A list of `k` matrices. Each matrix has shape `r x n` where `r`
is the dimensionality of the reduced POD space, and `n`, `k` are the
same of the parameter `space_timemu`.
:rtype: list
"""
spatial_modal_coefficients = self._spatial_pod.fit(space_timemu).reduce(
space_timemu
)
return np.split(spatial_modal_coefficients, self._ntrain, axis=1)
def fit(self, X, training_parameters):
"""
Compute the parametric Dynamic Modes Decomposition from the input data
stored in the array `X`. The shape of the parameter `X` must be
used as follows:
- 0: Training parameters;
- 1: Space;
- 2: Training time instants.
The parameter `training_parameters` contains the list of training
parameters corresponding to the training datasets in `X`. For instance,
`training_parameters[0]` is the parameter which generated the dataset
in `X[0]`. For this reason `len(training_parameters)` should be equal
to `X.shape[0]`.
:param numpy.ndarray X: Training snapshots of the parametric system,
observed for two or more parameters and in multiple time instants.
:param numpy.ndarray training_parameters: Training parameters
corresponding to the snapshots in `X`.
"""
if X.shape[0] != len(training_parameters):
raise ValueError(
"Unexpected number of snapshots for the given"
"parameters. Received {} parameters, and {} snapshots".format(
len(training_parameters), X.shape[0]
)
)
# we store these values for faster access
self._ntrain, self._space_dim, self._time_instants = X.shape
if self.is_partitioned and self._ntrain != len(self._dmd):
raise ValueError(
"Invalid number of DMD instances provided: "
"expected n_train={}, got {}".format(
self._ntrain, len(self._dmd)
)
)
# store the training parameters: they will be used in
# `reconstructed_data`
self._set_training_parameters(training_parameters)
# arrange the parametric snapshots in a convenient way to perform POD
space_timemu = self._arrange_parametric_snapshots(X)
# obtain POD modal coefficients from the training set
training_modal_coefficients = self._compute_training_modal_coefficients(
space_timemu
)
if not self._light:
self._training_modal_coefficients = np.array(
training_modal_coefficients
)
# fit DMD(s) with POD modal coefficients
if self.is_partitioned:
# partitioned parametric DMD
for dmd, data in zip(self._dmd, training_modal_coefficients):
dmd.fit(data, *self._dmd_fit_args, **self._dmd_fit_kwargs)
else:
spacemu_time = np.vstack(training_modal_coefficients)
self._dmd.fit(
spacemu_time, *self._dmd_fit_args, **self._dmd_fit_kwargs
)
# ------------------------------------------------------------
# getter properties for intermediate values of the computation
@property
def training_modal_coefficients(self):
"""
Modal coefficients of the input dataset. Since this is cached after
calls to :func:`fit` this property needs to be called after :func:`fit`,
and `light` should be set to `False` in the constructor of the class.
The tensor returned has the following shape:
- 0: Training parameters;
- 1: Dimensionality of the POD sub-space;
- 2: Time.
"""
if self._light:
raise RuntimeError(
"""Light instances do not cache the property
`training_modal_coefficients`."""
)
if self._training_modal_coefficients is None:
raise RuntimeError(
"""
Property not available now, did you call `fit()`?"""
)
return self._training_modal_coefficients
@property
def forecasted_modal_coefficients(self):
"""
Modal coefficients forecasted for the input parameters.
The tensor returned has the following shape:
- 0: Training parameters;
- 1: Dimensionality of the POD sub-space;
- 2: Time.
"""
forecasted = self._predict_modal_coefficients()
return forecasted.reshape((self._ntrain, -1, forecasted.shape[1]))
@property
def interpolated_modal_coefficients(self):
"""
Modal coefficients forecasted and then interpolated for the untested
parameters.
The tensor returned has the following shape:
- 0: Parameters;
- 1: Dimensionality of the POD sub-space;
- 2: Time.
"""
forecasted = self._predict_modal_coefficients()
return self._interpolate_missing_modal_coefficients(forecasted)
# ------------------------------------------------------------
def _predict_modal_coefficients(self):
"""
Predict future spatial modal coefficients in the time instants in
`dmd_time`.
:return: Predicted spatial modal coefficients. Shape: `rk x n` (`r`:
dimensionality of POD subspace, `k`: number of training parameters,
`n`: number of snapshots).
:rtype: numpy.ndarray
"""
if self.is_partitioned:
for dmd in self._dmd:
# we want to "bound" this DMD objects' dmd_time
dmd.dmd_time = self._reference_dmd.dmd_time
return np.vstack(
list(map(lambda dmd: dmd.reconstructed_data, self._dmd))
)
return self._dmd.reconstructed_data
def _interpolate_missing_modal_coefficients(
self, forecasted_modal_coefficients
):
"""
Interpolate spatial modal coefficients for the (untested) parameters
stored in `parameters`. The interpolation uses the interpolator
provided in the constructor of this instance.
The returned value is a 3D tensor, its shape is used as follows:
- 0: Parameters;
- 1: Reduced POD space;
- 2: Time.
:param numpy.ndarray forecasted_modal_coefficients: An array of spatial
modal coefficients for tested parameters. The shape is used like in
the matrix returned by :func:`_predict_modal_coefficients`.
:return: An array of (interpolated) spatial modal coefficients for
untested parameters.
:rtype: numpy.ndarray
"""
if self.parameters is None or len(self.parameters) == 0:
raise ValueError(
"""
Unknown parameters not found. Did you set `ParametricDMD.parameters`?"""
)
approx = self._approximation
forecasted_modal_coefficients = forecasted_modal_coefficients.reshape(
(self._ntrain, -1, forecasted_modal_coefficients.shape[1]),
order="C",
)
def interpolate_future_pod_coefficients(time_slice):
approx.fit(self.training_parameters, time_slice)
return approx.predict(self.parameters)
return np.dstack(
[
interpolate_future_pod_coefficients(time_slice)[..., None]
for time_slice in roll_shape(forecasted_modal_coefficients)
]
)
@property
def reconstructed_data(self):
"""
Get the reconstructed data, for the time instants specified in
`dmd_time`, and the parameters stored in `parameters`.
The shape of the returned data is distributed as follows:
- 0: Parameters;
- 1: Space;
- 2: Time.
:return: Snapshots predicted/interpolated using parametric DMD and the
given method for ROM.
:rtype: numpy.ndarray
"""
forecasted_modal_coefficients = self._predict_modal_coefficients()
interpolated_modal_coefficients = (
self._interpolate_missing_modal_coefficients(
forecasted_modal_coefficients
)
)
return np.apply_along_axis(
self._spatial_pod.expand, 1, interpolated_modal_coefficients
)
def save(self, fname):
"""
Save the object to `fname` using the pickle module.
:param str fname: the name of file where the reduced order model will
be saved.
Example:
>>> from pydmd import ParametricDMD
>>> pdmd = ParametricDMD(...) # Construct here the rom
>>> pdmd.fit(...)
>>> pdmd.save('pydmd.pdmd')
"""
with open(fname, "wb") as output:
pickle.dump(self, output, pickle.HIGHEST_PROTOCOL)
@staticmethod
def load(fname):
"""
Load the object from `fname` using the pickle module.
:return: The `ReducedOrderModel` loaded
Example:
>>> from pydmd import ParametricDMD
>>> pdmd = ParametricDMD.load('pydmd.pdmd')
>>> print(pdmd.reconstructed_data)
"""
with open(fname, "rb") as output:
return pickle.load(output)