Ejemplo n.º 1
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))
Ejemplo n.º 2
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
Ejemplo n.º 3
0
    def setUp(self):
        self.input = ph.Input(np.sin)
        self.phi = np.array([cr.Function(lambda x: 2 * x)])
        register_base("phi", self.phi, overwrite=True)
        self.test_func = ph.TestFunction("phi")

        nodes, self.ini_funcs = cure_interval(LagrangeFirstOrder, (0, 1),
                                              node_count=2)
        register_base("ini_funcs", self.ini_funcs, overwrite=True)
        self.xdt = ph.TemporalDerivedFieldVariable("ini_funcs", order=1)
        self.xdz_at1 = ph.SpatialDerivedFieldVariable("ini_funcs",
                                                      order=1,
                                                      location=1)

        self.prod = ph.Product(self.input, self.xdt)
Ejemplo n.º 4
0
    def setUp(self):
        interval = (0, 1)
        nodes, funcs = sf.cure_interval(sf.LagrangeFirstOrder, interval, 3)
        register_base("funcs", funcs, overwrite=True)
        x = ph.FieldVariable("funcs")
        x_dt = ph.TemporalDerivedFieldVariable("funcs", 1)
        x_dz = ph.SpatialDerivedFieldVariable("funcs", 1)
        register_base("scal_func", cr.Function(np.exp), overwrite=True)
        exp = ph.ScalarFunction("scal_func")

        alpha = 2
        self.term1 = ph.IntegralTerm(x_dt, interval, 1 + alpha)
        self.term2 = ph.IntegralTerm(x_dz, interval, 2)
        self.term3 = ph.IntegralTerm(ph.Product(x, exp), interval)

        self.weight_label = "funcs"
        self.weights = np.hstack([1, 1, 1, 2, 2, 2])
Ejemplo n.º 5
0
    def setUp(self):
        interval = (0, 1)
        nodes, funcs = sf.cure_interval(sf.LagrangeFirstOrder, interval, 3)
        register_base("funcs", funcs, overwrite=True)
        x_at1 = ph.FieldVariable("funcs", location=1)
        x_dt_at1 = ph.TemporalDerivedFieldVariable("funcs", 1, location=1)
        x_dz_at0 = ph.SpatialDerivedFieldVariable("funcs", 1, location=0)

        exp_func = cr.Function(np.exp)
        register_base("exp_func", exp_func, overwrite=True)
        exp_at1 = ph.ScalarFunction("exp_func", location=1)

        alpha = 2
        self.term1 = ph.ScalarTerm(x_dt_at1, 1 + alpha)
        self.term2 = ph.ScalarTerm(x_dz_at0, 2)
        self.term3 = ph.ScalarTerm(ph.Product(x_at1, exp_at1))

        self.weight_label = "funcs"
        self.weights = np.array([1, 1, 1, 2, 2, 2])
