@@ -31,83 +31,88 @@ def generateLua(modelName, testFMUDir, resultDir, fmiType):
3131 luaFilePath = os .path .join (resultDir , modelName + ".lua" )
3232
3333 # Set OMSimulator settings
34- startTime = " 0.0"
35- stopTime = " 1.0"
36- relTol = str ( default_tolerance )
37- absTol = str ( default_tolerance )
38- inputCSV = ""
34+ startTime = 0.0
35+ stopTime = 1.0
36+ relTol = 1e-6
37+ absTol = 1e-6
38+ inputCSV = os . path . join ( testFMUDir , modelName + "_in.csv" )
3939 refOptFile = os .path .join (testFMUDir , modelName + "_ref.opt" )
40+
4041 df = pd .read_csv (refOptFile , delimiter = ',' , index_col = 0 , header = None )
4142
42- if "StartTime" in df .index :
43- startTime = str (df .loc ["StartTime" , 1 ])
43+ if 'StartTime' in df .index :
44+ startTime = df .loc ['StartTime' , 1 ]
45+
46+ if 'StopTime' in df .index :
47+ stopTime = df .loc ['StopTime' , 1 ]
4448
45- if "StopTime" in df .index :
46- stopTime = str (df .loc ["StopTime" , 1 ])
49+ if 'RelTol' in df .index :
50+ if not df .loc ['RelTol' , 1 ] == 0 :
51+ relTol = df .loc ['RelTol' , 1 ]
4752
48- if "RelTol" in df .index :
49- if not df .loc ["RelTol" , 1 ] == 0 :
50- relTol = str ( df .loc ["RelTol" , 1 ])
53+ if 'AbsTol' in df .index :
54+ if not df .loc ['AbsTol' , 1 ] == 0 :
55+ absTol = df .loc ['AbsTol' , 1 ]
5156
52- if "AbsTol" in df .index :
53- if not df .loc ["AbsTol" , 1 ] == 0 :
54- absTol = str (df .loc ["AbsTol" , 1 ])
57+ stepSize = (stopTime - startTime ) / 500
58+ if 'StepSize' in df .index :
59+ if not df .loc ['StepSize' , 1 ] == 0 :
60+ stepSize = df .loc ['StepSize' , 1 ]
5561
56- maximumStepSize = str (( float ( stopTime ) - float ( startTime )) / 500 )
57- if "StepSize" in df .index :
58- if not df .loc ["StepSize" , 1 ] == 0 :
59- maximumStepSize = str ( df .loc ["StepSize" , 1 ])
62+ outputIntervalLength = ( stopTime - startTime ) / 500
63+ if 'OutputIntervalLength' in df .index :
64+ if not df .loc ['OutputIntervalLength' , 1 ] == 0 :
65+ outputIntervalLength = df .loc ['OutputIntervalLength' , 1 ]
6066
61- # Check for input file
67+ # input file
6268 inputs = []
6369 if os .path .isfile (inputCSV ):
6470 df = pd .read_csv (inputCSV , delimiter = ',' )
6571 inputs = list (df .columns .values [1 :])
66- if debugPrint :
67- print ("Model " + testFMU + " has inputs\n " )
68-
69- # System type
70- if fmiType == "me" :
71- systemType = "oms_system_sc"
72- elif fmiType == "cs" :
73- systemType = "oms_system_wc"
72+ print (' * test has inputs:{}' .format (inputs ))
73+
74+ # system type
75+ if fmiType == 'me' :
76+ systemType = 'oms_system_sc'
77+ elif fmiType == 'cs' :
78+ systemType = 'oms_system_wc'
7479 else :
7580 raise Exception ("Unsupportet FMU type \" " + fmiType + "\" \n " )
7681
7782 f = open (luaFilePath , "w" )
7883
79- f .write ("-- Lua file for " + modelName + ".fmu\n " )
80- f .write ("oms_setTempDirectory(\" " + tempDir + "\" )\n " )
81- f .write ("oms_newModel(\" model\" )\n " )
82- f .write ("oms_addSystem(\" model.root\" , " + systemType + ")\n " )
83-
84- f .write ("\n -- instantiate FMU\n " )
85- if os .path .isfile (inputCSV ):
86- f .write ("oms_addSubModel(\" model.root.input\" , \" " + inputCSV + "\" )\n " )
87- f .write ("oms_addSubModel(\" model.root.fmu\" , \" " + testFMU + "\" )\n " )
84+ f .write ('-- lua file for ' + modelName + '.fmu\n ' )
85+ f .write ('oms_setTempDirectory(\' ' + tempDir + '\' )\n ' )
86+ f .write ('oms_newModel(\' model\' )\n ' )
87+ f .write ('oms_addSystem(\' model.root\' , ' + systemType + ')\n ' )
8888
89+ f .write ('\n -- instantiate FMU\n ' )
90+ f .write ('oms_addSubModel(\' model.root.fmu\' , \' ' + testFMU + '\' )\n ' )
8991 if len (inputs ) > 0 :
90- f .write ("\n -- Connect inputs to FMU\n " )
91- for inp in inputs :
92- f .write ("oms_addConnection(\" model.root.input." + inp + "\" , \" model.root.fmu." + inp + "\" )\n " )
93-
94- f .write ("\n -- Simulation settings\n " )
95- f .write ("oms_setResultFile(\" model\" , \" " + modelName + "_out.csv\" )\n " )
96- f .write ("oms_setStartTime(\" model\" , " + startTime + ")\n " )
97- f .write ("oms_setStopTime(\" model\" , " + stopTime + ")\n " )
98- f .write ("oms_setTolerance(\" model\" , " + absTol + ", " + relTol + ")\n " )
99- if fmiType == "me" :
100- f .write ("oms_setVariableStepSize(\" model\" , 1e-12, 1e-12, " + maximumStepSize + ")\n " )
101- elif fmiType == "cs" :
102- f .write ("oms_setFixedStepSize(\" model\" , " + maximumStepSize + ")\n " )
103-
104- f .write ("\n -- Instantiate, initialize and simulate\n " )
105- f .write ("oms_instantiate(\" model\" )\n " )
106- f .write ("oms_initialize(\" model\" )\n " )
107- f .write ("oms_simulate(\" model\" )\n " )
108- f .write ("oms_terminate(\" model\" )\n " )
109- f .write ("oms_delete(\" model\" )\n " )
92+ f .write ('oms_addSubModel(\' model.root.input\' , \' ' + inputCSV + '\' )\n ' )
11093
94+ if len (inputs ) > 0 :
95+ f .write ('\n -- connect inputs to FMU\n ' )
96+ for input in inputs :
97+ f .write ('oms_addConnection(\' model.root.input.' + input + '\' , \' model.root.fmu.' + input + '\' )\n ' )
98+
99+ f .write ('\n -- simulation settings\n ' )
100+ f .write ('oms_setResultFile(\' model\' , \' ' + modelName + '_out.csv\' )\n ' )
101+ f .write (f'oms_setLoggingInterval(\' model\' , { outputIntervalLength } )\n ' )
102+ f .write (f'oms_setStartTime(\' model\' , { startTime } )\n ' )
103+ f .write (f'oms_setStopTime(\' model\' , { stopTime } )\n ' )
104+ f .write (f'oms_setTolerance(\' model\' , { absTol } , { relTol } )\n ' )
105+ if fmiType == 'me' :
106+ f .write (f'oms_setVariableStepSize(\' model\' , 1e-12, 1e-12, { stepSize } )\n ' )
107+ elif fmiType == 'cs' :
108+ f .write (f'oms_setFixedStepSize(\' model\' , { stepSize } )\n ' )
109+
110+ f .write ('\n -- instantiate, initialize and simulate\n ' )
111+ f .write ('oms_instantiate(\' model\' )\n ' )
112+ f .write ('oms_initialize(\' model\' )\n ' )
113+ f .write ('oms_simulate(\' model\' )\n ' )
114+ f .write ('oms_terminate(\' model\' )\n ' )
115+ f .write ('oms_delete(\' model\' )\n ' )
111116 f .close ()
112117
113118 expFile = open (os .path .join (resultDir , "OMSimulator_exp.log" ), "w" )
@@ -119,24 +124,27 @@ def generateLua(modelName, testFMUDir, resultDir, fmiType):
119124 return luaFilePath
120125
121126
122- def simulateFMU (omsimulator , testFMUDir , resultDir , modelName , fmiType , luaFile ):
127+ def simulateFMU (omsimulator , resultDir , modelName , luaFile ):
123128 """Import and simulate a FMU with OMSimulator
124129 Run given luaFile with given OMSimulator executable and measure time.
125130 Will save outputs to "OMSimulator_out.log", "OMSimulator_err.log" and "OMSimulator_exp.log".
126131 Return call comand cmd.
127132 """
128133 # Run lua file with OMSimulator via shell
129- cmd = ["--stripRoot=true" ,
134+ cmd = omsimulator + [
135+ f'--workingDir={ resultDir } ' ,
136+ "--stripRoot=true" ,
130137 "--skipCSVHeader=true" ,
131138 "--addParametersToCSV=true" ,
132139 "--suppressPath=true" ,
133140 "--timeout=" + str (ulimitOMSimulator ),
134- os . path . relpath ( luaFile , resultDir ) ]
141+ luaFile ]
135142
136143 # Call OMSimulator and measure time
137144 simTimeStart = time .time ()
138- proc = subprocess .Popen (omsimulator + cmd , cwd = resultDir , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
139- cmd = ' ' .join (omsimulator + cmd )
145+ print (cmd )
146+ proc = subprocess .Popen (cmd , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
147+ cmd = ' ' .join (cmd )
140148 simTime = time .time () - simTimeStart
141149 (out , err ) = proc .communicate ()
142150 exitCode = proc .returncode
@@ -190,19 +198,23 @@ def importFMU(crossCheckDir, testFMUDir, resultDir, modelName, fmiType, omsimula
190198 print ("Testing " + os .path .relpath (testFMU , crossCheckDir ))
191199
192200 if not os .path .isfile (testFMU ):
193- raise Exception ("FMU \" " + testFMU + "\" not found!" )
201+ print ("FMU \" " + testFMU + "\" not found!" )
202+ return
194203
195204 os .makedirs (resultDir , exist_ok = True )
196205
197206 # Copy FMU next to Lua file
198207 testFMU = os .path .abspath (os .path .join (testFMUDir , modelName + ".fmu" ))
199208 shutil .copy (testFMU , resultDir )
209+ inputCsv = os .path .abspath (os .path .join (testFMUDir , modelName + "_in.csv" ))
210+ if os .path .exists (inputCsv ):
211+ shutil .copy (inputCsv , resultDir )
200212
201213 # Generate lua file
202214 luaFile = generateLua (modelName , testFMUDir , resultDir , fmiType )
203215
204216 # Simulate FMU with OMSimulator
205- cmd = simulateFMU (omsimulator ,testFMUDir , resultDir , modelName , fmiType , luaFile )
217+ cmd = simulateFMU (omsimulator , resultDir , modelName , luaFile )
206218
207219 # Generate README
208220 createREADME (resultDir , modelName , cmd , luaFile )
@@ -224,7 +236,7 @@ def simulateWithOMSimulator(crossCheckDir, platform, omsimulator, omsVersion):
224236 # Make sure we are in crossCheckDir
225237 os .chdir (crossCheckDir )
226238
227- # Clean up possible existing result files and temp dir
239+ # clean up temp directory
228240 tempDir = os .path .join (tempfile .gettempdir (), 'cross-check' )
229241 shutil .rmtree (tempDir , ignore_errors = True )
230242 for fmiVersion in ["2.0" ]:
0 commit comments