Skip to content

Commit

Permalink
v8.5.0 RC2; fixed reaktoro initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
DanWBR committed Jun 16, 2023
1 parent 42afe67 commit 0eb009a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()"

Expand Down
19 changes: 12 additions & 7 deletions DWSIM.Thermodynamics.ReaktoroPropertyPackage/ReaktoroFlash.vb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
46 changes: 24 additions & 22 deletions DWSIM.Thermodynamics.ReaktoroPropertyPackage/ReaktoroLoader.vb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
44 changes: 24 additions & 20 deletions DWSIM.UnitOperations/Reactors/ReaktoroGibbs.vb
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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")

Expand Down

0 comments on commit 0eb009a

Please sign in to comment.