Getting Started 5
Getting Started 5
DYMOLA
Getting started with Dymola
Introduction
This chapter will take you through some examples in order to get you started with Dymola.
For detailed information about the program, you are referred to the on-line documentation
and the user’s manuals. The on-line documentation is available in the Help menu after se-
lecting Documentation. The tool tips and the “What’s this” feature are fast and convenient
ways to access information.
Start Dymola. A Dymola window appears. A Dymola window operates in one of the two
modes:
• Modeling for finding, browsing and composing models and model components.
• Simulation for making experiments on the model, plotting results, and animating behav-
ior.
Dymola starts in Modeling mode. The active mode is selected by clicking on the tabs in the
bottom right corner of the Dymola window.
The operations, tool buttons available, and types of sub-windows appearing depends on the
mode, and additional ones have been added after this guide was written. Dymola starts with
a useful default configuration, but allows customizing.
24
Opening a demo exam-
ple.
Dymola starts loading the model libraries needed for the robot model and displays it.
The robot demo.
The package browser in the upper left sub-window displays the package hierarchy and it is
now opened up with the robot model selected and highlighted. The model diagram in the
right sub-window opens up and shows the top-level structure of the model. The model dia-
gram has an icon for the model of the robot with connected drive lines for the joints. The
It is not necessary to select the robot component explicitly by pressing the left button on the
mouse to access its menu. It is sufficient to just have the cursor on its icon in the diagram
window and press the right button on the mouse. The component browser also gives easy
access to the robot component. Just position the cursor over “mechanics”. The component
browser provides a tree representation of the component structure. The diagram window
and the component browser are synchronized to give a consistent view. When you select a
component in the diagram window, it is also highlighted in the component browser and vice
versa. The diagram window gives the component structure of one component, while the
component browser gives a more global view; useful to avoid getting lost in the hierarchical
component structure.
26
The Diagram window now displays the mechanical structure consisting of connected joints
and masses. The component browser is opened up to also show the internals of the mechan-
ics model component.
The mechanical struc-
ture of the robot.
Double click on, for example, r1 at the bottom of the diagram window. This is a revolute
joint. The Parameter dialogue appears. The Parameter dialogue can also be accessed from
the right button menu. Double clicking the left button selects the first alternative from the
right button menu.
The parameter dialogue allows the user to inspect actual parameter values. In this case the
parameter values are write protected to avoid unintentional changes of the demo example.
Thus the dialogue just has a Close button (and an Info-button). When the parameter values
can be changed there is one OK button and one Cancel button to choose between. The val-
ues are dimmed to indicate they are not given at the top-level model, but somewhere down
in the component hierarchy.
A parameter dialogue may have several tabs. This dialogue has the tabs: General, Anima-
tion and Advanced. In a tab the parameters can be further structured into groups as shown.
It is easy for a model developer to customize the parameter dialogues. Graphical illustra-
tions can be included to show meaning of parameters.
Next to each parameter field is a triangle, this gives you a set of choices for editing the pa-
rameters (Edit gives a matrix editor/function call editor, Edit Text gives a larger input field,
etc.). Some parameters have a list of choices where you can select values instead of writing
them. One example is the parameter n, which defines the axis of rotation. The value for this
revolute joint is {0, 1, 0}, i.e. the axis of rotation is vertical. Press Close.
Choices for n.
28
To learn more about this component, select Info. An HTML browser is opened to show the
documentation of the Revolute joint.
If you do only want to see the documentation select the component in the diagram, press the
right mouse button and select Info. An HTML browser is opened to show the documenta-
tion of the Revolute joint. By double clicking on the other components, you can, for exam-
ple, see what parameters a mass has.
Let us now inspect the drive train model. There are several possible ways to get it dis-
played. Press the Previous button (the toolbar button with the bold left arrow) once to go to
the robot model and then use the right button menu on one of the axis icons. Please, note
that robot.mechanics also has components axis1, ..., axis6, but those are just connectors.
You shall inspect for example robot.axis1. Another convenient way is to use the component
browser and use the right button menu and select Show Component. Since this is the first
menu option, double clicking will open up the component in the Diagram window. Please,
recall that double clicking on a component in the diagram window pops up the parameter
dialogue.
The robot drive train.
The drive train includes a controller. A data bus is used to send measurements and reference
signals to the controller and control signals from the controller to the actuator. The bus for
one axis has the following signals:
There are send elements to output information to the bus as illustrated in the model diagram
above. Similarly there are a read elements to access information from the bus.
The controller of an axis gets references for the angular position and speed from the path
planning module as well as measurements of the actual values of them. The controller out-
puts a reference for the current of the motor, which drives the gearbox.
The motor model consists of the electromotorical force, three operational amplifiers, resis-
tors, inductors, and sensors for the feedback loop.
30
The robot motor.
View the component gear in a similar way as for the other components. It shows the gear-
box and the model of friction of the bearings and elasticity of the shafts.
The robot gearbox.
In addition the simulation menu includes commands to setup and run simulations. The Set-
up menu item opens a dialogue allowing the setting of start and stop times. The icon indi-
cates which toolbar button to use for quick access to the set up dialogue. The Simulate
menu item starts a simulation.
In this case, however, a command script has been prepared. To run the script, select Com-
mands select ‘Simulate’. Dymola then translates the model and simulates automatically.
The animation window is opened automatically, because the simulation result contains ani-
mation information.
Animated 3D view of
the robot.
Start the animation by selecting Animation/Run or clicking the run button on the tool-bar,
Animation toolbar.
32
which contains the usual buttons of pausing rewinding, stepping forward and backward, re-
spectively.
It is possible to change the viewing position by opening a view controller, Animation/3D
View Control.
The command Plot makes a plot of the speed reference and actual speed of the third joint.
Plotting the speed.
The variable m is the mass and L is the distance from the support to the center of mass. Let
us assume the string is inextensible and massless, and further, let us neglect the resistance of
the air and assume the gravitational field to be constant with g as the acceleration of gravity.
The equation of motion for the pendulum is given by the torque balance around the origin
as
J*der(w) = -m*g*L*sin(phi)
where J is the moment of inertia with respect to the origin. Assuming a point mass gives
J = m*L^2
The variable w is the angular velocity and der(w) denotes the time derivative of w, i.e., the
angular acceleration. For the angular position we have
der(phi) = w
Start Dymola or if it is already started then give the command File/Clear All in the Dymola
window.
Click on the tab for Modeling at the bottom right. Select File/New Model.
The first step to create
a new model.
34
The dialogue to name a
new model component.
Click OK. You will have then have to ‘Accept’ that you want to add this at the top-level.
You should in general store your models into packages, as will be described later.
A model can be inspected and edited in different views. When specifying a behavior direct-
ly in terms of equations, it is most convenient to work with the model as the Modelica Text.
Press the Modelica Text toolbar button (the second rightmost tool button). The right sub-
window can now be used as a text editor.
To declare the parameters and the variables, enter as shown the declarations for the parame-
ters m, L and g, which also are given default values. The parameter J is bound in terms of
other parameters. Finally, the time varying variables phi and w are declared. A start value is
given for phi, while w is implicitly given a start value of zero.
The model is ready to be saved. Select File/Save. Call the file pendulum.mo and place it in
a working directory.
Simulation
Now it is time to simulate. To enter the Simulation mode, click on the tab at the bottom
right. The simulation menu is now activated and new tool bar buttons appear.
To set up the simulation select Simulation/Setup or click directly on the Setup toolbar but-
ton.
36
Selecting Setup in the
Simulation menu.
Set the stop time to 10 seconds. Click either OK, or Store in model (additionally this store
some information in the model).
To run the simulation select Simulation/Simulate or click directly on the Simulate toolbar
button.
Selecting Simulate in
the Simulation menu.
Dymola first translates and manipulates the model and model equations to a form suitable
for efficient simulation and then runs the simulation. You may explicitly invoke translation
yourself by selecting Simulation/Translate or click on the Translate toolbar button.
When the simulation is finished, the component browser displays variables to plot. Click in
the square box in front of phi to get the angle plotted as shown above.
Let us study a swing pendulum with larger amplitude and let it start in almost the top posi-
tion with phi = 3. It is easy to change initial conditions. Just enter 3 in the value box for phi.
Click on the Simulate tool button.
38
Pendulum angle when
starting in almost the
top position.
The results of previous simulations are available as the experiment Pendulum 1 and we can
open it up and have phi of the two runs plotted in the same diagram.
The choice to add a component is preselected. Click OK. A menu to declare a variable pops
up:
40
Complete the description:
Click OK and the text appearing in the bottom row is inserted into the Modelica text win-
dow.
The other quantities are defined in analogue ways. You have now Length selected in the
package browser, to find the quantity Mass in Modelica.SIunits, enter M and the browser
goes to the first component starting with M. If it is not the desired one, press M once again
and so on to find it.
When completing the form to declare the angle phi, the start value of the angle is defined by
clicking on the small triangle to the left of the value field and selecting Edit. A submenu
pops up:
The icon on the second line indicates graphical information or annotations. It may be dis-
played. Put the cursor on in the window, click the right mouse button to get the context
menu. Select Expand/Show entire text, which reveals that it is an annotation documenting
which version of the Modelica standard library was used. Dymola uses this information to
check if compatible versions of libraries are used and to support automatic upgrading of
models to new versions of libraries. A model developer can provide conversion scripts that
specify how models shall be upgraded automatically to new versions of a library.
42
Motor drive built with
standard compon-
nents.
As when building a real system, there are several approaches. One extreme approach is to
build the system from scratch. However, it is often a difficult and time-consuming task. An-
other approach is to investigate if the system already is available on the market or if there is
some product that easily can be adapted or modified. If not, build the system from compo-
nents available when possible and develop only when necessary.
The idea of object-oriented modeling is to support easy and flexible reuse of model know-
ledge. Modelica has been designed to support reuse of model components as parts in differ-
ent models and to support easy adaptation of model components to make them describe
similar physical components. The design of Modelica has also been accompanied by the de-
velopment of model libraries.
Dymola reads in the library. The Modelica Standard Library is hierarchically structured into
sub-libraries.
1. Blocks with continuous and discrete input/output blocks such as transfer functions, filters,
and sources.
2. Constants provides constants from mathematics, machine dependent constants and con-
stants from nature.
3. Electrical provides electric and electronic components such as resistor, diode, MOS and
BJT transistor.
4. Icons provides common graphical layouts (used in the Modelica Standard Library).
5. Math gives access to mathematical functions such as sin, cos and log.
7. SIunits with about 450 type definitions with units, such as Angle, Voltage, and Inertia.
To get documentation for the entire Modelica Standard Library, place the cursor on Modeli-
ca, press the right mouse button and select Info. Your web browser is directed to an html file
containing documentation for Modelica. This documentation has been generated automati-
cally from the Modelica description of the library. There is basic information such as the
content of the library, conventions and conditions for use.
Dymola comes also with other free model libraries. To have a list select File / Libraries.
The package menu gives direct access to the sub-libraries. We will need components from
various sub-libraries. We will need rotational mechanical components as well as electrical
components for the motor.
To open the Modelica.Mechanics, click on Mechanics (double-click shows its text).
44
Opening Modelica.Me-
chanics.
A Library window includes a package browser, where the components of the selected sub-
library are displayed in a special window.
It is possible to close the package browser by toggling the button to the bottom left.
By using the right button at the bottom it is possible to go up in the package hierarchy and
by double clicking on the icons in the window it is possible to go down in the hierarchy. The
left and right arrow buttons allow going back and forth as in an ordinary web browser.
Open Modelica.Mechanics.Rotational in the library window by first double clicking on the
icon for Mechanics
Modelica.Mechanics.
and then on the icon for Rotational. It contains components for rotating elements and gear-
boxes, which are useful for our modeling of the electrical motor drive.
46
The rotational mechan-
ics library window.
To get a model window for Inertia, select its icon in the library window and once again
press the right mouse button, select Show in New Window and a window for the model In-
ertia is created.
Switch to the Modelica Text representation, where you find Euler’s equation as the last
equation.
After this introduction of how to access model components and documentation of a library,
we will continue by actually building a model for an electric DC motor. This task will give
us more experience.
Enter DriveLib as the new name of the package and click OK, and ‘Accept’ in the informa-
tion-window.
48
A package DriveLib is created and made visible in the package browser. Select Modelica
text to get the Modelica representation, which at this stage just specifies a package with no
contents.
Click on OK.
Opening DriveLib in the package browser shows that it has a component Motor as desired.
50
An empty Motor model.
The model window now contains an empty Motor model. The diagram window has a gray
frame and grid to indicate that the component is not write protected. It is possible to toggle
the grid using the toolbar button.
We will now start building the motor model. To make it easier to follow the instructions, the
result is displayed below:
The finished motor
model with all compo-
nents.
You can also drag from a library window. If you do not have a library window for it then
open one. Go to Modelica.Electrical.Analog.Basic.
The basic analog com-
ponents library win-
dow.
Drag a resistor from Basic to the Motor window and place it as shown above. The compo-
nent browser displays that Motor has a component Resistor1.
52
Inserting a resistor
component.
When inserting a component is given an automatically generated name. The name may be
changed in the parameter window. Double click on the component, to get its parameter win-
dow. The parameter window can also be reached by pointing on the component and press-
ing the right mouse button and select Parameters.
The parameter window
of a resistor with de-
fault settings.
Change the component name to Ra. The parameter window allows setting of parameter val-
ues. To set the resistance parameter, R, select the value field of parameter R and input 0.5.
Click OK.
Similarly drag an inductor to the Motor window. Name it La and set the inductance, L, to
0.05.
Drag a ground component into the motor model. Name it G. The ground component is as
important as in real electrical circuits. It defines the electrical potential to be zero at its con-
nection point. As in the real world, never forget to ground an electrical circuit.
Drag an electromotive force, EMF, component into the motor model. Name it emf.
A voltage source is to be found in Modelica.Electrical.Analog.Sources. Use a library win-
dow or package browser to locate it. Select SignalVoltage and drag it to the model window
of Motor. Name it Vs. Let Vs be selected and use Edit/Rotate 90 to turn the signal input,
Vs.inPort, from a top position to a left position. SignalVoltage produces between its two
electrical pins, p and n, a voltage difference, p.v-n.v, that is equal to the signal input. Get the
Info for SignalVoltage displayed in your web browser (point on the icon and use the right
mouse button menu). The documentation shows that SignalVoltage extends the model Mod-
elica.Electrical.Analog.Interfaces.OnePort. Click on that link. The documentation shows
that the pin p is a filled blue square. To get the proper sign we would like to have pin p in
the top position. To flip the component, use Edit/Flip Vertical.
A rotating inertia component is to be found in Modelica.Mechanics.Rotational. Drag and
drop such an inertia component. Name it Jm and set the inertia parameter, J, to 0.001.
Now all model components are in place. Components are connected by drawing lines be-
tween connectors. To connect the resistor to the inductor, point at the right connector of the
resistor (the small white square) and drag to the left connector of the inductor. The resistor
and the inductor are now connected and the graphical result is a line between them. When
connecting the voltage source and the resistor, break the line by clicking at an intermediate
point. There is a possibility to obtain automatic Manhattanize of connections. Select the
connection, Edit/Manhattanize. Draw all connections. Note that we could have drawn a
connection between two components as soon as we have the components and we have not
to wait until all model components are in place.
Finally, we have to introduce a signal connector for the voltage control and a flange connec-
tor corresponding to the shaft of the motor so the motor can be connected to an environ-
54
ment. We would like to place the icon of the connectors at the border of the grid of the
drawing pane, because the icon of a model component also includes the connectors. The
connector inPort must be compatible with the connector of Vs.inPort. There is a simple au-
tomatic to get a connector inPort that is a clone of Vs.inPort. Start drawing a connection
from Vs.inPort and go to the left until you reach the border of the grid. Then you double
click and select Create Connector from the menu popped up. The connector flange_b is cre-
ated in a similar way. If you would like to adjust the position of a connector it is easy to get
into connect mode. This can be avoided by toggling the toolbar button Connect Mode (to
the right of the Toggle Grid button).
Click on the toolbar button (to left of the button for activating the Diagram view) to find
that you also there can see icons for the connectors. Let us draw an icon for the motor mod-
el. One design is shown below.
The icon of the electri-
cal DC motor.
Start by drawing the big red cylinder (shaded rectangle); Click the Draw rectangle button
(yellow rectangle) and lay out a rectangle. Let it be selected. Click on the arrow to the right
of the Fill Color button. Select Colors... and then select a red color. Click OK. To select the
gradient, click once again on the arrow to the right of the Fill Color button. Select Gradient/
Horizontal. Draw the rest of the parts using draw rectangle or draw closed polygon in an
analogous way. To enter the text, click the Text button (the button labeled A) and lay out a
rectangle that is as long as the cylinder and one grid squares high. In the window prompt for
Documentation View.
To enter a description, put the cursor in the window. Click right mouse button and select
Edit Source. The description is given as html code. When done, click right mouse button
and deactivate Edit Source.
We have now created the model. Save it.
56
Checking the model.
The connector inPort defines the voltage reference, and should be defined for the complete
model, but is viewed as a known input to the model.
It is important to test all models carefully, because that eliminates tedious debugging later
on. Let us connect the motor to a voltage source. Create a model called TestMotor (select
File/New/Model or the Edit/New Model in the context-menu for DriveLib) and insert it into
DriveLib. It is good practice to keep test models. Use the package browser to drag over a
Motor component from DriveLib to TestMotor. We need a source for the signal input to the
motor. Signal sources are to be found in Modelica.Blocks.Sources.
Signal sources.
Drag, for example, over Step to the model window and connect it to the motor.
Now it is time to simulate. Click on the tab for Simulation. Click on the toolbar button Sim-
ulate to start a simulation.
First, we may establish that a positive input signal, makes angular position increase. The
plot looks almost like a straight line. However, there are some wriggles in the beginning.
Zoom in; use the mouse to stretch a rectangle over that portion of the curve you would like
to see. We may also plot the angular velocity Motor1.Jm.w: There is an oscillation which
dies out and the velocity becomes constant. There is much to be done to validate the model.
However, model validation is out of the scope for this introduction to Dymola.
It is possible to show several curves in the same diagram. It is just to tick the variables to be
plotted. A curve is erased by ticking once more. The toolbar button Erase Curves (white
rectangle) erases all curves in the active diagram. It also possible to have several diagrams.
To get a new diagram, select Plot/New Diagram or click on the toolbar button. The new di-
agram becomes active. Tick Motor1.Jm.w and the result shown below is obtained. Selecting
a diagram makes it active. Selecting Plot/Delete Diagram removes the diagram.
58
Angular velocity.
To test the model MotorDrive for normal operation, we need to define a reference for the
position. This can be done in different ways. A simple approach is to add a signal source di-
rectly to MotorDrive. However, we may like to use MotorDrive also for other purposes. If
we would like to use the drive as a component we could add a connector for the reference as
we did for the electric DC motor model. However, here we will take the opportunity to
show another useful way, namely use of extends. We will develop a new class, say Motor-
DriveTest, which extends MotorDrive. Select MotorDrive in the package browser and se-
lect ‘Edit/Extend From’ in the context menu. This gives the same dialogue as File/New/
Model, but with several fields filled out. (It extends from MotorDrive and is inserted in the
same package, DriveLib.) Enter MotorDriveTest as the name of the model. Click OK. The
result is a model window, where the diagram layer looks exactly like that of MotorDrive.
However, the components cannot be edited. Try to move or delete a component. It has no
effect. Drag over a component Step from Modelica.Blocks.Sources and connect it. Save the
model.
A model can be used for different tasks. One is tuning the controller manually. Click on the
tab for Simulate. Translate the model MotorDriveTest. The PID controller has four parame-
ters: k, Ti, Td and Nd.
60
Tuning the controller.
There are many ways to tune a PID controller. One is to disable the integrator part by setting
a large vale for Ti, say 1000 here. Disable also the derivative part by setting Td small, say
0.001. Simulate for 10 seconds. The step response for k = 1 is very slow. Increase it to find
out what happens. We leave the problem of tuning the controller to the interested reader.
Parameter expressions
Modelica supports parameter propagation and parameter expressions, which means that a
parameter can be expressed in terms of others. Assume that the load is a homogeneous cyl-
inder rotating long its axis and we would to have its mass, m, and radius, r, as primary pa-
rameters on the top level. The inertia is
J = 0.5 * m * r^2
We need to declare m and r in MotorDrive. Open MotorDrive. Activate the Modelica Text
representation; press the Modelica Text toolbar button (the second rightmost tool button).
The parameters and variables are more than real numbers. They are physical quantities. The
Modelica standard library provides type declarations for many physical quantities. Open
Modelica.SIunits in the package browser. For the parameter r, which is a radius, it is natural
to declare it as being of type Radius. To find it enter R and the browser goes to the first
component starting with R. If it is not the desired one, press R once again and so on to find
Click OK and the text appearing in the bottom row is inserted into the Modelica text win-
dow. The parameter m is defined in an analogue way.
Parameter declara-
tions added to motor
drive.
In Modelica Text representation above, the components and connections are indicated by
icons. It is possible have them expanded textually. Click the right mouse button and select
Expand/Show components and connect.
62
It is also possible to expand the annotations such as the graphics for the icon of the model,
the positions and sizes of the icons of the components, the path of the connections etc., by
clicking the right mouse button and select Expand/Show entire text. However, we refrain
from showing it in this document.
Activate the diagram representation. Double click on the load icon to open the parameter
window.
Binding a parameter to
an expression.
Click in the value field of J and enter the expression for J. Click OK. The model window
now displays the expression for the load inertia. When entering the expression, you are
some times not sure about the exact name of the variables names, for example is the radius
called r, r0 or r1? The problem is even more pronounced when you would like to reference
a variable a bit down in the component hierarchy. Dymola can assist you. First you enter
0.5* and then you click on the small triangle to the left of the value field. Select Component
Reference and then m.
64
A bound parameter
cannot be changed in-
teractively.
The parameters r and m can be set interactively between simulation runs, but not load.J, be-
cause it is no longer a free parameter, because there is an expression binding it to r and m.
Start Dymola. The package browser shows the library MultiBody. This library includes 3D
mechanical components such as joints and bodies, which can be used to build a model of
the Furuta pendulum. Use the package browser to open MultiBody. You may also create a
new library window. Select MultiBody in the package browser and click the right mouse
button and select Open Library Window.
The MBS library win-
dow.
To build the Furuta pendulum, you will need to use the Parts and Joints sub-libraries. Open
them by double-clicking.
66
The MBS parts library
window.
68
You should now have arrived at a model that is similar to the following.
The diagram of the Fu-
ruta pendulum.
Now it is time to simulate. To enter the Simulation mode, click on the tab at the bottom
right. The simulation menu is now activated and new tool bar buttons appear.
When building 3D mechanical models, it is possible to get visual feedback to check that the
mechanical system is put together as intended. Click on the toolbar button Visualize. The
animation window shows the initial configuration of the system.
Try to control the pendulum in such a way as to get a stable upright position. (A trick is to
use a “mechanical PD controller”, i.e. a spring and damper attached to the tip of the second
bar and to a high position.)
70