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