Пример #1
0
def get_gap_rom(rom):
    """Based on a rom, create model which is used to evaluate H2-Gap norm."""
    A = to_matrix(rom.A, format='dense')
    B = to_matrix(rom.B, format='dense')
    C = to_matrix(rom.C, format='dense')

    if isinstance(rom.E, IdentityOperator):
        P = spla.solve_continuous_are(A.T,
                                      C.T,
                                      B.dot(B.T),
                                      np.eye(len(C)),
                                      balanced=False)
        F = P @ C.T
    else:
        E = to_matrix(rom.E, format='dense')
        P = spla.solve_continuous_are(A.T,
                                      C.T,
                                      B.dot(B.T),
                                      np.eye(len(C)),
                                      e=E.T,
                                      balanced=False)
        F = E @ P @ C.T

    AF = A - F @ C
    mFB = np.concatenate((-F, B), axis=1)
    return LTIModel.from_matrices(
        AF, mFB, C, E=None if isinstance(rom.E, IdentityOperator) else E)
Пример #2
0
def _poles_b_c_to_lti(poles, b, c):
    r"""Create an |LTIModel| from poles and residue rank-1 factors.

    Returns an |LTIModel| with real matrices such that its transfer
    function is

    .. math::
        \sum_{i = 1}^r \frac{c_i b_i^T}{s - \lambda_i}

    where :math:`\lambda_i, b_i, c_i` are the poles and residue rank-1
    factors.

    Parameters
    ----------
    poles
        Sequence of poles.
    b
        |VectorArray| of right residue rank-1 factors.
    c
        |VectorArray| of left residue rank-1 factors.

    Returns
    -------
    |LTIModel|.
    """
    A, B, C = [], [], []
    for i, pole in enumerate(poles):
        if pole.imag == 0:
            A.append(pole.real)
            B.append(b[i].to_numpy().real)
            C.append(c[i].to_numpy().real.T)
        elif pole.imag > 0:
            A.append([[pole.real, pole.imag],
                      [-pole.imag, pole.real]])
            bi = b[i].to_numpy()
            B.append(np.vstack([2 * bi.real, -2 * bi.imag]))
            ci = c[i].to_numpy()
            C.append(np.hstack([ci.real.T, ci.imag.T]))
    A = spla.block_diag(*A)
    B = np.vstack(B)
    C = np.hstack(C)
    return LTIModel.from_matrices(A, B, C)
Пример #3
0
Файл: h2.py Проект: meretp/pymor
def _poles_b_c_to_lti(poles, b, c):
    r"""Create an |LTIModel| from poles and residue rank-1 factors.

    Returns an |LTIModel| with real matrices such that its transfer
    function is

    .. math::
        \sum_{i = 1}^r \frac{c_i b_i^T}{s - \lambda_i}

    where :math:`\lambda_i, b_i, c_i` are the poles and residue rank-1
    factors.

    Parameters
    ----------
    poles
        Sequence of poles.
    b
        |NumPy array| of shape `(rom.order, rom.dim_input)`.
    c
        |NumPy array| of shape `(rom.order, rom.dim_output)`.

    Returns
    -------
    |LTIModel|.
    """
    A, B, C = [], [], []
    for i, pole in enumerate(poles):
        if pole.imag == 0:
            A.append(pole.real)
            B.append(b[i].real)
            C.append(c[i].real[:, np.newaxis])
        elif pole.imag > 0:
            A.append([[pole.real, pole.imag],
                      [-pole.imag, pole.real]])
            B.append(np.vstack([2 * b[i].real, -2 * b[i].imag]))
            C.append(np.hstack([c[i].real[:, np.newaxis], c[i].imag[:, np.newaxis]]))
    A = spla.block_diag(*A)
    B = np.vstack(B)
    C = np.hstack(C)
    return LTIModel.from_matrices(A, B, C)
