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

      INCLUDE '(DBLPRC)'
      INCLUDE '(DIMPAR)'
      INCLUDE '(IOUNIT)'
*
*----------------------------------------------------------------------*
*                                                                      *
*     Copyright (C) 1990-2006      by    Alfredo Ferrari & Paola Sala  *
*     All Rights Reserved.                                             *
*                                                                      *
*                                                                      *
*     New source for FLUKA9x-FLUKA200x:                                *
*                                                                      *
*     Created on 07 january 1990   by    Alfredo Ferrari & Paola Sala  *
*                                                   Infn - Milan       *
*                                                                      *
*     Last change on 03-mar-06     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.            *
*                                                                      *
*----------------------------------------------------------------------*
*
      INCLUDE '(BEAMCM)'
      INCLUDE '(FHEAVY)'
      INCLUDE '(FLKSTK)'
      INCLUDE '(IOIOCM)'
      INCLUDE '(LTCLCM)'
      INCLUDE '(PAPROP)'
      INCLUDE '(SOURCM)'
      INCLUDE '(SUMCOU)'
*
      INCLUDE '(CASLIM)'
*
      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 ***
      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.
*  |
*  +-------------------------------------------------------------------*
*  |  Normal hadron:
      ELSE
         IONID = IJBEAM
         ILOFLK (NPFLKA) = IJBEAM
*  |  Flag this is prompt radiation
         LRADDC (NPFLKA) = .FALSE.
      END IF
*  |
*  +-------------------------------------------------------------------*
* From this point .....
* Particle generation (1 for primaries)
      LOFLK  (NPFLKA) = 1
* User dependent flag:
      LOUSE  (NPFLKA) = 0
* 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
* Group number for "low" energy neutrons, set to 0 anyway
      IGROUP (NPFLKA) = 0
* Kinetic energy of the particle (GeV)
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        
       XRNDM=FLRNDM(XRNDM)
       YRNDM=FLRNDM(YRNDM)
       ZRNDM=FLRNDM(ZRNDM)
       QRNDM=FLRNDM(QRNDM)
       WRNDM=FLRNDM(WRNDM)
      IF (YRNDM.LE.0.50) THEN
        GOTO 300
      ELSE IF (YRNDM.LE.0.70) THEN
        GOTO 400
      ELSE IF (YRNDM.LE.1.) THEN
        GOTO 500
      ENDIF
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        
*==============================================================
300   IF(XRNDM.LE.0.092) THEN
      TKEFLK (NPFLKA)=0.143
      ELSE IF(XRNDM.LE.0.184) THEN
      TKEFLK (NPFLKA)=0.146
      ELSE IF(XRNDM.LE.0.263) THEN
      TKEFLK (NPFLKA)=0.149
      ELSE IF(XRNDM.LE.0.336) THEN
      TKEFLK (NPFLKA)=0.152
      ELSE IF(XRNDM.LE.0.401) THEN
      TKEFLK (NPFLKA)=0.155
      ELSE IF(XRNDM.LE.0.475) THEN
      TKEFLK (NPFLKA)=0.158
      ELSE IF(XRNDM.LE.0.548) THEN
      TKEFLK (NPFLKA)=0.161
      ELSE IF(XRNDM.LE.0.622) THEN
      TKEFLK (NPFLKA)=0.164
      ELSE IF(XRNDM.LE.0.691) THEN
      TKEFLK (NPFLKA)=0.167
      ELSE IF(XRNDM.LE.0.760) THEN
      TKEFLK (NPFLKA)=0.170
      ELSE IF(XRNDM.LE.0.834) THEN
      TKEFLK (NPFLKA)=0.173
      ELSE IF(XRNDM.LE.0.908) THEN
      TKEFLK (NPFLKA)=0.176
      ELSE IF(XRNDM.LE.1.) THEN
      TKEFLK (NPFLKA)=0.179
      ENDIF
*
*==============================================*
      IF (ZRNDM.LT.0.5) THEN
      XFLK   (NPFLKA) = -50.D0
      TXFLK  (NPFLKA) = 1.D0
      ELSE
      XFLK   (NPFLKA) = 50.D0
      TXFLK  (NPFLKA) = -1.D0
      ENDIF
      YFLK   (NPFLKA) = -5.D0 + 1.7D0*QRNDM
      ZFLK   (NPFLKA) = 0.6D0 + 4.8D0*WRNDM
* Cosines (tx,ty,tz)
      TYFLK  (NPFLKA) = 0.D0
      TZFLK  (NPFLKA) = 0.D0
*
      GOTO 1000      
