-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_fmu.py
72 lines (56 loc) · 1.87 KB
/
test_fmu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#
# Santiago
from fmpy import read_model_description, extract
from fmpy.fmi2 import FMU2Slave
from fmpy.util import plot_result, download_test_file
import numpy as np
import shutil
import signal
import sys
def signal_handler(sig, frame):
print('Ctrl+C pressed')
fmu.terminate()
fmu.freeInstance()
print("Forcing the termination of the FMI Simulation")
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
fmu_filename = 'controllerFMU.fmu'
start_time = 0.0
threshold = 2.0
stop_time = 10.0
step_size = 0.5
# 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
# extract the FMU
unzipdir = extract(fmu_filename)
fmu = FMU2Slave(guid=model_description.guid,
unzipDirectory=unzipdir,
modelIdentifier=model_description.coSimulation.modelIdentifier,
instanceName='instance1')
# initialize
print("Initializing FMI Simulation")
fmu.instantiate()
fmu.setupExperiment(startTime=start_time)
fmu.enterInitializationMode()
fmu.exitInitializationMode()
time = start_time
# simulation loop
idx = 0
while time < stop_time:
print("Before FMI DoStep idx " + str(idx))
fmu.doStep(currentCommunicationPoint=time, communicationStepSize=step_size)
print("After FMI DoStep idx " + str(idx))
print("moveDiscreteCommand command (from controller): " + str(fmu.getBoolean([vrs["moveDiscreteCommand"]])))
print("moveDiscreteCommand arguments (from controller): " + str(fmu.getInteger([vrs["MovementArgs_target_X"],vrs["MovementArgs_target_Y"],vrs["MovementArgs_target_Z"]])))
# advance the time
time += step_size
idx = idx + 1
fmu.terminate()
fmu.freeInstance()
print("Terminating FMI Simulation")
# clean up
shutil.rmtree(unzipdir, ignore_errors=True)