22. A sketch and a monitor
Two pictures the studio draws itself. The Boundaries form starts with a sketch of the plate, its sides coloured and named by their condition, that the user clicks; and the run's plot becomes a monitor: the residual and a quantity of the solution on two axes, the values under the pointer, and a window of iterations whose mean is read on the axis.
What this chapter adds to chapter 21's program, tut21_tables.F90:
draw_monitor: The run's monitordraw_sketch: The plate on a canvas, its sides coloured and named by their condition, the body as a discside_label: A side's name and condition, as the sketch writes itsegment_distance: The distance from pointpto the segment of endse(x, y, x, y) [pixels]- changed:
draw_form,draw_run,follow_run,self_test,start_run
The whole program: tut22_monitor.F90, chapter 21's studio with the changes below; its solver is tut22_heat.F90, chapter 12's with one more number on each line, the plate's mean temperature:
write(output_unit, '(A,I6,A,ES9.3,A,F8.3)') 'iteration ', it, ' residual ', residual, ' mean ', mean
flush(output_unit) ! the studio reads each line as it is printed
call wait(pace)The monitor
if (ip_begin_plot('Monitor', width=-1.0_real64, height=-1.0_real64)) then
call ip_setup_axis('x1', label='iteration', auto_fit=solver%running()) ! follows the run while it runs
call ip_setup_axis('y1', label='residual', scale='log', auto_fit=.true.) ! each y axis fits its own items
call ip_setup_axis('y2', label='mean T [C]', auto_fit=.true.) ! on the right: the second axis
call ip_setup_legend(location='n', horizontal=.true.)
call ip_set_axes('x1', 'y1')
call ip_line('residual', steps, res) ! integer abscissae: converted by FIGURE, the ordinates in place
call ip_set_axes('x1', 'y2') ! the next items on the second y axis
call ip_line('mean T', steps, means)- Two y axes.
ip_setup_axis('y2', ...)turns on the second y axis, on the right;ip_set_axes('x1', 'y2')sends the next items to it (Axes). Each axis fits its own items: the residual over several decades on a log scale, the temperature in degrees on a linear one.auto_fitfollows the data every frame (here the x axis only while the run goes on, so that the user can zoom a finished run). - Integer abscissae.
stepsholds the iteration numbers asinteger(int32); FIGURE converts them for ImPlot, which readsxandyof one type, and reads the ordinates in place (Data in place). - The legend is above the plot area, its entries in a row (
ip_setup_legend).
What is under the pointer
readout = ''
if (ip_plot_hovered()) then ! the pointer over the plot area
call ip_plot_mouse(x, y, xaxis='x1', yaxis='y1') ! where it is, in iterations and residual
k = minloc(abs(steps - x), dim=1) ! the nearest iteration printed
write(readout, '(A,I0,A,ES9.2,A,A,A)') 'iteration ', steps(k), ': residual ', res(k), ', mean ', &
num(means(k)), ' C'
call ip_tag('x1', real(steps(k), real64), text=trim(readout(11:index(readout, ':') - 1))) ! on the x axis
call ip_set_axes('x1', 'y1') ! the text at the pointer, in the axes it was read in
call ip_text(trim(readout), x, y, offset=[12.0_real64, -12.0_real64])
endifip_plot_hovered() is true while the pointer is over the plot area; ip_plot_mouse(x, y, xaxis, yaxis) gives where it is in the units of the axes named: iterations and residual here (Reading a plot). The studio finds the nearest iteration printed, tags it on the x axis (ip_tag, a value in a box on the axis) and writes the values beside the pointer (ip_text, at a point of the plot, offset in pixels). Both are drawn in the axes current when they are called: the text goes back to y1, where y was read.
An averaging window
if (ip_drag_line_x(1_int32, window(1), thickness=2.0_real64)) window_moved = .true. ! the window's two ends,
if (ip_drag_line_x(2_int32, window(2), thickness=2.0_real64)) window_moved = .true. ! updated in place
window = [minval(window), maxval(window)]
inside = any(steps >= window(1) .and. steps <= window(2))
if (inside) then
window_mean = sum(means, mask=steps >= window(1) .and. steps <= window(2)) / &
count(steps >= window(1) .and. steps <= window(2))
call ip_tag('y2', window_mean, text='mean '//num(window_mean)) ! on the axis, in the last item's colour
endifThe window's two ends are drag lines, ip_drag_line_x(id, x), which update the variables in place and return .true. while dragged (Marks in a plot). Until the user moves them they span the whole run. The mean of the temperature over the window is tagged on the second axis, in the colour of the last item drawn, the mean temperature's line. A monitor of a real solver averages a lift coefficient this way over its last periods.
A sketch on a canvas
call ig_canvas('sketch', -1.0_real64, ig_em(9), ev) ! the width of the form, 9 em high
s = min((ev%size(1) - ig_em(10)) / inp%lx, (ev%size(2) - ig_em(3)) / inp%ly) ! the plate fits, labels around it
x0 = ev%origin(1) + ig_em(5)
y0 = ev%origin(2) + ev%size(2) - ig_em(1.5_real64) ! y up on the plate, down on the screen
x1 = x0 + s * inp%lx
y1 = y0 - s * inp%ly
ends(:, 1) = [x0, y0, x0, y1] ! west, east, south, north
ends(:, 2) = [x1, y0, x1, y1]
ends(:, 3) = [x0, y0, x1, y0]
ends(:, 4) = [x0, y1, x1, y1]
side_hover = 0_int32
if (ev%hovered) then ! hit testing is the program's: the side nearest the pointer
do k = 1_int32, 4_int32
d = segment_distance(ev%origin + ev%mouse, ends(:, k))
if (d < 6.0_real64) side_hover = k
enddo
endif
if (ev%clicked(1)) side_sel = side_hover ! a click on the plate's inside clears the choice
call app%palette_color('text', text_rgb)
call ig_draw_rect(x0, y0, x1, y1, [0.35_real64, 0.33_real64, 0.30_real64], filled=.true.)
call ig_draw_circle(x0 + s * inp%body_x, y0 - s * inp%body_y, s * inp%body_r, [0.75_real64, 0.75_real64, &
0.78_real64], filled=.true.)
do k = 1_int32, 4_int32
call ig_draw_line(ends(1, k), ends(2, k), ends(3, k), ends(4, k), &
merge(WARM, COOL, inp%bc_kind(k) == KINDS(1)), &
thickness=merge(5.0_real64, 2.0_real64, k == side_hover .or. k == side_sel))
enddo
call ig_draw_text(x0 - ig_em(4.5_real64), 0.5_real64 * (y0 + y1), side_label(1), text_rgb) ! the words carry
call ig_draw_text(x1 + ig_em(0.5_real64), 0.5_real64 * (y0 + y1), side_label(2), text_rgb) ! the meaning, not
call ig_draw_text(0.5_real64 * (x0 + x1), y0 + ig_em(0.2_real64), side_label(3), text_rgb) ! the colour alone
call ig_draw_text(0.5_real64 * (x0 + x1), y1 - ig_em(1.2_real64), side_label(4), text_rgb)ig_canvas(id, width, height, ev)places an area that takes the mouse;evtells what happened over it on this frame:hovered,clicked(1), the pointermousefrom its top-left corner, itsoriginon the screen and itssize(Canvas). A width of 0 or below is the space left plus it: -1, as here, is all of the form's width but one pixel.- The primitives draw in screen pixels: the program maps the plate's metres to pixels itself, y up on the plate and down on the screen.
- Hit testing is the program's: the canvas reports the pointer, and
segment_distancefinds the side within 6 pixels of it. That side is drawn thicker; a click chooses it, and the table shades its row. - Colour is never the only sign: each side is labelled with its name and its condition,
west 100.0 C,east insulated.
The test
call test_mouse_move(monitor_at(1), monitor_at(2)) ! the pointer over the monitor
call frames(4_int32)
call check(index(readout, 'iteration ') == 1, 'the pointer over the monitor reads '//trim(readout))
call check(abs(window_mean - sum(means) / size(means)) < 1.0e-9_real64, &
'the averaging window spans the run: its mean is the mean of every iteration')$ ./tut22_monitor --test tut22-cases
[ok] the title bar holds the studio's menus
[ok] the title drags the window
[ok] ctrl+o shows the file browser on the example cases
[ok] Choose opens the case, the window's title says which
[ok] escape leaves the title as it was, unedited
[ok] enter commits the title, one undo step, and the window's title marks it unsaved
[ok] a drop asks before discarding the edit
[ok] Discard opens the dropped case
[ok] F5 runs the solver to convergence
[ok] the task reads the field the solver wrote
[ok] right moves the probe line while Result has the focus
[ok] and not while the title field is edited
[ok] nor while another panel has the focus
[ok] the pointer over the monitor reads iteration 111: residual 3.91E-05, mean 20.615 C
[ok] the averaging window spans the run: its mean is the mean of every iteration
[ok] two runs in the table, sorted by iterations on a click of its header
[ok] a second click reverses the order
[ok] a right click on a run opens its menu
[ok] a click on a problem shows the Body section, the radius in keys
[ok] a click on the sketch's east side chooses it
[ok] an input with the body out of the plate is refused
[ok] and accepted once corrected
[ok] larger text: one em is 19 pixels, the bar 5 em
[ok] a stray ig_end_tab is reported by the next frame, naming the call
ig_end_tab: no ig_begin_tab is open (no scope is open)
[ok] no other frame of the test had a usage error
[ok] the title bar's close button ends the studio
26 of 26 checks passed(The ig_end_tab line is the report of chapter 19's deliberate mistake.)
What you learned
A second y axis and items sent to it; integer abscissae; the pointer read in plot units, tagged on an axis and written beside it; drag lines as an averaging window; a canvas with drawing primitives and the program's own hit testing. Reference: Axes, Reading a plot, Marks in a plot, Canvas.
Run it: build it as chapter 7 shows (with tut22_heat beside it), then ./tut22_monitor --examples /tmp/studio --case /tmp/studio/plate.
Next: 23. The field and its snapshots: the field coloured through a colormap in one call, a legend whose range the user drags, and a player over the snapshots the solver writes.