program main !*****************************************************************************80 ! !! stiff_exact_test() tests stiff_exact(). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 23 June 2025 ! ! Author: ! ! John Burkardt ! implicit none integer n call timestamp ( ) write ( *, '(a)' ) '' write ( *, '(a)' ) 'stiff_exact_test():' write ( *, '(a)' ) ' Fortran90 version' write ( *, '(a)' ) ' Test stiff_exact(), which evaluates exact solutions' write ( *, '(a)' ) ' of a stiff ODE.' call stiff_value ( ) n = 27 call stiff_euler_test ( n ) ! ! Terminate. ! write ( *, '(a)' ) '' write ( *, '(a)' ) 'stiff_exact_test():' write ( *, '(a)' ) ' Normal end of execution.' call timestamp ( ) 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 ! ! Output: ! ! integer IUNIT, the free unit number. ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) 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 subroutine r8vec_linspace ( n, a, b, x ) !*****************************************************************************80 ! !! r8vec_linspace() creates a vector of linearly spaced values. ! ! Discussion: ! ! An R8VEC is a vector of R8's. ! ! 4 points evenly spaced between 0 and 12 will yield 0, 4, 8, 12. ! ! In other words, the interval is divided into N-1 even subintervals, ! and the endpoints of intervals are used as the points. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 14 March 2011 ! ! Author: ! ! John Burkardt ! ! Input: ! ! integer N, the number of entries in the vector. ! ! real ( kind = rk8 ) A, B, the first and last entries. ! ! Output: ! ! real ( kind = rk8 ) X(N), a vector of linearly spaced data. ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) integer n real ( kind = rk8 ) a real ( kind = rk8 ) b integer i real ( kind = rk8 ) x(n) if ( n == 1 ) then x(1) = ( a + b ) / 2.0D+00 else do i = 1, n x(i) = ( real ( n - i, kind = rk8 ) * a & + real ( i - 1, kind = rk8 ) * b ) & / real ( n - 1, kind = rk8 ) end do end if return end subroutine stiff_euler_test ( n ) !*****************************************************************************80 ! !! stiff_euler_test() tests stiff_euler(). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 23 June 2025 ! ! Author: ! ! John Burkardt ! ! Input: ! ! integer N: the number of steps to take. ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) integer n integer, parameter :: n2 = 101 real ( kind = rk8 ) lambda real ( kind = rk8 ) t0 real ( kind = rk8 ) t1(n+1) real ( kind = rk8 ) t2(n2) real ( kind = rk8 ) tstop real ( kind = rk8 ) y0 real ( kind = rk8 ) y1(n+1) real ( kind = rk8 ) y2(n2) interface subroutine stiff_parameters ( & lambda_in, t0_in, y0_in, tstop_in, & lambda_out, t0_out, y0_out, tstop_out ) integer, parameter :: rk8 = kind ( 1.0D+00 ) real ( kind = rk8 ), optional :: lambda_in real ( kind = rk8 ), optional :: lambda_out real ( kind = rk8 ), optional :: t0_in real ( kind = rk8 ), optional :: t0_out real ( kind = rk8 ), optional :: y0_in real ( kind = rk8 ), optional :: y0_out real ( kind = rk8 ), optional :: tstop_in real ( kind = rk8 ), optional :: tstop_out end subroutine end interface write ( *, '(a)' ) '' write ( *, '(a)' ) 'stiff_euler_test():' write ( *, '(a)' ) ' Solve stiff ODE using the Euler method.' ! ! Set initial parameter values. ! lambda = 50 t0 = 0.0 y0 = 0.0 tstop = 1.0 call stiff_parameters ( & lambda_in = lambda, t0_in = t0, y0_in = y0, tstop_in = tstop ) write ( *, '(a)' ) '' write ( *, '(a)' ) ' Parameters:' write ( *, '(a,g14.6)' ) ' lambda = ', lambda write ( *, '(a,g14.6)' ) ' t0 = ', t0 write ( *, '(a,g14.6)' ) ' y0 = ', y0 write ( *, '(a,g14.6)' ) ' tstop = ', tstop call stiff_euler ( n, t1, y1 ) call r8vec_linspace ( n2, t0, tstop, t2 ) call stiff_exact ( n2, t2, y2 ) call plot2 ( n+1, t1, y1, n2, t2, y2, 'stiff_euler', & 'Stiff ODE: euler method' ) return end subroutine stiff_euler ( n, t, y ) !*****************************************************************************80 ! !! stiff_euler() uses the Euler method on the stiff ODE. ! ! Discussion: ! ! y' = lambda * ( cos(t-t0) - y ) ! y(t0) = y0 ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 23 June 2025 ! ! Author: ! ! John Burkardt ! ! Input: ! ! integer N: the number of steps to take. ! ! Output: ! ! real ( kind = rk8 ) T(N+1), Y(N+1): the times and estimated solutions. ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) integer n real ( kind = rk8 ) dt integer i real ( kind = rk8 ) lambda real ( kind = rk8 ) t(n+1) real ( kind = rk8 ) t0 real ( kind = rk8 ) tstop real ( kind = rk8 ) y(n+1) real ( kind = rk8 ) y0 interface subroutine stiff_parameters ( & lambda_in, t0_in, y0_in, tstop_in, & lambda_out, t0_out, y0_out, tstop_out ) integer, parameter :: rk8 = kind ( 1.0D+00 ) real ( kind = rk8 ), optional :: lambda_in real ( kind = rk8 ), optional :: lambda_out real ( kind = rk8 ), optional :: t0_in real ( kind = rk8 ), optional :: t0_out real ( kind = rk8 ), optional :: y0_in real ( kind = rk8 ), optional :: y0_out real ( kind = rk8 ), optional :: tstop_in real ( kind = rk8 ), optional :: tstop_out end subroutine end interface call stiff_parameters ( & lambda_out = lambda, t0_out = t0, y0_out = y0, tstop_out = tstop ) dt = ( tstop - t0 ) / real ( n, kind = rk8 ) t(1) = t0 y(1) = y0 do i = 1, n t(i+1) = t(i) + dt y(i+1) = y(i) + dt * lambda * ( cos ( t(i) ) - y(i) ) end do return end subroutine stiff_value ( ) !*****************************************************************************80 ! !! stiff_value() evaluates the exact solution for various initial conditions. ! ! Discussion: ! ! y' = lambda * ( cos(t-t0) - y ) ! y(t0) = y0 ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 23 June 2025 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) integer, parameter :: nt = 101 real ( kind = rk8 ) lambda real ( kind = rk8 ) t0 real ( kind = rk8 ) t1(nt) real ( kind = rk8 ) t2(nt) real ( kind = rk8 ) t3(nt) real ( kind = rk8 ) t4(nt) real ( kind = rk8 ) tstop real ( kind = rk8 ) y0 real ( kind = rk8 ) y1(nt) real ( kind = rk8 ) y2(nt) real ( kind = rk8 ) y3(nt) real ( kind = rk8 ) y4(nt) interface subroutine stiff_parameters ( & lambda_in, t0_in, y0_in, tstop_in, & lambda_out, t0_out, y0_out, tstop_out ) integer, parameter :: rk8 = kind ( 1.0D+00 ) real ( kind = rk8 ), optional :: lambda_in real ( kind = rk8 ), optional :: lambda_out real ( kind = rk8 ), optional :: t0_in real ( kind = rk8 ), optional :: t0_out real ( kind = rk8 ), optional :: y0_in real ( kind = rk8 ), optional :: y0_out real ( kind = rk8 ), optional :: tstop_in real ( kind = rk8 ), optional :: tstop_out end subroutine end interface ! ! Set initial parameter values. ! lambda = 50.0 t0 = 0.0 y0 = 0.0 tstop = 1.0 call stiff_parameters ( & lambda_in = lambda, t0_in = t0, y0_in = y0, tstop_in = tstop ) write ( *, '(a)' ) '' write ( *, '(a)' ) ' Parameters:' write ( *, '(a,g14.6)' ) ' lambda = ', lambda write ( *, '(a,g14.6)' ) ' t0 = ', t0 write ( *, '(a,g14.6)' ) ' y0 = ', y0 write ( *, '(a,g14.6)' ) ' tstop = ', tstop ! ! Default with t0 = 0, y0 = 0. ! t0 = 0.0 y0 = 0.0 call stiff_parameters ( & lambda_in = lambda, t0_in = t0, y0_in = y0, tstop_in = tstop ) call r8vec_linspace ( nt, t0, tstop, t1 ) call stiff_exact ( nt, t1, y1 ) ! ! Try t0=0, y0=0.9. ! t0 = 0.0 y0 = 0.9 call stiff_parameters ( & lambda_in = lambda, t0_in = t0, y0_in = y0, tstop_in = tstop ) call r8vec_linspace ( nt, t0, tstop, t2 ) call stiff_exact ( nt, t2, y2 ) ! ! Try t0=0, y0=1.5. ! t0 = 0.0 y0 = 1.5 call stiff_parameters ( & lambda_in = lambda, t0_in = t0, y0_in = y0, tstop_in = tstop ) call r8vec_linspace ( nt, t0, tstop, t3 ) call stiff_exact ( nt, t3, y3 ) ! ! t0 = 0.25, y0 = 0.5 ! t0 = 0.25 y0 = 0.5 call stiff_parameters ( & lambda_in = lambda, t0_in = t0, y0_in = y0, tstop_in = tstop ) call r8vec_linspace ( nt, t0, tstop, t4 ) call stiff_exact ( nt, t4, y4 ) ! ! Plot them. ! call plot4 ( nt, t1, y1, nt, t2, y2, nt, t3, y3, nt, t4, y4, & 'stiff_value', 'Stiff ODE exact solutions' ) return end subroutine plot2 ( n1, t1, y1, n2, t2, y2, header, title ) !*****************************************************************************80 ! !! plot2() plots two curves together. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 23 June 2025 ! ! Author: ! ! John Burkardt ! ! Input: ! ! integer N1: the size of the first data set. ! ! real ( kind = rk8 ) T1(N1), Y1(N1), the first dataset. ! ! integer N2: the size of the second data set. ! ! real ( kind = rk8 ) T2(N2), Y2(N2), the second dataset. ! ! character ( len = * ) HEADER: an identifier for the data. ! ! character ( len = * ) TITLE: a title to appear in the plot. ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) integer n1 integer n2 character ( len = 255 ) command_filename integer command_unit character ( len = 255 ) data1_filename character ( len = 255 ) data2_filename integer data_unit character ( len = * ) header integer i character ( len = * ) title real ( kind = rk8 ) t1(n1) real ( kind = rk8 ) t2(n2) real ( kind = rk8 ) y1(n1) real ( kind = rk8 ) y2(n2) ! ! Create the data files. ! call get_unit ( data_unit ) data1_filename = header // '_data1.txt' open ( unit = data_unit, file = data1_filename, status = 'replace' ) do i = 1, n1 write ( data_unit, '(5(2x,g14.6))' ) t1(i), y1(i) end do close ( unit = data_unit ) call get_unit ( data_unit ) data2_filename = header // '_data2.txt' open ( unit = data_unit, file = data2_filename, status = 'replace' ) do i = 1, n2 write ( data_unit, '(5(2x,g14.6))' ) t2(i), y2(i) end do close ( unit = data_unit ) write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' plot2: data stored in "' & // trim ( data1_filename ) // '" and "' // trim ( data2_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 "<-- T -->"' write ( command_unit, '(a)' ) 'set ylabel "<-- Y(T) -->"' write ( command_unit, '(a)' ) 'set title "' // trim ( title ) // '"' write ( command_unit, '(a)' ) 'set grid' write ( command_unit, '(a)' ) 'set style data lines' write ( command_unit, '(a)' ) 'plot "' // trim ( data1_filename ) // & '" using 1:2 with lines lw 3 lt rgb "red",\' write ( command_unit, '(a)' ) ' "' // trim ( data2_filename ) // & '" using 1:2 with lines lw 3 lt rgb "blue"' write ( command_unit, '(a)' ) 'quit' close ( unit = command_unit ) write ( *, '(a)' ) ' plot2: plot commands stored in "' & // trim ( command_filename ) // '".' return end subroutine plot4 ( n1, t1, y1, n2, t2, y2, n3, t3, y3, n4, t4, y4, & header, title ) !*****************************************************************************80 ! !! plot4() plots four curves together. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 23 June 2025 ! ! Author: ! ! John Burkardt ! ! Input: ! ! integer N1: the size of the first data set. ! ! real ( kind = rk8 ) T1(N1), Y1(N1), the first dataset. ! ! integer N2: the size of the second data set. ! ! real ( kind = rk8 ) T2(N2), Y2(N2), the second dataset. ! ! integer N3: the size of the data set 3. ! ! real ( kind = rk8 ) T3(N3), Y3(N3), dataset 3. ! ! integer N4: the size of data set 4. ! ! real ( kind = rk8 ) T4(N4), Y4(N4), dataset 4. ! ! character ( len = * ) HEADER: an identifier for the data. ! ! character ( len = * ) TITLE: a title to appear in the plot. ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) integer n1 integer n2 integer n3 integer n4 character ( len = 255 ) command_filename integer command_unit character ( len = 255 ) data1_filename character ( len = 255 ) data2_filename character ( len = 255 ) data3_filename character ( len = 255 ) data4_filename integer data_unit character ( len = * ) header integer i character ( len = * ) title real ( kind = rk8 ) t1(n1) real ( kind = rk8 ) t2(n2) real ( kind = rk8 ) t3(n3) real ( kind = rk8 ) t4(n4) real ( kind = rk8 ) y1(n1) real ( kind = rk8 ) y2(n2) real ( kind = rk8 ) y3(n3) real ( kind = rk8 ) y4(n4) ! ! Create the data files. ! call get_unit ( data_unit ) data1_filename = header // '_data1.txt' open ( unit = data_unit, file = data1_filename, status = 'replace' ) do i = 1, n1 write ( data_unit, '(5(2x,g14.6))' ) t1(i), y1(i) end do close ( unit = data_unit ) call get_unit ( data_unit ) data2_filename = header // '_data2.txt' open ( unit = data_unit, file = data2_filename, status = 'replace' ) do i = 1, n2 write ( data_unit, '(5(2x,g14.6))' ) t2(i), y2(i) end do close ( unit = data_unit ) call get_unit ( data_unit ) data3_filename = header // '_data3.txt' open ( unit = data_unit, file = data3_filename, status = 'replace' ) do i = 1, n3 write ( data_unit, '(5(2x,g14.6))' ) t3(i), y3(i) end do close ( unit = data_unit ) call get_unit ( data_unit ) data4_filename = header // '_data4.txt' open ( unit = data_unit, file = data4_filename, status = 'replace' ) do i = 1, n4 write ( data_unit, '(5(2x,g14.6))' ) t4(i), y4(i) end do close ( unit = data_unit ) write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' plot4: data stored in "' & // trim ( data1_filename ) // '", "' & // trim ( data2_filename ) // '", ' & // trim ( data3_filename ) // '", ' & // trim ( data4_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 "<-- T -->"' write ( command_unit, '(a)' ) 'set ylabel "<-- Y(T) -->"' write ( command_unit, '(a)' ) 'set title "' // trim ( title ) // '"' write ( command_unit, '(a)' ) 'set grid' write ( command_unit, '(a)' ) 'set style data lines' write ( command_unit, '(a)' ) 'plot "' // trim ( data1_filename ) // & '" using 1:2 with lines lw 3 lt rgb "red",\' write ( command_unit, '(a)' ) ' "' // trim ( data2_filename ) // & '" using 1:2 with lines lw 3 lt rgb "blue",\' write ( command_unit, '(a)' ) ' "' // trim ( data3_filename ) // & '" using 1:2 with lines lw 3 lt rgb "green",\' write ( command_unit, '(a)' ) ' "' // trim ( data4_filename ) // & '" using 1:2 with lines lw 3 lt rgb "magenta"' write ( command_unit, '(a)' ) 'quit' close ( unit = command_unit ) write ( *, '(a)' ) ' plot4: plot commands stored in "' & // trim ( command_filename ) // '".' return end subroutine timestamp ( ) !*****************************************************************************80 ! !! timestamp() prints the current YMDHMS date as a time stamp. ! ! Example: ! ! 31 May 2001 9:45:54.872 AM ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 18 May 2013 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) character ( len = 8 ) ampm integer d integer h integer m integer mm character ( len = 9 ), parameter, dimension(12) :: month = (/ & 'January ', 'February ', 'March ', 'April ', & 'May ', 'June ', 'July ', 'August ', & 'September', 'October ', 'November ', 'December ' /) integer n integer s integer values(8) integer 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 ( *, '(i2.2,1x,a,1x,i4,2x,i2,a1,i2.2,a1,i2.2,a1,i3.3,1x,a)' ) & d, trim ( month(m) ), y, h, ':', n, ':', s, '.', mm, trim ( ampm ) return end