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. See also OMEdit 2D Plotting.


model HelloWorld
  Real x(start = 1, fixed = true);
  parameter Real a = 1;
  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-6, method = 'dassl', fileNamePrefix = 'HelloWorld', options = '', outputFormat = 'csv', variableFilter = '.*', cflags = '', simflags = ''",
    messages = "LOG_SUCCESS       | info    | The initialization finished successfully without homotopy method.
LOG_SUCCESS       | info    | The simulation finished successfully.
    timeFrontend = 0.002282421,
    timeBackend = 0.006307601,
    timeSimCode = 6.784730000000001e-4,
    timeTemplates = 0.00230281,
    timeCompile = 0.4946645639999999,
    timeSimulation = 0.012554898,
    timeTotal = 0.518942511
end SimulationResult;

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

Listing 1 HelloWorld_res.csv

Use plot(x) to plot the diagram using OMPlot.


Figure 30 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")
>>> res:=strtok(readFile("HelloWorld_res.csv"), "\n");
>>> res[end]

Figure 31 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 = \"simple\" \"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;"