      SUBROUTINE MAGFLD ( X, Y, Z, BTX, BTY, BTZ, B, NREG, IDISC )

      INCLUDE '(DBLPRC)'
      INCLUDE '(DIMPAR)'
      INCLUDE '(IOUNIT)'
*----------------------------------------------------------------------*
*                                                                      *
*     Input variables:                                                 *
*            x,y,z = current position                                  *
*            nreg  = current region                                    *
*     Output variables:                                                *
*            btx,bty,btz = cosines of the magn. field vector           *
*            B = magnetic field intensity (Tesla)                      *
*            idisc = set to 1 if the particle has to be discarded      *
*                                                                      *
*----------------------------------------------------------------------*
*    110.2008                                                          *
*    KK: magnetic field for smf21b.inp                                 *
*----------------------------------------------------------------------*
      IDISC = 0
      GO TO (1,2,3,4,5,6,7,8,9,10), NREG
*----------------------------------------------------------------------*
* Regions with magnetic field
*----------------------------------------------------------------------*
*
* Horn
  2   CONTINUE
   	 R = SQRT (X**2 + Y**2)
* Circular field
* Y/R,-X/R: focusses pbars
* -Y/R,X/R: defocusses pbars
         BTX = Y / R
         BTY = -X / R
         BTZ = 0.D+00
* B [T] = I [kA] / (50 * R [cm])
         B   = 8.D+00 / R
         RETURN
*
* Quad 1 & 2
  3   CONTINUE
  4   CONTINUE
         R = SQRT (X**2 + Y**2)
* G in T/cm, must be positive
	 G = 6.45D-2
* Quadrupole field
* +X/R,+Y/R: DF in X, F  in Y (for pbars)
* -X/R,-Y/R: F  in X, DF in Y (for pbars)
         BTX = -Y / R
         BTY = -X / R
         BTZ = 0.D+00
         B   = G * R
         RETURN
*
* Quad 3 & 4
  5   CONTINUE
  6   CONTINUE
         R = SQRT (X**2 + Y**2)
	 G = 4.54D-2
         BTX = Y / R
         BTY = X / R
         BTZ = 0.D+00
         B   = G * R
         RETURN
*
* Quad 5
  7   CONTINUE
         R = SQRT (X**2 + Y**2)
	 G = 2.17D-2
         BTX = -Y / R
         BTY = -X / R
         BTZ = 0.D+00
         B   = G * R
         RETURN  
*
* Dipole 1
  8   CONTINUE
         BTX = 0.D+00
         BTY = 1.D+00
         BTZ = 0.D+00
         B   = 1.6D+00
         RETURN  
*
* Dipole 2
  9   CONTINUE
         BTX = 0.D+00
         BTY = -1.D+00
         BTZ = 0.D+00
         B   = 1.6D+00
         RETURN  
*
* Dipole 3
 10      CONTINUE
         BTX = 0.D+00
         BTY = 1.D+00
         BTZ = 0.D+00
         B   = 1.6D+00
         RETURN  
*----------------------------------------------------------------------*
* Regions without magnetic field, 
* if called, write error messege and discard particle
*----------------------------------------------------------------------*
   1  CONTINUE
      WRITE (LUNOUT,*)
     &      ' Magfld called in zone ',NREG,' where there should'
      WRITE (LUNOUT,*)
     &      ' be no magnetic field. Something is wrong'
      IDISC = 1
      RETURN
*=== End of subroutine magfld =========================================*
      END

