*$ CREATE SOURCE.FOR
*COPY SOURCE
*
*=== source ===========================================================*
*
      SUBROUTINE SOURCE ( NOMORE )

      INCLUDE '(DBLPRC)'
      INCLUDE '(DIMPAR)'
      INCLUDE '(IOUNIT)'
*
*----------------------------------------------------------------------*
*                                                                      *
*     Copyright (C) 1990-2010      by    Alfredo Ferrari & Paola Sala  *
*     All Rights Reserved.                                             *
*                                                                      *
*                                                                      *
*     New source for FLUKA9x-FLUKA20xy:                                *
*                                                                      *
*     Created on 07 January 1990   by    Alfredo Ferrari & Paola Sala  *
*                                                   Infn - Milan       *
*                                                                      *
*     Last change on  17-Oct-10    by    Alfredo Ferrari               *
*                                                                      *
*  This is just an example of a possible user written source routine.  *
*  note that the beam card still has some meaning - in the scoring the *
*  maximum momentum used in deciding the binning is taken from the     *
*  beam momentum.  Other beam card parameters are obsolete.            *
*                                                                      *
*       Output variables:                                              *
*                                                                      *
*              Nomore = if > 0 the run will be terminated              *
*                                                                      *
*  Read source term from a histogram                                   *
*     Author: Vasilis.Vlachoudis@cern.ch                               *
*                                                                      *
*----------------------------------------------------------------------*
*
      INCLUDE '(BEAMCM)'
      INCLUDE '(FHEAVY)'
      INCLUDE '(FLKSTK)'
      INCLUDE '(IOIOCM)'
      INCLUDE '(LTCLCM)'
      INCLUDE '(PAPROP)'
      INCLUDE '(SOURCM)'
      INCLUDE '(SUMCOU)'
*
      PARAMETER (NMAX=1000)
      DIMENSION ERG(NMAX), CUM(NMAX)
      CHARACTER*250 LINE
      SAVE  N, ERG, CUM
*
      LOGICAL LFIRST
*
      SAVE LFIRST
      DATA LFIRST / .TRUE. /
*======================================================================*
*                                                                      *
*                 BASIC VERSION                                        *
*                                                                      *
*======================================================================*
      NOMORE = 0
*  +-------------------------------------------------------------------*
*  |  First call initializations:
      IF ( LFIRST ) THEN
*  |  *** The following 3 cards are mandatory ***
         TKESUM = ZERZER
         LFIRST = .FALSE.
         LUSSRC = .TRUE.
*  |  *** User initialization ***
         LUNRD = NINT(WHASOU(1))      ! Logical unit from input file
***   read histogram
***   as pairs Energy Value
***   WARNING first value should have be 0
***           in order to define the lower energy limit
         N = 0

         SUM   = ZERZER                 ! Build cumulative sum
         EPREV = ZERZER
 10      CONTINUE
             READ (LUNRD, '(A)', ERR=9999, END=20 ) LINE
             READ (LINE, *, ERR=10) E, H

             N = N + 1
             IF (N .GT. NMAX)
     &            CALL FLABRT('SOURCE','Please increase NMAX')
             IF (N .EQ. 1 .AND. ABS(H).GT.AZRZRZ)
     &            CALL FLABRT(
     &                 'SOURCE','ZERO was was expected as first value')

*** Create cummulative sum of events! dN=dE*V
             SUM = SUM + H*(E-EPREV)
             EPREV = E

             ERG(N) = E
             CUM(N) = SUM
         GO TO 10
 20      CONTINUE
         CLOSE (LUNRD)
      END IF
*  |
*  +-------------------------------------------------------------------*
*  Push one source particle to the stack. Note that you could as well
*  push many but this way we reserve a maximum amount of space in the
*  stack for the secondaries to be generated
*  Npflka is the stack counter: of course any time source is called it
*  must be =0
      NPFLKA = NPFLKA + 1
*  Wt is the weight of the particle
      WTFLK  (NPFLKA) = ONEONE
      WEIPRI = WEIPRI + WTFLK (NPFLKA)
