Equation-Based Object-Oriented Languages for Acausal Modeling and Simulation Lecture 12a in EECS 144/244 University of California, Berkeley November 12, 2013 David Broman broman@eecs.berkeley.edu EECS Department University of California, Berkeley, USA and Linköping University, Sweden Some of the slides are based OSMC tutorials and contributed by Peter Fritzson (Based on book and lecture nodes), David Broman, Emma Larsdotter Nilsson, Peter Bunus, Adrian Pop, Jan Brugård, Mohsen Torabzadeh-Tari, and Adeel Asghar. Copyright © Open Source Modelica Consortium. 2 Agenda broman@eecs.berkeley.edu Part I EOO Languages for CPS Part II Modelica Overview Platform 1 Actuator Physical Interface Physical Plant 2 Network Physical Interface Platform 2 Sensor Computation 1 Delay 1 Computation 4 Computation 2 Sensor Platform 3 Physical Interface Delay 2 Computation 3 Physical Plant Plant 12 Physical Physical Interface Actuator Part III Modelyze – an extensible research language Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! Part III Modelyze – ! an Extensible Research Language! 3 broman@eecs.berkeley.edu Part I EOO Languages for CPS Platform 1 Actuator Physical Interface Physical Plant 2 Network Physical Interface Platform 2 Sensor Computation 1 Delay 1 Computation 4 Computation 2 Sensor Platform 3 Delay 2 Physical Interface Computation 3 Physical Plant Plant 12 Physical Part I ! EOO Languages! for CPS! Physical Interface Actuator Part II ! Modelica Overview! Part III Modelyze – ! an Extensible Research Language! 4 Cyber-Physical Systems (CPS) Power Plants Industrial Robots Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! broman@eecs.berkeley.edu Aircraft Part III Modelyze – ! an Extensible Research Language! 5 Modeling and Simulating Cyber-Physical Systems broman@eecs.berkeley.edu Platform 1 Actuator Physical Interface Physical Plant 2 Network Physical Interface Platform 2 Sensor Computation 1 Delay 1 Computation 4 Computation 2 Simulation with timing properties Various models of computation (MoC) Sensor Platform 3 Physical Interface Delay 2 Computation 3 Physical Plant Plant 12 Physical Model Equation-based model Hardware-in-the-loop (HIL) simulation Modeling Physical Interface Actuator Modeling Physical system available? Sensors System Actuators Physical system (the plant) Part I ! EOO Languages! for CPS! Cyber system: Computation (embedded) + Networking Part II ! Modelica Overview! Part III Modelyze – ! an Extensible Research Language! 6 Equation-Based Object-Oriented (EOO) Languages broman@eecs.berkeley.edu Domain-Specific Language (DSL) Models and Objects • Primarily domain: Modeling of physical systems • Multiple physical domains: e.g., mechanical, electrical, hydraulic Part I ! EOO Languages! for CPS! Equation-Based Object-Oriented (EOO) Part II ! Modelica Overview! • Object in e.g., Java, C++: object = data + methods • Objects in EOO languages: object = data + equations Part III Modelyze – ! an Extensible Research Language! 7 Equation-Based Object-Oriented (EOO) Languages Domain-Specific Language (DSL) connections! • Primarily domain: Modeling of physical systems objects (components)! Models and Objects Equation-Based ports! Object-Oriented (EOO) • Multiple physical domains: e.g., mechanical, electrical, hydraulic EOO model (textual) Part I ! EOO Languages! for CPS! broman@eecs.berkeley.edu Part II ! Modelica Overview! • Object in e.g., Java, C++: object = data + methods • Objects in EOO languages: object = data + equations EOO model (graphical) Part III Modelyze – ! an Extensible Research Language! Equation-Based Object-Oriented (EOO) Languages Domain-Specific Language (DSL) 8 broman@eecs.berkeley.edu Models and Objects • Primarily domain: Modeling of physical systems • Multiple physical domains: e.g., mechanical, electrical, hydraulic Equation-Based Object-Oriented (EOO) Acausality • Object in e.g., Java, C++: object = data + methods • Objects in EOO languages: object = data + equations • At the equation-level u=R*i • At the object connection level Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! Part III Modelyze – ! an Extensible Research Language! Equation-Based Object-Oriented (EOO) Languages Domain-Specific Language (DSL) Models and Objects • Multiple physical domains: e.g., mechanical, electrical, hydraulic Equation-Based acausal (non-causal) Object-Oriented (EOO) Acausality causal Part II ! Modelica Overview! • Object in e.g., Java, C++: object = data + methods • Objects in EOO languages: object = data + equations Physical topology ! • At the equation-level u = Ris*lost! i • At the object connection level Part III Modelyze – ! an Extensible Research Language! Equation-Based Object-Oriented (EOO) Languages Domain-Specific Language (DSL) 10 broman@eecs.berkeley.edu Models and Objects • Primarily domain: Modeling of physical systems • Multiple physical domains: e.g., mechanical, electrical, hydraulic Equation-Based acausal (non-causal) Object-Oriented (EOO) Acausality causal Part I ! EOO Languages! for CPS! broman@eecs.berkeley.edu Direction not determined at modeling time! • Primarily domain: Modeling of physical systems Part I ! EOO Languages! for CPS! 9 Part II ! Modelica Overview! • Object in e.g., Java, C++: object = data + methods • Objects in EOO languages: object = data + equations • At the equation-level u=R*i • At the object connection level Part III Modelyze – ! an Extensible Research Language! 11 Equation-Based Object-Oriented (EOO) Languages Domain-Specific Language (DSL) Models and Objects • Primarily domain: Modeling of physical systems • Multiple physical domains: e.g., mechanical, electrical, hydraulic Equation-Based Object-Oriented (EOO) • Modelica • VHDL-AMS • gPROMS • Modelyze • … Part I ! EOO Languages! for CPS! broman@eecs.berkeley.edu Acausality • Object in e.g., Java, C++: object = data + methods • Objects in EOO languages: object = data + equations • At the equation-level u=R*i • At the object connection level Part II ! Modelica Overview! Part III Modelyze – ! an Extensible Research Language! 12 broman@eecs.berkeley.edu Part II Modelica Overview Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! Part III Modelyze – ! an Extensible Research Language! 13 What is Modelica? broman@eecs.berkeley.edu A language for modeling of complex physical systems • • • • • • Robotics Automotive Aircrafts Satellites Power plants Systems biology Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! Part III Modelyze – ! an Extensible Research Language! 14 What is Modelica? broman@eecs.berkeley.edu A language for modeling of complex physical systems Primary designed for simulation, but there are also other usages of models, e.g. optimization. Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! Part III Modelyze – ! an Extensible Research Language! 15 What is Modelica? broman@eecs.berkeley.edu A language for modeling of complex physical systems i.e., Modelica is not a tool Free, open language specification: Available at: www.modelica.org Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! Part III Modelyze – ! an Extensible Research Language! 16 Modelica Tools broman@eecs.berkeley.edu Commercial Environments Free Environments Dymola by Dassault Systemes OpenModelica supported by OSMC SimulationX by ITI GmbH Jmodelica.org supported by Modelon LMS Imagine.Lab AMESim by LMS Modelicac (part of Scilab) MapleSim by Maplesoft SimForge MOSILAB by Fraunhofer FIRST CyModelica by CyDesign Labs OPTIMICA Studio by Modelon AB MWorks by Suzhou Tongyuan Wolfram SystemModeler by Wolfram Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! Part III Modelyze – ! an Extensible Research Language! 17 What is special about Modelica? broman@eecs.berkeley.edu Multi-Domain Modeling Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! Part III Modelyze – ! an Extensible Research Language! 18 What is special about Modelica? Multi-Domain Modeling Keeps the physical structure! ! broman@eecs.berkeley.edu Visual Acausal Component Modeling Acausal model (Modelica) Causal block-based model (Simulink) Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! Part III Modelyze – ! an Extensible Research Language! 19 What is special about Modelica? Hierarchical system modeling Multi-Domain Modeling k2 i qddRef qdRef Kd qdRef qRef 1 1 S S k1 cut joint r3Control r3Motor qd q: angle qd: angular velocity qdd: angular acceleration S 0.3 sum w Sum +1 +1 - rate2 rate3 b(s) 340.8 a(s) S joint=0 spring=c S iRef axis4 gear=i fric=Rv0 - Kv axis5 rel Jmotor=J pSum Visual Acausal Component Modeling 1 0.03 qRef axis6 tn r3Drive1 i broman@eecs.berkeley.edu axis3 tacho2 b(s) b(s) a(s) a(s) tacho1 PT1 axis2 g5 q qd C=0.004*D/w m Rd1=100 Rd2=100 - + cut in Rp1=200 Ri=10 diff + + pow er OpI Ra=250 La=(250/(2*D*w m)) Rp2=50 rate1 axis1 Vs Rd4=100 g1 Rd3=100 emf iRef g3 hall2 Courtesy of Martin Otter Part I ! EOO Languages! for CPS! y x inertial hall1 g2 Part II ! Modelica Overview! qd g4 w r q Part III Modelyze – ! an Extensible Research Language! 20 What is special about Modelica? Multi-Domain Modeling broman@eecs.berkeley.edu Visual Acausal Component A textual class-based language Modeling OO primary used for as a structuring concept Behaviour described declaratively using • Differential algebraic equations (DAE) (continuous-time) • Event triggers (discrete-time) Variable! declarations! ! Typed Declarative Textual Language Part I ! EOO Languages! for CPS! class VanDerPol "Van der Pol oscillator model" Real x(start = 1) "Descriptive string for x”; Real y(start = 1) "y coordinate”; parameter Real lambda = 0.3; equation der(x) = y; der(y) = -x + lambda*(1 - x*x)*y; end VanDerPol; Differential equations! ! Part II ! Modelica Overview! Part III Modelyze – ! an Extensible Research Language! 21 What is special about Modelica? Multi-Domain Modeling broman@eecs.berkeley.edu Visual Acausal Component Modeling Hybrid modeling = continuous-time + discrete-time modeling Continuous-time Discrete-time Typed Declarative Textual Language time Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! Hybrid Modeling Part III Modelyze – ! an Extensible Research Language! 22 Some Domains broman@eecs.berkeley.edu Domain Type Potential Flow Carrier Modelica Library Electrical Voltage Current Charge Electrical. Analog Position Force Linear momentum Mechanical. Translational Rotational Angle Torque Angular momentum Mechanical. Rotational Magnetic Magnetic potential Magnetic flux rate Magnetic flux Hydraulic Pressure Volume flow Volume HyLibLight Heat Temperature Heat flow Heat HeatFlow1D Chemical Chemical potential Particle flow Particles Under construction Presure Mass flow Air Translational Pneumatic Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! PneuLibLight Part III Modelyze – ! an Extensible Research Language! 23 Modelica Standard Library Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! broman@eecs.berkeley.edu Part III Modelyze – ! an Extensible Research Language! 24 Modelica in Autmotive Industry Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! broman@eecs.berkeley.edu Part III Modelyze – ! an Extensible Research Language! 25 Modelica in Avionics Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! broman@eecs.berkeley.edu Part III Modelyze – ! an Extensible Research Language! Modelica in Power Generation GTX Gas Turbine 26 broman@eecs.berkeley.edu Developed by MathCore for Siemens Hello Courtesy of Siemens Industrial Turbomachinery AB Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! Part III Modelyze – ! an Extensible Research Language! 27 Brief Modelica History broman@eecs.berkeley.edu Modelica design group meetings • First meeting in fall 1996 • International group of people with expert knowledge in both language design and physical modeling • Industry and academia Modelica Language Versions • v1.0 (1997), v2.0 (2002) v.2.2 (2005) v.3.0 (2007) 3.1 (2009) 3.2 (2010), 3.2 revision 1 (2012) Modelica Association established 2000 • Open, non-profit organization Modelica Conferences • 9 international conferences (2000-2012) Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! Part III Modelyze – ! an Extensible Research Language! 28 Typical Simulation Process Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! broman@eecs.berkeley.edu Part III Modelyze – ! an Extensible Research Language! 29 Simple model - Hello World! Equation: x = - x Initial condition: x(0) = 1 Name of model! broman@eecs.berkeley.edu Initial condition! model HelloWorld "A simple equation" Real x(start=1); parameter Real a = -1; equation der(x)= a*x; end HelloWorld; Continuous-time! variable ! Parameter, constant! during simulation ! Simulation in OpenModelica environment Differential equation ! 1 0.8 simulate(HelloWorld, stopTime = 2) plot(x) 0.6 0.4 0.2 0.5 1 Part I ! EOO Languages! for CPS! 1.5 2 Part II ! Modelica Overview! Part III Modelyze – ! an Extensible Research Language! 30 Textual and Graphical Models broman@eecs.berkeley.edu R1 R=10 R=100! L L=1! L=0.1 A C Used model (defined elsewhere)! G Named component = model instance! Modification of parameter value! Connect equations! Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! Part III Modelyze – ! an Extensible Research Language! 31 Equations and Inheritance broman@eecs.berkeley.edu R1 Pin p, n and Reals v and i are copied to the subclass R=10 R=100! L L=1! L=0.1 A C Equations are copied as well. G Inherits equations and ! components from ! TwoPin Differential equation! Algebraic equation! Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! Part III Modelyze – ! an Extensible Research Language! 32 Connectors (Ports) broman@eecs.berkeley.edu R1 R=10 R=100! L L=1! L=0.1 A C G Connectors are instances ! of a connector class. Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! Part III Modelyze – ! an Extensible Research Language! 33 Connections and Flow Variables R1 R=10 R=100! broman@eecs.berkeley.edu L L=1! L=0.1 A C G Equations from potential variables: L.n.v = AC.n.v AC.n.v = G.p.v Equation from flow variables:! L.n.i + AC.n.i + G.p.i = 0 Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! Fundamental concept making acausal modeling work (simplified) The resulting equation system is an DAE. (differential-algebraic equations). Part III Modelyze – ! an Extensible Research Language! 34 Hybrid Modeling broman@eecs.berkeley.edu Hybrid modeling = continuous-time + discrete changes (events) (Using Modelica terminology) Continuous-time Real x; Voltage v; Current i; Discrete changes discrete in time time Events! Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! Real x; Integer i; Boolean b; Part III Modelyze – ! an Extensible Research Language! 35 Event creation – when broman@eecs.berkeley.edu when-equations when <conditions> then <equations> end when; event 1 event 2 event 3 time Equations only active at event times Time event State event when time >= 10.0 then ... end when; when sin(x) > 0.5 then ... end when; Only dependent on time, can be scheduled in advance Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! Related to a state. Check for zero-crossing Part III Modelyze – ! an Extensible Research Language! 36 Reinit - discontinuous changes broman@eecs.berkeley.edu The value of a continuous-time state variable can be instantaneously changed by a reinit-equation within a when-equation model BouncingBall "the bouncing ball model" parameter Real g=9.81; //gravitational acc. parameter Real c=0.90; //elasticity constant Real height(start=10),velocity(start=0); equation der(height) = velocity; der(velocity)=-g; when height<0 then reinit(velocity, -c*velocity); end when; end BouncingBall; Reinit ”assigns” continuous-time variable velocity a new value Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! Initial conditions Part III Modelyze – ! an Extensible Research Language! 37 Modelica – large and complex broman@eecs.berkeley.edu We have just “scratched on the surface of the language” Examples of the features which has not been covered • Functions and algorithm sections • Arrays and matrices • Inner / outer variables (lookup in instance hierarchy) • Annotations • Loop constructs • Partial classes • Packages, blocks... And much more... Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! Part III Modelyze – ! an Extensible Research Language! 38 broman@eecs.berkeley.edu Part III Modelyze – an extensible research language Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! Part III Modelyze – ! an Extensible Research Language! 39 What is Modelyze? broman@eecs.berkeley.edu Purpose: Research language – address the expressiveness and analyzability problem by making the language extensible Modelyze (MODEL and analYZE) Small, simple, host language for embedding domain-specific languages (DSL) of different models of computation (MoC) Key aspect: Both the DSL and models in the DSL are defined in Modelyze (Broman & Siek, 2012) Gradually typed functional language (call-by-value) Formal semantics for a core of the language. Proven type soundness for the core. Prototype implementation (interpreter). Evaluated for series of equation-based DSLs. Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! Part III Modelyze – ! an Extensible Research Language! 40 Modelica Environment broman@eecs.berkeley.edu Model Model Model Library Library Library Result (e.g., simulation) Modelica Tool Modelica Model Language Specification - Type checking - Collapsing the instance hierarchy - Connection Semantics - Simulation (Runtime) Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! Part III Modelyze – ! an Extensible Research Language! 41 Modelyze Environment broman@eecs.berkeley.edu Model Model Model Library Library Library Library for using models Library forexpressing using models Library DSL - Connection Semantics - Connection Semantics semantics - Simulation (Runtime) - Simulation (Runtime) - Connection Semantics - Simulation (Runtime) Part I ! EOO Languages! for CPS! Result (e.g., simulation) Modelyze Tool Modelyze DSL Model Part II ! Modelica Overview! LanguageSpecification Specification Language Typechecking checking - - Type Collapsingthe theinstance instancehierarchy hierarch - - Collapsing - Connection Semantics - Simulation (Runtime) Part III Modelyze – ! an Extensible Research Language! 42 References and Further Reading • • • • • broman@eecs.berkeley.edu Sven Erik Mattsson, Hilding Elmqvist, and Martin Otter. Physical System Modeling with Modelica. Control Engineering Practice 6. Pages 501-510, 1998. Peter Fritzson. Principles of Object-Oriented Modeling and Simulation with Modelica 2.1. Wiley-IEEE Press, New York, 2004. Peter Fritzson, Peter Aronsson, Håkan Lundvall, Kaj Nyström, Adrian Pop, Levon Saldamli, and David Broman The OpenModelica Modeling, Simulation, and Software Development Environment. Simulation News Europe. Issue 44, Pages 8-16, ARGESIM, 2005 David Broman, Peter Fritzson, and Sébastien Furic. Types in the Modelica Language. In Proceedings of the Fifth International Modelica Conference, pages 303-315, Vienna, Austria, 2006. David Broman and Jeremy G. Siek. Modelyze: a gradually typed host language for embedding equation-based modeling languages. Technical Report UCB/EECS-2012-173, EECS Department, University of California, Berkeley, June 2012. See http://www.modelica.org for more information on Modelica, including the latest language specification. Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! Part III Modelyze – ! an Extensible Research Language! 43 Conclusions broman@eecs.berkeley.edu Platform 1 Actuator Physical Interface Physical Plant 2 Network Physical Interface Platform 2 Sensor Computation 1 Delay 1 Computation 4 Computation 2 Sensor Platform 3 Physical Interface Delay 2 EOO Languages are particularly good for physical modeling because of their acausal capability Computation 3 Physical Plant Plant 12 Physical Physical Interface Actuator Modelica is the current state-of-the-art EOO language. The fundamental formalism is DAEs. Modelyze is an extensible research language for embedding equation-based languages. Part I ! EOO Languages! for CPS! Part II ! Modelica Overview! Part III Modelyze – ! an Extensible Research Language!