FV3 Bundle
fv3-jedi/src/Utilities/random_vectors_mod.f90
Go to the documentation of this file.
1 !
2 ! (C) Copyright 2017-2018 UCAR.
3 !
4 ! This software is licensed under the terms of the Apache Licence Version 2.0
5 ! which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 !
7 
8 !> Fortran module for generating random vectors
10 
11 use, intrinsic :: iso_c_binding
13 
14 implicit none
15 private
16 public :: random_vector
17 
18 ! ------------------------------------------------------------------------------
19 !> Fortran generic for generating random 1d, 2d and 3d arrays
20 interface random_vector
23 end interface
24 ! ------------------------------------------------------------------------------
25 
26 !-------------------------------------------------------------------------------
27 interface
28 subroutine random_c(kk, pp) bind(C,name='random_f')
29  use, intrinsic :: iso_c_binding
30  implicit none
31  integer(c_int), intent(in) :: kk
32  real(kind=c_double), intent(out) :: pp
33 end subroutine random_c
34 end interface
35 !-------------------------------------------------------------------------------
36 
37 ! ------------------------------------------------------------------------------
38 contains
39 ! ------------------------------------------------------------------------------
40 
41 !> Generate a random 1d array of reals
42 subroutine random_vector_1(xx)
43 implicit none
44 real(kind=kind_real), intent(inout) :: xx(:)
45 real(kind=c_double) :: zz(size(xx))
46 integer(c_int) :: nn
47 
48 nn = size(xx)
49 call random_c(nn, zz(1))
50 xx(:)=zz(:)
51 
52 end subroutine random_vector_1
53 
54 ! ------------------------------------------------------------------------------
55 
56 !> Generate a random 2d array of reals
57 subroutine random_vector_2(xx)
58 implicit none
59 real(kind=kind_real), intent(inout) :: xx(:,:)
60 real(kind=c_double) :: zz(size(xx))
61 integer(c_int) :: nn
62 
63 nn = size(xx)
64 call random_c(nn, zz(1))
65 xx = reshape(zz, shape(xx))
66 
67 end subroutine random_vector_2
68 
69 ! ------------------------------------------------------------------------------
70 
71 !> Generate a random 3d array of reals
72 subroutine random_vector_3(xx)
73 implicit none
74 real(kind=kind_real), intent(inout) :: xx(:,:,:)
75 real(kind=c_double) :: zz(size(xx))
76 integer(c_int) :: nn
77 
78 nn = size(xx)
79 call random_c(nn, zz(1))
80 xx = reshape(zz, shape(xx))
81 
82 end subroutine random_vector_3
83 
84 !> Generate a random 4d array of reals
85 subroutine random_vector_4(xx)
86 implicit none
87 real(kind=kind_real), intent(inout) :: xx(:,:,:,:)
88 real(kind=c_double) :: zz(size(xx))
89 integer(c_int) :: nn
90 
91 nn = size(xx)
92 call random_c(nn, zz(1))
93 xx = reshape(zz, shape(xx))
94 
95 end subroutine random_vector_4
96 
97 ! ------------------------------------------------------------------------------
98 
99 end module random_vectors_mod
Fortran generic for generating random 1d, 2d and 3d arrays.
subroutine random_vector_3(xx)
Generate a random 3d array of reals.
subroutine random_vector_2(xx)
Generate a random 2d array of reals.
subroutine random_vector_1(xx)
Generate a random 1d array of reals.
subroutine random_vector_4(xx)
Generate a random 4d array of reals.
Fortran module for generating random vectors.