Example #1
0
    def test_get_column(self):
        """
        Test the get_column and get_data_matrix.
        """
        model_file = os.path.join(get_files_path(), 'Modelica',
                                  'RLC_Circuit.mo')
        compile_jmu('RLC_Circuit', model_file)
        model = JMUModel('RLC_Circuit.jmu')
        res = model.simulate()

        assert res.is_negated('resistor1.n.i')
        assert res.is_negated('capacitor.n.i')
        assert not res.is_variable('sine.freqHz')

        dataMatrix = res.data_matrix

        col = res.get_column('capacitor.v')

        nose.tools.assert_almost_equal(dataMatrix[0, col],
                                       res.initial('capacitor.v'), 5)
        nose.tools.assert_almost_equal(dataMatrix[-1, col],
                                       res.final('capacitor.v'), 5)

        nose.tools.assert_raises(VariableNotTimeVarying, res.get_column,
                                 'sine.freqHz')
Example #2
0
 def setUpClass(cls):
     curr_dir = os.path.dirname(os.path.abspath(__file__));
     mofile = os.path.join(get_files_path(), 'Modelica', 'StaticOptimizationTest.mop')
     compile_jmu("StaticOptimizationTest.StaticOptimizationTest2", mofile)
     cls.model = JMUModel("StaticOptimizationTest_StaticOptimizationTest2.jmu")
     cls.nlp = NLPInitialization(cls.model,stat=1)
     cls.ipopt_nlp = InitializationOptimizer(cls.nlp)
Example #3
0
    def setUpClass(cls):
        """Sets up the class."""
        fpath = os.path.join(get_files_path(), 'Modelica', 'CSTR.mop')
        cpath = "CSTR.CSTR_Init"

        compile_jmu(cpath, fpath, 
            compiler_options={'state_start_values_fixed':False})
Example #4
0
 def setUpClass(cls):
     curr_dir = os.path.dirname(os.path.abspath(__file__));
     mofile = os.path.join(get_files_path(), 'Modelica', 'StaticOptimizationTest.mop')
     compile_jmu("StaticOptimizationTest.StaticOptimizationTest2", mofile)
     cls.model = JMUModel("StaticOptimizationTest_StaticOptimizationTest2.jmu")
     cls.nlp = NLPInitialization(cls.model,stat=1)
     cls.ipopt_nlp = InitializationOptimizer(cls.nlp)
Example #5
0
    def setUpClass(cls):
        """
        Compile the test model.
        """
        #DAE with disc
        fpath_DISC = os.path.join(get_files_path(), 'Modelica',
                                  'IfExpExamples.mo')
        cpath_DISC = 'IfExpExamples.IfExpExample2'

        fname_DISC = compile_jmu(cpath_DISC, fpath_DISC)

        #DAE test model
        fpath_DAE = os.path.join(get_files_path(), 'Modelica',
                                 'Pendulum_pack_no_opt.mo')
        cpath_DAE = 'Pendulum_pack.Pendulum'

        fname_DAE = compile_jmu(cpath_DAE, fpath_DAE)

        #DAE with sens
        fpath_SENS = os.path.join(get_files_path(), 'Modelica',
                                  'QuadTankSens.mop')
        cpath_SENS = 'QuadTankSens'

        fname_SENS = compile_jmu(
            cpath_SENS,
            fpath_SENS,
            compiler_options={"enable_variable_scaling": True})

        fpath = os.path.join(get_files_path(), 'Modelica', 'DoubleInput.mo')
        cpath = 'DoubleInput_Nominal'
        fname = compile_jmu(cpath,
                            fpath,
                            compiler_options={"enable_variable_scaling": True})
    def test_scaling(self):
        """
        This tests a simulation when scaling is ON and OFF.
        """
        jmu_name = compile_jmu("Englezos652", os.path.join(get_files_path()
                        ,"Modelica","Englezos652.mop"),
                        compiler_options={"enable_variable_scaling":False})

        # Load a model instance into Python
        model = JMUModel(jmu_name)

        # Get and set the options
        opts = model.simulate_options()
        opts['IDA_options']['atol'] = 1.0e-9
        opts['IDA_options']['rtol'] = 1.0e-9
        opts['IDA_options']['sensitivity'] = True
        opts['ncp'] = 400
        
        res_no_scale = model.simulate(final_time=1697000, options=opts)
        
        jmu_name = compile_jmu("Englezos652", os.path.join(get_files_path()
                        ,"Modelica","Englezos652.mop"),
                        compiler_options={"enable_variable_scaling":True})

        # Load a model instance into Python
        model = JMUModel(jmu_name)
        
        res_with_scale = model.simulate(final_time=1697000, options=opts)
        
        nose.tools.assert_almost_equal(res_with_scale['x1'][-1], 
                                       res_no_scale['x1'][-1], 4)
        
        nose.tools.assert_almost_equal(res_with_scale['dx1/dk1'][-1], 
                                       res_no_scale['dx1/dk1'][-1], 1)
    def setUpClass(cls):
        """
        Compile the test model.
        """
        #DAE with disc
        fpath_DISC = os.path.join(get_files_path(), 'Modelica', 
            'IfExpExamples.mo')
        cpath_DISC = 'IfExpExamples.IfExpExample2'
        
        fname_DISC = compile_jmu(cpath_DISC, fpath_DISC)
        
        #DAE test model
        fpath_DAE = os.path.join(get_files_path(), 'Modelica', 
            'Pendulum_pack_no_opt.mo')
        cpath_DAE = 'Pendulum_pack.Pendulum'

        fname_DAE = compile_jmu(cpath_DAE, fpath_DAE)
        
        #DAE with sens
        fpath_SENS = os.path.join(get_files_path(), 'Modelica', 
            'QuadTankSens.mop')
        cpath_SENS = 'QuadTankSens'
        
        fname_SENS = compile_jmu(cpath_SENS, fpath_SENS, compiler_options={"enable_variable_scaling":True})
        
        fpath = os.path.join(get_files_path(), 'Modelica', 'DoubleInput.mo')
        cpath = 'DoubleInput_Nominal'
        fname = compile_jmu(cpath, fpath, 
                    compiler_options={"enable_variable_scaling":True})
