36
pe lab Peter Fritzson 1 OpenModelica Environment OpenModelica Environment and Modelica Overview and Modelica Overview Peter Fritzson, Adrian Pop, Peter Aronsson Peter Fritzson, Adrian Pop, Peter Aronsson OpenModelica Course at INRIA, 2006 06 08 OpenModelica Course at INRIA, 2006 06 08

Peter Fritzson 1 OpenModelica Environment and Modelica Overview Peter Fritzson, Adrian Pop, Peter Aronsson OpenModelica Course at INRIA, 2006 06 08

  • View
    226

  • Download
    1

Embed Size (px)

Citation preview

pelabPeter Fritzson 1

OpenModelica Environment OpenModelica Environment and Modelica Overviewand Modelica Overview

Peter Fritzson, Adrian Pop, Peter AronssonPeter Fritzson, Adrian Pop, Peter Aronsson

OpenModelica Course at INRIA, 2006 06 08OpenModelica Course at INRIA, 2006 06 08

pelabPeter Fritzson 2

OpenModelicaOpenModelica

• Goal: comprehensive modeling and simulation environment for research, teaching, and industrial usage

• Free, open-source for both academic and commercial use

• Now under Berkley New BSD open source license

• The OpenModelica compiler (OMC) now translated into MetaModelica

• Invitation for open-source cooperation around OpenModelica, tools, and applications

pelabPeter Fritzson 3

Background Background

Modelica – Modelica – the Next Generationthe Next GenerationModeling LanguageModeling Language

pelabPeter Fritzson 4

Examples of Complex Industrial ApplicationsExamples of Complex Industrial Applications

• Robotics

• Automotive

• Aircraft

• Phone Systems

• Power plants

• Heavy Vehicles

pelabPeter Fritzson 5

Stored Scientific and Engineering KnowledgeStored Scientific and Engineering Knowledge

Model knowledge is stored in books and human minds which computers cannot access

“The change of motion is proportional to the motive force impressed “– Newton

pelabPeter Fritzson 6

The Form – EquationsThe Form – Equations

• Equations were used in the third millennium B.C.• Equality sign was introduced by Robert Recorde in 1557

Newton still wrote text (Principia, vol. 1, 1686)“The change of motion is proportional to the motive force impressed ” CSSL (1967) introduced a special form of “equation”:

variable = expressionv = INTEG(F)/m

Programming languages usually do not allow equations!

pelabPeter Fritzson 7

Declarative languageEquations and mathematical functions allow acausal modeling,high level specification, increased correctness

Multi-domain modeling Combine electrical, mechanical, thermodynamic, hydraulic,

biological, control, event, real-time, etc...

Everything is a classStrongly typed object-oriented language with a general class concept, Java & MATLAB-like syntax

Visual component programmingHierarchical system architecture capabilities

Efficient, non-proprietaryEfficiency comparable to C; advanced equation compilation, e.g. 300 000 equations, ~150 000 lines on standard PC

Modelica – The Next Generation Modeling Modelica – The Next Generation Modeling LanguageLanguage

pelabPeter Fritzson 8

Modelica Language PropertiesModelica Language Properties

• Declarative and Object-Oriented

• Equation-based; continuous and discrete equations

• Parallel process modeling of real-time applications, according to synchronous data flow principle

• Functions with algorithms without global side-effects(but local data updates allowed)

• Type system inspired by Abadi/Cardelli

• Everything is a class – Real, Integer, models, functions, packages, parameterized classes....

pelabPeter Fritzson 9

Object OrientedObject OrientedMathematical Modeling with ModelicaMathematical Modeling with Modelica

• The static declarative structure of a mathematical model is emphasized

• OO is primarily used as a structuring concept

• OO is not viewed as dynamic object creation and sending messages

• Dynamic model properties are expressed in a declarative way through equations.

• Acausal classes supports better reuse of modeling and design knowledge than traditional classes

pelabPeter Fritzson 10

Brief Modelica HistoryBrief Modelica History

• First Modelica design group meeting in fall 1996• International group of people with expert knowledge in

both language design and physical modeling• Industry and academia

• Modelica Versions• 1.0 released September 1997• 2.0 released March 2002• Latest version, 2.2 released March 2005

• Modelica Association established 2000• Open, non-profit organization

pelabPeter Fritzson 11

Modelica ConferencesModelica Conferences

• The 1st International Modelica conference October, 2000

• The 2nd International Modelica conference March 18-19, 2002

• The 3rd International Modelica conference November 5-6, 2003 in Linköping, Sweden

• The 4th International Modelica conference March 6-7, 2005 in Hamburg, Germany

