Exemple #1
0
    def test_initialize(self):
        """
        Test the method initialize in FMUModelCS2
        """
        self._bounce=load_fmu(CS2, path_to_fmus_cs2, False)
        self._coupledCS2 = load_fmu(CoupledCS2, path_to_fmus_cs2, False)

        for i in range(10):
            self._bounce.initialize(relTol = 10**-i)  #Initialize multiple times with different relTol
            self._bounce.reset_slave()
        self._bounce.initialize()    #Initialize with default options
        self._bounce.reset_slave()

        self._bounce.initialize(tStart = 4.5)
        nose.tools.assert_almost_equal(self._bounce.time, 4.5)
        self._bounce.reset_slave()

        #Try to simulate past the defined stop
        self._coupledCS2.initialize(tStop=1.0 , StopTimeDefined = True)
        step_size=0.1
        total_time=0
        for i in range(10):
            self._coupledCS2.do_step(total_time, step_size)
            total_time += step_size
        status=self._coupledCS2.do_step(total_time, step_size)
        assert status != 0
        self._coupledCS2.reset_slave()

        #Try to initialize twice when not supported
        self._coupledCS2.initialize()
        nose.tools.assert_raises(FMUException, self._coupledCS2.initialize)
Exemple #2
0
    def test_correct_loading(self):

        model = load_fmu("Modelica_Mechanics_Rotational_Examples_CoupledClutches_ME.fmu",path_to_fmus_me1)
        assert isinstance(model, FMUModelME1)

        model = load_fmu("Modelica_Mechanics_Rotational_Examples_CoupledClutches_CS.fmu",path_to_fmus_cs1)
        assert isinstance(model, FMUModelCS1)
Exemple #3
0
    def test_get_directional_derivative(self):
        """
        Test the method get_directional_derivative in FMUModelME2
        """
        bounce = load_fmu(ME2, path_to_fmus_me2, False)

        coupled = load_fmu(CoupledME2, path_to_fmus_me2)

        bounce.initialize()
        coupled.initialize(tolControlled=False)

        nose.tools.assert_raises(FMUException, bounce.get_directional_derivative, [1], [1], [1])
        nose.tools.assert_raises(FMUException, coupled.get_directional_derivative, [1], [1], [1,2])

        states_list = coupled.get_states_list()
        der_list    = coupled.get_derivatives_list()
        states_ref  = [s.value_reference for s in states_list.values()]
        der_ref     = [s.value_reference for s in der_list.values()]

        nose.tools.assert_raises(FMUException, coupled.get_directional_derivative, [1], [der_ref[0]], [1])

        dir_der = coupled.get_directional_derivative(states_ref, der_ref, [1]*len(states_ref))
        assert len(dir_der) == len(der_list)
        nose.tools.assert_almost_equal(dir_der[1], 0)
        nose.tools.assert_almost_equal(dir_der[2], 1.000000)

        dir_der2 = coupled.get_directional_derivative(states_ref, der_ref, [2]*len(states_ref))
        assert len(dir_der) == len(der_list)
        diff = dir_der2 - 2*dir_der
        nose.tools.assert_almost_equal(sum(diff), 0)
Exemple #4
0
    def test_get_derivatives(self):
        """
        Test the method get_derivatives in FMUModelME2
        """
        bounce = load_fmu(ME2, path_to_fmus_me2, False)
        coupled = load_fmu(CoupledME2, path_to_fmus_me2)

        bounce.initialize()
        coupled.initialize(tolControlled=False)

        nx = bounce.get_ode_sizes()[0]
        der=bounce.get_derivatives()
        assert nx == len(der)

        nose.tools.assert_almost_equal(der[0], 4.000000)
        nose.tools.assert_almost_equal(der[1], -9.810000)

        bounce.set_real(1, 2.)
        bounce.set_real(2, -5.)
        der=bounce.get_derivatives()

        nose.tools.assert_almost_equal(der[0], 2.000000)
        nose.tools.assert_almost_equal(der[1], -5.000000)

        der_list = coupled.get_derivatives_list()
        der_ref  = N.array([s.value_reference for s in der_list.values()])
        der = coupled.get_derivatives()
        diff = N.sort(N.array([coupled.get_real(i) for i in der_ref]))-N.sort(der)
        nose.tools.assert_almost_equal(N.sum(diff), 0.)
    def test_default_simulation(self):
        """
        This test the default values of the simulation using simulate.
        """
        #Writing continuous
        bounce = load_fmu('bouncingBall.fmu', path_to_fmus_me1)
        opts = bounce.simulate_options()
        opts["CVode_options"]["rtol"] = 1e-4
        opts["CVode_options"]["atol"] = 1e-6
        res = bounce.simulate(final_time=3., options=opts)

        nose.tools.assert_almost_equal(res.solver.rtol, 1e-4, 6)
        assert res.solver.iter == 'Newton'
        
        nose.tools.assert_almost_equal(res.initial('h'),1.000000,5)
        nose.tools.assert_almost_equal(res.final('h'),-0.9804523,5)
        nose.tools.assert_almost_equal(res.final('time'),3.000000,5)
        
        #Writing continuous
        bounce = load_fmu('bouncingBall.fmu', path_to_fmus_me1)
        #bounce.initialize(options={'initialize':False})
        res = bounce.simulate(final_time=3.,
            options={'initialize':True,'CVode_options':{'iter':'FixedPoint','rtol':1e-6,'atol':1e-6}})
    
        nose.tools.assert_almost_equal(res.solver.rtol, 0.00000100, 7)
        assert res.solver.iter == 'FixedPoint'
        
        nose.tools.assert_almost_equal(res.initial('h'),1.000000,5)
        nose.tools.assert_almost_equal(res.final('h'),-0.98018113,5)
        nose.tools.assert_almost_equal(res.final('time'),3.000000,5)
