2D Plotting

This chapter covers the 2D plotting available in OpenModelica via OMNotebook, OMShell and command line script. The plotting is based on OMPlot application.

Example

class HelloWorld
  Real x(start = 1, fixed = true);
  parameter Real a = 1;
equation
  der(x) = - a * x;
end HelloWorld;

To create a simple time plot the above model HelloWorld is simulated. To reduce the amount of simulation data in this example the number of intervals is limited with the argument numberOfIntervals=5. The simulation is started with the command below.

>>> simulate(HelloWorld, outputFormat="csv", startTime=0, stopTime=4, numberOfIntervals=5)
record SimulationResult
    resultFile = "«DOCHOME»/HelloWorld_res.csv",
    simulationOptions = "startTime = 0.0, stopTime = 4.0, numberOfIntervals = 5, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'HelloWorld', options = '', outputFormat = 'csv', variableFilter = '.*', cflags = '', simflags = ''",
    messages = "",
    timeFrontend = 0.007405127,
    timeBackend = 0.00349565,
    timeSimCode = 0.052785563,
    timeTemplates = 0.003507567,
    timeCompile = 0.212164866,
    timeSimulation = 0.008143575,
    timeTotal = 0.287625249
end SimulationResult;

When the simulation is finished the file HelloWorld_res.csv contains the simulation data:

Listing 1 HelloWorld_res.csv
"time","x","der(x)"
0,1,-1
0.8,0.4493289092712475,-0.4493289092712475
1.6,0.2018973974273906,-0.2018973974273906
2.4,0.09071896372718975,-0.09071896372718975
3.2,0.04076293845066793,-0.04076293845066793
4,0.01831609502171534,-0.01831609502171534
4,0.01831609502171534,-0.01831609502171534

Diagrams are now created with the new OMPlot program by using the following plot command:

Figure 15 Simple 2D plot of the HelloWorld example.

By re-simulating and saving results at many more points, for example using the default 500 intervals, a much smoother plot can be obtained. Note that the default solver method dassl has more internal points than the output points in the initial plot. The results are identical, except the detailed plot has a smoother curve.

>>> 0==system("./HelloWorld -override stepSize=0.008")
true
>>> res:=strtok(readFile("HelloWorld_res.csv"), "\n");
>>> res[end]
"4,0.01831609502171534,-0.01831609502171534"

Figure 16 Simple 2D plot of the HelloWorld example with a larger number of output points.

Plotting Commands and their Options

All of these commands can have any number of optional arguments to further customize the the resulting diagram. The available options and their allowed values are listed below.

Option Default value Description
fileName The result of the last simulation The name of the result-file containing the variables to plot
grid true Determines whether or not a grid is shown in the diagram.
title "" This text will be used as the diagram title.
logX false Determines whether or not the horizontal axis is logarithmically scaled.
logY false Determines whether or not the vertical axis is logarithmically scaled.
xLabel "time" This text will be used as the horizontal label in the diagram.
yLabel "" This text will be used as the vertical label in the diagram.
xRange {0, 0} Determines the horizontal interval that is visible in the diagram. {0, 0} will select a suitable range.
yRange {0, 0} Determines the vertical interval that is visible in the diagram. {0, 0} will select a suitable range.
curveWidth 1.0 Defines the width of the curve.
curveStyle 1

Defines the style of the curve.

SolidLine=1, DashLine=2, DotLine=3, DashDotLine=4, DashDotDotLine=5, Sticks=6, Steps=7.

legendPosition "top" Defines the position of the legend in the diagram. Possible values are left, right, top, bottom and none.
externalWindow false Opens a new OMPlot window if set to true otherwise update the current opened window.