Skip to content

Commit

Permalink
Change random number generator to RANLUX from MT19937
Browse files Browse the repository at this point in the history
1) Default behaviour of SIMC is still to use the same starting
random seed for every time SIMC is run.
2) In input file one can set the random seed with parameter 'random_seed'
   i) random_seed= -1 then in dbase.f the random_seed will
        be set to time() which is standard gfortran function
        which returns an  integer representing
        the current time.
   ii) random_seed= any positive integer value to be seed
3) In input file one can set  parameter "random_state_file"
   i) This is a file with the 24 member integer array that
       will be used to start the random sequence


Changes:

1) Deleted mt19937.f
2) Add code ranlux.f which calls functions in the CERNLIB
     RANLUX library.
3) changed Makefile
     a) use ranlux.f instead of mt19937.f
     b) add jetset74 to CERNLIBS which has RANLUX library
4) Add documents/ranlux.pdf for RANLUX write-up
5) ranlux.f has subroutines:
    a) subroutine sgrnd(seed)
        i) call RLUXGO to initialize at highest level=3
        ii) if seed=0 then uses default seed INT=314159265
    b) function grnd()
       i) every 1000 grnd() calls in simc it
          executes call RANLUX(RVEC,LEN)
       ii) RVEC is real*4 array of 1000 dimensions with random
           numbers from 0 to 1 not including the endpoints excluded.
    c) subroutine save_random_state(fname)
       i) called in simc.f
       ii) call RLUXUT(IVEC), IVEC is 24 dimension integer sequence
       iii) saves a integer sequence to a file called:
           "outfiles/sname_start_random_state.dat"
	  where sname is the input file name
    d) subroutine start_file_random_state(fname)
        i) called in simc.f
        ii) reads 24 integers in the file random_state_file
            which is set in the input file
        ii)call RLUXIN(IVEC) which initiates the random number
           sequence

6) simc.f
   a) eliminate calls restorerndstate and saverndstate
   b) If random_state_file is not set in input file then
      call sgrnd(random_seed)
   c) If random_state_file is set in input file then
	call  start_file_random_state(random_state_file)
   d) always call save_random_state(start_random_state_file)

7) dbase.f
   a) always set  start_random_state_file to
        "outfiles/sname_start_random_state.dat"
	  where sname is the input file name
   b) if in input file the random_seed parameter .eq. -1
        then  random_seed=time()
8) simulate.inc
   a) add start_random_state_file to common /gnrl/
  • Loading branch information
MarkKJones committed Apr 20, 2020
1 parent cace4ac commit dde5fa7
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 261 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ C = $(simcdir)/calo/
OBJ1 = target.o brem.o gauss1.o NtupleInit.o NtupleClose.o enerloss_new.o
OBJ2 = radc.o init.o dbase.o physics_kaon.o physics_pion.o physics_delta.o physics_proton.o loren.o sf_lookup.o
OBJ3 = semi_physics.o rho_physics.o rho_decay.o generate_rho.o trg_track.o semi_dilution.o
OBJ4 = results_write.o event.o mt19937.o jacobians.o
OBJ4 = results_write.o event.o ranlux.o jacobians.o
OBJ5 = $(A)musc.o $(A)musc_ext.o $(A)project.o $(A)transp.o
OBJ6 = $(A)rotate_haxis.o $(A)rotate_vaxis.o $(A)locforunt.o
OBJ7 = $(H)mc_hms.o $(H)mc_hms_hut.o $(H)mc_hms_recon.o
Expand All @@ -51,7 +51,7 @@ my_deps = $(my_objs:.o=.d)

MYOS := $(subst -,,$(shell uname))
#CERNLIBS = -lgeant$(GEANTVER) -lpawlib -lgraflib -lgrafX11 -lpacklib -lmathlib
CERNLIBS = -Wl,-static -lgeant$(GEANTVER) -lpawlib -lgraflib -lgrafX11 -lpacklib -lkernlib -lmathlib -Wl,-dy
CERNLIBS = -Wl,-static -lgeant$(GEANTVER) -lpawlib -lgraflib -lgrafX11 -lpacklib -lkernlib -lmathlib -ljetset74 -Wl,-dy