• The 5th International Modelica conference September 4-5, 2006 in Vienna, Austria

pelabPeter Fritzson 12

Modelica Model Example – Industry RobotModelica Model Example – Industry Robot

inertialx

y

axis1

axis2

axis3

axis4

axis5

axis6

r3Drive1

1

r3Motorr3ControlqdRef

1

S

qRef

1

S

k2

i

k1

i

qddRef cut joint

q: angleqd: angular velocity

qdd: angular acceleration

qd

tn

Jmotor=J

gear=i

spring=c

fric

=R

v0

Srel

joint=0

S

Vs

-

+diff

-

+pow er

emf

La=(2

50/(2

*D*w

m))

Ra=250

Rd2=100

C=0.004*D/w m

-

+OpI

Rd1=100

Ri=10

Rp1=200

Rp2=50

Rd4=100

hall2

Rd3=100

g1

g2

g3

hall1

g4

g5

rw

cut in

iRef

qd q

rate2

b(s)

a(s)

rate3

340.8

S

rate1

b(s)

a(s)

tacho1

PT1

Kd

0.03

w Sum

-

sum

+1

+1

pSum

-

Kv

0.3

tacho2

b(s)

a(s)

q qd

iRefqRef

qdRef

Srel = n*transpose(n)+(identity(3)- n*transpose(n))*cos(q)- skew(n)*sin(q);wrela = n*qd;zrela = n*qdd;Sb = Sa*transpose(Srel);r0b = r0a;vb = Srel*va;wb = Srel*(wa + wrela);ab = Srel*aa;zb = Srel*(za + zrela + cross(wa, wrela));

pelabPeter Fritzson 13

Modelica Model ExampleModelica Model ExampleGTX Gas Turbine Power Cutoff MechanismGTX Gas Turbine Power Cutoff Mechanism

Hello

Courtesy of Siemens Industrial Turbomachinery AB

pelabPeter Fritzson 14

Recent Book, 2004Recent Book, 2004

Peter Fritzson

Principles of Object Oriented Modeling and Simulation with Modelica 2.1

Wiley-IEEE Press

940 pages

Book web page:www.mathcore.com/drmodelica

pelabPeter Fritzson 15

The OpenModelica Environment The OpenModelica Environment

pelabPeter Fritzson 16

OpenModelica End-Users vs. DevelopersOpenModelica End-Users vs. Developers

• OpenModelica End-Users• People who use OpenModelica for modeling and simulation

• OpenModelica Developers• People who develop/contribute to parts in the OpenModelica environment

including the OpenModelica compiler

pelabPeter Fritzson 17

OpenModelica End-User Subsystems OpenModelica End-User Subsystems

• OpenModelica End-User Subsystems – a pre-packaged kit containing tools needed for modeling, simulation, teaching

• OpenModelica Compiler (OMC) – compiles and executes/simulates Modelica models

• OMShell – interactive session handler for Modelica scripting• OMNotebook – interactive electronic notebook for Modelica

teaching (with DrModelica), scripting, and documentation• OpenModelica MDT – Eclipse Plugin (Modelica Development

Tooling), e.g. for library development (c.f. JDT – Java, CDT,)

• Graphic Model Editor from MathCore(only binary, but free for university usage)

pelabPeter Fritzson 18

OpenModelica Development Toolkit (OMDev) OpenModelica Development Toolkit (OMDev) – to Simplify Open Source Development– to Simplify Open Source Development• OMDev is a pre-packaged pre-compiled kit containing all

tools needed for OpenModelica development. Just unpack and start working on your platform. (Windows, (Linux))

• MetaModelica Compiler (MMC) – for developing OMC• OpenModelica Compiler (OMC) – for browsing support• Eclipse plugin MDT – (Modelica Development Tooling),

e.g. for compiler (OMC) development• Pre-compiled Corba (MICO) for tool communication• Packaged Gnu compiler (Mingw version for Windows)• Emacs mode• Online (web) Subversion for version handling• Online (web) Bugzilla for bug reporting• Automatic regression testing using a test suite• (Soon: release of interactive debugger)

pelabPeter Fritzson 19

OpenModelica Environment ArchitectureOpenModelica Environment Architecture

Modelica Compiler

Interactive session handler

Execution

Graphical Model Editor/Browser

Textual Model Editor

Modelica Debugger

Emacs Editor/Browser

DrModelica OMNoteBook Model Editor

Eclipse Plugin Editor/Browser

pelabPeter Fritzson 20

OpenModelica Client-Server ArchitectureOpenModelica Client-Server Architecture

