Exercise 8
- Due No Due Date
- Points None
Introduction to Modelica
In this exercise we will familiarize ourselves with the Modelica language using the software Modelon Impact, and introduce the second laboratory assignment.
Exercise 8.1 - Accessing Impact, and creating a new workspace
Impact is a software developed by the Lund company Modelon. It is a web-based IDE that allows for easy construction, simulation and animation of Modelica models. The IDE follows a similar structure to other Modelica IDEs, such as Dymola or OpenModelica, but being web-based no installation is required for users.
At the department, we have instances of Impact running on a group of lab computers which are accessible over the internet. The computers are named philon-x
(possibly also ktesibios-x
will be available) where x is some number between 01-12. To access e.g. philon-08
, open your web browser1 and type https://philon-08.control.lth.se
. It is important that not too many students use the same computer at the same time, so choose a computer at random and visit its webpage.
Sign in, click Start My Server
, choose modelon-impact-for-jupyterhub-combined
and press start. You have now spawned a new combined jupyter notebook / Impact server. Open a new launcher under File -> New Launcher
and press the Impact
button. Once started, create a new personal workspace by naming it after e.g. your STIL ID. You are now ready to continue to the next exercise.
Important: When working with Impact, please download your models and save them on your personal computer! The workspaces saved on one "x" computer will not be accessible on other computers, and the files will be purged from time to time. If you have not saved the files on your own personal computer, you will then lose that work.
1. It works for Chrome, but not Firefox
Exercise 8.2 - a first Modelica script
In your workspace, create a new model and give it a suitable name.
To write Modelica code in Impact, expand the left-side menu by pressing the small >
button to the left, find your model under the workspace
tab, right click it and choose Edit source
2,3. Update the code to the following.
model a_suitable_name "Hello World!"
parameter Real c = 2;
Real y(start=1);
equation
der(y) + y = c;
annotation(...);
end a_suitable_name;
Defining,
- A real parameter
and a real variable
with
- The equation relation describing the ODE
To simulate the model, first press the "Experimentation" tab in the top of the screen, and expand the right-side menu by pressing the small <
button to the right. Here you can set different simulation parameters. Change the stop time to 10 seconds, then simulate the model by pressing the Execution
button (the big play button).
Once completed, you can inspect the different variables by pressing the Result
tab at the top of the screen. To plot a variable, simply drag it to the center of the screen, do this now with .
2. Sometimes the code editor is turned off, and this option not displayed. To enable it, enter the Settings
menu in the top right corner and press enable code editor
.
3. If the option enable code editor
does not exist. Try to load an existing model (ex. some standard library model), right-click on a component, and try to open it as "code" rather than a diagram. Then, in the upper left corner, a dropdown menu will appear. Here you can toggle between the "diagram" and the "code" mode for all of your models. (I'll update this instruction to be more specific about how to open a component asap. /Martin)
Exercise 8.3 - the Lorenz attractor
You will now be tasked with implementing a more complicated ODE on your own. The following system, developed by Edward Lorenz for modeling atmospheric convection is known as the Lorenz system
For certain values of the parameters the Lorenz system becomes chaotic, meaning that minuscule changes in the values of the variables give rise to large differences in the resulting solution at later times. For the Lorenz system, most initial values will however give trajectories that end up in a set of strange orbits known as the Lorez attractor.
Create a new model implementing the Lorenz system with and
, and simulate it for 10 seconds. Plot the three variables in the same figure by dragging them into the same plot window. Further, plot
(and
) against
by creating a new figure for
, and dragging
into this figure's x-axis. Can you spot the attractor?
Exercise 8.4 - Models, instances and connectors
As an object-oriented language, Modelica allows us to create multiple instances (or objects) of defined models. It is further possible to share information between instances using the connector component. In this exercise, we will use these concepts to connect multiple models of a leaky water tank.
The height h of water in the leaky tank can be modeled using the following ODE
where is some constant, and
some external inflow.
Start by creating a new model in Impact, and open up the Modelica source editor. Inside the model, define the following connector, nested model and instance of the nested model.
connector WaterFlow
Real x;
end WaterFlow;
model WaterTank
WaterFlow F_in, F_out;
Real A, h
equation
der(h) = ...
F_out.x = ...
end WaterTank;
WaterTank w1(A=1, h(start=1), F_in.x=0.5)
a) Complete the implementation. Simulate the water tank w1
for 5 seconds and plot your results.
b) Create a second instance of the model named w2
, but set no value for the input w2.F_in.x
. Instead, connect the flows using connect(w1.F_out, w2.F_in)
under the equation block in the outer model. Simulate the model and plot the result, how many tanks can you connect in this manner?
Exercise 8.5 - Blocks and animation
Writing large models from scratch is both difficult and time consuming, but thanks to the object-oriented nature of Modelica it is easy to reuse already existing models to create even more advanced models. The Modelica Standard Library has many predefined models that can be used in this way.
To simplify model construction, it is possible to create models in a GUI (similar to Simulink) where objects and connections can be quickly created by drag-and-drop of model blocks. In this exercise, we will use this block programming to create a model of a simple pendulum.
First, create a new model. Under the Libraries
tab in the left-side menu, go to Modelica -> Mechanics -> MultiBody
. From here, drag and drop the following blocks to the center of the screen.
- a
World
block - a
Joints -> Revolute
block - a
Parts -> BodyBox
block
The blocks can be connected with left click drag from one connector to another (The connectors are for the MultiBody package the small white/gray rectangles at each block). Connect world.frame_b
to the connector revolute.frame_a
, and revolute.frame_b
to bodyBox.frame_a
.
By double clicking any of the blocks in the Diagram window you can set the parameters for that object. Open up the parameter settings for your bodyBox
and set r = {1,0,0}
.
An example of the final diagram is shown below.
After creating the model in the diagram window, you can inspect the resulting Modelica code generated by right clicking the model in under the workspace
tab and choose Edit source
.
Notice that we have created, and connected, three new objects named world
, revolute
and bodyBox
of the models corresponding to the blocks. Further, by right clicking any of the blocks in the diagram window and pressing View source
, you can inspect the Modelica code for each of these models.
Now simulate the pendulum for 10 seconds. As we have a physical model from the Modelica Standard Library, an animation of the simulation has also been generated. Right click the large eye symbol and press View 3D animation
, and hit play to run it.
Exercise 8.6 - Introduction to Lab 2
In the second lab of this course, you will be given an almost complete Modelica model of a small Furuta pendulum. It will be your task to
-
perform some hands-on parameter tuning from real data
-
extend the tuned model with an additional pendulum
-
implement a given controller to dampen disturbances
You can read more about it in Lab 2. Download the model and see that you can get it running in Impact