Skip to content

Commit

Permalink
add svd rank param for omega in dmdc
Browse files Browse the repository at this point in the history
  • Loading branch information
mtezzele committed Jan 22, 2021
1 parent 714ac2a commit fd151c3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
25 changes: 16 additions & 9 deletions pydmd/dmdc.py
Expand Up @@ -27,11 +27,18 @@ class DMDc(DMDBase):
is 0, that means no truncation.
:param bool opt: flag to compute optimal amplitudes. See :class:`DMDBase`.
Default is False.
:param svd_rank_omega: the rank for the truncation of the aumented matrix
omega composed by the left snapshots matrix and the control. Used only
for the `_fit_B_unknown` method of this class. It should be greater or
equal than `svd_rank`. For the possible values please refer to the
`svd_rank` parameter description above.
:type svd_rank_omega: int or float
"""
def __init__(self, svd_rank=0, tlsq_rank=0, opt=False):
def __init__(self, svd_rank=0, tlsq_rank=0, opt=False, svd_rank_omega=-1):
self.svd_rank = svd_rank
self.tlsq_rank = tlsq_rank
self.opt = opt
self.svd_rank_omega = svd_rank_omega
self.original_time = None

self._eigs = None
Expand Down Expand Up @@ -107,7 +114,7 @@ def _fit_B_known(self, X, Y, B):
:param numpy.ndarray X: the first matrix of original snapshots.
:param numpy.ndarray Y: the second matrix of original snapshots.
:param numpy.ndarray I: the input control matrix.
:param numpy.ndarray B: the matrib B.
:param numpy.ndarray B: the matrix B.
"""
X, Y = self._compute_tlsq(X, Y, self.tlsq_rank)

Expand All @@ -131,17 +138,15 @@ def _fit_B_unknown(self, X, Y):
:param numpy.ndarray X: the first matrix of original snapshots.
:param numpy.ndarray Y: the second matrix of original snapshots.
:param numpy.ndarray I: the input control matrix.
"""

omega = np.vstack([X, self._controlin])

Up, sp, Vp = self._compute_svd(omega, self.svd_rank)

Up, sp, Vp = self._compute_svd(omega, self.svd_rank_omega)
Up1 = Up[:self._snapshots.shape[0], :]
Up2 = Up[self._snapshots.shape[0]:, :]
# TODO: a second svd_rank?
Ur, sr, Vr = self._compute_svd(Y, -1)

Ur, sr, Vr = self._compute_svd(Y, self.svd_rank)
self._basis = Ur

self._Atilde = Ur.T.conj().dot(Y).dot(Vp).dot(np.diag(
Expand All @@ -167,7 +172,9 @@ def fit(self, X, I, B=None):
:type X: numpy.ndarray or iterable
:param I: the control input.
:type I: numpy.ndarray or iterable
:param numpy.ndarray B:
:param numpy.ndarray B: matrix that controls the control input
influences the system evolution.
:type B: numpy.ndarray or iterable
"""
self._snapshots, self._snapshots_shape = self._col_major_2darray(X)
self._controlin, self._controlin_shape = self._col_major_2darray(I)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_dmdc.py
Expand Up @@ -36,6 +36,18 @@ def test_eigs_b_known(self):
real_eigs = np.array([0.1, 1.5])
np.testing.assert_array_almost_equal(dmdc.eigs, real_eigs)

def test_eigs_b_unknown(self):
system = create_system_without_B()
dmdc = DMDc(svd_rank=3, opt=False, svd_rank_omega=4)
dmdc.fit(system['snapshots'], system['u'])
self.assertEqual(dmdc.eigs.shape[0], 3)

def test_modes_b_unknown(self):
system = create_system_without_B()
dmdc = DMDc(svd_rank=3, opt=False, svd_rank_omega=4)
dmdc.fit(system['snapshots'], system['u'])
self.assertEqual(dmdc.modes.shape[1], 3)

def test_reconstruct_b_known(self):
system = create_system_with_B()
dmdc = DMDc(svd_rank=-1)
Expand Down

0 comments on commit fd151c3

Please sign in to comment.