Parse

Client: Eclipse Plugin MDT

Corba

Client: OMShell Interactive

Session Handler

Server: Main Program Including Compiler,

Interpreter, etc.

Interactive

SCode

Inst

Ceval plot

system

etc.

Untyped API

Typed Checked Command API

Client: Graphic Model Editor

pelabPeter Fritzson 21

Released in OpenModelica 1.4.0Released in OpenModelica 1.4.0

• OpenModelica compiler/interpreter – OMC • Interactive session handler – OMShell • OpenModelica Notebook with DrModelica –

OMNotebook • OpenModelica Eclipse plugin MDT

• Preliminary versions:• Graphic Editor – Beta version available

• Debugger – soon released, beta version being improved

• Emacs mode – available

pelabPeter Fritzson 22

OpenModelica Compiler/InterpreterOpenModelica Compiler/Interpreter

• New version (1.4.0) released May 15, 2006• Currently implemented in 100 000 lines of

MetaModelica

• Includes code generation, BLT-transformation, index reduction, connection to DASSL, etc.

• Most of the Modelica language including classes, functions, inheritance, modifications, import, etc.

• Hybrid/Discrete event support

pelabPeter Fritzson 23

Corba Client-Server APICorba Client-Server API

• Simple text-based (string) communication in Modelica Syntax

• API supporting model structure query and update

Example Calls:Calls fulfill the normal Modelica function call syntax.:

saveModel("MyResistorFile.mo",MyResistor)

will save the model MyResistor into the file “MyResistorFile.mo”.

For creating new models it is most practical to send a model, e.g.:

model Foo end Foo; or, e.g.,connector Port end Port;

pelabPeter Fritzson 24

Some of the Corba API functionsSome of the Corba API functionssaveModel(A1<string>,A2<cref>) Saves the model (A2) in a file given by a string (A1). This call is also in typed API.

loadFile(A1<string>) Loads all models in the file. Also in typed API. Returns list of names of top level classes in the loaded files.

loadModel(A1<cref>) Loads the model (A1) by looking up the correct file to load in $MODELICAPATH. Loads all models in that file into the symbol table.

deleteClass(A1<cref>) Deletes the class from the symbol table.

addComponent(A1<ident>,A2<cref>,A3<cref>,annotate=<expr>)

Adds a component with name (A1), type (A2), and class (A3) as arguments. Optional annotations are given with the named argument annotate.

deleteComponent(A1<ident>,A2<cref>)

Deletes a component (A1) within a class (A2).

updateComponent(A1<ident>,A2<cref>,A3<cref>,annotate=<expr>)

Updates an already existing component with name (A1), type (A2), and class (A3) as arguments. Optional annotations are given with the named argument annotate.

addClassAnnotation(A1<cref>, annotate=<expr>)

Adds annotation given by A2( in the form annotate= classmod(...)) to the model definition referenced by A1. Should be used to add Icon Diagram and Documentation annotations.

getComponents(A1<cref>) Returns a list of the component declarations within class A1:{{Atype,varidA,”commentA”},{Btype,varidB,”commentB”}, {...}}

getComponentAnnotations(A1<cref>) Returns a list {...} of all annotations of all components in A1, in the same order as the components, one annotation per component.

getComponentCount(A1<cref>) Returns the number (as a string) of components in a class, e.g return “2” if there are 2 components.

getNthComponent(A1<cref>,A2<int>) Returns the belonging class, component name and type name of the nth component of a class, e.g. “A.B.C,R2,Resistor”, where the first component is numbered 1.

getNthComponentAnnotation(A1<cref>,A2<int>)

Returns the flattened annotation record of the nth component (A2) (the first is has no 1) within class/component A1. Consists of a comma separated string of 15 values, see Annotations in Section 2.4.4 below, e.g “false,10,30,...”

getNthComponentModification(A1<cref>,A2<int>)??

Returns the modification of the nth component (A2) where the first has no 1) of class/component A1.

getInheritanceCount(A1<cref>) Returns the number (as a string) of inherited classes of a class.

getNthInheritedClass(A1<cref>,A2<int>)

Returns the type name of the nth inherited class of a class. The first class has number 1.

getConnectionCount(A1<cref>) Returns the number (as a string) of connections in the model.

getNthConnection(A1<cref>,A2<int>)

Returns the nth connection, as a comma separated pair of connectors, e.g. “R1.n,R2.p”. The first has number 1.

getNthConnectionAnnotation(A1<cref>,A2<int>)

Returns the nth connection annotation as comma separated list of values of a flattened record, see Annotations in Section 2.4.4 below.