*  Particle type (1=proton.....). Ijbeam is the type set by the BEAM
*  card
*  +-------------------------------------------------------------------*
*  |  (Radioactive) isotope:
      IF ( IJBEAM .EQ. -2 .AND. LRDBEA ) THEN
         IARES  = IPROA
         IZRES  = IPROZ
         IISRES = IPROM
         CALL STISBM ( IARES, IZRES, IISRES )
         IJHION = IPROZ  * 1000 + IPROA
         IJHION = IJHION * 100 + KXHEAV
         IONID  = IJHION
         CALL DCDION ( IONID )
         CALL SETION ( IONID )
*  |
*  +-------------------------------------------------------------------*
*  |  Heavy ion:
      ELSE IF ( IJBEAM .EQ. -2 ) THEN
         IJHION = IPROZ  * 1000 + IPROA
         IJHION = IJHION * 100 + KXHEAV
         IONID  = IJHION
         CALL DCDION ( IONID )
         CALL SETION ( IONID )
         ILOFLK (NPFLKA) = IJHION
*  |  Flag this is prompt radiation
         LRADDC (NPFLKA) = .FALSE.
*  |  Group number for "low" energy neutrons, set to 0 anyway
         IGROUP (NPFLKA) = 0
*  |
*  +-------------------------------------------------------------------*
*  |  Normal hadron:
      ELSE
         IONID = IJBEAM
         ILOFLK (NPFLKA) = IJBEAM
*  |  Flag this is prompt radiation
         LRADDC (NPFLKA) = .FALSE.
*  |  Group number for "low" energy neutrons, set to 0 anyway
         IGROUP (NPFLKA) = 0
      END IF
*  |
*  +-------------------------------------------------------------------*
*  From this point .....

*** Select a random energy interval
      C = CUM(N) * FLRNDM(C)
*** Find interval (no need to check first interval CUM=0)
      DO I=2,N
        IF (CUM(I) .GT. C) THEN
***         Found interval I, select a random energy inside
            E = ERG(I-1) + (ERG(I)-ERG(I-1))*FLRNDM(C)
            GO TO 90
        END IF
      END DO
*** It should never come here
      CALL FLABRT('SOURCE','C .GT. CMAX!')
 90   CONTINUE

*  Particle generation (1 for primaries)
      LOFLK  (NPFLKA) = 1
*  User dependent flag:
      LOUSE  (NPFLKA) = 0
*  No channeling:
      LCHFLK (NPFLKA) = .FALSE.
      DCHFLK (NPFLKA) = ZERZER
*  User dependent spare variables:
      DO 100 ISPR = 1, MKBMX1
         SPAREK (ISPR,NPFLKA) = ZERZER
 100  CONTINUE
*  User dependent spare flags:
      DO 200 ISPR = 1, MKBMX2
         ISPARK (ISPR,NPFLKA) = 0
 200  CONTINUE
*  Save the track number of the stack particle:
      ISPARK (MKBMX2,NPFLKA) = NPFLKA
      NPARMA = NPARMA + 1
      NUMPAR (NPFLKA) = NPARMA
      NEVENT (NPFLKA) = 0
      DFNEAR (NPFLKA) = +ZERZER
*  ... to this point: don't change anything
*  Particle age (s)
      AGESTK (NPFLKA) = +ZERZER
      AKNSHR (NPFLKA) = -TWOTWO
*  Kinetic energy of the particle (GeV)
*     TKEFLK (NPFLKA) = SQRT ( PBEAM**2 + AM (IONID)**2 ) - AM (IONID)
      TKEFLK (NPFLKA) = E * EMVGEV
*  Particle momentum
      PMOFLK (NPFLKA) = SQRT ( TKEFLK (NPFLKA) * ( TKEFLK (NPFLKA)
     &                       + TWOTWO * AM (IONID) ) )
