Plotting OpenFOAM Forces on the Fly

I usually use matplotlib during post-processing for plotting x-y data, as the plots look nice and I can use my other python classes to generate and parse the data, which is quite nice. This works perfectly when the original data doesn’t change, while a plot window is open, which is not the case for e.g. the forces on a patch that are sampled during a simulation, by a function object. For this purpose I use good old gnuplot, which can be updated easily.

The forces are stored in forces/0/forces.dat inside the particular case directory. One problem arises, though: The forces are not stored in a gnuplot friendly format but as nested vectors with round brackets, which gnuplot cannot process. These brackets must hence get removed somehow and if possible in an automatic fashion. The removing itself can be done using sed:

sed -e "s/[(,)]//g" forces/0/forces.dat

This replaces any round brackets by nothing and prints the output to stdout. We can either pipe this output into a file and plot this file or use the output of sed directly in gnuplot. As the first way is not as elegant as the second, we’ll just use the latter.

plot '<sed -e "s/[(,)]//g" forces/0/forces.dat' using 1:2 with lines

Whenever the plot is updated, the sed command is executed which in turn reads the latest forces data, removes the brackets and hands it back to gnuplot.