Example #8
0
    def test_scaling(self):
        """
        This tests a simulation when scaling is ON and OFF.
        """
        jmu_name = compile_jmu("Englezos652", os.path.join(get_files_path()
                        ,"Modelica","Englezos652.mop"),
                        compiler_options={"enable_variable_scaling":False})

        # Load a model instance into Python
        model = JMUModel(jmu_name)

        # Get and set the options
        opts = model.simulate_options()
        opts['IDA_options']['atol'] = 1.0e-9
        opts['IDA_options']['rtol'] = 1.0e-9
        opts['IDA_options']['sensitivity'] = True
        opts['ncp'] = 400
        
        res_no_scale = model.simulate(final_time=1697000, options=opts)
        
        jmu_name = compile_jmu("Englezos652", os.path.join(get_files_path()
                        ,"Modelica","Englezos652.mop"),
                        compiler_options={"enable_variable_scaling":True})

        # Load a model instance into Python
        model = JMUModel(jmu_name)
        
        res_with_scale = model.simulate(final_time=1697000, options=opts)
        
        nose.tools.assert_almost_equal(res_with_scale['x1'][-1], 
                                       res_no_scale['x1'][-1], 4)
        
        nose.tools.assert_almost_equal(res_with_scale['dx1/dk1'][-1], 
                                       res_no_scale['dx1/dk1'][-1], 1)
Example #9
0
 def setUpClass(cls):
     """Sets up the test class."""
     fpath_daeinit = os.path.join(get_files_path(), 'Modelica', 
         'DAEInitTest.mo')
     cpath_daeinit = "DAEInitTest"
     compile_jmu(cpath_daeinit, fpath_daeinit, 
         compiler_options={'state_start_values_fixed':True, 'variability_propagation':False})
Example #10
0
    def setUpClass(cls):
        """
        Sets up the test class.
        """
        fpath = os.path.join(get_files_path(), 'Modelica', 'CSTR.mop')
        cpath = "CSTR.CSTR_Init_Optimization"

        compile_jmu(cpath, fpath)
Example #11
0
 def setup(self):
     """ 
     Setup test module. Compile test model (only needs to be done once) and 
     set log level. 
     """
     compile_jmu(cpath,
                 fpath,
                 compiler_options={'state_start_values_fixed': True})
Example #12
0
 def test_parameter_alias(self):
     """ Test simulate and write to file when model has parameter alias.
         (Test so that write to file does not crash.)
     """
     model_file = os.path.join(get_files_path(), 'Modelica', 'ParameterAlias.mo')
     compile_jmu('ParameterAlias', model_file)
     model = JMUModel('ParameterAlias.jmu')
     model.simulate()
Example #13
0
 def setUpClass(cls):
     """Sets up the test class."""
     # Compile the stationary initialization model into a JMU
     fpath1 = os.path.join(get_files_path(), 'Modelica', 'CSTRLib.mo')
     fpath2 = os.path.join(get_files_path(), 'Modelica', 'TestMinMax.mo')
     compile_jmu("CSTRLib.Components.Two_CSTRs_stat_init", fpath1)
     #compile_jmu("Tests.TestInvalidStart", fpath2)
     compile_jmu("TestMinMax.TestGuess", fpath2)
Example #14
0
    def setUpClass(cls):
        """Sets up the class."""
        fpath = os.path.join(get_files_path(), 'Modelica', 'CSTR.mop')
        cpath = "CSTR.CSTR_Init"

        compile_jmu(cpath,
                    fpath,
                    compiler_options={'state_start_values_fixed': False})
Example #15
0
    def setUpClass(cls):
        """
        Sets up the test class.
        """
        fpath = os.path.join(get_files_path(), 'Modelica', 'CSTR.mop')
        cpath = "CSTR.CSTR_Init_Optimization"

        compile_jmu(cpath, fpath)
Example #16
0
 def setUpClass(cls):
     """Sets up the test class."""
     # Compile the stationary initialization model into a JMU
     fpath1 = os.path.join(get_files_path(), 'Modelica', 'CSTRLib.mo')
     fpath2 = os.path.join(get_files_path(), 'Modelica', 'TestMinMax.mo')
     compile_jmu("CSTRLib.Components.Two_CSTRs_stat_init", fpath1)
     #compile_jmu("Tests.TestInvalidStart", fpath2)
     compile_jmu("TestMinMax.TestGuess", fpath2)
