



      SUBROUTINE MCXINI (START,XNUM,XNUMAMB,LOGPOS)




















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INCLUDE       'MCXINC.INC'
      INTEGER       START(MAXAA), XNUM, XNUMAMB(MAXAA), IAMB
      REAL          LOGPOS







      IF (XNUM.GT.3*MAXAA) THEN
         CALL GVSTTB ('?NEWB137')
         LOGPOS=0.0
         NUM=0
         GOTO 998
      END IF



      NUM=XNUM
      CUMPROB(0)=0.0



      DO 10 IAMB=1,NUM
         POS(IAMB)=START(IAMB)
         NUMAMB(IAMB)=XNUMAMB(IAMB)
         CUMPROB(IAMB)=CUMPROB(IAMB-1)+LOG(FLOAT(NUMAMB(IAMB)))/LOG(10.)
 10   CONTINUE
      LOGPOS=CUMPROB(NUM)
      VALUE=0.0
      VALDON=.FALSE.
      IF (LOGPOS.GT.250.0) THEN
         CALL GVSTTR ('?M4523',LOGPOS)
         LOGPOS=250.0
      END IF



  998 CONTINUE


      RETURN
      END
      SUBROUTINE MCXRUN (NSTEP,TFAC,FITFUNC,SETPROC,FINAL,FRACC)



















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INCLUDE       'MCXINC.INC'
      INTEGER       NSTEP, IAMB, OLDPOS, NEWPOS, IACCEPT, IREJECT, 
     +              GVFIRN, ISTEP, ICUT1, ICUT2
      REAL          FITFUNC, FINAL, TFAC, NEWVAL, DELTA, GVFRND, 
     +              CUT, FRACC
      EXTERNAL      FITFUNC, SETPROC







      IF (.NOT.VALDON) THEN
         DO 3 IAMB=1,NUM
            IF (NUMAMB(IAMB).GT.1) CALL SETPROC (IAMB,POS(IAMB))
 3       CONTINUE
         VALUE=FITFUNC (0)
         VALDON=.TRUE.
      END IF
      IACCEPT=0
      IREJECT=0
      IF (TFAC.LT.0) THEN
         CALL GVSTTB ('?NEWB138')
         goto 998
      END IF



      DO 100 ISTEP=1,NSTEP



 5       CONTINUE
         CUT=GVFRND(0)*CUMPROB(NUM)



         ICUT1=0
         ICUT2=NUM
 10      CONTINUE
         IAMB=(ICUT1+ICUT2)/2
         IF (CUMPROB(IAMB).LT.CUT) THEN
            ICUT1=IAMB
         ELSE
            ICUT2=IAMB
         END IF
         IF (ICUT2-ICUT1.GT.1) GOTO 10
         IAMB=ICUT2



         IF (NUMAMB(IAMB).EQ.1) THEN
            CALL GVSTTB ('?NEWB139')
            GOTO 5
         END IF



         NEWPOS=1+GVFIRN(NUMAMB(IAMB)-1)
         IF (NEWPOS.GE.POS(IAMB)) NEWPOS=NEWPOS+1



         OLDPOS=POS(IAMB)
         POS(IAMB)=NEWPOS
         CALL SETPROC (IAMB,POS(IAMB))
         NEWVAL=FITFUNC(0)
         DELTA=(NEWVAL-VALUE)



         IF (DELTA.GE.-TFAC) THEN
            VALUE=NEWVAL
            IACCEPT=IACCEPT+1
         ELSE
            POS(IAMB)=OLDPOS
            CALL SETPROC (IAMB,POS(IAMB))
            NEWVAL=FITFUNC(0)
            IF (ABS(NEWVAL-VALUE).GT.0.002) THEN
               CALL GVSTTB ('?NEWB140')
               CALL GVSTTR ('?M3063',NEWVAL)
               CALL GVSTTR ('?M3064',VALUE)
               CALL GVSTTI ('?M3065',ISTEP)
               CALL GVSTTI2 ('?M3066',IAMB,POS(IAMB))
            END IF
            VALUE=NEWVAL
            IREJECT=IREJECT+1
         END IF
 100  CONTINUE
      FINAL=VALUE
      FRACC=FLOAT(IACCEPT)/FLOAT(NSTEP)



  998 CONTINUE


      RETURN
      END
      SUBROUTINE MCXRES (XPOS)



















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INCLUDE       'MCXINC.INC'
      INTEGER       XPOS(*), IAMB





      DO 10 IAMB=1,NUM
         XPOS(IAMB)=POS(IAMB)
 10   CONTINUE





      RETURN
      END
