This guide walks through building a Model in the Sim Engine: its lifecycle methods, how to organize states into controllers, how to configure rates and dependencies, and how to safely use streams, async tasks, and profiling in the hot path.

1. Understand What a Model Is

A Model is a logical unit that runs at a configured rate, may depend on other models, and contains state controllers that group related states (play/reset/pause). Models can run in parallel; declare dependencies explicitly so the scheduler can respect ordering. A model's primary responsibilities:

2. Implement the Lifecycle Methods

MethodWhen it's called / what to do
ConstructorCalled once when the program loads. Initialize one-time objects, configure CPU affinity, and set thread priority.
OnReset()Called FIRST at start-up, and on every Reset command. Set the model's minor frame frequency via SetMinorFrame(), fetch shared resources (LRUs), and reset internal counters/state.
OnInit()Called at start-up after OnReset() (and OnStop()), and on every Init command. Initialize state controllers, set initial state statuses, configure stream triggers and send/receive callbacks, and call global initializers like OGIInit() (only from one model).
OnStateChanged()Called when the engine state changes: before the frames start (eRun / eStep) and before Stop, Reset, Init and Terminate. One-time actions at the start of a run go here (eRun).
OnRun()Main per-frame execution callback (the hot path). Execute state controllers, send/enqueue outputs, start async tasks without waiting, and collect lightweight profiling.
OnStop()Called at start-up between OnReset() and OnInit(), on every Stop command and at terminate. Quiesce resources, stop background operations, or pause state controllers; keep what OnRun() needs (a Run after a Stop continues without Reset / Init).
OnFirstRun() / OnFreeze()Not called by the engine: the generated model has them as empty functions, CModelBase has no such callbacks. Use OnStateChanged(eRun) instead.

Start-up order: OnReset() → OnStop() → OnInit(), then OnStateChanged(eRun) and the frames. Details: Order of the calls.

Example constructor setup — one-time thread, affinity, and priority configuration:

const int numOfTasks = 10;
tasks = std::make_unique<CTasks>(numOfTasks);
hAsyncThread = tasks->AddThread("AsyncThreadName1",
    [](void* pObj, void* param1, void* param2, void* param3) {
        ((CMyModel*)pObj)->AsyncCmd(nullptr, nullptr, nullptr);
    },
this);

// Use Core 0 and Core 2
cores = { true, false, true };

// Highest thread priority
eThreadPriority = EThreadPriority::Highest;

3. Group States into State Controllers

Inside a model, states are grouped into logical State Controllers. Each controller manages a group of states and has its own play/pause/reset controls. Group states to get:

// Add controller and set status
CStatesController::AddController<CCommCheck>("CommChecks");
CStatesController::SetStateStatus("CommChecks", CACState::EStateStatus::ePlay);

// Execute controllers in OnRun
CStatesController::ExecuteState("CommChecks");

4. Configure Rates and Dependencies

Each model executes at a configured minor frame rate:

Multiple models may run in parallel. If model A depends on model B, declare that dependency (via configuration or the scheduler) so execution is ordered correctly.

Example: a sensor model may run at 1 kHz while a heavy physics model runs at 125 Hz. The scheduler should ensure data produced by the physics model is propagated deterministically (e.g., via timestamped buffers) to the higher-rate consumer.

5. Connect Streams, IO, and Recording

Models can publish or subscribe to streams. Use stream triggers to run a model only when specific data arrives. There are three callback hooks around the raw send/receive path:

// Execute only when a block arrives on StreamName/BlockName
SetStreamTrigger("StreamName", "LRUName.BlockName");

// Called just before the raw block is sent out
SetStreamBlockPreSendCB("StreamName", "LRUName.BlockName", &Cmodel2::OnPreSendCB);

// Called just after the raw block is received, before it is accepted
SetStreamBlockPostReceiveAcceptCB("StreamName", "LRUName.BlockName", &Cmodel2::OnPostReceiveAcceptCB);

// Register receive callback
SetStreamBlockReceiveCB("StreamName", "LRUName.BlockName", &Cmodel2::OnReceiveCB);

All three callbacks share the same signature, which now includes the block name:

void Cmodel2::OnReceiveCB(const char* blockName, const uint8_t* raw, uint64_t size);
void Cmodel2::OnPreSendCB(const char* blockName, const uint8_t* raw, uint64_t size);
bool Cmodel2::OnPostReceiveAcceptCB(const char* blockName, const uint8_t* raw, uint64_t size);

For recording, push small descriptors into a lockless SPSC ring in the hot path and let the recording thread serialize or write the large data off the critical path.

6. Add Async Commands and Profiling Safely

For long-running or blocking operations (file I/O, network, complex computation), use a thread pool or async task manager, and follow these rules:

Keep profiling lightweight in the hot path — use scope profilers or running averages, and avoid heavy logging:

// scope profiler
{
static CRunningAverage runningAverage;
CSimScopeProfiler simScopeProfiler("profilingTaskName", &runningAverage);
}

// manual timing
auto startT = std::chrono::high_resolution_clock::now();
// ... work ...
runningAverage.add(std::chrono::duration_cast(
std::chrono::high_resolution_clock::now() - startT).count());

7. Follow the Typical Model Flow

OnReset():                (start-up: first)
- Set minor frame using SetMinorFrame()
- Reset state controllers and counters

OnInit():                 (start-up: after OnReset and OnStop)
- Initialize states and controllers
- Set stream triggers and callbacks
- Pre-allocate buffers

OnStateChanged(eRun):     (before the first frame of a run)
- One-time start tasks (timers, warm-up)

OnRun():
- Execute state controllers (fast, deterministic)
- Enqueue record descriptors into ring buffer
- Optionally start async tasks (non-blocking)

OnStop():                 (also at start-up, before OnInit)
- Quiesce background threads, flush buffers (keep what OnRun needs)

8. Apply Integration Best Practices

Danger zones: blocking I/O in OnRun(), dynamic allocation inside the hot path, and uncontrolled async thread spawns. Tip: call OGIInit() from exactly one model if OGI is included, and let only one model enable global memory monitors.