13. The result field
After a run, the solver leaves the temperature field in the case's field.dat. The Result panel reads it and shows it: a heatmap in a colormap, continuous or in bands, with a colour-scale legend; contour lines over it; a probe line the user drags, whose temperature profile is plotted beside it, with the spread of the neighbouring columns as error bars; and a probe point that reads the temperature under it.
The whole program: tut13_field.F90.
What this chapter adds to chapter 12's program, tut12_run.F90:
load_field: Read the case's field.dat, written by the solver; no file, no fielddraw_result: The Result paneldraw_profile: The temperature along the probe line, and the spread of the five columns around it at every fourth node- changed:
draw_panels,follow_run,open_case,write_examples
Reading the field
open(newunit=u, file=case_dir//'/field.dat', status='old', action='read', iostat=ios)
if (ios /= 0) return ! not run yet
read(u, *, iostat=ios) ! the comment line
read(u, *, iostat=ios) nx, ny, flx, fly
if (ios == 0) then
deallocate(field)
allocate(field(nx, ny))
read(u, *, iostat=ios) field ! row after row: field(:, 1), field(:, 2), ...
endif
close(u)
if (ios /= 0) then
deallocate(field)
allocate(field(0, 0))
call app%notify('field.dat does not read', kind='error')
return
endifThe solver writes a comment line, the grid and the plate's size, then the temperatures row after row; one list-directed read fills field(nx, ny) in Fortran's order, field(i, j) at x = (i - 1) dx, y = (j - 1) dy. The studio reads it after a converged run and when a case with a result is opened, then brings the Result panel forward.
A colormap, continuous or in bands
call ig_set_next_item_width(140.0_real64)
if (ig_combo('colormap', map, MAPS)) continue
call ig_same_line()
call ig_set_next_item_width(140.0_real64)
if (ig_slider('bands', bands, 0_int32, 16_int32)) bands = merge(0_int32, bands, bands == 1_int32) ! 0: continuous
call ig_same_line()
if (ig_checkbox('contour lines', contours)) continue
if (MAPS(map) == 'Jet') then ! a mark with the colour: never colour alone
call ig_same_line()
call ig_text_colored(ICON_TRIANGLE_ALERT//' Jet: not perceptually uniform', &
[0.95_real64, 0.75_real64, 0.2_real64, 1.0_real64])
endif
if (bands > 0_int32) then
cmap = ip_colormap(trim(MAPS(map)), levels=bands) ! the same map in discrete bands
else
cmap = ip_colormap(trim(MAPS(map)))
endifip_colormap(name)gives a colormap's id by name: FIGURE bundles perceptually uniform maps ('cividis','batlow', the diverging'cool-warm', ...) besides ImPlot's own, which keep their capitalized names ('Viridis','Jet') (Colormaps).levels=bandsgives the same map in discrete bands: equal temperature steps become visible as edges, as on a printed chart (Discrete levels).- Why not Jet: its lightness rises and falls along the scale, so a smooth field shows bright rings and sharp edges it does not have, and its order is lost in greyscale (Why not Jet). The studio offers it, and says so next to it with a mark, not only with a colour.
The heatmap, its legend and its contour lines
if (ip_begin_plot('Temperature [C]', width=420.0_real64, height=-1.0_real64, equal=.true.)) then
call ip_setup_axes('x [m]', 'y [m]')
! the cells' edges: half a spacing beyond the first and last nodes
call ip_heatmap('##t', field, -0.5_real64 * dx, flx + 0.5_real64 * dx, -0.5_real64 * dy, fly + 0.5_real64 * dy, &
scale_min=tmin, scale_max=tmax, colormap=cmap)
if (contours .and. bands > 0_int32) then ! the lines at the bands' edges
call ip_contour('##isotherms', field, 0.0_real64, flx, 0.0_real64, fly, &
levels=[(tmin + k * (tmax - tmin) / bands, k = 1_int32, bands - 1_int32)], &
color=[0.1_real64, 0.1_real64, 0.1_real64, 1.0_real64], labels=.true.)
elseif (contours) then ! about ten lines at round values
call ip_contour('##isotherms', field, 0.0_real64, flx, 0.0_real64, fly, nlevels=10_int32, &
color=[0.1_real64, 0.1_real64, 0.1_real64, 1.0_real64], labels=.true.)
endif
if (ip_drag_line_x(1_int32, probe_x, thickness=2.0_real64)) probe_x = min(flx, max(0.0_real64, probe_x))
if (ip_drag_point(2_int32, point(1), point(2), radius=6.0_real64)) then
point = [min(flx, max(0.0_real64, point(1))), min(fly, max(0.0_real64, point(2)))]
endif
i = 1_int32 + nint(point(1) / dx, int32)
j = 1_int32 + nint(point(2) / dy, int32)
write(text, '(F6.2,A)') field(i, j), ' C'
call ip_annotation(point(1), point(2), trim(adjustl(text)), color=[1.0_real64, 1.0_real64, 0.9_real64, 0.9_real64], &
offset=[14.0_real64, -14.0_real64])
call ip_end_plot()
endif
call ig_same_line()
if (bands > 0_int32) then ! the bands, their edges labelled
call ip_colormap_legend('legend', cmap, tmin, tmax, levels=bands, height=-1.0_real64, label_format='%.0f')
else
call ip_colormap_legend('legend', cmap, tmin, tmax, height=-1.0_real64, label_format='%.0f')
endifip_heatmap(label, field, xmin, xmax, ymin, ymax, scale_min=, scale_max=, colormap=)drawsfield(i, j)as coloured cells, read in place,j = 1at the bottom (Heatmaps). Its bounds are the cells' outer edges: half a spacing beyond the first and last nodes, so that each cell is centred on its node.equal=.true.keeps the plate's proportions.ip_contour(label, field, x0, x1, y0, y1, ...)draws contour lines of the same field, read in place: about ten at round values (nlevels), or, with bands, at the bands' edges (levels=).labels=.true.writes each line's value on it (Contour lines). A label starting with##keeps the lines out of the plot's legend.ip_drag_line_x(id, x)is a vertical line the user drags;xchanges in place and the function is.true.while it moves, so the studio clamps it to the plate.ip_drag_point(id, x, y)is the same for a point, andip_annotation(x, y, text)labels it with the temperature of the nearest node (Error bars, annotations and drag tools).ip_colormap_legend(id, colormap, vmin, vmax)is the colour-scale legend beside the plot, continuous or, withlevels, in the same bands with their edges labelled (A colour-scale legend).
The profile along the probe line
i = 1_int32 + nint(probe_x / (flx / (size(field, 1) - 1)), int32)
i0 = max(1_int32, i - 2_int32)
i1 = min(size(field, 1, kind=int32), i + 2_int32)
allocate(y(0), mean(0), below(0), above(0))
do j = 1_int32, size(field, 2, kind=int32), 4_int32
k = size(y) + 1_int32
y = [y, (j - 1) * dy]
mean = [mean, sum(field(i0:i1, j)) / (i1 - i0 + 1)]
below = [below, mean(k) - minval(field(i0:i1, j))]
above = [above, maxval(field(i0:i1, j)) - mean(k)]
enddo
write(title, '(A,F5.3,A)') 'Along x = ', probe_x, ' m###profile' ! ### : the title changes, the id stays
if (ip_begin_plot(trim(title), width=-1.0_real64, height=-1.0_real64)) then
call ip_setup_axes('y [m]', 'T [C]', auto_fit=.true.)
call ip_line('T', field, i, xscale=dy) ! the column i of the field, read in place
call ip_error_bars('5 columns: mean, least, greatest', y, mean, below, above, spec=ip_spec(size=5.0_real64))
call ip_end_plot()
endifip_line('T', field, i, xscale=dy)plots the columniof the field,field(i, :), against y, read in place: no copy of the column is made (Data in place).ip_error_bars(label, y, mean, below, above)draws asymmetric error bars: at every fourth node, the mean of the five columns around the probe line, down to their least and up to their greatest value. Close to the hot side the bars are long: the temperature changes fast across the line.'Along x = ... m###profile': the text after###is the plot's id, so the title changes as the line moves while the plot keeps its state (its zoom, its legend).
What you learned
A field read from the solver's file and shown as a heatmap in a perceptually uniform colormap, continuous or in bands, with its legend; contour lines; a probe line and a probe point the user drags; a profile read in place from a column of the field, with asymmetric error bars. Reference: Heatmaps, Colormaps, Plots, Contour lines.
Run it: build it as chapter 7 shows (with tut12_heat beside it), then ./tut13_field --examples /tmp/studio --case /tmp/studio/plate.
Next: 14. The field in 3-D: the temperature as a surface, the camera, a cut, picking.