program main !*****************************************************************************80 ! !! grid_surface() draws a surface from a table Z(X,Y). ! ! Discussion: ! ! Here, the grid is a 41x41 array of equally spaced points in [-2,2]x[-2,2]. ! ! and Z(X,Y) is a function which can be evaluated by: ! ! Z = exp ( - ( X^2 + Y^2 ) ) * cos ( 0.25 * X ) ! * sin ( Y ) * cos ( 2 * (X^2 + Y^2 ) ) ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 16 April 2020 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer, parameter :: m = 41 integer, parameter :: n = 41 character ( len = 255 ) command_filename integer command_unit character ( len = 255 ) data_filename integer data_unit character ( len = 12 ) :: header = 'grid_surface' integer i integer j real ( kind = rk ) r2 real ( kind = rk ) x real ( kind = rk ) y real ( kind = rk ) z write ( *, '(a)' ) '' write ( *, '(a)' ) 'grid_surface():' write ( *, '(a)' ) ' Fortran90 version' write ( *, '(a)' ) ' Make a surface plot Z(X,Y) for a 41x41 table.' ! ! Create the data file. ! call get_unit ( data_unit ) data_filename = header // '_data.txt' open ( unit = data_unit, file = data_filename, status = 'replace' ) do i = 1, m if ( 1 < i ) then write ( data_unit, '(a)' ) end if x = ( real ( m - i, kind = rk ) * ( -2.0 ) & + real ( i - 1, kind = rk ) * ( 2.0 ) ) & / real ( m - 1, kind = rk ) do j = 1, n y = ( real ( n - j, kind = rk ) * ( -2.0 ) & + real ( j - 1, kind = rk ) * ( 2.0 ) ) & / real ( n - 1, kind = rk ) r2 = x**2 + y**2 z = exp ( - r2 ) * cos ( 0.25 * x ) * sin ( y ) * cos ( 2 * r2 ) write ( data_unit, '(3(2x,g14.6))' ) x, y, z end do end do close ( unit = data_unit ) write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' grid_surface: data stored in "' & // trim ( data_filename ) // '".' ! ! Create the command file. ! call get_unit ( command_unit ) command_filename = trim ( header ) // '_commands.txt' open ( unit = command_unit, file = command_filename, status = 'replace' ) write ( command_unit, '(a)' ) '# ' // trim ( command_filename ) write ( command_unit, '(a)' ) '#' write ( command_unit, '(a)' ) '# Usage:' write ( command_unit, '(a)' ) '# gnuplot < ' // trim ( command_filename ) write ( command_unit, '(a)' ) '#' write ( command_unit, '(a)' ) 'set term png' write ( command_unit, '(a)' ) 'set output "' // trim ( header ) // '.png"' write ( command_unit, '(a)' ) 'set xlabel "X"' write ( command_unit, '(a)' ) 'set ylabel "Y"' write ( command_unit, '(a)' ) 'set zlabel "Z"' write ( command_unit, '(a)' ) 'set title "Grid Surface"' write ( command_unit, '(a)' ) 'set view 77, 77' write ( command_unit, '(a)' ) 'set xyplane -0.5' write ( command_unit, '(a)' ) 'set hidden3d' write ( command_unit, '(a)' ) 'set timestamp' write ( command_unit, '(a)' ) 'set grid' write ( command_unit, '(a)' ) 'set style data lines' write ( command_unit, '(a)' ) 'splot "' // trim ( data_filename ) // & '" with lines' write ( command_unit, '(a)' ) 'quit' close ( unit = command_unit ) write ( *, '(a)' ) ' grid_surface: plot commands stored in "' & // trim ( command_filename ) // '".' ! ! Terminate. ! write ( *, '(a)' ) '' write ( *, '(a)' ) 'grid_surface():' write ( *, '(a)' ) ' Normal end of execution.' stop ( 0 ) end subroutine get_unit ( iunit ) !*****************************************************************************80 ! !! GET_UNIT returns a free FORTRAN unit number. ! ! Discussion: ! ! A "free" FORTRAN unit number is a value between 1 and 99 which ! is not currently associated with an I/O device. A free FORTRAN unit ! number is needed in order to open a file with the OPEN command. ! ! If IUNIT = 0, then no free FORTRAN unit could be found, although ! all 99 units were checked (except for units 5, 6 and 9, which ! are commonly reserved for console I/O). ! ! Otherwise, IUNIT is a value between 1 and 99, representing a ! free FORTRAN unit. Note that GET_UNIT assumes that units 5 and 6 ! are special, and will never return those values. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 26 October 2008 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Output, integer IUNIT, the free unit number. ! implicit none integer i integer ios integer iunit logical lopen iunit = 0 do i = 1, 99 if ( i /= 5 .and. i /= 6 .and. i /= 9 ) then inquire ( unit = i, opened = lopen, iostat = ios ) if ( ios == 0 ) then if ( .not. lopen ) then iunit = i return end if end if end if end do return end