Exemple #6
0
 def test_log_file_name(self):
     model = load_fmu("bouncingBall.fmu",path_to_fmus_me1)
     assert os.path.exists("bouncingBall_log.txt")
     model = load_fmu("bouncingBall.fmu",path_to_fmus_me1,log_file_name="Test_log.txt")
     assert os.path.exists("Test_log.txt")
     model = FMUModelME1("bouncingBall.fmu",path_to_fmus_me1)
     assert os.path.exists("bouncingBall_log.txt")
     model = FMUModelME1("bouncingBall.fmu",path_to_fmus_me1,log_file_name="Test_log.txt")
     assert os.path.exists("Test_log.txt")
Exemple #7
0
 def test_log_file_name(self):
     model = load_fmu("bouncingBall.fmu",os.path.join(path_to_fmus,"CS1.0"))
     assert os.path.exists("bouncingBall_log.txt")
     model = load_fmu("bouncingBall.fmu",os.path.join(path_to_fmus,"CS1.0"),log_file_name="Test_log.txt")
     assert os.path.exists("Test_log.txt")
     model = FMUModelCS1("bouncingBall.fmu",os.path.join(path_to_fmus,"CS1.0"))
     assert os.path.exists("bouncingBall_log.txt")
     model = FMUModelCS1("bouncingBall.fmu",os.path.join(path_to_fmus,"CS1.0"),log_file_name="Test_log.txt")
     assert os.path.exists("Test_log.txt")
Exemple #8
0
    def test_multiple_loadings_and_simulations(self):
        model = load_fmu("bouncingBall.fmu",path_to_fmus_me1,enable_logging=False)
        res = model.simulate(final_time=1.0)
        h_res = res.final('h')

        for i in range(40):
            model = load_fmu("bouncingBall.fmu",path_to_fmus_me1,enable_logging=False)
            res = model.simulate(final_time=1.0)
        assert N.abs(h_res - res.final('h')) < 1e-4
 def setUp(self):
     """
     Load the test model.
     """
     self._bounce  = load_fmu('bouncingBall.fmu',path_to_fmus_me1)
     self._dq = load_fmu('dq.fmu',path_to_fmus_me1)
     self._bounce.initialize()
     self._dq.initialize()
     self._bounceSim = FMIODE(self._bounce)
     self._dqSim     = FMIODE(self._dq)
Exemple #10
0
    def test_init(self):
        """
        Test the method __init__ in FMUModelME2
        """
        bounce = load_fmu(ME2, path_to_fmus_me2, False)
        coupled = load_fmu(CoupledME2, path_to_fmus_me2)

        assert bounce.get_identifier() == 'BouncingBall2'
        nose.tools.assert_raises(FMUException, FMUModelME2, fmu=CS2, path=path_to_fmus_cs2, enable_logging=False)
        nose.tools.assert_raises(FMUException, FMUModelME2, fmu=CS1, path=path_to_fmus_cs1, enable_logging=False)
        nose.tools.assert_raises(FMUException, FMUModelME2, fmu=ME1, path=path_to_fmus_me1, enable_logging=False)
Exemple #11
0
 def setUp(self):
     """
     Sets up the test case.
     """
     self.rlc  = load_fmu('RLC_Circuit.fmu')
     self._bounce  = load_fmu('bouncingBall.fmu',path_to_fmus_me1)
     self._dq = load_fmu('dq.fmu',path_to_fmus_me1)
     self._bounce.initialize()
     self._dq.initialize()
     self.dep = load_fmu("DepParTests_DepPar1.fmu")
     self.dep.initialize()
Exemple #12
0
    def test_instantiate_model(self):
        """
        Test the method instantiate_model in FMUModelME2
        """
        bounce = load_fmu(ME2, path_to_fmus_me2, False)
        coupled = load_fmu(CoupledME2, path_to_fmus_me2)

        for i in range(5):
            name1 = 'model1' + str(i)
            #name2 = 'model2' + str(i)
            #coupled.instantiate_model(name=name2)
            bounce.instantiate_model(name=name1)
Exemple #13
0
    def test_reset_slave(self):
        """
        Test the method reset_slave in FMUModelCS2
        """
        self._bounce=load_fmu(CS2, path_to_fmus_cs2, False)
        self._bounce.initialize()
        self._coupledCS2 = load_fmu(CoupledCS2, path_to_fmus_cs2, False)
        self._coupledCS2.initialize()

        self._bounce.reset_slave()
        self._bounce.initialize()
        self._coupledCS2.reset_slave()
        self._coupledCS2.initialize()
