Menu

Is there a guide on how to configure DWSIM and Python installs to work togeather?

2024-05-10
2025-08-14
  • Mark Burgoyne

    Mark Burgoyne - 2024-05-10

    I'm having the similar issues as reported in this post , with errors using Automation2() and then it telling me that it could not load System.Windows.Forms when I tried Automation3()

    I am frankly not sure if I should be using Python, or IronPython, whether the version of Python matters, or whether there are other libraries / frameworks I should have also installed

    It would be really helpful for me if someone could outline how someone starting from a fresh system (no DWSIM, no Python, no NET frameworks etc) needs to set things up to get this working

    BTW - I'm on a Win11 system, and the error message(s) I have been getting are when trying to run the below starter script;

    Thanks in advance

    Mark

    from pythonnet import load
    load("coreclr")
    
    import clr
    
    # Load the DWSIM automation assembly
    clr.AddReference('DWSIM.Automation')
    
    dwsimpath = "C:\\Path\\to\\DWSIM\\"
    
    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")
    
    # Import the necessary namespaces from DWSIM
    from DWSIM.Automation import Automation3
    #from DWSIM.Automation import Automation2
    
    def calculate_mixture_density(pressure, temperature, co2_fraction, h2s_fraction):
        # Initialize DWSIM Automation Interface
        interop = Automation3()
        #interop = Automation2()
    
        # Create a new material stream in DWSIM
        stream = interop.CreateMaterialStream()
    
        # Set the compounds to CO2 and H2S
        stream.SetCompound("CarbonDioxide")
        stream.SetCompound("HydrogenSulfide")
    
        # Define the molar fractions
        stream.SetMolarFraction("CarbonDioxide", co2_fraction)
        stream.SetMolarFraction("HydrogenSulfide", h2s_fraction)
    
        # Define the temperature (K) and pressure (Pa)
        stream.SetTemperature(temperature)
        stream.SetPressure(pressure)
    
        # Set the thermodynamic model to GERG2008 EOS
        stream.SetPropertyPackage("GERG2008")
    
        # Calculate properties
        interop.CalculateAllProperties(stream)
    
        # Get the density of the mixture
        density = stream.GetDensity()
    
        # Release the COM object
        stream.Release()
    
        return density
    
    # Example usage
    pressure = 5000000  # in Pascals (5 MPa)
    temperature = 300  # in Kelvin
    co2_fraction = 0.7  # 70% CO2
    h2s_fraction = 0.3  # 30% H2S
    density = calculate_mixture_density(pressure, temperature, co2_fraction, h2s_fraction)
    print(f"Density of CO2 and H2S mixture at {pressure} Pa and {temperature} K is {density} kg/m³")
    
     

    Last edit: Mark Burgoyne 2024-05-10
  • Daniel Medeiros

    Daniel Medeiros - 2024-05-10

    what are the error messages?

     
    • Mark Burgoyne

      Mark Burgoyne - 2024-05-11

      Thanks Daniel. When I use the Automation2(), I get the following;

      NullReferenceException: Object reference not set to an instance of an object.
      at DWSIM.Automation.Automation2..ctor() in C:\Users\Daniel\source\repos\DanWBR\dwsim\DWSIM.Automation\Automation.cs:line 135
      at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
      at System.Reflection.MethodBaseInvoker.InvokeConstructorWithoutAlloc(Object obj, Boolean wrapInTargetInvocationException)

      And when I try the Automation3(), I get the following;


      FileNotFoundException Traceback (most recent call last)
      FileNotFoundException: Could not load file or assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.
      File name: 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
      at DWSIM.FlowsheetBase.FlowsheetBase._Closure$__187-0._Lambda$__9()
      at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
      --- End of stack trace from previous location ---
      at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
      at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)

      The above exception was the direct cause of the following exception:

      AggregateException Traceback (most recent call last)
      Cell In[4], line 66
      64 co2_fraction = 0.7 # 70% CO2
      65 h2s_fraction = 0.3 # 30% H2S
      ---> 66 density = calculate_mixture_density(pressure, temperature, co2_fraction, h2s_fraction)
      67 print(f"Density of CO2 and H2S mixture at {pressure} Pa and {temperature} K is {density} kg/m³")

      Cell In[4], line 30, in calculate_mixture_density(pressure, temperature, co2_fraction, h2s_fraction)
      27 def calculate_mixture_density(pressure, temperature, co2_fraction, h2s_fraction):
      28 # Initialize DWSIM Automation Interface
      29 #interop = Automation2()
      ---> 30 interop = Automation3()
      32 # Create a new material stream in DWSIM
      33 stream = interop.CreateMaterialStream()

      AggregateException: One or more errors occurred. (Could not load file or assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.) (Could not load file or assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.) (Could not load file or assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.)
      ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.
      File name: 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
      at DWSIM.FlowsheetBase.FlowsheetBase._Closure$__187-0._Lambda$__9()
      at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
      --- End of stack trace from previous location ---
      at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
      at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
      --- End of inner exception stack trace ---
      at System.Threading.Tasks.Task.WaitAllCore(Task[] tasks, Int32 millisecondsTimeout, CancellationToken cancellationToken)
      at System.Threading.Tasks.Task.WaitAll(Task[] tasks)
      at DWSIM.FlowsheetBase.FlowsheetBase.AddPropPacks() in C:\Users\Daniel\source\repos\DanWBR\dwsim\DWSIM.FlowsheetBase\FlowsheetBase.vb:line 3461
      at DWSIM.Automation.Automation3..ctor() in C:\Users\Daniel\source\repos\DanWBR\dwsim\DWSIM.Automation\Automation.cs:line 244
      at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
      at System.Reflection.MethodBaseInvoker.InvokeConstructorWithoutAlloc(Object obj, Boolean wrapInTargetInvocationException)
      ---> (Inner Exception #1) System.IO.FileNotFoundException: Could not load file or assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.
      File name: 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
      at DWSIM.FlowsheetBase.FlowsheetBase._Closure$__187-0._Lambda$__11()
      at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
      --- End of stack trace from previous location ---
      at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
      at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)<---

      ---> (Inner Exception #2) System.IO.FileNotFoundException: Could not load file or assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.
      File name: 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
      at DWSIM.FlowsheetBase.FlowsheetBase._Closure$__187-0._Lambda$__12()
      at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
      --- End of stack trace from previous location ---
      at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
      at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)<---

       
  • Daniel Medeiros

    Daniel Medeiros - 2024-05-11

    try replacing

    load("coreclr")

    by

    load("netfx")

     
    • Mark Burgoyne

      Mark Burgoyne - 2024-05-11

      Ok, after that I get a different error, and I also get the same missing attribute error when I try again with Automation2;


      AttributeError Traceback (most recent call last)
      Cell In[1], line 67
      65 co2_fraction = 0.7 # 70% CO2
      66 h2s_fraction = 0.3 # 30% H2S
      ---> 67 density = calculate_mixture_density(pressure, temperature, co2_fraction, h2s_fraction)
      68 print(f"Density of CO2 and H2S mixture at {pressure} Pa and {temperature} K is {density} kg/m³")

      Cell In[1], line 34, in calculate_mixture_density(pressure, temperature, co2_fraction, h2s_fraction)
      31 interop = Automation3()
      33 # Create a new material stream in DWSIM
      ---> 34 stream = interop.CreateMaterialStream()
      36 # Set the compounds to CO2 and H2S
      37 stream.SetCompound("CarbonDioxide")

      AttributeError: 'Automation3' object has no attribute 'CreateMaterialStream'

       

      Last edit: Mark Burgoyne 2024-05-11
  • Daniel Medeiros

    Daniel Medeiros - 2024-05-11
     
    • Mark Burgoyne

      Mark Burgoyne - 2024-05-12

      That seemed to work just fine, including creating the flow diagram image

       
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.