program main !*****************************************************************************80 ! !! doughnut_ode_test() tests doughnut_ode(). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 10 October 2025 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) character ( len = 255 ) command_filename integer command_unit character ( len = 255 ) data_filename integer data_unit real ( kind = rk8 ) m real ( kind = rk8 ) n integer i integer nt real ( kind = rk8 ), allocatable :: t(:) real ( kind = rk8 ) t0 real ( kind = rk8 ) tstop real ( kind = rk8 ), allocatable :: y(:,:) real ( kind = rk8 ) y0(3) interface subroutine doughnut_parameters ( & m_in, n_in, t0_in, y0_in, tstop_in, & m_out, n_out, t0_out, y0_out, tstop_out ) integer, parameter :: rk8 = kind ( 1.0D+00 ) real ( kind = rk8 ), optional :: m_in real ( kind = rk8 ), optional :: m_out real ( kind = rk8 ), optional :: n_in real ( kind = rk8 ), optional :: n_out real ( kind = rk8 ), optional :: t0_in real ( kind = rk8 ), optional :: t0_out real ( kind = rk8 ), optional :: y0_in(3) real ( kind = rk8 ), optional :: y0_out(3) real ( kind = rk8 ), optional :: tstop_in real ( kind = rk8 ), optional :: tstop_out end subroutine end interface call timestamp ( ) write ( *, '(a)' ) '' write ( *, '(a)' ) 'doughnut_ode_test():' write ( *, '(a)' ) ' Fortran90 version' write ( *, '(a)' ) ' Test doughnut_ode().' call doughnut_parameters ( & m_out = m, n_out = n, t0_out = t0, y0_out = y0, tstop_out = tstop ) write ( *, '(a)' ) '' write ( *, '(a)' ) ' Parameters:' write ( *, '(a,g14.6)' ) ' m = ', m write ( *, '(a,g14.6)' ) ' n = ', n write ( *, '(a,g14.6)' ) ' t0 = ', t0 write ( *, '(a,3g14.6)' ) ' y0 = ', y0(1:3) write ( *, '(a,g14.6)' ) ' tstop = ', tstop ! ! Compute solution. ! nt = 10000 allocate ( t(1:nt+1) ) call r8vec_linspace ( nt+1, t0, tstop, t ) allocate ( y(1:nt+1,3) ) call doughnut_euler ( nt, t, y ) ! ! Plot solution. ! call get_unit ( data_unit ) data_filename = 'doughnut_ode_data.txt' open ( unit = data_unit, file = data_filename, status = 'replace' ) do i = 1, nt + 1 write ( data_unit, '(2x,g14.6,2x,g14.6,2x,g14.6,2x,g14.6)' ) t(i), y(i,1:3) end do close ( unit = data_unit ) write ( *, '(a)' ) ' Created data file "' // trim ( data_filename ) // '".' ! ! Create the command file. ! call get_unit ( command_unit ) command_filename = 'doughnut_ode_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 "doughnut_ode.png"' write ( command_unit, '(a)' ) 'set xlabel "<--- X(T) --->"' write ( command_unit, '(a)' ) 'set ylabel "<--- Y(T) --->"' write ( command_unit, '(a)' ) 'set zlabel "<--- Z(T) --->"' write ( command_unit, '(a)' ) & 'set title "doughnut\_ode trajectory"' write ( command_unit, '(a)' ) 'set grid' write ( command_unit, '(a)' ) 'set style data lines' write ( command_unit, '(a)' ) 'splot "' // trim ( data_filename ) // & '" using 2:3:4 lw 2 linecolor rgb "blue"' close ( unit = command_unit ) write ( *, '(a)' ) & ' Created command file "' // trim ( command_filename ) // '".' ! ! Free memory. ! deallocate ( t ) deallocate ( y ) ! ! Terminate. ! write ( *, '(a)' ) '' write ( *, '(a)' ) 'doughnut_ode_test():' write ( *, '(a)' ) ' Normal end of execution.' call timestamp ( ) stop ( 0 ) end subroutine doughnut_euler ( nt, t, y ) !*****************************************************************************80 ! !! doughnut_euler() uses the Euler method on the doughnut ODE. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 10 October 2025 ! ! Author: ! ! John Burkardt ! ! Input: ! ! integer NT: the number of steps to take. ! ! Output: ! ! real ( kind = rk8 ) T(NT+1), Y(NT+1): the times and estimated solutions. ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) integer nt real ( kind = rk8 ) dt real ( kind = rk8 ) dydt(3) integer i real ( kind = rk8 ) t(nt+1) real ( kind = rk8 ) t0 real ( kind = rk8 ) tstop real ( kind = rk8 ) y(nt+1,3) real ( kind = rk8 ) y0(3) interface subroutine doughnut_parameters ( & m_in, n_in, t0_in, y0_in, tstop_in, & m_out, n_out, t0_out, y0_out, tstop_out ) integer, parameter :: rk8 = kind ( 1.0D+00 ) real ( kind = rk8 ), optional :: m_in real ( kind = rk8 ), optional :: m_out real ( kind = rk8 ), optional :: n_in real ( kind = rk8 ), optional :: n_out real ( kind = rk8 ), optional :: t0_in real ( kind = rk8 ), optional :: t0_out real ( kind = rk8 ), optional :: y0_in(3) real ( kind = rk8 ), optional :: y0_out(3) real ( kind = rk8 ), optional :: tstop_in real ( kind = rk8 ), optional :: tstop_out end subroutine end interface call doughnut_parameters ( & t0_out = t0, y0_out = y0, tstop_out = tstop ) dt = ( tstop - t0 ) / real ( nt, kind = rk8 ) do i = 1, nt + 1 if ( i == 1 ) then t(1) = t0 y(1,1:3) = y0(1:3) else call doughnut_deriv ( t(i-1), y(i-1,1:3), dydt ) t(i) = t(i-1) + dt y(i,1:3) = y(i-1,1:3) + dt * dydt(1:3) end if end do return 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 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