C
C *** READ OF SOIL DATA FROM DATA FILE
C
      SUBROUTINE LVIN(DOM,NOM,ML,NL,DNS,VS,DEP,NDOM,NSMD,NEMD,NDC)
C
C ** INPUT DATA **
C DOM   : INCREMENT OF CIRCULAR FREQUENCY (DELTA OMEGA)
C NOM   : TOTAL NUMBER OF CIRCULAR FREQUENCY
C ML    : NL IN MAIN 
C NL    : TOTAL NUMBER OF LAYERS           
C DNS(I): DENSITY OF Ith LAYER 
C VS (I): VS OF Ith LAYER      
C DEP(I): DEPTH (THICKNESS) OF Ith LAYER  
C NDOM  : DIVISION NUMBER FROM 0 TO MAX OMEGA TO GET CUT-OFF OMEGA
C NSMD  : FIRST OUTPUT MODE OF LOVE WAVE
C NEMD  : END OUTPUT MODE OF LOVE WAVE
C NDC   : DIVISION NUMBER FROM MIN TO MAX VS TO GET PHASE VELOCITY
C
      IMPLICIT REAL*8(A-H,O-Z)
      CHARACTER FNAME*30
      DIMENSION DNS(ML),VS(ML),DEP(ML)
      COMMON / MINMAX / VSMIN, VSMAX
C
C *** SOIL DATA INPUT FROM FILE DATA ***
C
      WRITE(*,*)
      WRITE(*,1000)
   10 WRITE(*,1010)
      READ(*,'(A)')FNAME
      IF(FNAME(1:1).EQ.' ')GOTO 10
 1000 FORMAT( ' From SUBROUTINE LVIN :')
 1010 FORMAT(/' Enter Soil Data File Name ',\)

      OPEN (7,FILE=FNAME)
      READ(7,1200)NL
      DO 100 IL=1,NL
        READ(7,1210)DNS(IL),VP,VS(IL),DEP(IL),QF
  100 CONTINUE
      CLOSE(7)
 1200 FORMAT(I10)
 1210 FORMAT(5F10.0)
C
C *** DATA ECHO ***
C
      WRITE(*,2000)NL
      DO 200 IL=1,NL
        WRITE(*,2010)IL,DNS(IL),VS(IL),DEP(IL)
  200 CONTINUE
 2000 FORMAT(/' Total Layers Number            =',I5)
 2010 FORMAT( ' Layer Num., Density, Vs, Depth =',I3,3F10.3)
C
C *** MIN AND MAX VS AND SOIL DATA CHECK ***
C
      VSMIN=VS(NL)
      VSMAX=0.0D0
      DO 300 IL=1,NL
        VSMIN=MIN(VSMIN,VS(IL))
        VSMAX=MAX(VSMAX,VS(IL))
  300 CONTINUE
      IF(VSMAX.NE.VS(NL))THEN
        WRITE(*,*)' FROM SUBROUTINE LVIN'
        WRITE(*,*)' VS(NS).NE.VSMAX, THUS STOP !!!'
        STOP
      END IF
C
      IF(NL.GT.50)THEN
        WRITE(*,*)' FROM SUBROUTINE LVIN'
        WRITE(*,*)' SOIL LAYERS NUMBER > 50, THUS STOP !!!'
        STOP
      END IF
C
C *** DATA INPUT FROM SCREEN ***
C
   20 WRITE(*,3000)
      READ(*,*)DOM,NOM
      WRITE(*,3005)DOM*DFLOAT(NOM)
      WRITE(*,3010)
      READ(*,*)NSMD
      WRITE(*,3020)
      READ(*,*)NEMD
      IF(NEMD.EQ.0)GOTO 30
      OMMAX=DOM*DFLOAT(NOM)
      WRITE(*,3030)0.D0,OMMAX
      WRITE(*,3040)
      READ(*,*)NDOM
   30 WRITE(*,3050)VSMIN,VSMAX
      WRITE(*,3060)
      READ(*,*)NDC
      WRITE(*,3070)
      READ(*,*)IYES
      IF(IYES.NE.0)GOTO 20
      WRITE(*,*)