Exemple #14
0
    def test_reset(self):
        """
        Test the method reset in FMUModelME2
        """
        bounce = load_fmu(ME2, path_to_fmus_me2, False)
        coupled = load_fmu(CoupledME2, path_to_fmus_me2)

        bounce.initialize()
        coupled.initialize(tolControlled=False)

        bounce.reset()
        coupled.reset()

        assert bounce.time is None
        assert coupled.time is None
Exemple #15
0
    def test_simulate_options(self):
        """
        Test the method simultaion_options in FMUModelCS2
        """
        #Do the setUp
        self._coupledCS2 = load_fmu(CoupledCS2, path_to_fmus_cs2, False)

        #Test the result file
        res=self._coupledCS2.simulate()
        assert res.result_file == 'Modelica_Mechanics_Rotational_Examples_CoupledClutches_result.txt'
        assert os.path.exists(res.result_file)

        self._coupledCS2.reset_slave()
        opts = {'result_file_name':'Modelica_Mechanics_Rotational_Examples_CoupledClutches_result_test.txt'}
        res=self._coupledCS2.simulate(options=opts)
        assert res.result_file == 'Modelica_Mechanics_Rotational_Examples_CoupledClutches_result_test.txt'
        assert os.path.exists(res.result_file)

        #Test the option in the simulate method
        self._coupledCS2.reset_slave()
        opts={}
        opts['ncp'] = 250
        opts['initialize'] = False
        self._coupledCS2.initialize()
        res=self._coupledCS2.simulate(options=opts)
        assert len(res['time']) == 251
Exemple #16
0
    def test_initialize(self):
        """
        Test the method initialize in FMUModelME2
        """
        bounce = load_fmu(ME2, path_to_fmus_me2, False)
        coupled = load_fmu(CoupledME2, path_to_fmus_me2)

        coupled.initialize(tolControlled=False)
        nose.tools.assert_almost_equal(coupled.time, 0.0)
        nose.tools.assert_raises(FMUException, coupled.initialize, tolControlled=False) #Cant initialize twice in a row

        bounce.initialize()
        nose.tools.assert_almost_equal(bounce.time, 0.0)

        bounce.reset()
        bounce.initialize(relativeTolerance=1e-7)
Exemple #17
0
 def test_terminate(self):
     """
     Test the method terminate in FMUModelME2
     """
     coupled = load_fmu(CoupledME2, path_to_fmus_me2)
     coupled.initialize(tolControlled=False)
     coupled.terminate()
 def test_relation_geinit(self):
     model = load_fmu("RelationTests_RelationGEInit.fmu")
     
     res = model.simulate(final_time=0.1)
     
     nose.tools.assert_almost_equal(res.initial("x"),0.0,places=3)
     nose.tools.assert_almost_equal(res.initial("y"),1.0,places=3)
Exemple #19
0
    def test_simulate_options(self):
        """
        Test the method simulate_options in FMUModelME2
        """
        coupled = load_fmu(CoupledME2, path_to_fmus_me2)

        opts=coupled.simulate_options()
        assert opts['initialize']
        assert not opts['with_jacobian']
        assert opts['ncp'] == 0

        #Test the result file
        res=coupled.simulate()
        assert res.result_file == 'Modelica_Mechanics_Rotational_Examples_CoupledClutches_result.txt'
        assert os.path.exists(res.result_file)

        coupled.reset()
        opts = {'result_file_name':'Modelica_Mechanics_Rotational_Examples_CoupledClutches_result_test.txt'}
        res=coupled.simulate(options=opts)
        assert res.result_file == 'Modelica_Mechanics_Rotational_Examples_CoupledClutches_result_test.txt'
        assert os.path.exists(res.result_file)

        #Test the option in the simulate method
        coupled.reset()
        opts={}
        opts['ncp'] = 250
        opts['initialize'] = False
        coupled.initialize(tolControlled=False)
        res=coupled.simulate(options=opts)
        assert len(res['time']) > 250
    def test_event_iteration(self):
        """
        This tests FMUs with event iteration (JModelica.org).
        """
        fmu_name = compile_fmu('EventIter.EventMiddleIter', os.path.join(path_to_mos,'EventIter.mo'))

        model = load_fmu(fmu_name)

        sim_res = model.simulate(final_time=10)

        nose.tools.assert_almost_equal(sim_res.initial('x'), 2.00000, 4)
        nose.tools.assert_almost_equal(sim_res.final('x'), 10.000000, 4)
        nose.tools.assert_almost_equal(sim_res.final('y'), 3.0000000, 4)
        nose.tools.assert_almost_equal(sim_res.final('z'), 2.0000000, 4)
        
        fmu_name = compile_fmu('EventIter.EventStartIter', os.path.join(path_to_mos,'EventIter.mo'))
        
        model = FMUModel(fmu_name)

        sim_res = model.simulate(final_time=10)

        nose.tools.assert_almost_equal(sim_res.initial('x'), 1.00000, 4)
        nose.tools.assert_almost_equal(sim_res.initial('y'), -1.00000, 4)
        nose.tools.assert_almost_equal(sim_res.initial('z'), 1.00000, 4)
        nose.tools.assert_almost_equal(sim_res.final('x'), -2.000000, 4)
        nose.tools.assert_almost_equal(sim_res.final('y'), -1.0000000, 4)
        nose.tools.assert_almost_equal(sim_res.final('z'), 4.0000000, 4)
