A4. From the log, with a script
Goal: plot the log a solver writes, one line per iteration, with a gnuplot script run by the foresight command line: no program to compile.
Here is the pretend solver, writing run.dat (with a restart at iteration 60) and run.csv:
solver.f90
fortran
program solver
!< A pretend flow solver: the logs every chapter of the tutorial reads.
!<
!< run.dat: iteration, three residuals, drag coefficient and its error, whitespace separated, a restart (blank line)
!< at iteration 60; run.csv: the same history as CSV with a header row.
use, intrinsic :: iso_fortran_env, only : real64
implicit none
integer, parameter :: rk = real64
integer :: dat, csv, it
real(rk) :: t, rho, mom, energy, cd, dcd
open(newunit=dat, file='run.dat', status='replace', action='write')
open(newunit=csv, file='run.csv', status='replace', action='write')
write(dat, '(A)') '# iteration continuity momentum energy cd dcd'
write(csv, '(A)') 'iteration,continuity,momentum,energy,cd,dcd'
do it = 1, 120
t = real(it, rk)
rho = 10.0_rk**(-0.045_rk * t) * (1.0_rk + 0.35_rk * sin(t / 3.0_rk))
mom = 10.0_rk**(-0.040_rk * t - 0.4_rk) * (1.0_rk + 0.25_rk * cos(t / 4.0_rk))
energy = 10.0_rk**(-0.050_rk * t + 0.3_rk)
cd = 0.30_rk + 0.25_rk * exp(-t / 25.0_rk) * cos(t / 6.0_rk)
dcd = 0.04_rk / sqrt(t)
if (it == 61) write(dat, '(A)') ''
write(dat, '(I0,5(1X,ES12.5E2))') it, rho, mom, energy, cd, dcd
write(csv, '(I0,5(",",ES12.5E2))') it, rho, mom, energy, cd, dcd
enddo
close(dat)
close(csv)
endprogram solvertext
$ head -n 4 run.dat
# iteration continuity momentum energy cd dcd
1 1.00482E+00 4.51026E-01 1.77828E+00 5.36869E-01 4.00000E-02
2 9.88751E-01 4.03780E-01 1.58489E+00 5.18076E-01 2.82843E-02
3 9.48652E-01 3.57237E-01 1.41254E+00 4.94586E-01 2.30940E-02A gnuplot script plots it:
gnuplot
# Tutorial 4: the solver log, plotted by a script.
set terminal html size 640,380
set output 'ch4.html'
set title 'Residual history'
set xlabel 'iteration'; set ylabel 'residual'
set logscale y
set grid
plot 'run.dat' using 1:2 with lines title 'continuity', \
'' using 1:3 with lines title 'momentum', \
'' using 1:4 every 8 with linespoints pt 7 title 'energy'text
$ foresight residuals.gp
foresight: ch4.html-e runs commands before the script, or instead of it:
text
$ foresight -e "set logscale y" -e "plot 'run.dat' u 1:2 t 'continuity'" -o ch4-e.svg
foresight: ch4-e.svgWhy it works
using 1:2plots column 2 against column 1;using 2alone plots it against the point number, asusing 0:2.''repeats the previous file; abbreviations work as in gnuplot:u 1:3 w l t 'momentum'.every 8keeps one point in 8, in each block.- The blank line of the restart ends a block: the lines break there, as in gnuplot. Two blank lines would start a new dataset, selected with
index. - The output defaults to the script name,
residuals.html;-osets another, and the extension picks the format. - The command line and the library are one engine: a script is what a program writes through
figure_objectcalls, line by line.
Try this
foresight residuals.gp -o residuals.txt: the same plot as text.gnuplot residuals.gp(with aset terminal svgline): the same script runs in gnuplot; path E has the differences.plot 'run.dat' index 0 u 1:2: the restart's first block only, after making its blank line two.
Pitfalls
- A column that holds text where a number is expected is a missing point, not an error:
set datafile missingand the data files page explain the rules. every 8counts inside each block: after the restart the markers start again at its first line.- Paths in a script are relative to where
foresightruns, not to the script.
Reference: Command Line, Data Files, plot items. Next: A5. CSV and expressions.