Example #17
0
    def setUpClass(cls):
        """
        Sets up the test class.
        """
        fpath = os.path.join(get_files_path(), 'Modelica', 'VDP.mop')
        cpath = "VDP_pack.VDP_Opt_Min_Time"

        compile_jmu(cpath, fpath, compiler_options={'state_start_values_fixed':True})
Example #18
0
 def setUpClass(cls):
     """
     Compile the test model.
     """
     # compile cstr
     fpath_cstr = os.path.join(get_files_path(), 'Modelica', 'CSTR.mop')
     cpath_cstr = "CSTR.CSTR_Opt"
     compile_jmu(cpath_cstr, fpath_cstr, 
         compiler_options={'state_start_values_fixed':True})
Example #19
0
 def setUpClass(cls):
     """
     Compile the test model.
     """
     # compile cstr
     fpath_cstr = os.path.join(get_files_path(), 'Modelica', 'CSTR.mop')
     cpath_cstr = "CSTR.CSTR_Init"
     compile_jmu(cpath_cstr, fpath_cstr,
         compiler_options={"enable_variable_scaling":True}) 
Example #20
0
 def setUpClass(cls):
     """
     Compile the test model.
     """
     # compile vdp
     fpath_vdp = os.path.join(get_files_path(), 'Modelica', 'VDP.mop')
     cpath_vdp = "VDP_pack.VDP_Opt_Min_Time"
     compile_jmu(cpath_vdp, fpath_vdp,
         compiler_options={'state_start_values_fixed':True}) 
Example #21
0
 def setUpClass(cls):
     """
     Compile the test model.
     """
     # compile cstr
     fpath_cstr = os.path.join(get_files_path(), 'Modelica', 'CSTR.mop')
     cpath_cstr = "CSTR.CSTR_Init"
     compile_jmu(cpath_cstr,
                 fpath_cstr,
                 compiler_options={"enable_variable_scaling": True})
Example #22
0
    def setUpClass(cls):
        cls.curr_dir = os.path.dirname(os.path.abspath(__file__));
        mofile = os.path.join(get_files_path(), 'Modelica', 
                'BlockingError.mop')

        m = compile_jmu("BlockingInitPack.M_init", mofile)
        cls.model = JMUModel(m)
        
        m = compile_jmu("BlockingInitPack.M_Opt",mofile)
        cls.opt_model = JMUModel(m)
Example #23
0
    def setUpClass(cls):
        cls.curr_dir = os.path.dirname(os.path.abspath(__file__))
        mofile = os.path.join(get_files_path(), 'Modelica',
                              'BlockingError.mop')

        m = compile_jmu("BlockingInitPack.M_init", mofile)
        cls.model = JMUModel(m)

        m = compile_jmu("BlockingInitPack.M_Opt", mofile)
        cls.opt_model = JMUModel(m)
Example #24
0
 def setUpClass(cls):
     """Sets up the test class."""
     fpath_daeinit = os.path.join(get_files_path(), 'Modelica',
                                  'DAEInitTest.mo')
     cpath_daeinit = "DAEInitTest"
     compile_jmu(cpath_daeinit,
                 fpath_daeinit,
                 compiler_options={
                     'state_start_values_fixed': True,
                     'variability_propagation': False
                 })
    def test_order_input(self):
        """
        This tests that the inputs are sorted in an correct value reference order
        when being written to file.
        """
        fpath = os.path.join(get_files_path(), 'Modelica', 'OrderInputs.mop')
        cpath = 'OptimInputs'
        
        unames = ['u1', 'u_e2', 'u_h3', 'u_c4', 'u_p5']
        n = len(unames)

        uvalues = [1.,2.,3.,4.,5.]

        data = N.array([[0.,1.,2.,3.,4.,5.],
                 [1.,1.,2.,3.,4.,5.],
                 [2.,1.,2.,3.,4.,5.]])
        inputtuple = (unames,data)
        jmu_name = compile_jmu(cpath,fpath)
        
        model = JMUModel(jmu_name)
        res = model.simulate(0,2,input=inputtuple)
        
        nose.tools.assert_almost_equal(res["u1"][-1], 1.000000000, 3)
        nose.tools.assert_almost_equal(res["u_e2"][-1], 2.00000, 3)
        nose.tools.assert_almost_equal(res["u_h3"][-1], 3.00000, 3)
        nose.tools.assert_almost_equal(res["u_c4"][-1], 4.000000, 3)
        nose.tools.assert_almost_equal(res["u_p5"][-1], 5.000000, 3)
        
        nose.tools.assert_almost_equal(res["T.u1"][-1], 1.000000000, 3)
        nose.tools.assert_almost_equal(res["T.u_e2"][-1], 2.00000, 3)
        nose.tools.assert_almost_equal(res["T.u_h3"][-1], 3.00000, 3)
        nose.tools.assert_almost_equal(res["T.u_c4"][-1], 4.000000, 3)
        nose.tools.assert_almost_equal(res["T.u_p5"][-1], 5.000000, 3)
    def test_scaling_test_2(self):
        """
        This tests a simulation when scaling is ON and there are input variables
        that are not used.
        """
        jmu_name = compile_jmu("Englezos652_with_input", os.path.join(get_files_path()
                        ,"Modelica","Englezos652.mop"),
                        compiler_options={"enable_variable_scaling":False})

        # Load a model instance into Python
        model = JMUModel(jmu_name)

        # Get and set the options
        opts = model.simulate_options()
        opts['IDA_options']['atol'] = 1.0e-6
        opts['IDA_options']['rtol'] = 1.0e-6
        opts['IDA_options']['sensitivity'] = True
        opts['ncp'] = 400
        
        res = model.simulate(0,1697000/3, options=opts)
        
        x1 = res["x1"][-1]
        r1 = res["r1"][-1]
        u1 = res["u1"][-1]

        nose.tools.assert_almost_equal(x1, 0.45537058, 3)
        nose.tools.assert_almost_equal(r1, 5.3287e-8, 2)
        nose.tools.assert_almost_equal(u1, 0.00000, 3)
