Exit Conditions

Normally a simulation is executed for a specified number of steps. However, some models may reach and end state early (e.g. if the population dies out).

In order to manage these conditional exit’s FLAME GPU 2 provides exit conditions, via the FLAMEGPU_EXIT_CONDITION macro. These are implemented the same as host functions, that were introduced in the earlier chapter, with the exception they must return either CONTINUE or EXIT.

In particular, submodels which are introduced in the following section, require an exit condition as they cannot have their number of steps specified in the normal manner.

FLAMEGPU_EXIT_CONDITION(my_exit_condition) {
    // A simple exit condition which forces the model to exit after 100 steps
    if (FLAMEGPU->getStepCounter() >= 100) {
        return flamegpu::EXIT;      // End the simulation here
    } else {
        return flamegpu::CONTINUE;  // Continue the simulation
    }
}

// Fully define the model
ModelDescription model("example model");
...

// Add 'my_exit_condition' to 'model'
model.addExitCondition(my_exit_condition);

If a model has multiple exit conditions, they will be executed in the order that they were added to the model. When multiple exit conditions are defined, conditions are only executed if earlier exit condition functions return CONTINUE.