#For use with gfortran compiler
# -fno-automatic - all program storage treated as static
Expand All @@ -63,7 +63,7 @@ ifeq ($(MYOS),Linux)
# CERN_ROOT = /usr/lib/cernlib/2006
# 64 bit, standard Fedora distributuion
# CERN_ROOT = /usr/lib64/cernlib/2006
FFLAGSA=-O -W -ffixed-line-length-132 -ff2c -fno-automatic -fdefault-real-8
FFLAGSA=-O -w -ffixed-line-length-132 -ff2c -fno-automatic -fdefault-real-8
INCLUDES=-I.
FFLAGS= $(INCLUDES) $(FFLAGSA)
FFLAG1=$(FFLAGS) -c
Expand Down
5 changes: 4 additions & 1 deletion dbase.f
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ subroutine dbase_read(H)
extra_dbase_file=' '
write(6,*) 'Enter the input filename (assumed to be in infiles directory)'
read(5,'(a)') dbase_file
j=index(dbase_file,'/')
if (j.eq.0) then !no path given
write(filename,'(a)') 'infiles/'//dbase_file
Expand All @@ -79,6 +78,7 @@ subroutine dbase_read(H)
endif
write(6,'(a10,a69)')'filename=',filename
if (i.gt.1) base=filename(j+1:i-1)
write(start_random_state_file,'(a)') 'outfiles/'//filename(j+1:i-1)//'_start_random_state.dat'
! ... load and book input file
Expand All @@ -103,6 +103,9 @@ subroutine dbase_read(H)
ierr = thbook()
if (ierr.ne.0) stop ' Booking problem! Not going to try again...wouldnt be prudent'
endif !extra dbase input file
c
if (random_seed .eq. -1) random_seed=time()
write(*,*) 'Use random seed = ',random_seed
C DJG: Ugly hack! This must come before the test on doing_pion
if(doing_pion .and. doing_semi) then
Expand Down
Binary file added documents/ranlux.pdf
Binary file not shown.
233 changes: 0 additions & 233 deletions mt19937.f

This file was deleted.

73 changes: 73 additions & 0 deletions ranlux.f
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
c NEW VERSION: sgrand and grand functions
c modified to use much imporvoed function RANLUX
c with luxury level 3. This avoid repeating
c number sequences.
c Implemented by Peter Bosted in late 2019.
subroutine sgrnd(seed)
c Initialize random number generator at highest level (3) using
c 32bit-integer seed. In future, could implement option to restart
c from a break point (see CERNLIB description), but that isn't done here
implicit none
integer seed,lux,k1,k2
lux=3
k1=0
k2=0
call RLUXGO(LUX,seed,K1,K2)
return
end
c
subroutine save_random_state(fname)
implicit none
integer*4 i,ivec(25)

character*(*) fname
c
call RLUXUT(IVEC)

open(unit=1,file=fname)
do i=1,25
write(1,*) ivec(i)
enddo
close(1)
c
return
end
c
subroutine start_file_random_state(fname)
implicit none
integer*4 i,ivec(25)

character*(*) fname
open(unit=1,file=fname)
do i=1,25
read(1,*) ivec(i)
enddo
close(1)
c
write(*,*) " Starting random sequence with file : ",fname
call RLUXIN(IVEC)

c
return
end
c
c

c

double precision function grnd()
implicit none
integer len/1000/,latest,i
real*4 rvec(1000)

if(latest.le.0 .or. latest.ge.1000) then
CALL RANLUX(RVEC,LEN)
latest=1
endif
grnd = rvec(latest)

latest = latest + 1

return
end

Loading

0 comments on commit dde5fa7

Please sign in to comment.