C
C *** PHASE VELOCITY (CC) OF LOVE WAVE
C
      SUBROUTINE LOVECC(DOM,NOM,MOM,NDC,CFOM,NSMD,NEMD,MMD,CC,NMODE)
C
C ** INPUT DATA
C DOM    : INCREMENT OF CIRCULAR FREQUENCY ( DELTA OMEGA )
C NOM    : TOTAL NUMBER OF CIRCULAR FREQUENCY
C MOM    : NOM IN MAIN PROGRAM DIMENSION
C        : IF MOM > 2**13(=8192), CAN NOT USE !!!
C NDC    : DIVISION NUMVER FROM MIN VS TO MAX VS TO GET CC
C NSMD   : START OUTPUT MODE OF LOVE WAVE
C NEMD   : END OUTPUT MODE OF LOVE WAVE
C CFOM(I): CUT-OFF OMEGA OF Ith MODE
C MMD    : NEMD IN MAIN PROGRAM DIMENSION
C ** OUTPUT DATA
C CC(I,J): LOVE WAVE PHASE VELOCITY OF Jth MODE AT Ith OMEGA
C NMODE(I): TOTAL NUMBER OF PHASE VELOCITY AT Ith OMEGA
C
      IMPLICIT REAL*8(A-H,O-Z)
      COMMON / LAYER / NL,VS(35),DEP(35),SM(35) / OMEGA / OM
     *       / MINMAX / VSMIN, VSMAX
      DIMENSION CC(MOM,0:MMD),CFOM(0:MMD),NMODE(MOM)
      EXTERNAL FUNCFL
C
C *** INCREMENT OF CC (DC) BETWEEN VS(NL)(= MAX VS) AND MINIMUM VS ***
C
      BN=VS(NL)
      DC=(BN-VSMIN)/DFLOAT(NDC)
C
C *** INPUT OF CONVERGENCE JUDGMENT CRITERIA FOR D2ROOT ***
C
      DCEPS=1000.0D0
      CEPSO=DC/DCEPS
      DFEPS=100000.0D0
      ITMAX=100
      WRITE(*,*)
      WRITE(*,*)'From SUBROUTINE LOVECC :'
      WRITE(*,*)
      WRITE(*,*)'Convergence Criteria for Phase Velocity :'
      WRITE(*,1000)CEPSO
      WRITE(*,1010)DFEPS
      WRITE(*,1020)ITMAX
      WRITE(*,1030)
      READ(*,*)IYES
      IF(IYES.NE.0)THEN
        WRITE(*,*)
        WRITE(*,1040)
        READ(*,*)CEPSO,DFEPS,ITMAX
      END IF
      WRITE(*,*)
      WRITE(*,*)
 1000 FORMAT(/' CEPS = DC/DCEPS            : CEPS  =',F15.10)
 1010 FORMAT( ' FEPS = DABS(FL1-FL2)/DFEPS : DFEPS =',F15.3)
 1020 FORMAT( ' Maximum Iteration Number   : ITMAX =',I15/)
 1030 FORMAT( ' Are These Criteria OK ? ( yes = 0 ) :',\)
 1040 FORMAT( ' Enter New CEPS, DFEPS, ITMAX :',\)
C
C *** PHASE VELOCITY FOR EACH OMEGA ***
C
      DO 100 IOM=1,NOM
C
C  ** OMEGA AND THE LOWEST USED OMEGA **
C
        OM=DFLOAT(IOM)*DOM
        IF(OM.LT.CFOM(NSMD))GOTO 100
C
C  ** LOWER TRIAL CC AND EIGEN VALUE EQUATION **
C
        CC1=VSMIN-DC
        FL1=FUNCFL(CC1)
C
C  ** CHECK OF LOWER TRIAL CC **
C
        IFC=2
        ICC=0
        IF(FL1.EQ.0.0D0)THEN
          CC(IOM,0)=CC1
          IF(NEMD.EQ.0)GOTO 40
          CC1=VSMIN
          FL1=FUNCFL(CC1)
          IFC=3
          ICC=1
        ENDIF
