From 72d758d2d717fbb2db30d92bf43680fd2188e088 Mon Sep 17 00:00:00 2001 From: David Minton Date: Tue, 25 May 2021 10:40:53 -0400 Subject: [PATCH] Added random number seeds as optional input to parameter file --- src/user/user_read_param_in.f90 | 1 + src/user/user_udio_reader.f90 | 31 ++++++++++++++++++++++++++----- src/user/user_udio_writer.f90 | 1 + 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/user/user_read_param_in.f90 b/src/user/user_read_param_in.f90 index 35d883b72..e1980b1c8 100644 --- a/src/user/user_read_param_in.f90 +++ b/src/user/user_read_param_in.f90 @@ -70,6 +70,7 @@ module subroutine user_read_param_in(param,inparfile) write(*,*) "EXTRA_FORCE = ",param%lextra_force write(*,*) "BIG_DISCARD = ",param%lbig_discard write(*,*) "RHILL_PRESENT = ",param%lrhill_present + write(*,*) "SEED = ",size(param%seed), param%seed(:) ierr = 0 ! Added by D. Minton diff --git a/src/user/user_udio_reader.f90 b/src/user/user_udio_reader.f90 index 5db017422..9814b98fe 100644 --- a/src/user/user_udio_reader.f90 +++ b/src/user/user_udio_reader.f90 @@ -27,17 +27,16 @@ module subroutine user_udio_reader(param, unit, iotype, v_list, iostat, iomsg) logical :: t0_set = .false. !! Is the initial time set in the input file? logical :: tstop_set = .false. !! Is the final time set in the input file? logical :: dt_set = .false. !! Is the step size set in the input file? + logical :: seed_set = .false. !! Is the random seed set in the input file? integer(I4B) :: ilength, ifirst, ilast !! Variables used to parse input file character(STRMAX) :: line !! Line of the input file character (len=:), allocatable :: line_trim,param_name, param_value character(*),parameter :: linefmt = '(A)' - integer(I4B) :: nseeds + integer(I4B) :: nseeds, nseeds_from_file, i call random_seed(size = nseeds) if (allocated(param%seed)) deallocate(param%seed) allocate(param%seed(nseeds)) - !seed(:) = [(i * 1, i = 1, nseeds)] - !call random_seed(put = seed) ! Parse the file line by line, extracting tokens then matching them up with known parameters if possible do @@ -145,7 +144,6 @@ module subroutine user_udio_reader(param, unit, iotype, v_list, iostat, iomsg) if (param_value == "YES" .or. param_value == 'T') param%lringmoons = .true. case ("RING_OUTFILE") param%ring_outfile = param_value - case ("TIDES") call util_toupper(param_value) if (param_value == "YES" .or. param_value == 'T') param%ltides = .true. @@ -159,7 +157,24 @@ module subroutine user_udio_reader(param, unit, iotype, v_list, iostat, iomsg) call util_toupper(param_value) if (param_value == "YES" .or. param_value == 'T') param%lyorp = .true. case("SEED") - read(param_value, *) param%qmin + read(param_value, *) nseeds_from_file + ! Because the number of seeds can vary between compilers/systems, we need to make sure we can handle cases in which the input file has a different + ! number of seeds than the current system. If the number of seeds in the file is smaller than required, we will use them as a source to fill in the missing elements. + ! If the number of seeds in the file is larger than required, we will truncate the seed array. + if (nseeds_from_file >= nseeds) then + do i = 1, nseeds + ifirst = ilast + 1 + param_value = user_get_token(line, ifirst, ilast, iostat) + read(param_value, *) param%seed(i) + end do + else if (nseeds_from_file < nseeds) then ! Seed array in file is too small + do i = 1, nseeds_from_file + ifirst = ilast + 1 + param_value = user_get_token(line, ifirst, ilast, iostat) + read(param_value, *) param%seed(i) + end do + end if + seed_set = .true. case default write(iomsg,*) "Unknown parameter -> ",param_name iostat = -1 @@ -240,6 +255,12 @@ module subroutine user_udio_reader(param, unit, iotype, v_list, iostat, iomsg) end if end if + if (seed_set) then + call random_seed(put = param%seed) + else + call random_seed(get = param%seed) + end if + return end subroutine user_udio_reader diff --git a/src/user/user_udio_writer.f90 b/src/user/user_udio_writer.f90 index f0c54b2c5..b37415bba 100644 --- a/src/user/user_udio_writer.f90 +++ b/src/user/user_udio_writer.f90 @@ -84,6 +84,7 @@ module subroutine user_udio_writer(param, unit, iotype, v_list, iostat, iomsg) write(unit, Rfmt) "MU2KG",MU2KG write(unit, Rfmt) "TU2S",TU2S write(unit, Rfmt) "DU2M",DU2M + write(unit,*) "SEED",size(param%seed),param%seed(:) write(unit, Lfmt) "EXTRA_FORCE",param%lextra_force write(unit, Lfmt) "BIG_DISCARD",param%lbig_discard