source: palm/trunk/SOURCE/random_gauss.f90 @ 1433

Last change on this file since 1433 was 1343, checked in by kanani, 11 years ago

last commit documented

  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
1 FUNCTION random_gauss( idum, upper_limit )
2
3!--------------------------------------------------------------------------------!
4! This file is part of PALM.
5!
6! PALM is free software: you can redistribute it and/or modify it under the terms
7! of the GNU General Public License as published by the Free Software Foundation,
8! either version 3 of the License, or (at your option) any later 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-2014 Leibniz Universitaet Hannover
18!--------------------------------------------------------------------------------!
19!
20! Current revisions:
21! -----------------
22!
23!
24! Former revisions:
25! -----------------
26! $Id: random_gauss.f90 1343 2014-03-26 17:07:58Z keck $
27!
28! 1342 2014-03-26 17:04:47Z kanani
29! REAL constants defined as wp-kind
30!
31! 1320 2014-03-20 08:40:49Z raasch
32! ONLY-attribute added to USE-statements,
33! kind-parameters added to all INTEGER and REAL declaration statements,
34! kinds are defined in new module kinds,
35! old module precision_kind is removed,
36! revision history before 2012 removed,
37! comment fields (!:) to be used for variable explanations added to
38! all variable declaration statements
39!
40! 1036 2012-10-22 13:43:42Z raasch
41! code put under GPL (PALM 3.9)
42!
43! RCS Log replace by Id keyword, revision history cleaned up
44!
45! Revision 1.1  1998/03/25 20:09:47  raasch
46! Initial revision
47!
48!
49! Description:
50! ------------
51! Generates a gaussian distributed random number (mean value 1, sigma = 1)
52! This routine is taken from the "numerical recipies".
53!------------------------------------------------------------------------------!
54
55    USE kinds
56
57    USE random_function_mod,                                                   &
58        ONLY:  random_function
59
60    IMPLICIT NONE
61
62    INTEGER(iwp) ::  idum          !:
63    INTEGER(iwp) ::  iset          !:
64
65    REAL(wp)     ::  fac           !:
66    REAL(wp)     ::  gset          !:
67    REAL(wp)     ::  random_gauss  !:
68    REAL(wp)     ::  rsq           !:
69    REAL(wp)     ::  upper_limit   !:
70    REAL(wp)     ::  v1            !:
71    REAL(wp)     ::  v2            !:
72
73    SAVE  iset, gset
74
75    DATA  iset /0/
76
77!
78!-- Random numbers are created as long as they do not fall below the given
79!-- upper limit
80    DO
81
82       IF ( iset == 0 )  THEN
83          rsq = 0.0_wp
84          DO  WHILE ( rsq >= 1.0_wp  .OR.  rsq == 0.0_wp )
85             v1  = 2.0_wp * random_function( idum ) - 1.0_wp
86             v2  = 2.0_wp * random_function( idum ) - 1.0_wp
87             rsq = v1**2 + v2**2
88          ENDDO
89          fac          = SQRT( -2.0_wp * LOG( rsq ) / rsq )
90          gset         = v1 * fac
91          random_gauss = v2 * fac + 1.0_wp
92          iset         = 1
93       ELSE
94          random_gauss = gset + 1.0_wp
95          iset         = 0
96       ENDIF
97
98       IF ( ABS( random_gauss - 1.0_wp ) <= upper_limit )  EXIT
99
100    ENDDO
101
102 END FUNCTION random_gauss
Note: See TracBrowser for help on using the repository browser.