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.007885789000000001,
timeBackend = 0.00311999,
timeSimCode = 0.057880566,
timeTemplates = 0.004230079,
timeCompile = 0.2350166269999999,
timeSimulation = 0.007557978000000001,
timeTotal = 0.315833646
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.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:
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"
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;"