Example #27
0
    def test_time_shift(self):
        """
        Test the time shift feature
        """
        model_file = os.path.join(get_files_path(), 'Modelica', 'RLC_Circuit.mo')
        compile_jmu('RLC_Circuit', model_file)
        model = JMUModel('RLC_Circuit.jmu')
        res = model.simulate()

        time_shifted_fix = res['time'] + 11.

        res.result_data.shift_time(11.)

        time_shifted = res['time']

        assert max(N.abs(time_shifted_fix - time_shifted)) < 1e-6
Example #28
0
    def test_order_input(self):
        """
        This tests that the inputs are sorted in an correct value reference order
        when being written to file.
        """
        fpath = os.path.join(get_files_path(), 'Modelica', 'OrderInputs.mop')
        cpath = 'OptimInputs'

        unames = ['u1', 'u_e2', 'u_h3', 'u_c4', 'u_p5']
        n = len(unames)

        uvalues = [1., 2., 3., 4., 5.]

        data = N.array([[0., 1., 2., 3., 4., 5.], [1., 1., 2., 3., 4., 5.],
                        [2., 1., 2., 3., 4., 5.]])
        inputtuple = (unames, data)
        jmu_name = compile_jmu(cpath, fpath)

        model = JMUModel(jmu_name)
        res = model.simulate(0, 2, input=inputtuple)

        nose.tools.assert_almost_equal(res["u1"][-1], 1.000000000, 3)
        nose.tools.assert_almost_equal(res["u_e2"][-1], 2.00000, 3)
        nose.tools.assert_almost_equal(res["u_h3"][-1], 3.00000, 3)
        nose.tools.assert_almost_equal(res["u_c4"][-1], 4.000000, 3)
        nose.tools.assert_almost_equal(res["u_p5"][-1], 5.000000, 3)

        nose.tools.assert_almost_equal(res["T.u1"][-1], 1.000000000, 3)
        nose.tools.assert_almost_equal(res["T.u_e2"][-1], 2.00000, 3)
        nose.tools.assert_almost_equal(res["T.u_h3"][-1], 3.00000, 3)
        nose.tools.assert_almost_equal(res["T.u_c4"][-1], 4.000000, 3)
        nose.tools.assert_almost_equal(res["T.u_p5"][-1], 5.000000, 3)
Example #29
0
    def test_scaling_test_2(self):
        """
        This tests a simulation when scaling is ON and there are input variables
        that are not used.
        """
        jmu_name = compile_jmu(
            "Englezos652_with_input",
            os.path.join(get_files_path(), "Modelica", "Englezos652.mop"),
            compiler_options={"enable_variable_scaling": False})

        # Load a model instance into Python
        model = JMUModel(jmu_name)

        # Get and set the options
        opts = model.simulate_options()
        opts['IDA_options']['atol'] = 1.0e-6
        opts['IDA_options']['rtol'] = 1.0e-6
        opts['IDA_options']['sensitivity'] = True
        opts['ncp'] = 400

        res = model.simulate(0, 1697000 / 3, options=opts)

        x1 = res["x1"][-1]
        r1 = res["r1"][-1]
        u1 = res["u1"][-1]

        nose.tools.assert_almost_equal(x1, 0.45537058, 3)
        nose.tools.assert_almost_equal(r1, 5.3287e-8, 2)
        nose.tools.assert_almost_equal(u1, 0.00000, 3)
Example #30
0
    def setup_class_base(cls,
                         mo_file,
                         class_name,
                         options={},
                         format='jmu',
                         target="me"):
        """
        Set up a new test model. Compiles the model. 
        Call this with proper args from setUpClass(). 
          mo_file     - the relative path from the files dir to the .mo file to compile
          class_name  - the qualified name of the class to simulate
          options     - a dict of options to set in the compiler, defaults to no options
          format      - either 'jmu' or 'fmu' depending on which format should be tested
        """
        global _model_name
        cls.mo_path = os.path.join(get_files_path(), 'Modelica', mo_file)

        if format == 'jmu':
            _model_name = compile_jmu(class_name,
                                      cls.mo_path,
                                      compiler_options=options)
        elif format == 'fmu':
            _model_name = compile_fmu(class_name,
                                      cls.mo_path,
                                      compiler_options=options,
                                      target=target)
        else:
            raise Exception("Format must be either 'jmu' or 'fmu'.")
