You are on page 1of 27

MBSE

Model-Based Systems Engineering Center

The Modelica Language

Chris Paredis

2008-2012 Copyright Georgia Tech. All Rights Reserved.

ASE 6002: Systems Design and Analysis

Objectives
Become familiar with the Modelica Language
Classes Connectors Variables, Parameters, Constants Equations Operators If and when clauses

Practice applying Modelica to a simple example

2008-2012 Copyright Georgia Tech. All Rights Reserved.

ASE 6002: Systems Design and Analysis

Scope
This is not a comprehensive overview of Modelica Mindset: You will copy and modify existing models Other sources for help:
By example look at other existing models in the standard library Brief documentation in "Modelica Reference" (top package in browser) Modelica tutorial: Help / Documentation / Modelica Tutorial Modelica specification: Help / Documentation / Modelica Specification ReadingMaterial section B
2008-2012 Copyright Georgia Tech. All Rights Reserved. ASE 6002: Systems Design and Analysis 3

Modelica

2008-2012 Copyright Georgia Tech. All Rights Reserved.

ASE 6002: Systems Design and Analysis

What Goes on Inside the Icons?


Dymola uses the Modelica language to represent models Modelica Object-oriented Abstraction Encapsulation Inheritance Standardized by Modelica Association Energy-based modeling Acausal Equation-based
2008-2012 Copyright Georgia Tech. All Rights Reserved.

model Damper "Linear 1D translational damper" extends Interfaces.Compliant; parameter Real d ( final unit= "N/ (m/s)", final min=0) = 0 "damping constant"; SI.Velocity v_rel "relative velocity between flange_a and flange_b"; equation v_rel = der(s_rel); d*v_rel = f; end Damper;

ASE 6002: Systems Design and Analysis

A Simple Modelica Class


model SlidingMass "Sliding mass with inertia" extends Interfaces.Rigid; import SI = Modelica.SIunits; parameter SI.Mass m = 1 "mass o the sliding mass"; SI.Velocity v "absolute velocity of the component"; SI.Acceleration a "absolute acceleration of the component"; equation v = der(s); a = der(v); m*a = flange_a.f + flange_b.f; end SlidingMass;
2008-2012 Copyright Georgia Tech. All Rights Reserved. ASE 6002: Systems Design and Analysis

Class consists of declaration + equations Specify the default value for parameters Modelica is case sensitive Objects are declared as instances of classes Equations are acausal Use dot-notation to refer to object variables
6

A Simple Modelica Class: contd


partial model Rigid "Rigid connection of two translational 1D flanges " SI.Position s "absolute position of center of component (s = flange_a.s + L/2 = flange_b.s - L/2)"; parameter SI.Length L=0 "length of component from left flange to right flange (= flange_b.s - flange_a.s)"; equation flange_a.s = s - L/2; flange_b.s = s + L/2; end Rigid;

This model is abstract and cannot be instantiated Comments appear in parameter dialog and results browser

2008-2012 Copyright Georgia Tech. All Rights Reserved.

ASE 6002: Systems Design and Analysis

Restricted Class Types


Almost everything in Modelica is a Class More meaningful by introducing restricted classes
Package Model Record Type Connector Block Function

We will only consider these two

2008-2012 Copyright Georgia Tech. All Rights Reserved.

ASE 6002: Systems Design and Analysis

Connectors
Can include only declarations no equations allowed "Through variables" are preceded by the keyword "flow" Why is this important? There may be multiple across and through variables Refer to a specific connector's variables using the dot-notation

connector Flange "1D translational flange" SI.Position s "absolute position of flange"; flow SI.Force f "cut force directed into flange"; end Flange;

2008-2012 Copyright Georgia Tech. All Rights Reserved.

ASE 6002: Systems Design and Analysis

The Declaration Section


Predefined Types/Classes
Boolean has value true or false Integer Real (choose single or double precisions at runtime) String Enumeration

Use Modelica.SIunits as much as possible Variable declaration


Type, name, optional modifiers, optional comment, semicolon
Real Vc (start=1) "Voltage across capacitor"; Capacitor C1(C=1);
2008-2012 Copyright Georgia Tech. All Rights Reserved. ASE 6002: Systems Design and Analysis 10

The Declaration Section


Arrays
As in the C programming language (but index starting @ 1) Real R[3,3];

Constants
constant keyword, followed by type, name, value
constant Real g = 9.81 "gravity";

