Пример #1
0
 def test_init(self):
     self.assertRaises(TypeError, sim.WeakFormulation, ["a", "b"])
     sim.WeakFormulation(ph.ScalarTerm(self.field_var_at1))  # scalar case
     sim.WeakFormulation([
         ph.ScalarTerm(self.field_var_at1),
         ph.IntegralTerm(self.field_var, (0, 1))
     ])  # vector case
Пример #2
0
    def test_Input_term(self):
        terms = sim.parse_weak_formulation(
            sim.WeakFormulation(self.input_term2)).get_terms()
        self.assertTrue(
            np.allclose(terms["G"][0][1], np.array([[0], [-2], [2]])))

        terms = sim.parse_weak_formulation(
            sim.WeakFormulation(self.input_term1_squared)).get_terms()
        self.assertTrue(
            np.allclose(terms["G"][0][2], np.array([[0], [0], [1]])))
Пример #3
0
    def setUp(self):

        self.u = CorrectInput()

        # setup temp and spat domain
        spat_domain = sim.Domain((0, 1), num=3)
        nodes, ini_funcs = sf.cure_interval(sf.LagrangeFirstOrder,
                                            spat_domain.bounds,
                                            node_count=3)
        register_base("init_funcs", ini_funcs, overwrite=True)

        # enter string with mass equations for testing
        int1 = ph.IntegralTerm(
            ph.Product(ph.TemporalDerivedFieldVariable("init_funcs", 2),
                       ph.TestFunction("init_funcs")), spat_domain.bounds)
        s1 = ph.ScalarTerm(
            ph.Product(
                ph.TemporalDerivedFieldVariable("init_funcs", 2, location=0),
                ph.TestFunction("init_funcs", location=0)))
        int2 = ph.IntegralTerm(
            ph.Product(ph.SpatialDerivedFieldVariable("init_funcs", 1),
                       ph.TestFunction("init_funcs", order=1)),
            spat_domain.bounds)
        s2 = ph.ScalarTerm(
            ph.Product(ph.Input(self.u),
                       ph.TestFunction("init_funcs", location=1)), -1)

        string_pde = sim.WeakFormulation([int1, s1, int2, s2])
        self.cf = sim.parse_weak_formulation(string_pde)
        self.ic = np.zeros((3, 2))
Пример #4
0
        def test_rd():
            # trajectory
            bound_cond_type = 'robin'
            actuation_type = 'dirichlet'
            u = tr.RadTrajectory(l, T, param, bound_cond_type, actuation_type)

            # integral terms
            int1 = ph.IntegralTerm(
                ph.Product(
                    ph.TemporalDerivedFieldVariable("init_funcs_2", order=1),
                    ph.TestFunction("init_funcs_2", order=0)), dz.bounds)
            int2 = ph.IntegralTerm(
                ph.Product(
                    ph.SpatialDerivedFieldVariable("init_funcs_2", order=0),
                    ph.TestFunction("init_funcs_2", order=2)), dz.bounds, -a2)
            int3 = ph.IntegralTerm(
                ph.Product(
                    ph.SpatialDerivedFieldVariable("init_funcs_2", order=1),
                    ph.TestFunction("init_funcs_2", order=0)), dz.bounds, -a1)
            int4 = ph.IntegralTerm(
                ph.Product(
                    ph.SpatialDerivedFieldVariable("init_funcs_2", order=0),
                    ph.TestFunction("init_funcs_2", order=0)), dz.bounds, -a0)
            # scalar terms from int 2
            s1 = ph.ScalarTerm(
                ph.Product(
                    ph.SpatialDerivedFieldVariable("init_funcs_2",
                                                   order=1,
                                                   location=l),
                    ph.TestFunction("init_funcs_2", order=0, location=l)), -a2)
            s2 = ph.ScalarTerm(
                ph.Product(
                    ph.SpatialDerivedFieldVariable("init_funcs_2",
                                                   order=0,
                                                   location=0),
                    ph.TestFunction("init_funcs_2", order=0, location=0)),
                a2 * alpha)
            s3 = ph.ScalarTerm(
                ph.Product(
                    ph.SpatialDerivedFieldVariable("init_funcs_2",
                                                   order=0,
                                                   location=0),
                    ph.TestFunction("init_funcs_2", order=1, location=0)), -a2)
            s4 = ph.ScalarTerm(
                ph.Product(
                    ph.Input(u),
                    ph.TestFunction("init_funcs_2", order=1, location=l)), a2)

            # derive state-space system
            rad_pde = sim.WeakFormulation(
                [int1, int2, int3, int4, s1, s2, s3, s4])
            cf = sim.parse_weak_formulation(rad_pde)
            ss = cf.convert_to_state_space()

            # simulate system
            t, q = sim.simulate_state_space(ss, np.zeros(ini_funcs_2.shape),
                                            dt)

            return t, q