Example #31
0
 def setUpClass(cls):
     """
     Sets up the test class.
     """
     # VDP model
     fpath_vdp = os.path.join(get_files_path(),'Modelica','VDP.mop')
     cpath_vdp = "VDP_pack.VDP_Opt"
     
     cls.jmu_name = compile_jmu(cpath_vdp, fpath_vdp)
Example #32
0
    def setUpClass(cls):
        """
        Sets up the test class.
        """
        # RLC model
        fpath_rlc = os.path.join(get_files_path(),'Modelica','RLC_Circuit.mo')
        cpath_rlc = "RLC_Circuit"

        cls.jmu_name = compile_jmu(cpath_rlc, fpath_rlc, compiler_options={'state_start_values_fixed':True})
Example #33
0
    def setUpClass(cls):
        """
        Sets up the test class.
        """
        # RLC model
        fpath_rlc = os.path.join(get_files_path(),'Modelica','RLC_Circuit.mo')
        cpath_rlc = "RLC_Circuit"

        cls.jmu_name = compile_jmu(cpath_rlc, fpath_rlc, compiler_options={'state_start_values_fixed':True})
Example #34
0
 def setUpClass(cls):
     """
     Sets up the test class.
     """
     # VDP model
     fpath_vdp = os.path.join(get_files_path(),'Modelica','VDP.mop')
     cpath_vdp = "VDP_pack.VDP_Opt"
     
     cls.jmu_name = compile_jmu(cpath_vdp, fpath_vdp)
Example #35
0
 def test_dependent_parameters(self):
     """ Test evaluation of dependent parameters. """
     path = os.path.join(get_files_path(), 'Modelica', 'DepPar.mo')
     jmu_name = compile_jmu('DepPar.DepPar1', path, 
         compiler_options={'state_start_values_fixed':True})
     model = JMUModel(jmu_name)
     
     assert (model.get('p1') == 1.0)
     assert (model.get('p2') == 2.0)
     assert (model.get('p3') == 6.0)
Example #36
0
 def test_optimize(self):
     """ Test the pyjmi.JMUModel.optimize function using all default parameters. """
     fpath_pend = os.path.join(get_files_path(), 'Modelica', 'Pendulum_pack.mop')
     cpath_pend = "Pendulum_pack.Pendulum_Opt"
     jmu_pend = compile_jmu(cpath_pend, fpath_pend,compiler_options={'state_start_values_fixed':True})
     pend = JMUModel(jmu_pend)
     
     res = pend.optimize()
     
     assert N.abs(res.final('cost') - 1.2921683e-01) < 1e-3
Example #37
0
 def test_optimize(self):
     """ Test the pyjmi.JMUModel.optimize function using all default parameters. """
     fpath_pend = os.path.join(get_files_path(), 'Modelica', 'Pendulum_pack.mop')
     cpath_pend = "Pendulum_pack.Pendulum_Opt"
     jmu_pend = compile_jmu(cpath_pend, fpath_pend,compiler_options={'state_start_values_fixed':True})
     pend = JMUModel(jmu_pend)
     
     res = pend.optimize()
     
     assert N.abs(res.final('cost') - 1.2921683e-01) < 1e-3
Example #38
0
 def test_dependent_parameters(self):
     """ Test evaluation of dependent parameters. """
     path = os.path.join(get_files_path(), 'Modelica', 'DepPar.mo')
     jmu_name = compile_jmu('DepPar.DepPar1', path, 
         compiler_options={'state_start_values_fixed':True})
     model = JMUModel(jmu_name)
     
     assert (model.get('p1') == 1.0)
     assert (model.get('p2') == 2.0)
     assert (model.get('p3') == 6.0)
Example #39
0
 def test_result(self):
     """ Test simulate and write to file when model has parameter alias.
         Also tests the methods is_variable and get_column in io.ResultDymolaTextual.
     """
     model_file = os.path.join(get_files_path(), 'Modelica', 'ParameterAlias.mo')
     compile_jmu('ParameterAlias', model_file)
     model = JMUModel('ParameterAlias.jmu')
     res = model.simulate()
     
     assert not res.is_variable('p2')
     assert not res.is_variable('x')
     assert not res.is_variable('p1')
     assert res.is_variable('der(y)')
     assert res.is_variable('y')
     
     assert res.get_column('der(y)') == 1
     assert res.get_column('y') == 2
     assert res.get_column('time') == 0
     
     assert res.is_negated('der(y)') == False
     assert res.is_negated('y') == False
Example #40
0
 def test_get_column(self):
     """
     Test the get_column and get_data_matrix.
     """
     model_file = os.path.join(get_files_path(), 'Modelica', 'RLC_Circuit.mo')
     compile_jmu('RLC_Circuit', model_file)
     model = JMUModel('RLC_Circuit.jmu')
     res = model.simulate()
     
     assert res.is_negated('resistor1.n.i')
     assert res.is_negated('capacitor.n.i')
     assert not res.is_variable('sine.freqHz')
     
     dataMatrix = res.data_matrix
     
     col = res.get_column('capacitor.v')
     
     nose.tools.assert_almost_equal(dataMatrix[0,col], res.initial('capacitor.v'),5)
     nose.tools.assert_almost_equal(dataMatrix[-1,col], res.final('capacitor.v'),5)
     
     nose.tools.assert_raises(VariableNotTimeVarying, res.get_column, 'sine.freqHz')
