Debugging Equation-Based Modelica Models Martin Sj¨ olund

advertisement
Debugging Equation-Based Modelica Models
Martin Sjölund
Programming Environments Laboratory (PELAB)
Department of Computer and Information Science
Linköping University
2015-03-27
Algorithmic Debugging (Imperative Programming)
Algorithmic Debugging
Algorithmic Debugging
I
Traditional statement-based debugging
I
Based on GDB with domain-specific extensions
I
Ability to break on conditions including the time variable in
Modelica
I
Debug algorithmic Modelica code
I
Debug MetaModelica code, in order to debug omc itself
Algorithmic Debugging
Adding Breakpoints
Future Work
I
Make the GDB-based debugger usable (pause, etc).
I
Support for Modelica arrays and records is missing. Currently
supports primitive Modelica types: Integer, Real, Boolean and
String.
I
Function breakpoints.
I
Support C code debugging.
Demo
Demo
Debugging Equations (Declarative Programming)
Debugging equation-based Modelica models
Background: A simple RC Circuit
r
R=1e6
c
pv
g
C=1e-6
+
model RC
import M o d e l i c a . E l e c t r i c a l . A n a l o g . B a s i c ;
import M o d e l i c a . E l e c t r i c a l . A n a l o g . S o u r c e s ;
Basic.Ground ground1 ;
Basi c.Resistor resistor1 ( R = 1 e6 ) ;
B as ic .C a pacitor capacitor1 ( C = 1 e-6 ) ;
S o u r c e s . S i n e V o l ta g e sinevoltage1 ( V = 240,
freqHz = 50) ;
equation
connect ( c a p a c i t o r 1 . n , g r o u n d 1 . p ) ;
connect ( s i n e v o l t a g e 1 . n , g r o u n d 1 . p ) ;
connect ( r e s i s t o r 1 . n , s i n e v o l t a g e 1 . p ) ;
connect ( r e s i s t o r 1 . p , c a p a c i t o r 1 . p ) ;
end RC ;
RC Circuit Flattened Code
class RC // 24 e q u a t i o n s and v a r i a b l e s
// ...
equation
ground1.p.v = 0.0;
assert (1.0 + resistor1.alpha * ( r e s i s t o r 1 . T _ h e a t P o r t - resistor1.T_ref ) >=
1 e-15, " Temperature outside scope of model ! " ) ;
r e s i s t or 1 .R _a c tu a l = resistor1.R * (1.0 + resistor1.alpha * (
r e s i s t o r 1 . T _ h e a t P o r t - resistor1.T_ref ) ) ;
resistor1.v = re s is t or 1. R _a ct u al * resistor1.i ;
r e s i s t o r 1 . L o s s Po w e r = resistor1.v * resistor1.i ;
resistor1.v = resistor1.p.v - resistor1.n.v ;
0.0 = resistor1.p.i + resistor1.n.i ;
resistor1.i = resistor1.p.i ;
r e s i s t o r 1 . T _ h e a t P o r t = resistor1.T ;
capacitor1.i = capacitor1.C * der ( capacitor1.v ) ;
capacitor1.v = capacitor1.p.v - capacitor1.n.v ;
0.0 = capacitor1.p.i + capacitor1.n.i ;
capacitor1.i = capacitor1.p.i ;
s i n e v o l t a g e 1 . s i g n a l S o u r c e . y = s i n e v o l t a g e 1 . s i g n a l S o u r c e . o f f s e t + ( if time <
s i n e v o l t a g e 1 . s i g n a l S o u r c e . s t a r t T i m e then 0.0 else
s i n e v o l t a g e 1 . s i g n a l S o u r c e . a m p l i t u d e * sin (6 . 28 31 8 53 0 71 79 5 86 *
s i n e v o l t a g e 1 . s i g n a l S o u r c e . f r e q H z * ( time sinevoltage1.signalSource.startTime ) + sinevoltage1.signalSource.phase
));
sine voltage1.v = s i n e v o l t a g e 1 . s i g n a l S o u r c e . y ;
sine voltage1.v = sinevoltage1.p.v - sinevoltage1.n.v ;
0.0 = sinevoltage1.p.i + sinevoltage1.n.i ;
// ...
end RC ;
Modelica Backend = Magic
Backend magic transforms the model to a causalised ODE
class RC // 5 e q u a t i o n s and v a r i a b l e s
// 14 alias v a r i a b l e s 5 c o n s t a n t s
equation
// 1
sinevoltage1.signalSource.y =
s i n e v o l t a g e 1 . s i g n a l S o u r c e . o f f s e t + ( if time <
s i n e v o l t a g e 1 . s i g n a l S o u r c e . s t a r t T i m e then 0.0 else
s i n e v o l t a g e 1 . s i g n a l S o u r c e . a m p l i t u d e * sin
(6.28318530717959 * (
s i n e v o l t a g e 1 . s i g n a l S o u r c e . f r e q H z * ( time sinevoltage1.signalSource.startTime )) +
sinevoltage1.signalSource.phase ));
// 2
resistor1.v = capacitor1.v - s i n e v o l t a g e 1 . s i g n a l S o u r c e . y ;
// 3
capacitor1.i = - resistor1.v / r e s i s t o r 1 . R _ a c t u a l ;
// 4
r e s i s t o r 1 . L o s s P o w e r = - resistor1.v * capacitor1.i ;
// 5
der ( capacitor1.v ) = capacitor1.i / capacitor1.C ;
end RC ;
Debugging a Modelica model
I
Many things influence simulation
I
No unique translation of equations to executable code
I
Selection of state variables
I
Sorting, matching
I
Numerical methods
I
Mapping errors back to source code
Simulating models with errors
I
I
No error-message
Cryptic error-message
I
I
Simulation progressing
slowly
I
I
Non-linear system 15
failed
Even fixed-step Euler
solver is affected!
Debugging is
time-consuming
Simulating models with errors
I
I
No error-message
Cryptic error-message
I
I
Non-linear system 15
failed
Simulation progressing
slowly
I
Even fixed-step Euler
solver is affected!
I
Debugging is
time-consuming
I
You might not even find
the error
Compiler performs transformations (alias elimination of a)
(1a)
a=b
(1a)
c =a+b
(1b)
d =a−b
(1c)
c =a+b ⇒ c =b+b
(1a)
simplify
d =a−b ⇒ d =b−b
⇒
c = 2.0 ∗ b
simplify
⇒
d = 0.0
(2)
(3)
Integration of Tools
OMC
Simulation info.json
Simulation exe
Messages (XML)
OMEdit
Show your work – compiler-generated JSON
{ " op " : " before - after " , " display " : " simplify " , "
data " : [ " a = 1 . 0 * fn ( x ) " , " a = fn ( x ) " ] }
{ " op " : " before - after " , " display " : " inline " , "
data " : [ " a = fn ( x ) " , " a = 2 7 3 . 1 5 + x " ] }
Creating a model with an error
model C h a t t e r i n g E v e n t s 1 " Exhibits chattering after t =
0.5, with generated events "
Real x ( start = 1, fixed = true ) ;
Real y ;
Real z ;
equation
z = if x > 0 then -1 else 1;
y = 2 * z;
der ( x ) = y ;
annotation ( Documentation ( info = " < html >
<p > After t = 0.5, chattering takes place, due to the
discontinuity in the right hand side of the first
equation. </ p >
<p > Chattering can be detected because lots of tightly
spaced events are generated. The feedback to the user
should allow to identify the equation from which the
zero crossing function that generates the events
originates. </ p >
</ html > " ) , experiment ( StopTime = 1) ) ;
end C h a t t e r i n g E v e n t s 1 ;
Simulating a model with an error
The Transformations Browser
The Transformations Browser (EngineV6 Overview)
The Transformations Browser (EngineV6 Cropped)
The Transformations Browser (Nonlinear system)
Setting up Profiling
Performance Profiling
I
Measuring performance of equation blocks to find bottlenecks
I
Useful as input before model simplification for real-time
platforms
I
Integrated with the debugger so it is possible to show what
the slow equations compute
I
Suitable for real-time profiling (less information), or a
complete view of all equation blocks and function calls
Figure : Profiling all equations in MSL 3.2.1 DoublePendulum
ABB Commercial Application Use of Debugger
ABB OPTIMAX R provides advanced model based control
products for power generation and water utilities.
ABB: “OpenModelica provides outstanding debugging features
that help to save a lot of time during model development.”
Future Work
I
More specialised views (nonlinear/etc)
I
Trace more operations (tearing/etc)
I
Integration with plotting / result-files
Demo
Demo
Download