Hello World

Installation and Setup | Introduction | Multidomain | Components | Custom Components | Hello World | External Functions | Hierarchical Models | Systems

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.


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 SystemModeler, is an equation-based language, printing a string does not make much sense. Instead our Hello World Modelica class solves a trivial differential equation:

The variable x in this equation is a dynamic variable (and a 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 a package. Choose File New Class.


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".


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.


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

The textual representation of the model should look as follows.

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.

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

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.


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 Messages view in Model Center.

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


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.


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.


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

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.


The Icon View button in the toolbar.

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


The drawing tools in the toolbar of the 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.


Editing the properties of a rectangle.

Change the fill color to Dark Green, select Solid as the fill pattern, and 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" in the Text Properties dialog box. Change the Color in Text Properties to Yellow.

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 below.


The icon of the HelloWorld model.

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


Change the model equation, for instance by adding a parameter and trying out the result.

Next Chapter

External Functions »

Installation and Setup | Introduction | Multidomain | Components | Custom Components | Hello World | External Functions | Hierarchical Models | Systems