C
 3000 FORMAT(/' Enter Delta Omega and Omega Number        ?    :',\)
 3005 FORMAT( ' Final Omega = ',F10.5)
 3010 FORMAT(/' Enter Output Start Love Mode (Fundamental=0) ? :',\)
 3020 FORMAT( ' Enter Output End Love Mode ( >= Start Mode ) ? :',\)
 3030 FORMAT(/' Minimum Omega and Maximum Omega = ',2F8.3)
 3040 FORMAT( ' To Get Cut-Off Omega,  Enter',
     *       /' Division Number between Min and Max Omega (ex.100) :',\)
 3050 FORMAT(/' Minimum Vs and Maximum Vs = ',2F10.3)
 3060 FORMAT( ' To Get Phase Velocity, Enter',
     *       /' Division Number between Min and Max Vs (ex.100)    :',\)
 3070 FORMAT(/' Input Data OK ? ( yes = Enter 0 )            ? :',\)
C
      RETURN
      END
C
C
C *** CUT-OFF OMEGA OF LOVE WAVE (C=VS(NL))
C
      SUBROUTINE LVCT(DOM,NOM,ML,NL,DNS,VS,DEP,NDOM,CFOM,NEMD,MMD,NCFOM)
C
C ** INPUT DATA
C DOM  : INCREMENT OF CIRCULAR FREQUENCY (DELTA OMEGA)
C NOM  : TOTAL NUMBER OF CIRCULAR FREQUENCY
C ML    : NL IN MAIN 
C NL    : TOTAL NUMBER OF LAYERS           
C DNS(I): DENSITY OF Ith LAYER 
C VS (I): VS OF Ith LAYER      
C DEP(I): DEPTH (THICKNESS) OF Ith LAYER  
C NDOM : DIVISION NUMBER from 0 to MAX OMEGA TO GET CUT-OFF OMEGA
C NEMD : HIGHEST OUTPUT MODE OF LOVE WAVE
C MMD  : NMD IN MAIN PROGRAM DIMENSION
C ** OUTPUT DATA
C CFOM(I): CUT-OFF OMEGA OF Ith MODE LOVE WAVE
C NCFOM  : NUMBER OF CUT-OFF OMEGA
C
      IMPLICIT REAL*8(A-H,O-Z)
      DIMENSION CFOM(0:MMD),DNS(ML),VS(ML),DEP(ML)
      EXTERNAL FA21
C
C *** 0 th MODE CUT-OFF OMEGA ***
C
      ICO=0
      CFOM(ICO)=0.0D0
      IF(NEMD.EQ.0)GOTO 40
C
C *** INCREMENT OF OMEGA ***
C
      DDOM=DFLOAT(NOM)*DOM/DFLOAT(NDOM)
C
C *** INPUT OF CONVERGENCE JUDGMENT CRITERIA FOR LVROOT ***
C
      DXEPS=100.0D0
      XEPSO=DDOM/DXEPS
      DFEPS=1000.0D0
      ITMAX=100
C     WRITE(*,*)
C     WRITE(*,*)'From SUBROUTINE LVCT :'
C     WRITE(*,*)
C     WRITE(*,*)'Convergence Criteria for Cut-Off Omega :'
C     WRITE(*,1000)XEPSO
C     WRITE(*,1010)DFEPS
C     WRITE(*,1020)ITMAX
C     WRITE(*,1030)
C     READ(*,*)IYES
C     IF(IYES.NE.0)THEN
C       WRITE(*,*)
C       WRITE(*,1040)
C       READ(*,*)XEPSO,DFEPS,ITMAX
C     END IF
C     WRITE(*,*)
C1000 FORMAT(/' XEPS = DDOM/DXEPS          : XEPS =',F15.10)
C1010 FORMAT( ' FEPS = DABS(FL1-FL2)/DFEPS : DFEPS =',F15.3)
C1020 FORMAT( ' Maximum Iteration Number   : ITMAX =',I15/)
C1030 FORMAT( ' Are These Criteria OK ? (yes = 0) :',\)
C1040 FORMAT( ' Enter New DDOM, DXEPS, DFEPS, ITMAX :',\)
C
C *** LOWER TRIAL OMEGA AND EIGEN VALUE EQUATION ***
C
      OM1=DDOM
      FL1=FA21(OM1,ML,NL,DNS,VS,DEP)
