Menu

Custom UO Python-Script Calculate and Recalculate

2020-06-12
2020-06-15
  • Luca Finardi

    Luca Finardi - 2020-06-12

    Hello Daniel,

    I'd like to ask your help on the attached simulation (created in R6.2).

    I have an heater and a Custom Python-Script UO17 with 2 input variables W1 and W2.
    Since UO17 is not connected to other streams, it does not enter in the flowsheet calculation (Alt+Ctrl F5 ignores it) so I need a script to have it calculated.

    I wrote a Script1 linked to Solver-Start that takes a material stream's property, copies it in UO17 variable W1 and then activates the UO17 calculation through an obj.Calculate().
    I also made a Script2 linked to UO17 calculation's start that copies in W2 another stream's property.

    So I expect the calculation's flow to be: Solver Start -> Script1 -> Script2 -> UO17 Calc. What I see instead is: Solver Start -> Script1 -> Error (unclear) or, pressing again F5, Solver Start -> Script1 without updating W1 -> UO17 Calc.

    Therefore my scripts do not work, but ...
    But selecting UO17 on the flowsheet and doing a right-button + Recalculate, all my scripts work perfectly and the two variables W1 and W2 are updated as expected without errors.

    What is the difference between the normal script Calculate() and the routine activated by Recalculate? and how can I get the same Recalculate results through my script?

    Thank in advance.

    Luca

     
  • Daniel Medeiros

    Daniel Medeiros - 2020-06-13

    When you call obj.Calculate(), it doesn't go through the Flowsheet Solver, it goes directly to the Unit Operation calculation routine isolatedly. You can, however, hold CTRL+ALT while pressing the Solve Flowsheet button to change the calculation order, but UO17 won't appear for the reasons you've know already.

    You can also unlink Script2 and modify Script1 to run Script2 directly:

    obj = Flowsheet.GetFlowsheetSimulationObject('UO-017')
    zobj = Flowsheet.GetFlowsheetSimulationObject('MSTR-016')
    
    Flowsheet.WriteMessage('Updating W1...')
    obj.InputVariables['W1']  = zobj.GetPhase('Overall').Properties.volumetric_flow # m3/s
    
    import clr, System
    clr.AddReference('System.Core')
    clr.ImportExtensions(System.Linq)
    
    Script2 = Flowsheet.Scripts.Values.Where(lambda x: x.Title == 'Script2').FirstOrDefault().ScriptText.replace('\r', '')
    exec(Script2)
    
    Flowsheet.WriteMessage('... now calculating UO17')
    obj.Calculate()
    
     
  • Luca Finardi

    Luca Finardi - 2020-06-15

    Thanks Daniel for the explanation on obj.Calculate(). But what instead does the right-click + Recalculate?
    Yes, to run directly Script2 through Linq is a very useful suggestion.
    Thanks again.
    Luca

     
    • Daniel Medeiros

      Daniel Medeiros - 2020-06-15

      It calls the flowsheet solver sending the object as the argument. The solver then starts calculating the flowsheet from the object.

       

Log in to post a comment.