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?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Going through all object contained inside a flowsheet using a for loop. For each object generate a IScript dictionary as follows:
importclrimportSystemimportuuidclr.AddReference("DWSIM.Interfaces")fromSystem.Collections.GenericimportList,DictionaryimportDWSIM.InterfacesdefcreateScript(obj_name):#GUID = str(uuid.uuid1())GUID=obj_nameFlowsheet.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 = GUIDFlowsheet.ScriptCollection.TryGetValue(GUID)[1].Linked=TrueFlowsheet.ScriptCollection.TryGetValue(GUID)[1].LinkedEventType=DWSIM.Interfaces.Enums.Scripts.EventType.ObjectCalculationStartedFlowsheet.ScriptCollection.TryGetValue(GUID)[1].LinkedObjectName=obj_nameFlowsheet.ScriptCollection.TryGetValue(GUID)[1].LinkedObjectType=DWSIM.Interfaces.Enums.Scripts.ObjectType.FlowsheetObjectFlowsheet.ScriptCollection.TryGetValue(GUID)[1].PythonInterpreter=DWSIM.Interfaces.Enums.Scripts.Interpreter.IronPythonFlowsheet.ScriptCollection.TryGetValue(GUID)[1].Title=obj_nameFlowsheet.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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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 flowsheetinterf=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! :)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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?
Can't you use the Solver Started event?
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.
Solution UPDATE:
Going through all object contained inside a flowsheet using a for loop. For each object generate a IScript dictionary as follows:
Once the scripts are generated you need to restart DWSIM to get the tabs if you added a unique ID.
Last edit: AlbertoB 2023-02-16
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:
But I must have missed something, cause this dosen't work.
Thanks in advance! :)