Пример #5
0
    def test_FieldVariable_term(self):
        terms = sim.parse_weak_formulation(
            sim.WeakFormulation(self.field_term_at1)).get_terms()
        self.assertTrue(
            np.allclose(terms["E"][0][1],
                        np.array([[0, 0, 1], [0, 0, 1], [0, 0, 1]])))

        terms = sim.parse_weak_formulation(
            sim.WeakFormulation(self.field_term_at1_squared)).get_terms()
        self.assertTrue(
            np.allclose(terms["E"][0][2],
                        np.array([[0, 0, 1], [0, 0, 1], [0, 0, 1]])))

        terms = sim.parse_weak_formulation(sim.WeakFormulation(
            self.field_int)).get_terms()
        self.assertTrue(
            np.allclose(
                terms["E"][0][1],
                np.array([[0.25, 0.5, 0.25], [0.25, 0.5, 0.25], [.25, .5,
                                                                 .25]])))

        terms = sim.parse_weak_formulation(
            sim.WeakFormulation(self.field_squared_int)).get_terms()
        self.assertTrue(
            np.allclose(
                terms["E"][0][2],
                np.array([[1 / 6, 1 / 3, 1 / 6], [1 / 6, 1 / 3, 1 / 6],
                          [1 / 6, 1 / 3, 1 / 6]])))

        terms = sim.parse_weak_formulation(
            sim.WeakFormulation(self.field_term_dz_at1)).get_terms()
        self.assertTrue(
            np.allclose(terms["E"][0][1],
                        np.array([[0, -2, 2], [0, -2, 2], [0, -2, 2]])))

        terms = sim.parse_weak_formulation(
            sim.WeakFormulation(self.field_dz_int)).get_terms()
        self.assertTrue(
            np.allclose(terms["E"][0][1],
                        np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]])))

        terms = sim.parse_weak_formulation(
            sim.WeakFormulation(self.field_term_ddt_at1)).get_terms()
        self.assertTrue(
            np.allclose(terms["E"][2][1],
                        np.array([[0, 0, 1], [0, 0, 1], [0, 0, 1]])))

        terms = sim.parse_weak_formulation(
            sim.WeakFormulation(self.field_ddt_int)).get_terms()
        self.assertTrue(
            np.allclose(
                terms["E"][2][1],
                np.array([[0.25, 0.5, 0.25], [0.25, 0.5, 0.25], [.25, .5,
                                                                 .25]])))
