2D Plotting¶
This chapter covers the 2D plotting available in OpenModelica via OMNotebook, OMShell and command line script. The plots are shown in the OMPlot application, or written into files (see Plots in Files). See also OMEdit 2D Plotting.
Example¶
model 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-6, method = 'auto', 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.001335065,
timeBackend = 0.0025166330000000003,
timeSimCode = 5.658820000000001e-4,
timeTemplates = 0.0019373160000000001,
timeCompile = 0.20131779600000002,
timeSimulation = 0.006575630000000001,
timeTotal = 0.21436944900000002
end SimulationResult;
When the simulation is finished the file HelloWorld_res.csv contains the simulation data:
"time","x","der(x)"
0,1,-1
0.8,0.4493268098265707,-0.4493268098265707
1.6,0.2018940604493751,-0.2018940604493751
2.4,0.09071733826357407,-0.09071733826357407
3.2,0.04076280378698163,-0.04076280378698163
4,0.01831630718240065,-0.01831630718240065
4,0.01831630718240065,-0.01831630718240065
Use plot(x) to show the plot in OMPlot. With the argument outputFile,
the plot is written into an SVG or a PNG file instead. The plots in this guide
are made that way:
>>> plot(x, outputFile = "helloworld.svg")
true
Figure 41 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 cvode, which the default method auto chooses, 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 -stepSize=0.008")
true
>>> res:=strtok(readFile("HelloWorld_res.csv"), "\n");
>>> res[end]
"4,0.01831635014318319,-0.01831635014318319"
>>> plot(x, outputFile = "helloworld-detailed.svg")
true
Figure 42 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)
"impure 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 or xonly.\";
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 left 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 left 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.\";
input String yAxis = \"L\" \"Sets the variable to be plotted on the left (L) or right (R) y-axis.\";
input String yLabelRight = \"\" \"This text will be used as the right vertical label in the diagram.\";
input Real yRangeRight[2] = {0.0, 0.0} \"Determines the right vertical interval that is visible in the diagram. {0,0} will select a suitable range.\";
input Boolean stack = false \"Draws each curve in a lane of its own, one below the other.\";
input String outputFile = \"\" \"Writes the plot into this file (.svg or .png) instead of showing it.\";
output Boolean success \"Returns true on success\";
end plot;"
Plots in Files¶
With the argument outputFile, plot, plotAll and
plotParametric write the plot into an SVG or a PNG file instead of
showing it, as in the plots above. This does not
need OMPlot or a display, so it also works in scripts that run in a terminal
or on a server.
plotFigure draws a figure: one or more plots, one below the other,
each with its own curves and axes. A figure is an
OpenModelica.Scripting.Figure record, the record of the figures
annotation of the Modelica Language Specification. Thus plotFigure can also
draw the figures that getModelFigures gives for a model.
>>> plotFigure(Figure(title = "HelloWorld", plots = {
Plot(title = "State", curves = {Curve(y = "x", legend = "x, at the end %{x}")}),
Plot(title = "Derivative", curves = {Curve(y = "der(x)", color = "red", pattern = LinePattern.Dash)})}), "helloworld-figure.svg")
true
Figure 43 A figure with two plots of the HelloWorld example.¶
A curve refers to its variables by name, as a string, and %{x} shows the
final value of x. The axes show the units of the variables, or their
display units. A curve can also take its values from another result file, to
compare two simulations. The fields color, pattern, thickness and
fileName of a curve are not part of the figures annotation.
>>> list(OpenModelica.Scripting.Curve)
"record Curve \"One curve of a figure plot, from the figures annotation.\"
String x = \"time\" \"result reference\";
String y \"result reference\";
String legend = \"\";
Integer zOrder = 0;
String color = \"\" \"A CSS color such as \\\"red\\\" or \\\"#ff0000\\\"; empty = automatic. Not in the figures annotation.\";
LinePattern pattern = LinePattern.Solid \"Not in the figures annotation.\";
Real thickness = 0.25 \"In mm. Not in the figures annotation.\";
String fileName = \"\" \"The result file; empty = the one of the figure. Not in the figures annotation.\";
annotation(
preferredView = \"text\");
end Curve;"
>>> list(OpenModelica.Scripting.plotFigure, interfaceOnly=true)
"impure function plotFigure
input Figure figure;
input String outputFile = \"\" \"The file to write, .svg or .png. Empty: show each plot of the figure with plot or plotParametric.\";
input String fileName = \"<default>\" \"The result file of the curves without a fileName. <default> will read the last simulation result\";
input Integer width = 800 \"The width of each plot, in pixels.\";
input Integer height = 450 \"The height of each plot, in pixels.\";
input LegendPosition legendPosition = LegendPosition.top;
output Boolean success \"Returns true on success\";
end plotFigure;"
Without outputFile, plotFigure shows each plot of the figure the way
plot and plotParametric do.
