From db875a8f87993e5a0b40b940cdb304aafe865374 Mon Sep 17 00:00:00 2001
From: Zizhe Wang <zizhe.wang@tu-dresden.de>
Date: Wed, 3 Jul 2024 16:42:28 +0200
Subject: [PATCH] feat load Modelica libraries

---
 src/config.json           |  6 ++++++
 src/config.py             |  6 +++++-
 src/parallel_computing.py | 18 +++++++++++++++++-
 3 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/src/config.json b/src/config.json
index d30634f..da762d2 100644
--- a/src/config.json
+++ b/src/config.json
@@ -30,5 +30,11 @@
         "PLOT_TITLE": "Pareto Front of Energy Consumption vs Comfort",
         "ENABLE_PLOT": false
     },
+    "LIBRARY_CONFIG": {
+        "LOAD_LIBRARIES": true,
+        "LIBRARIES": [
+            {"name": "", "path": ""}
+        ]
+    },
     "N_JOBS": -1
 }
\ No newline at end of file
diff --git a/src/config.py b/src/config.py
index 129e5cb..f8413b8 100644
--- a/src/config.py
+++ b/src/config.py
@@ -28,9 +28,13 @@ PARAM_BOUNDS = config['PARAM_BOUNDS']  # Parameter range
 OPTIMIZATION_CONFIG = config['OPTIMIZATION_CONFIG']
 PLOT_CONFIG = config['PLOT_CONFIG']
 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
-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
 def get_bounds(param_bounds):
diff --git a/src/parallel_computing.py b/src/parallel_computing.py
index 83e7302..ce12e65 100644
--- a/src/parallel_computing.py
+++ b/src/parallel_computing.py
@@ -14,10 +14,23 @@ import numpy as np
 from time import sleep
 from joblib import Parallel, delayed
 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
 
+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):
     """
     Function to optimize the Modelica model given specific parameter values.
@@ -36,6 +49,9 @@ def optimization_function(param_values, retries=3, delay=2):
             temp_model_path = os.path.join(temp_dir, MODEL_FILE).replace('\\', '/')
             shutil.copy(MODEL_PATH, temp_model_path)
 
+            if LOAD_LIBRARIES:
+                load_libraries(omc)
+
             # Load the model in the worker session
             load_temp_model_result = omc.sendExpression(f"loadFile(\"{temp_model_path}\")")
             print(f"Load model result in worker: {load_temp_model_result}")
-- 
GitLab