!= Sample program for gtool_history/gtool5
!
! * 2007/06/27 M.Odaka
! * 2006/10/25 Y.Morikawa
! * 2003/08/21 M.Odaka
! * 2001/02/27 S.Takehiro
!
! Solving diffusion equation
! \[
!     du/dt = \kappa d^2 u/dx^2
! \]
! for giving values of $u$ at $x=[0,1]$.
!
program diffusion_6

  use gtool_history                                   ! Access module (ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«æŒ‡å®š)
  use dc_types, only : DP                           ! Access module (ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«æŒ‡å®š)
  use dc_message, only : MessageNotify              ! Access module (ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«æŒ‡å®š)
  use dc_trace, only : SetDebug, DbgMessage         ! Access module (ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«æŒ‡å®š)

  integer, parameter     :: nx=30                   ! Grid number (ã‚°ãƒªãƒƒãƒ‰æ•°)
  integer, parameter     :: nt=200                  ! Time step number (æ™‚é–“ã‚¹ãƒ†ãƒƒãƒ—æ•°)
  integer, parameter     :: ndisp=10                ! Output interval (å‡ºåŠ›é–“éš”)
  real(DP), parameter    :: dx=1.0/(nx-1)           ! Grid interval (ã‚°ãƒªãƒƒãƒ‰é–“éš”)
  real(DP), parameter    :: dt=0.0005               ! Time step (æ™‚é–“é–“éš”)
  real(DP), dimension(nx):: x=(/(dx*(i-1),i=1,nx)/) ! X coordinate (åº§æ¨™å¤‰æ•°)
  real(DP), dimension(nx):: temp                    ! Temperature (æ¸©åº¦)
  real(DP), parameter    :: kappa=1.0               ! Diffusion coefficient (ç†±æ‹¡æ•£ä¿‚æ•°)
  real(DP)               :: sigma                   ! Parameter (è¨ˆç®—å®‰å®šæ¡ä»¶ãƒ‘ãƒ©ãƒ¡ã‚¿)

  call SetDebug                                     ! Debug mode on
                                                    ! (ãƒ‡ãƒãƒƒã‚°ãƒ¢ãƒ¼ãƒ‰ã‚ªãƒ³)

  call DbgMessage(fmt="*** Debug Message [diffusion_6] *** Debug Message On")
                                                    ! Debug message dump
                                                    ! (ãƒ‡ãƒãƒƒã‚°å‡ºåŠ›)

  tinit = 0.0                                       ! Set initial time
                                                    ! (åˆæœŸæ™‚åˆ»è¨å®š)

  sigma = kappa*dt/dx**2.0d0

  if ( sigma >= 0.5d0 ) then
    call MessageNotify( "E", &                      ! Error mesage dump
      &                 "diffusion_6", &            ! (ã‚¨ãƒ©ãƒ¼ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸å‡ºåŠ› )
      &                 "dt is too large: k*dt/(dx)^2 = %f", &
      &                  d=(/sigma/) )
  else if ( sigma >= 0.4d0 ) then
    call MessageNotify( "W", &                      ! Warning message dump
      &                 "diffusion_6", &            ! (è¦å‘Šãƒ¡ãƒƒã‚»ãƒ¼ã‚¸å‡ºåŠ›)
      &                 "dt is moderately large: k*dt/(dx)^2 = %f", &
      &                 d=(/sigma/) )
  else
    call MessageNotify( "M", &                      ! Message dump
      &                 "diffusion_6", &            ! (ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸å‡ºåŠ›)
      &                 "dt is sufficiently small: k*dt/(dx)^2 = %f", &
      &                  d=(/sigma/) )
  end if

  temp = exp(-((x-0.5)/0.1)**2)                     ! Set initial value
                                                    ! (åˆæœŸå€¤è¨å®š)

  call HistoryCreate( &                             ! Create output file
    & file='diffusion_6.nc', &                      ! (ãƒ’ã‚¹ãƒˆãƒªãƒ¼ä½œæˆ)
    & title='Diffusion equation',                        &
    & source='Sample program of gtool_history/gtool5',   &
    & institution='GFD_Dennou Club davis project',       &
    & dims=(/'x','t'/), dimsizes=(/nx,0/),               &
    & longnames=(/'X-coordinate','time        '/),       &
    & units=(/'m','s'/),                                 &
    & origin=real(tinit), interval=real(ndisp*dt) )

  call HistoryPut('x',x)                            ! Output 'x' (æ¬¡å…ƒå¤‰æ•°å‡ºåŠ›)

  call HistoryAddVariable( &                        ! Set output variable
    & varname='temp', dims=(/'x','t'/), &           ! (å¤‰æ•°å®šç¾©)
    & longname='temperature', units='K', xtype='double')

  call HistoryAddAttr('temp','gt_graph_tick_all',1)
  call HistoryAddAttr('temp','gt_graph_contour_spacing',(/0.0,1.0,0.01/))
  call HistoryAddAttr('temp','+gt_user_davis_kappa',kappa)

  call HistoryPut('temp',temp)                      ! Output 'temp' (å¤‰æ•°å‡ºåŠ›)

  call DbgMessage(fmt="*** Debug Message [diffusion_6] *** Begin time integration")
                                                    ! Debug message dump
                                                    ! (ãƒ‡ãƒãƒƒã‚°å‡ºåŠ›)

  do it=1,nt

    temp(2:nx-1) = temp(2:nx-1) &                   ! Time integration (æ™‚é–“ç©åˆ†)
      & + kappa*(temp(3:nx)-2*temp(2:nx-1)+temp(1:nx-2))/dx**2*dt

    if ( mod(it,ndisp) == 0 ) then
      call HistoryPut('temp',temp)                  ! Output 'temp' (å¤‰æ•°å‡ºåŠ›)
    endif

    call DbgMessage(fmt="*** Debug Message [diffusion_6] *** it=%d, &
      &             i= (/it/) )                     ! Debug message dump
                                                    ! (ãƒ‡ãƒãƒƒã‚°å‡ºåŠ›)

  end do

  call DbgMessage(fmt="*** Debug Message [diffusion_6] *** End time integration")
                                                    ! Debug message dump
                                                    ! (ãƒ‡ãƒãƒƒã‚°å‡ºåŠ›)

  call HistoryClose

  stop
end program diffusion_6