*  Cosines (tx,ty,tz)
*************************************************************
******** FROM ALBERTO FASSO**********************************
*  Sample the cosine of the polar angle
*      COSTHE = TWOTWO * FLRNDM(energy) - ONEONE
*      SINTHE = SQRT(ONEONE - COSTHE**2)
*      IF(FLRNDM(energy) .LE. HLFHLF) SINTHE = -SINTHE
**  Samples the azimuthal angle
*      PHI = TWOPIP * FLRNDM(COSTHE)
*      COSPHI = COS(PHI)
**  Cosines (tx,ty,tz) (make sure they are properly normalized)
*      TZFLK (NPFLKA) = COSTHE
*      TYFLK (NPFLKA) = SINTHE * COSPHI
*      TXFLK (NPFLKA) = SQRT(ONEONE - TZFLK(NPFLKA)**2
*     &                             - TYFLK(NPFLKA)**2)
*      IF(FLRNDM(energy) .LE. HLFHLF)
*     &                      TXFLK(NPFLKA) = -TXFLK(NPFLKA)
*  commented by Kelly 05/03/10
*      CALL RACO(UUU,VVV,WWW)
*       TZFLK (NPFLKA) = 1.0
*       TYFLK (NPFLKA) = 0.0
*       TXFLK (NPFLKA) = 0.0
* Cosines (tx,ty,tz)
      TXFLK  (NPFLKA) = UBEAM
      TYFLK  (NPFLKA) = VBEAM
      TZFLK  (NPFLKA) = WBEAM
*     TZFLK  (NPFLKA) = SQRT ( ONEONE - TXFLK (NPFLKA)**2
*    &                       - TYFLK (NPFLKA)**2 )
*  Polarization cosines:
      TXPOL  (NPFLKA) = -TWOTWO
      TYPOL  (NPFLKA) = +ZERZER
      TZPOL  (NPFLKA) = +ZERZER
*  Particle coordinates
      DX = 4.0D+00
      DY = 4.0D+00
      XFLK   (NPFLKA) = XBEAM + DX * FLRNDM(energy) 
      YFLK   (NPFLKA) = YBEAM + DY + FLRNDM(energy) 
      ZFLK   (NPFLKA) = ZBEAM

*     XFLK   (NPFLKA) = XBEAM
*     YFLK   (NPFLKA) = YBEAM
*     ZFLK   (NPFLKA) = ZBEAM
*  Calculate the total kinetic energy of the primaries: don't change
      IF ( ILOFLK (NPFLKA) .EQ. -2 .OR. ILOFLK (NPFLKA) .GT. 100000 )
     &   THEN
         TKESUM = TKESUM + TKEFLK (NPFLKA) * WTFLK (NPFLKA)
      ELSE IF ( ILOFLK (NPFLKA) .NE. 0 ) THEN
         TKESUM = TKESUM + ( TKEFLK (NPFLKA) + AMDISC (ILOFLK(NPFLKA)) )
     &          * WTFLK (NPFLKA)
      ELSE
         TKESUM = TKESUM + TKEFLK (NPFLKA) * WTFLK (NPFLKA)
      END IF
      RADDLY (NPFLKA) = ZERZER
*  Here we ask for the region number of the hitting point.
*     NREG (NPFLKA) = ...
*  The following line makes the starting region search much more
*  robust if particles are starting very close to a boundary:
      CALL GEOCRS ( TXFLK (NPFLKA), TYFLK (NPFLKA), TZFLK (NPFLKA) )
      CALL GEOREG ( XFLK  (NPFLKA), YFLK  (NPFLKA), ZFLK  (NPFLKA),
     &              NRGFLK(NPFLKA), IDISC )
*  Do not change these cards:
      CALL GEOHSM ( NHSPNT (NPFLKA), 1, -11, MLATTC )
      NLATTC (NPFLKA) = MLATTC
      CMPATH (NPFLKA) = ZERZER
      CALL SOEVSV
      RETURN
 9999 CALL FLABRT('SOURCE', 'Error reading source file')
      RETURN
*=== End of subroutine Source =========================================*
      END
