I have written a custom transient model in Python for a thermal energy storage system that tracks the internal temperature profile of the tank using the inlet temperature, pressure, and mass flow rate for a time step and subsequently calculates the exit temperature and pressure. I'm trying to figure out how to integrate it into a cycle that would include other dynamic components - such as a pump, turbine, and heat exchanger. Is this something that is possible with DWSIM? I see that there is a Python Script object under the User Models category, but it looks like I need to put the script into the script editor which I assume runs the script for each iteration, whereas I currently have a module that tracks the complex state between iterations and I need to interface with it. Thanks.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks for the answer. I've been trying to get the Script Manager working with my code but have been unable to do so. Running scripts with IronPython is working right out of the box, but since it is based on the archaic Python 2.7.12 I would have to make significant changes to my code which was written with Python 3.11 and the versions of the packages required wouldn't be available.
I see there is also Python.NET support, which will support up to Python 3.9, however I keep receiving this error when using it:
Error running script '': Python.Runtime.PythonException: a bytes-like object is required, not 'str'
at Python.Runtime.PythonException.ThrowLastAsClrException()
at Python.Runtime.PythonException.ThrowIfIsNull(NewReference& ob)
at Python.Runtime.PythonEngine.RunString(String code, BorrowedReference globals, BorrowedReference locals, RunFlagType flag)
at Python.Runtime.PythonEngine.Exec(String code, PyDict globals, PyObject locals)
at DWSIM.FormScript.RunScript_PythonNET(String scripttitle, String scripttext, FormFlowsheet fsheet) in C:\Users\Daniel\source\repos\DanWBR\dwsim\DWSIM\Forms\FormScript.vb:line 313
I've tried setting the Python 3.x DLL Directory in the General Settings several different ways. Using a traditional Python 3.9 installation as well as the portable Python 3.9 installation from winpython as suggested in the settings menu. Do you know what could be causing this error?
Also, is there any other way to run Python scripts in a separate interpreter asynchronously with an API or something like that? The code simply needs the temperature, pressure, and flow rate of the inlet and will return the temperature and pressure of the outlet.
Thank you.
Cory
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Sorry for the late reply. Yes, I was trying to print() something as a check that something is working.
Are you saying that a separate Python program can be running and communicate over a COM serial port with an IronPython script that handles the input and outputs from the custom DWSIM unit?
EDIT: I realize now that IronPython was running Python 2.7 and so print had to be used instead. Additionally, I was able to connect an IronPython script to an external Python program using socket; however, I'm a bit confused on how to initialize things at the beginning of the simulation for the Python Unit in the flowsheet and trigger the calculation of output based on input state for a dynamic simulation. Ideally I would connect to the external script when it initializes and maintain that connection throughout every update, but I don't know how to accomplish that from a single script so that the socket reference doesn't get removed.
Last edit: Cory 2023-12-12
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have written a custom transient model in Python for a thermal energy storage system that tracks the internal temperature profile of the tank using the inlet temperature, pressure, and mass flow rate for a time step and subsequently calculates the exit temperature and pressure. I'm trying to figure out how to integrate it into a cycle that would include other dynamic components - such as a pump, turbine, and heat exchanger. Is this something that is possible with DWSIM? I see that there is a Python Script object under the User Models category, but it looks like I need to put the script into the script editor which I assume runs the script for each iteration, whereas I currently have a module that tracks the complex state between iterations and I need to interface with it. Thanks.
You can use the python script unit operation + script manager with scripts linked to integrator events.
Hi Daniel,
Thanks for the answer. I've been trying to get the Script Manager working with my code but have been unable to do so. Running scripts with IronPython is working right out of the box, but since it is based on the archaic Python 2.7.12 I would have to make significant changes to my code which was written with Python 3.11 and the versions of the packages required wouldn't be available.
I see there is also Python.NET support, which will support up to Python 3.9, however I keep receiving this error when using it:
I've tried setting the Python 3.x DLL Directory in the General Settings several different ways. Using a traditional Python 3.9 installation as well as the portable Python 3.9 installation from winpython as suggested in the settings menu. Do you know what could be causing this error?
Also, is there any other way to run Python scripts in a separate interpreter asynchronously with an API or something like that? The code simply needs the temperature, pressure, and flow rate of the inlet and will return the temperature and pressure of the outlet.
Thank you.
Cory
Hi Cory,
Are you trying to
print()
something?Regarding the API thing, if you have something that can be seen through COM or .NET, then you can run it using IronPython.
Sorry for the late reply. Yes, I was trying to
print()
something as a check that something is working.Are you saying that a separate Python program can be running and communicate over a COM serial port with an IronPython script that handles the input and outputs from the custom DWSIM unit?
EDIT: I realize now that IronPython was running Python 2.7 and so
print
had to be used instead. Additionally, I was able to connect an IronPython script to an external Python program usingsocket
; however, I'm a bit confused on how to initialize things at the beginning of the simulation for the Python Unit in the flowsheet and trigger the calculation of output based on input state for a dynamic simulation. Ideally I would connect to the external script when it initializes and maintain that connection throughout every update, but I don't know how to accomplish that from a single script so that the socket reference doesn't get removed.Last edit: Cory 2023-12-12