Parameters
parameter keyword, followed by type, name, default value
parameter Real C = 1 "Capacitance";

Constants and parameters do not count as variables when verifying squareness!


2008-2012 Copyright Georgia Tech. All Rights Reserved. ASE 6002: Systems Design and Analysis 11

The Equations Section


Indicated by keyword: equation Equations are non-causal
F = m*a; a = F/m; 0 = F m*a;

Needs to be "square"
Number of equations = number of variables (constants and parameters are not variables)

der(.) operator: derivative of variable over time

2008-2012 Copyright Georgia Tech. All Rights Reserved.

ASE 6002: Systems Design and Analysis

12

Modelica Operators
Complete list in Modelica Reference
Logical: and, or, not, ==, <> Mathematical: abs(.), ceil(.), floor(.), sign(.), sqrt(.), min(.,.), max(.,.), Matrices: cross(.,.), diagonal(.), ones(.), zeros(.), size(.), Events: change(.), edge(.), pre(.), reinit(.), noEvent(.), smooth(.) Time related: initial(), terminate(.) Error checking: assert(.,.)
2008-2012 Copyright Georgia Tech. All Rights Reserved. ASE 6002: Systems Design and Analysis 13

If-Then-Else Clause and Expression


Clause
parameter Boolean linear=true; parameter Boolean quad=false; Real x, y; equation if linear then y = x + 1; elseif quad then y = x^2 + x + 1; else y = Modelica.Math.sin(x); end if;

Expression
equation y = if linear then x + 1 else (if quad then x^2 + x + 1 else Modelica.Math.sin(x));

Never use equality comparison for Real:


Never: x == 3.2 Why not?

2008-2012 Copyright Georgia Tech. All Rights Reserved.

ASE 6002: Systems Design and Analysis

14

When Clause
Should be read as: "When condition becomes true then do" pre(.) is the value right before the condition became true Why is pre(.) not needed in algorithm?
[] Boolean u_pos = u>=0; algorithm when change(u_pos) then []
2008-2012 Copyright Georgia Tech. All Rights Reserved.

model ZeroCounter import Modelica.Blocks.*; Interfaces.RealInput u; Interfaces.IntegerOutput y; equation when u>=0 then y = pre(y) + 1; end when; end ZeroCounter; [] algorithm when u>=0 then y := y + 1; end when; end ZeroCounter;
15

ASE 6002: Systems Design and Analysis

Models with Variable Causality


model Diode extends TwoPin; parameter Voltage Vth; protected Real s "State variable"; equation v = if s<0 then s+Vt else Vth; i = if s<0 then 0 else s; end Diode;

I
s>0

s<0

s=0

Vth

Modeling assumptions
No resistance Linearity
2008-2012 Copyright Georgia Tech. All Rights Reserved. ASE 6002: Systems Design and Analysis 16

Another Example: Stribeck Friction


Quite complicated 5 states:
1. 2. 3. 4. 5.

locked startForward forward startBackward Backward

Modelica. Mechanics. Translational. Stop


17

2008-2012 Copyright Georgia Tech. All Rights Reserved.

ASE 6002: Systems Design and Analysis

Summary
Introduced the Modelica Language
Classes Connectors Variables, Parameters, Constants Equations Operators If and when clauses

Lets practice applying Modelica to a simple example

2008-2012 Copyright Georgia Tech. All Rights Reserved.

ASE 6002: Systems Design and Analysis

18

Example Problem: An Overhead Crane Winch


Model a winch
Cable can stretch

Model gravity
Gravity is only applied to masses

Simulate
Winch drum with diameter = 0.3m Cable compliance per length = 1e-4 (m/N)/m (very flexible to make it interesting) Driven by the DC motor from HW1 After 1 second, connected to 60V battery Lifting mass of 100kg Mass starts 10m below winch; 10m of cable unwound Stop simulation when mass is at 1m below winch
2008-2012 Copyright Georgia Tech. All Rights Reserved. ASE 6002: Systems Design and Analysis 19

Further Reading
Peter Fritzson, Principles of Object-Oriented Modeling and Simulation with Modelica 2.1, Wiley-IEEE Computer Society Press, 2003. (ISBN: 047147163) Modelica Tutorial
http://www.modelica.org/documents/ModelicaTutorial14.pdf Should contain everything you need to know

