*$ 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.            *
*                                                                      *
*----------------------------------------------------------------------*
*  Version for isotropic Heavy ion source
*
      DIMENSION  CUMENE(0:98), ENELOW(99), ENEUP(99) 
*----------------------------------------------------------------------*
      INCLUDE '(BEAMCM)'
      INCLUDE '(FHEAVY)'
      INCLUDE '(FLKSTK)'
      INCLUDE '(IOIOCM)'
      INCLUDE '(LTCLCM)'
      INCLUDE '(PAPROP)'
      INCLUDE '(SOURCM)'
      INCLUDE '(SUMCOU)'
*
      LOGICAL LFIRST
*
      SAVE LFIRST
      DATA LFIRST / .TRUE. /
*======================================================================*
*                                                                      *
*                 BASIC VERSION                                        *
*                                                                      *
*======================================================================*
*    Heavy ion energy group(Gev/n)
      DATA ENELOW /
     & 1.00000D-03, 1.14980D-03, 1.32190D-03, 1.51990D-03, 1.74750D-03,
     & 2.00920D-03, 2.31010D-03, 2.65610D-03, 3.05390D-03, 3.51120D-03,
     & 4.03700D-03, 4.64160D-03, 5.33670D-03, 6.13590D-03, 7.05480D-03,
     & 8.11130D-03, 9.32600D-03, 1.07230D-02, 1.23280D-02, 1.41750D-02,
     & 1.62980D-02, 1.87380D-02, 2.15440D-02, 2.47710D-02, 2.84800D-02,
     & 3.27450D-02, 3.76490D-02, 4.32880D-02, 4.97700D-02, 5.72240D-02,
     & 6.57930D-02, 7.56460D-02, 8.69750D-02, 1.00000D-01, 1.14980D-01,
     & 1.32190D-01, 1.51990D-01, 1.74750D-01, 2.00920D-01, 2.31010D-01,
     & 2.65610D-01, 3.05390D-01, 3.51120D-01, 4.03700D-01, 4.64160D-01,
     & 5.33670D-01, 6.13590D-01, 7.05480D-01, 8.11130D-01, 9.32600D-01,
     & 1.07230D+00, 1.23280D+00, 1.41750D+00, 1.62980D+00, 1.87380D+00,
     & 2.15440D+00, 2.47710D+00, 2.84800D+00, 3.27450D+00, 3.76490D+00,
     & 4.32880D+00, 4.97700D+00, 5.72240D+00, 6.57930D+00, 7.56460D+00,
     & 8.69750D+00, 1.00000D+01, 1.14980D+01, 1.32190D+01, 1.51990D+01,
     & 1.74750D+01, 2.00920D+01, 2.31010D+01, 2.65610D+01, 3.05390D+01,
     & 3.51120D+01, 4.03700D+01, 4.64160D+01, 5.33670D+01, 6.13590D+01,
     & 7.05480D+01, 8.11130D+01, 9.32600D+01, 1.07230D+02, 1.23280D+02,
     & 1.41750D+02, 1.62980D+02, 1.87380D+02, 2.15440D+02, 2.47710D+02,
     & 2.84800D+02, 3.27450D+02, 3.76490D+02, 4.32880D+02, 4.97700D+02,
     & 5.72240D+02, 6.57930D+02, 7.56460D+02, 8.69750D+02/
      DATA ENEUP /
     & 1.14980D-03, 1.32190D-03, 1.51990D-03, 1.74750D-03, 2.00920D-03,
     & 2.31010D-03, 2.65610D-03, 3.05390D-03, 3.51120D-03, 4.03700D-03,
     & 4.64160D-03, 5.33670D-03, 6.13590D-03, 7.05480D-03, 8.11130D-03,
     & 9.32600D-03, 1.07230D-02, 1.23280D-02, 1.41750D-02, 1.62980D-02,
     & 1.87380D-02, 2.15440D-02, 2.47710D-02, 2.84800D-02, 3.27450D-02,
     & 3.76490D-02, 4.32880D-02, 4.97700D-02, 5.72240D-02, 6.57930D-02,
     & 7.56460D-02, 8.69750D-02, 1.00000D-01, 1.14980D-01, 1.32190D-01,
     & 1.51990D-01, 1.74750D-01, 2.00920D-01, 2.31010D-01, 2.65610D-01,
     & 3.05390D-01, 3.51120D-01, 4.03700D-01, 4.64160D-01, 5.33670D-01,
     & 6.13590D-01, 7.05480D-01, 8.11130D-01, 9.32600D-01, 1.07230D+00,
     & 1.23280D+00, 1.41750D+00, 1.62980D+00, 1.87380D+00, 2.15440D+00,
     & 2.47710D+00, 2.84800D+00, 3.27450D+00, 3.76490D+00, 4.32880D+00,
     & 4.97700D+00, 5.72240D+00, 6.57930D+00, 7.56460D+00, 8.69750D+00,
     & 1.00000D+01, 1.14980D+01, 1.32190D+01, 1.51990D+01, 1.74750D+01,
     & 2.00920D+01, 2.31010D+01, 2.65610D+01, 3.05390D+01, 3.51120D+01,
     & 4.03700D+01, 4.64160D+01, 5.33670D+01, 6.13590D+01, 7.05480D+01,
     & 8.11130D+01, 9.32600D+01, 1.07230D+02, 1.23280D+02, 1.41750D+02,
     & 1.62980D+02, 1.87380D+02, 2.15440D+02, 2.47710D+02, 2.84800D+02,
     & 3.27450D+02, 3.76490D+02, 4.32880D+02, 4.97700D+02, 5.72240D+02,
     & 6.57930D+02, 7.56460D+02, 8.69750D+02, 1.00000D+03/
  
