From e330711b2ef115562251660df0162147dd470d36 Mon Sep 17 00:00:00 2001
From: Zizhe Wang <zizhe.wang@tu-dresden.de>
Date: Thu, 4 Jul 2024 16:13:28 +0200
Subject: [PATCH] feat load libraries in orchestration workflow

---
 src/orchestration_config.json |  6 ++++++
 src/orchestration_wrapper.py  | 16 ++++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/src/orchestration_config.json b/src/orchestration_config.json
index 77174c8..8b9daca 100644
--- a/src/orchestration_config.json
+++ b/src/orchestration_config.json
@@ -45,5 +45,11 @@
         "PLOT_TITLE": "",
         "ENABLE_PLOT": false
     },
+    "LIBRARY_CONFIG": {
+        "LOAD_LIBRARIES": true,
+        "LIBRARIES": [
+            {"name": "PNlib-3.0.0", "path": "PNlib-3.0.0/PNlib/package.mo"}
+        ]
+    },
     "N_JOBS": -1
 }
\ No newline at end of file
diff --git a/src/orchestration_wrapper.py b/src/orchestration_wrapper.py
index 5531f2b..aa556c2 100644
--- a/src/orchestration_wrapper.py
+++ b/src/orchestration_wrapper.py
@@ -4,6 +4,7 @@
 import json
 import os
 from optimize_main import run_optimization
+from OMPython import OMCSessionZMQ
 
 class MOO4ModelicaWrapper:
     def __init__(self, orchestration_config_path, config_path):
@@ -11,6 +12,8 @@ class MOO4ModelicaWrapper:
         self.config_path = config_path
         self.load_configs()
         self.update_config_file()
+        self.omc = OMCSessionZMQ()
+        self.load_libraries()
 
     def load_configs(self):
         with open(self.orchestration_config_path, 'r') as f:
@@ -29,6 +32,7 @@ class MOO4ModelicaWrapper:
         self.config['OBJECTIVES'] = self.orchestration_config['OBJECTIVES']
         self.config['PLOT_CONFIG'] = self.orchestration_config['PLOT_CONFIG']
         self.config['N_JOBS'] = self.orchestration_config['N_JOBS']
+        self.config['LIBRARY_CONFIG'] = self.orchestration_config['LIBRARY_CONFIG']
         for key, value in self.orchestration_config['OPTIMIZATION_CONFIG'].items():
             self.config['OPTIMIZATION_CONFIG'][key] = value
 
@@ -45,6 +49,18 @@ class MOO4ModelicaWrapper:
         with open(self.config_path, 'w') as f:
             json.dump(self.config, f, indent=4)
 
+    def load_libraries(self):
+        if self.orchestration_config['LIBRARY_CONFIG']['LOAD_LIBRARIES']:
+            for lib in self.orchestration_config['LIBRARY_CONFIG']['LIBRARIES']:
+                library_path = os.path.join(os.getcwd(), 'models', lib['path']).replace('\\', '/')
+                print(f"Loading library: {lib['name']} from {library_path}")
+                load_library_result = self.omc.sendExpression(f'loadFile("{library_path}")')
+                print(f"Load library result: {load_library_result}")
+                if load_library_result is not True:
+                    messages = self.omc.sendExpression("getErrorString()")
+                    print(f"OpenModelica error messages: {messages}")
+                    raise RuntimeError(f"Failed to load library: {lib['name']}")
+
     def run_optimization(self):
         run_optimization()
 
-- 
GitLab