Esempio n. 1
0
    def test_predict(self, n=50, m=10, k=100, r=3):
        """Test
        _core._interpolate._inferred.InterpolatedInferredContinuousROM.predict().
        """
        # Get data for fitting.
        X1, Xdot1, U1 = _get_data(n, k, m)
        X2, Xdot2, U2 = X1 + 1, Xdot1.copy(), U1 + 1
        Xs = [X1, X2]
        Xdots = [Xdot1, Xdot2]
        Us = [U1, U2]
        ps = [1, 2]
        Vr = la.svd(np.hstack(Xs))[0][:, :r]

        # Parameters for predicting.
        x0 = np.zeros(n)
        nt = 5
        t = np.linspace(0, .01 * nt, nt)
        u = lambda t: np.zeros(m)

        # Fit / predict with no inputs.
        model = roi.InterpolatedInferredContinuousROM("AH")
        model.fit(Vr, ps, Xs, Xdots)
        model.predict(1, x0, t)
        model.predict(1.5, x0, t)

        # Fit / predict with inputs.
        model = roi.InterpolatedInferredContinuousROM("AHB")
        model.fit(Vr, ps, Xs, Xdots, Us)
        model.predict(1, x0, t, u)
        model.predict(1.5, x0, t, u)
Esempio n. 2
0
    def test_predict(self):
        """Test _core.InterpolatedInferredContinuousROM.predict()."""
        model = roi.InterpolatedInferredContinuousROM("cAH")

        # Get data for fitting.
        n, m, k, r = 50, 10, 100, 5
        X1, Xdot1, U1 = _get_data(n, k, m)
        X2, Xdot2, U2 = X1+1, Xdot1.copy(), U1+1
        Xs = [X1, X2]
        Xdots = [Xdot1, Xdot2]
        Us = [U1, U2]
        ps = [1, 2]
        Vr = la.svd(np.hstack(Xs))[0][:,:r]

        # Parameters for predicting.
        x0 = np.random.random(n)
        nt = 5
        t = np.linspace(0, .01*nt, nt)
        u = lambda t: np.ones(10)

        # Fit / predict with no inputs.
        model.fit(Vr, ps, Xs, Xdots)
        model.predict(1, x0, t)
        model.predict(1.5, x0, t)

        # Fit / predict with inputs.
        model.modelform = "cAHB"
        model.fit(Vr, ps, Xs, Xdots, Us)
        model.predict(1, x0, t, u)
        model.predict(1.5, x0, t, u)
    def test_str(self):
        """Test _core._base._ParametricMixin.__str__()
        (string representation).
        """
        # Continuous ROMs
        model = roi.InterpolatedInferredContinuousROM("A")
        assert str(model) == \
            "Reduced-order model structure: dx / dt = Ax(t)"
        model.c_ = lambda t: t
        model.A_ = lambda t: t
        model.modelform = "cA"
        assert str(model) == \
            "Reduced-order model structure: dx / dt = c(µ) + A(µ)x(t)"
        model.Hc_ = None
        model.Gc_ = lambda t: t
        model.B_ = None
        model.modelform = "HB"
        assert str(model) == \
            "Reduced-order model structure: dx / dt = H(x(t) ⊗ x(t)) + Bu(t)"
        model.modelform = "G"
        assert str(model) == \
            "Reduced-order model structure: dx / dt = G(µ)(x(t) ⊗ x(t) ⊗ x(t))"

        # Discrete ROMs
        model = roi.AffineIntrusiveDiscreteROM("cH")
        assert str(model) == \
            "Reduced-order model structure: x_{j+1} = c + H(x_{j} ⊗ x_{j})"
        model.c_ = lambda t: t
        model.Hc_ = None
        assert str(model) == \
            "Reduced-order model structure: x_{j+1} = c(µ) + H(x_{j} ⊗ x_{j})"
