Wolfram Computation Meets Knowledge

Modeling in the Text View

Hello World ModelExercise

The most basic Modelica model is a differential equation. In this example, a differential equation is implemented and simulated. Also, the process of creating an icon representing the model graphically is described in detail.

MODELS USED IN THIS TUTORIAL
HelloWorld

Hello World Model

There is a long tradition that the first example in any computer language is a trivial program printing the string "Hello World". Since Modelica, the language used in System Modeler, is an equation-based language, printing a string is of limited use. Instead the HelloWorld Modelica class solves a trivial differential equation:

Creating the Model

The variable x in this equation is a dynamic variable (and a natural choice of state variable) whose value can change over time. The time derivative is the derivative of x, written as der(x) in Modelica. All Modelica classes have a class declaration (block, model, package, etc.). In this example you will declare the class as a model.

You begin by creating a new model at the top level of the Modelica package hierarchy, i.e. the model will not be located inside any package. Choose File New Class.

2.gif

Choosing New Class from the File menu.

This will open the New Class dialog box, in which you will specify a name and description for the model. Give the model the name "HelloWorld" and the description "A differential equation". Also uncheck the Icon section, as you will create your own icon later. If the Icon section is checked, an icon can be selected for the new class from a set of predefined icons.

3.gif

Specifying a name and description for a new model.

After clicking the OK button, the model will be created and become visible in the Class Browser. At the same time, the model will also be opened in a class window. Click the Modelica Text View button in the toolbar to switch to the Modelica Text View of the class window.

4.gif

The Modelica Text View button in the toolbar in Model Center.

The textual representation of the empty model should look as follows.

model HelloWorld "A differential equation"
»;
end HelloWorld;

The symbol » in the second row contains hidden graphical information about the model and is automatically updated whenever you edit the model in any of the graphical views of the class window. Note that the description you entered in the dialog box has been added to the model. Now it is just a matter of adding the variable and the equation. You will do that by editing the definition of the model directly in the Modelica Text View.

model HelloWorld "A differential equation"
Real x(start = 1);
equation
der(x) = -x;
»;
end HelloWorld;

Note that when you declare the variable, you also set its initial value to 1 by specifying a value for its start attribute.

The HelloWorld model is now ready. Before simulating the model, you may want to verify its correctness by clicking the Validate Class button in the toolbar.

5.gif

The Validate Class button in the toolbar in Model Center.

This will generate a report in the Messages view, located below the class window. The validation of HelloWorld is successful, with only a notification about the initialization problem being underdetermined. You can learn more about the initialization problem in Removing Underdetermined Initialization.

6.gif

The Messages view in Model Center.

Simulating the Model

To simulate the model, click the Simulate Class button in the toolbar in Model Center.

7.gif

The Simulate Class button in the toolbar.

This will pop up a warning that no experiment information was found in the model. Assuming you are happy with the default settings for now, click the Simulate button in the dialog.

This starts Simulation Center, and the HelloWorld model will automatically be translated into an executable. An experiment is created for the HelloWorld model in the Experiment Browser of Simulation Center.

8.gif

The Settings view of the HelloWorld experiment in the Experiment Browser.

In the Experiment Browser, you can specify simulation settings, parameter values, and initial values for variables, but all settings will be left as is for now.

To plot the variable x over time in the plot window, open the Plot tab in the Experiment Browser and click the checkbox in front of x.

9.gif

Plotting the variable x in the HelloWorld model in Simulation Center.

Adding an Icon

To create an icon for the HelloWorld model, return to Model Center and switch to the icon view of the class window by clicking the Icon View button in the toolbar.

10.gif

The Icon View button in the toolbar.

To create an icon, use the drawing tools available in the Graphic Tools toolbar in Model Center.

11.gif

The drawing tools in the toolbar of Model Center.

Choose a suitable drawing tool, for example the Rectangle Tool, by clicking its corresponding button in the toolbar. This will change the appearance of the cursor. To start drawing, hold down the left mouse button and drag the cursor until you have the size and shape of the rectangle that you want. Release the mouse button to create the rectangle. To edit or view the rectangle properties, double-click the rectangle.

12.gif

Editing the properties of a rectangle.

Remove the border pattern, set the corner radius to 25, change the fill color to Dark Green, and select Solid as the fill pattern. When done, click the OK button. Next, press the Esc key to clear the selection in the icon view. Finally, choose the Text Tool and draw a text item covering the entire rectangle, and change the text to "Hello World" and the color to White in the Text Properties dialog box.

The reason why it was necessary to clear the selection before drawing the text item deserves an explanation. Without clearing the selection, you would have ended up moving the rectangle instead of adding a text item, as all drawing tools can also be used to move selected items.

The icon of the model should now look similar to the one following.

13.gif

The icon of the HelloWorld model.

The HelloWorld model will from now on be represented by this icon everywhere it is used.

Exercise

Change the dynamics of the HelloWorld model, for instance, by adding a parameter that changes the rate of decay in the equation and trying out the result.