Exemple #21
0
 def test_check_against_unneccesary_derivatives_eval(self):
     name = compile_fmu("RLC_Circuit",os.path.join(path_to_mofiles,"RLC_Circuit.mo"), compiler_options={"generate_html_diagnostics":True, "log_level":6})
     
     model = load_fmu(name, log_level=6)
     model.set("_log_level", 6)
     model.initialize()
     
     len_log = len(model.get_log())
     model.time = 1e-4
     model.get_derivatives()
     assert len(model.get_log()) > len_log
     len_log = len(model.get_log())
     model.get_derivatives()
     len_log_diff = len(model.get_log())-len_log
     model.time = 1e-4
     len_log = len(model.get_log())
     model.get_derivatives()
     assert len(model.get_log())-len_log_diff == len_log
     len_log = len(model.get_log())
     model.continuous_states = model.continuous_states
     model.get_derivatives()
     assert len(model.get_log())-len_log_diff == len_log
     len_log = len(model.get_log())
     model.continuous_states = model.continuous_states+1
     model.get_derivatives()
     assert len(model.get_log())-len_log_diff > len_log
    def test_reset(self):
        """
        Test resetting an FMU. (Multiple instances is NOT supported on Dymola
        FMUs)
        """
        #Writing continuous
        bounce = load_fmu('bouncingBall.fmu', path_to_fmus_me1)
        opts = bounce.simulate_options()
        opts["CVode_options"]["rtol"] = 1e-4
        opts["CVode_options"]["atol"] = 1e-6
        #bounce.initialize()
        res = bounce.simulate(final_time=3., options=opts)
        
        nose.tools.assert_almost_equal(res.initial('h'),1.000000,5)
        nose.tools.assert_almost_equal(res.final('h'),-0.9804523,5)
        
        bounce.reset()
        #bounce.initialize()
        
        nose.tools.assert_almost_equal(bounce.get('h'), 1.00000,5)
        
        res = bounce.simulate(final_time=3.,options=opts)

        nose.tools.assert_almost_equal(res.initial('h'),1.000000,5)
        nose.tools.assert_almost_equal(res.final('h'),-0.9804523,5)
 def test_cc_with_dopri(self):
     model = load_fmu("Modelica_Mechanics_Rotational_Examples_CoupledClutches.fmu")
     opts = model.simulate_options()
     opts["solver"] = "Dopri5"
     
     res = model.simulate(final_time=1.5,options=opts)
     
     assert (N.abs(res.final("J1.w") - 3.2450903041811698)) < 1e-3
 def test_singular_system_event_1(self):
     model = load_fmu("EventIter_SingularSystem1.fmu")
     
     #Check that we can initialize without error!
     model.initialize()
     
     nose.tools.assert_almost_equal(model.get("mode"), 0.0)
     nose.tools.assert_almost_equal(model.get("sa"), 0.0)
Exemple #25
0
 def setUp(self):
     """
     Sets up the test case.
     """
     self.m = load_fmu('LoggerTest.fmu')
     self.m.set_debug_logging(True)
     self.m.set('_log_level',6)
     self.m.set_log_level(5)
Exemple #26
0
    def test_simulation_with_reset_cs(self):

        model = load_fmu("Modelica_Mechanics_Rotational_Examples_CoupledClutches_CS.fmu",path_to_fmus_cs1)
        res1 = model.simulate(final_time=1.5)
        assert (res1["J1.w"][-1] - 3.245091100366517) < 1e-4
        model.reset()
        res2 = model.simulate(final_time=1.5)
        assert (res2["J1.w"][-1] - 3.245091100366517) < 1e-4
Exemple #27
0
    def test_time(self):
        """
        Test the method get/set_time in FMUModelME2
        """
        bounce = load_fmu(ME2, path_to_fmus_me2, False)
        coupled = load_fmu(CoupledME2, path_to_fmus_me2)

        coupled.reset()
        assert coupled.time is None
        coupled.initialize(tolControlled=False)
        nose.tools.assert_almost_equal(coupled._get_time(), 0.0)
        coupled._set_time(2.71)
        nose.tools.assert_almost_equal(coupled.time , 2.71)
        coupled._set_time(1.00)
        nose.tools.assert_almost_equal(coupled._get_time() , 1.00)

        nose.tools.assert_raises(TypeError, coupled._set_time, '2.0')
        nose.tools.assert_raises(TypeError, coupled._set_time, N.array([1.0, 1.0]))
Exemple #28
0
    def test_get_event_indicators(self):
        """
        Test the method get_event_indicators in FMUModelME2
        """
        bounce = load_fmu(ME2, path_to_fmus_me2, False)

        coupled = load_fmu(CoupledME2, path_to_fmus_me2)
        bounce.initialize()
        coupled.initialize(tolControlled=False)

        assert len(bounce.get_event_indicators()) == 1
        assert len(coupled.get_event_indicators()) == 54

        event_ind = bounce.get_event_indicators()
        nose.tools.assert_almost_equal(event_ind[0],1.000000)
        bounce.continuous_states = N.array([5.]*2)
        event_ind = bounce.get_event_indicators()
        nose.tools.assert_almost_equal(event_ind[0],5.000000)