Esempio n. 4
0
    def test_fit(self):
        """Test
        _core._interpolate._inferred.InterpolatedInferredContinuousROM.fit().
        """
        model = roi.InterpolatedInferredContinuousROM("cAH")

        # Get data for fitting.
        n, m, k, r = 50, 10, 100, 5
        X1, Xdot1, U1 = _get_data(n, k, m)
        X2, Xdot2, U2 = X1 + 1, Xdot1.copy(), U1 + 1
        Xs = [X1, X2]
        Xdots = [Xdot1, Xdot2]
        Us = [U1, U2]
        ps = [1, 2]
        Vr = la.svd(np.hstack(Xs))[0][:, :r]

        # Try with non-scalar parameters.
        with pytest.raises(ValueError) as ex:
            model.fit(Vr, [np.array([1, 1]), np.array([2, 2])], Xs, Xdots)
        assert ex.value.args[0] == "only scalar parameter values are supported"

        # Try with bad number of Xs.
        with pytest.raises(ValueError) as ex:
            model.fit(Vr, ps, [X1, X2, X2 + 1], Xdots)
        assert ex.value.args[0] == \
            "num parameter samples != num state snapshot sets (2 != 3)"

        # Try with bad number of Xdots.
        with pytest.raises(ValueError) as ex:
            model.fit(Vr, ps, Xs, Xdots + [Xdot1])
        assert ex.value.args[0] == \
            "num parameter samples != num velocity snapshot sets (2 != 3)"

        # Try with varying input sizes.
        model.modelform = "cAHB"
        with pytest.raises(ValueError) as ex:
            model.fit(Vr, ps, Xs, Xdots, [U1, U2[:-1]])
        assert ex.value.args[0] == "control inputs not aligned"

        # Fit correctly with no inputs.
        model.modelform = "cAH"
        model.fit(Vr, ps, Xs, Xdots)
        for attr in ["models_", "fs_"
                     ] + [s[:-1] + "s_" for s in _LSTSQ_REPORTS]:
            assert hasattr(model, attr)
            assert len(getattr(model, attr)) == len(model.models_)

        # Fit correctly with inputs.
        model.modelform = "cAHB"
        model.fit(Vr, ps, Xs, Xdots, Us)
        assert len(model) == len(ps)

        # Test again with Vr = None and projected inputs.
        Xs_ = [Vr.T @ X for X in Xs]
        Xdots_ = [Vr.T @ Xdot for Xdot in Xdots]
        model.fit(None, ps, Xs_, Xdots_, Us)
        assert len(model) == len(ps)
        assert model.Vr is None
        assert model.n is None
    def test_fit(self):
        """Test _core.InterpolatedInferredContinuousROM.fit()."""
        model = roi.InterpolatedInferredContinuousROM("cAH")

        # Get data for fitting.
        n, m, k, r = 50, 10, 100, 5
        X1, Xdot1, U1 = _get_data(n, k, m)
        X2, Xdot2, U2 = X1+1, Xdot1.copy(), U1+1
        Xs = [X1, X2]
        Xdots = [Xdot1, Xdot2]
        Us = [U1, U2]
        ps = [1, 2]
        Vr = la.svd(np.hstack(Xs))[0][:,:r]

        # Try with non-scalar parameters.
        with pytest.raises(ValueError) as ex:
            model.fit(Vr, [np.array([1,1]), np.array([2,2])], Xs, Xdots)
        assert ex.value.args[0] == "only scalar parameter values are supported"

        # Try with bad number of Xs.
        with pytest.raises(ValueError) as ex:
            model.fit(Vr, ps, [X1, X2, X2+1], Xdots)
        assert ex.value.args[0] == \
            "num parameter samples != num state snapshot sets (2 != 3)"

        # Try with bad number of Xdots.
        with pytest.raises(ValueError) as ex:
            model.fit(Vr, ps, Xs, Xdots + [Xdot1])
        assert ex.value.args[0] == \
            "num parameter samples != num velocity snapshot sets (2 != 3)"

        # Try with varying input sizes.
        model.modelform = "cAHB"
        with pytest.raises(ValueError) as ex:
            model.fit(Vr, ps, Xs, Xdots, [U1, U2[:-1]])
        assert ex.value.args[0] == \
            "shape of 'U' inconsistent across samples"

        # Fit correctly with no inputs.
        model.modelform = "cAH"
        model.fit(Vr, ps, Xs, Xdots)
        for attr in ["models_", "dataconds_", "residuals_", "fs_"]:
            assert hasattr(model, attr)
            assert len(getattr(model, attr)) == len(model.models_)

        # Fit correctly with inputs.
        model.modelform = "cAHB"
        model.fit(Vr, ps, Xs, Xdots, Us)

        assert len(model) == len(ps)