-
-
Save DanWBR/c355fd5420d20d960f5d084a7142cde8 to your computer and use it in GitHub Desktop.
import pythoncom | |
pythoncom.CoInitialize() | |
import clr | |
from System.IO import Directory, Path, File | |
from System import String, Environment | |
dwsimpath = "C:\\Users\\Daniel\\AppData\\Local\\DWSIM8\\" | |
clr.AddReference(dwsimpath + "CapeOpen.dll") | |
clr.AddReference(dwsimpath + "DWSIM.Automation.dll") | |
clr.AddReference(dwsimpath + "DWSIM.Interfaces.dll") | |
clr.AddReference(dwsimpath + "DWSIM.GlobalSettings.dll") | |
clr.AddReference(dwsimpath + "DWSIM.SharedClasses.dll") | |
clr.AddReference(dwsimpath + "DWSIM.Thermodynamics.dll") | |
clr.AddReference(dwsimpath + "DWSIM.Thermodynamics.ThermoC.dll") | |
clr.AddReference(dwsimpath + "DWSIM.UnitOperations.dll") | |
clr.AddReference(dwsimpath + "DWSIM.Inspector.dll") | |
clr.AddReference(dwsimpath + "System.Buffers.dll") | |
from DWSIM.Interfaces.Enums.GraphicObjects import ObjectType | |
from DWSIM.Thermodynamics import Streams, PropertyPackages | |
from DWSIM.UnitOperations import UnitOperations | |
from DWSIM.Automation import Automation3 | |
from DWSIM.GlobalSettings import Settings | |
Directory.SetCurrentDirectory(dwsimpath) | |
# create automation manager | |
interf = Automation3() | |
sim = interf.CreateFlowsheet() | |
# add water | |
water = sim.AvailableCompounds["Water"] | |
sim.SelectedCompounds.Add(water.Name, water) | |
# create and connect objects | |
m1 = sim.AddObject(ObjectType.MaterialStream, 50, 50, "inlet") | |
m2 = sim.AddObject(ObjectType.MaterialStream, 150, 50, "outlet") | |
e1 = sim.AddObject(ObjectType.EnergyStream, 100, 50, "power") | |
h1 = sim.AddObject(ObjectType.Heater, 100, 50, "heater") | |
m1 = m1.GetAsObject() | |
m2 = m2.GetAsObject() | |
e1 = e1.GetAsObject() | |
h1 = h1.GetAsObject() | |
sim.ConnectObjects(m1.GraphicObject, h1.GraphicObject, -1, -1) | |
sim.ConnectObjects(h1.GraphicObject, m2.GraphicObject, -1, -1) | |
sim.ConnectObjects(e1.GraphicObject, h1.GraphicObject, -1, -1) | |
sim.AutoLayout() | |
# steam tables property package | |
stables = PropertyPackages.SteamTablesPropertyPackage() | |
sim.AddPropertyPackage(stables) | |
# set inlet stream temperature | |
# default properties: T = 298.15 K, P = 101325 Pa, Mass Flow = 1 kg/s | |
m1.SetTemperature(300.0) # K | |
m1.SetMassFlow(100.0) # kg/s | |
# set heater outlet temperature | |
h1.CalcMode = UnitOperations.Heater.CalculationMode.OutletTemperature | |
h1.OutletTemperature = 400 # K | |
# request a calculation | |
Settings.SolverMode = 0 | |
errors = interf.CalculateFlowsheet2(sim) | |
print(String.Format("Heater Heat Load: {0} kW", h1.DeltaQ)) | |
# save file | |
fileNameToSave = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "heatersample.dwxmz") | |
interf.SaveFlowsheet(sim, fileNameToSave, True) | |
# save the pfd to an image and display it | |
clr.AddReference(dwsimpath + "SkiaSharp.dll") | |
clr.AddReference("System.Drawing") | |
from SkiaSharp import SKBitmap, SKImage, SKCanvas, SKEncodedImageFormat | |
from System.IO import MemoryStream | |
from System.Drawing import Image | |
from System.Drawing.Imaging import ImageFormat | |
PFDSurface = sim.GetSurface() | |
bmp = SKBitmap(1024, 768) | |
canvas = SKCanvas(bmp) | |
canvas.Scale(1.0) | |
PFDSurface.UpdateCanvas(canvas) | |
d = SKImage.FromBitmap(bmp).Encode(SKEncodedImageFormat.Png, 100) | |
str = MemoryStream() | |
d.SaveTo(str) | |
image = Image.FromStream(str) | |
imgPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "pfd.png") | |
image.Save(imgPath, ImageFormat.Png) | |
str.Dispose() | |
canvas.Dispose() | |
bmp.Dispose() | |
from PIL import Image | |
im = Image.open(imgPath) | |
im.show() |
@VarunK21 on macOS you need to install pythonnet v2.x, not v3.x which uses with clr_loader.
Also, you need to download the DWSIM app bundle from the downloads page, and then change the path to DWSIM libraries inside the python script to something like this, assuming that you put DWSIM.app on the Desktop:
dwsimpath = "/Users/[username]/Desktop/DWSIM.app/Contents/MonoBundle"
@VarunK21 on macOS you need to install pythonnet v2.x, not v3.x which uses with clr_loader.
Also, you need to download the DWSIM app bundle from the downloads page, and then change the path to DWSIM libraries inside the python script to something like this, assuming that you put DWSIM.app on the Desktop:
dwsimpath = "/Users/[username]/Desktop/DWSIM.app/Contents/MonoBundle"
@DanWBR sir, thank you for the constant support . Tried doing the same as suggested by you i.e., installing pythonnet 2.x and then importing clr , was able to import almost all the modules from DWSIM but two modules named 'GlobalSettings' and 'Streams' are throwing errors rather the kernel is dying while importing these two . Could you suggest what could be the reasons and work around for the same .
Hi @DanWBR sir, please help with this error message while executing the code in macOS -
Error message -
*Assertion assembly.c:1939, condition 'is_ok (hook _error)' not met,
function:mono_assembly_invoke_load_hook_internal, (null)
assembly:/Users/manish.dighore/opt/anaconda3/lib/mono/4.5/mscorlib.dll type:ReflectionTypeLoadException member :
(null)
============================================
Native Crash Reporting
Got a abrt while executing native code . This usually indicates a fatal error in the mono runtime or one of the native libraries used by our
application .
Flowsheet initialization and everything else is working perfectly fine, the line where we call self.interf.CalculateFlowsheet2(self.Sim)
causes the above error .
Hi all, can you share your results if any of you have successfully run the codes.
Hi all, can you share your results if any of you have successfully run the codes.
My results:
Reactor Heat Load: -66.5 kW
Ethylene oxide conversion: 27.95%
Water conversion: 6.98%
Hi all, If I want to perform exactly this reaction with Arrhenius via the Script Manager, how do I specify the quantities of the reactants?
Is the following valid? => water is R1, ethylene oxide is R2 and ethylene glycol is P1
A = 0.5 E = 0.0 with k1=A*math.exp(E)
and then how do I specify the reaction r1?
thanks in advance
I guess that your question isn't related to this gist, is it?
But yes, because I am trying to figure out how to work with the Script Manager and would like to run this reaction via the Script Manager as a test, since I know the results. Still have a few problems to handle Script Manager...
I think these questions can be asked in forum. There are several Q&A on Script Manager in forum answered by @DanWBR also you can find shared solution.
Thank you for providing this code. I have a problem with mono compatibility (or maybe pythonnet compatibility) with my M2 macbook. So, I didn't succeed in running the code. when I use (import clr) i a get a long error including (OSError: cannot load library '/Library/Frameworks/Mono.framework/Versions/6.12.0/lib/libmonosgen-2.0.dylib':). i tried to fix the problem with (command export DYLD_LIBRARY_PATH="/Library/Frameworks/Mono.framework/Versions/6.12.0/lib:$DYLD_LIBRARY_PATH"
) switching to mono 6.8 and installing different versions of pythonnet. but, the problem is still there. Is there any solution to fix or bypass this problem?
On tiny10 Win x64 I have python 10 through miniconda
Environment variables [System Path]:
C:\Users\ukhan\miniconda3\Scripts\;C:\Users\ukhan\miniconda3\Library\bin\;C:\Users\ukhan\miniconda3\;
then u need conda install pythonnet pywin32 -y
Everything works
I am trying to automate the simulation process so that instead of manually creating flowsheets everytime, I just pass a json file with connections given in the form of dictionaries i.e., pair value form between different equipments and store the results of certain parameters in csv file in the computer .