Menu

Trouble in using Cantera in DWSIm

Pietro
2021-12-29
2024-08-28
  • Pietro

    Pietro - 2021-12-29

    Dear all,
    I am trying to run the following script in Script Manager with Python.net as execution engine:

    import cantera as ct
    import numpy as np
    import System
    from System import IO

    ct.add_directory(IO.Path.GetDirectoryName(Flowsheet.FilePath))
    gas = ct.Solution('C:\Users\User1\AppData\Local\DWSIM6\samples\ChemDetailed.cti')

    Previously I did the following:
    1) I installed the portable Python distro as suggested by Daniel in this post (https://sourceforge.net/p/dwsim/discussion/844529/thread/652ccd224d/?limit=25.)
    2) I installed cantera in the portable Python distro and made sure that Cantera works, by running a simple Cantera example in the Python console of the distro.
    3) I set the directory C:\Users\User1l\Desktop\WPy64-3980\python-3.9.8.amd64 as Python 3.x DLL Directory in DWSim>General Settings>Other

    Now, by running the script with the line "gas=..." hidden, the script is computed successfully. Thus, I assume that cantera is successfully imported. When I run the script including the line "gas=...", an additional DWSim interface opens (the "Welcome to DWSim" interface). Afterwards, the script is processed for quite long time until DWSim crashes.

    Do you have an idea about what happens? Why a new Dwsim interface opens? Could you please help me in solving this bug?

    Thank you so much,
    Pietro

     
  • Pietro

    Pietro - 2022-01-08

    I found a solution: I substituted ChemDetailed.cti with ChemDetailed.yaml and it works. But couldn't say why...

     
  • Pietro

    Pietro - 2022-01-08

    But unfortunately I face another issue: I am still trying to model a unit operation using Python.Net as interpreter. When I run the lines

    import System
    oms1.Phases[0].Properties.temperature = System.NullableSystem.Double

    I get the error:

    2   08/01/2022 10:49:31 Error   module 'System' has no attribute 'Nullable'
    

    File "<string>", line 5, in <module>
    at Python.Runtime.PythonException.ThrowLastAsClrException()
    at Python.Runtime.PythonException.ThrowIfIsNull(BorrowedReference ob)
    at Python.Runtime.PythonEngine.RunString(String code, BorrowedReference globals, BorrowedReference locals, RunFlagType flag)
    at Python.Runtime.PythonEngine.Exec(String code, Nullable1 globals, Nullable1 locals)
    at DWSIM.UnitOperations.UnitOperations.CustomUO.Calculate(Object args) in C:\Users\Daniel\source\repos\DanWBR\dwsim6\DWSIM.UnitOperations\UnitOperations\PythonScriptUO.vb:line 347 </module></string>

    If I set IronPython as Interpreter I don't get this error.  For now,  I can't find any explaination...
    I use DWSIm version 6.7 and set C:\Users\User1l\Desktop\WPy64-3980\python-3.9.8.amd64 as Python 3.x DLL Directory in DWSim>General Settings>Other
    
    Any help is very appreciated....Thank you so much!!!
    
     
  • Pietro

    Pietro - 2022-01-10

    Hi guys,

    I realized a typing error in the previous post. The code I run is:

    import System
    oms1.Phases[0].Properties.temperature = System.Nullable [ System.Double ] (T)

    I also installed in my PC the version 3.6.8 of the portable Python (https://sourceforge.net/projects/winpython/files/WinPython_3.6/3.6.8.0/) and set it in the as Python 3.x DLL Directory in DWSim>General Settings>Other. Still I get the error:

    module 'System' has no attribute 'Nullable'

    At this point, for now I don't have a clue on how to go on. Any help is very deeply appreciated!

    By the way, what is "System" in DWSim?

    Thank you so much again for your support!!!

     

    Last edit: Pietro 2022-01-10
  • Daniel Medeiros

    Daniel Medeiros - 2022-01-10

    To set the temperature, you can now use this command:

    oms1.SetTemperature(T)
    
     
  • Pietro

    Pietro - 2022-01-11

    Thank you so much for the reply! Can I use the command
    oms1.SetTemperature(T) with Python.NET as python Execution Engine?

    I run:

    T = ims1.Phases[0].Properties.temperature
    oms1.Phases[0].Properties.temperature = oms1.SetTemperature(T)

    And I still get the error

    Error must be real number, not str
    File "<string>", line 67, in <module>
    at Python.Runtime.PythonException.ThrowLastAsClrException()
    at Python.Runtime.PythonException.ThrowIfIsNull(BorrowedReference ob)
    at Python.Runtime.PythonEngine.RunString(String code, BorrowedReference globals, BorrowedReference locals, RunFlagType flag)
    at Python.Runtime.PythonEngine.Exec(String code, Nullable1 globals, Nullable1 locals)
    at DWSIM.UnitOperations.UnitOperations.CustomUO.Calculate(Object args) in C:\Users\Daniel\source\repos\DanWBR\dwsim6\DWSIM.UnitOperations\UnitOperations\PythonScriptUO.vb:line 347 + Info</module></string>

    Must T be a double?
    
     
  • Pietro

    Pietro - 2022-01-11

    Ok, I found the problem.
    oms1.Phases[0].Properties.temperature = oms1.SetTemperature(T)
    is simply wrong.
    I just have to run
    oms1.SetTemperature(T)
    as suggested.

    Thank you so much for the support! And sorry for the spam...

     
  • Pietro

    Pietro - 2022-01-11

    Ok, I found the problem.
    oms1.Phases[0].Properties.temperature = oms1.SetTemperature(T)
    is simply wrong.
    I just have to run
    oms1.SetTemperature(T)
    as suggested.

    Thank you so much for the support! And sorry for the spam...

     
  • Nik

    Nik - 2024-08-19

    Hi everyone,
    I am new to DWSIM and I am trying to set a model based on Cantera Interop.
    I am using python 3.9 (portable version as suggested by Daniel) and have checked already that Cantera is installed and functioning. All good till here.

    The issue I have is similar to what Pietro reported here above a couple of years back:

    when running:
    "oms1.Phases[0].Compounds['Methane'].MoleFraction = System.NullableSystem.Double"

    I get an error message saying:
    "Error in 'Python.Runtime': module 'System' has no attribute 'Nullable'"

    I have checked directly in python and "System" works well.
    I have also set the new way of passing macro variables like T and P usign setTemperature, which work well.

    I am stucking when setting the composition as I am not able to find neither a solution to "System" nor an alternative as for SetTemperature or SetPressure.

    Any suggestion?

    Thanks a lot
    Nik

     
  • Daniel Medeiros

    Daniel Medeiros - 2024-08-19

    try using

    oms1.SetOverallCompoundMolarFlow('Methane', methane_molar_flow)
    
     
  • Nik

    Nik - 2024-08-19

    Thanks, Daniel.
    I got another error saying "an integer is required".
    I can go around the issue by rounding the number but that would not be numerically correct. Something I should do to troubleshoot the code?

    Set the properties on the Outlet Material Stream connected to Port 1(oms1)

    oms1.Clear()

    Set operative conditions

    oms1.SetTemperature(T)
    oms1.SetPressure(P)
    oms1.SetMassFlow(WF)

    Read mass fractions from cantera mixture. See the original 'gri30.yaml' file to inspect compound indexes in the array

    outYCH4 = gas1.Y[13]
    outYH2O = gas1.Y[5]
    outYCO = gas1.Y[14]
    outYCO2 = gas1.Y[15]
    outYH2 = gas1.Y[0]
    outYO2 = gas1.Y[3]

    Calculate mass fractions

    outWFCH4 = WFoutYCH4
    outWFH2O = WF
    outYH2O
    outWFCO = WFoutYCO
    outWFCO2 = WF
    outYCO2
    outWFH2 = WFoutYH2
    outWFO2 = WF
    outYO2

    Set compound mass flow

    oms1.SetOverallCompoundMassFlow('Methane', round(outWFCH4))
    oms1.SetOverallCompoundMassFlow('Water', round(outWFH2O))
    oms1.SetOverallCompoundMassFlow('Carbon monoxide', round(outWFCO))
    oms1.SetOverallCompoundMassFlow('Carbon dioxide', round(outWFCO2))
    oms1.SetOverallCompoundMassFlow('Hydrogen', round(outWFH2))
    oms1.SetOverallCompoundMassFlow('Oxygen', round(outWFO2))

    In the history log I got:
    This error was raised during the calculation of a Unit Operation or Material Stream.

    System.Exception: No method matches given arguments for MaterialStream.SetOverallCompoundMolarFlow: (<class 'str'="">, <class 'numpy.float64'="">) ......</class></class>

     

    Last edit: Nik 2024-08-19
  • Daniel Medeiros

    Daniel Medeiros - 2024-08-19

    maybe it is because this function has two variations and python only sees the one where the first argument is the compound index. also be careful to not misunderstand mass/molar and flow/fraction.

     
  • Nik

    Nik - 2024-08-19

    Thanks, Daniel.
    I will correct the calculations above to make sure mass and molar balances are respected.

    About the compound index, I guess it is the one defined in DWSIM: how can I get to know compound indexes? is there any function I could call?

    Anyhow, the note on the function says "String". see attached screenshot

    Thanks a lot

     

    Last edit: Nik 2024-08-19
  • Nik

    Nik - 2024-08-20

    Hi again,
    I have tried with creating a project from scratch without involving Cantera and the function i.e. oms1.SetOverallCompoundMassFlow('Methane',outCH4) works as expected.

    Is there a way to print outputs from a Python script for debugging?

    thanks a lot

     
  • Nik

    Nik - 2024-08-28

    Solved using "float" in the Set function:
    oms1.SetOverallCompoundMassFlow('Methane',float(outWFCH4))

     

Log in to post a comment.