Exemple #29
0
    def test_get_tolerances(self):
        """
        Test the method get_tolerances in FMUModelME2
        """
        bounce = load_fmu(ME2, path_to_fmus_me2, False)
        coupled = load_fmu(CoupledME2, path_to_fmus_me2)
        bounce.initialize()
        coupled.initialize(tolControlled=False)

        [rtol,atol] = bounce.get_tolerances()

        assert rtol == 0.0001
        nose.tools.assert_almost_equal(atol[0],0.0000010)
        nose.tools.assert_almost_equal(atol[1],0.0000010)

        [rtol,atol] = coupled.get_tolerances()

        assert rtol == 0.0001
        nose.tools.assert_almost_equal(atol[0],0.0000010)
Exemple #30
0
    def test_init(self):
        """
        Test the method __init__ in FMUModelCS2
        """
        self._bounce=load_fmu(CS2, path_to_fmus_cs2, False)

        assert self._bounce.get_identifier() == 'BouncingBall2'
        nose.tools.assert_raises(FMUException, FMUModelCS2, fmu=ME2, path=path_to_fmus_me2)
        nose.tools.assert_raises(FMUException, FMUModelCS2, fmu=CS1, path=path_to_fmus_cs1)
        nose.tools.assert_raises(FMUException, FMUModelCS2, fmu=ME1, path=path_to_fmus_me1)
Exemple #31
0
 def setUp(self):
     """
     Sets up the test case.
     """
     self._model = load_fmu(Test_FMI_Compile.fmuname)
Exemple #32
0
 def test_model_types_platfrom(self):
     dep = load_fmu(Test_FMUModelME1.depPar1)
     assert dep.model_types_platform == "standard32"
Exemple #33
0
 def test_reinit_writeback(self):
     model = load_fmu("Reinit_ReinitWriteback.fmu")
     model.simulate(start_time=0, final_time=21)
Exemple #34
0
    def test_simulate(self):
        """
        Test the main features of the method simulate() in FMUmodelCS2
        """
        #Set up for simulation
        self._bounce = load_fmu(CS2, path_to_fmus_cs2, False)
        self._coupledCS2 = load_fmu(CoupledCS2, path_to_fmus_cs2, False)

        #Try simulate the bouncing ball
        res = self._bounce.simulate()
        sim_time = res['time']
        nose.tools.assert_almost_equal(sim_time[0], 0.0)
        nose.tools.assert_almost_equal(sim_time[-1], 1.0)
        self._bounce.reset_slave()

        for i in range(5):
            res = self._bounce.simulate(start_time=0.1,
                                        final_time=1.0,
                                        options={'ncp': 500})
            sim_time = res['time']
            nose.tools.assert_almost_equal(sim_time[0], 0.1)
            nose.tools.assert_almost_equal(sim_time[-1], 1.0)
            assert sim_time.all(
            ) >= sim_time[0] - 1e-4  #Check that the time is increasing
            assert sim_time.all(
            ) <= sim_time[-1] + 1e+4  #Give it some marginal
            height = res['HIGHT']
            assert height.all(
            ) >= -1e-4  #The height of the ball should be non-negative
            nose.tools.assert_almost_equal(res.final('HIGHT'), 0.63489609999,
                                           4)
            if i > 0:  #check that the results stays the same
                diff = height_old - height
                nose.tools.assert_almost_equal(diff[-1], 0.0)
            height_old = height
            self._bounce.reset_slave()

        #Try to simulate the coupled-clutches
        res_coupled = self._coupledCS2.simulate()
        sim_time_coupled = res_coupled['time']
        nose.tools.assert_almost_equal(sim_time_coupled[0], 0.0)
        nose.tools.assert_almost_equal(sim_time_coupled[-1], 1.0)
        self._coupledCS2.reset_slave()

        for i in range(10):
            self._coupledCS2 = load_fmu(CoupledCS2, path_to_fmus_cs2, False)
            res_coupled = self._coupledCS2.simulate(start_time=0.0,
                                                    final_time=2.0)
            sim_time_coupled = res_coupled['time']
            nose.tools.assert_almost_equal(sim_time_coupled[0], 0.0)
            nose.tools.assert_almost_equal(sim_time_coupled[-1], 2.0)
            assert sim_time_coupled.all(
            ) >= sim_time_coupled[0] - 1e-4  #Check that the time is increasing
            assert sim_time_coupled.all(
            ) <= sim_time_coupled[-1] + 1e+4  #Give it some marginal

            #val_J1 = res_coupled['J1.w']
            #val_J2 = res_coupled['J2.w']
            #val_J3 = res_coupled['J3.w']
            #val_J4 = res_coupled['J4.w']

            val = [
                res_coupled.final('J1.w'),
                res_coupled.final('J2.w'),
                res_coupled.final('J3.w'),
                res_coupled.final('J4.w')
            ]
            if i > 0:  #check that the results stays the same
                for j in range(len(val)):
                    nose.tools.assert_almost_equal(val[j], val_old[j])
            val_old = val
            self._coupledCS2.reset_slave()

        #Compare to something we know is correct
        cs1_model = load_fmu(
            'Modelica_Mechanics_Rotational_Examples_CoupledClutches_CS.fmu',
            path_to_fmus_cs1, False)
        res1 = cs1_model.simulate(final_time=10,
                                  options={'result_file_name': 'result1'})
        self._coupledCS2 = load_fmu(CoupledCS2, path_to_fmus_cs2, False)
        res2 = self._coupledCS2.simulate(
            final_time=10, options={'result_file_name': 'result2'})
        diff1 = res1.final("J1.w") - res2.final("J1.w")
        diff2 = res1.final("J2.w") - res2.final("J2.w")
        diff3 = res1.final("J3.w") - res2.final("J3.w")
        diff4 = res1.final("J4.w") - res2.final("J4.w")
        nose.tools.assert_almost_equal(abs(diff1), 0.000, 1)
        nose.tools.assert_almost_equal(abs(diff2), 0.000, 1)
        nose.tools.assert_almost_equal(abs(diff3), 0.000, 1)
        nose.tools.assert_almost_equal(abs(diff4), 0.000, 1)
