Ejemplo n.º 1
0
    def test_caching(self):
        negated_alias = FMUModelME1("NegatedAlias.fmu",
                                    os.path.join(file_path, "files", "FMUs",
                                                 "XML", "ME1.0"),
                                    _connect_dll=False)

        assert len(negated_alias.cache) == 0  #No starting cache

        vars_1 = negated_alias.get_model_variables()
        vars_2 = negated_alias.get_model_variables()
        assert id(vars_1) == id(vars_2)

        vars_3 = negated_alias.get_model_variables(filter="*")
        assert id(vars_1) != id(vars_3)

        vars_4 = negated_alias.get_model_variables(type=0)
        assert id(vars_3) != id(vars_4)

        vars_5 = negated_alias.get_model_time_varying_value_references()
        vars_7 = negated_alias.get_model_time_varying_value_references()
        assert id(vars_5) != id(vars_1)
        assert id(vars_5) == id(vars_7)

        negated_alias = FMUModelME1("NegatedAlias.fmu",
                                    os.path.join(file_path, "files", "FMUs",
                                                 "XML", "ME1.0"),
                                    _connect_dll=False)

        assert len(negated_alias.cache) == 0  #No starting cache

        vars_6 = negated_alias.get_model_variables()
        assert id(vars_1) != id(vars_6)
Ejemplo n.º 2
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")
Ejemplo n.º 3
0
    def test_ode_get_sizes(self):
        bounce = FMUModelME1("bouncingBall.fmu", os.path.join(file_path, "files", "FMUs", "XML", "ME1.0"), _connect_dll=False)
        dq = FMUModelME1("dq.fmu", os.path.join(file_path, "files", "FMUs", "XML", "ME1.0"), _connect_dll=False)
        
        [nCont,nEvent] = bounce.get_ode_sizes()
        assert nCont == 2
        assert nEvent == 1

        [nCont,nEvent] = dq.get_ode_sizes()
        assert nCont == 1
        assert nEvent == 0
Ejemplo n.º 4
0
    def test_get_name(self):
        bounce = FMUModelME1("bouncingBall.fmu",
                             os.path.join(file_path, "files", "FMUs", "XML",
                                          "ME1.0"),
                             _connect_dll=False)
        dq = FMUModelME1("dq.fmu",
                         os.path.join(file_path, "files", "FMUs", "XML",
                                      "ME1.0"),
                         _connect_dll=False)

        assert bounce.get_name() == 'bouncingBall'
        assert dq.get_name() == 'dq'
Ejemplo n.º 5
0
 def test_log_file_name(self):
     model = FMUModelME1("bouncingBall.fmu",
                         os.path.join(file_path, "files", "FMUs", "XML",
                                      "ME1.0"),
                         _connect_dll=False)
     assert os.path.exists("bouncingBall_log.txt")
     model = FMUModelME1("bouncingBall.fmu",
                         os.path.join(file_path, "files", "FMUs", "XML",
                                      "ME1.0"),
                         _connect_dll=False,
                         log_file_name="Test_log.txt")
     assert os.path.exists("Test_log.txt")
Ejemplo n.º 6
0
 def test_get_variable_description(self):
     model = FMUModelME1("CoupledClutches.fmu",
                         os.path.join(file_path, "files", "FMUs", "XML",
                                      "ME1.0"),
                         _connect_dll=False)
     assert model.get_variable_description(
         "J1.phi") == "Absolute rotation angle of component"
Ejemplo n.º 7
0
def furuta_dfo_cost(x):

    # We need to scale the inputs x down since they are scaled up 
    # versions of the parameters (x = scalefactor*[param1 param2])
    armFrictionCoefficient = x[0]/1e3
    pendulumFrictionCoefficient = x[1]/1e3
    
    model = FMUModelME1(os.path.join(curr_dir, 'files', 'FMUs', 'Furuta.fmu'))

    # Set new parameter values into the model 
    model.set('armFriction', armFrictionCoefficient)
    model.set('pendulumFriction', pendulumFrictionCoefficient)
    
    # Create options object and set verbosity to zero to disable printouts
    opts = model.simulate_options()
    opts['CVode_options']['verbosity'] = 50
    opts['ncp'] = 800
    opts['filter'] = ['armJoint.phi', 'pendulumJoint.phi'] 
    
    # Simulate model response with new parameter values
    res = model.simulate(start_time=0., final_time=40, options=opts)

    # Load simulation result
    phi_sim = res['armJoint.phi']
    theta_sim = res['pendulumJoint.phi']
    t_sim  = res['time']
    
    # Evaluate the objective function
    y_meas = N.vstack((phi_meas, theta_meas))
    y_sim = N.vstack((phi_sim, theta_sim))

    obj = dfo.quad_err_simple(t_meas, y_meas, t_sim, y_sim)

    return obj
