Skip to content

F3. The report ​

Goal: a whole report, written by the program that has the results: text, three figures with their alternative texts and numbered captions, a table of statistics and a table of the history.

console
$ pf_report && ls report
drag.svg
drag_residual.svg
report.md
residuals.svg
console
$ head -n 16 report/report.md
# Run report

The pretend flow solver of path A: 120 iterations, a restart at iteration 60. This page, its figures and its tables were written by `pf_report.f90`.

![Plot: Convergence. x axis: iteration. y axis: residual. Series: continuity, momentum, energy.](residuals.svg)

*Figure 1. The three residuals fall by five decades or more; the restart at iteration 60 does not disturb them.*

![Plot: Drag coefficient. x axis: iteration. y axis: cd. Series: cd.](drag.svg)

*Figure 2. The drag coefficient settles at 0.30; its error bars shrink as one over the square root of the iteration.*

![Plot. x axis: continuity residual. y axis: cd. Series: after the restart.](drag_residual.svg)

*Figure 3. The drag coefficient against the continuity residual after the restart: it closes in on 0.30 as the residual falls.*

The page it writes is the run report: open it to see the result as VitePress renders it. The program reads the logs of path A's solver; its parts are below, the whole of it in pf_report.f90.

A figure drawn by a script, its caption set there:

fortran
! a figure drawn by a script: its caption set there, its information from the figure the script built
call gp%init('report/residuals.svg')
call gp%run_text("set terminal svg size 600,340"//nl// &
                 "set logscale y; set format y '10^{%L}'"//nl// &
                 "set xlabel 'iteration'; set ylabel 'residual'; set title 'Convergence'"//nl// &
                 "set caption 'The three residuals fall by five decades or more; the restart at iteration 60 does not "// &
                 "disturb them.'"//nl// &
                 "plot 'run.dat' u 1:2 w l t 'continuity', '' u 1:3 w l t 'momentum', '' u 1:4 w l t 'energy'", &
                 iostat, iomsg)
if (iostat /= 0_I4P) error stop iomsg
call gp%figure%save('report/residuals.svg', info=meta)
call add_figure(meta)

A figure of the library:

fortran
! a figure of the library: the drag coefficient and its error bars
call fig%init(width=600_I4P, height=340_I4P)
call fig%set_enhanced
call fig%set_title('Drag coefficient')
call fig%set_xlabel('iteration')
call fig%set_ylabel('c_d')
call fig%plot(it, cd, title='c_d', with='yerrorbars', ylow=cd - dcd, yhigh=cd + dcd)
call fig%set_caption('The drag coefficient settles at 0.30; its error bars shrink as one over the square root of '// &
                     'the iteration.')
call fig%save('report/drag.svg', info=meta)
call add_figure(meta)

Each figure goes into the page the same way, numbered:

fortran
subroutine add_figure(info)
!< The figure in the page: an image with its alternative text, its caption numbered below it.
type(figure_info), intent(in) :: info   !< The figure's information.
character(len=:), allocatable :: name   !< The file, relative to the page.
character(len=12)             :: number !< Figure number.

figures = figures + 1_I4P
write(number, '(I0)') figures
name = info%file(index(info%file, '/', back=.true.) + 1:)
write(md, '(A)') '!['//info%alt//']('//name//')', '', &
                 '*Figure '//trim(number)//'. '//info%caption//'*', ''
endsubroutine add_figure

A table written by the program, of the stats of the last iterations:

fortran
! a table written by the program: statistics of the last 60 iterations
r = stats(cd(61:))
write(md, '(A)') '## Statistics', '', 'The last 60 iterations, after the restart.', '', &
                 '| quantity | mean | standard deviation | min | max |', '| --- | --: | --: | --: | --: |'
write(md, '(A,4(" | ",F8.5)," |")') '| drag coefficient', r%y%mean, r%y%stddev, r%y%min, r%y%max
r = stats(log10(rho(61:)))
write(md, '(A,4(" | ",F8.3)," |")') '| log10 continuity residual', r%y%mean, r%y%stddev, r%y%min, r%y%max
write(md, '(A)') ''

A table written by a script into the same page, appended after the program closed it:

fortran
! a table written by a script into the page: the history every 20 iterations, its header from the CSV's
write(md, '(A)') '## History', '', 'Every 20 iterations, from `run.csv`.', ''
close(md)
call gp%run_text("set datafile separator comma; set datafile columnheaders"//nl// &
                 "set table '"//page//"' append markdown"//nl// &
                 "plot 'run.csv' every 20 using 1:""continuity"":""momentum"":""cd"" with table"//nl// &
                 "unset table", iostat, iomsg)
if (iostat /= 0_I4P) error stop iomsg

Why it works ​

  • The report is plain Markdown written with write: no dependency, the program decides what is said. The figures and tables are files next to it, referenced relatively, so the directory moves as a whole (to a wiki, a repository, a web page).
  • Scripts and library meet in the same program: gp%figure is the figure a script built, saved again with info to read its alternative text and caption.
  • The numbers in the text come from the run, not from a copy: a new run writes a new report.

Try this ​

  • Render the report outside the site: pandoc report/report.md -o report.html --standalone.
  • Add a figure of the timing (timestep.dat of the cookbook) and a table of its stats.

Pitfalls ​

  • Close the page before a script appends to it, and reopen it with position='append' to go on writing.
  • Relative image paths are relative to the page: write the figures where the page is, or adjust the paths.
  • BARUCH, the Markdown reader and writer of this family of libraries, will write such reports with a streaming writer (headings, lists, tables, escapes) when released; foresight does not depend on it, and the figure_info above is all it needs.

foresight is available under your choice of license: GPL-3.0-or-later, BSD-2-Clause, BSD-3-Clause or MIT (License).