Пример #6
0
        def test_dr():
            # trajectory
            bound_cond_type = 'dirichlet'
            actuation_type = 'robin'
            u = tr.RadTrajectory(l, T, param, bound_cond_type, actuation_type)
            # integral terms
            int1 = ph.IntegralTerm(
                ph.Product(
                    ph.TemporalDerivedFieldVariable("init_funcs_1", order=1),
                    ph.TestFunction("init_funcs_1", order=0)), dz.bounds)
            int2 = ph.IntegralTerm(
                ph.Product(
                    ph.SpatialDerivedFieldVariable("init_funcs_1", order=1),
                    ph.TestFunction("init_funcs_1", order=1)), dz.bounds, a2)
            int3 = ph.IntegralTerm(
                ph.Product(
                    ph.SpatialDerivedFieldVariable("init_funcs_1", order=0),
                    ph.TestFunction("init_funcs_1", order=1)), dz.bounds, a1)
            int4 = ph.IntegralTerm(
                ph.Product(
                    ph.SpatialDerivedFieldVariable("init_funcs_1", order=0),
                    ph.TestFunction("init_funcs_1", order=0)), dz.bounds, -a0)
            # scalar terms from int 2
            s1 = ph.ScalarTerm(
                ph.Product(
                    ph.SpatialDerivedFieldVariable("init_funcs_1",
                                                   order=0,
                                                   location=l),
                    ph.TestFunction("init_funcs_1", order=0, location=l)), -a1)
            s2 = ph.ScalarTerm(
                ph.Product(
                    ph.SpatialDerivedFieldVariable("init_funcs_1",
                                                   order=0,
                                                   location=l),
                    ph.TestFunction("init_funcs_1", order=0, location=l)),
                a2 * beta)
            s3 = ph.ScalarTerm(
                ph.Product(
                    ph.SpatialDerivedFieldVariable("init_funcs_1",
                                                   order=1,
                                                   location=0),
                    ph.TestFunction("init_funcs_1", order=0, location=0)), a2)
            s4 = ph.ScalarTerm(
                ph.Product(
                    ph.Input(u),
                    ph.TestFunction("init_funcs_1", order=0, location=l)), -a2)
            # derive state-space system
            rad_pde = sim.WeakFormulation(
                [int1, int2, int3, int4, s1, s2, s3, s4])
            cf = sim.parse_weak_formulation(rad_pde)
            ss = cf.convert_to_state_space()

            # simulate system
            t, q = sim.simulate_state_space(ss, np.zeros(ini_funcs_1.shape),
                                            dt)

            # check if (x'(0,t_end) - 1.) < 0.1
            self.assertLess(
                np.abs(ini_funcs_1[0].derive(1)(sys.float_info.min) *
                       (q[-1, 0] - q[-1, 1])) - 1, 0.1)
            return t, q
