Leeetal Cyphysim Emsoft 2015
Leeetal Cyphysim Emsoft 2015
Leeetal Cyphysim Emsoft 2015
using CyPhySim
instead of an integral as
d
x(t) = x(t),
dt
where x is the input signal and x is the output. (The rela-
tionship between these representations is the fundamental
theorem of calculus.)
Note that x(t) does not depend on the value of x at time
t. This is a key causality property of the actor, and it
ensures that the feedback loops of Figure 1 are construc-
tive [11]. At each time t, there is no cyclic dependency in
a feedback loop. The values of the signals at time t can be
determined without knowing the inputs to the integrators at
time t. Once these are known, the inputs to the integrators
at time t become known. But these inputs will only affect
Figure 1: Graphical model of the Lorenz attractor. future outputs of the integrators. This is an example of a
computational property of these models.
Computers perform discrete calculations. How can they
integrate a continuous-time signal? Numeric integration
discrete semantics. Let us begin with a careful examination
techniques include implicit methods (e.g. backward Eu-
of the most primitive element in such models, the integrator.
ler, which computes x(tk+1 ) = x(tk ) + x(tk+1 ) (tk+1 tk ))
and explicit methods (e.g. forward Euler, which com-
2.1 Integrators putes x(tk+1 ) = x(tk ) + x(tk ) (tk+1 tk )). Implicit methods
The Integrator actors in Figure 1 have a derivative input sacrifice the causality property, requiring that at time t, the
and a state output, as shown in Figure 3. At time t, the input x(t) be known in order to determine the output x(t).
state output is Implicit methods, therefore, change the causality properties
of fundamental calculus, and create causality loops when
Z t
x(t) = x0 + x( )d, used in feedback systems. Causality loops can also arise
t0 when models are given by DAEs rather than ODEs. We
will say more about this below, but for now, we assume that
where x0 is the initialState, a parameter of the actor, t0 is only explicit solvers will be used.
the start time of the model execution, and x is the deriva- An ordinary differential equation (ODE) can be writ-
tive input. Note that this can be written using a derivative ten as
x(t) = f (x(t), u(t), t), (1)
Strange Attractor where x is the state variable (or vector), u is the input,
25 t is time, and f is a function giving the derivative in terms
20
of the state, the input, and the time. If x and u are scalars,
f has the form f : R3 R. Given an initial condition
15
x0 = x(t0 ), this is equivalent to
10 Z t Z t
5 x(t) = x0 + x( )d = f (x( ), u( ), )d.
t0 t0
x2
0
The general structure of such a system is shown in Figure 4.
-5
-10
2.2 Classical ODE Solvers
-15
Under certain circumstances, the value of a continuous
-20
signal at a future time t + h can be given by a Taylor se-
ries, which expresses that value in terms of the value and
-15 -10 -5 0 5 10 15
x1 derivatives at the current time t,
h2
Figure 2: Plot of signals for the Lorenz attractor. x(t + h) = x(t) + hx(t) + x(t) +
2!
solution provides a semantic benchmark against which any
numerical solver can be evaluated for accuracy. This seman-
tic benchmark is called the ideal solver semantics in [15].
This is the third example of computational property. Just as
it is useful to have a theory of real numbers against which to
evaluate the accuracy of floating-point numbers, it is useful
to have an ideal solver semantics against which to evaluate
the accuracy of numerical solvers.
position
0
-5
0 2 4 6 8 10 12 14
time
forces. 2
position
0
-2
-4
-6
-8
12.820 12.825 12.830 12.835 12.840
time
3 3 3
smoothToken(2, 2, {-1})
2 2 2
smoothToken(1, 0, {0})
1 1 1
2
0 t 0 t 0 t
1 3 4 1 2 3 4 1 2 3 4
1 1 1
smoothToken(1, 2, {0}) smoothToken(0, 0, {1}) smoothToken(0, 0, {0, 1})
Figure 10: Illustration of smooth tokens representing piecewise-constant, -affine, and -quadratic signals.
signal has changed by more than the specified quantum, as Hence, the next quantization happens at = t0 +
p
defined by the selected solver, explained in detail below. 2 q/|u(t0 )|, at which time an output will be produced, un-
Three types of solvers are provided: less the function u changes earlier.
For QSS3, a quantization event occurs when a piecewise
1. QSS1: Input u is assumed to be piecewise constant.
quadratic approximation of the state trajectory deviates
2. QSS2: Input u is assumed to be piecewise affine. from a piecewise cubic approximation by the quantum.
When an output is produced, its value will be the current
3. QSS3: Input u is assumed to be piecewise quadratic. state of the integrator. In addition, it may contain derivative
An input event can be a smooth token, which carries not information. For QSS1, the input is semantically piecewise
only a value, but also zero or more derivatives of the input constant, so the output is piecewise affine; hence each out-
signal at that time. To provide a piecewise affine input to a put event will be a smooth token that is piecewise affine,
QSS2 integrator, for example, you can specify an input with with a first derivative equal to the most recently received
the expression smoothToken(2, 0, {1}), which specifies a input value (see Section 6.4 below for an elaboration of this
value of 2 at time 0 with a first derivative of 1. All other behavior). For QSS2, the output will have a first and second
derivatives are assumed to be zero. A QSS1 integrator will derivative obtained from the input. For QSS3, the output
ignore all derivatives on the input. A QSS2 integrator will will have first, second, and third derivatives.
ignore all but the first derivative on the input. A QSS3
integrator will ignore all but the first and second derivatives 6.2 Quantums
on the input. The frequency with which the output q of this integrator
The integrator produces an output whenever a quantiza- is produced depends on the solver choice and the absolute-
tion event occurs. For QSS1, a quantization event occurs Quantum and relativeQuantum parameter values. These
when the state of the integrator changes by the quantum determine when a quantization event occurs, as explained
(see below for an explanation of the quantum). For exam- above. The quantum is equal to the larger of absolute-
ple, if the input is a constant 1 and the quantum is 0.1, then Quantum and the product of relativeQuantum and the cur-
an output will be produced every 0.1 seconds, because the rent state value. The simplest case is where the solver is
input specifies that the state has slope 1, so it will increase QSS1 and relativeQuantum is zero. In this case, a quantiza-
by the quantum every 0.1 seconds. tion event occurs whenever the integral of the input signal
For QSS2, a quantization event occurs when a piecewise changes by the absoluteQuantum. For QSS1, the input is
linear approximation of the state trajectory deviates from assumed to be piecewise constant. If the input is a smooth
a piecewise quadratic approximation by the quantum. For token, the derivatives of the input are ignored.
example, suppose we have t = t0 , input u(t), current state When an impulse input is received, the value of that event
x(t0 ) and quantum q > 0, for some q R. Then, the piece- is added to the current state of this integrator (any deriva-
wise linear approximation to the state trajectory is tives provided on the impulse input are ignored). Then an
output event is produced and the integrator is reinitialized
l(t) = x(t0 ) + x(t0 ) (t t0 ) (6) so that the next output quantum is relative to the new state
on t [t0 , ] for some, yet to be determined , whereas the value.
piecewise quadratic approximation is
6.3 Performance and Accuracy
x(t) = x(t0 ) + x(t0 ) (t t0 ) + x(t0 ) (t t0 )2 /2. (7) In our implementation in CyPhySim, the Lorenz attrac-
We seek tor of Figure 1 executes approximately 2.5 times faster using
QSS3 integrators vs. an RK 2-3 solver. However, since this
arg min{t > t0 | q = |l(t) x(t)|} system is chaotic, it is not really meaningful to compare
= arg min{t > t0 | q = |u(t0 ) (t t0 )2 /2|}. (8) accuracy. Chaotic systems often have the property that ar-
points in time, as illustrated in the upper plot in Figure
12. At each of these points, a smooth token is produced.
The zero crossings are predictable from these points, so no
rollback is needed to identify the points in time where the
collisions occur. The lower plot is created by sampling the
position signal with a sample interval of 0.01. The samples
between computed points of the upper plot are extrapolated
automatically from the smooth tokens. In the RK 2-3 model,
computation is performed at 14,072 time points, not count-
ing rejected time points due to rollback that is caused by the
zero-crossing detector. On a Mac Powerbook Pro, the RK
2-3 simulation completes in 3.3 seconds, whereas the QSS
simulation completes in 0.085 seconds, on average, so the
QSS simulation is approximately 38 times faster.
As discussed above in Section 2.2, most classical ODE
solvers require rollback to adjust step sizes based on error
Figure 11: QSS version of a bouncing ball model. estimates and to iterate to the time of events such as for
zero crossings. QSS solvers have the interesting property
Actual Points Computed that if the QSS1, 2, and 3 assumptions about the integrator
10 inputs, given above in Section 6.1, are indeed valid, then
rollback is never required. If these assumptions are valid,
5 then there is no error due to numerical approximation of
position
0
derivatives does, in fact, introduce errors in the numerical
integration. Management of these errors appears to be a rel-
-5
atively unstudied problem, at least to the knowledge of these
authors, except for asymptotically stable linear time invari-
0 2 4 6 8 10 12 14
time ant systems for which the global error can be computed [6].
Figure 12: Position vs. time for the QSS bouncing 6.4 Derivative Propagation
ball. Mathematically, integration is an operation that always
occurs over a measurable interval. Semantically, this means
that integration always introduces an (infinitesimal) delay
bitrarily small perturbations can have arbitrarily big conse- between its input and output. In CyPhySim, this means
quences, a phenomenon known as the butterfly effect. that the output of an integrator can be only based on pre-
Consider again the bouncing ball model of Figure 6. This vious inputs up to, but not including, the current times-
problem is ideally suited to QSS because the force on the ball tamp. Semantically, QSS integrator is a composition of an
is piecewise constant, and accuracy comparisons are more ordinary integrator with a hysteretic quantizer, the latter of
meaningful. The velocity of the ball is therefore piecewise which suppresses changes in its output until it has deviated
affine, and the position is piecewise quadratic. A QSS model from its old value by one quantum. However, since a QSS
in CyPhySim is shown in Figure 11, and a plot of its execu- integrator outputs derivative information inside its smooth
tion is shown in Figure 12. The upper plot shows the actual tokens, and the derivative is precisely the input itself, one
points in time computed by the simulation. may ask whether the output should propagate changes in
The model in Figure 6 uses an RK 2-3 solver with an error the input instantaneously at the following microstep, rather
tolerance of 104 . Figure 11 uses a QSS2 solver for the left than suppress them until a quantum is reached at a future
integrator (which outputs the velocity) and a QSS3 solver time.
for the right integrator (which outputs the position).2 Both While propagating derivative information instantaneously
simulations are run for 14 seconds, taking the model just alters QSS semantics slightly, it can only increase the accu-
past the point of tunneling. racy of the model, since downstream components are noti-
In the QSS model, computation is performed only at 46 fied earlier of the changes than they otherwise would be.
2
This highlights another advantage QSS, which is that the Of course, it would also come at a computational cost, as
choice of QSS order can be optimized for each state vari- the model is now evaluated much more frequently than the
able independently, although in this case, there is very little quantum size would otherwise suggest. Preliminary results,
observable difference if QSS3 is used throughout. however, seem to suggest that it is possible for the accu-
Figure 13: RLC circuit requiring an algebraic loop
solver (after Figure 12.8 in [6]). Figure 14: Model of the RLC circuit of Figure 13.
racy improvement to compensate for the performance loss Mockup Interface (FMI) is a standard for model exchange
by causing the model to correct its errors sooner rather than and co-simulation of dynamical models [19]. CyPhySim sup-
later, and hence do so less often. Therefore, to allow for the ports importing FMUs designed for model exchange or co-
potential benefits of such behavior, a QSS integrator in Cy- simulation. For FMU for model-exchange, it provides a QSS
PhySim has a propagateInputDerivatives parameter that simulation engine for numerical integration. This strategy
dictates whether changes in the input should be propagated also enables co-simulation of DE models with FMI, a com-
to the output at the current model time. bination also described in [24].
Many CPS applications include sampled-data subsystems
7. ALGEBRAIC LOOP SOLVERS with regular, periodic sample rates. CyPhySim leverages the
CyPhySim includes a mechanism for specifying alge- synchronous-reactive (SR) domain of Ptolemy II to provide
braic loop solvers, including a simple successive substitu- such models, which permits specification of a sample rate
tion mechanism, a Newton-Raphson solver, and a homotopy and enables structured multi rate systems. Such SR mod-
method. The model builder is given explicit control over the els interoperate well with DE, QSS, and Continuous models
solution method and initial guesses in order to ensure deter- [14]. CyPhySim incorporates a new innovation that enables
ministic results. arbitrary hierarchical nesting of these models. Sampled data
Figure 13 shows the electrical circuit with an algebraic systems can contain continuous or DE subsystems and vice
loop, which is the problem shown in Figure 12.8 in [6]. For versa.
this system, an algebraic loop needs to be solved whenever Nevertheless, much work remains to be done. The ap-
a QSS integrator changes its output, or when VoltageSource proach used to handle algebraic loops in Section 7 is seman-
triggers an event. Figure 14 shows the CyPhySim imple- tically sound, but the graphical syntax of such models is
mentation. The actor labeled AlgebraicLoop implements not satisfactory. The requirement that algebraic loops be
the algebraic loop that needs to be solved. It has an input factored out into a different level of hierarchy in the model
for the voltage source, and two pairs of inputs and outputs leads to good separation of concerns, but it makes the mod-
that connect the algebraic loop to the capacitor and the in- els considerably less readable. A better approach would be
ductor, each having a state variable. to graphically or textually describe the structure of the mod-
els in the most natural way, including where appropriate
using acausal ports as in Modelica, and then to automat-
8. FURTHER WORK ically transform the model for analysis and execution into
Some of the capabilities of CyPhySim are beyond the a form that separates concerns and implements the correct
scope of this paper. For example, CyPhySim includes semantics.
the ability to import functional mockup units (FMUs), The operations on derivatives that are described in Section
which are components designed in some foreign modeling 5.1 are particularly convenient to implement in CyPhySim,
language or tool (such as Modelica), and exported with because in the underlying Ptolemy II system, arithmetic op-
an interface defined by the FMI standard. The Functional erations on tokens exchanged between actors are polymor-
phic, which means that the token itself defines what addi- design of embedded systems. In EMSOFT (Salzburg,
tion, multiplication, division, etc. mean. An interesting pos- Austria, 2007), ACM, pp. 114 123.
sibility, which is not so trivial to implement in the software, [15] Liu, J., and Lee, E. A. On the causality of
would extend the manipulation of derivatives to transcen- mixed-signal and hybrid models. In 6th International
dental functions and other common operations on signals. In Workshop on Hybrid Systems: Computation and
effect, this would endow a numerical solver with a measure Control (HSCC 03) (Prague, Czech Republic, 2003),
of symbolic computation capability, creating an interesting O. Maler and A. Pnueli, Eds., vol. LNCS 2623,
blend of symbolic and numerical simulation. Springer-Verlag, pp. 328342.
[16] Maler, O., Manna, Z., and Pnueli, A. From timed
9. REFERENCES to hybrid systems. In Real-Time: Theory and Practice,
[1] Bliudze, S., and Furic, S. An operational semantics REX Workshop (1992), Springer-Verlag, pp. 447484.
for hybrid systems involving behavioral abstraction. In [17] Matsikoudis, E., and Lee, E. A. On fixed points of
Modelica Conference (2014). QSS. Quantizing signals strictly causal functions. In International Conference
rather than time. on Formal Modeling and Analysis of Timed Systems
[2] Broman, D., Brooks, C., Greenberg, L., Lee, (FORMATS) (Buenos Aires, Argentina, 2013),
E. A., Masin, M., Tripakis, S., and Wetter, M. vol. LNCS 8053, Springer-Verlag, pp. 183197.
Determinate composition of fmus for co-simulation. In [18] Migoni, G., Bortolotto, M., Kofman, E., and
International Conference on Embedded Software Cellier, F. E. Linearly implicit quantization-based
(EMSOFT) (2013), IEEE. integration methods for stiff ordinary differential
[3] Broman, D., Greenberg, L., Lee, E. A., Masin, equations. Simulation Modelling Practice and Theory
M., Tripakis, S., and Wetter, M. Requirements 35 (2013), 118136.
for hybrid cosimulation standards. In Hybrid Systems: [19] Modelica Association. Functional mock-up
Computation and Control (HSCC) (2015). interface for model exchange and co-simulation.
[4] Cardoso, J., Lee, E. A., Liu, J., and Zheng, H. Report Version 2.0, July 25 2014.
Continuous-time models. In System Design, Modeling, [20] Nagel, L. W., and Pederson, D. O. SPICE
and Simulation using Ptolemy II, C. Ptolemaeus, Ed. (simulation program with integrated circuit emphasis).
Ptolemy.org, Berkeley, CA, 2014. Technical memorandum, Electronics Research
[5] Cassandras, C. G. Discrete Event Systems, Laboratory, University of California, Berkeley, April
Modeling and Performance Analysis. Irwin, 1993. 1973.
[6] Cellier, F. E., and Kofman, E. Continuous System [21] Pantelides, C. C. The consistent initialization of
Simulation. Springer, 2006. differential-algebraic systems. SIAM Journal of
[7] Fishman, G. S. Discrete-Event Simulation: Modeling, Scientific and Statistical Computing 9, 2 (1988),
Programming, and Analysis. Springer-Verlag, 2001. 213231.
[8] Floros, X., Cellier, F. E., and Kofman, E. [22] Paynter, H. M. Analysis and Design of Engineering
Discretizing time or states? a comparative study Systems. The M.I.T. Press, Cambridge, MA, 1961.
between dassl and qss - work in progress paper -. In [23] Ptolemaeus, C., Ed. System Design, Modeling, and
Workshop on Equation-Based Object-Oriented Simulation using Ptolemy II. Ptolemy.org, Berkeley,
Modeling Languages and Tools (EOOLT) (2010), CA, 2014.
Linkoping University. [24] Savicks, V., Butler, M., and Colley, J.
[9] Kofman, E., and Junco, S. Quantized-state Co-simulating Event-B and continuous models via
systems: A DEVS approach for continuous system FMI. In Summer Simulation Multi-Conference
simulation. Transactions of The Society for Modeling (SummerSim) (2014), pp. 259256.
and Simulation International 18, 1 (2001), 28. [25] Tiller, M. M. Introduction to Physical Modeling with
[10] Lee, E. A. Modeling concurrent real-time processes Modelica. Kluwer Academic Publishers, 2001.
using discrete events. Annals of Software Engineering [26] Zeigler, B. P., and Lee, J. S. Theory of quantized
7 (1999), 2545. systems: Formal basis for DEVS/HLA distributed
[11] Lee, E. A. Constructive models of discrete and simulation environment. In SPIE Conference on
continuous physical phenomena. IEEE Access 2, 1 Enabling Technology for Simulation Science (1998),
(2014), 125. vol. SPIE Vol. 3369, pp. 4958.
[12] Lee, E. A., and Tripakis, S. Modal models in [27] Zeigler, B. P., Praehofer, H., and Kim, T. G.
Ptolemy. In 3rd International Workshop on Theory of Modeling and Simulation, 2nd ed. Academic
Equation-Based Object-Oriented Modeling Languages Press, 2000.
and Tools (EOOLT) (Oslo, Norway, 2010), vol. 47,
Linkoping University Electronic Press, Linkoping
University, pp. 1121.
[13] Lee, E. A., and Zheng, H. Operational semantics of
hybrid systems. In Hybrid Systems: Computation and
Control (HSCC) (Zurich, Switzerland, 2005),
M. Morari and L. Thiele, Eds., vol. LNCS 3414,
Springer-Verlag, pp. 2553.
[14] Lee, E. A., and Zheng, H. Leveraging synchronous
language principles for heterogeneous modeling and