def test_initialization_from_data(self):
        n_e = 50
        hs = N.ones(n_e) * 1. / n_e
        n_cp = 3

        nlp = NLPCollocationLagrangePolynomials(self.model, n_e, hs, n_cp)
        nlp.set_initial_from_dymola(self.expected, hs, 0, 1)
        nlp.export_result_dymola()
        self.data = ResultDymolaTextual(
            "NominalTests_NominalOptTest2_result.txt")
        self.assert_all_trajectories(['x', 'der(x)', 'u'])
    def test_init(self):

        m = JMUModel(self.jn)

        # Create a new collocation object
        n_e = 30
        coll = NLPCollocationLagrangePolynomials(m, n_e, N.ones(n_e) / n_e, 3)

        # Initialize with optimization result
        coll.set_initial_from_dymola(self.res.result_data, N.array([]), 0, 1)

        # Write initial point to file
        coll.export_result_dymola('Init_res.txt')

        # Load result
        res_init = ResultDymolaTextual('Init_res.txt')

        # Load test fixture
        res_init_fix = ResultDymolaTextual(
            self.curr_dir + '/../files/Results/MinTimeInit_init_fix.txt')

        # Extract trajectories
        dx = res_init.get_variable_data('der(x)')
        dv = res_init.get_variable_data('der(v)')
        x = res_init.get_variable_data('x')
        v = res_init.get_variable_data('v')
        u = res_init.get_variable_data('u')

        dx_fix = res_init_fix.get_variable_data('der(x)')
        dv_fix = res_init_fix.get_variable_data('der(v)')
        x_fix = res_init_fix.get_variable_data('x')
        v_fix = res_init_fix.get_variable_data('v')
        u_fix = res_init_fix.get_variable_data('u')

        # Comparison tests
        N.testing.assert_array_almost_equal(dx_fix.x, dx.x)
        N.testing.assert_array_almost_equal(dv_fix.x, dv.x)
        N.testing.assert_array_almost_equal(x_fix.x, x.x)
        N.testing.assert_array_almost_equal(v_fix.x, v.x)
        N.testing.assert_array_almost_equal(u_fix.x, u.x)

        if False:
            plt.figure(1)
            plt.subplot(2, 1, 1)
            plt.plot(x.t, x.x, 'r')
            plt.hold(True)
            plt.plot(v.t, v.x, 'r')
            plt.grid(True)
            plt.subplot(2, 1, 2)
            plt.plot(u.t, u.x, 'r')
            plt.grid(True)
Beispiel #3
0
    def setUp(self):
        resfile = os.path.join(get_files_path(), 'Results', 
            'BlockingInitPack_M_init_result.txt')
        self.res_init = ResultDymolaTextual(resfile)

        self.n_e = 5 # Number of elements 
        self.hs = N.ones(self.n_e)*1./self.n_e # Equidistant points
        self.n_cp = 3; # Number of collocation points in each element

        # Blocking factors for control parametrization
        blocking_factors=N.ones(self.n_e,dtype=N.int)

        self.nlp = NLPCollocationLagrangePolynomials(self.opt_model,
            self.n_e,self.hs,self.n_cp,blocking_factors)

        self.nlp.set_initial_from_dymola(self.res_init, self.hs, 0., 10.)

        self.nlp.export_result_dymola("qwe.txt")

        self.res_init2 = ResultDymolaTextual('qwe.txt')