Ejemplo n.º 8
0
    def test_unicode_description(self):
        full_path = os.path.join(file_path, "files", "FMUs", "XML", "ME1.0", "Description.fmu")
        model = FMUModelME1(full_path, _connect_dll=False)
        
        desc = model.get_variable_description("x")

        assert desc == "Test symbols '' ‘’"
Ejemplo n.º 9
0
 def test_get_fmi_options(self):
     """
     Test that simulate_options on an FMU returns the correct options
     class instance.
     """
     bounce = FMUModelME1("bouncingBall.fmu", os.path.join(file_path, "files", "FMUs", "XML", "ME1.0"), _connect_dll=False)
     assert isinstance(bounce.simulate_options(), fmi_algorithm_drivers.AssimuloFMIAlgOptions)
Ejemplo n.º 10
0
 def test_get_time_varying_variables_with_alias(self):
     model = FMUModelME1("Alias1.fmu", os.path.join(file_path, "files", "FMUs", "XML", "ME1.0"), _connect_dll=False)
     
     [r,i,b] = model.get_model_time_varying_value_references(filter="y*")
     
     assert len(r) == 1
     assert r[0] == model.get_variable_valueref("y")
Ejemplo n.º 11
0
    def test_get_erronous_nominals(self):
        model = FMUModelME1("NominalTest4.fmu",
                            os.path.join(file_path, "files", "FMUs", "XML",
                                         "ME1.0"),
                            _connect_dll=False)

        nose.tools.assert_almost_equal(model.get_variable_nominal("x"), 2.0)
        nose.tools.assert_almost_equal(model.get_variable_nominal("y"), 1.0)
Ejemplo n.º 12
0
    def test_loading_xml_me1(self):

        model = FMUModelME1("CoupledClutches.fmu",
                            os.path.join(file_path, "files", "FMUs", "XML",
                                         "ME1.0"),
                            _connect_dll=False)

        assert model.get_name() == "CoupledClutches", model.get_name()
Ejemplo n.º 13
0
    def setUp(self):
        """
        Sets up the test case.
        """
        fpath = os.path.join(path_to_mofiles, 'RLC_Circuit.mo')
        fmuname = compile_fmu('RLC_Circuit', fpath)

        self._model = FMUModelME1(fmuname)
Ejemplo n.º 14
0
    def test_default_experiment(self):
        model = FMUModelME1("CoupledClutches.fmu",
                            os.path.join(file_path, "files", "FMUs", "XML",
                                         "ME1.0"),
                            _connect_dll=False)

        assert np.abs(model.get_default_experiment_start_time()) < 1e-4
        assert np.abs(model.get_default_experiment_stop_time() - 1.5) < 1e-4
        assert np.abs(model.get_default_experiment_tolerance() - 0.0001) < 1e-4
Ejemplo n.º 15
0
 def test_get_time_varying_variables(self):
     model = FMUModelME1("RLC_Circuit.fmu", os.path.join(file_path, "files", "FMUs", "XML", "ME1.0"), _connect_dll=False)
     
     [r,i,b] = model.get_model_time_varying_value_references()
     [r_f, i_f, b_f] = model.get_model_time_varying_value_references(filter="*")
     
     assert len(r) == len(r_f)
     assert len(i) == len(i_f)
     assert len(b) == len(b_f)
