!= Sample program for gtool_history/gtool5
!
! * 2009/10/18 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_cal1

  use gtool_history                                 ! Access module (繝｢繧ｸ繝･繝ｼ繝ｫ謖�ｮ�)
  use dc_calendar                               ! 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_CAL_DATE)    :: start_date          ! Start Date (繝｢繝�Ν髢句ｧ区律譎�)
  type(DC_CAL_DATE)    :: end_date            ! End Date (繝｢繝�Ν邨ゆｺ�律譎�)
  real(DP)             :: curtime             ! Current time [sec] (迴ｾ蝨ｨ譎ょ綾 [遘綻)
  real(DP)             :: endtime             ! End time [sec] (邨ゆｺ�凾蛻ｻ [遘綻)
  real(DP)             :: dsptime             ! Output interval [min] (蜃ｺ蜉幃俣髫� [蛻�])
  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 (辭ｱ諡｡謨｣菫よ焚)

  character(256):: date_str

  call DCCalCreate( cal_type = 'Gregorian' )        ! Set calendar
                                                    ! (證ｦ險ｭ螳�)
  call DCCalDateCreate( year = 2009, month =  2, day = 28, &
    &                   hour =   23, min   = 59, sec = 0.0_DP, &
    &                   date = start_date )
                                                    ! Set date
                                                    ! (譌･譎りｨｭ螳�)
  curtime = DCCalConvertToSec(  0.0_DP, 'sec' )
                                                    ! Set initial time
                                                    ! (蛻晄悄譎ょ綾險ｭ螳�)
  endtime = DCCalConvertToSec( 10.0_DP, 'min' )
                                                    ! Set end time
                                                    ! (邨ゆｺ�凾蛻ｻ險ｭ螳�)
  dsptime = 1.0_DP
                                                    ! Set output interval
                                                    ! (蜃ｺ蜉幃俣髫碑ｨｭ螳�)

  temp = exp(-((x-0.5)/0.1)**2)                     ! Set initial value
                                                    ! (蛻晄悄蛟､險ｭ螳�)

  call HistoryCreate( &                             ! Create output file
    & file='diffusion_cal1.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'/),                             &
    & origind=curtime, intervald=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' (螟画焚蜃ｺ蜉�)

  call DCCalDateInquire( date_str, date = start_date )
                                        ! Inquire date and set a character variable
                                        ! (譌･譎ゅｒ蝠上＞蜷医ｏ縺帙※譁�ｭ怜梛螟画焚縺ｸ莉｣蜈･)

  call HistoryAddAttr( 't', 'units', 'min since ' // trim(date_str) )
                                                    ! Output start date to a file
                                                    ! (髢句ｧ区律譎ゅ�繝輔ぃ繧､繝ｫ縺ｸ縺ｮ蜃ｺ蜉�)

  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.0_DP ) then   ! Check output timing (蜃ｺ蜉帙ち繧､繝溘Φ繧ｰ蛻､螳�)
      call HistoryPut('temp',temp)                  ! Output 'temp' (螟画焚蜃ｺ蜉�)
    endif

    curtime = curtime + dt                          ! Progress model time (繝｢繝�Ν譎ょ綾騾ｲ陦�)
  enddo

  call DCCalDateInquire( date_str, curtime, start_date )
                                        ! Inquire date and set a character variable
                                        ! (譌･譎ゅｒ蝠上＞蜷医ｏ縺帙※譁�ｭ怜梛螟画焚縺ｸ莉｣蜈･)

  write(6,*) "End:   ", trim(date_str)              ! Display end date (邨ゆｺ�律譎ょ�蜉�)

  call HistoryClose
  stop
end program diffusion_cal1