Ejemplo n.º 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
Ejemplo n.º 7
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)
Ejemplo n.º 8
0
    def setUp(self):
        # scalars
        self.scalars = ph.Scalars(np.vstack(list(range(3))))

        # inputs
        self.u = np.sin
        self.input = ph.Input(self.u)
        self.input_squared = ph.Input(self.u, exponent=2)

        nodes, self.ini_funcs = sf.cure_interval(sf.LagrangeFirstOrder, (0, 1),
                                                 node_count=3)

        # TestFunctions
        register_base("ini_funcs", self.ini_funcs, overwrite=True)
        self.phi = ph.TestFunction("ini_funcs")
        self.phi_at0 = ph.TestFunction("ini_funcs", location=0)
        self.phi_at1 = ph.TestFunction("ini_funcs", location=1)
        self.dphi = ph.TestFunction("ini_funcs", order=1)
        self.dphi_at1 = ph.TestFunction("ini_funcs", order=1, location=1)

        # FieldVars
        self.field_var = ph.FieldVariable("ini_funcs")
        self.field_var_squared = ph.FieldVariable("ini_funcs", exponent=2)

        self.odd_weight_field_var = ph.FieldVariable(
            "ini_funcs", weight_label="special_weights")
        self.field_var_at1 = ph.FieldVariable("ini_funcs", location=1)
        self.field_var_at1_squared = ph.FieldVariable("ini_funcs",
                                                      location=1,
                                                      exponent=2)

        self.field_var_dz = ph.SpatialDerivedFieldVariable("ini_funcs", 1)
        self.field_var_dz_at1 = ph.SpatialDerivedFieldVariable("ini_funcs",
                                                               1,
                                                               location=1)

        self.field_var_ddt = ph.TemporalDerivedFieldVariable("ini_funcs", 2)
        self.field_var_ddt_at0 = ph.TemporalDerivedFieldVariable("ini_funcs",
                                                                 2,
                                                                 location=0)
        self.field_var_ddt_at1 = ph.TemporalDerivedFieldVariable("ini_funcs",
                                                                 2,
                                                                 location=1)

        # create all possible kinds of input variables
        self.input_term1 = ph.ScalarTerm(ph.Product(self.phi_at1, self.input))
        self.input_term1_swapped = ph.ScalarTerm(
            ph.Product(self.input, self.phi_at1))
        self.input_term1_squared = ph.ScalarTerm(
            ph.Product(self.input_squared, self.phi_at1))

        self.input_term2 = ph.ScalarTerm(ph.Product(self.dphi_at1, self.input))
        self.func_term = ph.ScalarTerm(self.phi_at1)

        # same goes for field variables
        self.field_term_at1 = ph.ScalarTerm(self.field_var_at1)
        self.field_term_at1_squared = ph.ScalarTerm(self.field_var_at1_squared)
        self.field_term_dz_at1 = ph.ScalarTerm(self.field_var_dz_at1)
        self.field_term_ddt_at1 = ph.ScalarTerm(self.field_var_ddt_at1)

        self.field_int = ph.IntegralTerm(self.field_var, (0, 1))
        self.field_squared_int = ph.IntegralTerm(self.field_var_squared,
                                                 (0, 1))
        self.field_dz_int = ph.IntegralTerm(self.field_var_dz, (0, 1))
        self.field_ddt_int = ph.IntegralTerm(self.field_var_ddt, (0, 1))

        self.prod_term_fs_at1 = ph.ScalarTerm(
            ph.Product(self.field_var_at1, self.scalars))
        self.prod_int_fs = ph.IntegralTerm(
            ph.Product(self.field_var, self.scalars), (0, 1))
        self.prod_int_f_f = ph.IntegralTerm(
            ph.Product(self.field_var, self.phi), (0, 1))
        self.prod_int_f_squared_f = ph.IntegralTerm(
            ph.Product(self.field_var_squared, self.phi), (0, 1))
        self.prod_int_f_f_swapped = ph.IntegralTerm(
            ph.Product(self.phi, self.field_var), (0, 1))

        self.prod_int_f_at1_f = ph.IntegralTerm(
            ph.Product(self.field_var_at1, self.phi), (0, 1))
        self.prod_int_f_at1_squared_f = ph.IntegralTerm(
            ph.Product(self.field_var_at1_squared, self.phi), (0, 1))

        self.prod_int_f_f_at1 = ph.IntegralTerm(
            ph.Product(self.field_var, self.phi_at1), (0, 1))
        self.prod_int_f_squared_f_at1 = ph.IntegralTerm(
            ph.Product(self.field_var_squared, self.phi_at1), (0, 1))

        self.prod_term_f_at1_f_at1 = ph.ScalarTerm(
            ph.Product(self.field_var_at1, self.phi_at1))
        self.prod_term_f_at1_squared_f_at1 = ph.ScalarTerm(
            ph.Product(self.field_var_at1_squared, self.phi_at1))

        self.prod_int_fddt_f = ph.IntegralTerm(
            ph.Product(self.field_var_ddt, self.phi), (0, 1))
        self.prod_term_fddt_at0_f_at0 = ph.ScalarTerm(
            ph.Product(self.field_var_ddt_at0, self.phi_at0))

        self.prod_term_f_at1_dphi_at1 = ph.ScalarTerm(
            ph.Product(self.field_var_at1, self.dphi_at1))

        self.temp_int = ph.IntegralTerm(
            ph.Product(self.field_var_ddt, self.phi), (0, 1))
        self.spat_int = ph.IntegralTerm(
            ph.Product(self.field_var_dz, self.dphi), (0, 1))
        self.spat_int_asymmetric = ph.IntegralTerm(
            ph.Product(self.field_var_dz, self.phi), (0, 1))

        self.alternating_weights_term = ph.IntegralTerm(
            self.odd_weight_field_var, (0, 1))