Exercise 1

1 Question

Packages are nice to use in Modelica, but what exactly is a package?


1.1

A package is simply a container or namespace for names of classes, functions, constants, and other allowed definitions. A package is also one or the restricted classes.


2 Question

Why use the word encapsulated when creating packages?


2.1

Adding the word encapsulated to the package keeps the package well structured as well as being easier to understand and maintain.


3 Creating a Package

Create a package that contains a constant PI, a type Distance, a function RectangleArea, a function TriangleArea, a function CirclePerimeter and a function CircleArea.


x
 
1
2



Write a class,
GeometryUser, that uses the package and calls the functions.


2
 
1
2

3.1

3.1.1 package Geometry

2
 
1
loadModel(Modelica)
2

40
 
1
package Geometry
2
  constant Real PI = 3.1415926536;
3
4
  type Distance = Real(unit = "m");
5
6
  function RectangleArea
7
    input Distance B;
8
    input Distance H;
9
    output Distance area;
10
  algorithm
11
    area := B*H;
12
  end RectangleArea;
13
14
15
  function TriangleArea
16
    input Distance B;
17
    input Distance H;
18
    output Distance area;
19
  algorithm
20
    area := RectangleArea(B, H)/2;
21
  end TriangleArea;
22
23
24
  function CirclePerimeter
25
    input Distance radius;
26
    output Distance perimeter;
27
  algorithm
28
    perimeter := 2*PI*radius;
29
  end CirclePerimeter;
30
31
32
  function CircleArea
33
    input Distance radius;
34
    output Distance area;
35
  algorithm
36
     area := PI*radius^2;
37
  end CircleArea;
38
39
end Geometry;
40

3.1.2 GeometryUser

7
 
1
class GeometryUser
2
  Geometry.Distance rectangleArea   = Geometry.RectangleArea(4, 5);
3
  Geometry.Distance triangleArea    = Geometry.TriangleArea(4, 5);
4
  Geometry.Distance circlePerimeter = Geometry.CirclePerimeter(3.5);
5
  Geometry.Distance circleArea      = Geometry.CircleArea(3.5);
6
end GeometryUser;
7

3.1.3 Simulation of GeometryUser

2
 
1
simulate( GeometryUser )
2

3
 
1
{val(rectangleArea,0), val(triangleArea,0),
2
val(circlePerimeter,0),val(circleArea,0)}
3