!= Sample program for gtool_history/gtool5
!
! * 2009/02/28 Y.Morikawa
! * 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_date

  use gtool_history                                 ! Access module (ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«æŒ‡å®š)
  use dc_date                                       ! Access module (ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«æŒ‡å®š)
  use dc_types                                      ! Access module (ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«æŒ‡å®š)

  integer, parameter     :: nx=30                   ! Grid number (ã‚°ãƒªãƒƒãƒ‰æ•°)
  real(DP), parameter    :: dx=1.0/(nx-1)           ! Grid interval (ã‚°ãƒªãƒƒãƒ‰é–“éš”)
  real(DP), parameter    :: dt=1.                   ! Time step [s] (æ™‚é–“é–“éš” [ç§’])
  type(DC_DIFFTIME)      :: deltime                 ! Time step (æ™‚é–“é–“éš”)
  type(DC_DIFFTIME)      :: endtime                 ! End time (çµ‚äº†æ™‚åˆ»)
  type(DC_DIFFTIME)      :: dsptime                 ! Output interval (å‡ºåŠ›é–“éš”)
  type(DC_DIFFTIME)      :: curtime                 ! Current time (ç¾åœ¨æ™‚åˆ»)
  real(DP), dimension(nx):: x=(/(dx*(i-1),i=1,nx)/) ! X coordinate (åº§æ¨™å¤‰æ•°)
  real(DP), dimension(nx):: temp                    ! Temperature (æ¸©åº¦)
  real(DP), parameter    :: kappa=0.0001            ! Diffusion coefficient (ç†±æ‹¡æ•£ä¿‚æ•°)

  type(DC_DATETIME):: date                          ! Date (æ—¥æ™‚)
  character(256):: date_str

  call DCDateTimeCreate(date, year=2009, mon=2, day=28, hour=23, min=59)
                                                    ! Set date
                                                    ! (æ—¥æ™‚è¨å®š)

  call DCDiffTimeCreate(curtime, 0., 'sec')         ! Set initial time
                                                    ! (åˆæœŸæ™‚åˆ»è¨å®š)
  call DCDiffTimeCreate(deltime, dt, 'sec')         ! Set time step
                                                    ! (æ™‚é–“é–“éš”è¨å®š)
  call DCDiffTimeCreate(endtime, 10., 'min')        ! Set end time
                                                    ! (çµ‚äº†æ™‚åˆ»è¨å®š)
  call DCDiffTimeCreate(dsptime, 0.1, 'min')        ! Set output interval
                                                    ! (å‡ºåŠ›é–“éš”è¨å®š)

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

  call HistoryCreate( &                             ! Create output file
    & file='diffusion_date.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  ','min'/),                             &
    & origin=curtime, interval=dsptime )

  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' (å¤‰æ•°å‡ºåŠ›)

  date_str = toChar(date)
  write(6,*) "Start: ", trim(date_str)              ! Display start date (é–‹å§‹æ—¥æ™‚å‡ºåŠ›)

  do while ( curtime <= endtime )                   ! Check termination (çµ‚äº†åˆ¤å®š)

    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( curtime, dsptime ) == 0 ) then        ! Check output timing (å‡ºåŠ›ã‚¿ã‚¤ãƒŸãƒ³ã‚°åˆ¤å®š)
      call HistoryPut('temp',temp)                  ! Output 'temp' (å¤‰æ•°å‡ºåŠ›)
    endif

    curtime = curtime + deltime                     ! Progress model time (ãƒ¢ãƒ‡ãƒ«æ™‚åˆ»é€²è¡Œ)
  enddo

  date = date + curtime                             ! Evaluate end date (çµ‚äº†æ—¥æ™‚ç®—å‡º)
  date_str = toChar(date)                           ! Convert date into characters (æ—¥æ™‚ã‚’æ–‡å—åž‹å¤‰æ•°ã¸å¤‰æ›)
  write(6,*) "End:   ", trim(date_str)              ! Display end date (çµ‚äº†æ—¥æ™‚å‡ºåŠ›)

  call HistoryClose
  stop
end program diffusion_date