Ejemplo n.º 16
0
    def test_get_variable_by_valueref(self):
        bounce = FMUModelME1("bouncingBall.fmu",
                             os.path.join(file_path, "files", "FMUs", "XML",
                                          "ME1.0"),
                             _connect_dll=False)
        assert "der(v)" == bounce.get_variable_by_valueref(3)
        assert "v" == bounce.get_variable_by_valueref(2)

        nose.tools.assert_raises(FMUException, bounce.get_variable_by_valueref,
                                 7)
Ejemplo n.º 17
0
    def test_get_scalar_variable(self):
        negated_alias = FMUModelME1("NegatedAlias.fmu", os.path.join(file_path, "files", "FMUs", "XML", "ME1.0"), _connect_dll=False)

        sc_x = negated_alias.get_scalar_variable("x")
        
        assert sc_x.name == "x"
        assert sc_x.value_reference >= 0
        assert sc_x.type == fmi.FMI_REAL
        assert sc_x.variability == fmi.FMI_CONTINUOUS
        assert sc_x.causality == fmi.FMI_INTERNAL
        assert sc_x.alias == fmi.FMI_NO_ALIAS

        nose.tools.assert_raises(FMUException, negated_alias.get_scalar_variable, "not_existing")
Ejemplo n.º 18
0
def run_demo(with_plots=True):
    
    # Choose starting point (initial estimation)
    x0 = N.array([0.012,0.002])

    # Choose lower and upper bounds (optional)
    lb = N.zeros(2)
    ub = x0 + 1e-2

    x_opt,f_opt,nbr_iters,nbr_fevals,solve_time = dfo.nelme_modified(furuta_dfo_cost,xstart=x0*1e3,
                                                            lb=lb,ub=ub*1e3,
                                                            x_tol=1e-3,f_tol=1e-2,debug=False)
    [armFrictionCoefficient_opt,pendulumFrictionCoefficient_opt] = x_opt/1e3
    
    # Load model
    model = FMUModelME1(os.path.join(curr_dir, 'files', 'FMUs', 'Furuta.fmu'), enable_logging=False)
    
    # Set optimal parameter values into the model
    model.set('armFriction', armFrictionCoefficient_opt)
    model.set('pendulumFriction', pendulumFrictionCoefficient_opt)
    
    opts = model.simulate_options()
    opts['filter'] = ['armJoint.phi', 'pendulumJoint.phi']
    
    # Simulate model response with optimal parameter values
    res = model.simulate(start_time=0., final_time=40)
    
    # Load optimal simulation result
    phi_opt = res['armJoint.phi']
    theta_opt = res['pendulumJoint.phi']
    t_opt  = res['time']
    
    assert N.abs(res.final('armJoint.phi') + 0.309)      < 3e-2
    assert N.abs(res.final('pendulumJoint.phi') - 3.130) < 3e-2
    assert N.abs(res.final('time') - 40.0)               < 1e-3
    
    if with_plots:
        plt.figure(1)
        plt.subplot(2,1,1)
        plt.plot(t_opt, theta_opt, linewidth=1, label='Simulation optimal parameters')
        plt.plot(t_meas, theta_meas, linewidth=1, label='Physical data')
        plt.legend()
        plt.subplot(2,1,2)
        plt.plot(t_opt, phi_opt, linewidth=1, label='Simulation optimal parameters')
        plt.plot(t_meas, phi_meas, linewidth=1, label='Physical data')
        plt.legend()
        plt.show()
Ejemplo n.º 19
0
 def setUp(self):
     """
     Sets up the test class.
     """
     self.fmu_name = compile_fmu(self._cpath, self._fpath)
     self.model = FMUModelME1(self.fmu_name)
Ejemplo n.º 20
0
 def setUp(self):
     """
     Sets up the test class.
     """
     self.fmu_name = compile_fmu(self._cpath, self._fpath,compiler_options={'compliance_as_warning':True, 'generate_runtime_option_parameters':False})
     self.model = FMUModelME1(self.fmu_name)
Ejemplo n.º 21
0
 def test_get_variable_nominal_valueref(self):
     bounce = FMUModelME1("bouncingBall.fmu", os.path.join(file_path, "files", "FMUs", "XML", "ME1.0"), _connect_dll=False)
     assert bounce.get_variable_nominal("v") == bounce.get_variable_nominal(valueref=2)