Changeset 13581


Ignore:
Timestamp:
Sep 24, 2019 8:44:55 AM (2 months ago)
Author:
Christian Andersson
Message:

Merged the latest trunk changes into the PyFMI release branch. Related to ticket:5849

Location:
PyFMI/branches/PyFMI-2.5.x
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • PyFMI/branches/PyFMI-2.5.x

  • PyFMI/branches/PyFMI-2.5.x/CHANGELOG

    r13362 r13581  
    11--- CHANGELOG ---
     2
     3--- PyFMI-trunk ---
     4    * Added option to specify if the variable descriptions should be
     5      stored or not in the result file (ticket:5846)
     6    * Fixed issue with estimating directional derivatives when the
     7      structure info is not used and the matrix has zero dim (ticket:5836)
    28
    39--- PyFMI-2.5.4 ---
  • PyFMI/branches/PyFMI-2.5.x/setup.py

    r13363 r13581  
    156156    if not x.find('--version'):
    157157        VERSION = x[10:]
     158        copy_args.remove(x)
     159    if not x.find('--debug'):
     160        if x[8:].upper() == "TRUE":
     161            debug_flag = True
     162        else:
     163            debug_flag = False
    158164        copy_args.remove(x)
    159165   
  • PyFMI/branches/PyFMI-2.5.x/src/common

  • PyFMI/branches/PyFMI-2.5.x/src/common/io.py

    r13031 r13581  
    17261726
    17271727        f.write('\n')
     1728       
     1729        if not opts["result_store_variable_description"]:
     1730            max_desc_length = 0
     1731            descriptions    = [[0,""] for d in descriptions]
     1732            descs_sens      = ["" for d in descs_sens]
    17281733
    17291734        # Write descriptions       
     
    21352140        len_name_items = len(sorted_vars)+1
    21362141        len_desc_items = len_name_items
    2137         """
    2138         name_data = ["time"] + [var.name for var in sorted_vars]
    2139         desc_data = ["Time in [s]"] + [var.description for var in sorted_vars]
    2140        
    2141         len_name_data, name_data = fmi_util.convert_str_list(name_data)
    2142         len_desc_data, desc_data = fmi_util.convert_str_list(desc_data)
    2143         """
     2142       
    21442143        len_name_data, name_data, len_desc_data, desc_data = fmi_util.convert_sorted_vars_name_desc(sorted_vars)
    21452144       
    21462145        self._write_header("name", len_name_data, len_name_items, "char")
    21472146        self.dump_native_data(name_data)
     2147       
     2148        if not opts["result_store_variable_description"]:
     2149            len_desc_data = 1
     2150            desc_data = encode(" "*len_desc_items)
     2151       
    21482152        self._write_header("description", len_desc_data, len_desc_items, "char")
    21492153        self.dump_native_data(desc_data)
  • PyFMI/branches/PyFMI-2.5.x/src/pyfmi

  • PyFMI/branches/PyFMI-2.5.x/src/pyfmi/fmi.pyx

    r13365 r13581  
    533533            self.file_object = None
    534534
    535     cdef _logger(self, FMIL.jm_string module, int log_level, FMIL.jm_string message) with gil:
     535    cdef _logger(self, FMIL.jm_string c_module, int log_level, FMIL.jm_string c_message) with gil:
     536        module  = decode(c_module)
     537        message = decode(c_message)
     538       
    536539        if self._additional_logger:
    537540            self._additional_logger(module, log_level, message)
     
    78027805                A = output_matrix
    78037806           
     7807            if len_v == 0 or len_f == 0:
     7808                return A
     7809           
    78047810            dfpert = N.zeros(len_f, dtype = N.double)
    78057811            for i in range(len_v):
  • PyFMI/branches/PyFMI-2.5.x/src/pyfmi/fmi_algorithm_drivers.py

    r13066 r13581  
    116116            into memory after the simulation finishes.
    117117            Default: True
     118           
     119        result_store_variable_description --
     120            Determines if the description for the variables should be
     121            stored in the result file or not. Only impacts the result
     122            file formats that supports storing the variable description
     123            ("file" and "binary").
     124            Default: True
    118125
    119126        filter --
     
    169176            'result_handler': None,
    170177            'return_result': True,
     178            'result_store_variable_description': True,
    171179            'filter':None,
    172180            'extra_equations':None,
     
    616624            into memory after the simulation finishes.
    617625            Default: True
     626       
     627        result_store_variable_description --
     628            Determines if the description for the variables should be
     629            stored in the result file or not. Only impacts the result
     630            file formats that supports storing the variable description
     631            ("file" and "binary").
     632            Default: True
    618633           
    619634        time_limit --
     
    644659            'result_handling':"binary",
    645660            'result_handler': None,
     661            'result_store_variable_description': True,
    646662            'return_result': True,
    647663            'time_limit': None,
  • PyFMI/branches/PyFMI-2.5.x/tests/test_fmi.py

    r13365 r13581  
    458458           
    459459class Test_FMUModelME2:
     460   
     461    @testattr(stddist = True)
     462    def test_estimate_directional_derivatives_without_structure_info(self):
     463        full_path = os.path.join(file_path, "files", "FMUs", "XML", "ME2.0", "BouncingBall.fmu")
     464        model = Dummy_FMUModelME2([], full_path, _connect_dll=False)
     465       
     466        def f(*args, **kwargs):
     467            derh = model.values[model.variables["v"].value_reference]
     468            derv = -9.81
     469            model.values[model.variables["der(h)"].value_reference] = derh
     470            return np.array([derh, derv])
     471        model.get_derivatives = f
     472       
     473        model.initialize()
     474        model.event_update()
     475        model.enter_continuous_time_mode()
     476       
     477        [As, Bs, Cs, Ds] = model.get_state_space_representation(use_structure_info=False)
     478        [A, B, C, D] = model.get_state_space_representation()
     479       
     480        assert As.shape == A.shape, str(As.shape)+' '+str(A.shape)
     481        assert Bs.shape == B.shape, str(Bs.shape)+' '+str(B.shape)
     482        assert Cs.shape == C.shape, str(Cs.shape)+' '+str(C.shape)
     483        assert Ds.shape == D.shape, str(Ds.shape)+' '+str(D.shape)
     484       
     485        assert np.allclose(As, A.toarray()), str(As)+' '+str(A.toarray())
     486        assert np.allclose(Bs, B.toarray()), str(Bs)+' '+str(B.toarray())
     487        assert np.allclose(Cs, C.toarray()), str(Cs)+' '+str(C.toarray())
     488        assert np.allclose(Ds, D.toarray()), str(Ds)+' '+str(D.toarray())
     489   
    460490    @testattr(stddist = True)
    461491    def test_estimate_directional_derivatives_BCD(self):
     
    484514            B = func(use_structure_info=True, output_matrix=A)
    485515            assert A is B #Test that the returned matrix is actually the same as the input
    486             np.allclose(A.toarray(),B.toarray())
     516            assert np.allclose(A.toarray(),B.toarray())
    487517            A = func(use_structure_info=False)
    488518            B = func(use_structure_info=False, output_matrix=A)
    489519            assert A is B
    490             np.allclose(A,B)
     520            assert np.allclose(A,B)
    491521            C = func(use_structure_info=True, output_matrix=A)
    492522            assert A is not C
    493             np.allclose(C.toarray(), A)
     523            assert np.allclose(C.toarray(), A)
    494524            D = func(use_structure_info=False, output_matrix=C)
    495525            assert D is not C
    496             np.allclose(D, C.toarray())
     526            assert np.allclose(D, C.toarray())
    497527       
    498528        B = model._get_B(use_structure_info=True)
     
    500530        D = model._get_D(use_structure_info=True)
    501531       
    502         np.allclose(B.toarray(), np.array([[0.0],[0.0]]))
    503         np.allclose(C.toarray(), np.array([[0.0, 0.0],[0.0, 1.0], [1.0, 0.0]]))
    504         np.allclose(D.toarray(), np.array([[-1.0],[0.0], [1.0]]))
     532        assert np.allclose(B.toarray(), np.array([[0.0],[0.0]]))
     533        assert np.allclose(C.toarray(), np.array([[0.0, 0.0],[0.0, 1.0], [1.0, 0.0]]))
     534        assert np.allclose(D.toarray(), np.array([[-1.0],[0.0], [1.0]]))
    505535       
    506536        B = model._get_B(use_structure_info=False)
     
    508538        D = model._get_D(use_structure_info=False)
    509539       
    510         np.allclose(B, np.array([[0.0],[0.0]]))
    511         np.allclose(C, np.array([[0.0, 0.0],[0.0, 1.0], [1.0, 0.0]]))
    512         np.allclose(D, np.array([[-1.0],[0.0], [1.0]]))
     540        assert np.allclose(B, np.array([[0.0],[0.0]]))
     541        assert np.allclose(C, np.array([[0.0, 0.0],[0.0, 1.0], [1.0, 0.0]]))
     542        assert np.allclose(D, np.array([[-1.0],[0.0], [1.0]]))
    513543       
    514544    @testattr(stddist = True)
     
    535565        [state_dep, input_dep] = model.get_output_dependencies()
    536566       
    537         assert len(state_dep.keys()) == 0
    538         assert len(input_dep.keys()) == 0
     567        assert len(state_dep.keys()) == 0, len(state_dep.keys())
     568        assert len(input_dep.keys()) == 0, len(input_dep.keys())
    539569   
    540570    @testattr(stddist = True)
     
    546576        [state_dep, input_dep] = model.get_derivatives_dependencies()
    547577       
    548         assert len(state_dep.keys()) == 0
    549         assert len(input_dep.keys()) == 0
     578        assert len(state_dep.keys()) == 0, len(state_dep.keys())
     579        assert len(input_dep.keys()) == 0, len(input_dep.keys())
    550580   
    551581    @testattr(stddist = True)
     
    566596        model = FMUModelME2("CoupledClutches.fmu", os.path.join(file_path, "files", "FMUs", "XML", "ME2.0"), _connect_dll=False)
    567597       
    568         assert model.get_variable_unit("J1.w") == "rad/s"
    569         assert model.get_variable_unit("J1.phi") == "rad"
     598        assert model.get_variable_unit("J1.w") == "rad/s", model.get_variable_unit("J1.w")
     599        assert model.get_variable_unit("J1.phi") == "rad", model.get_variable_unit("J1.phi")
    570600       
    571601        nose.tools.assert_raises(FMUException, model.get_variable_unit, "clutch1.useHeatPort")
     
    577607        model = FMUModelME2("CoupledClutches.fmu", os.path.join(file_path, "files", "FMUs", "XML", "ME2.0"), _connect_dll=False)
    578608       
    579         assert model.get_variable_display_unit("J1.phi") == "deg"
     609        assert model.get_variable_display_unit("J1.phi") == "deg", model.get_variable_display_unit("J1.phi")
    580610        nose.tools.assert_raises(FMUException, model.get_variable_display_unit, "J1.w")
    581611
     
    595625       
    596626        enum = model.get_variable_declared_type("x")
    597         assert len(enum.items.keys()) == 2
     627        assert len(enum.items.keys()) == 2, len(enum.items.keys())
    598628        enum = model.get_variable_declared_type("home")
    599         assert len(enum.items.keys()) == 4
     629        assert len(enum.items.keys()) == 4, len(enum.items.keys())
    600630       
    601631        nose.tools.assert_raises(FMUException, model.get_variable_declared_type, "z")
     
    635665        for var in vars:
    636666            [r,i,b] = model.get_model_time_varying_value_references(filter=var)
    637             assert len(r) == 1
     667            assert len(r) == 1, len(r)
    638668       
    639669        [r,i,b] = model.get_model_time_varying_value_references(filter=list(vars.keys()))
    640         assert len(r) == 1
     670        assert len(r) == 1, len(r)
    641671   
    642672    @testattr(stddist = True)
     
    704734        sc_x = negated_alias.get_scalar_variable("x")
    705735       
    706         assert sc_x.name == "x"
    707         assert sc_x.value_reference >= 0
    708         assert sc_x.type == fmi.FMI2_REAL
    709         assert sc_x.variability == fmi.FMI2_CONTINUOUS
    710         assert sc_x.causality == fmi.FMI2_LOCAL
     736        assert sc_x.name == "x", sc_x.name
     737        assert sc_x.value_reference >= 0, sc_x.value_reference
     738        assert sc_x.type == fmi.FMI2_REAL, sc_x.type
     739        assert sc_x.variability == fmi.FMI2_CONTINUOUS, sc_x.variability
     740        assert sc_x.causality == fmi.FMI2_LOCAL, sc_x.causality
    711741
    712742        nose.tools.assert_raises(FMUException, negated_alias.get_scalar_variable, "not_existing")
     
    725755        model = FMUModelME1("CoupledClutches.fmu", os.path.join(file_path, "files", "FMUs", "XML", "ME1.0"), _connect_dll=False)
    726756       
    727         assert model.get_name() == "CoupledClutches"
     757        assert model.get_name() == "CoupledClutches", model.get_name()
    728758
    729759    @testattr(stddist = True)
     
    732762        model = FMUModelCS1("CoupledClutches.fmu", os.path.join(file_path, "files", "FMUs", "XML", "CS1.0"), _connect_dll=False)
    733763       
    734         assert model.get_name() == "CoupledClutches"
     764        assert model.get_name() == "CoupledClutches", model.get_name()
    735765       
    736766    @testattr(stddist = True)
     
    739769        model = FMUModelME2("CoupledClutches.fmu", os.path.join(file_path, "files", "FMUs", "XML", "ME2.0"), _connect_dll=False)
    740770       
    741         assert model.get_name() == "CoupledClutches"
     771        assert model.get_name() == "CoupledClutches", model.get_name()
    742772       
    743773    @testattr(stddist = True)
     
    746776        model = FMUModelCS2("CoupledClutches.fmu", os.path.join(file_path, "files", "FMUs", "XML", "CS2.0"), _connect_dll=False)
    747777       
    748         assert model.get_name() == "CoupledClutches"
     778        assert model.get_name() == "CoupledClutches", model.get_name()
  • PyFMI/branches/PyFMI-2.5.x/tests/test_io.py

    r13031 r13581  
    151151   
    152152    @testattr(stddist = True)
     153    def test_description_not_stored(self):
     154        model = Dummy_FMUModelME1([], "CoupledClutches.fmu", os.path.join(file_path, "files", "FMUs", "XML", "ME1.0"), _connect_dll=False)
     155        model.initialize()
     156       
     157        opts = model.simulate_options()
     158        opts["result_store_variable_description"] = False
     159       
     160        result_writer = ResultHandlerFile(model)
     161        result_writer.set_options(opts)
     162        result_writer.simulation_start()
     163        result_writer.initialize_complete()
     164        result_writer.integration_point()
     165        result_writer.simulation_end()
     166       
     167        res = ResultDymolaTextual('CoupledClutches_result.txt')
     168       
     169        assert res.description[res.get_variable_index("J1.phi")] == "", "Description is not empty, " + res.description[res.get_variable_index("J1.phi")]
     170   
     171    @testattr(stddist = True)
    153172    def test_get_description_unicode(self):
    154173        model = Dummy_FMUModelME1([], "Description.fmu", os.path.join(file_path, "files", "FMUs", "XML", "ME1.0"), _connect_dll=False)
     
    428447   
    429448    @testattr(stddist = True)
     449    def test_description_not_stored(self):
     450        model = Dummy_FMUModelME1([], "CoupledClutches.fmu", os.path.join(file_path, "files", "FMUs", "XML", "ME1.0"), _connect_dll=False)
     451        model.initialize()
     452       
     453        opts = model.simulate_options()
     454        opts["result_store_variable_description"] = False
     455       
     456        result_writer = ResultHandlerBinaryFile(model)
     457        result_writer.set_options(opts)
     458        result_writer.simulation_start()
     459        result_writer.initialize_complete()
     460        result_writer.integration_point()
     461        result_writer.simulation_end()
     462       
     463        res = ResultDymolaBinary('CoupledClutches_result.mat')
     464       
     465        assert res.description[res.get_variable_index("J1.phi")] == "", "Description is not empty, " + res.description[res.get_variable_index("J1.phi")]
     466   
     467    @testattr(stddist = True)
    430468    def test_read_all_variables(self):
    431469        res = ResultDymolaBinary(os.path.join(file_path, "files", "Results", "DoublePendulum.mat"))
Note: See TracChangeset for help on using the changeset viewer.