-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulationBuilder.py
50 lines (43 loc) · 1.77 KB
/
simulationBuilder.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
import os
import re
import json
from pathlib import Path
from OMPython import OMCSessionZMQ
# Instantiate omc
omc = OMCSessionZMQ()
omc.sendExpression("installPackage(Modelica)")
omc.sendExpression("loadModel(Modelica)")
print(f'{omc.sendExpression("getErrorString()")}')
# Get all models
patternModel = re.compile(r".+\.mo")
modelsList = os.listdir("models")
if(not os.path.isdir("fmus")): os.mkdir("fmus")
modelsList = [file for file in modelsList if patternModel.match(file)]
omc.sendExpression(f'cd("{os.getcwd()}")')
print(f'Current OMC working directory: {omc.sendExpression("cd()")}')
for file in modelsList:
# Generate Models Path
modelsPath = Path("models")
# Open file
fileHandler = open(modelsPath / file, "r")
modelString = fileHandler.readline()
# Find model name
patternModelName = re.compile(r"\s*model\s+(\w+)", re.MULTILINE)
modelNameMatch = patternModelName.match(modelString)
modelName = modelNameMatch.group(1)
modelCMD = 'cd("models")'
print(f'Changed directory to "models": {omc.sendExpression(modelCMD)}')
print(f"Loading model file {file}")
loadSuccessful = omc.sendExpression(f'loadFile("{file}")')
if loadSuccessful:
upOneCMD = 'cd("..")'
print(f'Going back to top directory: {omc.sendExpression(upOneCMD)}')
fmusCMD = 'cd("fmus")'
print(f'Current going to "fmus" directory: {omc.sendExpression(fmusCMD)}')
print(f"Building FMU for model {modelName}")
answer = omc.sendExpression(f'buildModelFMU({modelName}, fmuType="me_cs")')
print(f'{omc.sendExpression("getErrorString()")}')
print(f"{modelName}.fmu generated")
print(f'Going back to top directory: {omc.sendExpression(upOneCMD)}')
else:
print(f"Loading file {file} was not successful")