1 | !> @file random_gauss.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-2017 Leibniz Universitaet Hannover |
---|
18 | !------------------------------------------------------------------------------! |
---|
19 | ! |
---|
20 | ! Current revisions: |
---|
21 | ! ----------------- |
---|
22 | ! |
---|
23 | ! |
---|
24 | ! Former revisions: |
---|
25 | ! ----------------- |
---|
26 | ! $Id: random_gauss.f90 2696 2017-12-14 17:12:51Z raasch $ |
---|
27 | ! |
---|
28 | ! 2000 2016-08-20 18:09:15Z knoop |
---|
29 | ! Forced header and separation lines into 80 columns |
---|
30 | ! |
---|
31 | ! 1682 2015-10-07 23:56:08Z knoop |
---|
32 | ! Code annotations made doxygen readable |
---|
33 | ! |
---|
34 | ! 1342 2014-03-26 17:04:47Z kanani |
---|
35 | ! REAL constants defined as wp-kind |
---|
36 | ! |
---|
37 | ! 1320 2014-03-20 08:40:49Z raasch |
---|
38 | ! ONLY-attribute added to USE-statements, |
---|
39 | ! kind-parameters added to all INTEGER and REAL declaration statements, |
---|
40 | ! kinds are defined in new module kinds, |
---|
41 | ! old module precision_kind is removed, |
---|
42 | ! revision history before 2012 removed, |
---|
43 | ! comment fields (!:) to be used for variable explanations added to |
---|
44 | ! all variable declaration statements |
---|
45 | ! |
---|
46 | ! 1036 2012-10-22 13:43:42Z raasch |
---|
47 | ! code put under GPL (PALM 3.9) |
---|
48 | ! |
---|
49 | ! RCS Log replace by Id keyword, revision history cleaned up |
---|
50 | ! |
---|
51 | ! Revision 1.1 1998/03/25 20:09:47 raasch |
---|
52 | ! Initial revision |
---|
53 | ! |
---|
54 | ! |
---|
55 | ! Description: |
---|
56 | ! ------------ |
---|
57 | !> Generates a gaussian distributed random number (mean value 1, sigma = 1) |
---|
58 | !> This routine is taken from the "numerical recipies". |
---|
59 | !------------------------------------------------------------------------------! |
---|
60 | FUNCTION random_gauss( idum, upper_limit ) |
---|
61 | |
---|
62 | |
---|
63 | USE kinds |
---|
64 | |
---|
65 | USE random_function_mod, & |
---|
66 | ONLY: random_function |
---|
67 | |
---|
68 | IMPLICIT NONE |
---|
69 | |
---|
70 | INTEGER(iwp) :: idum !< |
---|
71 | INTEGER(iwp) :: iset !< |
---|
72 | |
---|
73 | REAL(wp) :: fac !< |
---|
74 | REAL(wp) :: gset !< |
---|
75 | REAL(wp) :: random_gauss !< |
---|
76 | REAL(wp) :: rsq !< |
---|
77 | REAL(wp) :: upper_limit !< |
---|
78 | REAL(wp) :: v1 !< |
---|
79 | REAL(wp) :: v2 !< |
---|
80 | |
---|
81 | SAVE iset, gset |
---|
82 | |
---|
83 | DATA iset /0/ |
---|
84 | |
---|
85 | ! |
---|
86 | !-- Random numbers are created as long as they do not fall below the given |
---|
87 | !-- upper limit |
---|
88 | DO |
---|
89 | |
---|
90 | IF ( iset == 0 ) THEN |
---|
91 | rsq = 0.0_wp |
---|
92 | DO WHILE ( rsq >= 1.0_wp .OR. rsq == 0.0_wp ) |
---|
93 | v1 = 2.0_wp * random_function( idum ) - 1.0_wp |
---|
94 | v2 = 2.0_wp * random_function( idum ) - 1.0_wp |
---|
95 | rsq = v1**2 + v2**2 |
---|
96 | ENDDO |
---|
97 | fac = SQRT( -2.0_wp * LOG( rsq ) / rsq ) |
---|
98 | gset = v1 * fac |
---|
99 | random_gauss = v2 * fac + 1.0_wp |
---|
100 | iset = 1 |
---|
101 | ELSE |
---|
102 | random_gauss = gset + 1.0_wp |
---|
103 | iset = 0 |
---|
104 | ENDIF |
---|
105 | |
---|
106 | IF ( ABS( random_gauss - 1.0_wp ) <= upper_limit ) EXIT |
---|
107 | |
---|
108 | ENDDO |
---|
109 | |
---|
110 | END FUNCTION random_gauss |
---|