c
c     Tests all the conversion routines at once by
c     chaining them toghether
c     
c     Good to have, to check for stupid mistakes
c     
      subroutine testChain()
      implicit none
      
      integer nX, nY, nMax
      
      parameter(nMax=1000)

      parameter(nX=50)
      parameter(nY=150)
      
      real*8 u, uu, uv
      real*8 psi, w,wTmp
      real*8 x,y

      real*8 eps
      parameter(eps=1.d-20)

      dimension u(2,0:nMax+1,0:nMax+1)
      dimension Uu(0:nMax+1,0:nMax+1)
      dimension Uv(0:nMax+1,0:nMax+1)
      dimension w(0:nMax+1,0:nMax+1)
      dimension wTmp(0:nMax+1,0:nMax+1)
      dimension psi(0:nMax+1,0:nMax+1)

      dimension x(0:nx+1), y(0:ny+1)

      call step(x, 0.d0, 3.d0, nx, .TRUE.)
      call step(y, 0.d0, 1.d0, ny, .FALSE.)


      call set_const_2d(psi(0,0), 
     *     nx, ny, nMax, 0.d0)

      call set_const_2d(w(0,0), 
     *     nx, ny, nMax, 0.d0)

      call initializeFlow(
     *     u,  nX, nY, 
     *     nMax, x, 
     *     y)


      call splitUV(U,
     *     uu,uv,nx,ny,
     *     nmax)

      call printResult(x(0),y,uu,
     *     nx,ny,nmax,0.d0,0,
     *     .TRUE. , .TRUE.)

      call printResult(x(0),y,uv,
     *     nx,ny,nmax,0.d0,1,
     *     .TRUE. , .TRUE.)

      call wFromU(w, u, nX, nY, 
     *     nMax, x(1)-x(0), 
     *     y(1)-y(0), eps)

      call printResult(x,y,w,
     *     nx,ny,nmax,0.d0,2,
     *     .TRUE. , .TRUE.)


      call psiFromW(psi, w,wTmp,
     *     nx, ny, nMax,
     *     x(1)-x(0), y(1)-y(0), eps)

      
      call printResult(x,y,psi,
     *     nx,ny,nmax,0.d0,3,
     *     .TRUE. , .TRUE.)


      call uFromPsi(u, psi, nX, nY, 
     *     nMax, x(1)-x(0), y(1)-y(0), eps)


      call splitUV(U,
     *     uu,uv,nx,ny,
     *     nmax)

      call printResult(x(0),y,uu,
     *     nx,ny,nmax,0.d0,4,
     *     .TRUE. , .TRUE.)

      call printResult(x(0),y,uv,
     *     nx,ny,nmax,0.d0,5,
     *     .TRUE. , .TRUE.)


      return
      end

c     
c     Splits a U field into U and V components so
c     they can be seen
c     
c     Used by test routines
c     
      subroutine splitUV(U, Uu, Uv,
     *     nX, nY, nMax)

      real*8 u, uu, uv

      integer nX, nY, nMax
      integer i,j

      dimension U(2, 0:nMax+1, 0:nMax+1)
      dimension Uu(0:nMax+1,0:nMax+1)
      dimension Uv(0:nMax+1,0:nMax+1)

      do j=0,nMax+1
         do i=0,nMax+1
            Uu(i,j) = u(1,i,j)
            Uv(i,j) = u(2,i,j)
         enddo
      enddo

      return
      end