C
C  ** SEARCH OF CORRECT CC FROM MINIMUM VS TO MAXIMUM VS(=VS(NL)) **
C
        DO 110 IC=IFC,NDC+2
C
C   * HIGHER TRIAL CC AND EIGEN VALUE EQUARION *
C
          CC2=VSMIN+DFLOAT(IC-2)*DC
          FL2=FUNCFL(CC2)
C
C   * CHECK OF TRIAL CC ( LOWER AND HIGER CC ) *
C
          IF(FL1*FL2)10,20,30
C
C     * CASE OF LOWER CC < CORRECT CC < HIGHER CC *
C
C       * SEARCH OF CORRECT CC BY D2ROOT *
C
   10     CEPS=CEPSO
          FEPS=DABS(FL1-FL2)/DFEPS
          ITR=ITMAX
          CL=CC1
          CR=CC2
          CALL D2ROOT(FUNCFL,CL,CR,CEPS,FEPS,ITR,CCLR,IER)
C
C       * D2ROOT ERROR OR NOT CONVERGENCE WORK *
C
          IF(IER.NE.0)THEN
            WRITE(*,*)
            WRITE(*,*)' FROM SUBROUTINE LOVECC :'
            WRITE(*,*)' D2ROOT ERROR, IER =',IER
            WRITE(*,*)' OMEGA =',OM
            WRITE(*,2000)CEPS,FEPS,ITR
            WRITE(*,*)' CC1,CC2 =',CC1,CC2
            WRITE(*,*)' FL1,FL2 =',FL1,FL2
            IF(IER.EQ.3000)THEN
              WRITE(*,*)' NOT CONVERGENCE (IER=3000) !!!'
              WRITE(*,*)' CL,CR =',CL,CR
              FLL=FUNCFL(CL)
              FLR=FUNCFL(CR)
              WRITE(*,*)' FLL,FLR =',FLL,FLR
              WRITE(*,*)' CC IS CHANGED BY MEAN CC !!!'
              CCLR=(CR+CL)/2.0D0
              FLLR=FUNCFL(CCLR)
              WRITE(*,*)' NEW CC, FL =',CCLR,FLLR
            ELSE
              WRITE(*,*)' IER=2000 INPUT DATA ERROR  !!!'
              WRITE(*,*)' IER=2100 FL1*FL2 > 0       !!!'
              WRITE(*,*)' STOP  !!!'
              STOP
            ENDIF
          ENDIF
 2000     FORMAT(' CEPS,FEPS,ITRATION =',2D15.7,I5)
C
C       * CORRECT CC BY D2ROOT *
C
          CC(IOM,ICC)=CCLR
          IF(NEMD.EQ.ICC)GOTO 40
          ICC=ICC+1
          GOTO 30
C
C    * CASE OF CORRECT CC = HIGHER CC *
C
   20     CC(IOM,ICC)=CC2
          IF(NEMD.EQ.ICC)GOTO 40
          ICC=ICC+1
          CC2=CC1+DC/2.0D0
          FL2=FUNCFL(CC2)
C
C    * CASE OF LOWER CC < HIGER CC < CORRECT CC *
C
C      * LOWER CC ==> HIGHER CC AND NEXT STEP *
C
   30     CC1=CC2
          FL1=FL2
C
  110   CONTINUE
C
C  ** MODE NUMBER AT EACH OMEGA **
C
        NMODE(IOM)=ICC-1
        GOTO 50
   40   NMODE(IOM)=NEMD
C
C *** IMFORMATION OUTPUT ON SCREEN ***
C
   50   WRITE(*,3000)OM,DOM*DFLOAT(NOM)
 3000   FORMAT('+Now Caluculating at OMEGA / Final OMEGA =',2F8.3)
C
C *** NEXT OMEGA ***
C
  100 CONTINUE
C
      WRITE(*,*)
C
      RETURN
      END
