[1682] | 1 | !> @file random_gauss.f90 |
---|
[2000] | 2 | !------------------------------------------------------------------------------! |
---|
[2696] | 3 | ! This file is part of the PALM model system. |
---|
[1036] | 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 | ! |
---|
[4360] | 17 | ! Copyright 1997-2020 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 4360 2020-01-07 11:25:50Z monakurppa $ |
---|
[2716] | 27 | ! Corrected "Former revisions" section |
---|
| 28 | ! |
---|
[4182] | 29 | ! 3655 2019-01-07 16:51:22Z knoop |
---|
| 30 | ! Corrected "Former revisions" section |
---|
[1321] | 31 | ! |
---|
[4182] | 32 | ! Revision 1.1 1998/03/25 20:09:47 raasch |
---|
| 33 | ! Initial revision |
---|
| 34 | ! |
---|
| 35 | ! |
---|
[1] | 36 | ! Description: |
---|
| 37 | ! ------------ |
---|
[1682] | 38 | !> Generates a gaussian distributed random number (mean value 1, sigma = 1) |
---|
| 39 | !> This routine is taken from the "numerical recipies". |
---|
[1] | 40 | !------------------------------------------------------------------------------! |
---|
[1682] | 41 | FUNCTION random_gauss( idum, upper_limit ) |
---|
| 42 | |
---|
[1] | 43 | |
---|
[1320] | 44 | USE kinds |
---|
[1] | 45 | |
---|
[1320] | 46 | USE random_function_mod, & |
---|
| 47 | ONLY: random_function |
---|
| 48 | |
---|
[1] | 49 | IMPLICIT NONE |
---|
| 50 | |
---|
[1682] | 51 | INTEGER(iwp) :: idum !< |
---|
| 52 | INTEGER(iwp) :: iset !< |
---|
[1] | 53 | |
---|
[1682] | 54 | REAL(wp) :: fac !< |
---|
| 55 | REAL(wp) :: gset !< |
---|
| 56 | REAL(wp) :: random_gauss !< |
---|
| 57 | REAL(wp) :: rsq !< |
---|
| 58 | REAL(wp) :: upper_limit !< |
---|
| 59 | REAL(wp) :: v1 !< |
---|
| 60 | REAL(wp) :: v2 !< |
---|
[1320] | 61 | |
---|
[1] | 62 | SAVE iset, gset |
---|
| 63 | |
---|
| 64 | DATA iset /0/ |
---|
| 65 | |
---|
| 66 | ! |
---|
| 67 | !-- Random numbers are created as long as they do not fall below the given |
---|
| 68 | !-- upper limit |
---|
| 69 | DO |
---|
| 70 | |
---|
| 71 | IF ( iset == 0 ) THEN |
---|
[1342] | 72 | rsq = 0.0_wp |
---|
| 73 | DO WHILE ( rsq >= 1.0_wp .OR. rsq == 0.0_wp ) |
---|
| 74 | v1 = 2.0_wp * random_function( idum ) - 1.0_wp |
---|
| 75 | v2 = 2.0_wp * random_function( idum ) - 1.0_wp |
---|
[1] | 76 | rsq = v1**2 + v2**2 |
---|
| 77 | ENDDO |
---|
[1342] | 78 | fac = SQRT( -2.0_wp * LOG( rsq ) / rsq ) |
---|
[1] | 79 | gset = v1 * fac |
---|
[1342] | 80 | random_gauss = v2 * fac + 1.0_wp |
---|
[1] | 81 | iset = 1 |
---|
| 82 | ELSE |
---|
[1342] | 83 | random_gauss = gset + 1.0_wp |
---|
[1] | 84 | iset = 0 |
---|
| 85 | ENDIF |
---|
| 86 | |
---|
[1342] | 87 | IF ( ABS( random_gauss - 1.0_wp ) <= upper_limit ) EXIT |
---|
[1] | 88 | |
---|
| 89 | ENDDO |
---|
| 90 | |
---|
| 91 | END FUNCTION random_gauss |
---|