KULI newsletter background

Controlling KULI from external programs in a safe and efficient way

 

When you run a KULI simulation from within the KULI GUI, everything is done automatically. KULI always calls its own calculation kernel in an efficient way, controls the time steps and the iteration process, and in particular, displays information, warnings and errors in the appropriate windows. When you run a KULI simulation via the COM interface, it is the responsibility of the user to write the script in a way that similar functionality is provided. Using “events” is a recommended way to do this. So what is an “event”? An event is information sent out (“fired”) by the KULI calculation kernel whenever it has reached a certain state, finished some calculation part, detected an error, etc. KULI does not require any action in order to proceed, but gives the possibility to take some action.

 

Examples:

When you perform a transient simulation and KULI has finished a time step, then KULI fires the event “OnEndOfTimeStep”, which essentially means KULI is telling you “I have finished the current time step, do you want to do anything?” In your script you can now implement this event and read the current sensor values within this event. In the same way, KULI fires the event “OnNextTime” at the beginning of a new time step. Within this event, you could set boundary conditions for this new time step. The big advantage of using these events is that you could run a transient simulation just by using the command “RunAnalysis” or “SimulateOperatingPoint(0)” and do all the setting and reading of parameters within events. You don’t need to take care about controlling time steps or iterations.

 

The main structure of the script would be this one – VBA code as an example:

 

Sub runkuli_()

    Set KULI = New KuliAnalysisCtr2

    KULI.KuliFileName = “C:/mykulifile.scs”

    calcOK = KULI.Initialize()

    KULI.SimulateOperatingPoint (0)

    KULI.CleanUp

    Set KULI = Nothing   

End Sub

 

Private Sub KULI_OnNextTime(ByVal itNo As Long, ByVal curTime As Double)

    newvalue = Cells(curTime, 1)

    KULI.SetCOMValueByID(“myInputCOMID”, newvalue)

End Sub

 

Private Sub KULI_OnEndOfTimeStep(ByVal itNo As Long, ByVal curTime As Double)

myresult = KULI.GetCOMValueByID(“myOutputCOMID”)

Cells(curTime, 2) = myresult

End Sub

 

The item “KULI / EXCEL - Transient Simulation” in the KULI online library gives a detailed example of how to implement these events in Visual Basic (i.e., in an Excel Macro).

In the case of steady state simulations, you could run a KULI file that contains a parameter variation with the help of the command “RunParameterVariation()” and read the results within the event “OnEndOfOperatingPoint”.

The “OnError” event is the most important event, which KULI fires when it has detected an error. KULI also provides information about the severity of the error and some text describing the error within this event. If you do not handle this event, then a more or less severe error could remain undetected and wrong, or strange results could be given in the end. Therefore, it is strongly recommended to handle the “OnError” event. When received, the “OnMessage” event should also be handled. A suggested implementation for these two events can be found in the online help and in the online library in the item “KULI / EXCEL – Steady State Simulation”.

 

Figure 1: Different error message styles depending on error severity

 

The KULI online help provides detailed information for all the events in the section KULI advanced -> advanced com -> Controlling KULI from external applications -> List of events

Learn more in the KULI online help.