module OW_fftw_areal1
!--------------------------------------------------
! Setting FFTW plans
!

use OW_type
use OW_parms_areal

implicit none

integer*8,pointer,private,save			:: planF(:),planI(:)
complex, allocatable,private, save		:: tfld(:)
integer, private, save				:: ntfld
integer, private				:: node, inode

contains

!---------------------------------------------------------
subroutine owfftw_init(inode_in,node_in,ntfld_in)

integer						:: node_in,inode_in,ntfld_in

node=node_in
inode=inode_in
ntfld=ntfld_in

allocate(tfld(ntfld))
allocate(planF(node))
allocate(planI(node))

end subroutine
!-----------------------------------------------------------------------------
subroutine owfftw_set()

integer						:: i
integer, parameter   				:: FFTW_R2HC=0,FTW_HC2R=1,FFTW_DHT=2,FFTW_REDFT00=3,FFTW_REDFT01=4
integer, parameter   				:: FFTW_REDFT10=5,FFTW_REDFT11=6,FFTW_RODFT00=7,FFTW_RODFT01=8,FFTW_RODFT10=9
integer, parameter   				:: FFTW_RODFT11=10,FFTW_FORWARD=-1,FFTW_BACKWARD=+1,FFTW_DESTROY_INPUT=1
integer, parameter   				:: FFTW_UNALIGNED=2,FFTW_CONSERVE_MEMORY=4,FFTW_EXHAUSTIVE=8,FTW_PRESERVE_INPUT=16
integer, parameter   				:: FFTW_PATIENT=32,FFTW_ESTIMATE=64,FFTW_ESTIMATE_PATIENT=128
integer, parameter   				:: FFTW_BELIEVE_PCOST=256,FFTW_DFT_R2HC_ICKY=512,FFTW_NONTHREADED_ICKY=1024
integer, parameter   				:: FFTW_NO_BUFFERING=2048,FFTW_NO_INDIRECT_OP=4096,FFTW_ALLOW_LARGE_GENERIC=8192
integer, parameter   				:: FFTW_NO_RANK_SPLITS=16384,FFTW_NO_VRANK_SPLITS=32768,FFTW_NO_VRECURSE=65536

do i=1,node
   call sfftw_plan_dft(planF(i),1,ntfld,tfld,tfld,fftW_FORWARD,fftW_PATIENT)
   call sfftw_plan_dft(planI(i),1,ntfld,tfld,tfld,fftw_BACKWARD,fftW_PATIENT)
end do

end subroutine
!------------------------------------------------------------------------------
subroutine owfftw_exeF(wfld)

complex						:: wfld(:)

 tfld=wfld
 call sfftw_execute(planF(inode))
 wfld=tfld/sqrt(1.*ntfld)

end subroutine
!--------------------------------------------------------------------------------
subroutine owfftw_exeI(wfld)

complex						:: wfld(:)

 tfld=wfld
 call sfftw_execute(planI(inode))
 wfld=tfld/sqrt(1.*ntfld)

end subroutine
!--------------------------------------------------------------------------------
subroutine owfftw_clean()

deallocate(tfld)

end subroutine
!--------------------------------------------------------------------------------
end module
