c
c     =====================================================
      subroutine mapc2p(xc,yc,xp,yp)
c     =====================================================
c
c     # on input,  (xc,yc) is a computational grid point
c     # on output, (xp,yp) is corresponding point in physical space
c
c     # quadrilateral grid on a rectangle with circular inclusions
c     # For i=1:ncircles, there is a circular inclusion centered at
c     # (x0circ(i),y0circ(i)) of radius rcirc(i).  The grid is deformed
c     # within a square of radius r2circ(i).  These square regions should
c     # be non-overlapping or a strange grid may be generated.
c
      implicit double precision (a-h,o-z)
      common /comcirc/ x0circ(100),y0circ(100),rcirc(100),r2circ(100),
     &                 ncircles
c
c
      xp = xc
      yp = yc

      do i=1,ncircles
         x0 = x0circ(i)
         y0 = y0circ(i)
         r1 = rcirc(i)
         r2 = r2circ(i)
         xc0 = dabs(xc-x0)
         yc0 = dabs(yc-y0)
         if (dmax1(xc0,yc0) .le. r2) then
            write(28,*) xc,yc,i
            xc1 = xc0/r2
            yc1 = yc0/r2
            d = dmax1(dmax1(xc1,yc1), 1.d-10)
            d = dmin1(d, 0.9999d0)
            d1 = d*r2/dsqrt(2.d0)
c           rad = dsqrt(2.d0) * d1  !# for concentric circular grid lines
            rad = r1                !# for constant curvature grid lines
            if (d .gt. r1/r2) then
               d1 = r1/dsqrt(2.d0) + (d - r1/r2) * (r2 - r1/dsqrt(2.d0))
     &              / (1.d0 - r1/r2)
               d2 = dmax1(1.d0 - d, 1.d-8)
               rad = r1 * ((1.d0 - r1/r2) / d2) ** (r2/r1 + 0.5d0)
               endif
            xp2 = d1/d * xc1
            yp2 = d1/d * yc1
            center = d1 - dsqrt(rad**2 - d1**2)
            if (xc1 .eq. d) xp2 = center + dsqrt(rad**2 - yp2**2)
            if (yc1 .eq. d) yp2 = center + dsqrt(rad**2 - xp2**2)
            xp2 = dsign(xp2,xc-x0)
            yp2 = dsign(yp2,yc-y0)
            xp = x0 + xp2
            yp = y0 + yp2
            endif
         enddo

      return
      end
