This repository has been archived by the owner on Feb 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
test_build.py
177 lines (117 loc) · 5.72 KB
/
test_build.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import unittest
import subprocess
import os
import shutil
fmus_dir = os.path.join(os.path.dirname(__file__), 'fmus') # /path/to/fmi-cross-check/fmus
test_fmus_version = '0.0.2'
try:
from fmpy import simulate_fmu
from fmpy.util import compile_platform_binary
fmpy_available = True
except:
print("FMPy not available. Skipping simulation.")
fmpy_available = False
fmi3_available = False
try:
import fmpy
fmi3_available = 'fmi3' in dir(fmpy)
except:
pass
if not fmi3_available:
print("FMI 3.0 support in FMPy not available. Skipping FMI 3.0 simulation.")
test_fmus_dir = os.path.dirname(__file__)
models = ['BouncingBall', 'Dahlquist', 'Resource', 'Stair', 'VanDerPol', 'Feedthrough']
if os.name == 'nt':
generator = 'Visual Studio 15 2017 Win64'
else:
generator = 'Unix Makefiles'
def copy_to_cross_check(build_dir, model_names, fmi_version, fmi_types):
if fmus_dir is None:
return
for fmi_type in fmi_types:
for model in model_names:
target_dir = os.path.join(fmus_dir, fmi_version, fmi_type, fmpy.platform, 'Test-FMUs', test_fmus_version, model)
if not os.path.exists(target_dir):
os.makedirs(target_dir)
shutil.copy(os.path.join(build_dir, 'dist', model + '.fmu'), target_dir)
shutil.copy(os.path.join(test_fmus_dir, model, model + '_ref.csv'), target_dir)
shutil.copy(os.path.join(test_fmus_dir, model, model + '_ref.opt'), target_dir)
class BuildTest(unittest.TestCase):
""" Build all variants of the Test FMUs and simulate the default experiment """
@classmethod
def setUpClass(cls):
# clean up
for name in ['fmi1_me', 'fmi1_cs', 'fmi2', 'fmi3']:
build_dir = os.path.join(test_fmus_dir, name)
if os.path.isdir(build_dir):
print("Removing " + build_dir)
shutil.rmtree(build_dir)
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,
input=input)
dev = validate_result(result, ref)
self.assertLess(dev, 0.2, "Failed to validate " + model)
def test_fmi1_me(self):
build_dir = os.path.join(test_fmus_dir, 'fmi1_me')
if not os.path.exists(build_dir):
os.makedirs(build_dir)
subprocess.call(['cmake', '-G', generator, '-DFMI_VERSION=1', '-DFMI_TYPE=ME', '..'], cwd=build_dir)
subprocess.call(['cmake', '--build', '.', '--config', 'Release'], cwd=build_dir)
model_names = ['BouncingBall', 'Dahlquist', 'Stair', 'VanDerPol']
if fmpy_available:
self.validate(build_dir,
fmi_types=['ModelExchange'],
models=model_names)
copy_to_cross_check(build_dir=build_dir, model_names=model_names, fmi_version='1.0', fmi_types=['me'])
def test_fmi1_cs(self):
build_dir = os.path.join(test_fmus_dir, 'fmi1_cs')
if not os.path.exists(build_dir):
os.makedirs(build_dir)
subprocess.call(['cmake', '-G', generator, '-DFMI_VERSION=1', '-DFMI_TYPE=CS', '..'], cwd=build_dir)
subprocess.call(['cmake', '--build', '.', '--config', 'Release'], cwd=build_dir)
model_names = ['BouncingBall', 'Dahlquist', 'Resource', 'Stair', 'VanDerPol']
if fmpy_available:
self.validate(build_dir,
fmi_types=['CoSimulation'],
models=model_names)
copy_to_cross_check(build_dir=build_dir, model_names=model_names, fmi_version='1.0', fmi_types=['cs'])
def test_fmi2(self):
build_dir = os.path.join(test_fmus_dir, 'fmi2')
if not os.path.exists(build_dir):
os.makedirs(build_dir)
subprocess.call(['cmake', '-G', generator, '-DFMI_VERSION=2', '..'], cwd=build_dir)
subprocess.call(['cmake', '--build', '.', '--config', 'Release'], cwd=build_dir)
if fmpy_available:
self.validate(build_dir)
self.validate(build_dir, compile=True)
copy_to_cross_check(build_dir=build_dir, model_names=models, fmi_version='2.0', fmi_types=['cs', 'me'])
def test_fmi3(self):
print('FMI 3.0')
build_dir = os.path.join(test_fmus_dir, 'fmi3')
if not os.path.exists(build_dir):
os.makedirs(build_dir)
subprocess.call(['cmake', '-G', generator, '-DFMI_VERSION=3', '..'], cwd=build_dir)
subprocess.call(['cmake', '--build', '.', '--config', 'Release'], cwd=build_dir)
if fmi3_available:
self.validate(build_dir, models=['BouncingBall', 'Dahlquist', 'Feedthrough', 'Resource', 'Stair', 'VanDerPol'])
copy_to_cross_check(build_dir=build_dir, model_names=models, fmi_version='3.0', fmi_types=['cs', 'me'])
if __name__ == '__main__':
unittest.main()