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

Last change on this file since 1682 was 1682, checked in by knoop, 9 years ago

Code annotations made doxygen readable

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