Example #41
0
    def test_alias_variables(self):
        """
        This tests a simulation when there are alias in the sensitivity parameters.
        """
        #DAE with sens
        file_name = os.path.join(get_files_path(), 'Modelica',
                                 'SensitivityTests.mop')
        model_name = 'SensitivityTests.SensTest1'

        jmu_name = compile_jmu(model_name, file_name)

        model = JMUModel(jmu_name)

        opts = model.simulate_options()

        opts['IDA_options']['sensitivity'] = True

        res = model.simulate(options=opts)

        x1 = res['dx1/da']
        #x2 = res['dx2/da']
        x3 = res['dx3/da']
        x4 = res['dx4/da']
        x5 = res['dx5/da']

        #nose.tools.assert_almost_equal(x2[-1], 0.000000, 4)
        nose.tools.assert_almost_equal(x3[-1], 1.000000, 4)
        nose.tools.assert_almost_equal(x4[-1], -1.000000, 4)
        nose.tools.assert_almost_equal(x1[-1], x5[-1], 4)

        #The same test using continuous writing

        jmu_name = 'SensitivityTests_SensTest1.jmu'

        model = JMUModel(jmu_name)

        opts = model.simulate_options()

        opts['IDA_options']['sensitivity'] = True
        opts['report_continuously'] = True

        res = model.simulate(options=opts)

        x1 = res['dx1/da']
        #x2 = res['dx2/da']
        x3 = res['dx3/da']
        x4 = res['dx4/da']
        x5 = res['dx5/da']

        #nose.tools.assert_almost_equal(x2[-1], 0.000000, 4)
        nose.tools.assert_almost_equal(x3[-1], 1.000000, 4)
        nose.tools.assert_almost_equal(x4[-1], -1.000000, 4)
        nose.tools.assert_almost_equal(x1[-1], x5[-1], 4)
 def test_alias_variables(self):
     """
     This tests a simulation when there are alias in the sensitivity parameters.
     """
     #DAE with sens
     file_name = os.path.join(get_files_path(), 'Modelica', 
         'SensitivityTests.mop')
     model_name = 'SensitivityTests.SensTest1'
     
     jmu_name = compile_jmu(model_name, file_name)
     
     model = JMUModel(jmu_name)
     
     opts = model.simulate_options()
     
     opts['IDA_options']['sensitivity'] = True
     
     res = model.simulate(options=opts)
     
     x1 = res['dx1/da']
     #x2 = res['dx2/da']
     x3 = res['dx3/da']
     x4 = res['dx4/da']
     x5 = res['dx5/da']
     
     #nose.tools.assert_almost_equal(x2[-1], 0.000000, 4)
     nose.tools.assert_almost_equal(x3[-1], 1.000000, 4)
     nose.tools.assert_almost_equal(x4[-1], -1.000000,4)
     nose.tools.assert_almost_equal(x1[-1], x5[-1], 4)
     
     #The same test using continuous writing
     
     jmu_name = 'SensitivityTests_SensTest1.jmu'
     
     model = JMUModel(jmu_name)
     
     opts = model.simulate_options()
     
     opts['IDA_options']['sensitivity'] = True
     opts['report_continuously'] = True
     
     res = model.simulate(options=opts)
     
     x1 = res['dx1/da']
     #x2 = res['dx2/da']
     x3 = res['dx3/da']
     x4 = res['dx4/da']
     x5 = res['dx5/da']
     
     #nose.tools.assert_almost_equal(x2[-1], 0.000000, 4)
     nose.tools.assert_almost_equal(x3[-1], 1.000000, 4)
     nose.tools.assert_almost_equal(x4[-1], -1.000000,4)
     nose.tools.assert_almost_equal(x1[-1], x5[-1], 4)
Example #43
0
    def test_initialize_with_solverargs(self):
        """ Test the pyjmi.JMUModel.initialize function using all default parameters. """
        fpath_pend = os.path.join(get_files_path(), 'Modelica', 'Pendulum_pack.mop')
        cpath_pend = "Pendulum_pack.Pendulum"
        
        jmu_pend = compile_jmu(cpath_pend, fpath_pend)
        pend = JMUModel(jmu_pend)
        
        res = pend.initialize(options={'IPOPT_options':{'max_iter':1000}})

        assert N.abs(res.final('theta') - 0.1)              < 1e-3
        assert N.abs(res.final('dtheta') - 0.)              < 1e-3
        assert N.abs(res.final('x') - 0)                    < 1e-3
        assert N.abs(res.final('dx') - 0)                   < 1e-3
        assert N.abs(res.final('der(theta)') - 0)           < 1e-3
        assert N.abs(res.final('der(dtheta)') - 0.09983341) < 1e-3
        assert N.abs(res.final('der(x)') - 0)               < 1e-3
        assert N.abs(res.final('der(dx)') - 0)              < 1e-3
