Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def validate(self, build_dir, fmi_types=['ModelExchange', 'CoSimulation'], models=models, compile=False):
from fmpy.util import read_csv, validate_result
for model in models:
print(model)
fmu_filename = os.path.join(build_dir, 'dist', model + '.fmu')
if model == 'Feedthrough':
start_values = {'real_fixed_param': 1, 'string_param': "FMI is awesome!"}
in_csv = os.path.join(test_fmus_dir, model, model + '_in.csv')
input = read_csv(in_csv)
else:
start_values = {}
input = None
ref_csv = os.path.join(test_fmus_dir, model, model + '_ref.csv')
for fmi_type in fmi_types:
ref = read_csv(ref_csv)
if compile:
compile_platform_binary(fmu_filename)
result = simulate_fmu(fmu_filename,
fmi_type=fmi_type,
start_values=start_values,
fmu_filename = os.path.join(build_dir, 'dist', model + '.fmu')
if model == 'Feedthrough':
start_values = {'real_fixed_param': 1, 'string_param': "FMI is awesome!"}
in_csv = os.path.join(test_fmus_dir, model, model + '_in.csv')
input = read_csv(in_csv)
else:
start_values = {}
input = None
ref_csv = os.path.join(test_fmus_dir, model, model + '_ref.csv')
for fmi_type in fmi_types:
ref = read_csv(ref_csv)
if compile:
compile_platform_binary(fmu_filename)
result = simulate_fmu(fmu_filename,
fmi_type=fmi_type,
start_values=start_values,
input=input)
dev = validate_result(result, ref)
self.assertLess(dev, 0.2, "Failed to validate " + model)
from fmpy.util import add_remoting
add_remoting(args.fmu_filename)
elif args.command == 'simulate':
from fmpy import simulate_fmu
from fmpy.util import read_csv, write_csv, plot_result
if args.start_values:
if len(args.start_values) % 2 != 0:
raise Exception("Start values must be name-value pairs.")
start_values = {k: v for k, v in zip(args.start_values[::2], args.start_values[1::2])}
else:
start_values = {}
input = read_csv(args.input_file) if args.input_file else None
if args.fmi_logging:
fmi_call_logger = lambda s: print('[FMI] ' + s)
else:
fmi_call_logger = None
result = simulate_fmu(args.fmu_filename,
validate=args.validate,
start_time=args.start_time,
stop_time=args.stop_time,
solver=args.solver,
step_size=args.step_size,
relative_tolerance=args.relative_tolerance,
output_interval=args.output_interval,
record_events=not args.dont_record_events,
fmi_type=None,
('Feedthrough', {'real_fixed_param': 1}, 0.2),
('Resource', {}, 0.2),
('Stair', {}, 10),
('VanDerPol', {}, 0.1),
]
for model_name, start_values, output_interval in info:
print(model_name)
fmu = os.path.join(dist_dir, model_name + '.fmu')
ref_csv = os.path.join(src_dir, model_name, model_name + '_ref.csv')
ref_png = os.path.join(src_dir, model_name, model_name + '_ref.svg')
in_csv = os.path.join(src_dir, model_name, model_name + '_in.csv')
if os.path.isfile(in_csv):
input = read_csv(in_csv)
else:
input = None
result = simulate_fmu(fmu,
fmi_type='ModelExchange',
# solver='Euler',
start_values=start_values,
input=input,
output_interval=output_interval
)
write_csv(ref_csv, result)
ref = read_csv(ref_csv)
plot_result(ref, events=True, filename=ref_png)
md_file = os.path.join(src_dir, model_name, 'readme.md')
if self.ui.solverComboBox.currentText() == 'Fixed-step':
solver = 'Euler'
else:
solver = 'CVode'
if self.ui.inputCheckBox.isChecked():
input_variables = []
for variable in self.modelDescription.modelVariables:
if variable.causality == 'input':
input_variables.append(variable.name)
try:
from fmpy.util import read_csv
filename = self.ui.inputFilenameLineEdit.text()
input = read_csv(filename, variable_names=input_variables)
except Exception as e:
self.log.log('error', "Failed to load input from '%s'. %s" % (filename, e))
return
else:
input = None
output = []
for variable in self.modelDescription.modelVariables:
output.append(variable.name)
fmi_type = 'CoSimulation' if self.fmiTypeComboBox.currentText() == 'Co-Simulation' else 'ModelExchange'
self.simulationThread = SimulationThread(filename=self.filename,
fmiType=fmi_type,
stopTime=stop_time,
solver=solver,
def simulate(options):
# read the input file
if 'input_filename' in options:
input = read_csv(options['input_filename'])
else:
input = None
# simulate the FMU
result = fmpy.simulate_fmu(filename=options['fmu_filename'],
validate=False,
step_size=options['step_size'],
stop_time=options['stop_time'],
input=input,
output=options['output_variable_names'])
return result