Exemple #35
0
 def setUp(self):
     """
     Sets up the test case.
     """
     self.m = load_fmu(Test_RaisesIfNonConverge.fmu)
Exemple #36
0
    def test_simulate(self):
        """
        Test the main features of the method simulate() in FMUmodelCS2
        """
        #Set up for simulation
        bounce = load_fmu(self.bouncing_name)
        coupled = load_fmu(self.coupled_name)

        #Try simulate the bouncing ball
        res = bounce.simulate()
        sim_time = res['time']
        nose.tools.assert_almost_equal(sim_time[0], 0.0)
        nose.tools.assert_almost_equal(sim_time[-1], 1.0)
        bounce.reset()

        for i in range(5):
            res = bounce.simulate(start_time=0.1,
                                  final_time=1.0,
                                  options={'ncp': 500})
            sim_time = res['time']
            nose.tools.assert_almost_equal(sim_time[0], 0.1)
            nose.tools.assert_almost_equal(sim_time[-1], 1.0)
            assert sim_time.all(
            ) >= sim_time[0] - 1e-4  #Check that the time is increasing
            assert sim_time.all(
            ) <= sim_time[-1] + 1e-4  #Give it some marginal
            height = res['h']
            assert height.all(
            ) >= -1e-4  #The height of the ball should be non-negative
            nose.tools.assert_almost_equal(res.final('h'), 6.0228998448008104,
                                           4)
            if i > 0:  #check that the results stays the same
                diff = height_old - height
                nose.tools.assert_almost_equal(diff[-1], 0.0)
            height_old = height
            bounce.reset()

        #Try to simulate the coupled-clutches
        res_coupled = coupled.simulate()
        sim_time_coupled = res_coupled['time']
        nose.tools.assert_almost_equal(sim_time_coupled[0], 0.0)
        nose.tools.assert_almost_equal(sim_time_coupled[-1], 1.5)
        coupled.reset()

        for i in range(10):
            coupled = load_fmu(self.coupled_name)
            res_coupled = coupled.simulate(start_time=0.0, final_time=2.0)
            sim_time_coupled = res_coupled['time']
            nose.tools.assert_almost_equal(sim_time_coupled[0], 0.0)
            nose.tools.assert_almost_equal(sim_time_coupled[-1], 2.0)
            assert sim_time_coupled.all(
            ) >= sim_time_coupled[0] - 1e-4  #Check that the time is increasing
            assert sim_time_coupled.all(
            ) <= sim_time_coupled[-1] + 1e-4  #Give it some marginal

            #val_J1 = res_coupled['J1.w']
            #val_J2 = res_coupled['J2.w']
            #val_J3 = res_coupled['J3.w']
            #val_J4 = res_coupled['J4.w']

            val = [
                res_coupled.final('J1.w'),
                res_coupled.final('J2.w'),
                res_coupled.final('J3.w'),
                res_coupled.final('J4.w')
            ]
            if i > 0:  #check that the results stays the same
                for j in range(len(val)):
                    nose.tools.assert_almost_equal(val[j], val_old[j])
            val_old = val
            coupled.reset()
        """
Exemple #37
0
 def test_version(self):
     """
     This tests the (get)-property of version.
     """
     rlc = load_fmu(Test_FMUModelCS1.rlc_circuit)
     assert rlc._get_version() == '1.0'
Exemple #38
0
    def test_asseert_fail(self):
        model = load_fmu(Test_FMUModelCS1.assert_fail)

        nose.tools.assert_raises(Exception, model.simulate)
Exemple #39
0
 def test_valid_platforms(self):
     """
     This tests the (get)-property of types platform
     """
     rlc = load_fmu('RLC_Circuit.fmu')
     assert rlc._get_types_platform() == 'standard32'
Exemple #40
0
    def test_unknown_solver(self):
        rlc = load_fmu(Test_FMUModelCS1.rlc_circuit)
        rlc.set("_cs_solver", 2)  #Does not exists

        nose.tools.assert_raises(FMUException, rlc.simulate)
Exemple #41
0
    def test_version(self):
        negated_alias = load_fmu(Test_FMUModelBase.negAliasFmu)

        assert negated_alias.get_version() == "1.0"
Exemple #42
0
 def test_instantiate_model(self):
     """
     Test the method instantiate_model in FMUModelME2
     """
     for i in range(5):
         bounce = load_fmu(self.bouncing_name)
Exemple #43
0
 def setUp(self):
     """
     Sets up the test case.
     """
     self.m = load_fmu(self.fmu)
Exemple #44
0
    def test_version(self):
        bounce = load_fmu(self.bouncing_name)
        assert bounce.get_version() == "2.0"

        coupled = load_fmu(self.coupled_name)
        assert coupled.get_version() == "2.0"
Exemple #45
0
 def test_initialize_once(self):
     negated_alias = load_fmu(Test_FMUModelBase.negAliasFmu)
     negated_alias.initialize()
     nose.tools.assert_raises(FMUException, negated_alias.initialize)
 def test_event_infinite_iteration_2(self):
     model = load_fmu("EventIter_EventInfiniteIteration2.fmu")
     nose.tools.assert_raises(FMUException, model.initialize)
def main():
    model_path = get_model_path()

    # 1. Solve the initialization problem
    # Compile the stationary initialization model into an FMU
    init_fmu = compile_fmu("TanksPkg.ThreeTanksInit", model_path)
    init_model = load_fmu(init_fmu)

    # Set input for Stationary point A
    u_0_a = 0
    init_model.set('u', u_0_a)
    # Solve the initialization problem using FMI
    init_model.initialize()
    # Store stationary point A
    [h1_0_a, h2_0_a, h3_0_a] = init_model.get(['h1', 'h2', 'h3'])
    # Print some data for stationary point A
    print_stationary_point('A', h1_0_a, h2_0_a, h3_0_a, u_0_a)

    # Set inputs for Stationary point B
    init_model.reset()  # reset the FMU so that we can initialize it again
    u_0_b = 88.0
    init_model.set('u', u_0_b)
    # Solve the initialization problem using FMI
    init_model.initialize()
    # Store stationary point B
    [h1_0_b, h2_0_b, h3_0_b] = init_model.get(['h1', 'h2', 'h3'])
    # Print some data for stationary point B
    print_stationary_point('B', h1_0_b, h2_0_b, h3_0_b, u_0_b)

    init_res = simulate_tanks(model_path,
                              u=u_0_b,
                              with_plots=True,
                              with_full_traj_obj=True)

    h1_sim = init_res['h1']
    h2_sim = init_res['h2']
    h3_sim = init_res['h3']

    h1_sim_final = h1_sim[-1]
    h2_sim_final = h2_sim[-1]
    h3_sim_final = h3_sim[-1]

    print("Final values of the simulation:", h1_sim_final, h2_sim_final,
          h3_sim_final)

    # 3. Solve the optimal control problem
    # Compile and load optimization problem
    op = transfer_optimization_problem("TanksPkg.three_tanks_time_optimal",
                                       model_path)

    # Set initial values
    # op.set('h1_final', h1_sim_final)
    # op.set('h2_final', h2_sim_final)
    # op.set('h3_final', h3_sim_final)
    op.set('h1_final', float(h1_0_b))
    op.set('h2_final', float(h2_0_b))
    op.set('h3_final', float(h3_0_b))
    op.set('u_max', U_MAX)

    # Set options
    opt_opts = op.optimize_options()
    # opt_opts['n_e'] = 80  # Number of elements
    opt_opts['variable_scaling'] = False
    opt_opts['init_traj'] = init_res
    # opt_opts['nominal_traj'] = init_res
    opt_opts['IPOPT_options']['tol'] = 1e-3
    opt_opts['verbosity'] = 1
    print("OPTIMISATION OPTIONS:")
    for option, value in opt_opts.items():
        print('\t', option, ':', value)

    # Solve the optimal control problem
    res = op.optimize(options=opt_opts)

    # Extract variable profiles
    h1_res = res['h1']
    h2_res = res['h2']
    h3_res = res['h3']
    u_res = res['u']
    time_res = res['time']
    print("Final value of time:", time_res[-1])
    print(dir(res))

    # Plot the results
    plot_results(h1_res,
                 h2_res,
                 h3_res,
                 time_res,
                 u_res,
                 title="Trajektorie optymalne")

    normalised_u = []
    for val in u_res:
        if val < 50:
            normalised_u.append(0)
        else:
            normalised_u.append(U_MAX)
    for i in range(10):
        try:
            sim_result = simulate_tanks(model_path,
                                        u=normalised_u,
                                        t_final=time_res[-1],
                                        with_plots=True)
            plot_with_optimal_trajectories(time_res, sim_result['time'],
                                           sim_result["h1"], sim_result["h2"],
                                           sim_result["h3"], h1_res, h2_res,
                                           h3_res, normalised_u)
            break
        except FMUException:
            pass
Exemple #48
0
    def test_simulate(self):
        """
        Test the method simulate in FMUModelME2
        """
        bounce = load_fmu(ME2, path_to_fmus_me2, False)
        coupled = load_fmu(CoupledME2, path_to_fmus_me2)

        #Try simulate the bouncing ball
        res = bounce.simulate()
        sim_time = res['time']
        nose.tools.assert_almost_equal(sim_time[0], 0.0)
        nose.tools.assert_almost_equal(sim_time[-1], 1.0)
        bounce.reset()

        opts = bounce.simulate_options()
        opts["CVode_options"]["rtol"] = 1e-6
        opts["CVode_options"]["atol"] = 1e-6
        opts["ncp"] = 500

        for i in range(5):
            res = bounce.simulate(start_time=0.1, final_time=1.0, options=opts)
            sim_time = res['time']
            nose.tools.assert_almost_equal(sim_time[0], 0.1)
            nose.tools.assert_almost_equal(sim_time[-1], 1.0)
            assert sim_time.all(
            ) >= sim_time[0] - 1e-4  #Check that the time is increasing
            assert sim_time.all(
            ) <= sim_time[-1] + 1e+4  #Give it some marginal
            height = res['HIGHT']
            assert height.all(
            ) >= -1e-4  #The height of the ball should be non-negative
            nose.tools.assert_almost_equal(res.final('HIGHT'), 0.6269474005, 4)
            if i > 0:  #check that the results stays the same
                diff = height_old - height
                nose.tools.assert_almost_equal(diff[-1], 0.0)
            height_old = height
            bounce.reset()

        #Try to simulate the coupled-clutches
        res_coupled = coupled.simulate()
        sim_time_coupled = res_coupled['time']
        nose.tools.assert_almost_equal(sim_time_coupled[0], 0.0)
        nose.tools.assert_almost_equal(sim_time_coupled[-1], 1.0)
        coupled.reset()

        for i in range(10):
            res_coupled = coupled.simulate(start_time=0.0, final_time=2.0)
            sim_time_coupled = res_coupled['time']
            nose.tools.assert_almost_equal(sim_time_coupled[0], 0.0)
            nose.tools.assert_almost_equal(sim_time_coupled[-1], 2.0)
            assert sim_time_coupled.all(
            ) >= sim_time_coupled[0] - 1e-4  #Check that the time is increasing
            assert sim_time_coupled.all(
            ) <= sim_time_coupled[-1] + 1e+4  #Give it some marginal

            #val_J1 = res_coupled['J1.w']
            #val_J2 = res_coupled['J2.w']
            #val_J3 = res_coupled['J3.w']
            #val_J4 = res_coupled['J4.w']

            val = [
                res_coupled.final('J1.w'),
                res_coupled.final('J2.w'),
                res_coupled.final('J3.w'),
                res_coupled.final('J4.w')
            ]
            if i > 0:  #check that the results stays the same
                for j in range(len(val)):
                    nose.tools.assert_almost_equal(val[j], val_old[j])
            val_old = val
            coupled.reset()

        #Compare to something we know is correct
        me1_model = load_fmu(
            'Modelica_Mechanics_Rotational_Examples_CoupledClutches_ME.fmu',
            path_to_fmus_me1, False)
        res1 = me1_model.simulate(final_time=2.,
                                  options={'result_file_name': 'result1'})
        coupled = load_fmu(CoupledME2, path_to_fmus_me2, False)
        res2 = coupled.simulate(final_time=2.,
                                options={'result_file_name': 'result2'})
        diff1 = res1.final("J1.w") - res2.final("J1.w")
        diff2 = res1.final("J2.w") - res2.final("J2.w")
        diff3 = res1.final("J3.w") - res2.final("J3.w")
        diff4 = res1.final("J4.w") - res2.final("J4.w")
        nose.tools.assert_almost_equal(abs(diff1), 0.0000, 2)
        nose.tools.assert_almost_equal(abs(diff2), 0.0000, 2)
        nose.tools.assert_almost_equal(abs(diff3), 0.0000, 2)
        nose.tools.assert_almost_equal(abs(diff4), 0.0000, 2)

        #Try simualte the robot
        robot = load_fmu(Robot, path_to_fmus_me2)
        result = robot.simulate()
Exemple #49
0
 def test_simulation_cs(self):
     model = load_fmu(
         "Modelica_Mechanics_Rotational_Examples_CoupledClutches_CS.fmu",
         path_to_fmus_cs1)
     res = model.simulate(final_time=1.5)
     assert (res.final("J1.w") - 3.245091100366517) < 1e-4
Exemple #50
0
 def test_event_infinite_iteration_3(self):
     model = load_fmu("EventIter_EventInfiniteIteration3.fmu")
     nose.tools.assert_raises(FMUException, model.simulate)
Exemple #51
0
    def test_get_string(self):
        model = load_fmu(self.string1)

        for i in range(100):  #Test so that memory issues are detected
            assert model.get("str")[0] == "hej"
Exemple #52
0
 def test_exception_output_derivatives(self):
     model = load_fmu(
         "Modelica_Mechanics_Rotational_Examples_CoupledClutches_CS.fmu",
         path_to_fmus_cs1)
     nose.tools.assert_raises(FMUException, model.get_output_derivatives,
                              "u", 1)
Exemple #53
0
 def test_types_platform(self):
     model = load_fmu(
         "Modelica_Mechanics_Rotational_Examples_CoupledClutches_CS.fmu",
         path_to_fmus_cs1)
     assert model.types_platform == "standard32"
Exemple #54
0
 def test_default_simulation_stop_time(self):
     model = load_fmu(
         "Modelica_Mechanics_Rotational_Examples_CoupledClutches_CS.fmu",
         path_to_fmus_cs1)
     res = model.simulate()
     assert N.abs(1.5 - res.final('time')) < 1e-4
    def test_integer_start_time(self):
        model = load_fmu("NegatedAliasCS2.fmu")

        #Assert that there is no exception when reloading the file
        res = model.simulate(start_time=0)