Example #44
0
    def test_initialize_with_solverargs(self):
        """ Test the pyjmi.JMUModel.initialize function using all default parameters. """
        fpath_pend = os.path.join(get_files_path(), 'Modelica', 'Pendulum_pack.mop')
        cpath_pend = "Pendulum_pack.Pendulum"
        
        jmu_pend = compile_jmu(cpath_pend, fpath_pend)
        pend = JMUModel(jmu_pend)
        
        res = pend.initialize(options={'IPOPT_options':{'max_iter':1000}})

        assert N.abs(res.final('theta') - 0.1)              < 1e-3
        assert N.abs(res.final('dtheta') - 0.)              < 1e-3
        assert N.abs(res.final('x') - 0)                    < 1e-3
        assert N.abs(res.final('dx') - 0)                   < 1e-3
        assert N.abs(res.final('der(theta)') - 0)           < 1e-3
        assert N.abs(res.final('der(dtheta)') - 0.09983341) < 1e-3
        assert N.abs(res.final('der(x)') - 0)               < 1e-3
        assert N.abs(res.final('der(dx)') - 0)              < 1e-3
Example #45
0
 def test_simulate_initialize_arg(self):
     """ Test pyjmi.JMUModel.simulate alg_arg 'initialize'. """
     # This test is built on that simulation without initialization fails.
     # Since simulation without initialization fails far down in Sundials
     # no "proper" exception is thrown which means that I can only test that
     # the general Exception is returned which means that the test is pretty
     # unspecific (the test will pass for every type of error thrown). Therefore,
     # I first test that running the simulation with default settings succeeds, so
     # at least one knows that the error has with initialization to do.
     name = compile_jmu(self.cpath_minit, self.fpath_minit)
     model = JMUModel(name)
     
     nose.tools.ok_(model.simulate())
     
     model = JMUModel(name)
     
     nose.tools.assert_raises(Exception,
                              model.simulate,
                              options={'initialize':False})
Example #46
0
 def test_simulate_initialize_arg(self):
     """ Test pyjmi.JMUModel.simulate alg_arg 'initialize'. """
     # This test is built on that simulation without initialization fails.
     # Since simulation without initialization fails far down in Sundials
     # no "proper" exception is thrown which means that I can only test that
     # the general Exception is returned which means that the test is pretty
     # unspecific (the test will pass for every type of error thrown). Therefore,
     # I first test that running the simulation with default settings succeeds, so
     # at least one knows that the error has with initialization to do.
     name = compile_jmu(self.cpath_minit, self.fpath_minit)
     model = JMUModel(name)
     
     nose.tools.ok_(model.simulate())
     
     model = JMUModel(name)
     
     nose.tools.assert_raises(Exception,
                              model.simulate,
                              options={'initialize':False})
    def test_double_input(self):
        """
        This tests double input.
        """
        fpath = os.path.join(get_files_path(), 'Modelica', 'DoubleInput.mo')
        cpath = 'DoubleInput'

        # compile VDP
        fname = compile_jmu(cpath, fpath, 
                    compiler_options={'state_start_values_fixed':True})

        # Load the dynamic library and XML data
        dInput = JMUModel(fname)
        
        t = N.linspace(0.,10.,100) 
        u1 = N.cos(t)
        u2 = N.sin(t)
        u_traj = N.transpose(N.vstack((t,u1,u2)))
        
        res = dInput.simulate(final_time=10, input=(['u1','u2'],u_traj))
        
        nose.tools.assert_almost_equal(res.initial('u1'), 1.000000000, 3)
        nose.tools.assert_almost_equal(res.initial('u2'), 0.000000000, 3)
        nose.tools.assert_almost_equal(res.final('u1'), -0.839071529, 3)
        nose.tools.assert_almost_equal(res.final('u2'), -0.544021110, 3)
        
        #TEST REVERSE ORDER OF INPUT
        
        # Load the dynamic library and XML data
        dInput = JMUModel(fname)
        
        t = N.linspace(0.,10.,100) 
        u1 = N.cos(t)
        u2 = N.sin(t)
        u_traj = N.transpose(N.vstack((t,u2,u1)))
        
        res = dInput.simulate(final_time=10, input=(['u2','u1'],u_traj))
        
        nose.tools.assert_almost_equal(res.initial('u1'), 1.000000000, 3)
        nose.tools.assert_almost_equal(res.initial('u2'), 0.000000000, 3)
        nose.tools.assert_almost_equal(res.final('u1'), -0.839071529, 3)
        nose.tools.assert_almost_equal(res.final('u2'), -0.544021110, 3)
