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

Last change on this file since 4186 was 4182, checked in by scharf, 5 years ago
  • corrected "Former revisions" section
  • minor formatting in "Former revisions" section
  • added "Author" section
  • Property svn:keywords set to Id
File size: 3.9 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 4182 2019-08-22 15:20:23Z suehring $
27! Corrected "Former revisions" section
28!
29! 4144 2019-08-06 09:11:47Z raasch
30! relational operators .EQ., .NE., etc. replaced by ==, /=, etc.
31!
32! 3802 2019-03-17 13:33:42Z raasch
33! type conversion added to avoid compiler warning about constant integer
34! division truncation
35!
36! 3655 2019-01-07 16:51:22Z knoop
37! Corrected "Former revisions" section
38!
39! Revision 1.1  1998/02/04 16:09:45  raasch
40! Initial revision
41!
42!
43! Description:
44! ------------
45!> Random number generator, produces numbers equally distributed in interval [0,1]
46!> This routine is taken from the "numerical recipes"
47!------------------------------------------------------------------------------!
48 MODULE random_function_mod
49 
50
51    USE kinds
52
53    IMPLICIT NONE
54
55    PRIVATE
56
57    PUBLIC random_function, random_function_ini
58
59    INTEGER(iwp), PUBLIC, SAVE ::  random_iv(32)  !<
60    INTEGER(iwp), PUBLIC, SAVE ::  random_iy      !<
61
62    INTERFACE random_function_ini
63       MODULE PROCEDURE random_function_ini
64    END INTERFACE random_function_ini
65
66    INTERFACE random_function
67       MODULE PROCEDURE random_function
68    END INTERFACE random_function
69
70 CONTAINS
71
72!------------------------------------------------------------------------------!
73! Description:
74! ------------
75!> @todo Missing subroutine description.
76!------------------------------------------------------------------------------!
77    SUBROUTINE random_function_ini
78
79       IMPLICIT NONE
80
81       random_iv = 0
82       random_iy = 0
83
84    END SUBROUTINE random_function_ini
85
86    FUNCTION random_function( idum )
87
88
89       IMPLICIT NONE
90
91       INTEGER(iwp) ::  ia               !<
92       INTEGER(iwp) ::  idum             !<
93       INTEGER(iwp) ::  im               !<
94       INTEGER(iwp) ::  iq               !<
95       INTEGER(iwp) ::  ir               !<
96       INTEGER(iwp) ::  ndiv             !<
97       INTEGER(iwp) ::  ntab             !<
98
99       INTEGER(iwp) ::  j                !<
100       INTEGER(iwp) ::  k                !<
101
102       REAL(wp)     ::  am               !<
103       REAL(wp)     ::  eps              !<
104       REAL(wp)     ::  random_function  !<
105       REAL(wp)     ::  rnmx             !<
106
107       PARAMETER ( ia=16807, im=2147483647, am=1.0_wp/im, iq=127773, ir=2836, &
108                   ntab=32, ndiv=1+INT(REAL(im-1)/ntab), eps=1.2e-7_wp, rnmx=1.0_wp-eps )
109
110       IF ( idum <= 0  .OR.  random_iy == 0 )  THEN
111          idum = max (-idum,1)
112          DO  j = ntab+8,1,-1
113             k    = idum / iq
114             idum = ia * ( idum - k * iq ) - ir * k
115             IF ( idum < 0 )  idum = idum + im
116             IF ( j <= ntab )  random_iv(j) = idum
117          ENDDO
118          random_iy = random_iv(1)
119       ENDIF
120
121       k    = idum / iq
122       idum = ia * ( idum - k * iq ) - ir * k
123       IF ( idum < 0 )  idum = idum + im
124       j            = 1 + random_iy / ndiv
125       random_iy    = random_iv(j)
126       random_iv(j) = idum
127       random_function  = MIN( am * random_iy , rnmx )
128
129    END FUNCTION random_function
130
131 END MODULE random_function_mod
Note: See TracBrowser for help on using the repository browser.