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 numpy and matplotlib installed on the computer running the simulation.
This tutorial will introduce 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,
Build and compile the project:
If you only plan to use the existing functionality, you can use a pre-built binary. You are encouraged to improve the library with pull-requests, fork it, or be inspired.
To include live-plotting functionalities in a SimObject you can add fhsim_pyplot
as a requirement in your SimObject library. We assume your SimObject library has a conanfile.py
.
fhsim_pyplot/1.0.0@sintef/stable
to its list of requirements.Add the following to your CMakeLists.txt
:
pyplot/pyplot.h
in your SimObject source file. In pyplot
there are two plotting-functions defined, namely plot_single()
and plot3D_pos()
. You are encouraged to contribute by adding more as described below.pyplot.dll
or libpyplot.so
to your FhSim runtime directory.Creating new plot functions is straightforward. Each plot function in pyplot/pyplot.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. Observe that the syntax for creating a plot is quite similar to the syntax used by matplotlib in python.