Example #48
0
    def test_double_input(self):
        """
        This tests double input.
        """
        fpath = os.path.join(get_files_path(), 'Modelica', 'DoubleInput.mo')
        cpath = 'DoubleInput'

        # compile VDP
        fname = compile_jmu(
            cpath, fpath, compiler_options={'state_start_values_fixed': True})

        # Load the dynamic library and XML data
        dInput = JMUModel(fname)

        t = N.linspace(0., 10., 100)
        u1 = N.cos(t)
        u2 = N.sin(t)
        u_traj = N.transpose(N.vstack((t, u1, u2)))

        res = dInput.simulate(final_time=10, input=(['u1', 'u2'], u_traj))

        nose.tools.assert_almost_equal(res.initial('u1'), 1.000000000, 3)
        nose.tools.assert_almost_equal(res.initial('u2'), 0.000000000, 3)
        nose.tools.assert_almost_equal(res.final('u1'), -0.839071529, 3)
        nose.tools.assert_almost_equal(res.final('u2'), -0.544021110, 3)

        #TEST REVERSE ORDER OF INPUT

        # Load the dynamic library and XML data
        dInput = JMUModel(fname)

        t = N.linspace(0., 10., 100)
        u1 = N.cos(t)
        u2 = N.sin(t)
        u_traj = N.transpose(N.vstack((t, u2, u1)))

        res = dInput.simulate(final_time=10, input=(['u2', 'u1'], u_traj))

        nose.tools.assert_almost_equal(res.initial('u1'), 1.000000000, 3)
        nose.tools.assert_almost_equal(res.initial('u2'), 0.000000000, 3)
        nose.tools.assert_almost_equal(res.final('u1'), -0.839071529, 3)
        nose.tools.assert_almost_equal(res.final('u2'), -0.544021110, 3)
    def test_double_input_with_function(self):
        """
        This tests double input with function.
        """
        fpath = os.path.join(get_files_path(), 'Modelica', 'DoubleInput.mo')
        cpath = 'DoubleInput'

        # compile VDP
        fname = compile_jmu(cpath, fpath, 
                    compiler_options={'state_start_values_fixed':True})

        # Load the dynamic library and XML data
        dInput = JMUModel(fname)

        def func(t):
            return N.array([N.cos(t),N.sin(t)])
        
        res = dInput.simulate(final_time=10, input=(['u1','u2'],func))

        nose.tools.assert_almost_equal(res.initial('u1'), 1.000000000, 3)
        nose.tools.assert_almost_equal(res.initial('u2'), 0.000000000, 3)
        nose.tools.assert_almost_equal(res.final('u1'), -0.839071529, 3)
        nose.tools.assert_almost_equal(res.final('u2'), -0.544021110, 3)
        
        #TEST REVERSE ORDER OF INPUT
        
        # Load the dynamic library and XML data
        dInput = JMUModel(fname)

        def func(t):
            return [N.sin(t),N.cos(t)]
        
        res = dInput.simulate(final_time=10, input=(['u2','u1'],func))

        nose.tools.assert_almost_equal(res.initial('u1'), 1.000000000, 3)
        nose.tools.assert_almost_equal(res.initial('u2'), 0.000000000, 3)
        nose.tools.assert_almost_equal(res.final('u1'), -0.839071529, 3)
        nose.tools.assert_almost_equal(res.final('u2'), -0.544021110, 3)
Example #50
0
    def test_double_input_with_function(self):
        """
        This tests double input with function.
        """
        fpath = os.path.join(get_files_path(), 'Modelica', 'DoubleInput.mo')
        cpath = 'DoubleInput'

        # compile VDP
        fname = compile_jmu(
            cpath, fpath, compiler_options={'state_start_values_fixed': True})

        # Load the dynamic library and XML data
        dInput = JMUModel(fname)

        def func(t):
            return N.array([N.cos(t), N.sin(t)])

        res = dInput.simulate(final_time=10, input=(['u1', 'u2'], func))

        nose.tools.assert_almost_equal(res.initial('u1'), 1.000000000, 3)
        nose.tools.assert_almost_equal(res.initial('u2'), 0.000000000, 3)
        nose.tools.assert_almost_equal(res.final('u1'), -0.839071529, 3)
        nose.tools.assert_almost_equal(res.final('u2'), -0.544021110, 3)

        #TEST REVERSE ORDER OF INPUT

        # Load the dynamic library and XML data
        dInput = JMUModel(fname)

        def func(t):
            return [N.sin(t), N.cos(t)]

        res = dInput.simulate(final_time=10, input=(['u2', 'u1'], func))

        nose.tools.assert_almost_equal(res.initial('u1'), 1.000000000, 3)
        nose.tools.assert_almost_equal(res.initial('u2'), 0.000000000, 3)
        nose.tools.assert_almost_equal(res.final('u1'), -0.839071529, 3)
        nose.tools.assert_almost_equal(res.final('u2'), -0.544021110, 3)
Example #51
0
 def setUpClass(clf):
     clf.curr_dir = os.path.dirname(os.path.abspath(__file__))
     clf.jn = compile_jmu(
         'MinTimeInit', clf.curr_dir + '/../files/Modelica/MinTimeInit.mop')
     clf.m = JMUModel(clf.jn)
Example #52
0
 def setup(self):
     """ 
     Setup test module. Compile test model (only needs to be done once) and 
     set log level. 
     """
     compile_jmu(cpath, fpath, compiler_options={"state_start_values_fixed": True})
Example #53
0
 def setUpClass(clf):
     clf.curr_dir = os.path.dirname(os.path.abspath(__file__));
     clf.jn = compile_jmu('MinTimeInit',clf.curr_dir + '/../files/Modelica/MinTimeInit.mop')
     clf.m = JMUModel(clf.jn)