Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def main():
model_description = read_model_description(options.fmu_filename)
unzipdir = extract(options.fmu_filename)
fmu = FMU2Slave(guid=model_description.guid,
unzipDirectory=unzipdir,
modelIdentifier=model_description.coSimulation.modelIdentifier,
instanceName='instance1')
# initialize
fmu.instantiate()
fmu.setupExperiment(tolerance=1E-4, startTime=0.0, stopTime=options.stop_time)
fmu.enterInitializationMode()
fmu.exitInitializationMode()
start = datetime.datetime.now()
def test_extracted_fmu(self):
""" Simulate an extracted FMU """
download_test_file('2.0', 'CoSimulation', 'MapleSim', '2017', 'CoupledClutches', 'CoupledClutches.fmu')
# extract the FMU
tempdir = extract('CoupledClutches.fmu')
# load the model description before the simulation
model_description = read_model_description(tempdir)
result = simulate_fmu(tempdir, model_description=model_description)
self.assertIsNotNone(result)
# clean up
shutil.rmtree(tempdir)
if 'notCompliantWithLatestRules' in files:
return problems # stop here
path, model_name = os.path.split(model_dir)
path, _ = os.path.split(path)
path, _ = os.path.split(path)
path, platform = os.path.split(path)
path, fmi_type = os.path.split(path)
_, fmi_version = os.path.split(path)
fmu_filename = os.path.join(model_dir, model_name + '.fmu')
# validate the modelDescription.xml
try:
model_description = read_model_description(fmu_filename, validate=True)
except Exception as e:
problems.append("Error in %s. %s" % (fmu_filename, e))
return problems # stop here
# check FMI version
if model_description.fmiVersion != fmi_version:
problems.append("%s is not an FMI %s FMU" % (fmu_filename, fmi_version))
return problems # stop here
# check FMI type
if fmi_type == 'cs' and model_description.coSimulation is None:
problems.append("%s does not support co-simulation" % fmu_filename)
return problems # stop here
elif fmi_type == 'me' and model_description.modelExchange is None:
problems.append("%s does not support model-exchange" % fmu_filename)
return problems # stop here
def compile_platform_binary(filename, output_filename=None):
""" Compile the binary of an FMU for the current platform and add it to the FMU
Parameters:
filename: filename of the source code FMU
output_filename: filename of the FMU with the compiled binary (None: overwrite existing FMU)
"""
from . import read_model_description, extract, platform, platform_tuple
import zipfile
from shutil import copyfile, rmtree
unzipdir = extract(filename)
model_description = read_model_description(filename)
binary = compile_dll(model_description=model_description, sources_dir=os.path.join(unzipdir, 'sources'))
unzipdir2 = extract(filename)
platform_dir = os.path.join(unzipdir2, 'binaries', platform if model_description.fmiVersion in ['1.0', '2.0'] else platform_tuple)
if not os.path.exists(platform_dir):
os.makedirs(platform_dir)
copyfile(src=binary, dst=os.path.join(platform_dir, os.path.basename(binary)))
if output_filename is None:
output_filename = filename # overwrite the existing archive
# create a new archive from the existing files + compiled binary
# skip FMUs in _FMIModelicaTest and other directories
if options['fmi_version'] not in ['1.0', '2.0']:
continue
if skip(options):
continue
print(root)
##########################
# VALIDATE FMU AND FILES #
##########################
# read the model description
try:
model_description = fmpy.read_model_description(fmu_filename, validate=True, validate_variable_names=True)
xml_cell = '<span class="label label-success">valid</span>'
except Exception as e:
# try again without validation
model_description = fmpy.read_model_description(fmu_filename, validate=False)
xml_cell = '<span title="' + str(e) + '" class="label label-danger">invalid</span>'
# check for documentation
if model_description.fmiVersion == '1.0':
doc_path = 'documentation/_main.html'
else:
doc_path = 'documentation/index.html'
doc_cell = '<span is="" title="' + doc_path +" class="label label-default">n/a</span>'
with zipfile.ZipFile(fmu_filename, 'r') as zf:
if doc_path in zf.namelist():
if skip(include=args.include, exclude=args.exclude):
continue
print(root)
##########################
# VALIDATE FMU AND FILES #
##########################
# read the model description
try:
model_description = fmpy.read_model_description(fmu_filename)
xml_cell = '<span class="label label-success">valid</span>'
except Exception as e:
# try again without validation
model_description = fmpy.read_model_description(fmu_filename, validate=False)
xml_cell = '<span title="' + str(e) + '" class="label label-danger">invalid</span>'
# check for documentation
if model_description.fmiVersion == '1.0':
doc_path = 'documentation/_main.html'
else:
doc_path = 'documentation/index.html'
doc_cell = '<span is="" title="' + doc_path +" class="label label-default">n/a</span>'
with zipfile.ZipFile(fmu_filename, 'r') as zf:
if doc_path in zf.namelist():
# TODO: validate HTML?
doc_cell = '<span class="label label-success">available</span>'
input_variables = []
def simulate_custom_input(show_plot=True):
# define the model name and simulation parameters
fmu_filename = 'CoupledClutches.fmu'
start_time = 0.0
threshold = 2.0
stop_time = 2.0
step_size = 1e-3
# download the FMU
download_test_file('2.0', 'CoSimulation', 'MapleSim', '2016.2', 'CoupledClutches', fmu_filename)
# read the model description
model_description = read_model_description(fmu_filename)
# collect the value references
vrs = {}
for variable in model_description.modelVariables:
vrs[variable.name] = variable.valueReference
# get the value references for the variables we want to get/set
vr_inputs = vrs['inputs'] # normalized force on the 3rd clutch
vr_outputs4 = vrs['outputs[4]'] # angular velocity of the 4th inertia
# extract the FMU
unzipdir = extract(fmu_filename)
fmu = FMU2Slave(guid=model_description.guid,
unzipDirectory=unzipdir,
modelIdentifier=model_description.coSimulation.modelIdentifier,
def load(self, filename):
import zipfile
if not self.isVisible():
self.show()
try:
self.modelDescription = md = read_model_description(filename)
except Exception as e:
QMessageBox.warning(self, "Failed to load FMU", "Failed to load %s. %s" % (filename, e))
return
# show model.png
try:
pixmap = QPixmap()
# load the model.png
with zipfile.ZipFile(filename, 'r') as zf:
pixmap.loadFromData(zf.read('model.png'), format='PNG')
# show the unscaled version in tooltip
buffer = QBuffer()
buffer.open(QIODevice.WriteOnly)
pixmap.save(buffer, "PNG", quality=100)
def instantiate_fmu(component, ssp_unzipdir, start_time, parameter_set=None):
""" Instantiate an FMU """
fmu_filename = os.path.join(ssp_unzipdir, component.source)
component.unzipdir = extract(fmu_filename)
# read the model description
model_description = read_model_description(fmu_filename, validate=False)
if model_description.coSimulation is None:
raise Exception("%s does not support co-simulation." % component.source)
# collect the value references
component.variables = {}
for variable in model_description.modelVariables:
# component.vrs[variable.name] = variable.valueReference
component.variables[variable.name] = variable
fmu_kwargs = {'guid': model_description.guid,
'unzipDirectory': component.unzipdir,
'modelIdentifier': model_description.coSimulation.modelIdentifier,
'instanceName': component.name}
if model_description.fmiVersion == '1.0':