0

For "flavorful" output during iteration procedure, I'd like to use following approach (I saw it for some program written in C++) in Fortran. Every new iteration gives me the line in the console with corresponding information:

Iteration XX Accuracy X.XXXXE-XX Time spent XX seconds

I want every new line to replace the previous one (i.e. labels "Iteration" "Accuracy" ... etc. remain unchanged, while values change).

I tried different variants including

backspace(6)
backspace(0)
write (*,*) char(8)//'Accuracy ...'

Of course, first two lines give a runtime error and the last gives nothing.

How can I implement such an approach?

francescalus
  • 30,576
  • 16
  • 61
  • 96
MuKeP
  • 65
  • 1
  • 5
  • As @francescalus correctly noted, the solution is given on reference link. But the simplest is not the one chosen. The one given by @tony rollett is more convinient. `write (unit,'(A,i,A\)') char(13)//'Iteration ',iter,' Accuracy ',accur`. – MuKeP Dec 08 '17 at 11:05

1 Answers1

1

It's perhaps a bit rather complicated solution, but one option would be to use the ncurses library - http://genepi.qimr.edu.au/staff/davidD/

Below is a modification of the program testcurs.f90available for download from the linked page. It just displays a progress message which is being progressively updated. In order to test it, you will need also the ncurses.f90 module and then link with -lncurses, i.e.,

gfortran -c ncurses.f95
gfortran -o test testcurs.f95 -lncurses

The example (showing just the program itself, testcurs.f90 contains also the definition of initTest etc.):

program testcurses
  use curses
  use commands
  type (C_PTR) :: iwin = C_NULL_PTR
  integer (C_INT) :: key
  integer :: istat, new_option=1, old_option=0
  CHARACTER(LEN=1024) :: msg

  call initTest(iwin, istat)

  if (istat /= 0) then
    write(*,'(a)') 'ERROR: initscr failed!'
    stop
  end if

  ierr = wbkgd(iwin, curses_a_reverse)
  ierr = erase()
  ierr = attrset(curses_a_bold)
  DO i = 10, 100, 10
    WRITE(msg, '(''Progress '', I0, ''%'')') i

    ierr = mvaddstr(20, 20, TRIM(msg) // C_NULL_CHAR)
    ierr = refresh()
    CALL SLEEP(1)
  END DO
  key = getch()

  ierr = delwin(iwin)
  ierr = endwin()
end program testcurses
ewcz
  • 12,819
  • 1
  • 25
  • 47