      subroutine invert(flag, 
     &     in_xx,in_xy,in_xz,in_yy,in_yz,in_zz,
     &     rg, out_xx,out_xy,out_xz,out_yy,out_yz,out_zz)
c      integer nx,ny,nz
      integer flag

      real rg(:,:,:)
      real in_xx(:,:,:),in_xy(:,:,:),in_xz(:,:,:),
     &     in_yy(:,:,:),in_yz(:,:,:),in_zz(:,:,:) 

      real out_xx(:,:,:),out_xy(:,:,:),out_xz(:,:,:),
     &     out_yy(:,:,:),out_yz(:,:,:),out_zz(:,:,:) 

c    compute determinant of the given metric
c     Note that rg is not yet the roor of the determinant.
c      write(6,*) 'invert entered'
      rg= - in_xz**2*in_yy + 2*in_xy*in_xz*in_yz - in_xx*in_yz**2 
     &    - in_xy**2*in_zz + in_xx*in_yy*in_zz

c    invert metric
      out_xx=(-in_yz**2 + in_yy*in_zz)/rg
      out_xy=(in_xz*in_yz - in_xy*in_zz)/rg
c      write(6,*) '********in_xx********',in_xx
      out_yy=(-in_xz**2 + in_xx*in_zz)/rg
      out_xz=(-in_xz*in_yy + in_xy*in_yz)/rg
      out_yz=(in_xy*in_xz - in_xx*in_yz)/rg
      out_zz=(-in_xy**2 + in_xx*in_yy)/rg

c    assign meaning to rg depending on the flag

c     if 1, then the in_xx was the uppwe contravariant metric so 
c     the determinant that we have must be inverted to get the 
c     covariant determinant.
      if (flag.eq.1) rg = 1./rg
c     if -1, the in_xx of this program was the down covariant metric
c     so we do not need to do anything.

c     Finally, take the root of rg so it becomes what it was meant
c     to be (since the beginning of time...)

      rg = sqrt(rg)
c      write(6,*) maxval(out_xx),minval(out_xx)

      return
      end subroutine invert












