Menu

DWSIM-7.5.1: python and Automation2 under linux

Anonymous
2022-05-04
2022-05-05
  • Anonymous

    Anonymous - 2022-05-04

    I am trying to get Automation2 to work under linux (Debian 11) from python (3.9.2), but I keep running into

    System.NullReferenceException: Object reference not set to an instance of an object
    

    The new GUI works ok on my machine, the classic UI crashes, if that's any help.

    I started from here: https://gist.github.com/DanWBR/c355fd5420d20d960f5d084a7142cde8

    pythoncom is windows specific, so I skipped it. I have no idea if this is critical (if it were, using dwsim from python under linux would be a no-go area...)

    So my test script looks like this:

    #! /usr/bin/python3
    import clr
    import sys
    import glob
    
    from System.IO import Directory, Path, File
    
    dwsimpath = "/usr/local/lib/dwsim/"
    
    # Add all dlls
    for dll in glob.glob(dwsimpath+"*.dll"):
        try:
            clr.AddReference(dll)
            print("Added reference to %s" %dll)
        except BaseException as e:
            print("Could not add reference to %s (invalid image?)" %dll)
    
    import DWSIM.Automation
    
    # Results in: System.NullReferenceException: Object reference not set to an instance of an object
    interf = DWSIM.Automation.Automation2()
    

    Running it produces the following output:

    [lots of lines concerned with references to dlls]
    
    Initializing DWSIM Automation Interface...
    
    
    APP CRASH!!!
    
    System.IndexOutOfRangeException: Index was outside the bounds of the array.
      at Gtk.Application.SetPrgname () [0x00005] in <91487213bf394f4aaf87012594abed2f>:0 
      at Gtk.Application.do_init (System.String progname, System.String[]& args, System.Boolean check) [0x00000] in <91487213bf394f4aaf87012594abed2f>:0 
      at Gtk.Application.InitCheck (System.String progname, System.String[]& args) [0x00000] in <91487213bf394f4aaf87012594abed2f>:0 
      at Eto.GtkSharp.Helper.Init () [0x00007] in <2084ba8256c9420ea60ad1e5f1df14e5>:0 
      at Eto.GtkSharp.Forms.ApplicationHandler.Initialize () [0x00006] in <2084ba8256c9420ea60ad1e5f1df14e5>:0 
      at Eto.WidgetHandler`1[TWidget].Eto.Widget.IHandler.Initialize () [0x00000] in <23bf6400f02d49eba883a8238fbdb959>:0 
      at Eto.Widget.Initialize () [0x0000a] in <23bf6400f02d49eba883a8238fbdb959>:0 
      at Eto.Widget..ctor () [0x00099] in <23bf6400f02d49eba883a8238fbdb959>:0 
      at Eto.Forms.Application..ctor (Eto.Forms.Application+InitHelper init) [0x00016] in <23bf6400f02d49eba883a8238fbdb959>:0 
      at Eto.Forms.Application..ctor (Eto.Platform platform) [0x00007] in <23bf6400f02d49eba883a8238fbdb959>:0 
      at DWSIM.UI.Desktop.Program.MainApp (System.String[] args) [0x00531] in <fc86f5821461452cb699997cb9ceb6e9>:0 
    Traceback (most recent call last):
      File "./processSimulation/00_pythonScripting/./runDWSim.py", line 21, in <module>
        interf = DWSIM.Automation.Automation2()
    System.NullReferenceException: Object reference not set to an instance of an object
      at DWSIM.Automation.Automation2..ctor () [0x0003a] in <f5f021e4dcfa42b2bd8b0e4b0d93a743>:0 
      at (wrapper managed-to-native) System.Reflection.RuntimeConstructorInfo.InternalInvoke(System.Reflection.RuntimeConstructorInfo,object,object[],System.Exception&)
      at System.Reflection.RuntimeConstructorInfo.InternalInvoke (System.Object obj, System.Object[] parameters, System.Boolean wrapExceptions) [0x00008] in <12b418a7818c4ca0893feeaaf67f1e7f>:0 
    

    Note the nullptr in

    at Eto.WidgetHandler`1[TWidget].Eto.Widget.IHandler.Initialize () [0x00000] in <23bf6400f02d49eba883a8238fbdb959>:0 
    

    There are a couple of topics related to this, but no solution is available as of today:

    Only occurs when trying to create a second instance: https://sourceforge.net/p/dwsim/discussion/scripting/thread/c84c1f2adb/
    Unresolved, user switched to c#: https://sourceforge.net/p/dwsim/discussion/linux_newui/thread/c473d78387/

    I put together a small c# example, and that works (as in "does NOT crash"):

    using System;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    
    using DWSIM.Automation;
    using DWSIM.Interfaces;
    
    
    namespace DWSIM_test
    {
        class MainClass
        {
            static string DWSIMLibrariesDir = "/usr/local/lib/dwsim/";
    
            static Assembly LoadFromSameFolder(object sender, ResolveEventArgs args)
            {
                string assemblyPath = Path.Combine(DWSIMLibrariesDir, new AssemblyName(args.Name).Name + ".dll");
                if (!File.Exists(assemblyPath))
                {
                    string assemblyPath2 = Path.Combine(DWSIMLibrariesDir, new AssemblyName(args.Name).Name + ".exe");
                    if (!File.Exists(assemblyPath2))
                    {
                        return null;
                    }
                    else
                    {
                        Assembly assembly = Assembly.LoadFrom(assemblyPath2);
                        return assembly;
                    }
                }
                else
                {
                    Assembly assembly = Assembly.LoadFrom(assemblyPath);
                    return assembly;
                }
            }
    
            static void Main()
            {
    
                // sets the assembly resolver to find remaining DWSIM libraries on demand
                AppDomain currentDomain = AppDomain.CurrentDomain;
                currentDomain.AssemblyResolve += new ResolveEventHandler(LoadFromSameFolder);
    
                System.IO.Directory.SetCurrentDirectory(DWSIMLibrariesDir);
    
                //create automation manager
                var interf = new DWSIM.Automation.Automation2();
    
                DWSIM.Interfaces.IFlowsheet sim;
    
                Console.WriteLine("Finished OK!");
    
            }
    
        }
    }
    

    Under linux, I compile it with the following makefile:

    all:
        mcs -lib:/usr/local/lib/dwsim  -reference:CapeOpen.dll -reference:DWSIM.Automation.dll -reference:DWSIM.Interfaces.dll -reference:DWSIM.SharedClasses.dll  -reference:DWSIM.Thermodynamics.dll -reference:DWSIM.UnitOperations.dll dwsimtest.cs
    

    For running the resulting dwsimtest.exe, I need to

    export MONO_PATH=/usr/local/lib/dwsim
    

    From this, I think that:
    1. Generally, mono etc. do work and I am able to create an Automation2 instance from c# code
    2. This does not work from python code.

    So, what might be the problem? Is it my installation of python-3.9.2? A bug? Incompatible mono version?

     
  • Daniel Medeiros

    Daniel Medeiros - 2022-05-04

    Hi Jordi,

    This code works on Linux: https://gist.github.com/DanWBR/55cba7856f12cdde92a9e3a87f5194b9

    It works with mono 6.8 but crashes with 6.12.

    You must install this version of Python.NET: https://pypi.org/project/pythonnet/3.0.0a2/

    In your Documents/DWSIM Application Data folder, open dwsim_newui.ini and change the Linux renderer in [PlatformRenderers] from Gtk2 to WinForms. This will render DWSIM unusable in GUI mode, but gets Automation mode working.

    There are some problems with setting flowsheet object properties from python, as they seem to have no effect. I'm investigating this.

    Regards
    Daniel

     
  • Anonymous

    Anonymous - 2022-05-04

    Hi Daniel,

    thank you for your quick reply. I have been partially successful!

    1. The installed mono version is Mono JIT compiler version 6.8.0.105 (Debian 6.8.0.105+dfsg-3.2 Tue Jun 29 21:01:01 UTC 2021),
    2. I uninstalled pythonnet-3.0.0-dev1 and installed version 3.0.0a2
    3. Changed the platformRenderer for Linux to WinForms

    I was able to run both my testing script and your example.
    1. My testing script (which does not do anything yet) segfaulted after successfully creating the Automation2 object
    2. Your example crashed upon saving the flowsheet:

    Initializing DWSIM Automation Interface...
    DWSIM Automation Interface initialized successfully.
    Initializing the Flowsheet, please wait...
    start creating workbook...
    creating workbook done: 0 ms.
    start creating worksheet...
    init srm takes 63 ms.
    creating worksheet done: 71 ms.
    Set ScrollBarHorizontalLargeChange to -1
    Set ScrollBarVerticalLargeChange to -1
    Renderer Dispose Time: 0.111 ms
    update viewport bounds done: 13 ms.
    LayoutConnectedGraphWithMds: nodes 4 edges 3
    done with LayoutConnectedComponents
    Outlet Temperature: 323.928082035709 K
    Saving the Flowsheet, please wait...
    Traceback (most recent call last):
      File "[path_stripped]/fromDaniel.py", line 84, in <module>
        interf.SaveFlowsheet(sim, fileNameToSave, True)
    System.NullReferenceException: Object reference not set to an instance of an object
      at Microsoft.VisualBasic.ApplicationServices.AssemblyInfo.get_Version () [0x00001] in <8620e8455c354b54923c1dae63fe3fd6>:0 
      at DWSIM.FlowsheetBase.FlowsheetBase.SaveToXML () [0x0007a] in <69bdc17eb00240a199257f27bf7c0529>:0 
      at DWSIM.UI.Forms.Flowsheet.SaveSimulation (System.String path, System.Boolean backup) [0x00054] in <54f755f687d241dcb825ff38397ba96b>:0 
      at DWSIM.Automation.Automation2.SaveFlowsheet (DWSIM.Interfaces.IFlowsheet flowsheet, System.String filepath, System.Boolean compressed) [0x0001b] in <f5f021e4dcfa42b2bd8b0e4b0d93a743>:0 
      at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
      at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0007c] in <12b418a7818c4ca0893feeaaf67f1e7f>:0 
    

    Without saving the flowsheet, it wrote the png file to my Desktop folder and then segfaulted like my script did.

    I think I should be able to work with this as it is now. Any idea about the flowsheet saving crash?

     
  • Daniel Medeiros

    Daniel Medeiros - 2022-05-04

    The crash issue was fixed in DWSIM 7.5.5, Jordy.

     
  • Anonymous

    Anonymous - 2022-05-05

    Excellent, now everything runs fine! Both scripts still segfault at the end, but that does not hamper my progress.

    I was not aware of version 7.5.5 because the Download button still takes us to 7.5.1.

    Anyway, thank you very much! If you ever come to Cologne, I'd like to buy you a beer or three.

     
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.