source: palm/trunk/SOURCE/random_function_mod.f90 @ 4178

Last change on this file since 4178 was 4144, checked in by raasch, 2 years ago

relational operators .EQ., .NE., etc. replaced by ==, /=, etc.

  • Property svn:keywords set to Id
File size: 4.8 KB
Line 
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 4144 2019-08-06 09:11:47Z suehring $
27! relational operators .EQ., .NE., etc. replaced by ==, /=, etc.
28!
29! 3802 2019-03-17 13:33:42Z raasch
30! type conversion added to avoid compiler warning about constant integer
31! division truncation
32!
33! 3655 2019-01-07 16:51:22Z knoop
34! Corrected "Former revisions" section
35!
36! 2696 2017-12-14 17:12:51Z kanani
37! Change in file header (GPL part)
38!
39! 2101 2017-01-05 16:42:31Z suehring
40!
41! 2000 2016-08-20 18:09:15Z knoop
42! Forced header and separation lines into 80 columns
43!
44! 1850 2016-04-08 13:29:27Z maronga
45! Module renamed
46!
47!
48! 1682 2015-10-07 23:56:08Z knoop
49! Code annotations made doxygen readable
50!
51! 1342 2014-03-26 17:04:47Z kanani
52! REAL constants defined as wp-kind
53!
54! 1320 2014-03-20 08:40:49Z raasch
55! ONLY-attribute added to USE-statements,
56! kind-parameters added to all INTEGER and REAL declaration statements,
57! kinds are defined in new module kinds,
58! old module precision_kind is removed,
59! revision history before 2012 removed,
60! comment fields (!:) to be used for variable explanations added to
61! all variable declaration statements
62!
63! 1092 2013-02-02 11:24:22Z raasch
64! unused variables removed
65!
66! 1036 2012-10-22 13:43:42Z raasch
67! code put under GPL (PALM 3.9)
68!
69! RCS Log replace by Id keyword, revision history cleaned up
70!
71! Revision 1.1  1998/02/04 16:09:45  raasch
72! Initial revision
73!
74!
75! Description:
76! ------------
77!> Random number generator, produces numbers equally distributed in interval [0,1]
78!> This routine is taken from the "numerical recipes"
79!------------------------------------------------------------------------------!
80 MODULE random_function_mod
81 
82
83    USE kinds
84
85    IMPLICIT NONE
86
87    PRIVATE
88
89    PUBLIC random_function, random_function_ini
90
91    INTEGER(iwp), PUBLIC, SAVE ::  random_iv(32)  !<
92    INTEGER(iwp), PUBLIC, SAVE ::  random_iy      !<
93
94    INTERFACE random_function_ini
95       MODULE PROCEDURE random_function_ini
96    END INTERFACE random_function_ini
97
98    INTERFACE random_function
99       MODULE PROCEDURE random_function
100    END INTERFACE random_function
101
102 CONTAINS
103
104!------------------------------------------------------------------------------!
105! Description:
106! ------------
107!> @todo Missing subroutine description.
108!------------------------------------------------------------------------------!
109    SUBROUTINE random_function_ini
110
111       IMPLICIT NONE
112
113       random_iv = 0
114       random_iy = 0
115
116    END SUBROUTINE random_function_ini
117
118    FUNCTION random_function( idum )
119
120
121       IMPLICIT NONE
122
123       INTEGER(iwp) ::  ia               !<
124       INTEGER(iwp) ::  idum             !<
125       INTEGER(iwp) ::  im               !<
126       INTEGER(iwp) ::  iq               !<
127       INTEGER(iwp) ::  ir               !<
128       INTEGER(iwp) ::  ndiv             !<
129       INTEGER(iwp) ::  ntab             !<
130
131       INTEGER(iwp) ::  j                !<
132       INTEGER(iwp) ::  k                !<
133
134       REAL(wp)     ::  am               !<
135       REAL(wp)     ::  eps              !<
136       REAL(wp)     ::  random_function  !<
137       REAL(wp)     ::  rnmx             !<
138
139       PARAMETER ( ia=16807, im=2147483647, am=1.0_wp/im, iq=127773, ir=2836, &
140                   ntab=32, ndiv=1+INT(REAL(im-1)/ntab), eps=1.2e-7_wp, rnmx=1.0_wp-eps )
141
142       IF ( idum <= 0  .OR.  random_iy == 0 )  THEN
143          idum = max (-idum,1)
144          DO  j = ntab+8,1,-1
145             k    = idum / iq
146             idum = ia * ( idum - k * iq ) - ir * k
147             IF ( idum < 0 )  idum = idum + im
148             IF ( j <= ntab )  random_iv(j) = idum
149          ENDDO
150          random_iy = random_iv(1)
151       ENDIF
152
153       k    = idum / iq
154       idum = ia * ( idum - k * iq ) - ir * k
155       IF ( idum < 0 )  idum = idum + im
156       j            = 1 + random_iy / ndiv
157       random_iy    = random_iv(j)
158       random_iv(j) = idum
159       random_function  = MIN( am * random_iy , rnmx )
160
161    END FUNCTION random_function
162
163 END MODULE random_function_mod
Note: See TracBrowser for help on using the repository browser.