Пример #7
0
    def test_modal(self):
        order = 8

        def char_eq(w):
            return w * (np.sin(w) + self.params.m * w * np.cos(w))

        def phi_k_factory(freq, derivative_order=0):
            def eig_func(z):
                return np.cos(
                    freq * z) - self.params.m * freq * np.sin(freq * z)

            def eig_func_dz(z):
                return -freq * (np.sin(freq * z) +
                                self.params.m * freq * np.cos(freq * z))

            def eig_func_ddz(z):
                return freq**2 * (-np.cos(freq * z) +
                                  self.params.m * freq * np.sin(freq * z))

            if derivative_order == 0:
                return eig_func
            elif derivative_order == 1:
                return eig_func_dz
            elif derivative_order == 2:
                return eig_func_ddz
            else:
                raise ValueError

        # create eigenfunctions
        eig_frequencies = ut.find_roots(char_eq,
                                        n_roots=order,
                                        grid=np.arange(0, 1e3, 2),
                                        rtol=-2)
        print("eigenfrequencies:")
        print(eig_frequencies)

        # create eigen function vectors
        class SWMFunctionVector(cr.ComposedFunctionVector):
            """
            String With Mass Function Vector, necessary due to manipulated scalar product
            """
            @property
            def func(self):
                return self.members["funcs"][0]

            @property
            def scalar(self):
                return self.members["scalars"][0]

        eig_vectors = []
        for n in range(order):
            eig_vectors.append(
                SWMFunctionVector(
                    cr.Function(phi_k_factory(eig_frequencies[n]),
                                derivative_handles=[
                                    phi_k_factory(eig_frequencies[n],
                                                  der_order)
                                    for der_order in range(1, 3)
                                ],
                                domain=self.dz.bounds,
                                nonzero=self.dz.bounds),
                    phi_k_factory(eig_frequencies[n])(0)))

        # normalize eigen vectors
        norm_eig_vectors = [cr.normalize_function(vec) for vec in eig_vectors]
        norm_eig_funcs = np.array([vec.func for vec in norm_eig_vectors])
        register_base("norm_eig_funcs", norm_eig_funcs, overwrite=True)

        norm_eig_funcs[0](1)

        # debug print eigenfunctions
        if 0:
            func_vals = []
            for vec in eig_vectors:
                func_vals.append(np.vectorize(vec.func)(self.dz))

            norm_func_vals = []
            for func in norm_eig_funcs:
                norm_func_vals.append(np.vectorize(func)(self.dz))

            clrs = ["r", "g", "b", "c", "m", "y", "k", "w"]
            for n in range(1, order + 1, len(clrs)):
                pw_phin_k = pg.plot(title="phin_k for k in [{0}, {1}]".format(
                    n, min(n + len(clrs), order)))
                for k in range(len(clrs)):
                    if k + n > order:
                        break
                    pw_phin_k.plot(x=np.array(self.dz),
                                   y=norm_func_vals[n + k - 1],
                                   pen=clrs[k])

            app.exec_()

        # create terms of weak formulation
        terms = [
            ph.IntegralTerm(ph.Product(
                ph.FieldVariable("norm_eig_funcs", order=(2, 0)),
                ph.TestFunction("norm_eig_funcs")),
                            self.dz.bounds,
                            scale=-1),
            ph.ScalarTerm(ph.Product(
                ph.FieldVariable("norm_eig_funcs", order=(2, 0), location=0),
                ph.TestFunction("norm_eig_funcs", location=0)),
                          scale=-1),
            ph.ScalarTerm(
                ph.Product(ph.Input(self.u),
                           ph.TestFunction("norm_eig_funcs", location=1))),
            ph.ScalarTerm(ph.Product(
                ph.FieldVariable("norm_eig_funcs", location=1),
                ph.TestFunction("norm_eig_funcs", order=1, location=1)),
                          scale=-1),
            ph.ScalarTerm(
                ph.Product(
                    ph.FieldVariable("norm_eig_funcs", location=0),
                    ph.TestFunction("norm_eig_funcs", order=1, location=0))),
            ph.IntegralTerm(
                ph.Product(ph.FieldVariable("norm_eig_funcs"),
                           ph.TestFunction("norm_eig_funcs", order=2)),
                self.dz.bounds)
        ]
        modal_pde = sim.WeakFormulation(terms, name="swm_lib-modal")
        eval_data = sim.simulate_system(modal_pde,
                                        self.ic,
                                        self.dt,
                                        self.dz,
                                        der_orders=(2, 0))

        # display results
        if show_plots:
            win = vis.PgAnimatedPlot(eval_data[0:2],
                                     title="modal approx and derivative")
            win2 = vis.PgSurfacePlot(eval_data[0])
            app.exec_()

        # test for correct transition
        self.assertTrue(
            np.isclose(eval_data[0].output_data[-1, 0], self.y_end, atol=1e-3))