addConnection(A1<cref>,A2<cref>,A3<cref>, annotate=<expr>)

Adds connection connect(A1,A2) to model A3, with annotation given by the named argument annotate.

updateConnection(A1<cref>,A2<cref>,A3<cref>,annotate=<expr>)

Updates an already existing connection.

deleteConnection(A1<cref>,A2<cref>,A3<cref>)

Deletes the connection connect(A1,A2) in class given by A3.

addEquation(A1<cref>,A2<expr>,A3<expr>)(NotYetImplemented)

Adds the equation A2=A3 to the model named by A1.

getEquationCount(A1<cref>)(NotYetImplemented)

Returns the number of equations (as a string) in the model named A1. (This includes connections)

getNthEquation(A1<cref>,A2<int>)(NotYetImplemented)

Returns the nth (A2) equation of the model named by A1. e.g. “der(x)=-1” or “connect(A.b,C.a)”. The first has number 1.

deleteNthEquation(A1<cref>,A2<int>)(NotYetImplemented)

Deletes the nth (A2) equation in the model named by A1. The first has number 1.

getConnectorCount(A1<cref>) Returns the number of connectors (as a string) of a class A1. NOTE: partial code instantiation of inheritance is performed before investigating the connector count, in order also to get the inherited connectors.

getNthConnector(A1<cref>,A2<int>) Returns the name of the nth connector, e.g “n”. The first connector has number 1.

getNthConnectorIconAnnotation(A1<cref>,A2<int>)

Returns the nth connector icon layer annotation as comma separated list of values of a flat record, see Annotation below. NOTE: Since connectors can be inherited, a partial instantiation of the inheritance structure is performed. The first has number 1.

getNthConnectorDiagramAnnotation(A1<cref>,A2<int>)

(NotYetImplemented)

Returns the nth connector diagram layer annotation as comma separated list of values of a flat record, see Annotation below. NOTE: Since connectors can be inherited, a partial instantiation of the inheritance structure is performed. The first has number 1.

getIconAnnotation(A1<cref>) Returns the Icon Annotation of the class named by A1.

getDiagramAnnotation(A1<cref>) Returns the Diagram annotation of the class named by A1. NOTE: Since the Diagram annotations can be found in base classes a partial code instantiation is performed that flattens the inheritance hierarchy in order to find all annotations.

getPackages(A1<cref>) Returns the names of all Packages in a class/package named by A1 as a list, e.g.: {Electrical,Blocks,Mechanics, Constants,Math,SIunits}

getPackages() Returns the names of all package definitions in the global scope.

getClassNames(A1<cref>) Returns the names of all class defintions in a class/package.

getClassNames() Returns the names of all class definitions in the global scope.

getClassRestriction(A1<cref>) Returns the kind of restricted class of <cref>, e.g. "model", "connector", "function", "package", etc.

isType(A1<cref>) Returns "true" if class is a type, otherwise "false".

isPrimitive(A1<cref>) Returns "true" if class is of primitive type, otherwise "false".

isConnector(A1<cref>) Returns "true" if class is a connector, otherwise "false".

isModel(A1<cref>) Returns "true" if class is a model, otherwise "false".

isRecord(A1<cref>) Returns "true" if class is a record, otherwise "false".

isBlock(A1<cref>) Returns "true" if class is a block, otherwise "false".

isFunction(A1<cref>) Returns "true" if class is a function, otherwise "false".

isPackage(A1<cref>) Returns "true" if class is a package, otherwise "false".

isClass(A1<cref>) Returns "true" if A1 is a class, otherwise "false".

isParameter(A1<cref>) Returns "true" if A1 is a parameter, otherwise "false".

isConstant(A1<cref>) Returns "true" if A1 is a constant, otherwise "false".

isProtected(A1<cref>) Returns "true" if A1 is protected, otherwise "false".

existClass(A1<cref) Returns "true" if class exists in symbolTable, otherwise "false".

existModel(A1<cref>) Returns "true" if class exists in symbol table and has restriction model, otherwise "false".

existPackage(A1<cref>) Returns "true" if class exists in symbol table and has restriction package, otherwise "false".

pelabPeter Fritzson 25

PlatformsPlatforms

• All OpenModelica GUI tools (OMShell, OMNotebook, ...) are developed on the Qt4 GUI library, portable between Windows, Linux, Mac

• Both compilers (OMC, MMC) are portable between the three platforms

• Windows – currently main development and release platform

• Linux – available• Mac (Berkeley) Unix – planned

pelabPeter Fritzson 26

