Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from ctypes import cdll
import os
from fmpy import sharedLibraryExtension, platform_tuple
library_dir, _ = os.path.split(__file__)
# load SUNDIALS shared libraries
sundials_nvecserial = cdll.LoadLibrary(os.path.join(library_dir, platform_tuple, 'sundials_nvecserial' + sharedLibraryExtension))
sundials_sunmatrixdense = cdll.LoadLibrary(os.path.join(library_dir, platform_tuple, 'sundials_sunmatrixdense' + sharedLibraryExtension))
sundials_sunlinsoldense = cdll.LoadLibrary(os.path.join(library_dir, platform_tuple, 'sundials_sunlinsoldense' + sharedLibraryExtension))
sundials_cvode = cdll.LoadLibrary(os.path.join(library_dir, platform_tuple, 'sundials_cvode' + sharedLibraryExtension))
check_call([
'cmake',
'-DEXAMPLES_ENABLE_C=OFF',
'-DBUILD_STATIC_LIBS=OFF',
'-DCMAKE_INSTALL_PREFIX=cvode-5.3.0/dynamic/install',
'-DCMAKE_USER_MAKE_RULES_OVERRIDE=../OverrideMSVCFlags.cmake',
'-G', generator,
'-S', 'cvode-5.3.0',
'-B', 'cvode-5.3.0/dynamic'
])
check_call(['cmake', '--build', 'cvode-5.3.0/dynamic', '--target', 'install', '--config', 'Release'])
os.mkdir(sundials_binary_dir)
os.path.join('cvode-5.3.0', 'dynamic', 'install', 'sundials_cvode' + sharedLibraryExtension)
for name in ['sundials_cvode', 'sundials_nvecserial', 'sundials_sunlinsoldense', 'sundials_sunmatrixdense']:
src = os.path.join('cvode-5.3.0', 'dynamic', 'install', 'lib', sl_prefix + name + sl_suffix)
dst = os.path.join(sundials_binary_dir, name + sl_suffix)
shutil.copy(src, dst)
# build cswrapper
os.mkdir('cswrapper/build')
check_call([
'cmake',
'-DCVODE_INSTALL_DIR=../cvode-5.3.0/static/install',
'-G', generator,
'-S', 'cswrapper',
'-B', 'cswrapper/build'
])
def __init__(self, **kwargs):
# build the path to the shared library
kwargs['libraryPath'] = os.path.join(kwargs['unzipDirectory'], 'binaries', platform_tuple,
kwargs['modelIdentifier'] + sharedLibraryExtension)
super(_FMU3, self).__init__(**kwargs)
# inquire version numbers and setting logging status
self._fmi3Function('fmi3GetVersion', [], fmi3String)
self._fmi3Function('fmi3SetDebugLogging', [
(fmi3Instance, 'instance'),
(fmi3Boolean, 'loggingOn'),
(c_size_t, 'nCategories'),
(POINTER(fmi3String), 'categories')
])
self._fmi3Function('fmi3FreeInstance', [(fmi3Instance, 'instance')], None)
# Enter and exit initialization mode, terminate and reset
libraryPath path to the shared library
fmiCallLogger logger callback that takes a message as input
"""
self.guid = guid
self.modelIdentifier = modelIdentifier
self.unzipDirectory = unzipDirectory
self.instanceName = instanceName if instanceName is not None else self.modelIdentifier
self.fmiCallLogger = fmiCallLogger
# remember the current working directory
work_dir = os.getcwd()
if libraryPath is None:
library_dir = os.path.join(unzipDirectory, 'binaries', platform)
libraryPath = str(os.path.join(library_dir, self.modelIdentifier + sharedLibraryExtension))
else:
library_dir = os.path.dirname(libraryPath)
# check if shared library exists
if not os.path.isfile(libraryPath):
raise Exception("Cannot find shared library %s." % libraryPath)
# change to the library directory as some DLLs expect this to resolve dependencies
os.chdir(library_dir)
# load the shared library
try:
self.dll = cdll.LoadLibrary(libraryPath)
except Exception as e:
raise Exception("Failed to load shared library %s. %s" % (libraryPath, e))
raise Exception("More than one SourceFileSet is not supported.")
source_file_set = build_configuration.sourceFileSets[0]
source_files += source_file_set.sourceFiles
for definition in source_file_set.preprocessorDefinitions:
literal = definition.name
if definition.value is not None:
literal += '=' + definition.value
preprocessor_definitions.append(literal)
if len(source_files) == 0:
raise Exception("No source files specified in the model description.")
target = build_configuration.modelIdentifier + sharedLibraryExtension
print('Compiling %s...' % target)
if compiler == 'vc':
vc_versions = visual_c_versions()
if len(vc_versions) == 0:
raise Exception("No VisualStudio found")
# use the latest version
vc_version = vc_versions[-1]
if vc_version < 150:
command = r'call "%%VS%dCOMNTOOLS%%\..\..\VC\vcvarsall.bat"' % vc_version
else:
from fmpy import platform_tuple, sharedLibraryExtension
from fmpy.util import download_file
import tarfile
import os
import shutil
from subprocess import check_call
if os.name == 'nt':
generator = 'Visual Studio 15 2017 Win64'
sl_prefix = ''
sl_suffix = sharedLibraryExtension
else:
generator = 'Unix Makefiles'
sl_prefix = 'lib'
sl_suffix = sharedLibraryExtension
sundials_binary_dir = os.path.join('fmpy', 'sundials', platform_tuple)
# clean up
for build_dir in ['cswrapper/build', 'cvode-5.3.0', sundials_binary_dir, 'fmpy/logging/build']:
if os.path.isdir(build_dir):
shutil.rmtree(build_dir)
url = 'https://computing.llnl.gov/projects/sundials/download/cvode-5.3.0.tar.gz'
checksum = 'd7ff8e77bb2a59cf8143de30f05a2651c2b4d29b586f8003f9187bf9e5a7da6e'
filename = os.path.basename(url)
download_file(url, checksum)
# response = requests.get(url)
from fmpy import platform_tuple, sharedLibraryExtension
from fmpy.util import download_file
import tarfile
import os
import shutil
from subprocess import check_call
if os.name == 'nt':
generator = 'Visual Studio 15 2017 Win64'
sl_prefix = ''
sl_suffix = sharedLibraryExtension
else:
generator = 'Unix Makefiles'
sl_prefix = 'lib'
sl_suffix = sharedLibraryExtension
sundials_binary_dir = os.path.join('fmpy', 'sundials', platform_tuple)
# clean up
for build_dir in ['cswrapper/build', 'cvode-5.3.0', sundials_binary_dir, 'fmpy/logging/build']:
if os.path.isdir(build_dir):
shutil.rmtree(build_dir)
url = 'https://computing.llnl.gov/projects/sundials/download/cvode-5.3.0.tar.gz'
checksum = 'd7ff8e77bb2a59cf8143de30f05a2651c2b4d29b586f8003f9187bf9e5a7da6e'
filename = os.path.basename(url)