Пример #8
0
    def test_fem(self):
        """
        use best documented fem case to test all steps in simulation process
        """

        # enter string with mass equations
        # nodes, ini_funcs = sf.cure_interval(sf.LagrangeFirstOrder,
        nodes, ini_funcs = sf.cure_interval(sf.LagrangeSecondOrder,
                                            self.dz.bounds,
                                            node_count=11)
        register_base("init_funcs", ini_funcs, overwrite=True)
        int1 = ph.IntegralTerm(ph.Product(
            ph.TemporalDerivedFieldVariable("init_funcs", 2),
            ph.TestFunction("init_funcs")),
                               self.dz.bounds,
                               scale=self.params.sigma * self.params.tau**2)
        s1 = ph.ScalarTerm(ph.Product(
            ph.TemporalDerivedFieldVariable("init_funcs", 2, location=0),
            ph.TestFunction("init_funcs", location=0)),
                           scale=self.params.m)
        int2 = ph.IntegralTerm(ph.Product(
            ph.SpatialDerivedFieldVariable("init_funcs", 1),
            ph.TestFunction("init_funcs", order=1)),
                               self.dz.bounds,
                               scale=self.params.sigma)
        s2 = ph.ScalarTerm(
            ph.Product(ph.Input(self.u),
                       ph.TestFunction("init_funcs", location=1)),
            -self.params.sigma)

        # derive sate-space system
        string_pde = sim.WeakFormulation([int1, s1, int2, s2], name="fem_test")
        self.cf = sim.parse_weak_formulation(string_pde)
        ss = self.cf.convert_to_state_space()

        # generate initial conditions for weights
        q0 = np.array([
            cr.project_on_base(self.ic[idx], ini_funcs) for idx in range(2)
        ]).flatten()

        # simulate
        t, q = sim.simulate_state_space(ss, q0, self.dt)

        # calculate result data
        eval_data = []
        for der_idx in range(2):
            eval_data.append(
                sim.evaluate_approximation(
                    "init_funcs",
                    q[:,
                      der_idx * ini_funcs.size:(der_idx + 1) * ini_funcs.size],
                    t, self.dz))
            eval_data[-1].name = "{0}{1}".format(
                self.cf.name, "_" + "".join(["d" for x in range(der_idx)]) +
                "t" if der_idx > 0 else "")

        # display results
        if show_plots:
            win = vis.PgAnimatedPlot(eval_data[:2],
                                     title="fem approx and derivative")
            win2 = vis.PgSurfacePlot(eval_data[0])
            app.exec_()

        # test for correct transition
        self.assertTrue(
            np.isclose(eval_data[0].output_data[-1, 0], self.y_end, atol=1e-3))

        # save some test data for later use
        root_dir = os.getcwd()
        if root_dir.split(os.sep)[-1] == "tests":
            res_dir = os.sep.join([os.getcwd(), "resources"])
        else:
            res_dir = os.sep.join([os.getcwd(), "tests", "resources"])

        if not os.path.isdir(res_dir):
            os.makedirs(res_dir)

        file_path = os.sep.join([res_dir, "test_data.res"])
        with open(file_path, "w+b") as f:
            dump(eval_data, f)
Пример #9
0
 def test_alternating_weights(self):
     self.assertRaises(
         ValueError, sim.parse_weak_formulation,
         sim.WeakFormulation(
             [self.alternating_weights_term, self.field_int]))
