diff --git a/src/korc_c_random.cpp b/src/korc_c_random.cpp index 1a681c05..e1e43cdb 100755 --- a/src/korc_c_random.cpp +++ b/src/korc_c_random.cpp @@ -58,11 +58,11 @@ extern "C" { return static_cast (r)->get_number_N(); } - void random_N_destroy(void *r) { + void random_destroy_N(void *r) { delete static_cast (r); } - void random_U_destroy(void *r) { + void random_destroy_U(void *r) { delete static_cast (r); } diff --git a/src/korc_experimental_pdf.f90 b/src/korc_experimental_pdf.f90 index bdf3948a..71090f10 100755 --- a/src/korc_experimental_pdf.f90 +++ b/src/korc_experimental_pdf.f90 @@ -939,6 +939,9 @@ SUBROUTINE sample_Hollmann_distribution(params,spp) ! write(output_unit_write,'("sampled eta: ",E17.10)') eta + if (.not.params%SameRandSeed) then + call finalize_random_seed + end if END SUBROUTINE sample_Hollmann_distribution FUNCTION PSI_ROT_exp(R,R0,sigR,Z,Z0,sigZ,theta) @@ -1383,7 +1386,10 @@ subroutine sample_Hollmann_distribution_3D(params,spp,F) ! write(output_unit_write,*) 'Z_samples',Z_samples ! write(output_unit_write,*) 'G_samples',G_samples ! write(output_unit_write,*) 'eta_samples',eta_samples - + + if (.not.params%SameRandSeed) then + call finalize_random_seed + end if end if @@ -1971,7 +1977,10 @@ subroutine sample_Hollmann_distribution_3D_psi(params,spp,F) if (TRIM(h_params%current_direction) .EQ. 'PARALLEL') then eta_samples = 180.0_rp - eta_samples end if - + + if (.not.params%SameRandSeed) then + call finalize_random_seed + end if end if params%GC_coords=.FALSE. @@ -2506,7 +2515,10 @@ subroutine sample_Hollmann_distribution_1Dtransport(params,spp,F) if (TRIM(h_params%current_direction) .EQ. 'PARALLEL') then eta_samples = 180.0_rp - eta_samples end if - + + if (.not.params%SameRandSeed) then + call finalize_random_seed + end if end if params%GC_coords=.FALSE. diff --git a/src/korc_fields.f90 b/src/korc_fields.f90 index 1f0cc842..66209d90 100755 --- a/src/korc_fields.f90 +++ b/src/korc_fields.f90 @@ -1161,6 +1161,8 @@ subroutine unitVectors(params,Xo,F,b1,b2,b3,flag,cart,hint,Bo) !write(output_unit_write,*) 'out unitVectors' + call finalize_random_seed + end subroutine unitVectors diff --git a/src/korc_initialize.f90 b/src/korc_initialize.f90 index e831415c..c7ea30f9 100755 --- a/src/korc_initialize.f90 +++ b/src/korc_initialize.f90 @@ -738,4 +738,14 @@ subroutine set_up_particles_ic(params,F,spp,P) end subroutine set_up_particles_ic + subroutine take_down_particles_ic(params) + implicit none + + TYPE(KORC_PARAMS), INTENT(INOUT) :: params + !! Core KORC simulation parameters. + + if (params%restart.OR.params%proceed.or.params%reinit) then + call finalize_random_seed + end if + end subroutine end module korc_initialize diff --git a/src/korc_random.f90 b/src/korc_random.f90 index 1a77847c..6decf73f 100755 --- a/src/korc_random.f90 +++ b/src/korc_random.f90 @@ -104,6 +104,7 @@ END SUBROUTINE random_set_dist END INTERFACE PUBLIC :: initialize_random + PUBLIC :: finalize_random CONTAINS @@ -125,6 +126,16 @@ SUBROUTINE initialize_random(seed) !$OMP END PARALLEL END SUBROUTINE initialize_random + SUBROUTINE finalize_random + IMPLICIT NONE + + INTEGER :: thread_num + !$OMP PARALLEL + thread_num = get_thread_number() + CALL random_destroy_U(states(thread_num)) + !$OMP END PARALLEL + END SUBROUTINE + SUBROUTINE initialize_random_U(seed) IMPLICIT NONE @@ -134,6 +145,12 @@ SUBROUTINE initialize_random_U(seed) END SUBROUTINE initialize_random_U + SUBROUTINE finalize_random_U + IMPLICIT NONE + + CALL random_destroy_U(state) + END SUBROUTINE + SUBROUTINE initialize_random_N(seed) IMPLICIT NONE @@ -142,7 +159,13 @@ SUBROUTINE initialize_random_N(seed) state = random_construct_N(seed) END SUBROUTINE initialize_random_N - + + SUBROUTINE finalize_random_N + IMPLICIT NONE + + CALL random_destroy_N(state) + END SUBROUTINE + FUNCTION get_random() IMPLICIT NONE diff --git a/src/korc_rnd_numbers.f90 b/src/korc_rnd_numbers.f90 index f6ae48fe..e0fde1fe 100755 --- a/src/korc_rnd_numbers.f90 +++ b/src/korc_rnd_numbers.f90 @@ -183,4 +183,19 @@ function lcg(s) end function lcg end subroutine init_random_seed +subroutine finalize_random_seed +#ifdef PARALLEL_RANDOM + use korc_random +#endif + +implicit none + +#ifdef PARALLEL_RANDOM +call finalize_random +call finalize_random_U +call finalize_random_N +#endif + +end subroutine + end module korc_rnd_numbers diff --git a/src/korc_spatial_distribution.f90 b/src/korc_spatial_distribution.f90 index ef6e93ae..52419073 100755 --- a/src/korc_spatial_distribution.f90 +++ b/src/korc_spatial_distribution.f90 @@ -100,12 +100,15 @@ subroutine disk(params,spp) call init_random_seed(params) call RANDOM_NUMBER(theta) + call finalize_random_seed theta = 2.0_rp*C_PI*theta ! Uniform distribution on a disk at a fixed azimuthal theta +! FIXME: Is it really necessary to reset the seed here? call init_random_seed(params) call RANDOM_NUMBER(r) + call finalize_random_seed r = SQRT((spp%r_outter**2 - spp%r_inner**2)*r + spp%r_inner**2) spp%vars%X(:,1) = ( spp%Ro + r*COS(theta) )*COS(spp%PHIo) @@ -162,6 +165,8 @@ subroutine torus(params,spp) DEALLOCATE(theta) DEALLOCATE(zeta) DEALLOCATE(r) + + call finalize_random_seed end subroutine torus !subroutine torus(params,spp) @@ -312,14 +317,19 @@ subroutine elliptic_torus(params,spp) call init_random_seed(params) call RANDOM_NUMBER(theta) theta = 2.0_rp*C_PI*theta + call finalize_random_seed +! FIXME: Is it really necessary to reset the seed here? call init_random_seed(params) call RANDOM_NUMBER(zeta) zeta = 2.0_rp*C_PI*zeta + call finalize_random_seed ! Uniform distribution on a disk at a fixed azimuthal theta +! FIXME: Is it really necessary to reset the seed here? call init_random_seed(params) call RANDOM_NUMBER(r) + call finalize_random_seed r = SQRT((spp%r_outter**2 - spp%r_inner**2)*r + spp%r_inner**2) @@ -426,14 +436,19 @@ subroutine exponential_torus(params,spp) call init_random_seed(params) call RANDOM_NUMBER(theta) theta = 2.0_rp*C_PI*theta + call finalize_random_seed call init_random_seed(params) +! FIXME: Is it really necessary to reset the seed here? call RANDOM_NUMBER(zeta) zeta = 2.0_rp*C_PI*zeta + call finalize_random_seed ! Uniform distribution on a disk at a fixed azimuthal theta call init_random_seed(params) +! FIXME: Is it really necessary to reset the seed here? call RANDOM_NUMBER(r) + call finalize_random_seed ! Newton-Raphson applied here for finding the radial distribution do pp=1_idef,spp%ppp @@ -542,14 +557,19 @@ subroutine exponential_elliptic_torus(params,spp) call init_random_seed(params) call RANDOM_NUMBER(theta) theta = 2.0_rp*C_PI*theta + call finalize_random_seed +! FIXME: Is it really necessary to reset the seed here? call init_random_seed(params) call RANDOM_NUMBER(zeta) zeta = 2.0_rp*C_PI*zeta + call finalize_random_seed ! Uniform distribution on a disk at a fixed azimuthal theta +! FIXME: Is it really necessary to reset the seed here? call init_random_seed(params) call RANDOM_NUMBER(r) + call finalize_random_seed do pp=1_idef,spp%ppp rl = 0.0_rp @@ -662,14 +682,19 @@ subroutine gaussian_elliptic_torus(params,spp) call init_random_seed(params) call RANDOM_NUMBER(theta) theta = 2.0_rp*C_PI*theta + call finalize_random_seed +! FIXME: Is it really necessary to reset the seed here? call init_random_seed(params) call RANDOM_NUMBER(zeta) zeta = 2.0_rp*C_PI*zeta + call finalize_random_seed ! Uniform distribution on a disk at a fixed azimuthal theta +! FIXME: Is it really necessary to reset the seed here? call init_random_seed(params) call RANDOM_NUMBER(r) + call finalize_random_seed sigma = 1.0_rp/SQRT(2.0_rp*(spp%falloff_rate/params%cpp%length)) sigma = sigma/params%cpp%length @@ -951,14 +976,19 @@ subroutine gaussian_torus(params,spp) call init_random_seed(params) call RANDOM_NUMBER(theta) theta = 2.0_rp*C_PI*theta + call finalize_random_seed +! FIXME: Is it really necessary to reset the seed here? call init_random_seed(params) call RANDOM_NUMBER(zeta) zeta = 2.0_rp*C_PI*zeta + call finalize_random_seed ! Uniform distribution on a disk at a fixed azimuthal theta +! FIXME: Is it really necessary to reset the seed here? call init_random_seed(params) call RANDOM_NUMBER(r) + call finalize_random_seed sigma = 1.0_rp/SQRT(2.0_rp*(spp%falloff_rate/params%cpp%length)) sigma = sigma/params%cpp%length @@ -1685,6 +1715,9 @@ subroutine MH_psi(params,spp,F) ! write(output_unit_write,*) 'G_samples',G_samples ! write(output_unit_write,*) 'eta_samples',eta_samples + if (.not.params%SameRandSeed) then + call init_random_seed(params) + end if end if params%GC_coords=.FALSE. @@ -2153,6 +2186,9 @@ subroutine FIO_therm(params,spp,F,P) G_samples=1/sqrt(1-V_samples**2) ETA_samples=acos(XI_samples)*180/C_PI + if (.not.params%SameRandSeed) then + call init_random_seed(params) + end if end if params%GC_coords=.FALSE. @@ -2633,6 +2669,9 @@ subroutine BMC_radial(params,spp,F,P) ! write(output_unit_write,*) 'G_samples',G_samples ! write(output_unit_write,*) 'eta_samples',eta_samples + if (.not.params%SameRandSeed) then + call init_random_seed(params) + end if end if params%GC_coords=.FALSE. diff --git a/src/korc_velocity_distribution.f90 b/src/korc_velocity_distribution.f90 index 4a7a4199..428d9952 100755 --- a/src/korc_velocity_distribution.f90 +++ b/src/korc_velocity_distribution.f90 @@ -476,6 +476,8 @@ subroutine gyro_distribution(params,F,spp) DEALLOCATE(b2) DEALLOCATE(b3) DEALLOCATE(hint) + + call finalize_random_seed end subroutine gyro_distribution diff --git a/src/main.f90 b/src/main.f90 index cd41e438..2f362b1b 100755 --- a/src/main.f90 +++ b/src/main.f90 @@ -727,6 +727,9 @@ program main call timing_KORC(params) ! * * * FINALIZING SIMULATION * * * + + call take_down_particles_ic(params) + call finalize_HDF5() #ifdef PSPLINE