module xmap_mod
!! X-domain mapping
  use down_mod

  implicit none
  type(txmap), public :: smap,xmap
contains
!----------------------------------------------------------------
  subroutine xmap_init()
    integer :: er
    real :: a
#ifdef DEBUG
    character(len=128), parameter :: unit='xmap_init'
    if(run%debug) call in(unit)
#endif

    
!    dont seem to remember why or when I needed these
    !! mx
    allocate(xmap%mx(down%amx%n),stat=er)
    if(er/=0) call seperr('cant allocate mx')
    !! my
    allocate(xmap%my(down%amy%n),stat=er)
    if(er/=0) call seperr('cant allocate my')
    call xmap_op()
    a=4*size(xmap%mx)+4*size(xmap%my)
    down%mem=down%mem+a
#ifdef MEMCHECK
    if(run%debug) write(run%io,*) 'xmap.F90 kmaps: allocate',a/1024./1024.,'Mbytes.  Total=',down%mem/1024./1024.
#endif
#ifdef DEBUG
    if(run%debug) call out(unit)
#endif
  end subroutine xmap_init
!----------------------------------------------------------------
  subroutine xmap_op()
    integer :: imx,imy
    integer :: er

#ifdef DEBUG
    character(len=128), parameter :: unit='xmap_op'
    if(run%debug) call in(unit)
#endif

    !! mx
    do imx=1,down%amx%n
       xmap%mx(imx) = down%amx%o + (imx-1)* down%amx%d
    end do

    !! my 
    do imy=1,down%amy%n
       xmap%my(imy) = down%amy%o + (imy-1)* down%amy%d
    end do

#ifdef DEBUG
    if(run%debug) call out(unit)
#endif
  end subroutine xmap_op
  !----------------------------------------------------------------
end module xmap_mod
