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 = "stdout            | info    | The initialization finished successfully without homotopy method.
stdout            | info    | The simulation finished successfully.
",
    timeFrontend = 0.004675414,
    timeBackend = 0.00388707,
    timeSimCode = 0.05672146,
    timeTemplates = 0.03603407600000001,
    timeCompile = 0.400597872,
    timeSimulation = 0.010536619,
    timeTotal = 0.51259938
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:

_images/helloworld.svg

Figure 21 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"
_images/helloworld-detailed.svg

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

Plot Command Interface

Plot command have a number of optional arguments to further customize the the resulting diagram.

>>> list(OpenModelica.Scripting.plot,interfaceOnly=true)
"function plot
  input VariableNames vars \"The variables you want to plot\";
  input Boolean externalWindow = false \"Opens the plot in a new plot window\";
  input String fileName = \"<default>\" \"The filename containing the variables. <default> will read the last simulation result\";
  input String title = \"\" \"This text will be used as the diagram title.\";
  input String grid = \"detailed\" \"Sets the grid for the plot i.e simple, detailed, none.\";
  input Boolean logX = false \"Determines whether or not the horizontal axis is logarithmically scaled.\";
  input Boolean logY = false \"Determines whether or not the vertical axis is logarithmically scaled.\";
  input String xLabel = \"time\" \"This text will be used as the horizontal label in the diagram.\";
  input String yLabel = \"\" \"This text will be used as the vertical label in the diagram.\";
  input Real xRange[2] = {0.0, 0.0} \"Determines the horizontal interval that is visible in the diagram. {0,0} will select a suitable range.\";
  input Real yRange[2] = {0.0, 0.0} \"Determines the vertical interval that is visible in the diagram. {0,0} will select a suitable range.\";
  input Real curveWidth = 1.0 \"Sets the width of the curve.\";
  input Integer curveStyle = 1 \"Sets the style of the curve. SolidLine=1, DashLine=2, DotLine=3, DashDotLine=4, DashDotDotLine=5, Sticks=6, Steps=7.\";
  input String legendPosition = \"top\" \"Sets the POSITION of the legend i.e left, right, top, bottom, none.\";
  input String footer = \"\" \"This text will be used as the diagram footer.\";
  input Boolean autoScale = true \"Use auto scale while plotting.\";
  input Boolean forceOMPlot = false \"if true launches OMPlot and doesn't call callback function even if it is defined.\";
  output Boolean success \"Returns true on success\";
end plot;"