-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdymola_simulation.py
More file actions
92 lines (76 loc) · 3.05 KB
/
Copy pathdymola_simulation.py
File metadata and controls
92 lines (76 loc) · 3.05 KB
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
import os
import platform
import time
from dymola.dymola_interface import DymolaInterface
from dymola.dymola_exception import DymolaException
def dymola_simulation(model_info, path_dymola, solver, printInfo = True):
'''
'''
# Retrieving model information
root_path = model_info['root_path']
library_path = model_info['library_path']
model_path = model_info['model_path']
model_name = model_info['model_name']
output_path = model_info['output_path']
dymola = None
try:
if printInfo:
print("Creating and starting Dymola instance")
# Creating dymola instance
dymola = DymolaInterface(dymolapath = path_dymola)
if printInfo:
print(f"Using Dymola port:" + str(dymola._portnumber))
print(f"Changing working directory to: {output_path}")
try:
if not os.path.exists(output_path):
os.makedirs(output_path)
print("Working directory created")
except OSError as ex:
print("1: Failed to create folder for working directory")
# CHANGING THE PATH TO OPENING THE LIBRARY AND THE MODEL
result = dymola.cd(root_path)
if not result:
print("1: Failed to change working directory")
# Opening OpenIPSL library
dymola.openModel(library_path)
if result and printInfo:
print("Library opened")
# Opening model
dymola.openModel(model_path)
if result and printInfo:
print("Model opened")
# CHANGING THE PATH FOR THE WORKING DIRECTORY
# Note that the model is already opened
result = dymola.cd(output_path)
if not result:
print("1: Failed to change working directory")
dymola.ExecuteCommand("Advanced.TranslationInCommandLog = true")
# Simulating the model
if solver == 'dassl':
dymola.ExecuteCommand("Advanced.Define.DAEsolver = true")
print("DAE setting changed for dassl")
if solver in ["Rkfix2", "Rkfix4", "Euler"]:
dymola.ExecuteCommand("Evaluate = true")
print("Running simulation...")
result = dymola.simulateModel(model_name, method = solver, stopTime=120, numberOfIntervals=240000, tolerance=1e-06, resultFile = model_name + "_{}".format(solver))
else:
# Running simulation for dassl
print("Running simulation...")
result = dymola.simulateModel(model_name, method = solver, stopTime=120, numberOfIntervals=5000, tolerance=1e-06, resultFile = model_name + "_{}".format(solver))
if not result:
print("Simulation failed. Below is the error log:")
log = dymola.getLastErrorLog()
print(log)
else:
print("Simulation OK")
# Close Dymola
dymola.close()
except DymolaException as ex:
if printInfo:
print(("Error: " + str(ex)))
else:
pass
finally:
if dymola is not None:
dymola.close()
dymola = None