From 0eb009a3a3e028ddaaebb1410fd20e904adc417d Mon Sep 17 00:00:00 2001 From: Daniel Medeiros Date: Thu, 15 Jun 2023 20:13:58 -0400 Subject: [PATCH] v8.5.0 RC2; fixed reaktoro initialization --- .../ActivityCoefficients.vb | 20 ++++---- .../ReaktoroFlash.vb | 19 +++++--- .../ReaktoroLoader.vb | 46 ++++++++++--------- .../Reactors/ReaktoroGibbs.vb | 44 ++++++++++-------- 4 files changed, 72 insertions(+), 57 deletions(-) diff --git a/DWSIM.Thermodynamics.ReaktoroPropertyPackage/ActivityCoefficients.vb b/DWSIM.Thermodynamics.ReaktoroPropertyPackage/ActivityCoefficients.vb index 4a51fa87e..14cac65fe 100644 --- a/DWSIM.Thermodynamics.ReaktoroPropertyPackage/ActivityCoefficients.vb +++ b/DWSIM.Thermodynamics.ReaktoroPropertyPackage/ActivityCoefficients.vb @@ -73,17 +73,21 @@ Public Class ActivityCoefficients Try - Dim sys As Object = Py.Import("sys") - sys.path.append(libpath) + If libpath <> "" Then - Dim os As Object = Py.Import("os") + Dim sys As Object = Py.Import("sys") + sys.path.append(libpath) - Dim dllpath = Path.Combine(libpath, "reaktoro") - Dim shareddllpath = Path.Combine(Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location), "python_packages", "reaktoro_shared") + Dim os As Object = Py.Import("os") - os.add_dll_directory(dllpath) - os.add_dll_directory(shareddllpath) - os.add_dll_directory(Settings.PythonPath) + Dim dllpath = Path.Combine(libpath, "reaktoro") + Dim shareddllpath = Path.Combine(Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location), "python_packages", "reaktoro_shared") + + os.add_dll_directory(dllpath) + os.add_dll_directory(shareddllpath) + os.add_dll_directory(Settings.PythonPath) + + End If Dim codeToRedirectOutput As String = "import sys" & Environment.NewLine + "from io import BytesIO as StringIO" & Environment.NewLine + "sys.stdout = mystdout = StringIO()" & Environment.NewLine + "sys.stdout.flush()" & Environment.NewLine + "sys.stderr = mystderr = StringIO()" & Environment.NewLine + "sys.stderr.flush()" diff --git a/DWSIM.Thermodynamics.ReaktoroPropertyPackage/ReaktoroFlash.vb b/DWSIM.Thermodynamics.ReaktoroPropertyPackage/ReaktoroFlash.vb index 3c3cc7851..7a0e42681 100644 --- a/DWSIM.Thermodynamics.ReaktoroPropertyPackage/ReaktoroFlash.vb +++ b/DWSIM.Thermodynamics.ReaktoroPropertyPackage/ReaktoroFlash.vb @@ -153,16 +153,21 @@ Imports DWSIM.GlobalSettings Dim ex0 As Exception = Nothing Dim sys As Object = Py.Import("sys") - sys.path.append(libpath) - Dim os As Object = Py.Import("os") + If libpath <> "" Then - Dim dllpath = Path.Combine(libpath, "reaktoro") - Dim shareddllpath = Path.Combine(Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location), "python_packages", "reaktoro_shared") + sys.path.append(libpath) - os.add_dll_directory(dllpath) - os.add_dll_directory(shareddllpath) - os.add_dll_directory(Settings.PythonPath) + Dim os As Object = Py.Import("os") + + Dim dllpath = Path.Combine(libpath, "reaktoro") + Dim shareddllpath = Path.Combine(Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location), "python_packages", "reaktoro_shared") + + os.add_dll_directory(dllpath) + os.add_dll_directory(shareddllpath) + os.add_dll_directory(Settings.PythonPath) + + End If Try diff --git a/DWSIM.Thermodynamics.ReaktoroPropertyPackage/ReaktoroLoader.vb b/DWSIM.Thermodynamics.ReaktoroPropertyPackage/ReaktoroLoader.vb index e4b49aa69..a2f76254e 100644 --- a/DWSIM.Thermodynamics.ReaktoroPropertyPackage/ReaktoroLoader.vb +++ b/DWSIM.Thermodynamics.ReaktoroPropertyPackage/ReaktoroLoader.vb @@ -6,37 +6,39 @@ Public Class ReaktoroLoader Public Shared Function Initialize() As String - If Settings.RunningPlatform() <> Settings.Platform.Windows Then + If Settings.RunningPlatform() = Settings.Platform.Windows Then - Throw New Exception("The Reaktoro Property Package is not available on Linux/macOS.") + Dim pyver = PythonEngine.Version - End If + Dim libpath = "", dllpath, shareddllpath As String - Dim pyver = PythonEngine.Version + If pyver.Contains("3.7.") Then + libpath = Path.Combine(Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location), "python_packages", "reaktoro_py37") + ElseIf pyver.Contains("3.8.") Then + libpath = Path.Combine(Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location), "python_packages", "reaktoro_py38") + ElseIf pyver.Contains("3.9.") Then + libpath = Path.Combine(Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location), "python_packages", "reaktoro_py39") + Else + Throw New Exception("Reaktoro requires a Python distribution version between 3.7 and 3.9 (inclusive). Found version " & pyver) + End If - Dim libpath = "", dllpath, shareddllpath As String + dllpath = Path.Combine(libpath, "reaktoro") + shareddllpath = Path.Combine(Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location), "python_packages", "reaktoro_shared") - If pyver.Contains("3.7.") Then - libpath = Path.Combine(Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location), "python_packages", "reaktoro_py37") - ElseIf pyver.Contains("3.8.") Then - libpath = Path.Combine(Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location), "python_packages", "reaktoro_py38") - ElseIf pyver.Contains("3.9.") Then - libpath = Path.Combine(Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location), "python_packages", "reaktoro_py39") - Else - Throw New Exception("Reaktoro requires a Python distribution version between 3.7 and 3.9 (inclusive). Found version " & pyver) - End If + Dim append As String = Settings.PythonPath + ";" + Path.Combine(Settings.PythonPath, "Library", "bin") + + ";" + dllpath + ";" + shareddllpath + ";" + Dim p1 As String = append + Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine) + Environment.SetEnvironmentVariable("PATH", p1, EnvironmentVariableTarget.Process) - dllpath = Path.Combine(libpath, "reaktoro") - shareddllpath = Path.Combine(Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location), "python_packages", "reaktoro_shared") + LoadReaktoroLib(Path.Combine(dllpath, "Reaktoro.dll")) - Dim append As String = Settings.PythonPath + ";" + Path.Combine(Settings.PythonPath, "Library", "bin") + - ";" + dllpath + ";" + shareddllpath + ";" - Dim p1 As String = append + Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine) - Environment.SetEnvironmentVariable("PATH", p1, EnvironmentVariableTarget.Process) + Return libpath - LoadReaktoroLib(Path.Combine(dllpath, "Reaktoro.dll")) + Else + + Return "" - Return libpath + End If End Function diff --git a/DWSIM.UnitOperations/Reactors/ReaktoroGibbs.vb b/DWSIM.UnitOperations/Reactors/ReaktoroGibbs.vb index 2b99f43b0..2e878cf26 100644 --- a/DWSIM.UnitOperations/Reactors/ReaktoroGibbs.vb +++ b/DWSIM.UnitOperations/Reactors/ReaktoroGibbs.vb @@ -159,17 +159,21 @@ Namespace Reactors Using Py.GIL - Dim sys As Object = Py.Import("sys") - sys.path.append(libpath) + If libpath <> "" Then - Dim os As Object = Py.Import("os") + Dim sys As Object = Py.Import("sys") + sys.path.append(libpath) - Dim dllpath = Path.Combine(libpath, "reaktoro") - Dim shareddllpath = Path.Combine(Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location), "python_packages", "reaktoro_shared") + Dim os As Object = Py.Import("os") - os.add_dll_directory(dllpath) - os.add_dll_directory(shareddllpath) - os.add_dll_directory(Settings.PythonPath) + Dim dllpath = Path.Combine(libpath, "reaktoro") + Dim shareddllpath = Path.Combine(Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location), "python_packages", "reaktoro_shared") + + os.add_dll_directory(dllpath) + os.add_dll_directory(shareddllpath) + os.add_dll_directory(Settings.PythonPath) + + End If Dim reaktoro As Object = Py.Import("reaktoro") @@ -607,27 +611,27 @@ Namespace Reactors DWSIM.GlobalSettings.Settings.InitializePythonEnvironment() - Else - - Throw New Exception("This Unit Operation is not available on Linux/macOS.") - End If Dim libpath = DWSIM.Thermodynamics.ReaktoroPropertyPackage.ReaktoroLoader.Initialize() Using Py.GIL - Dim sys As Object = Py.Import("sys") - sys.path.append(libpath) + If libpath <> "" Then + + Dim sys As Object = Py.Import("sys") + sys.path.append(libpath) - Dim os As Object = Py.Import("os") + Dim os As Object = Py.Import("os") - Dim dllpath = Path.Combine(libpath, "reaktoro") - Dim shareddllpath = Path.Combine(Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location), "python_packages", "reaktoro_shared") + Dim dllpath = Path.Combine(libpath, "reaktoro") + Dim shareddllpath = Path.Combine(Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location), "python_packages", "reaktoro_shared") - os.add_dll_directory(dllpath) - os.add_dll_directory(shareddllpath) - os.add_dll_directory(Settings.PythonPath) + os.add_dll_directory(dllpath) + os.add_dll_directory(shareddllpath) + os.add_dll_directory(Settings.PythonPath) + + End If Dim reaktoro As Object = Py.Import("reaktoro")