! Sample program for gtool_history/gtool4          2001/02/27 S.Takehiro
!                                                  2003/08/21 M.Odaka
! 
! Solving diffusion equation 
!     du/dt = kappa d^2 u/dx^2
! for giving values of u at x=0 and 1. 
!
program diffusion

  use gtool_history                                          ! $B%b%8%e!<%k;XDj(B

  integer, parameter              :: nx=30                   ! $B%0%j%C%I?t(B
  integer, parameter              :: nt=200                  ! $B;~4V%9%F%C%W?t(B
  integer, parameter              :: ndisp=10                ! $B=PNO4V3V(B
  double precision, parameter     :: dx=1.0/(nx-1)           ! $B%0%j%C%I4V3V(B
  double precision, parameter     :: dt=0.0005               ! $B;~4V4V3V(B
  double precision, dimension(nx) :: x=(/(dx*(i-1),i=1,nx)/) ! $B:BI8JQ?t(B
  double precision, dimension(nx) :: temp                    ! $B29EY(B
  double precision, parameter     :: kappa=1.0               ! $BG.3H;678?t(B

  tinit = 0.0                                                ! $B=i4|;~9o(B

  temp = exp(-((x-0.5)/0.1)**2)                              ! $B=i4|CM@_Dj(B

  call HistoryCreate( &                                      ! $B%R%9%H%j!<:n@.(B
       file='diffusion_2.nc', title='Diffusion equation', &
       source='Sample program of gtool_history/gtool4', &
       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)                                     ! $B<!85JQ?t=PNO(B

  call HistoryAddVariable( &                                 ! $BJQ?tDj5A(B
       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('t',real(tinit))                           ! $B=i4|;~9o=PNO(B
  call HistoryPut('temp',temp)                               ! $BJQ?t=PNO(B

  do it=1,nt
     temp(2:nx-1) = temp(2:nx-1) &                           ! $B;~4V@QJ,(B
          +kappa*(temp(3:nx)-2*temp(2:nx-1)+temp(1:nx-2))/dx**2*dt
       
     if ( mod(nt,ndisp) .eq. 0 ) then
        call HistoryPut('t',real(it*dt))                     ! $B;~9o=PNO(B
        call HistoryPut('temp',temp)                         ! $BJQ?t=PNO(B
     endif
  enddo

  call HistoryClose
  stop
end program diffusion