*    Normalized spectrum
      DATA CUMENE / 0.D0,
     & 3.71411D-06, 6.53586D-06, 1.02889D-05, 1.52822D-05, 2.19254D-05,
     & 3.07653D-05, 4.25275D-05, 5.81795D-05, 7.90149D-05, 1.06756D-04,
     & 1.43693D-04, 1.92885D-04, 2.58405D-04, 3.45674D-04, 4.61897D-04,
     & 6.16684D-04, 8.22512D-04, 1.09644D-03, 1.46011D-03, 1.94213D-03,
     & 2.57991D-03, 3.42153D-03, 4.52756D-03, 5.97561D-03, 7.86199D-03,
     & 1.03055D-02, 1.34490D-02, 1.74656D-02, 2.25554D-02, 2.89512D-02,
     & 3.69136D-02, 4.67285D-02, 5.87052D-02, 7.31455D-02, 9.03820D-02,
     & 1.10695D-01, 1.34346D-01, 1.61529D-01, 1.92361D-01, 2.26848D-01,
     & 2.64871D-01, 3.06189D-01, 3.50413D-01, 3.97007D-01, 4.45323D-01,
     & 4.94612D-01, 5.44066D-01, 5.92860D-01, 6.40214D-01, 6.85355D-01,
     & 7.27742D-01, 7.66853D-01, 8.02364D-01, 8.34126D-01, 8.62114D-01,
     & 8.86415D-01, 9.07241D-01, 9.24863D-01, 9.39599D-01, 9.51782D-01,
     & 9.61758D-01, 9.69847D-01, 9.76354D-01, 9.81547D-01, 9.85662D-01,
     & 9.88905D-01, 9.91443D-01, 9.93422D-01, 9.94958D-01, 9.96144D-01,
     & 9.97058D-01, 9.97760D-01, 9.98297D-01, 9.98708D-01, 9.99021D-01,
     & 9.99259D-01, 9.99440D-01, 9.99577D-01, 9.99681D-01, 9.99759D-01,
     & 9.99819D-01, 9.99864D-01, 9.99898D-01, 9.99923D-01, 9.99943D-01,
     & 9.99957D-01, 9.99968D-01, 9.99976D-01, 9.99982D-01, 9.99987D-01,
     & 9.99991D-01, 9.99993D-01, 9.99995D-01, 9.99996D-01, 9.99997D-01,
     & 9.99998D-01, 9.99999D-01, 1.00000D+00/
*  +-------------------------------------------------------------------*   
      NOMORE = 0
*  +-------------------------------------------------------------------*
*  |  First call initializations:
      IF ( LFIRST ) THEN
*  |  *** The following 3 cards are mandatory ***
         TKESUM = ZERZER
         LFIRST = .FALSE.
         LUSSRC = .TRUE.
*  |  *** User initialization ***
         write(lunout,*)
         write(lunout,*) '--->modified source.f for Alpha particle'
         write(lunout,*) ' in solar min '          
      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
*  +-------------------------------------------------------------------*
* Sample the energy group
      XI = FLRNDM(DUMMY)
      DO 500 K = 1 ,99
         IF (XI .LE. CUMENE(K)) THEN
            ENERGY = ((ENEUP(K) + ENELOW(K))/ TWOTWO) * 4.D0
            GO TO 600
         END IF
 500   CONTINUE
       STOP ' Failed to sample the energy group '
 600   CONTINUE
       count=count+1
      NPFLKA = NPFLKA + 1
      WTFLK (NPFLKA) = ONEONE
      WEIPRI = WEIPRI + WTFLK (NPFLKA)
*  +--------------------------------------------------------------------*
*  |  (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)
      TKEFLK (NPFLKA) = ENERGY
*      TKEFLK (NPFLKA) = SQRT ( PBEAM**2 + AM (IONID)**2 ) - AM (IONID)
* Particle momentum
*      PMOFLK (NPFLKA) = PBEAM
      PMOFLK (NPFLKA) = SQRT ( TKEFLK (NPFLKA) * ( TKEFLK (NPFLKA)
     *                      + TWOTWO * AM (IONID) ) )
*   +------------------------------------------------------------------------*
* Isotropic angular distribution 
      YI = FLRNDM(DUMMY)
      COSTH = ONEONE - 2.D0 * YI
      IF (COSTH .GT. ONEONE) THEN
         COSTH = ONEONE
      ELSE IF (COSTH .LT. -ONEONE) THEN
         COSTH = -ONEONE
      END IF
      ZI = FLRNDM(DUMMY)
      ANG = TWOPIP * ZI
      UBEAM = SQRT (ONEONE - COSTH * COSTH) * COS(ANG)
      VBEAM = SQRT (ONEONE - COSTH * COSTH) * SIN(ANG)
      WBEAM = COSTH
*    +-----------------------------------------------------------------------*
* 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
      XBEAM = 0.D0
      YBEAM = 0.D0
      ZBEAM = 8.D2
      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
*=== End of subroutine Source =========================================*
      END