Пример #4
0
    def reduce(self, sigma, b, c):
        """Realization-independent tangential Hermite interpolation.

        Parameters
        ----------
        sigma
            Interpolation points (closed under conjugation), list of length `r`.
        b
            Right tangential directions, |NumPy array| of shape `(fom.input_dim, r)`.
        c
            Left tangential directions, |NumPy array| of shape `(fom.output_dim, r)`.

        Returns
        -------
        lti
            The reduced-order |LTIModel| interpolating the transfer function of `fom`.
        """
        r = len(sigma)
        assert isinstance(b, np.ndarray) and b.shape == (self.fom.input_dim, r)
        assert isinstance(c,
                          np.ndarray) and c.shape == (self.fom.output_dim, r)

        # rescale tangential directions (to avoid overflow or underflow)
        if b.shape[0] > 1:
            for i in range(r):
                b[:, i] /= spla.norm(b[:, i])
        else:
            b = np.ones((1, r))
        if c.shape[0] > 1:
            for i in range(r):
                c[:, i] /= spla.norm(c[:, i])
        else:
            c = np.ones((1, r))

        # matrices of the interpolatory LTI system
        Er = np.empty((r, r), dtype=complex)
        Ar = np.empty((r, r), dtype=complex)
        Br = np.empty((r, self.fom.input_dim), dtype=complex)
        Cr = np.empty((self.fom.output_dim, r), dtype=complex)

        Hs = [self.fom.eval_tf(s, mu=self.mu) for s in sigma]
        dHs = [self.fom.eval_dtf(s, mu=self.mu) for s in sigma]

        for i in range(r):
            for j in range(r):
                if i != j:
                    Er[i, j] = -c[:, i].dot(
                        (Hs[i] - Hs[j]).dot(b[:, j])) / (sigma[i] - sigma[j])
                    Ar[i, j] = -c[:, i].dot(
                        (sigma[i] * Hs[i] - sigma[j] * Hs[j])).dot(b[:, j]) / (
                            sigma[i] - sigma[j])
                else:
                    Er[i, i] = -c[:, i].dot(dHs[i].dot(b[:, i]))
                    Ar[i, i] = -c[:, i].dot(
                        (Hs[i] + sigma[i] * dHs[i]).dot(b[:, i]))
            Br[i, :] = Hs[i].T.dot(c[:, i])
            Cr[:, i] = Hs[i].dot(b[:, i])

        # transform the system to have real matrices
        T = np.zeros((r, r), dtype=complex)
        for i in range(r):
            if sigma[i].imag == 0:
                T[i, i] = 1
            else:
                indices = np.nonzero(
                    np.isclose(sigma[i + 1:], sigma[i].conjugate()))[0]
                if len(indices) > 0:
                    j = i + 1 + indices[0]
                    T[i, i] = 1
                    T[i, j] = 1
                    T[j, i] = -1j
                    T[j, j] = 1j
        Er = (T.dot(Er).dot(T.conj().T)).real
        Ar = (T.dot(Ar).dot(T.conj().T)).real
        Br = (T.dot(Br)).real
        Cr = (Cr.dot(T.conj().T)).real

        return LTIModel.from_matrices(Ar,
                                      Br,
                                      Cr,
                                      D=None,
                                      E=Er,
                                      cont_time=self.fom.cont_time)
Пример #5
0
    def reduce(self, sigma, b, c):
        """Realization-independent tangential Hermite interpolation.

        Parameters
        ----------
        sigma
            Interpolation points (closed under conjugation), sequence of
            length `r`.
        b
            Right tangential directions, |NumPy array| of shape
            `(r, fom.dim_input)`.
        c
            Left tangential directions, |NumPy array| of shape
            `(r, fom.dim_output)`.

        Returns
        -------
        lti
            The reduced-order |LTIModel| interpolating the transfer
            function of `fom`.
        """
        r = len(sigma)
        assert b.shape == (r, self.fom.dim_input)
        assert c.shape == (r, self.fom.dim_output)

        # rescale tangential directions (to avoid overflow or underflow)
        b = b * (1 / np.linalg.norm(b)) if b.shape[1] > 1 else np.ones((r, 1))
        c = c * (1 / np.linalg.norm(c)) if c.shape[1] > 1 else np.ones((r, 1))

        # matrices of the interpolatory LTI system
        Er = np.empty((r, r), dtype=np.complex_)
        Ar = np.empty((r, r), dtype=np.complex_)
        Br = np.empty((r, self.fom.dim_input), dtype=np.complex_)
        Cr = np.empty((self.fom.dim_output, r), dtype=np.complex_)

        Hs = [self.fom.eval_tf(s, mu=self.mu) for s in sigma]
        dHs = [self.fom.eval_dtf(s, mu=self.mu) for s in sigma]

        for i in range(r):
            for j in range(r):
                if i != j:
                    Er[i, j] = -c[i] @ (Hs[i] - Hs[j]) @ b[j] / (sigma[i] -
                                                                 sigma[j])
                    Ar[i, j] = (
                        -c[i] @ (sigma[i] * Hs[i] - sigma[j] * Hs[j]) @ b[j] /
                        (sigma[i] - sigma[j]))
                else:
                    Er[i, i] = -c[i] @ dHs[i] @ b[i]
                    Ar[i, i] = -c[i] @ (Hs[i] + sigma[i] * dHs[i]) @ b[i]
            Br[i, :] = Hs[i].T @ c[i]
            Cr[:, i] = Hs[i] @ b[i]

        # transform the system to have real matrices
        T = np.zeros((r, r), dtype=np.complex_)
        for i in range(r):
            if sigma[i].imag == 0:
                T[i, i] = 1
            else:
                j = np.argmin(np.abs(sigma - sigma[i].conjugate()))
                if i < j:
                    T[i, i] = 1
                    T[i, j] = 1
                    T[j, i] = -1j
                    T[j, j] = 1j
        Er = (T @ Er @ T.conj().T).real
        Ar = (T @ Ar @ T.conj().T).real
        Br = (T @ Br).real
        Cr = (Cr @ T.conj().T).real

        return LTIModel.from_matrices(Ar,
                                      Br,
                                      Cr,
                                      None,
                                      Er,
                                      cont_time=self.fom.cont_time)
