From 7f30fece4e562551aca6d7e299f9d09d6dc991cf Mon Sep 17 00:00:00 2001 From: David A Minton Date: Fri, 10 Dec 2021 12:20:15 -0500 Subject: [PATCH 1/2] Added random seed array to the crater type and added code to save and retrieve seeds when calling crater_generate so that the same population of craters is generated for a given input seed, regardless of what other features that use random numbers are used. --- src/Makefile.am | 4 ++-- src/crater/crater_generate.f90 | 13 ++++++++----- src/globals/module_globals.f90 | 1 + src/main/CTEM.f90 | 11 +++++------ src/realistic/realistic_perlin_noise.f90 | 8 -------- 5 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 9e40e222..90ac9873 100755 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -5,9 +5,9 @@ PAR = -qopenmp -parallel HEAPARR = -heap-arrays OPTREPORT = -qopt-report=5 IPRODUCTION = -g -traceback -no-wrap-margin -assume byterecl -O3 -qopt-prefetch=0 -sox $(PAR) $(SIMDVEC) $(HEAPARR) -AM_FCFLAGS = $(IPRODUCTION) $(OPTREPORT) +IDEBUG = -O0 -g -traceback -debug all -nogen-interfaces -assume byterecl -m64 -heap-arrays -FR -no-pie -no-ftz -fpe-all=0 -mp1 -fp-model strict -fpe0 -align all -pad -ip -prec-div -prec-sqrt -assume protect-parens -CB -no-wrap-margin -init=snan,arrays +AM_FCFLAGS = $(IPRODUCTION) #ifort debug flags -#AM_FCFLAGS = -O0 -p -g -debug all -traceback -CB -assume byterecl -m64 -heap-arrays -FR $(PAR) $(SIMDVEC) #gfortran optimized flags #AM_FCFLAGS = -O3 -fopenmp -ffree-form -g -fbounds-check -fbacktrace diff --git a/src/crater/crater_generate.f90 b/src/crater/crater_generate.f90 index cc6b9a23..037d3e77 100644 --- a/src/crater/crater_generate.f90 +++ b/src/crater/crater_generate.f90 @@ -39,12 +39,15 @@ subroutine crater_generate(user,crater,domain,prod,production_list,vdist,surf) integer(I4B) :: k,khi,klo,Nk integer(I8B) :: random_index,numremaining,nabove,nabovep1 - !TESTING FOR VARIABLE FE MODERL - real(DP) :: mfe,bfe - - ! Get all six random numbers we need in one call - if (.not.domain%initialize) call random_number(rn) + if (.not.domain%initialize) then + ! Initialize the random number generator with the current value of the seeds + call random_seed(put=crater%seedarr) + call random_number(rn) + ! Save the current value of the seeds for the next time we need a new crater. This ensures we can get a repeatable population population of craters, + ! regardless of whether other procedures that use the random number generator are used or not + call random_seed(get=crater%seedarr) + end if ! Find crater center position if (domain%initialize) then diff --git a/src/globals/module_globals.f90 b/src/globals/module_globals.f90 index 42948217..2d7a70e5 100644 --- a/src/globals/module_globals.f90 +++ b/src/globals/module_globals.f90 @@ -124,6 +124,7 @@ module module_globals real(DP) :: floordepth real(DP) :: floordiam real(DP) :: peakheight + integer(I4B),dimension(:),allocatable :: seedarr ! Random number generator seed array end type cratertype ! Derived data type for domain variables (sizes and dimensions) diff --git a/src/main/CTEM.f90 b/src/main/CTEM.f90 index b302ecc1..b7289b0d 100644 --- a/src/main/CTEM.f90 +++ b/src/main/CTEM.f90 @@ -50,7 +50,6 @@ program CTEM integer(I8B) :: totalimpacts ! Total number of impacts ever produced integer(I4B) :: ncount ! Current count in ctem_driver IDL run integer(I4B) :: n, xp, yp, i ! Size of random number generator seed array -integer(I4B),dimension(:),allocatable :: seedarr ! Random number generator seed array real(DP) :: curyear real(DP) :: mass real(DP) :: masstot @@ -102,9 +101,9 @@ program CTEM ! Reset random number generator call random_seed call random_seed(size=n) -allocate(seedarr(n)) -call io_read_const(totalimpacts,ncount,curyear,restart,fracdone,masstot,seedarr) -call random_seed(put=seedarr) +allocate(crater%seedarr(n)) +call io_read_const(totalimpacts,ncount,curyear,restart,fracdone,masstot,crater%seedarr) +call random_seed(put=crater%seedarr) ! Read in old grid arrays, production function, and velocity distributions if (restart .or. user%tallyonly) then @@ -125,9 +124,9 @@ program CTEM fracdone,nflux,ntotcrat,curyear,rclist) ! Get the last seed and save it to file - call random_seed(get=seedarr) + call random_seed(get=crater%seedarr) totalimpacts = totalimpacts + ntotcrat - call io_write_const(totalimpacts,ncount,curyear,restart,fracdone,masstot,seedarr) + call io_write_const(totalimpacts,ncount,curyear,restart,fracdone,masstot,crater%seedarr) call crater_tally_true(domain,truelist(:,1:ntrue),ntrue,truedist) end if diff --git a/src/realistic/realistic_perlin_noise.f90 b/src/realistic/realistic_perlin_noise.f90 index 3c342fd8..f325b3a0 100644 --- a/src/realistic/realistic_perlin_noise.f90 +++ b/src/realistic/realistic_perlin_noise.f90 @@ -605,18 +605,10 @@ subroutine realistic_ejecta_texture(user,surf,crater,deltaMtot,inc,ejecta_dem) real(DP) :: splatmag ! The magnitude of the splat features relative to the ejecta thickness integer(I4B) :: nsplat_octaves integer(I4B) :: nseed - integer(I4B),dimension(:),allocatable :: seed real(DP),dimension(:),allocatable :: Sarr,Aarr real(DP),dimension(:,:),allocatable :: anchor - !Executable code - call random_seed(size = nseed) - allocate(seed(nseed)) - seed = 42 * (/ (i - 1, i = 1, nseed) /) - call random_seed(put = seed) - deallocate(seed) - ! Copernicus values num_octaves = 20 From e1cd6bc480379cf403b9fc7bcc23d6a0abf48612 Mon Sep 17 00:00:00 2001 From: David A Minton Date: Fri, 10 Dec 2021 12:57:53 -0500 Subject: [PATCH 2/2] Removed code that pulled the current random seed prior to saving it to file so that we don't accidentally get out of sequence --- src/main/CTEM.f90 | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/CTEM.f90 b/src/main/CTEM.f90 index b7289b0d..986356c8 100644 --- a/src/main/CTEM.f90 +++ b/src/main/CTEM.f90 @@ -124,7 +124,6 @@ program CTEM fracdone,nflux,ntotcrat,curyear,rclist) ! Get the last seed and save it to file - call random_seed(get=crater%seedarr) totalimpacts = totalimpacts + ntotcrat call io_write_const(totalimpacts,ncount,curyear,restart,fracdone,masstot,crater%seedarr) call crater_tally_true(domain,truelist(:,1:ntrue),ntrue,truedist)