!= Sample program for gtool_history/gtool5
!
! * 2007/06/25 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_4

  use gtool_history                                   ! Access module (繝｢繧ｸ繝･繝ｼ繝ｫ謖�ｮ�)
  use dc_types, only : DP                           ! Access module (繝｢繧ｸ繝･繝ｼ繝ｫ謖�ｮ�)
  use dc_message, only : MessageNotify              ! 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 (險育ｮ怜ｮ牙ｮ壽擅莉ｶ繝代Λ繝｡繧ｿ)

  tinit = 0.0                                       ! Set initial Time
                                                    ! (蛻晄悄譎ょ綾險ｭ螳�)

  sigma = kappa*dt/dx**2.0d0

  if ( sigma >= 0.5d0 ) then
    call MessageNotify( "E", &                      ! Error mesage dump
      &                 "diffusion_4", &            !(繧ｨ繝ｩ繝ｼ繝｡繝�そ繝ｼ繧ｸ蜃ｺ蜉� )
      &                 "dt is too large: k*dt/(dx)^2 = %f", &
      &                  d=(/sigma/) )
  else if ( sigma >= 0.4d0 ) then
    call MessageNotify( "W", &                      ! Warning message dump
      &                 "diffusion_4", &            ! (隴ｦ蜻翫Γ繝�そ繝ｼ繧ｸ蜃ｺ蜉�)
      &                 "dt is moderately large: k*dt/(dx)^2 = %f", &
      &                 d=(/sigma/) )
  else
    call MessageNotify( "M", &                      ! Message dump
      &                 "diffusion_4", &            ! (繝｡繝�そ繝ｼ繧ｸ蜃ｺ蜉�)
      &                 "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_4.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' (螟画焚蜃ｺ蜉�)

  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
  enddo

  call HistoryClose
  stop
end program diffusion_4