1 | !> @file random_function_mod.f90 |
---|
2 | !------------------------------------------------------------------------------! |
---|
3 | ! This file is part of the PALM model system. |
---|
4 | ! |
---|
5 | ! PALM is free software: you can redistribute it and/or modify it under the |
---|
6 | ! terms of the GNU General Public License as published by the Free Software |
---|
7 | ! Foundation, either version 3 of the License, or (at your option) any later |
---|
8 | ! version. |
---|
9 | ! |
---|
10 | ! PALM is distributed in the hope that it will be useful, but WITHOUT ANY |
---|
11 | ! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
---|
12 | ! A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
---|
13 | ! |
---|
14 | ! You should have received a copy of the GNU General Public License along with |
---|
15 | ! PALM. If not, see <http://www.gnu.org/licenses/>. |
---|
16 | ! |
---|
17 | ! Copyright 1997-2019 Leibniz Universitaet Hannover |
---|
18 | !------------------------------------------------------------------------------! |
---|
19 | ! |
---|
20 | ! Current revisions: |
---|
21 | ! ----------------- |
---|
22 | ! |
---|
23 | ! |
---|
24 | ! Former revisions: |
---|
25 | ! ----------------- |
---|
26 | ! $Id: random_function_mod.f90 3802 2019-03-17 13:33:42Z Giersch $ |
---|
27 | ! type conversion added to avoid compiler warning about constant integer |
---|
28 | ! division truncation |
---|
29 | ! |
---|
30 | ! 3655 2019-01-07 16:51:22Z knoop |
---|
31 | ! Corrected "Former revisions" section |
---|
32 | ! |
---|
33 | ! 2696 2017-12-14 17:12:51Z kanani |
---|
34 | ! Change in file header (GPL part) |
---|
35 | ! |
---|
36 | ! 2101 2017-01-05 16:42:31Z suehring |
---|
37 | ! |
---|
38 | ! 2000 2016-08-20 18:09:15Z knoop |
---|
39 | ! Forced header and separation lines into 80 columns |
---|
40 | ! |
---|
41 | ! 1850 2016-04-08 13:29:27Z maronga |
---|
42 | ! Module renamed |
---|
43 | ! |
---|
44 | ! |
---|
45 | ! 1682 2015-10-07 23:56:08Z knoop |
---|
46 | ! Code annotations made doxygen readable |
---|
47 | ! |
---|
48 | ! 1342 2014-03-26 17:04:47Z kanani |
---|
49 | ! REAL constants defined as wp-kind |
---|
50 | ! |
---|
51 | ! 1320 2014-03-20 08:40:49Z raasch |
---|
52 | ! ONLY-attribute added to USE-statements, |
---|
53 | ! kind-parameters added to all INTEGER and REAL declaration statements, |
---|
54 | ! kinds are defined in new module kinds, |
---|
55 | ! old module precision_kind is removed, |
---|
56 | ! revision history before 2012 removed, |
---|
57 | ! comment fields (!:) to be used for variable explanations added to |
---|
58 | ! all variable declaration statements |
---|
59 | ! |
---|
60 | ! 1092 2013-02-02 11:24:22Z raasch |
---|
61 | ! unused variables removed |
---|
62 | ! |
---|
63 | ! 1036 2012-10-22 13:43:42Z raasch |
---|
64 | ! code put under GPL (PALM 3.9) |
---|
65 | ! |
---|
66 | ! RCS Log replace by Id keyword, revision history cleaned up |
---|
67 | ! |
---|
68 | ! Revision 1.1 1998/02/04 16:09:45 raasch |
---|
69 | ! Initial revision |
---|
70 | ! |
---|
71 | ! |
---|
72 | ! Description: |
---|
73 | ! ------------ |
---|
74 | !> Random number generator, produces numbers equally distributed in interval [0,1] |
---|
75 | !> This routine is taken from the "numerical recipies" |
---|
76 | !------------------------------------------------------------------------------! |
---|
77 | MODULE random_function_mod |
---|
78 | |
---|
79 | |
---|
80 | USE kinds |
---|
81 | |
---|
82 | IMPLICIT NONE |
---|
83 | |
---|
84 | PRIVATE |
---|
85 | |
---|
86 | PUBLIC random_function, random_function_ini |
---|
87 | |
---|
88 | INTEGER(iwp), PUBLIC, SAVE :: random_iv(32) !< |
---|
89 | INTEGER(iwp), PUBLIC, SAVE :: random_iy !< |
---|
90 | |
---|
91 | INTERFACE random_function_ini |
---|
92 | MODULE PROCEDURE random_function_ini |
---|
93 | END INTERFACE random_function_ini |
---|
94 | |
---|
95 | INTERFACE random_function |
---|
96 | MODULE PROCEDURE random_function |
---|
97 | END INTERFACE random_function |
---|
98 | |
---|
99 | CONTAINS |
---|
100 | |
---|
101 | !------------------------------------------------------------------------------! |
---|
102 | ! Description: |
---|
103 | ! ------------ |
---|
104 | !> @todo Missing subroutine description. |
---|
105 | !------------------------------------------------------------------------------! |
---|
106 | SUBROUTINE random_function_ini |
---|
107 | |
---|
108 | IMPLICIT NONE |
---|
109 | |
---|
110 | random_iv = 0 |
---|
111 | random_iy = 0 |
---|
112 | |
---|
113 | END SUBROUTINE random_function_ini |
---|
114 | |
---|
115 | FUNCTION random_function( idum ) |
---|
116 | |
---|
117 | |
---|
118 | IMPLICIT NONE |
---|
119 | |
---|
120 | INTEGER(iwp) :: ia !< |
---|
121 | INTEGER(iwp) :: idum !< |
---|
122 | INTEGER(iwp) :: im !< |
---|
123 | INTEGER(iwp) :: iq !< |
---|
124 | INTEGER(iwp) :: ir !< |
---|
125 | INTEGER(iwp) :: ndiv !< |
---|
126 | INTEGER(iwp) :: ntab !< |
---|
127 | |
---|
128 | INTEGER(iwp) :: j !< |
---|
129 | INTEGER(iwp) :: k !< |
---|
130 | |
---|
131 | REAL(wp) :: am !< |
---|
132 | REAL(wp) :: eps !< |
---|
133 | REAL(wp) :: random_function !< |
---|
134 | REAL(wp) :: rnmx !< |
---|
135 | |
---|
136 | PARAMETER ( ia=16807, im=2147483647, am=1.0_wp/im, iq=127773, ir=2836, & |
---|
137 | ntab=32, ndiv=1+INT(REAL(im-1)/ntab), eps=1.2e-7_wp, rnmx=1.0_wp-eps ) |
---|
138 | |
---|
139 | IF ( idum .le. 0 .or. random_iy .eq. 0 ) THEN |
---|
140 | idum = max (-idum,1) |
---|
141 | DO j = ntab+8,1,-1 |
---|
142 | k = idum / iq |
---|
143 | idum = ia * ( idum - k * iq ) - ir * k |
---|
144 | IF ( idum .lt. 0 ) idum = idum + im |
---|
145 | IF ( j .le. ntab ) random_iv(j) = idum |
---|
146 | ENDDO |
---|
147 | random_iy = random_iv(1) |
---|
148 | ENDIF |
---|
149 | |
---|
150 | k = idum / iq |
---|
151 | idum = ia * ( idum - k * iq ) - ir * k |
---|
152 | IF ( idum .lt. 0 ) idum = idum + im |
---|
153 | j = 1 + random_iy / ndiv |
---|
154 | random_iy = random_iv(j) |
---|
155 | random_iv(j) = idum |
---|
156 | random_function = min ( am * random_iy , rnmx ) |
---|
157 | |
---|
158 | END FUNCTION random_function |
---|
159 | |
---|
160 | END MODULE random_function_mod |
---|