Skip to content

Commit 5cfd0a9

Browse files
committed
common: move PRNG helpers (s_prng/modmul/f_unit_vector) to m_helper
1 parent 642cc2b commit 5cfd0a9

2 files changed

Lines changed: 43 additions & 40 deletions

File tree

src/common/m_helper.fpp

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module m_helper
1818
public :: s_comp_n_from_prim, s_comp_n_from_cons, s_initialize_bubbles_model, s_initialize_nonpoly, s_simpson, s_transcoeff, &
1919
& s_int_to_str, s_transform_vec, s_transform_triangle, s_transform_model, s_swap, f_cross, f_create_transform_matrix, &
2020
& f_create_bbox, s_print_2D_array, f_xor, f_logical_to_int, associated_legendre, real_ylm, double_factorial, factorial, &
21-
& f_cut_on, f_cut_off, s_downsample_data, s_upsample_data, s_cross_product
21+
& f_cut_on, f_cut_off, s_downsample_data, s_upsample_data, s_cross_product, f_unit_vector, s_prng, modmul
2222

2323
contains
2424

@@ -297,6 +297,48 @@ contains
297297
298298
end function f_cross
299299
300+
!> Generate a unit vector uniformly distributed on the sphere from two random parameters.
301+
function f_unit_vector(theta, eta) result(vec)
302+
303+
$:GPU_ROUTINE(parallelism='[seq]')
304+
real(wp), intent(in) :: theta, eta
305+
real(wp) :: zeta, xi
306+
real(wp), dimension(3) :: vec
307+
308+
xi = 2._wp*pi*theta
309+
zeta = acos(2._wp*eta - 1._wp)
310+
vec(1) = sin(zeta)*cos(xi)
311+
vec(2) = sin(zeta)*sin(xi)
312+
vec(3) = cos(zeta)
313+
314+
end function f_unit_vector
315+
316+
!> Generate a pseudo-random number between 0 and 1 using a linear congruential generator.
317+
subroutine s_prng(var, seed)
318+
319+
$:GPU_ROUTINE(parallelism='[seq]')
320+
integer, intent(inout) :: seed
321+
real(wp), intent(out) :: var
322+
323+
seed = mod(modmul(seed), modulus)
324+
var = seed/real(modulus, wp)
325+
326+
end subroutine s_prng
327+
328+
!> Compute a modular multiplication step for the linear congruential pseudo-random number generator.
329+
function modmul(a) result(val)
330+
331+
$:GPU_ROUTINE(parallelism='[seq]')
332+
integer, intent(in) :: a
333+
integer :: val
334+
real(wp) :: x, y
335+
336+
x = (multiplier/real(modulus, wp))*a + (increment/real(modulus, wp))
337+
y = nint((x - floor(x))*decimal_trim)/decimal_trim
338+
val = nint(y*modulus)
339+
340+
end function modmul
341+
300342
!> @brief Computes the cross product c = a x b of two 3D vectors.
301343
subroutine s_cross_product(a, b, c)
302344

src/pre_process/m_perturbation.fpp

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -334,45 +334,6 @@ contains
334334

335335
end subroutine s_generate_random_perturbation
336336

337-
!> Generate a unit vector uniformly distributed on the sphere from two random parameters.
338-
function f_unit_vector(theta, eta) result(vec)
339-
340-
real(wp), intent(in) :: theta, eta
341-
real(wp) :: zeta, xi
342-
real(wp), dimension(3) :: vec
343-
344-
xi = 2._wp*pi*theta
345-
zeta = acos(2._wp*eta - 1._wp)
346-
vec(1) = sin(zeta)*cos(xi)
347-
vec(2) = sin(zeta)*sin(xi)
348-
vec(3) = cos(zeta)
349-
350-
end function f_unit_vector
351-
352-
!> Generate a pseudo-random number between 0 and 1 using a linear congruential generator.
353-
subroutine s_prng(var, seed)
354-
355-
integer, intent(inout) :: seed
356-
real(wp), intent(out) :: var
357-
358-
seed = mod(modmul(seed), modulus)
359-
var = seed/real(modulus, wp)
360-
361-
end subroutine s_prng
362-
363-
!> Compute a modular multiplication step for the linear congruential pseudo-random number generator.
364-
function modmul(a) result(val)
365-
366-
integer, intent(in) :: a
367-
integer :: val
368-
real(wp) :: x, y
369-
370-
x = (multiplier/real(modulus, wp))*a + (increment/real(modulus, wp))
371-
y = nint((x - floor(x))*decimal_trim)/decimal_trim
372-
val = nint(y*modulus)
373-
374-
end function modmul
375-
376337
!> Deallocate the temporary primitive variable array used by elliptic smoothing.
377338
impure subroutine s_finalize_perturbation_module()
378339

0 commit comments

Comments
 (0)