      subroutine mapping2d(n, i, j, id)
      integer n, i, j

      id = i*(n-1)+j
      return
      end

      function mapping3d(n1, n2, i, j, k)
      integer i, j, k, mapping3d

!write(6,*) 'start mapping'
      mapping3d = i+(j-1)*n1+(k-1)*n1*n2-1
!write(6,*) n1,n2,i,j,mapping3d 
      return
      end

      subroutine invmapping2d(id, n, i, j)
      integer id, n, i, j

      i = id / (n-1)
      j = id - i*(n-1)

      return
      end


      subroutine invmapping3d(id, n1, n2, i, j, k)
      integer id, n1, n2, i, j
     
      id = id+1
      k=id/(n1*n2) + 1
      j=(id-(k-1)*n1*n2)/n1 +1

      i=id-(j-1)*n1 - (k-1)*n1*n2

      return
      end
