This chapter defines synchronous behavior suited for implementation of control systems. The synchronous behavior relies on an additional kind of discrete-time variables and equations, as well as an additional kind of when-clause. The benefits of synchronous behavior is that it allows a model to define large sampled data systems in a safe way, so that the translator can provide good a diagnostic in case of a modeling error.
The following small example shows the most important elements:
A periodic clock is defined with Clock(3). The argument of Clock defines the sampling interval (for details see section 16.3).
Clocked variables (such as yd, xd, ud) are associated uniquely with a clock and can only be directly accessed when the associated clock is active. Since all variables in a clocked equation must belong to the same clock, clocking errors can be detected at compile time. If variables from different clocks shall be used in an equation, explicit cast operators must be used, such as sample to convert from continuous-time to clocked discrete-time or hold to convert from clocked discrete-time to continuous-time.
A continuous-time variable is sampled at a clock tick with sample. The operator returns the value of the continuous-time variable when the clock is active.
When no argument is defined for Clock, the clock is deduced by clock inference.
For a when-clause with an associated clock, all equations inside the when-clause are clocked with the given clock. All equations on an associated clock are treated together and in the same way regardless of whether they are inside a when-clause or not. This means that automatic sampling and hold of variables inside the when-clause does not apply (explicit sampling and hold is required) and that general equations can be used in such when-clauses (this is not allowed for when-clauses with Boolean conditions, that require a variable reference on the left-hand side of an equation).
The when-clause in the controller could also be removed and the controller could just be defined by the equations:
previous(xd) returns the value of xd at the previous clock tick. At the first sample instant, the start value of xd is returned.
A discrete-time signal (such as ud) is converted to a continuous-time signal with hold.
If a variable belongs to a particular clock, then all other equations where this variable is used, with the exception of as argument to certain special operators, belong also to this clock, as well as all variables that are used in these equations. This property is used for clock inference and allows defining an associated clock only at a few places (above only in the sampler, whereas in the discrete controller and the hold the sampling period is inferred).
The approach in this chapter is based on the clock calculus and inference system proposed by Colaço and Pouzet (2003) and implemented in Lucid Synchrone version 2 and 3 (Pouzet, 2006). However, the Modelica approach also uses multi-rate periodic clocks based on rational arithmetic introduced by Forget et al. (2008), as an extension of the Lucid Synchrone semantics. These approaches belong to the class of synchronous languages (Benveniste et al., 2003).
[Periodically sampled control systems could also be defined with standard when-clauses, see section 8.3.5, and the sample operator, see section 3.7.5. For example:
Equations in a when-clause with a Boolean condition have the property that (a) variables on the left hand side of the equal sign are assigned a value when the when-condition becomes true and otherwise hold their value, (b) variables not assigned in the when-clause are directly accessed (= automatic sample semantics), and (c) the variables assigned in the when-clause can be directly accessed outside of the when-clause (= automatic hold semantics).
Using standard when-clauses works well for individual simple sampled blocks, but the synchronous approach using clocks and clocked equations provide the following benefits (especially for large sampled systems):
Possibility to detect inconsistent sampling rate, since clock partitioning (see section 16.7), replaces the automatic sample and hold semantics. Examples:
If when-clauses in different blocks should belong to the same controller part, but by accident different when-conditions are given, then this is accepted (no error is detected).
If a sampled data library such as the Modelica_LinearSystems2.Contoller library is used, at every block the sampling of the block has to be defined as integer multiple of a base sampling rate. If several blocks should belong to the same controller part, and different integer multiples are given, then the translator has to accept this (no error is detected).
Note: Clocked systems can mix different sampling rates in well-defined ways when needed.
Fewer initial conditions are needed, as only a subset of clocked variables need initial conditions – the clocked state variables (see section 16.4). For a standard when-clause all variables assigned in a when-clause must have an initial value because they might be used, before they are assigned a value the first time. As a result, all these variables are “discrete-time states” although in reality only a subset of them need an initial value.
More general equations can be used, compared to standard when-clauses that require a restricted form of equations where the left hand side has to be a variable, in order to identify the variables that are assigned in the when-clause. This restriction can be circumvented for standard when-clauses, but is absent for clocked equations and make it more convenient to define nonlinear control algorithms.
Clocked equations allow clock inference, meaning that the sampling need only be given once for a sub-system. For a standard when-clause the condition (sampling) must be explicitly propagated to all blocks, which is tedious and error prone for large systems.
Possible to use general continuous-time models in synchronous models (e.g., some advanced controllers use an inverse model of a plant in the feedforward path of the controller, see Thümmel et al. (2005)). This powerful feature of Modelica to use a nonlinear plant model in a controller would require to export the continuous-time model with an embedded integration method and then import it in an environment where the rest of the controller is defined. With clocked equations, clocked controllers with continuous-time models can be directly defined in Modelica.
Clocked equations are straightforward to optimize because they are evaluated exactly once at each event instant. In contrast a standard when-clause with sample conceptually requires several evaluations of the model (in some cases tools can optimize this to avoid unneeded evaluations). The problem for the standard when-clause is that after v is changed, pre(v) shall be updated and the model re-evaluated, since the equations could depend on pre(v). For clocked equations this iteration can be omitted since previous(v) can only occur in the clocked equations that are only run the first event iterations.
Clocked subsystems using arithmetic blocks are straightforward to optimize. When a standard math-block (e.g., addition) is part of a clocked sub-system it is automatically clocked and only evaluated when the clocked equations trigger. For standard when-clauses one either needs a separate sampled math-block for each operation, or it will conceptually be evaluated all the time. However, tools may perform a similar optimization for standard when-clauses and it is only relevant in large sampled systems.
]
In this section various terms are defined.
In section 3.8.5 the term discrete-time Modelica expression and in section 3.8.6 the term continuous-time Modelica expression is defined. In this chapter, two additional kinds of discrete-time expressions/variables are defined that are associated to clocks and are therefore called clocked discrete-time expressions. The different kinds of discrete-time variables in Modelica are defined below.
(See section 3.8.5.) Variables of base type Real, Integer, Boolean, enumeration, and String that are constant inside each interval (i.e., piecewise constant continuous-time variables). In other words, changes value only at events: , for . Such variables depend continuously on time and they are discrete-time variables. See figure 16.2. ∎
Clock variables are of base type Clock. A clock is either defined by a constructor (such as Clock(3)) that defines when the clock ticks (is active) at a particular time instant, or it is defined with clock operators relatively to other clocks, see section 16.5.1. See figure 16.3. ∎
The elements of clocked variables are of base type Real, Integer, Boolean, enumeration, String that are associated uniquely with a clock . A clocked variable can only be directly accessed at the event instant where the associated clock is active. A constant and a parameter can always be used at a place where a clocked variable is required.
[Note that clock variables are not included in this list. This implies that clock variables cannot be used where clocked variables are required.]
At time instants where the associated clock is not active, the value of a clocked variable can be inquired by using an explicit cast operator, see below. In such a case hold semantics is used, in other words the value of the clocked variable from the last event instant is used. See figure 16.4. ∎
There are two kinds of clock partitions:
A base-partition identifies a set of equations and a set of variables which must be executed together in one task. Different base-partitions can be associated to separate tasks for asynchronous execution. ∎
A sub-partition identifies a subset of equations and a subset of variables of a base-partition which are partially synchronized with other sub-partitions of the same base-partition, i.e., synchronized when the ticks of the respective clocks are simultaneous. ∎
The terminology for the partitions is as follows:
Clocked base-partitions.
Discrete-time sub-partitions.
Discretized sub-partitions.
Unclocked base-partition.
[Note that the term clock partition refers to these partitions in general, whereas clocked base-partition is a specific kind of partition. Previously the discrete-time sub-partitions were called clocked discrete-time (sub-clock partition). Further, discretized sub-partitions were called discretized continuous-time (sub-clock partition). When emphasizing that the partitions are clock partitions, sub-partitions can still be referred to as sub-clock partitions; and similarly for base-partition.]
The built-in operators (with function syntax) defined in the following sections have partially restrictions on their input arguments that are not present for Modelica functions. To define the restrictions, the following term is used.
A component expression is a component-reference which is a valid expression, i.e., not referring to models or blocks with equations. In detail, it is an instance of a (a) base type, (b) derived type, (c) record, (d) an array of such an instance (a-c), (e) one or more elements of such an array (d) defined by index expressions which are evaluable (see below), or (f) an element of records.
[The essential features are that one or several values are associated with the instance, that start values can be defined on these values, and that no equations are associated with the instance. A component expression can be constant or can vary with time.]
∎
In the following sections, when defining an operator with function calling syntax, there are some common restrictions being used for the input arguments (operands). For example, an input argument to the operator may be required to be a component expression (definition 16.6) or evaluable expression (section 3.8). To emphasize that there are no such restrictions, an input argument may be said to be just an expression.
[The reason for restricting an input argument to be a component expression is that the start value of the input argument is returned before the first tick of the clock of the input argument and this is not possible for a general expression.
The reason for restricting an input argument to be an evaluable expression is to ensure that clock analysis can be performed during translation. In cases when special handling of parameter expressions is specified, it is an indication that the values are not needed during translation.]
[Example: The input argument to previous is restricted to be a component expression.
]
[Example: The named argument factor of subSample is restricted to be an evaluable expression.
]
None of the operators defined in this chapter vectorize, but some can operate directly on array variables (including clocked array variables, but not clock array variables). They are not callable in functions.
The overloaded constructors listed below are available to generate clocks, and it is possible to call them with the specified named arguments, or with positional arguments (according to the order shown in the details after the table).
Expression | Description | Details |
---|---|---|
Clock() | Inferred clock | Operator 16.1 |
Clock(intervalCounter, resolution) | Rational interval clock | Operator 16.2 |
Clock(interval) | Real interval clock | Operator 16.3 |
Clock(condition, startInterval) | Event clock | Operator 16.4 |
Clock(c, solverMethod) | Solver clock | Operator 16.5 |
Inferred clock. The operator returns a clock that is inferred.
[Example:
Note, in most cases, the operator is not needed and equations could be written without a when-clause (but not in the example above, since the print statement is otherwise not associated to a clock). This style is useful if a modeler would clearly like to mark the equations that must belong to one clock (although a tool could figure this out as well, if the when-clause is not present).]
Rational interval clock. The first input argument, , is a clocked component expression (definition 16.6) or an evaluable expression of type Integer with min = 0. The optional second argument (defaults to 1) is an evaluable expression of type Integer with min = 1 and unit = "Hz". If is an evaluable expression with value zero, the period of the clock is derived by clock inference, see section 16.7.5.
If is an evaluable expression greater than zero, the clock defines a periodic clock. If is a clocked component expression it must be greater than zero. The result is of base type Clock that ticks when time becomes , , , … The clock starts at the start of the simulation or when the controller is switched on. At the start of the simulation, previous() = .start and the clocks ticks the first time. At the first clock tick must be computed and the second clock tick is then triggered at . At the second clock tick at time , a new value for must be computed and the next clock tick is scheduled at , and so on.
[The given interval and time shift can be modified by using the subSample, superSample, shiftSample and backSample operators on the returned clock, see section 16.5.2.]
[Example:
]
Note that operator interval(c) of Clock c = Clock(nextInterval, ) returns:previous() / (in seconds)
Real interval clock. The input argument, , is a clocked component expression (definition 16.6) or a parameter expression. The must be strictly positive () of type Real with unit = "s". The result is of base type Clock that ticks when time becomes , , , … The clock starts at the start of the simulation or when the controller is switched on. Here the next clock tick is scheduled at = previous() = .start. At the second clock tick at time , the next clock tick is scheduled at = previous(), and so on. If is a parameter expression, the clock defines a periodic clock.
[Note, the clock is defined with previous(). Therefore, for sorting the input argument is treated as known. The given interval and time shift can be modified by using the subSample, superSample, shiftSample and backSample operators on the returned clock, see section 16.5.2. There are restrictions where this operator can be used, see Clock expressions below. Note that does not have to an evaluable expression, since different real interval clocks are never compared.]
Event clock. The first input argument, , is a continuous-time expression of type Boolean. The optional argument (defaults to 0) is the value returned by interval() at the first tick of the clock, see section 16.9. The result is of base type Clock that ticks when edge(pre()) becomes true.
[This clock is used to trigger a clocked base-partition due to a state event (that is a zero-crossing of a Real variable) in an unclocked base-partition, or due to a hardware interrupt that is modeled as Boolean in the simulation model. The additional pre delays the event clock one event iteration, but does not introduce any time-delay.]
[Example: Demonstrating the subtle effect of the additional pre:
The first tick of c (the clock of b2) is at 0.5 where b2 is true. This is because both the event clock and sample introduce a delay of one event iteration, keeping them synchronized.]
[The implicitly given interval and time shift can be modified by using the subSample, superSample, shiftSample and backSample operators on the returned clock, see section 16.5.2, provided the base interval is not smaller than the implicitly given interval.]
Solver clock. The first input argument, , is a clock and the operator returns this clock. The returned clock is associated with the second input argument of type String. The meaning of is defined in section 16.8.2. If is the empty String, then this Clock construct does not associate an integrator with the returned clock.
[Example:
]
Besides inferred clocks and solver clocks, one of the following mutually exclusive associations of clocks are possible in one base-partition:
One or more periodic rational interval clocks, provided they are consistent with each other, see section 16.7.5.
[Example: Assume y = subSample(u), and Clock(1, 10) is associated with u and Clock(2, 10) is associated with y, then this is correct, but it would be an error if y is associated with a Clock(1, 3).]
Exactly one non-periodic rational interval clock.
Exactly one real interval clock.
[Example: Assume Clock c = Clock(2.5), then variables in the same base-partition can be associated multiple times with c but not multiple times with Clock(2.5).]
Exactly one event clock.
A default clock, if neither a real interval, nor a rational interval nor an event clock is associated with a base-partition. In this case the default clock is associated with the fastest sub-partition.
[Typically, a tool will use Clock(1.0) as a default clock and will raise a warning, that it selected a default clock.]
Clock variables can be used in a restricted form of expressions. Generally, every expression switching between clock variables must be an evaluable expression (in order that clock analysis can be performed when translating a model). Thus subscripts on clock variables and conditions of if-then-else switching between clock variables must be evaluable expressions, and there are similar restrictions for sub-clock conversion operators section 16.5.2. Otherwise, the following expressions are allowed:
Declaring arrays of clocks.
[Example: Clock c1[3] = {Clock(1), Clock(2), Clock(3)}]
Array constructors of clocks: {}, [], cat.
Array access of clocks.
[Example: sample(u, c1[2])]
Equality of clocks.
[Example: c1 = c2]
if-expressions of clocks in equations.
[Example:
]
Clock variables can be declared in models, blocks, connectors, and records. A clock variable can be declared with the prefixes input, output, inner, outer, but not with the prefixes flow, stream, discrete, parameter, or constant.
A component expression which is not a parameter, and to which previous has been applied. ∎
The previous value of a clocked variable can be accessed with the previous operator, listed below.
Expression | Description | Details |
---|---|---|
previous() | Previous value of clocked variable | Operator 16.6 |
The input argument is a component expression (definition 16.6). If is a parameter, its value is returned.
Otherwise: Input and return arguments are on the same clock. At the first tick of the clock of or after a reset transition (see section 17.3.2), the start value of is returned, see section 16.9. At subsequent activations of the clock of , the value of from the previous clock activation is returned.
[At a clock tick only the (previous) values of the clocked state variables are needed to compute the new values of all clocked variables on that clock. This roughly corresponds to state variables in continuous time.]
A set of clock conversion operators together act as boundaries between different clock partitions.
The operators listed below convert between a clocked and an unclocked representation and vice versa.
Expression | Description | Details |
---|---|---|
sample(, ) | Sample unclocked expression | Operator 16.7 |
hold() | Zeroth order hold of clocked-time variable | Operator 16.8 |
Input argument is in an unclocked base-partition, and there are no variability restrictions, i.e., it is continuous-time according to section 3.8.6. The optional input argument is of type Clock, and can in a call be given as a named argument (with the name ), or as positional argument. The operator returns a clocked variable that has as associated clock and has the value of the left limit of when is active (that is the value of just before the event of is triggered). If is not provided, it is inferred, see section 16.7.5.
[Since the operator returns the left limit of , it introduces an infinitesimal small delay between the unclocked and the clocked partition. This corresponds to the reality, where a sampled data system cannot act infinitely fast and even for a very idealized simulation, an infinitesimal small delay is present. The consequences for the sorting are discussed below.
Input argument can be a general expression, because the argument is unclocked and therefore has always a value. It can also be a constant, a parameter or a piecewise constant expression.
Note that sample is an overloaded function: If sample has two positional input arguments and the second argument is of type Real, it is the operator from section 3.7.5. If sample has one input argument, or it has two input arguments and the second argument is of type Clock, it is the base-clock conversion operator from this section.]
Input argument is a clocked (definition 16.3) component expression (definition 16.6) or a parameter expression. The operator returns a piecewise constant signal of the same type as . When the clock of ticks, the operator returns and otherwise returns the value of from the last clock activation. Before the first clock activation of , the operator returns the start value of , see section 16.9.
[Since the input argument is not defined before the first tick of the clock of , the restriction is present, that it must be a component expression (or a parameter expression), in order that the initial value of can be used in such a case.]
[Example: Assume there is the following model:
The value of yc at the first clock tick is (and not ). The reason is that the continuous-time model der(y) + y = 2 is first initialized and after initialization y has the value 2. At the first clock tick at , the left limit of y is 2 and therefore .]
[Since sample(u) returns the left limit of u, and the left limit of u is a known value, all inputs to a base-partition are treated as known during sorting. Since a periodic and interval clock can tick at most once at a time instant, and since the left limit of a variable does not change during event iteration (i.e., re-evaluating a base-partition associated with a condition clock always gives the same result because the sample(u) inputs do not change and therefore need not to be re-evaluated), all base-partitions, see section 16.7.3, need not to be sorted with respect to each other. Instead, at an event instant, active base-partitions can be evaluated first (and once) in any order. Afterwards, the unclocked base-partition is evaluated.
Event iteration takes place only over the unclocked base-partition. In such a scenario, accessing the left limit of u in sample(u) just means to pick the latest available value of u when the base-partition is entered, storing it in a local variable of the base-partition and only using this local copy during evaluation of the equations in this base-partition.]
The operators listed below convert between synchronous clocks.
Expression | Description | Details |
---|---|---|
subSample(, factor) | Clock that is slower by a factor | Operator 16.9 |
superSample(, factor) | Clock that is faster by a factor | Operator 16.10 |
shiftSample(, shiftCounter, resolution) | Clock with time-shifted ticks | Operator 16.11 |
backSample(, backCounter, resolution) | Inverse of shiftSample | Operator 16.12 |
noClock() | Clock that is always inferred | Operator 16.13 |
These operators have the following properties:
The input argument is a clocked expression or an expression of type Clock. (The operators can operate on all types of clocks.) If is a clocked expression, the operator returns a clocked variable that has the same type as the expression. If is an expression of type Clock, the operator returns a Clock – except for noClock where it is an error.
The optional input arguments factor (defaults to 0, with min = 0), and resolution (defaults to 1, with min = 1) are evaluable expressions of type Integer.
Calls of the operators can use named arguments for the multi-letter arguments (i.e., not for ) with the given names, or positional arguments.
[Named arguments can make the calls easier to understand.]
The input arguments shiftCounter and backCounter are evaluable expressions of type Integer with min = 0.
The clock of y = subSample(, ) is times slower than the clock of . At every ticks of the clock of , the operator returns the value of . The first activation of the clock of y coincides with the first activation of the clock of , and then every activation of the clock of y coincides with the every th activativation of the clock of . If is not provided or is equal to zero, it is inferred, see section 16.7.5.
The clock of y = superSample(, ) is times faster than the clock of . At every tick of the clock of y, the operator returns the value of from the last tick of the clock of . The first activation of the clock of y coincides with the first activation of the clock of , and then the interval between activations of the clock of is split equidistantly into activations, such that the activation of y coincides with the activation of .
[Thus subSample(superSample(, ), ) = .]
If is not provided or is equal to zero, it is inferred, see section 16.7.5. If an event clock is associated to a base-partition, all its sub-partitions must have resulting clocks that are sub-sampled with an Integer factor with respect to this base-clock.
[Example:
]
The operator c = shiftSample(, , ) splits the interval between ticks of into equidistant intervals . The clock c then ticks intervals after each tick of .
It leads to
[Note, due to the restriction of superSample on event clocks, shiftSample can only shift the number of ticks of the event clock, but cannot introduce new ticks. Example:
Additional example showing the full form:
Here u ticks at 0, 2, 3, 4, 5, 6. First you superSample to split each sampling interval in two equal parts leading to the ticks 0.0, 1.0, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0. Then the simple shiftSample removes the first three ticks giving 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0. And finally every other point is removed by subSample, and s1 ticks at 2.5, 3.5, 4.5, 5.5.]
The input argument is either a component expression (definition 16.6) or an expression of type Clock. This is an inverse of shiftSample such that Clock y = backSample(, , ) implicitly defines a clock y such that shiftSample(y, , ) activates at the same times as . It is an error if the clock of y starts before the base-clock of .
At every tick of the clock of y, the operator returns the value of from the last tick of the clock of . If is a clocked component expression, the operator returns the start value of , see section 16.9, before the first tick of the clock of .
[Example:
]
The clock of y = noClock() is always inferred, and must be part of the same base-clock as y. At every tick of the clock of y, the operator returns the value of from the last tick of the clock of . If noClock() is called before the first tick of the clock of , the start value of is returned.
[Clarification of backSample:
Let and be positive integers with , and
Then when ys exists, also yb exists and ys = yb.
The variable yb exists for the above parameterization with a < b one clock tick before ys. Therefore, backSample is basically a shiftSample with a different parameterization and the clock of backSample.y ticks before the clock of u. Before the clock of u ticks, yb = u.start.]
[Clarification of noClock operator:
Note, that noClock(u) is not equivalent to sample(hold(u)). Consider the following model:
Due to the infinitesimal delay of sample, z will not show the current value of x as clk2 ticks, but will show its previous value (left limit). However, y will show the current value, since it has no infinitesimal delay.]
Note that it is not legal to compute the derivative of the sample, subSample, superSample, backSample, shiftSample, and noClock operators.
In addition to the previously discussed when-equation (see section 8.3.5), a clocked when-clause is introduced:
The clocked when-clause cannot be nested and does not have any elsewhen part. It cannot be used inside an algorithm. General equations are allowed in a clocked when-clause.
For a clocked when-clause, all equations inside the when-clause are clocked with the same clock given by the .
This section defines how clock-partitions and clocks associated with equations are inferred.
[Typically clock partitioning is performed before sorting the equations. The benefit is that clocking and symbolic transformation errors are separated.]
Every clocked variable is uniquely associated with exactly one clock.
After model flattening, every equation in an equation section, every expression and every algorithm section is either unclocked, or it is uniquely associated with exactly one clock. In the latter case it is called a clocked equation, a clocked expression or clocked algorithm section respectively. The associated clock is either explicitly defined by a when-clause, see section 16.5.2, or it is implicitly defined by the requirement that a clocked equation, a clocked expression and a clocked algorithm section must have the same clock as the variables used in them with exception of the expressions used as first arguments in the conversion operators of section 16.5. Clock inference means to infer the clock of a variable, an equation, an expression or an algorithm section if the clock is not explicitly defined and is deduced from the required properties in the previous two paragraphs.
All variables in an expression without clock conversion operators must have the same clock to infer the clocks for each variable and expression. The clock inference works both forward and backwards regarding the data flow and is also being able to handle algebraic loops. The clock inference method uses the set of variable incidences of the equations, i.e., what variables that appear in each equation.
Note that incidences of the first argument of clock conversion operators of section 16.5 are handled specially.
[As clock partitions are solely determined by the equations, two different clock partitions can have clocks defined by the same expressions. It is a quality of implementation issue that such partitions are executed synchronously, e.g., by putting them in the same task in a real-time simulation context.]
The clock partitioning is conceptually performed after model flattening, i.e., redeclarations have been elaborated, arrays of model components expanded into scalar model components, and overloading resolved. Furthermore, function calls to inline functions have been inlined.
[This is called conceptually, because a tool might do this more efficiently in a different way, provided the result is the same as if everything is flattened. For example, array and matrix equations and records don’t not need to be expanded if they have the same clock.]
Furthermore, each non-trivial expression (non-literal, non-constant, non-parameter, non-variable), , appearing as first argument of a clock conversion operator (except hold and backSample) is recursively replaced by a unique variable, , and the equation is added to the equation set.
Consider the set of equations and the set of unknown variables (not constants and parameters) in a flattened model, i.e., . The partitioning is described in terms of an undirected graph with the nodes being the set of equations and variables, . The set for an equation in is a subset of , in general, the unknowns which lexically appear in . There is an edge in of the graph between an equation, , and a variable, , if :
A set of clock partitions is the connected components (Wikipedia, Connected components) of this graph with appropriate definition of the incidence operator.
A special case is the built-in variable time (see section 3.6.7). Each use of time is conceptually included as a separate variable in this analysis, with .
[This means that time can be used in different partitions without any restrictions. Additionally, it means that every sub-partition directly referencing time contains a call to der.]
The goal is to identify all clocked equations and variables that should be executed together in the same task, as well as to identify the unclocked base-partition.
The base-partitioning is performed with base-clock inference which uses the following incidence definition: =
the unknown variables, as well as variables x in der(x), pre(x), and previous(x), which lexically appear in
except as first argument of base-clock conversion operators: sample and hold and Clock(condition=, startInterval=).
The resulting set of connected components, is the partitioning of the equations and variables, , according to base-clocks and unclocked partitions.
The base partitions are identified as clocked or as unclocked partitions according to the following properties:
A variable u in sample(u), a variable y in y = hold(ud), and a variable b in Clock(b, startInterval=) where the Boolean b is in an unclocked partition.
Correspondingly, variables u and y in y = sample(uc), y = subSample(u), y = superSample(u), y = shiftSample(u), y = backSample(u), y = previous(u), are in a clocked base-partition. Equations in a clocked when-clause are also in a clocked base-partition. Other base-partitions, where none of the variables in the partition are associated with any of the operators above, have an unspecified partition kind and are considered to be unclocked base-partitions.
All unclocked base-partitions are collected together and form the unclocked base-partition.
[Example:
After base-partitioning, the following partitions are identified:
]
For each clocked base-partition Bi, identified in section 16.7.3, the sub-partitioning is performed with sub-clock inference which uses the following incidence definition:
=
the unknown variables, as well as variables x in der(x), pre(x), and previous(x), which lexically appear in
except as first argument of sub-clock conversion operators: subSample, superSample, shiftSample, backSample, noClock, and Clock with first argument of Boolean type.
The resulting set of connected components, is the partitioning of the equations and variables, , according to sub-clocks.
The connected components (corresponding to the sub-clocks) are then further split into strongly connected components corresponding to systems of equations. The resulting sets of equations and variables shall be possible to solve separately, meaning that systems of equations cannot involve different sub-clocks.
It can be noted that:
[Example: After sub-partitioning of the example from section 16.7.3, the following partitions are identified:
]
[Example: Forbidding systems of equations involving different sub-clocks means that the following is forbidden:
Here a and z are part of one sub-clock, and y of another, and the system of equations involve both of them.
The following legal example solves the issues in the previous example by replacing a by x-z (and simplifying the equations). Additionally, it shows that it is not required that the sub-clocks can necessarily be sorted:
Here x and z are part of one sub-partition, and y of another. The equations form three equation systems with one equation in each (hence trivially satisfying the requirement that only variables from one sub-partition are being solved). The equation systems need to be solved in a strict order, but the first and last equation system belong to one sub-clock, while the second equation system belongs to another sub-clock. This illustrates that there is no guarantee that the sub-partitions can be ordered in agreement with the equation systems. Note that equation systems with more than one equation are also allowed in sub-partitions.]
For each base-partition, the base interval needs to be determined and for each sub-partition, the sub-sampling factors and shift need to be determined. The sub-partition intervals are constrained by subSample and superSample factors which might be known (or evaluable expression) or unspecified, as well as by shiftSample, shiftCounter and resolution, or backSample, backCounter and resolution. This constraint set is used to solve for all intervals and sub-sampling factors and shift of the sub-partitions. The model is erroneous if no solution exist.
[It must be possible to determine that the constraint set is valid at compile time. However, in certain cases, it could be possible to defer providing actual numbers until run-time.]
It is required that accumulated sub- and supersampling factors in the range of 1 to 263 can be handled.
[64 bit internal representation of numerator and denominator with sign can be used and gives minimum resolution seconds and maximum range seconds = years.]
[The goal is that every continuous-time Modelica model can be utilized in a sampled data control system. This is achieved by solving the continuous-time equations with a defined integration method between clock ticks. With this feature, it is for example possible to invert the nonlinear dynamic model of a plant, see Thümmel et al. (2005), and use it in a feedforward path of an advanced control system that is associated with a clock.
This feature also allows defining multi-rate systems: Different parts of the continuous-time model are associated to different clocks and are solved with different integration methods between clock ticks, e.g., a very fast sub-system with an implicit solver with a small step-size and a slow sub-system with an explicit solver with a large step-size.]
With the language elements defined in this section, continuous-time equations can be used in clocked partitions. Hereby, the continuous-time equations are solved with the defined integration method between clock ticks.
Such a sub-partition is called a discretized sub-partition, and the clock ticks are not interpreted as events, but as step-sizes of the integrator that the integrator must hit exactly. Hence, no event handling is triggered at clock ticks (provided an explicit event is not triggered from the model at this time instant).
[The interpretation of the clock ticks is the same assumption as for manually discretized controllers, such as the z-transform.]
[It is not defined how to handle events that are triggered while solving a discretized sub-partition. For example, a tool could handle events in the same way as for a usual simulation – but only check them at the time associated with clock-ticks.
Alternatively, relations might be interpreted literally, so that events are no longer triggered (in order that the time for an integration step is always the same, as needed for hard real-time requirements). However, even if relations do not generate events, when-clauses and operators edge and change should behave as normal.]
From the viewpoint of other partitions, the discretized continuous-time variables only have values at clock ticks (internally it may be more complicated, see section 16.8.2). Therefore, outside the discretized sub-partitions themselves, they are treated similarly to discrete-time sub-partitions. Especially, operators such as sample, hold, subSample must be used to communicate signals of the discretized sub-partition with other partitions.
Additionally to the variability of expressions defined in section 3.8, an orthogonal concept clocked variability is defined in this section. If not explicitly stated otherwise, an expression with a variability such as continuous-time or discrete-time means that the expression is inside a partition that is unclocked. If an expression is present in a base-partition that is not an unclocked base-partition, it is a clocked expression and has clocked variability.
After sub-clock inferencing, see section 16.7.5, every sub-partition that is associated with a clock has to be categorized as discrete-time or discretized.
[Previously, discrete-time sub-partition was refered to as clocked discrete-time partition, and discretized sub-partition as clocked discretized continuous-time partition.]
If a clocked sub-partition contains any of the operators der, delay, spatialDistribution, or event related operators from section 3.7.5 (with exception of noEvent and smooth), or contains a when-clause with a Boolean condition, it is a discretized sub-partition. Otherwise, it is a discrete-time sub-partition.
[That is, a discrete-time sub-partition is a standard sampled data system described by difference equations.]
A discretized sub-partition has to be solved with a solver method of section 16.8.2. When previous(x) is used on a continuous-time state variable x, then previous(x) uses the start value of x as value for the first clock tick.
The use of the operator sample from section 3.7.5 in a discretized sub-partition is problematic. A diagnostic is recommended, especially if the operator is intended to generate events faster than the clock ticks, and otherwise the sampling should ideally be adjusted to the clock ticks.
[The reason for not disallowing sample in a discretized sub-partition is to make it possible to include any continuous-time Modelica model in a sampled data control system. Note that even if the sampling is slower than the clock ticks (or even the same rate) it still introduces the problem of possibly uneven sampling.]
In a discrete-time sub-partition none of the event generating mechanisms apply. Especially neither relations, nor any of the built-in operators of section 3.7.2 (event triggering mathematical functions) will trigger events.
A sub-partition can have an integration method, directly associated (section 16.8.3) or inferred from other sub-partitions (section 16.8.4). A predefined type ModelicaServices.Types.SolverMethod defines the methods supported by the respective tool by using the choices annotation.
[The ModelicaServices package contains tool specific definitions. A string is used instead of an enumeration, since different tools might have different values and then the integer mapping of an enumeration is misleading since the same value might characterize different integrators.]
The following names of solver methods are standardized:
If a tool supports one of the integrators of SolverMethod, it must use the solver method name of above.
[A tool may also support other integrators. Typically, a tool supports at least methods "External" and "ExplicitEuler". If a tool does not support the integration method defined in a model, typically a warning message is printed and the method is changed to "External".]
If the solver method is "External", then the sub-partition associated with this method is integrated by the simulation environment for an interval of length of interval() using a solution method defined in the simulation environment.
[An example of such a solution method could be to have a table of the clocks that are associated with discretized sub-partitions and a method selection per clock. In such a case, the solution method might be a variable step solver with step-size control that integrates between two clock ticks. The simulation environment might also combine all partitions associated with method "External", as well as all unclocked partitions, and integrate them together with the solver selected by the simulation environment.]
If the solver method is not "External", then the sub-partition is integrated using the given method with the step-size interval().
[For a periodic clock, the integration is thus performed with fixed step size.]
The solvers are defined with respect to the underlying ordinary differential equation in state space form that corresponds to the sub-partition before it has been discretized, at least conceptually:
where:
is time
is the continuous-time Real vector of input variables
is the discrete-time Real/Integer/Boolean/String vector of input variables
is the continuous-time real vector of states
is the continuous-time or discrete-time Real/Integer/Boolean/String vector of algebraic and/or output variables
A solver method is applied to a discretized sub-partition. Such a partition has explicit inputs marked by sample(), subSample(), superSample(), shiftSample() and/or backSample(). Furthermore, the outputs of such a partition are marked by hold(), subSample(), superSample(), shiftSample(), and/or backSample(). The arguments of these operators are to be used as input signals and output signals in the conceptual ordinary differential equation above, and in the discretization formulae below, respectively.
The solver methods (with exception of "External") are defined by integrating from clock tick to clock tick and computing the desired variables at , with and (for all methods: ):
SolverMethod | Solution method |
---|---|
"ExplicitEuler" | |
"ExplicitMidPoint2" | |
"ExplicitRungeKutta4" | |
"ImplicitEuler" | Equation system with unknowns: , |
"ImplicitTrapezoid" | Equation system with unknowns: , |
The initial conditions will be used at the first tick of the clock, and the first integration step will go from the first to the second tick of the clock.
[Example: Assume the differential equation
shall be transformed to a discretized sub-partition with the "ExplicitEuler" method. The following model is a manual implementation:
]
[For the implicit integration methods the efficiency can be enhanced by utilizing the discretization formula during the symbolic transformation of the equations. For example, linear differential equations are then mapped to linear and not non-linear algebraic equation systems, and also the structure of the equations can be utilized. For details see Elmqvist et al. (1995). It might be necessary to associate additional data for an implicit integration method, e.g., the relative tolerance to solve the non-linear algebraic equation systems, or the maximum number of iterations in case of hard realtime requirements. This data is tool specific and is typically either defined with a vendor annotation or is given in the simulation environment.]
A SolverMethod can be associated to a clock with the overloaded Clock constructor Clock(, solverMethod=), see section 16.3. If a clock is associated with a sub-clock of a discretized sub-partition and a SolverMethod is associated with this clock, then the sub-partition is integrated with it.
[Example:
]
If a solverMethod is not explicitly associated with a sub-partition, it is inferred with a similar mechanism as for sub-clock inferencing, see section 16.7.5.
First, one set is constructed for each sub-partition, containing just this sub-partition. These sets are then merged as follows: For each set without a specified solverMethod, the set is merged with sets connected to it (these may contain a solverMethod), and this is repeated until it is not possible to merge more sets. The sets connected in this way should be part of the same base-partition and connected through a sub-clock conversion operator (subSample, superSample, shiftSample, backSample, or noClock).
It is an error if this set contains multiple different values for solverMethod.
If the set contains continuous-time equations:
It is an error if this set contains no solverMethod.
Otherwise, the specified solverMethod is used.
If the set does not contain continuous-time equations, there is no need for a solverMethod. However, inferencing between sub-partitions works the same regardless of whether there are continuous-time equations.
[Example:
Note that there is only one base-partition, but it has two different periodic rational clocks - consistent with section 16.7.5.
Here z is a continuous-time equation connected directly to both x and y sub-partitions that have different solverMethod.]
The standard scheme for initialization of Modelica models does not apply for discrete-time sub-partitions. Instead, initialization is performed in the following way:
Variables in discrete-time sub-partitions cannot be used in initial equation or initial algorithm sections.
Attribute fixed cannot be applied to variables in discrete-time sub-partitions. The attribute fixed is true for clocked states, otherwise false.
A few additional utility operators are listed below.
Expression | Description | Details |
---|---|---|
firstTick() | Test for first clock tick | Operator 16.14 |
interval() | Interval between previous and present tick | Operator 16.15 |
It is an error if these operators are called in the unclocked base-partition.
This operator returns true at the first tick of the clock of the expression, in which this operator is called. The operator returns false at all subsequent ticks of the clock. The optional argument is only used for clock inference, see section 16.7.
This operator returns the interval between the previous and present tick of the clock of the expression, in which this operator is called. The optional argument is only used for clock inference, see section 16.7. At the first tick of the clock the following is returned:
If the specified clock interval is a parameter expression, this value is returned.
Otherwise the start value of the variable specifying the interval is returned.
For an event clock the additional startInterval argument to the event clock constructor is returned.
The return value of interval is a scalar Real number.
[Example: A discrete PI controller is parameterized with the parameters of a continuous PI controller, in order that the discrete block is robust against changes in the sample period. This is achieved by discretizing a continuous PI controller (here with an implicit Euler method):
A continuous-time model is inverted, discretized and used as feedforward controller for a PI controller (der, previous, interval are used in the same partition):
]
The execution of sub-partitions requires exact time management for proper synchronization. The implication is that testing a Real-valued time variable to determine sampling instants is not possible. One possible method is to use counters to handle sub-sampling scheduling,
and to test the counter to determine when the sub-clock is ticking:
The Clock___activated flag is used as the guard for the sub partition equations.
[Consider the following example:
A possible implementation model is shown below using Modelica 3.2 semantics. The base-clock is determined to 0.001 seconds and the sub-sampling factors to 1000 and 60000.
]