Changeset 13031


Ignore:
Timestamp:
Jul 1, 2019 11:41:06 AM (5 months ago)
Author:
Christian Andersson
Message:

Merged latests changes from trunk into the release branch. Related to ticket:5799

Location:
PyFMI/branches/PyFMI-2.5.x
Files:
19 edited
8 copied

Legend:

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

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

    r12936 r13031  
    11--- CHANGELOG ---
     2
     3--- PyFMI-2.5.2 ---
     4    * Improved relative imports of Assimulo dependent classes (ticket:5798)
     5    * Fixed unicode symbols in result files (ticket:5797)
    26
    37--- PyFMI-2.5.1 ---
  • PyFMI/branches/PyFMI-2.5.x/doc/sphinx/source/changelog.rst

    r12936 r13031  
    33Changelog
    44==========
     5
     6--- PyFMI-2.5.2 ---
     7    * Improved relative imports of Assimulo dependent classes (ticket:5798)
     8    * Fixed unicode symbols in result files (ticket:5797)
    59
    610--- PyFMI-2.5.1 ---
  • PyFMI/branches/PyFMI-2.5.x/doc/sphinx/source/conf.py

    r12936 r13031  
    5252#
    5353# The short X.Y version.
    54 version = '2.5.1'
     54version = '2.5.2'
    5555# The full version, including alpha/beta/rc tags.
    56 release = '2.5.1'
     56release = '2.5.2'
    5757
    5858# The language for content autogenerated by Sphinx. Refer to documentation
     
    109109# The name for this set of Sphinx documents.  If None, it defaults to
    110110# "<project> v<release> documentation".
    111 html_title = "PyFMI 2.5.1 documentation"
     111html_title = "PyFMI 2.5.2 documentation"
    112112
    113113# A shorter title for the navigation bar.  Default is the same as html_title.
  • PyFMI/branches/PyFMI-2.5.x/setup.py

    r12936 r13031  
    4545AUTHOR = "Modelon AB"
    4646AUTHOR_EMAIL = ""
    47 VERSION = "2.5.1"
     47VERSION = "2.5.2"
    4848LICENSE = "LGPL"
    4949URL = "https://jmodelica.org/pyfmi"
  • PyFMI/branches/PyFMI-2.5.x/src/common

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

    r12936 r13031  
    2727
    2828if python3_flag:
    29     import codecs
    3029    def encode(x):
    3130        if isinstance(x, str):
    32             return codecs.latin_1_encode(x)[0]
     31            return x.encode()
    3332        else:
    3433            return x
  • PyFMI/branches/PyFMI-2.5.x/src/common/core.py

    r11463 r13031  
    522522        self._ordinate = ordinate
    523523        self._n = N.size(abscissa)
     524        self._n_ordinate = N.size(self.ordinate,1)
    524525        self._x0 = abscissa[0]
    525526        self._xf = abscissa[-1]
     
    591592            corresponding to the argument x.
    592593        """       
    593         y = N.zeros([N.size(x),N.size(self.ordinate,1)])
    594         for i in range(N.size(y,1)):
     594        y = N.zeros([N.size(x),self._n_ordinate])
     595        for i in range(self._n_ordinate):
    595596            y[:,i] = N.interp(x,self.abscissa,self.ordinate[:,i])
    596597        return y
  • PyFMI/branches/PyFMI-2.5.x/src/common/io.py

    r12936 r13031  
    21352135        len_name_items = len(sorted_vars)+1
    21362136        len_desc_items = len_name_items
    2137        
     2137        """
    21382138        name_data = ["time"] + [var.name for var in sorted_vars]
    21392139        desc_data = ["Time in [s]"] + [var.description for var in sorted_vars]
     
    21412141        len_name_data, name_data = fmi_util.convert_str_list(name_data)
    21422142        len_desc_data, desc_data = fmi_util.convert_str_list(desc_data)
     2143        """
     2144        len_name_data, name_data, len_desc_data, desc_data = fmi_util.convert_sorted_vars_name_desc(sorted_vars)
    21432145       
    21442146        self._write_header("name", len_name_data, len_name_items, "char")
  • PyFMI/branches/PyFMI-2.5.x/src/pyfmi

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

    r12947 r13031  
    1818Module containing the FMI interface Python wrappers.
    1919"""
     20"""
     21For profiling:
     22# cython: linetrace=True
     23# cython: binding=True
     24# distutils: define_macros=CYTHON_TRACE_NOGIL=1
     25"""
    2026import os
    2127import sys
     
    3743#from pyfmi.common.core cimport BaseModel
    3844
    39 from pyfmi.common import python3_flag, encode, decode
    40 from pyfmi.fmi_util import cpr_seed, enable_caching
     45#from pyfmi.common import python3_flag, encode, decode
     46from pyfmi.fmi_util import cpr_seed, enable_caching, python3_flag
     47from pyfmi.fmi_util cimport encode, decode
    4148
    4249int   = N.int32
     
    21862193            if selected_filter:
    21872194                for j in range(length_filter):
    2188                     #if re.match(filter_list[j], name):
    21892195                    if filter_list[j].match(name):
    21902196                        break
  • PyFMI/branches/PyFMI-2.5.x/src/pyfmi/fmi_algorithm_drivers.py

    r12936 r13031  
    3636from timeit import default_timer as timer
    3737
    38 try:
    39     import assimulo
    40     assimulo_present = True
    41 except:
    42     logging.warning(
    43         'Could not load Assimulo module. Check pyfmi.check_packages()')
    44     assimulo_present = False
    45 
    46 if assimulo_present:
    47     from pyfmi.simulation.assimulo_interface import FMIODE, FMIODESENS, FMIODE2, FMIODESENS2
    48     from pyfmi.simulation.assimulo_interface import write_data
    49     import assimulo.solvers as solvers
    50 
    5138default_int = int
    5239int = N.int32
     
    156143            The absolute tolerance.
    157144            Default: "Default" (rtol*0.01*(nominal values of the continuous states))
     145       
     146        maxh    --
     147            The maximum step-size allowed to be used by the solver.
     148            Default: 0.0 (i.e. no limit on the step-size)
    158149
    159150        discr   --
     
    181172            'extra_equations':None,
    182173            'CVode_options':{'discr':'BDF','iter':'Newton',
    183                             'atol':"Default",'rtol':"Default",'external_event_detection':False},
     174                            'atol':"Default",'rtol':"Default","maxh":0.0,'external_event_detection':False},
    184175            'Radau5ODE_options':{'atol':"Default",'rtol':"Default"},
    185176            'RungeKutta34_options':{'atol':"Default",'rtol':"Default"},
     
    234225        self.timings = {}
    235226        self.time_start_total = timer()
    236 
    237         if not assimulo_present:
     227       
     228        try:
     229            import assimulo
     230        except:
    238231            raise fmi.FMUException(
    239232                'Could not find Assimulo package. Check pyfmi.check_packages()')
     233               
     234        # import Assimulo dependent classes
     235        from pyfmi.simulation.assimulo_interface import FMIODE, FMIODESENS, FMIODE2, FMIODESENS2
    240236
    241237        # set start time, final time and input trajectory
     
    418414
    419415        # solver
     416        import assimulo.solvers as solvers
     417       
    420418        solver = self.options['solver']
    421419        if hasattr(solvers, solver):
  • PyFMI/branches/PyFMI-2.5.x/src/pyfmi/fmi_extended.pyx

    r12966 r13031  
    2424import re
    2525from collections import OrderedDict
    26 from pyfmi.simulation.assimulo_interface import FMIODE
    2726
    2827import numpy as N
     
    480479       
    481480        #Create an assimulo problem
     481        from pyfmi.simulation.assimulo_interface import FMIODE
    482482        self._explicit_problem = FMIODE(self, input=None, result_file_name = '',
    483483                                        with_jacobian=False, start_time=start_time)
  • PyFMI/branches/PyFMI-2.5.x/src/pyfmi/fmi_util.pyx

    r12947 r13031  
    3434import sys
    3535
    36 from pyfmi.common import python3_flag, encode, decode
     36python3_flag = True if sys.hexversion > 0x03000000 else False
     37
     38cpdef decode(x):
     39    if python3_flag:
     40        return x.decode()
     41    else:
     42        return x
     43
     44cpdef encode(x):
     45    if python3_flag:
     46        if isinstance(x, str):
     47            return x.encode()
     48        else:
     49            return x
     50    else:
     51        return x.encode("utf-8")
    3752
    3853def enable_caching(obj):
     
    300315   
    301316    for i in range(items):
     317        data[i] = encode(data[i])
    302318        j = len(data[i])
    303319        if j+1 > length:
     
    307323   
    308324    for i in range(items):
    309         py_byte_string = encode(data[i])#.encode("latin-1")
    310         tmp = py_byte_string
     325        tmp = data[i]
    311326        tmp_length = len(tmp)
    312327        k = i*length
     
    319334    FMIL.free(output)
    320335   
    321     return length, py_string#.encode("latin-1")
     336    return length, py_string
     337
     338cpdef convert_sorted_vars_name_desc(list sorted_vars):
     339    cdef int items = len(sorted_vars)
     340    cdef int i, name_length_trial, desc_length_trial, kd, kn
     341    cdef list desc = [encode("Time in [s]")]
     342    cdef list name = [encode("time")]
     343    cdef int name_length = len(name[0])
     344    cdef int desc_length = len(desc[0])
     345    cdef char *desc_output
     346    cdef char *name_output
     347    cdef char *ctmp_name
     348    cdef char *ctmp_desc
     349   
     350    for i in range(items):
     351        var = sorted_vars[i]
     352        tmp_name = encode(var.name)
     353        tmp_desc = encode(var.description)
     354        name.append(tmp_name)
     355        desc.append(tmp_desc)
     356       
     357        name_length_trial = len(tmp_name)
     358        desc_length_trial = len(tmp_desc)
     359       
     360        if name_length_trial+1 > name_length:
     361             name_length = name_length_trial + 1
     362        if desc_length_trial+1 > desc_length:
     363             desc_length = desc_length_trial + 1
     364   
     365    desc_output = <char*>FMIL.calloc((items+1)*desc_length,sizeof(char))
     366    name_output = <char*>FMIL.calloc((items+1)*name_length,sizeof(char))
     367   
     368    for i in range(items+1):
     369        ctmp_name = name[i]
     370        ctmp_desc = desc[i]
     371       
     372        name_length_trial = len(ctmp_name)
     373        desc_length_trial = len(ctmp_desc)
     374        kn = i*name_length
     375        kd = i*desc_length
     376       
     377        FMIL.memcpy(&name_output[kn], ctmp_name, name_length_trial)
     378        FMIL.memcpy(&desc_output[kd], ctmp_desc, desc_length_trial)
     379   
     380    py_desc_string = desc_output[:(items+1)*desc_length]
     381    py_name_string = name_output[:(items+1)*name_length]
     382   
     383    FMIL.free(name_output)
     384    FMIL.free(desc_output)
     385   
     386    return name_length, py_name_string, desc_length, py_desc_string
     387   
    322388
    323389cpdef convert_scalarvariable_name_to_str(list data):
  • PyFMI/branches/PyFMI-2.5.x/src/pyfmi/master.pyx

    r12945 r13031  
    1717
    1818import pyfmi.fmi as fmi
    19 from pyfmi.fmi_algorithm_drivers import FMICSAlgOptions, AssimuloSimResult
    2019from pyfmi.common.algorithm_drivers import OptionBase, InvalidAlgorithmOptionException
    2120from pyfmi.common.io import ResultDymolaTextual, ResultHandlerFile, ResultHandlerDummy
     
    10751074            else:
    10761075                raise fmi.FMUException("Currently only writing result to file and none is supported.")
     1076            from pyfmi.fmi_algorithm_drivers import FMICSAlgOptions
    10771077            local_opts = FMICSAlgOptions()
    10781078            local_opts["result_file_name"] = model.get_identifier()+'_'+str(i)+'_result.txt'
     
    13931393        for i,model in enumerate(self.models):
    13941394            if opts["result_handling"] == "file":
     1395                from pyfmi.fmi_algorithm_drivers import AssimuloSimResult
    13951396                dym_textual = ResultDymolaTextual(model.get_identifier()+'_'+str(i)+'_result.txt')
    13961397                res[i] = AssimuloSimResult(model, model.get_identifier()+'_'+str(i)+'_result.txt', None, dym_textual, None)
  • PyFMI/branches/PyFMI-2.5.x/tests/test_fmi.py

    r12966 r13031  
    246246
    247247class Test_FMUModelBase:
     248    @testattr(stddist = True)
     249    def test_unicode_description(self):
     250        full_path = os.path.join(file_path, "files", "FMUs", "XML", "ME1.0", "Description.fmu")
     251        model = FMUModelME1(full_path, _connect_dll=False)
     252       
     253        desc = model.get_variable_description("x")
     254
     255        assert desc == "Test symbols '' ‘’"
    248256   
    249257    @testattr(stddist = True)
     
    393401            A = func(use_structure_info=False)
    394402            B = func(use_structure_info=False, output_matrix=A)
    395             print(A)
    396403            assert A is B
    397404            np.allclose(A,B)
     
    488495
    489496class Test_FMUModelBase2:
     497    @testattr(stddist = True)
     498    def test_unicode_description(self):
     499        full_path = os.path.join(file_path, "files", "FMUs", "XML", "ME2.0", "Description.fmu")
     500        model = FMUModelME2(full_path, _connect_dll=False)
     501       
     502        desc = model.get_variable_description("x")
     503
     504        assert desc == "Test symbols '' ‘’"
    490505   
    491506    @testattr(stddist = True)
  • PyFMI/branches/PyFMI-2.5.x/tests/test_fmi_master.py

    r12969 r13031  
    2121
    2222from pyfmi import testattr
    23 from pyfmi.fmi import FMUModel, FMUException, FMUModelME1, FMUModelCS1, load_fmu, FMUModelCS2, FMUModelME2, PyEventInfo
    24 from pyfmi.fmi_coupled import CoupledFMUModelME2
    25 from pyfmi.fmi_extended import FMUModelME1Extended
     23from pyfmi.fmi import FMUModel, FMUException, FMUModelME1, FMUModelCS1, load_fmu, FMUModelCS2, FMUModelME2
    2624import pyfmi.fmi_util as fmi_util
    2725import pyfmi.fmi as fmi
    28 import pyfmi.fmi_algorithm_drivers as fmi_algorithm_drivers
    2926from pyfmi import Master
    30 from pyfmi.tests.test_util import Dummy_FMUModelME2
    31 from pyfmi.common.io import ResultHandler
     27from pyfmi.tests.test_util import Dummy_FMUModelME2, Dummy_FMUModelCS2
    3228
    3329file_path = os.path.dirname(os.path.abspath(__file__))
     
    3531cs2_xml_path = os.path.join(file_path, "files", "FMUs", "XML", "CS2.0")
    3632me2_xml_path = os.path.join(file_path, "files", "FMUs", "XML", "ME2.0")
     33
     34import warnings
     35warnings.filterwarnings("ignore")
    3736
    3837class Test_Master:
     
    8483        model_sub1 = FMUModelCS2("LinearStability.SubSystem1.fmu", cs2_xml_path, _connect_dll=False)
    8584        model_sub2 = FMUModelCS2("LinearStability.SubSystem2.fmu", cs2_xml_path, _connect_dll=False)
    86        
     85        
    8786        models = [model_sub1, model_sub2]
    8887        connections = [(model_sub1,"y1",model_sub2,"u2"),
     
    101100        sim = Master(models, connections)
    102101        assert not sim.algebraic_loops
     102   
     103    @testattr(stddist = True)
     104    def test_basic_simulation(self):
     105        model_sub1 = Dummy_FMUModelCS2([], "LinearCoSimulation_LinearSubSystem1.fmu", cs2_xml_path, _connect_dll=False)
     106        model_sub2 = Dummy_FMUModelCS2([], "LinearCoSimulation_LinearSubSystem2.fmu", cs2_xml_path, _connect_dll=False)
     107       
     108        a1 = model_sub1.values[model_sub1.get_variable_valueref("a1")]
     109        b1 = model_sub1.values[model_sub1.get_variable_valueref("b1")]
     110        c1 = model_sub1.values[model_sub1.get_variable_valueref("c1")]
     111        d1 = model_sub1.values[model_sub1.get_variable_valueref("d1")]
     112       
     113        a2 = model_sub2.values[model_sub2.get_variable_valueref("a2")]
     114        b2 = model_sub2.values[model_sub2.get_variable_valueref("b2")]
     115        c2 = model_sub2.values[model_sub2.get_variable_valueref("c2")]
     116        d2 = model_sub2.values[model_sub2.get_variable_valueref("d2")]
     117       
     118        def do_step1(current_t, step_size, new_step=True):
     119            u1 = model_sub1.values[model_sub1.get_variable_valueref("u1")]
     120           
     121            model_sub1.continuous_states = 1.0/a1*(np.exp(a1*step_size)-1.0)*b1*u1+np.exp(a1*step_size)*model_sub1.continuous_states
     122            model_sub1.values[model_sub1.get_variable_valueref("y1")] = c1*model_sub1.continuous_states+d1*u1
     123            model_sub1.completed_integrator_step()
     124            return 0
     125       
     126        def do_step2(current_t, step_size, new_step=True):
     127            u2 = model_sub2.values[model_sub2.get_variable_valueref("u2")]
     128           
     129            model_sub2.continuous_states = 1.0/a2*(np.exp(a2*step_size)-1.0)*b2*u2+np.exp(a2*step_size)*model_sub2.continuous_states
     130            model_sub2.values[model_sub2.get_variable_valueref("y2")] = c2*model_sub2.continuous_states+d2*u2
     131            model_sub2.completed_integrator_step()
     132            return 0
     133           
     134        model_sub1.do_step = do_step1
     135        model_sub2.do_step = do_step2
     136       
     137        models = [model_sub1, model_sub2]
     138        connections = [(model_sub1,"y1",model_sub2,"u2"),
     139                   (model_sub2,"y2",model_sub1,"u1")]
     140                   
     141        master = Master(models, connections)
     142       
     143        opts = master.simulate_options()
     144        opts["step_size"] = 0.0005
     145       
     146        res = master.simulate(options=opts)
     147       
     148        nose.tools.assert_almost_equal(res[model_sub1].final("x1"), 0.0859764038708439, 3)
     149        nose.tools.assert_almost_equal(res[model_sub2].final("x2"), 0.008392664839635064, 4)
     150   
     151    @testattr(stddist = True)
     152    def test_unstable_simulation(self):
     153        model_sub1 = Dummy_FMUModelCS2([], "LinearCoSimulation_LinearSubSystem1.fmu", cs2_xml_path, _connect_dll=False)
     154        model_sub2 = Dummy_FMUModelCS2([], "LinearCoSimulation_LinearSubSystem2.fmu", cs2_xml_path, _connect_dll=False)
     155       
     156        model_sub2.set("d2", 1.1) #Coupled system becomes unstable
     157       
     158        a1 = model_sub1.values[model_sub1.get_variable_valueref("a1")]
     159        b1 = model_sub1.values[model_sub1.get_variable_valueref("b1")]
     160        c1 = model_sub1.values[model_sub1.get_variable_valueref("c1")]
     161        d1 = model_sub1.values[model_sub1.get_variable_valueref("d1")]
     162       
     163        a2 = model_sub2.values[model_sub2.get_variable_valueref("a2")]
     164        b2 = model_sub2.values[model_sub2.get_variable_valueref("b2")]
     165        c2 = model_sub2.values[model_sub2.get_variable_valueref("c2")]
     166        d2 = model_sub2.values[model_sub2.get_variable_valueref("d2")]
     167       
     168        def do_step1(current_t, step_size, new_step=True):
     169            u1 = model_sub1.values[model_sub1.get_variable_valueref("u1")]
     170           
     171            model_sub1.continuous_states = 1.0/a1*(np.exp(a1*step_size)-1.0)*b1*u1+np.exp(a1*step_size)*model_sub1.continuous_states
     172            model_sub1.values[model_sub1.get_variable_valueref("y1")] = c1*model_sub1.continuous_states+d1*u1
     173            model_sub1.completed_integrator_step()
     174            return 0
     175       
     176        def do_step2(current_t, step_size, new_step=True):
     177            u2 = model_sub2.values[model_sub2.get_variable_valueref("u2")]
     178           
     179            model_sub2.continuous_states = 1.0/a2*(np.exp(a2*step_size)-1.0)*b2*u2+np.exp(a2*step_size)*model_sub2.continuous_states
     180            model_sub2.values[model_sub2.get_variable_valueref("y2")] = c2*model_sub2.continuous_states+d2*u2
     181            model_sub2.completed_integrator_step()
     182            return 0
     183           
     184        model_sub1.do_step = do_step1
     185        model_sub2.do_step = do_step2
     186       
     187        models = [model_sub1, model_sub2]
     188        connections = [(model_sub1,"y1",model_sub2,"u2"),
     189                   (model_sub2,"y2",model_sub1,"u1")]
     190                   
     191        master = Master(models, connections)
     192       
     193        opts = master.simulate_options()
     194        opts["step_size"] = 0.0005
     195       
     196        res = master.simulate(final_time=0.3, options=opts)
     197       
     198        assert abs(res[model_sub1].final("x1")) > 100
     199        assert abs(res[model_sub2].final("x2")) > 100
    103200
    104201
     
    122219        cls.linear_sub2_dir = compile_fmu("LinearCoSimulation.LinearSubSystem2", file_name, target="cs", version="2.0",
    123220                                    compiler_options={"generate_ode_jacobian": True}, compile_to="LinearSub2Dir.fmu")
    124    
    125     @testattr(stddist = True)
    126     def test_basic_simulation(self):
    127         model = load_fmu(self.linear_full)
    128        
    129         model_sub1 = load_fmu(self.linear_sub1)
    130         model_sub2 = load_fmu(self.linear_sub2)
    131        
    132         models = [model_sub1, model_sub2]
    133         connections = [(model_sub1,"y1",model_sub2,"u2"),
    134                    (model_sub2,"y2",model_sub1,"u1")]
    135                    
    136         master = Master(models, connections)
    137        
    138         opts = master.simulate_options()
    139         opts["step_size"] = 0.0005
    140        
    141         res = master.simulate(options=opts)
    142        
    143         res_full = model.simulate()
    144        
    145         nose.tools.assert_almost_equal(res[model_sub1].final("x1"), res_full.final("p1.x1"), 2)
    146         nose.tools.assert_almost_equal(res[model_sub2].final("x2"), res_full.final("p2.x2"), 2)
    147        
     221
    148222    @testattr(stddist = True)
    149223    def test_linear_correction_simulation(self):
     
    230304
    231305    @testattr(stddist = True)
    232     def test_unstable_simulation(self):
    233         model = load_fmu(self.linear_full)
    234        
    235         model_sub1 = load_fmu(self.linear_sub1)
    236         model_sub2 = load_fmu(self.linear_sub2)
    237         model_sub2.set("d2", 1.1) #Coupled system becomes unstable
    238        
    239         models = [model_sub1, model_sub2]
    240         connections = [(model_sub1,"y1",model_sub2,"u2"),
    241                    (model_sub2,"y2",model_sub1,"u1")]
    242                    
    243         master = Master(models, connections)
    244        
    245         opts = master.simulate_options()
    246         opts["step_size"] = 0.0005
    247        
    248         res = master.simulate(final_time=0.3, options=opts)
    249        
    250         assert abs(res[model_sub1].final("x1")) > 100
    251         assert abs(res[model_sub2].final("x2")) > 100
    252 
    253     @testattr(stddist = True)
    254306    def test_initialize(self):
    255307        model = load_fmu(self.linear_full)
  • PyFMI/branches/PyFMI-2.5.x/tests/test_io.py

    r12969 r13031  
    150150        assert res.description[res.get_variable_index("J1.phi")] == "Absolute rotation angle of component"
    151151   
    152 
     152    @testattr(stddist = True)
     153    def test_get_description_unicode(self):
     154        model = Dummy_FMUModelME1([], "Description.fmu", os.path.join(file_path, "files", "FMUs", "XML", "ME1.0"), _connect_dll=False)
     155        model.initialize()
     156       
     157        result_writer = ResultHandlerFile(model)
     158        result_writer.set_options(model.simulate_options())
     159        result_writer.simulation_start()
     160        result_writer.initialize_complete()
     161        result_writer.integration_point()
     162        result_writer.simulation_end()
     163       
     164        res = ResultDymolaTextual('Description_result.txt')
     165        desc = res.description[res.get_variable_index("x")]
     166
     167        assert desc == u"Test symbols '' ‘’"
    153168   
    154169    @testattr(stddist = True)
     
    377392class TestResultFileBinary:
    378393    @testattr(stddist = True)
    379     def test_get_description(self):
    380         model = Dummy_FMUModelME1([], "CoupledClutches.fmu", os.path.join(file_path, "files", "FMUs", "XML", "ME1.0"), _connect_dll=False)
     394    def test_get_description_unicode(self):
     395        model = Dummy_FMUModelME1([], "Description.fmu", os.path.join(file_path, "files", "FMUs", "XML", "ME1.0"), _connect_dll=False)
    381396        model.initialize()
    382397       
     
    388403        result_writer.simulation_end()
    389404       
     405        res = ResultDymolaBinary('Description_result.mat')
     406       
     407        desc = res.description[res.get_variable_index("x")]
     408        #This handling should in the future be nativly handled by the IO module       
     409        desc = desc.encode("latin_1", "replace").decode("utf-8", "replace")
     410       
     411        assert desc == u"Test symbols '' ‘’"
     412       
     413    @testattr(stddist = True)
     414    def test_get_description(self):
     415        model = Dummy_FMUModelME1([], "CoupledClutches.fmu", os.path.join(file_path, "files", "FMUs", "XML", "ME1.0"), _connect_dll=False)
     416        model.initialize()
     417       
     418        result_writer = ResultHandlerBinaryFile(model)
     419        result_writer.set_options(model.simulate_options())
     420        result_writer.simulation_start()
     421        result_writer.initialize_complete()
     422        result_writer.integration_point()
     423        result_writer.simulation_end()
     424       
    390425        res = ResultDymolaBinary('CoupledClutches_result.mat')
    391426       
  • PyFMI/branches/PyFMI-2.5.x/tests/test_util.py

    r12966 r13031  
    142142        self.time = 0.0
    143143   
    144     def initialize(self, *args, **kwargs):
    145         pass
     144    def enter_initialization_mode(self, *args, **kwargs):
     145        self._has_entered_init_mode = True
     146        return 0
     147   
     148    def exit_initialization_mode(self, *args, **kwargs):
     149        self._has_entered_init_mode = True
     150        return 0
     151   
     152    def initialize(self, *args, **kwargs):
     153        self.enter_initialization_mode()
    146154   
    147155    def event_update(self, *args, **kwargs):
     
    175183    def get_boolean(self, vref):
    176184        return self.get_real(vref)
    177 
    178 class Dummy_FMUModelME2(FMUModelME2):
    179     #Override properties
    180     time = None
    181     continuous_states = None
    182     nominal_continuous_states = None
    183    
    184     def __init__(self, negated_aliases, *args,**kwargs):
    185         FMUModelME2.__init__(self, *args, **kwargs)
    186    
    187         self.continuous_states = np.zeros(self.get_ode_sizes()[0])
    188         self.nominal_continuous_states = np.ones(self.get_ode_sizes()[0])
    189         self.variables = self.get_model_variables(include_alias=False)
    190         self.negated_aliases = negated_aliases
    191        
    192         self.reset()
    193    
    194     def reset(self, *args, **kwargs):
    195         self.values = {}
    196         for var in self.variables:
    197             try:
    198                 start = self.get_variable_start(var)
    199             except FMUException:
    200                 start = 0.0
    201             self.values[self.variables[var].value_reference] = start
    202         for alias in self.negated_aliases:
    203             self.values[self.variables[alias[1]].value_reference] = -self.values[self.variables[alias[0]].value_reference]
    204        
    205         states = self.get_states_list()
    206         for i,state in enumerate(states):
    207             self.continuous_states[i] = self.values[states[state].value_reference]
    208    
    209     def setup_experiment(self, *args, **kwargs):
    210         self.time = 0.0
    211    
    212     def enter_initialization_mode(self, *args, **kwargs):
    213         self._has_entered_init_mode = True
    214         return 0
    215    
    216     def exit_initialization_mode(self, *args, **kwargs):
    217         self._has_entered_init_mode = True
    218         return 0
    219    
    220     def initialize(self, *args, **kwargs):
    221         self.enter_initialization_mode()
    222    
    223     def event_update(self, *args, **kwargs):
    224         pass
    225    
    226     def enter_continuous_time_mode(self, *args, **kwargs):
    227         pass
    228    
    229     def completed_integrator_step(self, *args, **kwargs):
    230         states = self.get_states_list()
    231         for i,state in enumerate(states):
    232             self.values[states[state].value_reference] = self.continuous_states[i]
    233         for alias in self.negated_aliases:
    234             self.values[self.variables[alias[1]].value_reference] = -self.values[self.variables[alias[0]].value_reference]
    235         return [False, False]
    236    
    237     def get_derivatives(self):
    238         return -self.continuous_states
    239        
    240     def get_real(self, vref):
    241         self.get_derivatives()
    242         vals = []
    243         if type(vref) == int:
    244             vref = [vref]
    245         for v in vref:
    246             vals.append(self.values[v])
    247         return np.array(vals)
    248185   
    249186    def set_real(self, vref, values):
    250187        for i,v in enumerate(vref):
    251188            self.values[v] = values[i]
     189
     190class Dummy_FMUModelME2(FMUModelME2):
     191    #Override properties
     192    time = None
     193    continuous_states = None
     194    nominal_continuous_states = None
     195   
     196    def __init__(self, negated_aliases, *args,**kwargs):
     197        FMUModelME2.__init__(self, *args, **kwargs)
     198   
     199        self.continuous_states = np.zeros(self.get_ode_sizes()[0])
     200        self.nominal_continuous_states = np.ones(self.get_ode_sizes()[0])
     201        self.variables = self.get_model_variables(include_alias=False)
     202        self.negated_aliases = negated_aliases
     203        self.states = self.get_states_list()
     204       
     205        self.reset()
     206   
     207    def reset(self, *args, **kwargs):
     208        self.values = {}
     209        for var in self.variables:
     210            try:
     211                start = self.get_variable_start(var)
     212            except FMUException:
     213                start = 0.0
     214            self.values[self.variables[var].value_reference] = start
     215        for alias in self.negated_aliases:
     216            self.values[self.variables[alias[1]].value_reference] = -self.values[self.variables[alias[0]].value_reference]
     217       
     218        states = self.states
     219        for i,state in enumerate(states):
     220            self.continuous_states[i] = self.values[states[state].value_reference]
     221   
     222    def setup_experiment(self, *args, **kwargs):
     223        self.time = 0.0
     224   
     225    def enter_initialization_mode(self, *args, **kwargs):
     226        self._has_entered_init_mode = True
     227        return 0
     228   
     229    def exit_initialization_mode(self, *args, **kwargs):
     230        self._has_entered_init_mode = True
     231        return 0
     232   
     233    def initialize(self, *args, **kwargs):
     234        self.enter_initialization_mode()
     235   
     236    def event_update(self, *args, **kwargs):
     237        pass
     238   
     239    def enter_continuous_time_mode(self, *args, **kwargs):
     240        pass
     241   
     242    def completed_integrator_step(self, *args, **kwargs):
     243        states = self.states
     244        for i,state in enumerate(states):
     245            self.values[states[state].value_reference] = self.continuous_states[i]
     246        for alias in self.negated_aliases:
     247            self.values[self.variables[alias[1]].value_reference] = -self.values[self.variables[alias[0]].value_reference]
     248        return [False, False]
     249   
     250    def get_derivatives(self):
     251        return -self.continuous_states
     252       
     253    def get_real(self, vref):
     254        self.get_derivatives()
     255        vals = []
     256        if type(vref) == int:
     257            vref = [vref]
     258        for v in vref:
     259            vals.append(self.values[v])
     260        return np.array(vals)
     261   
     262    def set_real(self, vref, values):
     263        for i,v in enumerate(vref):
     264            self.values[v] = values[i]
    252265   
    253266    def get_integer(self, vref):
Note: See TracChangeset for help on using the changeset viewer.