def test_type_definitions(self): """ Read the Type Definitions from the modelDescription.xml """ for fmi_version in ['1.0', '2.0']: download_file( 'https://trac.fmi-standard.org/export/HEAD/branches/public/Test_FMUs/FMI_' + fmi_version + '/CoSimulation/win64/Dymola/2017/DFFREG/DFFREG.fmu') model_description = read_model_description('DFFREG.fmu') real = model_description.typeDefinitions[0] self.assertEqual('Real', real.type) self.assertEqual('Modelica.SIunits.Time', real.name) self.assertEqual('Time', real.quantity) self.assertEqual('s', real.unit) logic = model_description.typeDefinitions[1] self.assertEqual('Enumeration', logic.type) self.assertEqual('Modelica.Electrical.Digital.Interfaces.Logic', logic.name) self.assertEqual(9, len(logic.items)) high_impedance = logic.items[4] self.assertEqual("'Z'", high_impedance.name) self.assertEqual(5, int(high_impedance.value)) self.assertEqual("Z High Impedance", high_impedance.description)
def setUp(self): download_file( url= 'https://github.com/modelica/Reference-FMUs/releases/download/v0.0.3/Reference-FMUs-0.0.3.zip', checksum= 'f9b5c0199127d174e38583fc8733de4286dfd1da8236507007ab7b38f0e32796') extract('Reference-FMUs-0.0.3.zip', 'Reference-FMUs')
def test_type_definitions(self): """ Read the Type Definitions from the modelDescription.xml """ for fmi_version in ['1.0', '2.0']: download_file( 'https://github.com/modelica/fmi-cross-check/raw/master/fmus/' + fmi_version + '/cs/win64/Dymola/2017/DFFREG/DFFREG.fmu') model_description = read_model_description('DFFREG.fmu') real = model_description.typeDefinitions[0] self.assertEqual('Real', real.type) self.assertEqual('Modelica.SIunits.Time', real.name) self.assertEqual('Time', real.quantity) self.assertEqual('s', real.unit) logic = model_description.typeDefinitions[1] self.assertEqual('Enumeration', logic.type) self.assertEqual('Modelica.Electrical.Digital.Interfaces.Logic', logic.name) self.assertEqual(9, len(logic.items)) high_impedance = logic.items[4] self.assertEqual("'Z'", high_impedance.name) self.assertEqual(5, int(high_impedance.value)) self.assertEqual("Z High Impedance", high_impedance.description)
def test_cmake(self): """ Create a CMake project """ from subprocess import call, list2cmdline import shutil cmake_available = False try: res = call(['cmake']) cmake_available = True except OSError as e: pass for fmu in self.fmus: download_file(self.url + fmu) filename = os.path.basename(fmu) model_name, _ = os.path.splitext(filename) # clean up if os.path.isdir(model_name): shutil.rmtree(model_name) # create an empty directory os.makedirs(model_name) # create the CMake project create_cmake_project(filename, model_name) if cmake_available and os.name == 'nt': params = [ 'cd', model_name, '&', 'cmake', '-G', 'Visual Studio 14 2015 Win64', '.', '&', # create a Visual Studio 2015 solution r'%VS140COMNTOOLS%..\..\VC\vcvarsall.bat ', '&', # set VS environment variables 'msbuild', model_name + '.sln' # build the solution ] print(list2cmdline(params)) # create and build the solution status = call(params, shell=True) self.assertEqual(0, status) # simulate the FMU result = simulate_fmu( filename=os.path.join(model_name, filename)) self.assertIsNotNone(result)
def test_cmake(self): """ Create a CMake project """ from subprocess import check_call import shutil from fmpy.util import visual_c_versions try: # check if CMake is installed check_call(['cmake']) cmake_available = True except: cmake_available = False for fmu in self.fmus: download_file(self.url + fmu) filename = os.path.basename(fmu) model_name, _ = os.path.splitext(filename) # clean up if os.path.isdir(model_name): shutil.rmtree(model_name) # create the CMake project create_cmake_project(filename, model_name) if not cmake_available: continue # skip compilation # generate the build system cmake_args = ['cmake', '.'] vc_versions = visual_c_versions() if os.name == 'nt': if 160 in vc_versions: cmake_args += ['-G', 'Visual Studio 16 2019', '-A', 'x64'] elif 150 in vc_versions: cmake_args += ['-G', 'Visual Studio 15 2017 Win64'] elif 140 in vc_versions: cmake_args += ['-G', 'Visual Studio 14 2015 Win64'] elif 120 in vc_versions: cmake_args += ['-G', 'Visual Studio 12 2013 Win64'] elif 110 in vc_versions: cmake_args += ['-G', 'Visual Studio 11 2012 Win64'] check_call(args=cmake_args, cwd=model_name) # run the build system check_call(args=['cmake', '--build', '.'], cwd=model_name) # simulate the FMU result = simulate_fmu(filename=os.path.join(model_name, filename)) self.assertIsNotNone(result)
def setUpClass(cls): # download the FMU and input file download_test_file('2.0', 'ModelExchange', 'MapleSim', '2016.2', 'CoupledClutches', 'CoupledClutches.fmu') download_test_file('2.0', 'ModelExchange', 'MapleSim', '2016.2', 'CoupledClutches', 'CoupledClutches_in.csv') download_file( 'https://github.com/modelica/fmi-cross-check/raw/master/fmus/2.0/me/win64/Dymola/2019FD01/Rectifier/Rectifier.fmu' )
def test_compile(self): """ Compile the platform binary """ for fmu in self.fmus: download_file(self.url + fmu) filename = os.path.basename(fmu) compile_platform_binary(filename) result = simulate_fmu(filename=filename) self.assertIsNotNone(result)
def setUp(self): download_file( url= 'https://github.com/modelica/Reference-FMUs/releases/download/v0.0.3/Reference-FMUs-0.0.3.zip', checksum= 'f9b5c0199127d174e38583fc8733de4286dfd1da8236507007ab7b38f0e32796') extract('Reference-FMUs-0.0.3.zip', 'Reference-FMUs-dist') download_file( url='https://github.com/modelica/Reference-FMUs/archive/v0.0.3.zip', checksum= 'ce58f006d3fcee52261ce2f7a3dad635161d4dcaaf0e093fdcd5ded7ee0df647') extract('v0.0.3.zip', 'Reference-FMUs-repo')
def setUp(self): download_file( url= f'https://github.com/modelica/Reference-FMUs/releases/download/v{v}/Reference-FMUs-{v}.zip', checksum= '2e4665f7d1e3f058d68da024aa0e98de8cf11d70944fe1703e04d5779fbf428a') extract('Reference-FMUs-' + v + '.zip', 'Reference-FMUs-dist') download_file( url=f'https://github.com/modelica/Reference-FMUs/archive/v{v}.zip', checksum= '58a9f5cd55f9f6a28025ffef3fcfb42804530c8478240db60d448c005582f7d7') extract(f'v{v}.zip', 'Reference-FMUs-repo')
def setUp(self): download_file( url='https://github.com/modelica/Reference-FMUs/releases/download/v' + v + '/Reference-FMUs-' + v + '.zip', checksum= 'ed4b2346782c44937a411037c19a32ac2bd09cd43a5fce9bb0fddc571723fc3a') extract('Reference-FMUs-' + v + '.zip', 'Reference-FMUs-dist') download_file( url='https://github.com/modelica/Reference-FMUs/archive/v' + v + '.zip', checksum= '1c9efd38cbe89ea8f31e588fe8a767b082d23a1ed1f3875e2ac770fc3f371489') extract('v' + v + '.zip', 'Reference-FMUs-repo')
def test_cmake(self): """ Create a CMake project """ from subprocess import check_call import shutil from fmpy import platform try: # check if CMake is installed check_call(['cmake']) cmake_available = True except: cmake_available = False for fmu in self.fmus: download_file(self.url + fmu) filename = os.path.basename(fmu) model_name, _ = os.path.splitext(filename) # clean up if os.path.isdir(model_name): shutil.rmtree(model_name) # create the CMake project create_cmake_project(filename, model_name) if not cmake_available: continue # skip compilation if platform == 'win32': generator = 'Visual Studio 14 2015' elif platform == 'win64': generator = 'Visual Studio 14 2015 Win64' else: generator = 'Unix Makefiles' # generate the build system check_call(args=['cmake', '-G', generator, '.'], cwd=model_name) # run the build system check_call(args=['cmake', '--build', '.'], cwd=model_name) # simulate the FMU result = simulate_fmu(filename=os.path.join(model_name, filename)) self.assertIsNotNone(result)
def test_compile(self): """ Compile directly """ for fmu in self.fmus: download_file(self.url + fmu) filename = os.path.basename(fmu) # compile in-place result = simulate_fmu(filename=filename, use_source_code=True) self.assertIsNotNone(result) # add binary to FMU compile_platform_binary(filename) result = simulate_fmu(filename=filename) self.assertIsNotNone(result)
def test_remoting_cs(self): download_file( 'https://github.com/modelica/fmi-cross-check/raw/master/fmus/2.0/cs/win32/FMUSDK/2.0.4/vanDerPol/vanDerPol.fmu' ) filename = 'vanDerPol.fmu' self.assertNotIn('win64', supported_platforms(filename)) simulate_fmu(filename, fmi_type='CoSimulation') add_remoting(filename) self.assertIn('win64', supported_platforms(filename)) simulate_fmu(filename, fmi_type='CoSimulation')
def test_step_size_cs(self): url = 'https://github.com/modelica/fmi-cross-check/raw/master/fmus/2.0/cs/win64/Test-FMUs/0.0.2/Dahlquist/Dahlquist.fmu' sha256 = '6df6ab64705615dfa1217123a103c23384a081763a6f71726ba7943503da8fc0' download_file(url, checksum=sha256) h = 0.02 result = simulate_fmu(os.path.basename(url), output_interval=h, stop_time=10) time = result['time'] grid = np.array(range(501)) * h self.assertTrue(np.all(time == grid))
def simulate_controlled_drivetrain(show_plot=True): """ Download and simulate ControlledDrivetrain.ssp Parameters: show_plot plot the results """ ssp_filename = r'ControlledDrivetrain.ssp' download_file('https://github.com/CATIA-Systems/FMPy/releases/download/v0.1.1/' + ssp_filename, checksum='45e667ed') print("Simulating %s..." % ssp_filename) result = simulate_ssp(ssp_filename, stop_time=4, step_size=1e-3) if show_plot: print("Plotting results...") plot_result(result, names=['reference.y', 'drivetrain.w', 'controller.y'], window_title=ssp_filename) print('Done.') return result
def test_compile(self): fmus = [ 'c-code/dSPACE_TargetLink/Release_2016-B/poscontrol/FmuController.fmu', 'c-code/MapleSim/2016.2/Rectifier/Rectifier.fmu', 'c-code/Dymola/2017/IntegerNetwork1/IntegerNetwork1.fmu', ] for fmu in fmus: download_file(self.url + fmu) filename = os.path.basename(fmu) # compile in-place result = simulate_fmu(filename=filename, use_source_code=True) self.assertIsNotNone(result) # add binary to FMU compile_platform_binary(filename) result = simulate_fmu(filename=filename) self.assertIsNotNone(result)
def run_efficient_loop(): # download the FMU url = 'https://github.com/modelica/fmi-cross-check/raw/master/fmus/2.0/cs/win64/Test-FMUs/0.0.2/VanDerPol/VanDerPol.fmu' sha = 'a870f5f7f712e8152bfd60a1c2fd1c0bc10d4ca8124bd3031e321e8dd1e71bb0' download_file(url, sha) # extract the FMU to a temporary directory unzipdir = extract('VanDerPol.fmu') # read the model description model_description = read_model_description(unzipdir) # instantiate the FMU fmu_instance = instantiate_fmu(unzipdir, model_description, 'ModelExchange') # perform the iteration for i in range(100): # reset the FMU instance instead of creating a new one fmu_instance.reset() # calculate the parameters for this run start_values = {'mu': i * 0.01} # pass the unzipdir, model description and FMU instance to simulate_fmu() result = simulate_fmu(unzipdir, start_values=start_values, model_description=model_description, fmu_instance=fmu_instance) # free the FMU instance and unload the shared library fmu_instance.freeInstance() # delete the temporary directory shutil.rmtree(unzipdir, ignore_errors=True)
def reference_fmus_repo_dir(resources_dir): version = '0.0.14' checksum = '93ffb56774b15130b6993d345dff1795ddce7872706c0b4d8f4d8edd361a8a7a' zip_file = download_file( url= f'https://github.com/modelica/Reference-FMUs/archive/v{version}.zip', checksum=checksum) extract(filename=zip_file, unzipdir=resources_dir) repo_dir = resources_dir / f'Reference-FMUs-{version}' yield repo_dir
def reference_fmus_dist_dir(resources_dir): version = '0.0.16' checksum = '9817c0ee19c8648ae681a4529bac805542743c29ab81f643165e2c828eb2beed' zip_file = download_file( url= f'https://github.com/modelica/Reference-FMUs/releases/download/v{version}/Reference-FMUs-{version}.zip', checksum=checksum) dist_dir = resources_dir / 'Reference-FMUs-dist' extract(filename=zip_file, unzipdir=dist_dir) yield dist_dir
def test_compile(self): """ Compile the platform binary """ # add debug info if os.name == 'nt': compiler_options = '/LDd /Zi' else: compiler_options = '-g -fPIC' for fmu in self.fmus: filename = download_file(self.url + fmu) compile_platform_binary(filename, compiler_options=compiler_options) result = simulate_fmu(filename=filename) self.assertIsNotNone(result)
def test_validate_variable_names(self): filename = download_file( url= 'https://github.com/modelica/fmi-cross-check/raw/master/fmus/2.0/me/win64/MapleSim/2015.1/CoupledClutches/CoupledClutches.fmu', checksum= 'af8f8ca4d7073b2d6207d8eea4a3257e3a23a69089f03181236ee3ecf13ff77f') problems = [] try: read_model_description(filename, validate=True, validate_variable_names=True) except ValidationError as e: problems = e.problems self.assertEqual(len(problems), 124)
def test_remoting_win32_on_win64_cs(self): filename = download_file( 'https://github.com/modelica/fmi-cross-check/raw/master/fmus/2.0/cs/win32/FMUSDK/2.0.4/vanDerPol/vanDerPol.fmu', checksum= '6a782ae3b3298081f9c620a17dedd54370622bd2bb78f42cb027243323a1b805') self.assertNotIn('win64', supported_platforms(filename)) simulate_fmu(filename, fmi_type='CoSimulation', remote_platform='win32') add_remoting(filename, host_platform='win64', remote_platform='win32') self.assertIn('win64', supported_platforms(filename)) simulate_fmu(filename, fmi_type='CoSimulation', remote_platform=None)
def test_remoting_linux64_on_win64(self): filename = download_file( 'https://github.com/modelica/fmi-cross-check/raw/master/fmus/2.0/cs/linux64/MapleSim/2021.1/Rectifier/Rectifier.fmu', checksum= 'b9238cd6bb684f1cf5b240ca140ed5b3f75719cacf81df5ff0cae74c2e31e52e') self.assertNotIn('win64', supported_platforms(filename)) simulate_fmu(filename, remote_platform='linux64') add_remoting(filename, host_platform='win64', remote_platform='linux64') self.assertIn('win64', supported_platforms(filename)) simulate_fmu(filename, remote_platform=None)
def test_remoting_win32_on_win64_me(self): filename = download_file( 'https://github.com/modelica/fmi-cross-check/raw/master/fmus/2.0/me/win32/MapleSim/2021.1/CoupledClutches/CoupledClutches.fmu', checksum= '2a22c800285bcda810d9fe59234ee72c29e0fea86bb6ab7d6eb5b703f0afbe4e') self.assertNotIn('win64', supported_platforms(filename)) simulate_fmu(filename, fmi_type='ModelExchange', remote_platform='win32', fmi_call_logger=None) add_remoting(filename, 'win64', 'win32') self.assertIn('win64', supported_platforms(filename)) simulate_fmu(filename, fmi_type='ModelExchange', remote_platform=None)
def test_remoting_win64_on_linux64_cs(self): filename = download_file( 'https://github.com/modelica/fmi-cross-check/raw/master/fmus/2.0/cs/win64/Dymola/2019FD01/DFFREG/DFFREG.fmu', checksum= 'b4baf75e189fc7078b76c3d9f23f6476ec103d93f60168df4e82fa4dc053a93c') self.assertNotIn('linux64', supported_platforms(filename)) simulate_fmu(filename, remote_platform='win64', stop_time=5, output_interval=0.01) add_remoting(filename, host_platform='linux64', remote_platform='win64') self.assertIn('win64', supported_platforms(filename)) simulate_fmu(filename, remote_platform=None)
def test_remoting_linux64_on_win64(self): if not isfile( join(dirname(fmpy.__file__), 'remoting', 'linux64', 'server_tcp')): return # Linux binary is missing filename = download_file( 'https://github.com/modelica/fmi-cross-check/raw/master/fmus/2.0/cs/linux64/MapleSim/2021.1/Rectifier/Rectifier.fmu', checksum= 'b9238cd6bb684f1cf5b240ca140ed5b3f75719cacf81df5ff0cae74c2e31e52e') self.assertNotIn('win64', supported_platforms(filename)) simulate_fmu(filename, remote_platform='linux64') add_remoting(filename, host_platform='win64', remote_platform='linux64') self.assertIn('win64', supported_platforms(filename)) simulate_fmu(filename, remote_platform=None)
def setUp(self): download_file( url= 'https://github.com/modelica/fmi-cross-check/raw/master/fmus/2.0/me/win64/MapleSim/2015.1/CoupledClutches/CoupledClutches.fmu', checksum= 'af8f8ca4d7073b2d6207d8eea4a3257e3a23a69089f03181236ee3ecf13ff77f')
# clean up for p in ['rpclib-2.2.1', 'remoting/client/build', 'remoting/server/build']: if os.path.exists(p): shutil.rmtree(p) for f in ['fmpy/remoting/client.dll', 'fmpy/remoting/server.exe']: if os.path.exists(f): os.remove(f) rpclib_url = 'https://github.com/rpclib/rpclib/archive/v2.2.1.zip' rpclib_checksum = '70f10b59f0eb303ccee4a9dda32e6ed898783be9a539d32b43e6fcb4430dce0c' rpclib_filename = os.path.basename(rpclib_url) download_file(rpclib_url, rpclib_checksum) extract(rpclib_filename, '.') # root = os.path.dirname(__file__) # build RPCLIB for win32 check_call([ 'cmake', '-DCMAKE_INSTALL_PREFIX=rpclib-2.2.1/win32/install', '-DRPCLIB_MSVC_STATIC_RUNTIME=ON', '-G', 'Visual Studio 15 2017', '-S', 'rpclib-2.2.1', '-B', 'rpclib-2.2.1/win32' ])
import os import tarfile import shutil from subprocess import check_call from fmpy.util import download_file url = 'https://github.com/rpclib/rpclib/archive/v2.2.1.tar.gz' checksum = 'ceef2c521a1712035bc64d1bd5e3b2c7de16a1d856cbbeadd000ae318c96463f' # build configuration config = 'Release' download_file(url, checksum) filename = os.path.basename(url) basedir = os.path.dirname(__file__) source_dir = 'rpclib-2.2.1' rpclib_dir = os.path.join(basedir, source_dir).replace('\\', '/') shutil.rmtree(source_dir, ignore_errors=True) print("Extracting %s" % filename) with tarfile.open(filename, 'r:gz') as tar: tar.extractall() path = os.path.dirname(__file__)
'Win32'], 'i686-windows'), ('win64', ['-G', 'Visual Studio 17 2022', '-A', 'x64'], 'x86_64-windows')] sl_prefix = '' sl_suffix = sharedLibraryExtension else: generators = [(platform, ['-G', 'Unix Makefiles'], current_platform_tuple)] sl_prefix = 'lib' sl_suffix = sharedLibraryExtension # clean up shutil.rmtree('sundials-5.3.0', ignore_errors=True) filename = download_file( url= 'https://github.com/LLNL/sundials/releases/download/v5.3.0/sundials-5.3.0.tar.gz', checksum='88dff7e11a366853d8afd5de05bf197a8129a804d9d4461fb64297f1ef89bca7' ) with tarfile.open(filename, "r:gz") as tar: tar.extractall() for platform, cmake_options, platform_tuple in generators: os.makedirs(f'sundials-5.3.0/{platform}/static') # build CVode as static library check_call([ 'cmake', '-D', 'BUILD_ARKODE=OFF', '-D', 'BUILD_CVODES=OFF', '-D', 'BUILD_IDA=OFF', '-D', 'BUILD_IDAS=OFF', '-D', 'BUILD_KINSOL=OFF', '-D', 'BUILD_SHARED_LIBS=OFF', '-D',