Marine systems simulation
|
Live plotting directly from SimObjects can be achieved by integrating with Python and matplotlib. This tutorial explains how.
This way of achieving live plotting depends on having Python (version >=3.6) and matplotlib installed on the computer running the simulation. Multiple Python installations exist, such as Spyder or Anaconda, but the clean version from www.python.org is recommended (because of comatibility issues with zlib
).
This tutorial will introde an example project demonstrating setup of the plotting, before instruct how to include the plotting functionalities in your own SimObjects and tailor plots to your needs.
First, clone the project from code.sintef.no,
git clone ssh://git@git.code.sintef.no/fhsim/fhsimpyplot.git
then build and compile the project (assuming you want visualization),
mkdir buildvis cd buildvis cmake -DPY_PLOT=1 -DFH_VISUALIZATION=1 -Ax64 .. cmake --build . --target install --config release
Note that the variable PY_PLOT
is set to 1 to enable the plotting functions. If you don't have Python installed, CMake will notify you and remove all Python dependencies when building and compiling the project.
To run a test using the SimObjects, simply execute FhRtVis.exe
with the SpringMass.xml
file that can be found in the input-folder.
Live-plotting of vertical position of mass using matplotlib and Python
Note that the labels and title in the plot-window can be set in the configuration file, see SpringMass.xml
for more information.
To include live-plotting functionalities in a SimObject:
Python
in the fhsimPython
-library to your own library.Add the following to the outer CMakeLists.txt
file:
${Python_SRC}
and ${Python_H}
to your target in CMakeLists.txt
. See the CMakeLists.txt
-file for the fhsimPyplot
-library for more information.PlotFunctions.h
in your simobject source file (as done in CMass.cpp
). In PythonFunctions
there are two plotting-functions defined, namely PlotSingle()
and Plot3DPos()
. More will be added. If you want, you can create tailored plot-functions in PlotFunctions.h/PlotFunctions.cpp
. This is briefly discussed in the following.Creating new plot functions is quite simple. Each plot function in PlotFunctions.h/PlotFunctions.cpp
are linked to the matplotlibcpp.h
-file. If you, as an example, want to create a function which plots two data-vectors, x1
and x2
, in two subplots in the figure, you can add something similar to the following code:
Note that syntax for creating a plot is quite similar to the syntax used by matplotlib in python.