Пример #6
0
    def reduce(self, sigma, b, c):
        """Realization-independent tangential Hermite interpolation.

        Parameters
        ----------
        sigma
            Interpolation points (closed under conjugation), list of length `r`.
        b
            Right tangential directions, |NumPy array| of shape `(fom.input_dim, r)`.
        c
            Left tangential directions, |NumPy array| of shape `(fom.output_dim, r)`.

        Returns
        -------
        lti
            The reduced-order |LTIModel| interpolating the transfer function of `fom`.
        """
        r = len(sigma)
        assert isinstance(b, np.ndarray) and b.shape == (self.fom.input_dim, r)
        assert isinstance(c, np.ndarray) and c.shape == (self.fom.output_dim, r)

        # rescale tangential directions (to avoid overflow or underflow)
        if b.shape[0] > 1:
            for i in range(r):
                b[:, i] /= spla.norm(b[:, i])
        else:
            b = np.ones((1, r))
        if c.shape[0] > 1:
            for i in range(r):
                c[:, i] /= spla.norm(c[:, i])
        else:
            c = np.ones((1, r))

        # matrices of the interpolatory LTI system
        Er = np.empty((r, r), dtype=complex)
        Ar = np.empty((r, r), dtype=complex)
        Br = np.empty((r, self.fom.input_dim), dtype=complex)
        Cr = np.empty((self.fom.output_dim, r), dtype=complex)

        Hs = [self.fom.eval_tf(s) for s in sigma]
        dHs = [self.fom.eval_dtf(s) for s in sigma]

        for i in range(r):
            for j in range(r):
                if i != j:
                    Er[i, j] = -c[:, i].dot((Hs[i] - Hs[j]).dot(b[:, j])) / (sigma[i] - sigma[j])
                    Ar[i, j] = -c[:, i].dot((sigma[i] * Hs[i] - sigma[j] * Hs[j])).dot(b[:, j]) / (sigma[i] - sigma[j])
                else:
                    Er[i, i] = -c[:, i].dot(dHs[i].dot(b[:, i]))
                    Ar[i, i] = -c[:, i].dot((Hs[i] + sigma[i] * dHs[i]).dot(b[:, i]))
            Br[i, :] = Hs[i].T.dot(c[:, i])
            Cr[:, i] = Hs[i].dot(b[:, i])

        # transform the system to have real matrices
        T = np.zeros((r, r), dtype=complex)
        for i in range(r):
            if sigma[i].imag == 0:
                T[i, i] = 1
            else:
                indices = np.nonzero(np.isclose(sigma[i + 1:], sigma[i].conjugate()))[0]
                if len(indices) > 0:
                    j = i + 1 + indices[0]
                    T[i, i] = 1
                    T[i, j] = 1
                    T[j, i] = -1j
                    T[j, j] = 1j
        Er = (T.dot(Er).dot(T.conj().T)).real
        Ar = (T.dot(Ar).dot(T.conj().T)).real
        Br = (T.dot(Br)).real
        Cr = (Cr.dot(T.conj().T)).real

        return LTIModel.from_matrices(Ar, Br, Cr, D=None, E=Er, cont_time=self.fom.cont_time)