subroutine box_behnken ( dim_num, x_num, range, x ) !*****************************************************************************80 ! !! BOX_BEHNKEN returns a Box-Behnken design for the given number of factors. ! ! Modified: ! ! 30 July 2005 ! ! Author: ! ! John Burkardt ! ! Reference: ! ! George Box, Donald Behnken, ! Some new three level designs for the study of quantitative variables, ! Technometrics, ! Volume 2, pages 455-475, 1960. ! ! Parameters: ! ! Input, integer ( kind = 4 ) DIM_NUM, the spatial dimension. ! ! Input, integer ( kind = 4 ) X_NUM, the number of elements of the design. ! X_NUM should be equal to DIM_NUM * 2**(DIM_NUM-1) + 1. ! ! Input, real ( kind = 8 ) RANGE(DIM_NUM,2), the minimum and maximum ! value for each component. ! ! Output, real ( kind = 8 ) X(DIM_NUM,X_NUM), the elements of the design. ! implicit none integer ( kind = 4 ) dim_num integer ( kind = 4 ) x_num integer ( kind = 4 ) i integer ( kind = 4 ) i2 integer ( kind = 4 ) j integer ( kind = 4 ) last_low real ( kind = 8 ) range(dim_num,2) real ( kind = 8 ) x(dim_num,x_num) ! ! Ensure that the range is legal. ! if ( any ( range(1:dim_num,2) <= range(1:dim_num,1) ) ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'BOX_BEHNKEN - Fatal error!' write ( *, '(a)' ) ' For some index I,' write ( *, '(a)' ) ' RANGE(I,2) <= RANGE(I,1).' stop end if ! ! The first point is the center. ! j = 1 x(1:dim_num,j) = ( range(1:dim_num,1) + range(1:dim_num,2) ) / 2.0D+00 ! ! For subsequent elements, one entry is fixed at the middle of the range. ! The others are set to either extreme. ! do i = 1, dim_num j = j + 1 x(1:dim_num,j) = range(1:dim_num,1) x(i,j) = ( range(i,1) + range(i,2) ) / 2.0D+00 ! ! The next element is made by finding the last low value, making it ! high, and all subsequent high values low. ! do last_low = -1 do i2 = 1, dim_num if ( x(i2,j) == range(i2,1) ) then last_low = i2 end if end do if ( last_low == -1 ) then exit end if j = j + 1 x(1:dim_num,j) = x(1:dim_num,j-1) x(last_low,j) = range(last_low,2) do i2 = last_low + 1, dim_num if ( x(i2,j) == range(i2,2) ) then x(i2,j) = range(i2,1) end if end do end do end do return end subroutine box_behnken_size ( dim_num, x_num ) !*****************************************************************************80 ! !! BOX_BEHNKEN_SIZE returns the size of a Box-Behnken design. ! ! Modified: ! ! 29 July 2005 ! ! Author: ! ! John Burkardt ! ! Reference: ! ! George Box, Donald Behnken, ! Some new three level designs for the study of quantitative variables, ! Technometrics, ! Volume 2, pages 455-475, 1960. ! ! Parameters: ! ! Input, integer ( kind = 4 ) DIM_NUM, the spatial dimension. ! ! Output, integer ( kind = 4 ) X_NUM, the number of elements of the design. ! X_NUM will be equal to DIM_NUM * 2**(DIM_NUM-1) + 1. ! implicit none integer ( kind = 4 ) dim_num integer ( kind = 4 ) x_num if ( 1 <= dim_num ) then x_num = 1 + dim_num * 2**( dim_num - 1 ) else x_num = -1 end if return end subroutine box_behnken_write ( dim_num, x_num, range, x, file_out_name ) !*****************************************************************************80 ! !! BOX_BEHNKEN_WRITE writes a Box-Behnken dataset to a file. ! ! Discussion: ! ! The initial lines of the file are comments, which begin with a ! '#' character. ! ! Thereafter, each line of the file contains the DIM_NUM-dimensional ! components of the next entry of the dataset. ! ! Modified: ! ! 30 July 2005 ! ! Author: ! ! John Burkardt ! ! Reference: ! ! George Box, Donald Behnken, ! Some new three level designs for the study of quantitative variables, ! Technometrics, ! Volume 2, pages 455-475, 1960. ! ! Parameters: ! ! Input, integer ( kind = 4 ) DIM_NUM, the spatial dimension. ! ! Input, integer ( kind = 4 ) X_NUM, the number of elements in the subsequence. ! ! Input, real RANGE(DIM_NUM,2), the minimum and maximum value of ! each component. ! ! Input, real ( kind = 8 ) X(NDIM,N), the points. ! ! Input, character ( len = * ) FILE_OUT_NAME, the output file name. ! implicit none integer ( kind = 4 ) dim_num integer ( kind = 4 ) x_num character ( len = * ) file_out_name integer ( kind = 4 ) file_out_unit integer ( kind = 4 ) i integer ( kind = 4 ) ios integer ( kind = 4 ) j real ( kind = 8 ) range(dim_num,2) character ( len = 40 ) string real ( kind = 8 ) x(dim_num,x_num) call get_unit ( file_out_unit ) open ( unit = file_out_unit, file = file_out_name, status = 'replace', & iostat = ios ) if ( ios /= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'BOX_BEHNKEN_WRITE - Fatal error!' write ( *, '(a)' ) ' Could not open the output file:' write ( *, '(a)' ) ' "' // trim ( file_out_name ) // '".' stop end if call timestring ( string ) write ( file_out_unit, '(a)' ) '# ' // trim ( file_out_name ) write ( file_out_unit, '(a)' ) '# created by BOX_BEHNKEN.F90' write ( file_out_unit, '(a)' ) '#' write ( file_out_unit, '(a)' ) '# File generated on ' & // trim ( string ) write ( file_out_unit, '(a)' ) '#' write ( file_out_unit, '(a,i12)' ) '# DIM_NUM = ', dim_num write ( file_out_unit, '(a,i12)' ) '# X_NUM = ', x_num write ( file_out_unit, '(a)' ) '# RANGE:' do i = 1, dim_num, 5 write ( file_out_unit, '(a,f10.6,2x,f10.6)' ) '# ', range(i,1:2) end do write ( file_out_unit, '(a,g14.6)' ) '# EPSILON (unit roundoff ) = ', & epsilon ( x(1,1) ) write ( file_out_unit, '(a)' ) '#' write ( string, '(a,i3,a)' ) '(', dim_num, '(2x,f10.6))' do j = 1, x_num write ( file_out_unit, string ) x(1:dim_num,j) end do close ( unit = file_out_unit ) return end subroutine get_unit ( iunit ) !*****************************************************************************80 ! !! GET_UNIT returns a free FORTRAN unit number. ! ! Discussion: ! ! A "free" FORTRAN unit number is an integer ( kind = 4 ) 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 an integer ( kind = 4 ) 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. ! ! Modified: ! ! 18 September 2005 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Output, integer ( kind = 4 ) IUNIT, the free unit number. ! implicit none integer ( kind = 4 ) i integer ( kind = 4 ) ios integer ( kind = 4 ) 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 subroutine r8mat_transpose_print ( m, n, a, title ) !*****************************************************************************80 ! !! R8MAT_TRANSPOSE_PRINT prints an R8MAT, transposed. ! ! Modified: ! ! 14 June 2004 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer ( kind = 4 ) M, N, the number of rows and columns. ! ! Input, real ( kind = 8 ) A(M,N), an M by N matrix to be printed. ! ! Input, character ( len = * ) TITLE, an optional title. ! implicit none integer ( kind = 4 ) m integer ( kind = 4 ) n real ( kind = 8 ) a(m,n) character ( len = * ) title call r8mat_transpose_print_some ( m, n, a, 1, 1, m, n, title ) return end subroutine r8mat_transpose_print_some ( m, n, a, ilo, jlo, ihi, jhi, title ) !*****************************************************************************80 ! !! R8MAT_TRANSPOSE_PRINT_SOME prints some of an R8MAT, transposed. ! ! Modified: ! ! 14 June 2004 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer ( kind = 4 ) M, N, the number of rows and columns. ! ! Input, real ( kind = 8 ) A(M,N), an M by N matrix to be printed. ! ! Input, integer ( kind = 4 ) ILO, JLO, the first row and column to print. ! ! Input, integer ( kind = 4 ) IHI, JHI, the last row and column to print. ! ! Input, character ( len = * ) TITLE, an optional title. ! implicit none integer ( kind = 4 ), parameter :: incx = 5 integer ( kind = 4 ) m integer ( kind = 4 ) n real ( kind = 8 ) a(m,n) character ( len = 14 ) ctemp(incx) integer ( kind = 4 ) i integer ( kind = 4 ) i2 integer ( kind = 4 ) i2hi integer ( kind = 4 ) i2lo integer ( kind = 4 ) ihi integer ( kind = 4 ) ilo integer ( kind = 4 ) inc integer ( kind = 4 ) j integer ( kind = 4 ) j2hi integer ( kind = 4 ) j2lo integer ( kind = 4 ) jhi integer ( kind = 4 ) jlo character ( len = * ) title if ( 0 < len_trim ( title ) ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) trim ( title ) end if do i2lo = max ( ilo, 1 ), min ( ihi, m ), incx i2hi = i2lo + incx - 1 i2hi = min ( i2hi, m ) i2hi = min ( i2hi, ihi ) inc = i2hi + 1 - i2lo write ( *, '(a)' ) ' ' do i = i2lo, i2hi i2 = i + 1 - i2lo write ( ctemp(i2), '(i7,7x)') i end do write ( *, '('' Row '',5a14)' ) ctemp(1:inc) write ( *, '(a)' ) ' Col' write ( *, '(a)' ) ' ' j2lo = max ( jlo, 1 ) j2hi = min ( jhi, n ) do j = j2lo, j2hi do i2 = 1, inc i = i2lo - 1 + i2 write ( ctemp(i2), '(g14.6)' ) a(i,j) end do write ( *, '(i5,1x,5a14)' ) j, ( ctemp(i), i = 1, inc ) end do end do write ( *, '(a)' ) ' ' return end subroutine timestamp ( ) !*****************************************************************************80 ! !! TIMESTAMP prints the current YMDHMS date as a time stamp. ! ! Example: ! ! May 31 2001 9:45:54.872 AM ! ! Modified: ! ! 26 February 2005 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! None ! implicit none character ( len = 8 ) ampm integer ( kind = 4 ) d integer ( kind = 4 ) h integer ( kind = 4 ) m integer ( kind = 4 ) mm character ( len = 9 ), parameter, dimension(12) :: month = (/ & 'January ', 'February ', 'March ', 'April ', & 'May ', 'June ', 'July ', 'August ', & 'September', 'October ', 'November ', 'December ' /) integer ( kind = 4 ) n integer ( kind = 4 ) s integer ( kind = 4 ) values(8) integer ( kind = 4 ) y call date_and_time ( values = values ) y = values(1) m = values(2) d = values(3) h = values(5) n = values(6) s = values(7) mm = values(8) if ( h < 12 ) then ampm = 'AM' else if ( h == 12 ) then if ( n == 0 .and. s == 0 ) then ampm = 'Noon' else ampm = 'PM' end if else h = h - 12 if ( h < 12 ) then ampm = 'PM' else if ( h == 12 ) then if ( n == 0 .and. s == 0 ) then ampm = 'Midnight' else ampm = 'AM' end if end if end if write ( *, '(a,1x,i2,1x,i4,2x,i2,a1,i2.2,a1,i2.2,a1,i3.3,1x,a)' ) & trim ( month(m) ), d, y, h, ':', n, ':', s, '.', mm, trim ( ampm ) return end subroutine timestring ( string ) !*****************************************************************************80 ! !! TIMESTRING writes the current YMDHMS date into a string. ! ! Example: ! ! STRING = 'May 31 2001 9:45:54.872 AM' ! ! Modified: ! ! 26 February 2005 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Output, character ( len = * ) STRING, contains the date information. ! A character length of 40 should always be sufficient. ! implicit none character ( len = 8 ) ampm integer ( kind = 4 ) d integer ( kind = 4 ) h integer ( kind = 4 ) m integer ( kind = 4 ) mm character ( len = 9 ), parameter, dimension(12) :: month = (/ & 'January ', 'February ', 'March ', 'April ', & 'May ', 'June ', 'July ', 'August ', & 'September', 'October ', 'November ', 'December ' /) integer ( kind = 4 ) n integer ( kind = 4 ) s character ( len = * ) string integer ( kind = 4 ) values(8) integer ( kind = 4 ) y call date_and_time ( values = values ) y = values(1) m = values(2) d = values(3) h = values(5) n = values(6) s = values(7) mm = values(8) if ( h < 12 ) then ampm = 'AM' else if ( h == 12 ) then if ( n == 0 .and. s == 0 ) then ampm = 'Noon' else ampm = 'PM' end if else h = h - 12 if ( h < 12 ) then ampm = 'PM' else if ( h == 12 ) then if ( n == 0 .and. s == 0 ) then ampm = 'Midnight' else ampm = 'AM' end if end if end if write ( string, '(a,1x,i2,1x,i4,2x,i2,a1,i2.2,a1,i2.2,a1,i3.3,1x,a)' ) & trim ( month(m) ), d, y, h, ':', n, ':', s, '.', mm, trim ( ampm ) return end