Engee documentation

Callbacks

Do not confuse the callbacks described in this article with library callbacks. DiffEqCallbacks.jl. Read more about working with them in Event Handling and Callback Functions.

Model Callbacks — these are functions that are called automatically in response to certain events in the model. Engee callbacks are created in a programming language Julia.

To open callbacks, go to the settings window lk 5 and click Edit source code pen button graphs:

callbacks start 1 en

All Engee callbacks are available in the menu that opens. They are empty by default and do not contain any code.:

callback start 2 en

All Engee callbacks can be roughly divided into 4 groups:

Opening the model

Model Execution

Saving the model

Closing the model

PreLoadFunc

PostLoadFunc

InitFunc

StartFunc

PauseFunc

ContinueFunc

StopFunc

PreSaveFunc

PostSaveFunc

CloseFunc

Description of callbacks

Opening the model

They are used to adjust the behavior of the model before and after opening.

_ Learn more about model opening callbacks_

Callbacks used: PreLoadFunc (opening) and PostLoadFunc (closing).

Description:

  • PreLoadFunc — runs before loading the model.

    Software control methods cannot be used in the callback of this model, because the model has not yet been loaded.
  • PostLoadFunc — performed after loading the model. You can use the model parameters in it, since they are already loaded.

Callbacks based on the model opening condition allow:

  • Prepare the Engee workspace or perform initialization tasks before loading the model using PreLoadFunc;

  • Configure the model parameters or perform actions that depend on the uploaded data after loading the model using PostLoadFunc.

Model Execution

They are used in the simulation process.

_ Learn more about model execution callbacks_

Callbacks used: InitFunc (initialization), StartFunc (launch), PauseFunc (pause), ContinueFunc (execution), StopFunc (stop).

When you start the model and stop the simulation process at least once, all these functions will work.

Description:

  • InitFunc — performed after the compilation stage of the model;

  • StartFunc — runs until the first step of the simulation;

  • PauseFunc — called in case of a pause during the simulation;

  • ContinueFunc — runs in one step when starting the model after a pause;

  • StopFunc — it is executed during the shutdown of the simulation process.

Saving the model

Used when saving the model.

_ Learn more about model save callbacks_

Callbacks used: PreSave (before saving), PostSave (after saving)

Description:

  • PreSave — performed before saving the model;

  • PostSave — executed after saving the model.

Closing the model

Used when closing the model.

_ Learn more about model closure callbacks_

Callbacks used: CloseFunc (closing)

Description:

  • CloseFunc — executed after the model is closed.

Callbacks based on the closure condition of the model allow:

  • Free up resources (memory or file descriptors) that were used during the operation of the model;

  • Perform any final tasks that need to be done before the model is closed.;

  • Save the state of the model (or its data) before closing.

Messages in the diagnostic window

In the model’s callback code, you can send messages to model diagnostic window model diagnosis main using the functions:

  • engee.info(msg) — information message;

  • engee.warning(msg) — warning message;

These functions only work inside callbacks (masks and models). Collect text through interpolation ("Value = $(x)") or string(…​).
examples by callback groups

Opening the model (PreLoadFunc, PostLoadFunc):

PreLoadFunc() = begin
    engee.info("Opening the model")
end

PostLoadFunc() = begin
    engee.info("The model is loaded")
end

Model Execution (InitFunc, StartFunc, PauseFunc, ContinueFunc, StopFunc):

InitFunc() = engee.info("Initialization is completed")
StartFunc() = engee.info("The simulation is running")
PauseFunc() = engee.warning("The simulation is on pause")
ContinueFunc() = engee.info("I continue after a pause")
StopFunc() = engee.info("The simulation is stopped")

Saving the model (PreSave, PostSave):

PreSave() = engee.info("Saving the model…")
PostSave() = engee.info("The model is saved")

Closing the model (CloseFunc):

CloseFunc() = begin
    try
        # freeing up temporary resources
        engee.info("Resources are released, the model is closed")
    catch e
        engee.warning("Couldn't close the model correctly: $(e)")
    end
end
Also see the section about messages in block masks.: guide/masks-main.adoc#mask-callbacks-diagnostics.

Example of work

To see an example of how callbacks work, go to link.

You can assign a variable to Engee for each callback. This is convenient to track the triggering depending on the simulation condition, for example, inside the model opening call. PreLoadFunc you can use the code:

# model opening variables
PreL = 1 # performed before loading the model
PostL = 0 # performed after loading the model

# model execution variables
Init = 0 # it is executed after the model is compiled
Start = 0 # runs until the first step of the simulation
Pause = 0 # called when the simulation is paused
Continue = 0 # it is performed in one step when the model is started after a pause
Stop = 0 # it is performed during the simulation shutdown

# model retention variables
PreS = 0 # performed before saving the model
PostS = 0 # performed after saving the model

# model closure variables
Close = 0 # performed after the model is closed

This code initializes variables with null values. Variable PreL, associated with opening the model, is equal to one, since its parameters are loaded before and after loading the model (they are always executed). For example, in the model execution callbacks StartFunc you can set a variable Start = 1. If this callback is successful, the value of the variable will change from 0 (not completed) on 1 (completed). You can monitor the execution of callbacks using the variables window.:

callback variables 1 en

Each time the callback is triggered, the variables are updated, and then their values are output for analysis and debugging of the simulation process.