A1. A first plot
Goal: the solver holds its residual history in an array: plot it from the program itself, as an interactive page and as an image.
fortran
program ch1_first_plot
!< Tutorial 1: the residual of a run, plotted from the arrays that hold it.
use foresight, only : figure_object, I4P, R8P
implicit none
type(figure_object) :: fig ! the figure: one plot, gnuplot defaults
real(R8P), allocatable :: it(:) ! iterations
real(R8P), allocatable :: res(:) ! continuity residual
integer(I4P) :: i
it = [(real(i, R8P), i = 1, 120)]
res = 10.0_R8P**(-0.045_R8P * it) * (1.0_R8P + 0.35_R8P * sin(it / 3.0_R8P))
call fig%init(width=640_I4P, height=380_I4P)
call fig%set_title('Residual history')
call fig%set_xlabel('iteration')
call fig%set_ylabel('continuity residual')
call fig%plot(it, res, title='continuity')
call fig%save('ch1.html') ! interactive page
call fig%save('ch1.svg') ! static image, the same drawing
endprogram ch1_first_plotBuilding it
bash
fobis build --mode foresight-static-gnu # lib/libforesight.a and its modules
gfortran -I lib/mod ch1_first_plot.f90 lib/libforesight.a -o ch1_first_plot
./ch1_first_plot # writes ch1.html and ch1.svguse foresight gives the whole API and the PENF kinds I4P, R8P. As a FoBiS or fpm dependency, see Installation.
Open ch1.html: it needs nothing but a browser, also from a file on a cluster copied to your laptop. The plot above is that page: wheel to zoom about the pointer (shift: x only, ctrl: y only), drag to pan, right-drag a box, u to undo, a to autoscale, g for the grid, h for help. The static image is the same drawing, for reports and slides:
Why it works
figure_objectis a figure: one plot, with gnuplot's defaults (600x480 px, autoscaled axes, the key top right).initresets it, here with a size.set_title,set_xlabel,set_ylabelare gnuplot'sset title,set xlabel,set ylabel: every method is named after the gnuplot command it does, so the gnuplot reference documents the library too.plot(x, y, title=...)adds a series;titleis its entry in the key. Call it again to add more.saverenders the figure; the format follows the extension:.htmlan interactive page,.svga static image of the same drawing,.pdfa PDF,.txt(or-, the standard output) text. Ticks and labels are computed by the same rules in Fortran and in the page, so a zoom regenerates them as gnuplot would.
Try this
call fig%save('-'): the plot as text on the terminal, the quickest look over ssh.- Plot
log10(rho)instead ofrho, then go to A3 for the right way, a log axis. - Save to
ch1.pdfand open it: the figure a paper takes (path C).
Pitfalls
- The arrays of a series must have the same size; the plot keeps a copy, so they can change after the call.
initforgets everything, series and settings: call it once per figure, before the settings.- A NaN is a gap in the line, not an error: a series full of NaN draws nothing.
Reference: Fortran Library, Output Formats, Interactive Viewer. Next: A2. Styles and the key.