*===========================================================
400   IF(XRNDM.LE.0.083) THEN
      TKEFLK (NPFLKA)=0.127
      ELSE IF(XRNDM.LE.0.165) THEN
      TKEFLK (NPFLKA)=0.130
      ELSE IF(XRNDM.LE.0.239) THEN
      TKEFLK (NPFLKA)=0.133
      ELSE IF(XRNDM.LE.0.312) THEN
      TKEFLK (NPFLKA)=0.136
      ELSE IF(XRNDM.LE.0.376) THEN
      TKEFLK (NPFLKA)=0.139
      ELSE IF(XRNDM.LE.0.440) THEN
      TKEFLK (NPFLKA)=0.142
      ELSE IF(XRNDM.LE.0.505) THEN
      TKEFLK (NPFLKA)=0.145
      ELSE IF(XRNDM.LE.0.560) THEN
      TKEFLK (NPFLKA)=0.148
      ELSE IF(XRNDM.LE.0.615) THEN
      TKEFLK (NPFLKA)=0.151
      ELSE IF(XRNDM.LE.0.670) THEN
      TKEFLK (NPFLKA)=0.154
      ELSE IF(XRNDM.LE.0.725) THEN
      TKEFLK (NPFLKA)=0.157
      ELSE IF(XRNDM.LE.0.789) THEN
      TKEFLK (NPFLKA)=0.160
      ELSE IF(XRNDM.LE.0.853) THEN
      TKEFLK (NPFLKA)=0.163
      ELSE IF(XRNDM.LE.0.927) THEN
      TKEFLK (NPFLKA)=0.166
      ELSE IF(XRNDM.LE.1.) THEN
      TKEFLK (NPFLKA)=0.169
      ENDIF
*
*===============================================*
      IF (ZRNDM.LT.0.5) THEN
      XFLK   (NPFLKA) = -50.D0
      TXFLK  (NPFLKA) = 1.D0
      ELSE
      XFLK   (NPFLKA) = 50.D0
      TXFLK  (NPFLKA) = -1.D0
      ENDIF
      YFLK   (NPFLKA) = -7.D0 + 2.D0*QRNDM
      ZFLK   (NPFLKA) = 0.4D0 + 5.2D0*WRNDM
* Cosines (tx,ty,tz)
      TYFLK  (NPFLKA) = 0.D0
      TZFLK  (NPFLKA) = 0.D0
*
      GOTO 1000      
*============================================
500   IF(XRNDM.LE.0.146) THEN
      TKEFLK (NPFLKA)=0.106
      ELSE IF(XRNDM.LE.0.292) THEN
      TKEFLK (NPFLKA)=0.109
      ELSE IF(XRNDM.LE.0.417) THEN
      TKEFLK (NPFLKA)=0.112
      ELSE IF(XRNDM.LE.0.521) THEN
      TKEFLK (NPFLKA)=0.115
      ELSE IF(XRNDM.LE.0.625) THEN
      TKEFLK (NPFLKA)=0.118
      ELSE IF(XRNDM.LE.0.708) THEN
      TKEFLK (NPFLKA)=0.121
      ELSE IF(XRNDM.LE.0.792) THEN
      TKEFLK (NPFLKA)=0.124
      ELSE IF(XRNDM.LE.0.854) THEN
      TKEFLK (NPFLKA)=0.127
      ELSE IF(XRNDM.LE.0.917) THEN
      TKEFLK (NPFLKA)=0.130
      ELSE IF(XRNDM.LE.0.958) THEN
      TKEFLK (NPFLKA)=0.133
      ELSE IF(XRNDM.LE.0.979) THEN
      TKEFLK (NPFLKA)=0.136
      ELSE IF(XRNDM.LE.1.) THEN
      TKEFLK (NPFLKA)=0.139
      ENDIF
*
*===============================================*
      IF (ZRNDM.LT.0.5) THEN
      XFLK   (NPFLKA) = -50.D0
      TXFLK  (NPFLKA) = 1.D0
      ELSE
      XFLK   (NPFLKA) = 50.D0
      TXFLK  (NPFLKA) = -1.D0
      ENDIF
      YFLK   (NPFLKA) = -8.7D0 + 1.7D0*QRNDM
      ZFLK   (NPFLKA) = 0.6D0 + 4.8D0*WRNDM
* Cosines (tx,ty,tz)
      TYFLK  (NPFLKA) = 0.D0
      TZFLK  (NPFLKA) = 0.D0
*
      GOTO 1000       
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%            
* Particle momentum
*      PMOFLK (NPFLKA) = PBEAM
1000  PMOFLK (NPFLKA) = SQRT ( TKEFLK (NPFLKA) * ( TKEFLK (NPFLKA)
     &                       + TWOTWO * AM (ILOFLK(NPFLKA)) ) )
* Polarization cosines:
      TXPOL  (NPFLKA) = -TWOTWO
      TYPOL  (NPFLKA) = +ZERZER
      TZPOL  (NPFLKA) = +ZERZER
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*  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
*=== End of subroutine Source =========================================*
      END

