      SUBROUTINE MINV(A,N,D)
      implicit double precision (A-H,O-Z)
      include 'SIZES'
      include 'MOP6FILE'
      dimension A(*)
**********************************************************************
*
*     INVERT A MATRIX USING GAUSS-JORDAN METHOD.  PART OF DIIS
*     A - INPUT MATRIX (MUST BE A GENERAL MATRIX), DESTROYED IN
*        COMPUTATION AND REPLACED BY RESULTANT INVERSE.
*     N - ORDER OF MATRIX A
*     D - RESULTANT DETERMINANT
*
**********************************************************************
      dimension M(MAXPAR), L(MAXPAR)
C
C     SEARCH FOR LARGEST ELEMENT
C
      D=1.0D0
      NK=-N
      do 180 K=1,N
         NK=NK+N
         L(K)=K
         M(K)=K
         KK=NK+K
         BIGA=A(KK)
         do 20 J=K,N
            IZ=N*(J-1)
            do 20 I=K,N
               IJ=IZ+I
   10          if (ABS(BIGA).LT.ABS(A(IJ))) then
                  BIGA=A(IJ)
                  L(K)=I
                  M(K)=J
               end if
   20    continue
C
C     INTERCHANGE ROWS
C
         J=L(K)
         if (J-K) 50,50,30
   30    KI=K-N
         do 40 I=1,N
            KI=KI+N
            HOLD=-A(KI)
            JI=KI-K+J
            A(KI)=A(JI)
   40    A(JI)=HOLD
C
C     INTERCHANGE COLUMNS
C
   50    I=M(K)
         if (I-K) 80,80,60
   60    JP=N*(I-1)
         do 70 J=1,N
            JK=NK+J
            JI=JP+J
            HOLD=-A(JK)
            A(JK)=A(JI)
   70    A(JI)=HOLD
C
C     DIVIDE COLUMN BY MINUS PIVOT (VALUE OF PIVOT ELEMENT IS
C     CONTAINED IN BIGA)
C
   80    if (BIGA) 100,90,100
   90    D=0.0
         return
  100    do 120 I=1,N
            if (I-K) 110,120,110
  110       IK=NK+I
            A(IK)=A(IK)/(-BIGA)
  120    continue
C  REDUCE MATRIX
         do 150 I=1,N
            IK=NK+I
            HOLD=A(IK)
            IJ=I-N
            do 150 J=1,N
               IJ=IJ+N
               if (I-K) 130,150,130
  130          if (J-K) 140,150,140
  140          KJ=IJ-I+K
               A(IJ)=HOLD*A(KJ)+A(IJ)
  150    continue
C
C     DIVIDE ROW BY PIVOT
C
         KJ=K-N
         do 170 J=1,N
            KJ=KJ+N
            if (J-K) 160,170,160
  160       A(KJ)=A(KJ)/BIGA
  170    continue
C
C     PRODUCT OF PIVOTS
C
         D=MAX(-1.D25,MIN(1.D25,D))
         D=D*BIGA
C
C     REPLACE PIVOT BY RECIPROCAL
C
         A(KK)=1.0/BIGA
  180 continue
C
C     FINAL ROW AND COLUMN INTERCHANGE
C
      K=N
  190 K=(K-1)
      if (K) 260,260,200
  200 I=L(K)
      if (I-K) 230,230,210
  210 JQ=N*(K-1)
      JR=N*(I-1)
      do 220 J=1,N
         JK=JQ+J
         HOLD=A(JK)
         JI=JR+J
         A(JK)=-A(JI)
  220 A(JI)=HOLD
  230 J=M(K)
      if (J-K) 190,190,240
  240 KI=K-N
      do 250 I=1,N
         KI=KI+N
         HOLD=A(KI)
         JI=KI-K+J
         A(KI)=-A(JI)
  250 A(JI) =HOLD
      go to 190
  260 return
      end
