Events
Event-based programming is a style of computer programming where parts of the code respond to events being triggered by other sections of the code (see Fundamentals). In simulation models, events are generally linked to a simulation clock that is responsible for moving through the simulation period required and repeatedly triggering events at set times and in a predefined order. However, events can also be triggered when something happens (e.g. CropHarvestedEvent, PaddockBurnedEvent or RuminantsWeanedEvent) and may not necessarily be performed in a simulation, thus not triggering the associated code and actions. This allows for a modular style of programming where code is only used as needed.
The APSIM Next Generation modelling environment is responsible for providing a simulation clock that controls the timing of all processes in all the APSIM models. Initially, CLEM operated only on a monthly time-step while APSIM is based on a daily time-step, but a range of CLEM components are now able to run at a range of intervals down to the same daily time-step. While CLEM is able to respond to any event triggered by the APSIM environment or other APSIM models, it triggers a series of its own events to control the simulation. These events are either triggered at the start (setup) of the simulation or at the end of every month for all the monthly processes.

A number of CLEM events are provided for setting up resources and activities prior to the simulation. They are performed in the following order.

This event allows Resources the chance to be set up including initialising the stores to the value provided before the simulation commences.

This event allows Activities the chance to be set up and happens after the resources have been defined so decisions can be made based upon available resources.

This event is used to start performing the validation of all models below the CLEM component.
There are times when decisions need to be made after Resources and Activities are defined, but still before the simulation starts. This event provides the opportunity to perform these actions after validation has been performed and no errors detected.

This event allows Activities the last chance to be set up after validation is performed and all errors reported and trapped.

The following CLEM specific events are performed in the order provided on the last day of the month.

This event provides the opportunity for stores to be set or reset each time-step.

This event provides the opportunity to reset labour availability for the upcoming time-step.

This event triggers all activities responsible for managing pasture growth to add the new growth so that it is available to determine the fate of following decisions.

This event tells activities that all pasture has been updated and the amount present is what can be used in this time-step.

This is when all cut and carry from native pasture and crops is undertaken.

Perform all breeding. This will include the calculation of pregnancy and lactation demands before determining the potential intake of animals.

This event provides the opportunity mothers' milk production to be calculated before consumption by sucklings and milking.

Calculate the potential intake of animals so future feeding decisions can be made.

This is the main event that activities respond to and most will perform their actions here in the order they are provided in the simulation tree.
- Most activities with specific requirements will use this event

Calculate animal weight gain or loss after all feeding and grazing activities have been performed.

Calculate the manure produced based on animal actual intake determined during the Animal weight gain event.

Provide the opportunity to collect all manure previously produced.

Determine animal death from mortality rates or falling below critical weight.

Perform any milking of the herd after sucklings have taken required milk.

The opportunity for events to modify calculations of ecological state. This is useful for tracking pasture condition.

This event allows activities to mark animals prior to the main herd management.

This event allows activities to mark animals for sale and determine what needs to be purchased to maintain the herd.

This event allows activities to stock animals and adjust any lists of required animals or those marked for sale.
- Perform predictive seasonal stocking happens in this step for these reasons.

This event triggers all animal sales based on trucking rules if supplied.

This event triggers pasture detachment and decay toward the end of the time-step.

Perform calculations of the herd state

This event triggers all code required to age resources. This relates to the age of labour and ruminants as well as decay of pasture and manure pools.

This event triggers all animal purchases based on trucking rules if supplied.

Provides the opportunity perform actions before the end of time-step

Provides the opportunity to clean up at the end of each time-step
See next section - Features