C
C *** LOWER TRIAL OMEGA CHECK ***
C
      ICO=1
      IFOM=2
      IF(FL1.EQ.0.0D0)THEN
        CFOM(1)=OM1
        IF(NEMD.EQ.1)GOTO 40
        OM1=2.0D0*DDOM
        FL1=FA21(OM1,ML,NL,DNS,VS,DEP)
        ICO=2
        IFOM=3
      ENDIF
C
C *** SEARCH OF CUT-OFF OMEGA OF HIGHER LOVE MODE ***
C
      DO 100 IOM=IFOM,NDOM
C
C  ** HIGHER TRIAL OMEGA AND EIGEN VALUE EQUATION **
C
        OM2=DFLOAT(IOM)*DDOM
        FL2=FA21(OM2,ML,NL,DNS,VS,DEP)
C
C  ** LOWER AND HIGER OMEGA CHECK **
C
        IF(FL1*FL2)10,20,30
C
C   * CASE OF LOWER OM < CUT-OFF OM < HIGHER OM *
C
C     * SEARCH OF CUT-OFF OM BY D2ROOT *
C
   10     XEPS=XEPSO
          FEPS=DABS(FL1-FL2)/DFEPS
          ITR=ITMAX
          OML=OM1
          OMR=OM2
          CALL LVROOT(FA21,ML,NL,DNS,VS,DEP
     *               ,OML,OMR,XEPS,FEPS,ITR,OMLR,IER)
C
C     * D2ROOT ERROR OR NOT CONVERGENCE WORK *
C
          IF(IER.NE.0)THEN
            WRITE(*,*)
            WRITE(*,*)' FROM SUBROUTINE LVCT :'
            WRITE(*,*)' D2ROOT ERROR, IER =',IER
            WRITE(*,*)' XEPS,FEPS,ITMAX =',XEPS,FEPS,ITR
            WRITE(*,*)' OM1,OM2 =',OM1,OM2
            WRITE(*,*)' FL1,FL2 =',FL1,FL2
            IF(IER.EQ.3000)THEN
              WRITE(*,*)' NOT CONVERGENCE (IER=3000) !!!'
              WRITE(*,*)' OML,OMR =',OML,OMR
              FLL=FA21(OML,ML,NL,DNS,VS,DEP)
              FLR=FA21(OMR,ML,NL,DNS,VS,DEP)
              WRITE(*,*)' FLL,FLR =',FLL,FLR
              WRITE(*,*)' OMEGA IS CHANGED BY MEAN OMEGA !!!'
              OMLR=(OMR+OML)/2.0D0
              FLLR=FA21(OMLR,ML,NL,DNS,VS,DEP)
              WRITE(*,*)' NEW OM, FL =',OMLR,FLLR
            ELSE
              WRITE(*,*)' IER=2000 INPUT DATA ERROR  !!!'
              WRITE(*,*)' IER=2100 FL1*FL2 > 0       !!!'
              WRITE(*,*)' STOP  !!!'
              STOP
            ENDIF
          ENDIF
C
C     * ENTER OF CUT-OFF OMEGA *
C
          CFOM(ICO)=OMLR
          IF(NEMD.EQ.ICO)GOTO 40
          ICO=ICO+1
          GOTO 30
C
C   * CASE OF HIGER TRIAL OM = CUT-OFF OM *
C
   20     CFOM(ICO)=OM2
          IF(NEMD.EQ.ICO)GOTO 40
          ICO=ICO+1
          OM2=OM1+DDOM/2.0D0
          FL2=FA21(OM2,ML,NL,DNS,VS,DEP)
C
C   * CASE OF LOWER OM < HIGHER OM < CUT-OFF OM *
C
C     * HIGHER OM ==> LOWER OM AND NEXT STEP *
C
   30     OM1=OM2
          FL1=FL2
C
  100 CONTINUE
C
C *** NUMBER OF CUT-OFF OMEGA ***
C
      NCFOM=ICO-1
      RETURN
C
   40 NCFOM=ICO
C
      RETURN
      END
