Engee documentation
Notebook

Callbacks

In this demo, we will show the possibilities of calling functions from the model according to the conditions executed inside the model.

The model itself is a logable sinusoid. The scheme of operation, in which it is possible to enable callbacks during the simulation process, is shown in the figure below.

This demo will consist of a set of tests that can be conditionally divided into 4 groups.:

  1. Functions based on the model opening condition,
  2. functions according to the conditions of the model execution,
  3. functions based on the condition of saving the model,
  4. Functions based on the closure condition of the model.
image_2.png

To open the callback window, open "Model Settings" -> "Edit source code".

To trigger the tests described below, you need to run model_callback.engee.

The first test is the triggering of callbacks when the model is opened.

image_3.png
image.png
In [ ]:
PreL = 0
PostL = 0
Init = 0
Start = 0
Pause = 0
Continue = 0
Stop = 0
PreS = 0
PostS = 0
Close = 0
Out[0]:
0

As we can see, two variables are initialized at the moment of opening.:

  1. before opening the model,
  2. After opening the model.
In [ ]:
print("PreLoad: " * string(PreL) * " / PostLoad: " * string(PostL) *
" / PreSave: " * string(PreS) * " / PostSave: " * string(PostS) *
" / Close: " * string(Close))
PreLoad: 1 / PostLoad: 1 / PreSave: 0 / PostSave: 0 / Close: 0

The second test is the actions when saving the model.
image.png

image_2.png
image_3.png

Similarly to the previous version, there are two functions with conditions before saving and after saving.

In [ ]:
print("PreLoad: " * string(PreL) * " / PostLoad: " * string(PostL) *
" / PreSave: " * string(PreS) * " / PostSave: " * string(PostS) *
" / Close: " * string(Close))
PreLoad: 1 / PostLoad: 1 / PreSave: 1 / PostSave: 1 / Close: 0

The third test is the closure of the model.

image_2.png

From it, we can see that this function is triggered when the model is closed.

In [ ]:
print("Close: " * string(Close))
Close: 1

The last group of callbacks are functions that occur based on actions during the simulation. There are 5 of them, as shown in the picture below.

image_2.png

Now let's look at the cases when each of these functions is called. As you can see at the stage before opening the model, we initialized all the variables, which we subsequently change depending on the condition.

  1. The initialization function is called after the compilation stage of the model.
  2. The start function is performed at the first step of the simulation.
  3. The pause function is called in case of a pause during simulation.
  4. A function performed at all simulation steps.
  5. The function of stopping the simulation process.

Accordingly, if we run the model and pause the process at least once during the simulation, then all of the above functions will be performed. The figure below shows that after running our tests in the workspace, all variables are equal to 1.

image.png

Conclusion

In this demo, we have shown how to use callbacks. This tool is designed to automate modeling processes, as well as to save and analyze simulation results. With the help of callbacks, we can completely get rid of the binding to scripts and implement our entire project exclusively in models, starting from the stage of declaring parameters and ending with the stage of analyzing the results.

Blocks used in example