[1682] | 1 | !> @file random_gauss.f90 |
---|
[2000] | 2 | !------------------------------------------------------------------------------! |
---|
[1036] | 3 | ! This file is part of PALM. |
---|
| 4 | ! |
---|
[2000] | 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. |
---|
[1036] | 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 | ! |
---|
[2101] | 17 | ! Copyright 1997-2017 Leibniz Universitaet Hannover |
---|
[2000] | 18 | !------------------------------------------------------------------------------! |
---|
[1036] | 19 | ! |
---|
[484] | 20 | ! Current revisions: |
---|
[1] | 21 | ! ----------------- |
---|
[1321] | 22 | ! |
---|
[2001] | 23 | ! |
---|
[1321] | 24 | ! Former revisions: |
---|
| 25 | ! ----------------- |
---|
| 26 | ! $Id: random_gauss.f90 2101 2017-01-05 16:42:31Z knoop $ |
---|
| 27 | ! |
---|
[2001] | 28 | ! 2000 2016-08-20 18:09:15Z knoop |
---|
| 29 | ! Forced header and separation lines into 80 columns |
---|
| 30 | ! |
---|
[1683] | 31 | ! 1682 2015-10-07 23:56:08Z knoop |
---|
| 32 | ! Code annotations made doxygen readable |
---|
| 33 | ! |
---|
[1343] | 34 | ! 1342 2014-03-26 17:04:47Z kanani |
---|
| 35 | ! REAL constants defined as wp-kind |
---|
| 36 | ! |
---|
[1321] | 37 | ! 1320 2014-03-20 08:40:49Z raasch |
---|
[1320] | 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 |
---|
[1037] | 45 | ! |
---|
| 46 | ! 1036 2012-10-22 13:43:42Z raasch |
---|
| 47 | ! code put under GPL (PALM 3.9) |
---|
| 48 | ! |
---|
[3] | 49 | ! RCS Log replace by Id keyword, revision history cleaned up |
---|
| 50 | ! |
---|
[1] | 51 | ! Revision 1.1 1998/03/25 20:09:47 raasch |
---|
| 52 | ! Initial revision |
---|
| 53 | ! |
---|
| 54 | ! |
---|
| 55 | ! Description: |
---|
| 56 | ! ------------ |
---|
[1682] | 57 | !> Generates a gaussian distributed random number (mean value 1, sigma = 1) |
---|
| 58 | !> This routine is taken from the "numerical recipies". |
---|
[1] | 59 | !------------------------------------------------------------------------------! |
---|
[1682] | 60 | FUNCTION random_gauss( idum, upper_limit ) |
---|
| 61 | |
---|
[1] | 62 | |
---|
[1320] | 63 | USE kinds |
---|
[1] | 64 | |
---|
[1320] | 65 | USE random_function_mod, & |
---|
| 66 | ONLY: random_function |
---|
| 67 | |
---|
[1] | 68 | IMPLICIT NONE |
---|
| 69 | |
---|
[1682] | 70 | INTEGER(iwp) :: idum !< |
---|
| 71 | INTEGER(iwp) :: iset !< |
---|
[1] | 72 | |
---|
[1682] | 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 !< |
---|
[1320] | 80 | |
---|
[1] | 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 |
---|
[1342] | 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 |
---|
[1] | 95 | rsq = v1**2 + v2**2 |
---|
| 96 | ENDDO |
---|
[1342] | 97 | fac = SQRT( -2.0_wp * LOG( rsq ) / rsq ) |
---|
[1] | 98 | gset = v1 * fac |
---|
[1342] | 99 | random_gauss = v2 * fac + 1.0_wp |
---|
[1] | 100 | iset = 1 |
---|
| 101 | ELSE |
---|
[1342] | 102 | random_gauss = gset + 1.0_wp |
---|
[1] | 103 | iset = 0 |
---|
| 104 | ENDIF |
---|
| 105 | |
---|
[1342] | 106 | IF ( ABS( random_gauss - 1.0_wp ) <= upper_limit ) EXIT |
---|
[1] | 107 | |
---|
| 108 | ENDDO |
---|
| 109 | |
---|
| 110 | END FUNCTION random_gauss |
---|