Skip to content
Snippets Groups Projects
Commit db875a8f authored by Zizhe Wang's avatar Zizhe Wang
Browse files

feat load Modelica libraries

parent 923b9028
Branches
No related tags found
No related merge requests found
...@@ -30,5 +30,11 @@ ...@@ -30,5 +30,11 @@
"PLOT_TITLE": "Pareto Front of Energy Consumption vs Comfort", "PLOT_TITLE": "Pareto Front of Energy Consumption vs Comfort",
"ENABLE_PLOT": false "ENABLE_PLOT": false
}, },
"LIBRARY_CONFIG": {
"LOAD_LIBRARIES": true,
"LIBRARIES": [
{"name": "", "path": ""}
]
},
"N_JOBS": -1 "N_JOBS": -1
} }
\ No newline at end of file
...@@ -28,9 +28,13 @@ PARAM_BOUNDS = config['PARAM_BOUNDS'] # Parameter range ...@@ -28,9 +28,13 @@ PARAM_BOUNDS = config['PARAM_BOUNDS'] # Parameter range
OPTIMIZATION_CONFIG = config['OPTIMIZATION_CONFIG'] OPTIMIZATION_CONFIG = config['OPTIMIZATION_CONFIG']
PLOT_CONFIG = config['PLOT_CONFIG'] PLOT_CONFIG = config['PLOT_CONFIG']
N_JOBS = config['N_JOBS'] N_JOBS = config['N_JOBS']
LIBRARY_CONFIG = config.get('LIBRARY_CONFIG', {})
LOAD_LIBRARIES = LIBRARY_CONFIG.get('LOAD_LIBRARIES', False)
LIBRARIES = LIBRARY_CONFIG.get('LIBRARIES', [])
# Derived configuration # Derived configuration
MODEL_PATH = os.path.join(os.getcwd(), MODEL_FILE) BASE_DIR = os.getcwd()
MODEL_PATH = os.path.join(BASE_DIR, 'models', MODEL_FILE)
# Helper functions to get bounds and types # Helper functions to get bounds and types
def get_bounds(param_bounds): def get_bounds(param_bounds):
......
...@@ -14,10 +14,23 @@ import numpy as np ...@@ -14,10 +14,23 @@ import numpy as np
from time import sleep from time import sleep
from joblib import Parallel, delayed from joblib import Parallel, delayed
from OMPython import OMCSessionZMQ from OMPython import OMCSessionZMQ
from config import MODEL_FILE, MODEL_NAME, SIMULATION_STOP_TIME, PARAMETERS, OBJECTIVE_NAMES, PARAM_BOUNDS, PARAM_TYPES, MODEL_PATH, PRECISION, N_JOBS from config import MODEL_FILE, MODEL_NAME, SIMULATION_STOP_TIME, PARAMETERS, OBJECTIVE_NAMES, PARAM_BOUNDS, PARAM_TYPES, MODEL_PATH, PRECISION, N_JOBS, LOAD_LIBRARIES, LIBRARIES
temp_dirs = [] # List to store paths of temporary directories temp_dirs = [] # List to store paths of temporary directories
def load_libraries(omc):
"""
Function to load Modelica libraries.
"""
for library in LIBRARIES:
library_path = os.path.join(os.getcwd(), 'models', library['path']).replace('\\', '/')
load_library_result = omc.sendExpression(f'loadFile("{library_path}")')
print(f"Load library result: {load_library_result}")
if load_library_result is not True:
messages = omc.sendExpression("getErrorString()")
print(f"OpenModelica error messages: {messages}")
raise RuntimeError(f"Failed to load library: {library['name']}")
def optimization_function(param_values, retries=3, delay=2): def optimization_function(param_values, retries=3, delay=2):
""" """
Function to optimize the Modelica model given specific parameter values. Function to optimize the Modelica model given specific parameter values.
...@@ -36,6 +49,9 @@ def optimization_function(param_values, retries=3, delay=2): ...@@ -36,6 +49,9 @@ def optimization_function(param_values, retries=3, delay=2):
temp_model_path = os.path.join(temp_dir, MODEL_FILE).replace('\\', '/') temp_model_path = os.path.join(temp_dir, MODEL_FILE).replace('\\', '/')
shutil.copy(MODEL_PATH, temp_model_path) shutil.copy(MODEL_PATH, temp_model_path)
if LOAD_LIBRARIES:
load_libraries(omc)
# Load the model in the worker session # Load the model in the worker session
load_temp_model_result = omc.sendExpression(f"loadFile(\"{temp_model_path}\")") load_temp_model_result = omc.sendExpression(f"loadFile(\"{temp_model_path}\")")
print(f"Load model result in worker: {load_temp_model_result}") print(f"Load model result in worker: {load_temp_model_result}")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment