Some integration methods do more calculations before generating the next output value. These calculations are called minor steps, the output generation is called a major step.
integration method |
minor steps |
|
0 |
variable |
|
0 |
|
1 |
|
3 |
|
variable |
|
variable |
|
variable |
|
variable |
|
variable |
During a minor step, all model equations are executed. In most cases you will not notice this because only the results of the major step are shown in the simulator. There is one exception. If you use equations like
variables
real y;
initialequations
y = 0;
equations
y = y + 1;
you will find that the value of y depends on the integration method that is used. The reason is obvious once your realize that the equations are executed during minor steps. If you use a Runge Kutta 2 integration method, there is one minor step during which y is increased and one major step during which y is increased again!
To prevent an equation from being calculated during minor steps, you can us the predefined variable major. This variable will return the boolean false when calculations are performed for a minor step and will return true when calculations are performed during a major step. The major variable is used for example in the library model Amplitude Sensor:
parameters
real initial = 0.0;
variables
real prev, peak;
initialequations
peak = initial;
prev = 0;
output = initial;
code
if major then
peak = max([abs(input), peak]);
if (input > 0 and prev < 0) or (input < 0 and prev > 0) then
output = peak;
peak = 0;
end;
prev = input;
end;
Note that the initial values are set in the initialequations section, because we do no want the variables to be set to zero at every integration step. Instead of an equations section, a code section is used to prevent 20-sim from rewriting the equations in a different order.