Interactive Session Handler – on dcmotor ExampleInteractive Session Handler – on dcmotor Example(Session handler called OMShell – OpenModelica Shell)(Session handler called OMShell – OpenModelica Shell)

>>simulate(dcmotor,startTime=0.0,stopTime=10.0)>>plot({load.w,load.phi})

model dcmotor Modelica.Electrical.Analog.Basic.Resistor r1(R=10); Modelica.Electrical.Analog.Basic.Inductor i1; Modelica.Electrical.Analog.Basic.EMF emf1; Modelica.Mechanics.Rotational.Inertia load; Modelica.Electrical.Analog.Basic.Ground g; Modelica.Electrical.Analog.Sources.ConstantVoltage v;equation connect(v.p,r1.p); connect(v.n,g.p); connect(r1.n,i1.p); connect(i1.n,emf1.p); connect(emf1.n,g.p); connect(emf1.flange_b,load.flange_a);end dcmotor;

pelabPeter Fritzson 27

Event Handling by OpenModelica – BouncingBallEvent Handling by OpenModelica – BouncingBall

>>simulate(BouncingBall, stopTime=3.0);>>plot({h,flying});

model BouncingBall parameter Real e=0.7 "coefficient of restitution"; parameter Real g=9.81 "gravity acceleration"; Real h(start=1) "height of ball"; Real v "velocity of ball"; Boolean flying(start=true) "true, if ball is flying"; Boolean impact; Real v_new;equation impact=h <= 0.0; der(v)=if flying then -g else 0; der(h)=v; when {h <= 0.0 and v <= 0.0,impact} then v_new=if edge(impact) then -e*pre(v) else 0; flying=v_new > 0; reinit(v, v_new); end when;end BouncingBall;

pelabPeter Fritzson 28

Run Scripts in OpenModelicaRun Scripts in OpenModelica

• RunScript command interprets a .mos file• .mos means MOdelica Script file• Example:

>> runScript("sim_BouncingBall.mos")

loadFile("BouncingBall.mo");simulate(BouncingBall, stopTime=3.0);plot({h,flying});

The file sim_BouncingBall.mos :

pelabPeter Fritzson 29

OpenModelica MDT – Eclipse PluginOpenModelica MDT – Eclipse Plugin

• Browsing of Modelica/MetaModelica packages, classes, functions

• Automatic building of executables• Separate compilation• Syntax highlighting• Code completion, Code query support for

developers• Automatic Indentation

pelabPeter Fritzson 30

Eclipse MDT in Action – Browsing and Building Eclipse MDT in Action – Browsing and Building OMCOMC

pelabPeter Fritzson 31

New Graphic Model EditorNew Graphic Model Editor(From MathCore; runs on Windows, Linux)(From MathCore; runs on Windows, Linux)

• Runs together with OpenModelica

• Free for university usage

pelabPeter Fritzson 32

OpenModelica Simple Electronic Notebook with OpenModelica Simple Electronic Notebook with DrModelicaDrModelica

• Primarily for teaching

• OMNotebook Does not need Mathematica

pelabPeter Fritzson 33

Interactive Contents in DrModelica Contains Interactive Contents in DrModelica Contains Examples and Exercises from Modelica BookExamples and Exercises from Modelica Book

Recent Book, 2004:Recent Book, 2004:

pelabPeter Fritzson 34

OpenModelica Algorithmic Code Debugger (prel.)OpenModelica Algorithmic Code Debugger (prel.)

pelabPeter Fritzson 35

Meta-Modelica Compiler (MMC)Meta-Modelica Compiler (MMC)

• Supports extended subset of Modelica• Used for development of OMC• Some MetaModelica Language properties:

• Modelica syntax and base semantics

• Pattern matching (named/positional)

• Local equations (local within expression)

• Recursive tree data structures

• Lists and tuples

• Garbage collection of heap-allocated data

• Arrays (with local update as in standard Modelica)

• Polymorphic functions

• Function parameters to functions

• Simple builtin exception (failure) handling mechanism

pelabPeter Fritzson 36

ConclusionsConclusions

• www.ida.liu.se/projects/OpenModelica Download OpenModelica

• www.mathcore.com/DrModelica Modelica book page

• www.modelica.org Modelica Association

• Emails: {petfr,adrpo,petar}@ida.liu.se, [email protected]

• OpenModelica version 1.3.1 released Nov 2005• Recent OpenModelica version 1.4.0 released May

15, 2006• OpenModelica in MetaModelica• Many bugfixes• OpenModelica MDT Eclipse plugin• Graphic model editor (available for beta testing)

• Cooperation and feedback welcome!