C
C
C *** PHASE VELOCITY (CC) OF LOVE WAVE
C
      SUBROUTINE LVCC(DOM,NOM,MOM,ML,NL,DNS,VS,DEP
     *               ,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 ML     : NL IN MAIN 
C NL     : TOTAL NUMBER OF LAYERS           
C DNS(I) : DENSITY OF Ith LAYER 
C VS (I) : VS OF Ith LAYER      
C DEP(I) : DEPTH (THICKNESS) OF Ith LAYER  
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 / MINMAX / VSMIN, VSMAX / OMEGA /OM
      DIMENSION CC(MOM,0:MMD),CFOM(0:MMD),NMODE(MOM)
     *         ,DNS(ML),VS(ML),DEP(ML)
      EXTERNAL FNFL
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 LVROOT ***
C
      DCEPS=1000.0D0
      CEPSO=DC/DCEPS
      DFEPS=100000.0D0
      ITMAX=100
      WRITE(*,*)
      WRITE(*,*)'From SUBROUTINE LVCC :'
      WRITE(*,*)
C     WRITE(*,*)'Convergence Criteria for Phase Velocity :'
C     WRITE(*,1000)CEPSO
C     WRITE(*,1010)DFEPS
C     WRITE(*,1020)ITMAX
C     WRITE(*,1030)
C     READ(*,*)IYES
C     IF(IYES.NE.0)THEN
C       WRITE(*,*)
C       WRITE(*,1040)
C       READ(*,*)CEPSO,DFEPS,ITMAX
C     END IF
C     WRITE(*,*)
C     WRITE(*,*)
C1000 FORMAT(/' CEPS = DC/DCEPS            : CEPS  =',F15.10)
C1010 FORMAT( ' FEPS = DABS(FL1-FL2)/DFEPS : DFEPS =',F15.3)
C1020 FORMAT( ' Maximum Iteration Number   : ITMAX =',I15/)
C1030 FORMAT( ' Are These Criteria OK ? (yes = 0) :',\)
C1040 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=FNFL(CC1,ML,NL,DNS,VS,DEP)
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=FNFL(CC1,ML,NL,DNS,VS,DEP)
          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=FNFL(CC2,ML,NL,DNS,VS,DEP)
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 LVROOT *
C
   10     CEPS=CEPSO
          FEPS=DABS(FL1-FL2)/DFEPS
          ITR=ITMAX
          CL=CC1
          CR=CC2
          CALL LVROOT(FNFL,ML,NL,DNS,VS,DEP
     *               ,CL,CR,CEPS,FEPS,ITR,CCLR,IER)
C
C       * LVROOT ERROR OR NOT CONVERGENCE WORK *
C
          IF(IER.NE.0)THEN
            WRITE(*,*)
            WRITE(*,*)' FROM SUBROUTINE LVCC :'
            WRITE(*,*)' LVROOT 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=FNFL(CL,ML,NL,DNS,VS,DEP)
              FLR=FNFL(CR,ML,NL,DNS,VS,DEP)
              WRITE(*,*)' FLL,FLR =',FLL,FLR
              WRITE(*,*)' CC IS CHANGED BY MEAN CC !!!'
              CCLR=(CR+CL)/2.0D0
              FLLR=FNFL(CCLR,ML,NL,DNS,VS,DEP)
              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 LVROOT *
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=FNFL(CC2,ML,NL,DNS,VS,DEP)
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
C
C
C *** EIGEN VALUE EQUATION OF LOVE WAVE, IN CASE OF C=VS(NL)
C
      REAL FUNCTION FA21*8 (OM,ML,NL,DNS,VS,DEP)
C
C INPUT DATA
C OM    : CIRCULAR FREQUENCY (OMEGA)
C ML    : NL IN MAIN 
C NL    : TOTAL NUMBER OF LAYERS           
C DNS(I): DENSITY OF Ith LAYER 
C VS (I): VS OF Ith LAYER      
C DEP(I): DEPTH (THICKNESS) OF Ith LAYER  
C OUTPUT DATA
C FA21  : EIGEN VALUE EQUATION OF LOVE WAVE, IN CASE OF C=VS(NL)
C
      IMPLICIT REAL*8(A-H,O-Z)
      DIMENSION DNS(ML),VS(ML),DEP(ML)
      COMPLEX*16 R11,R12,R21,R22,SA11,SA12,SA21,SA22
     *         ,SB11,SB12,SB21,SB22
C
      BN=VS(NL)
      WN=OM/BN
C
      R11=(1.0D0,0.0D0)
      R12=(0.0D0,0.0D0)
      R21=(0.0D0,0.0D0)
      R22=(1.0D0,0.0D0)
C
      DO 100 IL=NL-1,1,-1
C
        SA11=R11
        SA12=R12
        SA21=R21
        SA22=R22
C
        BM=VS(IL)
        RBM=DSQRT(BN*BN/(BM*BM)-1.0D0)
        QM=WN*RBM*DEP(IL)
        CQ=DCOS(QM)
        SQ=DSIN(QM)
C
        SM=DNS(IL)*VS(IL)*VS(IL)
        SB11=DCMPLX(CQ,0.0D0)
        SB12=DCMPLX(0.0D0,SQ/(SM*RBM))
        SB21=DCMPLX(0.0D0,SQ*SM*RBM)
        SB22=DCMPLX(CQ,0.0D0)
C
        R11=SA11*SB11+SA12*SB21
        R12=SA11*SB12+SA12*SB22
        R21=SA21*SB11+SA22*SB21
        R22=SA21*SB12+SA22*SB22
C
  100 CONTINUE
C
      FA21=IMAG(R21)
C
      RETURN
      END
C
C
C *** EIGEN VALUE EQUATION OF LOVE WAVE
C
      REAL FUNCTION FNFL*8 (CC,ML,NL,DNS,VS,DEP)
C
C INPUT DATA
C OM     : CIRCULAR FREQUENCY (OMEGA) FROM COMMON
C CC     : PHASE VELOCITY OF LOVE WAVE
C ML     : NL IN MAIN 
C NL     : TOTAL NUMBER OF LAYERS           
C DNS(I) : DENSITY OF Ith LAYER 
C VS (I) : VS OF Ith LAYER      
C DEP(I) : DEPTH (THICKNESS) OF Ith LAYER  
C OUTPUT DATA
C FNFL   : EIGEN VALUE EQUATION OF LOVE WAVE
C
      IMPLICIT REAL*8(A-H,O-Z)
      DIMENSION DNS(ML),VS(ML),DEP(ML)
      COMMON / OMEGA /OM
      COMPLEX*16 R11,R12,R21,R22,SA11,SA12,SA21,SA22
     *          ,SB11,SB12,SB21,SB22
C
      WN=OM/CC
C
      R11=(1.0D0,0.0D0)
      R12=(0.0D0,0.0D0)
      R21=(0.0D0,0.0D0)
      R22=(1.0D0,0.0D0)
C
      DO 100 IL=NL-1,1,-1
C
        SA11=R11
        SA12=R12
        SA21=R21
        SA22=R22
C
        BM=VS(IL)
        CB2=CC*CC/(BM*BM)
        IF(BM-CC)10,20,30
C
   10     RBM=DSQRT(CB2-1.0D0)
          QM=WN*RBM*DEP(IL)
          CQ=DCOS(QM)
          SQ=DSIN(QM)
          STQ=RBM*SQ
          SDQ=SQ/RBM
          GOTO 40
C
   20     CQ=1.0D0
          STQ=0.0D0
          SDQ=WN*DEP(IL)
          GOTO 40
C
   30     RBM=DSQRT(1.0D0-CB2)
          QM=WN*RBM*DEP(IL)
          EX1=DEXP(QM)
          EX2=DEXP(-QM)
          CQ=(EX1+EX2)/2.0D0
          EX12=(EX1-EX2)/2.0D0
          STQ=-RBM*EX12
          SDQ=EX12/RBM
C
   40   SM=DNS(IL)*VS(IL)*VS(IL)
        SB11=DCMPLX(CQ,0.0D0)
        SB12=DCMPLX(0.0D0,SDQ/SM)
        SB21=DCMPLX(0.0D0,STQ*SM)
        SB22=DCMPLX(CQ,0.0D0)
C
        R11=SA11*SB11+SA12*SB21
        R12=SA11*SB12+SA12*SB22
        R21=SA21*SB11+SA22*SB21
        R22=SA21*SB12+SA22*SB22
C
  100 CONTINUE
C
      RBN=-DSQRT(1.0D0-CC*CC/(VS(NL)*VS(NL)))
      SM=DNS(NL)*VS(NL)*VS(NL)
      FNFL=IMAG(R21)+SM*RBN*REAL(R11)
C
      RETURN
      END
C
C
C *** ROOT FINDING OF FUNC(X)= 0 BY TWO DIVISION AND LINEAR
C     INTERPOLATION COMBINED METHOD
C *** SOURCE PROGRAM IS $DNTBM (MSL2) AND ARRENGED IN 1988
C
      SUBROUTINE LVROOT(FUNC,ML,NL,DNS,VS,DEP
     *                 ,XL,XR,XEPS,FEPS,ITMAX,X,IER)
C
C ** INPUT DATA **
C FUNC  : FUNCTION OF X (NEED EXTERNAL IN MAIN PROGRAM !!!)
C ML    : NL IN MAIN 
C NL    : TOTAL NUMBER OF LAYERS           
C DNS(I): DENSITY OF Ith LAYER 
C VS (I): VS OF Ith LAYER      
C DEP(I): DEPTH (THICKNESS) OF Ith LAYER  
C XL    : LEFT(LOWER LIMIT) X (XL<XS)
C XR    : RIGHT(HIGHER LIMIT) X (XS<XR)
C XEPS  : CRITERION VALUE TO JUGDE CONVERGENCE OF X (XEPS>0)
C FEPS  : CRITERION VALUE TO JUGDE CONVERGENCE OF FUNC(X) (FEPS>0)
C ITMAX : MAXIMUM ITERATION NUMBER
C ** OUTPUT DATA **
C ITMAX : FINAL ITERATION NUMBER IN THE SUBROUTINE
C X     : ROOT (SOLUSION) OF FUNC(X)=0
C IER   : RETURN CODE (IF ERROR, IER.NE.0.  SEE $DNTBM,MSL2)
C
      IMPLICIT REAL*8(A-H,O-Z)
      DIMENSION DNS(ML),VS(ML),DEP(ML)
C
C *** INPUT DATA CHECK ***
C
      IER = 0
      IF(XEPS.LT.0.0D0 .OR. FEPS.LT.0.0D0 .OR. ITMAX.LT.1) GO TO 260
C
      N   = 1
      X   = XL
      FL  = FUNC(X,ML,NL,DNS,VS,DEP)
      IF(FL) 100,555,100
  100 X   = XR
      FR  =  FUNC(X,ML,NL,DNS,VS,DEP)
      IF(FR)110,555,110
  110 IF(FR*FL) 130,555,120
  120 X   = (XL+XR)*.5D0
  125 CONTINUE
      IER=2100
      N   = ITMAX
      GO TO 555
C
C *** ROOT FINDING OF FUNC(X,ML,NL,DNS,VS,DEP)= 0 ***
C
  130 DO  250 N=1,ITMAX
C
C   * TRIAL X AND FUNC(X,ML,NL,DNS,VS,DEP)BY TWO DIVISION METHOD *
C
        X   = (XL+XR)*.5D0
        F   = FUNC(X,ML,NL,DNS,VS,DEP)
C
C   * REPLACE OF XL OR XR BY TRIAL X *
C
        IF(F) 140,555,140
C
  140     IF(F*FL) 150,555,160
C
  150       XR  = X
            FR  = F
            GO TO 170
  160       XL  = X
            FL  = F
C
C   * TRIAL X AND FUNC(X,ML,NL,DNS,VS,DEP)BY LINEAR INTERPOLATION METHOD *
C
  170   DF  = FL-FR
        DX  = (XR-XL)*FL
        DX  = DX/DF
        X   = XL+DX
        F   = FUNC(X,ML,NL,DNS,VS,DEP)
C
C   * REPLACE OF XL OR XR BY TRIAL X *
C
        IF(F) 175,555,175
C
  175     IF(FL*F) 180,190,190
C
  180       XR  = X
            FR  = F
            GO TO 200
  190       XL  = X
            FL  = F
C
C   * CONVERGENCE JUDGMENT OF TRIAL X AND FUNC(X,ML,NL,DNS,VS,DEP) *
C
  200   SX  = XR-XL
        IF(ABS(X).LE.1.0D0) GO TO 210
          SX  = SX/X
  210   IF(ABS(SX)-XEPS) 220,220,250
  220     X   = XR
        IF(ABS(FR)-FEPS) 555,555,230
  230     X   = XL
        IF(ABS(FL)-FEPS) 555,555,250
C
  250 CONTINUE
C
C *** ITERATION OVER ERROR ***
C
      IER=3000
      N = ITMAX
      GO TO 555
C
C *** INPUT DATA ERROR ***
C
  260 IER=2000
      N = ITMAX
C
C *** FINAL WORK ***
C
  555 CONTINUE
      ITMAX = N
C
      RETURN
      END

