Menu

Catch events when an object starts calculating

AlbertoB
2023-02-14
2023-03-24
  • AlbertoB

    AlbertoB - 2023-02-14

    Dear all

    I am programming a series of functions which they need to be linked to the start of the calculation of each specific object. I know I can do it through the script manager and that works great. The problem comes when I have over 50 objects. I don't want to have 50 scripts each one linked to their own object.

    Is there a way I can catch the calculationStarted event so I can run an specific function? I went through the code and apparently there is no event creation it is just a direct call to the ProcessScripts function which looks for the script linked to that object and runs it.

    I guess my options are:
    1. Program my own sequential solver routine
    2. Automate the creation of pythonscripts each of them linked to an object and with a callback to an specific function on a global script.

    What are your thoughts?

     
  • Daniel Medeiros

    Daniel Medeiros - 2023-02-14

    Can't you use the Solver Started event?

     
  • AlbertoB

    AlbertoB - 2023-02-15

    Sure, the problem is that I do not know which object is being calculated. Ideally would be to have an event which tells that a new calculation in an object started and which object is being calculated. In that way I can have a single function which is getting that info and doing something instead of having a script link to an event for every object in my flow sheet.

     
  • AlbertoB

    AlbertoB - 2023-02-16

    Solution UPDATE:

    Going through all object contained inside a flowsheet using a for loop. For each object generate a IScript dictionary as follows:

    import clr
    import System
    import uuid
    clr.AddReference("DWSIM.Interfaces")
    from System.Collections.Generic import List, Dictionary
    import DWSIM.Interfaces
    
    def createScript(obj_name):
        #GUID = str(uuid.uuid1())
        GUID = obj_name
        Flowsheet.ScriptCollection.Add(GUID, DWSIM.FlowsheetSolver.Script())
        #By not declaring the ID the tabs of each script is not loaded
        #Flowsheet.ScriptCollection.TryGetValue(GUID)[1].ID = GUID
        Flowsheet.ScriptCollection.TryGetValue(GUID)[1].Linked = True
        Flowsheet.ScriptCollection.TryGetValue(GUID)[1].LinkedEventType = DWSIM.Interfaces.Enums.Scripts.EventType.ObjectCalculationStarted
        Flowsheet.ScriptCollection.TryGetValue(GUID)[1].LinkedObjectName = obj_name
        Flowsheet.ScriptCollection.TryGetValue(GUID)[1].LinkedObjectType = DWSIM.Interfaces.Enums.Scripts.ObjectType.FlowsheetObject
        Flowsheet.ScriptCollection.TryGetValue(GUID)[1].PythonInterpreter = DWSIM.Interfaces.Enums.Scripts.Interpreter.IronPython
        Flowsheet.ScriptCollection.TryGetValue(GUID)[1].Title = obj_name
        Flowsheet.ScriptCollection.TryGetValue(GUID)[1].ScriptText = str("Whatever code of Python you want")
    

    Once the scripts are generated you need to restart DWSIM to get the tabs if you added a unique ID.

     
    👍
    2

    Last edit: AlbertoB 2023-02-16
  • Ella Daiki

    Ella Daiki - 2023-03-24

    Hello,
    tank you for sharing the code, it was a big help!!
    However, I do not know how to access the flowsheet that I have previously loaded into DWSIM via python as well. In order to then define, for example, the temperature of the material flows and the outlet temperature of the reactor.
    I tried something like the this:

    # create new flowsheet
    interf = Automation()
    sim = interf.CreateFlowsheet()
    
    # define Script Manager
    # [...] as in the post above 
    
        sim.Scripts.TryGetValue(GUID)[1].ScriptText = str('MyReaction\n'
                                                          '\n import math\n'
                                                         '\n obj = sim.GetFlowsheetSimulationObject(pfr)'
                                                         # pfr was defined bevor as well as input and output
                                                          '\n obj.set_OutletTemperature(300.0)')
    
    createScript('MyReaction')
    

    But I must have missed something, cause this dosen't work.
    Thanks in advance! :)

     
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.