From 2ccb4d356e552ced14c6433b3d7441c85d16beaa Mon Sep 17 00:00:00 2001
From: Bichen <libichen94@gmail.com>
Date: Fri, 20 Jul 2018 14:38:47 +0200
Subject: [PATCH] 1. Fix some path bugs when the tool is used on Linux 2. Add
 an encoding declaration 3. Fix bugs of the environment name in Linux 4. Fix
 the terminate bug in OMInterface class

---
 Py4Mod/py4mod/ModelicaModel.py                 |  8 +++++---
 Py4Mod/py4mod/OMInterface.py                   | 10 +++++++---
 Scripts/ModPowerSystemsCI/RunOMSimulationCI.py | 16 ++++++++++++----
 3 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/Py4Mod/py4mod/ModelicaModel.py b/Py4Mod/py4mod/ModelicaModel.py
index 7fb8d80..a806289 100644
--- a/Py4Mod/py4mod/ModelicaModel.py
+++ b/Py4Mod/py4mod/ModelicaModel.py
@@ -1,3 +1,5 @@
+# -*- coding: latin -*-
+
 from shutil import copyfile
 from shutil import copy
 from bisect import bisect
@@ -13,8 +15,8 @@ class ModelicaModel(object):
 		"""
 		self._model = model # model identifier in package hierarchy [e.g. packageName.subPackageName.modelName]
 		self._mainPath = mainPath # path where either the model file itself or the corresponding package.mo can be found
-		if not self._mainPath[-1] == "\\":
-			self._mainPath = self._mainPath + "\\"
+		if not self._mainPath[-1] == "/":
+			self._mainPath = self._mainPath + "/"
 		self._modelName = self.getModelName() # actual name of model extracted from self._model
 		self._modelPath = self.__getModelPath() # complete path of the model file deduced from self._mainPath and self._model			  
 		self._modelLines = self.__readModel()
@@ -476,7 +478,7 @@ class ModelicaModel(object):
 
 	def __getModelPath(self):
 		"""Creates the modelpath out of the projectpath and the model."""
-		modelpath = re.sub(r"\.", "\\\\", self._model)
+		modelpath = re.sub(r"\.", "/", self._model)
 		modelpath = self._mainPath + modelpath + ".mo"
 		return  modelpath
 
diff --git a/Py4Mod/py4mod/OMInterface.py b/Py4Mod/py4mod/OMInterface.py
index 166294a..8351108 100644
--- a/Py4Mod/py4mod/OMInterface.py
+++ b/Py4Mod/py4mod/OMInterface.py
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
 import os
 import sys
 import platform
@@ -256,7 +257,9 @@ class OMInterface(object):
 		#check_exeFile_ = os.path.exists(exeFile)		
 			
 		if (os.path.exists(exeFile)):
-			omhome = os.path.join(os.environ.get("OPENMODELICAHOME"), 'bin').replace("\\", "/")
+			tmp_to_change = "/tmp/OpenModelica_" + os.environ['LOGNAME'] + "/OMEdit/"
+			#omhome = os.path.join(os.environ.get("OPENMODELICAHOME"), 'bin').replace("\\", "/")
+			omhome = os.path.join(tmp_to_change, 'bin').replace("\\", "/")
 			my_env = os.environ.copy()
 			my_env["PATH"] = omhome + os.pathsep + my_env["PATH"]
 			#process = subprocess.Popen(cmd, cwd=self.changeWorkingDirectory(), env=my_env)
@@ -280,10 +283,11 @@ class OMInterface(object):
 						#print(output)
 						
 			rc = process.poll()
-			process.terminate()
+			if rc is not 0:
+				process.terminate()
 			
 			if (rc is not 0):
-				raise Exception(str(stderr))	#not tested
+				raise Exception(str(stderr))	# not tested
 			
 			self.__simulationFlag = True
 			self.resultFileName = os.path.join(self.changeWorkingDirectory(), self.modelName + '_res.mat')
diff --git a/Scripts/ModPowerSystemsCI/RunOMSimulationCI.py b/Scripts/ModPowerSystemsCI/RunOMSimulationCI.py
index f4146c7..33d609b 100644
--- a/Scripts/ModPowerSystemsCI/RunOMSimulationCI.py
+++ b/Scripts/ModPowerSystemsCI/RunOMSimulationCI.py
@@ -1,6 +1,7 @@
 import os
 import sys
-sys.path.append(r"D:\HIWI\Git\python-for-modelica-dev_interface\Py4Mod\py4mod")
+os.chdir(r"/home/cafi/Desktop/CITest")
+sys.path.append(r"./python-for-modelica/Py4Mod/py4mod")
 
 from ModelicaModel import ModelicaModel
 
@@ -9,15 +10,22 @@ def simulate_modelica(model_name, model_path):
 
     # Initialization
     interface.createInterface("OPENMODELICA")
-    interface.loadFile(model_path + '\ModPowerSystems\package.mo')
+    interface.loadFile("/home/cafi/Desktop/CITest/modpowersystems/ModPowerSystems/package.mo")
 
     # Redirection
     cwd = os.getcwd()
-    wd = os.path.join(cwd, 'test')
+    wd = os.path.join(cwd, model_name)
     if not os.path.exists(wd):
         os.makedirs(wd)
     interface.changeWorkingDirectory(wd.replace("\\", "/"))
 
     # Build & Run
     interface.buildModel()
-    interface.simulate()
\ No newline at end of file
+    interface.simulate()
+
+
+name = 'ModPowerSystems.DynPhasorThreePhase.Examples.BasicCircuits.VoltageSource_RL'
+path = './modpowersystems'
+
+
+simulate_modelica(name, path)
\ No newline at end of file
-- 
GitLab