Modelica Specification
http://www.modelica.org/documents/ModelicaSpec22.pdf Advanced reference document
2008-2012 Copyright Georgia Tech. All Rights Reserved. ASE 6002: Systems Design and Analysis 20

Solutions

2008-2012 Copyright Georgia Tech. All Rights Reserved.

ASE 6002: Systems Design and Analysis

21

Simulation Model
dCMotor cableWinch=0...

stepVoltage

ground

2008-2012 Copyright Georgia Tech. All Rights Reserved.

+ -

fixed
fixed1=0

slidingMassGravity
22

ASE 6002: Systems Design and Analysis

Winch Model
Modify Modelica.Mechanics.Rotational.IdealGearR2T
Modifications are high-lighted in red
model Winch "Winch with flexible cable" parameter Modelica.SIunits.Length R = 0.2 "Radius of winch drum"; parameter Real c(final unit="(m/N)/m", final min=0) = 1e-6 "Cable compliance per unit length"; parameter Modelica.SIunits.Length L0=0 "initial length of unwound cable"; Modelica.SIunits.Length length "rest length of unwound cable"; Modelica.SIunits.Length stretchedLength "stretched length of cable"; Modelica.SIunits.Torque tau_support; Modelica.SIunits.Force f_support; // NOTE: "initial equations" are enforced only at start of simulation initial equation length = L0; stretchedLength = L0;
2008-2012 Copyright Georgia Tech. All Rights Reserved. ASE 6002: Systems Design and Analysis 23

Winch Continued
equation // make sure the length never goes to zero because that would // result in a singularity assert(length>0,"length of unwound cable must be positive"); // distance between translational connectors is the stretched length stretchedLength = bearingT.s - flange_b.s; // the equation for cable strain c*f_support = (stretchedLength-length)/length; // relate the angle to the length (flange_a.phi - bearingR.phi) = -length/R; 0 = flange_a.tau/R + flange_b.f; 0 = flange_a.tau + tau_support; 0 = flange_b.f + f_support; [] end Winch;

2008-2012 Copyright Georgia Tech. All Rights Reserved.

ASE 6002: Systems Design and Analysis

24

SlidingMass with Gravity


Modify Modelica.Mechanics.Translational.SlidingMass
Modifications are high-lighted in red
model SlidingMassGravity "Sliding mass with inertia and gravity" extends Modelica.Mechanics.Translational.Interfaces.Rigid; parameter Modelica.SIunits.Mass m(min=0)=1 "mass of the sliding mass"; parameter Modelica.SIunits.Acceleration g=9.81 "gravitational acceleration"; Modelica.SIunits.Velocity v "absolute velocity of component"; Modelica.SIunits.Acceleration a "absolute acceleration of component"; equation v = der(s); a = der(v); m*(a+g) = flange_a.f + flange_b.f; end SlidingMassGravity;

2008-2012 Copyright Georgia Tech. All Rights Reserved.

ASE 6002: Systems Design and Analysis

25

Simulation Model
model WinchTest Components.Winch cableWinch(L0=10, R=0.15, c=1e-4); ElectricVehicle.Components.DCMotor dCMotor; Modelica.Electrical.Analog.Basic.Ground ground; Components.SlidingMassGravity slidingMassGravity(m=100); Modelica.Mechanics.Translational.Fixed fixed; Modelica.Mechanics.Rotational.Fixed fixed1; Modelica.Electrical.Analog.Sources.StepVoltage stepVoltage(V=60, startTime=1); equation connect(dCMotor.flange_b, cableWinch.flange_a); connect(cableWinch.flange_b, slidingMassGravity.flange_a); connect(fixed.flange_b, cableWinch.bearingT); connect(fixed1.flange_b, cableWinch.bearingR); connect(stepVoltage.n, ground.p); connect(stepVoltage.p, dCMotor.pin_n); connect(stepVoltage.n, dCMotor.pin_p); algorithm when cableWinch.length <= 1 then Add this to stop the terminate("mass reached top"); simulation end when; end WinchTest;
2008-2012 Copyright Georgia Tech. All Rights Reserved. ASE 6002: Systems Design and Analysis 26

Alternative Approach
y

world

actuatedPris... n={0,1,0}

dCMotor

cableWinch=0...

stepVoltage

ground
m=100

2008-2012 Copyright Georgia Tech. All Rights Reserved.

+ -

fixed1=0

pointMass

ASE 6002: Systems Design and Analysis

27

You might also like