Пример #10
0
    def test_Product_term(self):
        # TODO create test functionality that will automatically check if Case is also valid for swapped arguments

        terms = sim.parse_weak_formulation(
            sim.WeakFormulation(self.prod_term_fs_at1)).get_terms()
        self.assertTrue(
            np.allclose(terms["E"][0][1],
                        np.array([[0, 0, 0], [0, 0, 1], [0, 0, 2]])))

        terms = sim.parse_weak_formulation(
            sim.WeakFormulation(self.prod_int_fs)).get_terms()
        self.assertTrue(
            np.allclose(terms["E"][0][1],
                        np.array([[0, 0, 0], [0.25, .5, .25], [.5, 1, .5]])))

        terms = sim.parse_weak_formulation(
            sim.WeakFormulation(self.prod_int_f_f)).get_terms()
        self.assertTrue(
            np.allclose(
                terms["E"][0][1],
                np.array([[1 / 6, 1 / 12, 0], [1 / 12, 1 / 3, 1 / 12],
                          [0, 1 / 12, 1 / 6]])))

        terms = sim.parse_weak_formulation(
            sim.WeakFormulation(self.prod_int_f_squared_f)).get_terms()
        self.assertTrue(
            np.allclose(
                terms["E"][0][2],
                np.array([[1 / 8, 1 / 24, 0], [1 / 24, 1 / 4, 1 / 24],
                          [0, 1 / 24, 1 / 8]])))

        terms = sim.parse_weak_formulation(
            sim.WeakFormulation(self.prod_int_f_f_swapped)).get_terms()
        self.assertTrue(
            np.allclose(
                terms["E"][0][1],
                np.array([[1 / 6, 1 / 12, 0], [1 / 12, 1 / 3, 1 / 12],
                          [0, 1 / 12, 1 / 6]])))

        terms = sim.parse_weak_formulation(
            sim.WeakFormulation(self.prod_int_f_at1_f)).get_terms()
        self.assertTrue(
            np.allclose(terms["E"][0][1],
                        np.array([[0, 0, 0.25], [0, 0, 0.5], [0, 0, .25]])))

        terms = sim.parse_weak_formulation(
            sim.WeakFormulation(self.prod_int_f_at1_squared_f)).get_terms()
        self.assertTrue(
            np.allclose(terms["E"][0][2],
                        np.array([[0, 0, 0.25], [0, 0, 0.5], [0, 0, .25]])))

        terms = sim.parse_weak_formulation(
            sim.WeakFormulation(self.prod_int_f_f_at1)).get_terms()
        self.assertTrue(
            np.allclose(terms["E"][0][1],
                        np.array([[0, 0, 0], [0, 0, 0], [0.25, 0.5, .25]])))

        terms = sim.parse_weak_formulation(
            sim.WeakFormulation(self.prod_int_f_squared_f_at1)).get_terms()
        self.assertTrue(
            np.allclose(
                terms["E"][0][2],
                np.array([[0, 0, 0], [0, 0, 0], [1 / 6, 1 / 3, 1 / 6]])))

        terms = sim.parse_weak_formulation(
            sim.WeakFormulation(self.prod_term_f_at1_f_at1)).get_terms()
        self.assertTrue(
            np.allclose(terms["E"][0][1],
                        np.array([[0, 0, 0], [0, 0, 0], [0, 0, 1]])))

        terms = sim.parse_weak_formulation(
            sim.WeakFormulation(
                self.prod_term_f_at1_squared_f_at1)).get_terms()
        self.assertTrue(
            np.allclose(terms["E"][0][2],
                        np.array([[0, 0, 0], [0, 0, 0], [0, 0, 1]])))

        # more complex terms
        terms = sim.parse_weak_formulation(
            sim.WeakFormulation(self.prod_int_fddt_f)).get_terms()
        self.assertTrue(
            np.allclose(
                terms["E"][2][1],
                np.array([[1 / 6, 1 / 12, 0], [1 / 12, 1 / 3, 1 / 12],
                          [0, 1 / 12, 1 / 6]])))

        terms = sim.parse_weak_formulation(
            sim.WeakFormulation(self.prod_term_fddt_at0_f_at0)).get_terms()
        self.assertTrue(
            np.allclose(terms["E"][2][1],
                        np.array([[1, 0, 0], [0, 0, 0], [0, 0, 0]])))

        terms = sim.parse_weak_formulation(sim.WeakFormulation(
            self.spat_int)).get_terms()
        self.assertTrue(
            np.allclose(terms["E"][0][1],
                        np.array([[2, -2, 0], [-2, 4, -2], [0, -2, 2]])))

        terms = sim.parse_weak_formulation(
            sim.WeakFormulation(self.spat_int_asymmetric)).get_terms()
        self.assertTrue(
            np.allclose(terms["E"][0][1],
                        np.array([[-.5, .5, 0], [-.5, 0, .5], [0, -.5, .5]])))

        terms = sim.parse_weak_formulation(
            sim.WeakFormulation(self.prod_term_f_at1_dphi_at1)).get_terms()
        self.assertTrue(
            np.allclose(terms["E"][0][1],
                        np.array([[0, 0, 0], [0, 0, -2], [0, 0, 2]])))

        terms = sim.parse_weak_formulation(
            sim.WeakFormulation(self.input_term1)).get_terms()
        self.assertTrue(
            np.allclose(terms["G"][0][1], np.array([[0], [0], [1]])))

        terms = sim.parse_weak_formulation(
            sim.WeakFormulation(self.input_term1_swapped)).get_terms()
        self.assertTrue(
            np.allclose(terms["G"][0][1], np.array([[0], [0], [1]])))
Пример #11
0
 def test_TestFunction_term(self):
     terms = sim.parse_weak_formulation(sim.WeakFormulation(
         self.func_term)).get_terms()
     self.assertTrue(np.allclose(terms["f"], np.array([[0], [0], [1]])))