! Sample program for gtool_history/gtool4 and ISPACK   2001/09/23 S.Takehiro
! 
! Solving 2-D non-divergent (barotropic) fluid system
!     d\zeta/dt + J(\psi,\zeta) + beta d\psi/dx = \nabla^2\zeta,
!     \nabla\psi = \zeta
!
program baro

  use n2pack
  use gtool_history

!  integer,parameter      :: im=32, jm=32     ! 格子点の設定(X,Y)
!  integer,parameter      :: km=10, lm=10     ! 切断波数の設定(X,Y)
!  integer,parameter      :: km=15, lm=15     ! 切断波数の設定(X,Y)
  integer,parameter      :: im=128, jm=128   ! 格子点の設定(X,Y)
  integer,parameter      :: km=63,  lm=63    ! 切断波数の設定(X,Y)

  double precision, dimension(0:jm-1,0:im-1) :: psi,  zeta   ! 格子データ
  double precision, dimension(-lm:lm,-km:km) :: spsi, szeta  ! スペクトルデータ

  double precision, dimension(0:jm-1,0:im-1) :: x, y
  double precision, parameter                :: xmin=0.0, xmax=1.0
  double precision, parameter                :: ymin=0.0, ymax=1.0

  double precision, parameter  :: beta=0.0
  double precision, parameter  :: nu=1e-17
  integer, parameter           :: nv=5
  double precision, dimension(-lm:lm,-km:km) :: snu    ! スペクトルデータ
  double precision, parameter  :: pi=3.141592653589793
  
  double precision, parameter :: dt=1e-1
  integer, parameter          :: nt=200, ndisp=10

  double precision, parameter  :: x1=(xmax-xmin)*0.4 ; y1=(ymax-ymin)*0.4
  double precision, parameter  :: x2=(xmax-xmin)*0.6 ; y2=(ymax-ymin)*0.6
  double precision, parameter  :: sigma=0.05

  integer :: i,j, k,l

  do i=0,im-1
     do j=0,jm-1
        x(j,i)= (xmax-xmin)/im*i
        y(j,i)= (ymax-ymin)/jm*j
     enddo
  enddo

  call n2initial(im,jm,km,lm,xmax-xmin,ymax-ymin)

 !------------------- 粘性設定 ----------------------
  do k=-km,km
     do l=-lm,lm
        snu(l,k)=nu*((2*pi*k/(xmax-xmin))**2+(2*pi*l/(ymax-ymin))**2)**nv
     enddo
  enddo

 !------------------- 初期値設定 ----------------------
  zeta=  exp(-((x-x1)**2+(y-y1)**2)/(2*sigma**2)) &
       + exp(-((x-x2)**2+(y-y2)**2)/(2*sigma**2))
  szeta = s_g(zeta)
  szeta(0,0)=0.0
  psi=s_laplainv_s(szeta)

  call output_gtool4_init                            ! ヒストリー初期化

  do it=1,nt
    ! szeta = szeta + &
    !        dt*(  - s_jac_ss(spsi,szeta) &
    !             - beta*s_dx_s(spsi) - snu*szeta  )

     szeta = (szeta + dt*(-s_jac_ss(spsi,szeta)))/(1+snu*dt)

     spsi = s_laplainv_s(szeta)

     if(mod(it,ndisp) .eq. 0)then
        psi = g_s(spsi)
        zeta = g_s(szeta)
        call output_gtool4
     endif
  enddo

  call output_gtool4_close
  stop

contains
  subroutine output_gtool4_init
    call HistoryCreate( &                             ! ヒストリー作成
           file='baro.nc', title='2D barotrpic model', &
           source='Sample program of gtool_history/gtool4', &
           institution='GFD_Dennou Club davis project',     &
           dims=(/'x','y','t'/), dimsizes=(/im,jm,0/),         &
           longnames=(/'X-coordinate','Y-coordinate','time        '/),&
           units=(/'1','1','1'/),                           &
           origin=0.0, interval=real(ndisp*dt) )

    call HistoryPut('x',x(1,0:im-1))                          ! 変数出力
    call HistoryPut('y',y(0:jm-1,1))                          ! 変数出力

    call HistoryAddVariable( &                                ! 変数定義
           varname='psi', dims=(/'x','y','t'/), & 
           longname='stream function', units='1', xtype='double')
    call HistoryAddVariable( &                                ! 変数定義
           varname='zeta', dims=(/'x','y','t'/), & 
           longname='vorticity', units='1', xtype='double')
  end subroutine output_gtool4_init

  subroutine output_gtool4
    write(6,*) 'it = ',it
    call HistoryPut('psi',transpose(psi))
    call HistoryPut('zeta',transpose(zeta))
  end subroutine output_gtool4

  subroutine output_gtool4_close
    call HistoryClose
  end subroutine output_gtool4_close

end program baro

