## Model Sections

Model "sections" are lists of equations in a model source file which are evaluated at specific times and for specific purposes. For example, certain equations may only need to be evaluated once at the beginning of the simulation. Other equations may need to be evaluated at every point in simulated time (e.g., on every derivative evaluation). Other equations may only need to be evaluated when a particular event in the simulation occurs. These model sections are denoted by a keyword, followed by a list of statements, followed by the "end" keyword. In some cases, sections can be nested. Each of the section types supported by the CSL language is described below.

### Model

The "model" section encompasses all equations and sub-sections associated with a particular model. Every model must contain at least one model section. The "model" keyword should be followed by a unique (alphanumeric plus underscore, no spaces) name identifying the model. This name can be used to build models by composing smaller models (experimental feature). Each model statement must have an associated "end" statement; all model equations and subsections are contained within the model section.

A CSL file may contain multiple model sections, but the last one will be treated as the model to be executed when the simulation runs. It may reference sub-models defined previously in the file.

```
model example_model
! ... sub-sections, statements and equations ...
end
```

### Initial

The "initial" section contains equations or statements which should be evaluated once at the beginning of the simulation. For example: equations which perform unit conversions of model parameters might be placed in the initial section, if those quantities do not change again over the course of the simulation. Note that a model may contain multiple initial sections, and that these sections may even be embedded in other types of sections. As part of the compilation process, the Magnolia translator will extract and combine all initial sections.

```
model example_model
initial
! The INITIAL section contains statements which are evaluated
! once at the beginning of the simulation run
end ! initial
! Other model sections and statements
end ! model
```

### Terminal

The counterpart to the initial section is the terminal section. It contains statements and equations should should be executed once, at the conclusion of the simulation run. It might be used, for example, to perform scaling of output quantities. Like the initial section, a model may contain multiple terminal sections, which will be combined by the Magnolia translator as part of the compilation process.

```
model example_model
! Other model sections and statements
terminal
! The TERMINAL section contains statements which are evaluated
! once at the conclusion of the simulation run
end
end ! model
```

### Dynamic

The "dynamic" section contains any statements or equations which should be evaluated at each communication interval. A communication interval is the period in simulated time between which model outputs are logged. This interval is defined via the CINTERVAL statement, described in detail in the Language Reference. For example, if model outputs are desired at 15 minute intervals for a simulation with time in units of hours, then outputs would be computed at an interval of 0.25 hours, meaning statements in the dynamic section would be at this interval. The dynamics section is typically used to perform scaling of time-dependent simulation outputs.

```
model example_model
! Other model sections and statements
dynamic
! The DYNAMIC section contains statements which are evaluated
! at each output time point
end ! dynamic
end ! model
```

### Derivative

The derivative section contains any statements or sections which must be evaluated at every point in simulated time. As its name implies, it is typically used to computed derivatives, or any quantities which are inputs to derivative equations. A model may have multiple derivative sections. These sections will be collected, combined and sorted by the Magnolia translator prior to the simulation run. Typically, the derivative section is contained in the dynamic section, but this is more of good coding practice than a strict requirement of the language.

```
model example_model
! Other model sections and statements
dynamic
derivative
! The DERIVATIVE section contains statements which are
! used to compute derivatives
! Example: exponential decay
constant k = 1.0 ! Decay rate constant
constant xic = 10.0 ! Initial condition
xd = -k*x ! Derivative
x = integ(xd, xic) ! Integrate derivative to compute state
constant tstop = 10.0
termt(t >= tstop, 'Stopped on time limit')
end ! derivative
end ! dynamic
end ! model
```

### Discrete

Discrete sections contain equations or statements which should be evaluated at specific times or when particular events take place. In other words, discrete events happen instantaneously. Example of discrete events could be a bolus dosing event in a pharmacokinetic model, or contact between two rigid bodies in a mechanical model.

Because different events may occur due to different causes, and each event may trigger different the evaluation of different equations, a CSL model may have multiple discrete sections. Each discrete section must have a corresponding, unique name to identify it. Discrete section names mst follow the same naming conventions as CSL variables. These sections are invoked by a SCHEDULE or INTERVAL statement by using the name of the corresponding section. The INTERVAL and SCHEDULE statements are described in detail in the Language Reference.

Like the derivative section, discrete sections are typically contained in the dynamic section.

```
model example_model
! Other model sections and statements
dynamic
discrete EVENT1
! DISCRETE sections contain statements which are only
! evaluated at specific time points. A model
! can contain an arbitrary number of DISCRETE sections.
! Equations and statements associated with EVENT1
end ! EVENT1
discrete EVENT2
! Equations and statements associated with EVENT2
end ! EVENT2
end ! dynamic
end ! model
```