Menu

Include User Libraies

Mohammad
2021-08-26
2021-08-27
  • Mohammad

    Mohammad - 2021-08-26

    In Script Manager one can add several scripts!
    Is there any way one script use another? I mean if we implement solver in one script and want to call it from another, how is this possible in DWSIM?

    For Custom Unit Operation, DWSIM create two windows under script: they are called: Script and Includes
    Is it possible to add user libraries, user solver and codes to be used in the Script? I mean by import keyword!

     
  • Mohammad

    Mohammad - 2021-08-26

    Thank you Daniel!
    Does this mean if I have a solver in tab 2 for example to solve a BVP the I have to add that code to be able to call it in tab 1:

    import clr
    import System
    from System import *
    
    clr.AddReference('System.Core')
    clr.ImportExtensions(System.Linq)
    
    # get the script text from "Functions" using LINQ
    source = Flowsheet.Scripts.Values.Where(lambda x: x.Title == 'Functions').FirstOrDefault().ScriptText.replace('\r', '')
    
    # execute the script
    exec(source)
    

    Why not use import mylib if tab 2 has name mylib?

     
  • Daniel Medeiros

    Daniel Medeiros - 2021-08-26

    Honestly I don't know how to use import with libraries in other tabs...

     
  • Mohammad

    Mohammad - 2021-08-26

    Thank you for your prompt reply! I will update you here if I can find any better solution!

     
  • Mohammad

    Mohammad - 2021-08-27

    Hi Daniel,

    I think I am able to successfully import user modules (when stored on disk) in DWSIM (both through UO script editor, and main Script Manager)

    Example 1: Use absolute path
    Assume you have user modules in G:\DWSIM\python\read-write-object-data\libs and want to use in your simulation

    # Import modules by adding absolute path
    
    import sys
    
    sys.path.append(r'G:\DWSIM\python\read-write-object-data\libs')
    Flowsheet.WriteMessage(str(sys.path))
    
    import mylib
    
    x = mylib.Rg
    
    Flowsheet.WriteMessage(str(x))
    
    Flowsheet.WriteMessage("finished successfully...")
    

    Example 2: Use relative path
    Assume you have user modules in libs directory which is located as a child or sub directory with respect to the current simulation file and want to use your module in your simulation

    import sys
    home = Flowsheet.FilePath
    Flowsheet.WriteMessage(home)
    home = home.rsplit('\\',1)[0]
    Flowsheet.WriteMessage(home)
    
    # add relative path now
    sys.path.append(home +r"\libs")
    
    # TEST
    import mylib
    x = mylib.Rg
    Flowsheet.WriteMessage(str(x))
    Flowsheet.WriteMessage("finished...")
    

    Here using the home = Flowsheet.FilePath the absolute path of current simulation is retrieved! and then after removing the file name the calculated path is added to module search path! you can then add your sub directory or ant path relative to the current simulation path!

    • Those write messages are only for demo.
     

    Last edit: Mohammad 2021-08-27
  • Mohammad

    Mohammad - 2021-08-27

    Daniel
    This worth to be documented! If Flowsheet can expose its home directory then the above solution is much more easier!

     
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.