*$ 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,
     & 1.43063E-06, 2.51925E-06, 3.96848E-06, 5.89810E-06, 8.46701E-06,
     & 1.18872E-05, 1.64401E-05, 2.25003E-05, 3.05692E-05, 4.13149E-05,
     & 5.56249E-05, 7.46887E-05, 1.00091E-04, 1.33948E-04, 1.79088E-04,
     & 2.39304E-04, 3.19571E-04, 4.26763E-04, 5.69760E-04, 7.60494E-04,
     & 1.01498E-03, 1.35435E-03, 1.80621E-03, 2.40729E-03, 3.20520E-03,
     & 4.26168E-03, 5.65515E-03, 7.48612E-03, 9.87876E-03, 1.29874E-02,
     & 1.69978E-02, 2.21306E-02, 2.86438E-02, 3.68203E-02, 4.69918E-02,
     & 5.94910E-02, 7.46704E-02, 9.28711E-02, 1.14405E-01, 1.39527E-01,
     & 1.68406E-01, 2.01116E-01, 2.37596E-01, 2.77631E-01, 3.20863E-01,
     & 3.66778E-01, 4.14726E-01, 4.63954E-01, 5.13658E-01, 5.62944E-01,
     & 6.11079E-01, 6.57260E-01, 7.00844E-01, 7.41357E-01, 7.78446E-01,
     & 8.11885E-01, 8.41633E-01, 8.67747E-01, 8.90392E-01, 9.09797E-01,
     & 9.26255E-01, 9.40071E-01, 9.51570E-01, 9.61060E-01, 9.68834E-01,
     & 9.75160E-01, 9.80273E-01, 9.84387E-01, 9.87680E-01, 9.90305E-01,
     & 9.92388E-01, 9.94037E-01, 9.95338E-01, 9.96361E-01, 9.97164E-01,
     & 9.97793E-01, 9.98285E-01, 9.98669E-01, 9.98968E-01, 9.99201E-01,
     & 9.99382E-01, 9.99523E-01, 9.99632E-01, 9.99717E-01, 9.99783E-01,
     & 9.99834E-01, 9.99873E-01, 9.99904E-01, 9.99928E-01, 9.99946E-01,
     & 9.99960E-01, 9.99971E-01, 9.99979E-01, 9.99986E-01, 9.99991E-01,
     & 9.99995E-01, 9.99998E-01, 0.10000E+01/
*  +-------------------------------------------------------------------*   
      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 Proton 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) 
            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) ) )

*    +-----------------------------------------------------------------*
      Call SFLOOD ( XXX, YYY, ZZZ, UXXX, VXXX, WZZZ )
      UBEAM = UXXX
      VBEAM = VYYY
      WBEAM = WZZZ
* 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 ( radius of sphere =401 cm)
      XBEAM = XXX * 401.D0
      YBEAM = YYY * 401.D0
      ZBEAM = ZZZ * 401.D0
      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


