source: palm/trunk/SOURCE/random_generator_parallel_mod.f90 @ 1850

Last change on this file since 1850 was 1850, checked in by maronga, 8 years ago

added _mod string to several filenames to meet the naming convection for modules

  • Property svn:keywords set to Id
File size: 16.1 KB
Line 
1!> @file random_generator_parallel_mod.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-2016 Leibniz Universitaet Hannover
17!------------------------------------------------------------------------------!
18!
19! Current revisions:
20! -----------------
21! Module renamed
22!
23!
24! Former revisions:
25! -----------------
26! $Id: random_generator_parallel_mod.f90 1850 2016-04-08 13:29:27Z maronga $
27!
28! 1682 2015-10-07 23:56:08Z knoop
29! Code annotations made doxygen readable
30!
31! 1400 2014-05-09 14:03:54Z knoop
32! Initial revision
33!
34!
35! Description:
36! ------------
37!> This module contains and supports the random number generating routine ran_parallel.
38!> ran_parallel returns a uniform random deviate between 0.0 and 1.0
39!> (exclusive of the end point values).
40!> Additionally it provides the generator with five integer for use as initial state space.
41!> The first tree integers (iran, jran, kran) are maintained as non negative values,
42!> while the last two (mran, nran) have 32-bit nonzero values.
43!> Also provided by this module is support for initializing or reinitializing
44!> the state space to a desired standard sequence number, hashing the initial
45!> values to random values, and allocating and deallocating the internal workspace
46!> Random number generator, produces numbers equally distributed in interval
47!>
48!> This routine is taken from the "numerical recipies vol. 2"
49!------------------------------------------------------------------------------!
50MODULE random_generator_parallel
51 
52
53   USE kinds
54   
55   IMPLICIT NONE
56   
57   PRIVATE
58   PUBLIC random_number_parallel, random_seed_parallel, random_dummy,          &
59          id_random_array, seq_random_array
60   
61   INTEGER(isp), SAVE :: lenran=0             !<
62   INTEGER(isp), SAVE :: seq=0                !<
63   INTEGER(isp), SAVE :: iran0                !<
64   INTEGER(isp), SAVE :: jran0                !<
65   INTEGER(isp), SAVE :: kran0                !<
66   INTEGER(isp), SAVE :: mran0                !<
67   INTEGER(isp), SAVE :: nran0                !<
68   INTEGER(isp), SAVE :: rans                 !<
69   
70   INTEGER(isp), DIMENSION(:, :), POINTER, SAVE :: ranseeds   !<
71   
72   INTEGER(isp), DIMENSION(:), POINTER, SAVE :: iran   !<
73   INTEGER(isp), DIMENSION(:), POINTER, SAVE :: jran   !<
74   INTEGER(isp), DIMENSION(:), POINTER, SAVE :: kran   !<
75   INTEGER(isp), DIMENSION(:), POINTER, SAVE :: mran   !<
76   INTEGER(isp), DIMENSION(:), POINTER, SAVE :: nran   !<
77   INTEGER(isp), DIMENSION(:), POINTER, SAVE :: ranv   !<
78   
79   
80   
81   INTEGER(isp), DIMENSION(:,:), ALLOCATABLE   ::  id_random_array    !<
82   INTEGER(isp), DIMENSION(:,:,:), ALLOCATABLE ::  seq_random_array   !<
83   
84   REAL(wp), SAVE :: amm   !<
85   
86   REAL(wp) :: random_dummy=0.0   !<
87   
88   INTERFACE random_number_parallel
89      MODULE PROCEDURE ran0_s
90   END INTERFACE
91   
92   INTERFACE random_seed_parallel
93      MODULE PROCEDURE random_seed_parallel
94   END INTERFACE
95   
96   INTERFACE ran_hash
97      MODULE PROCEDURE ran_hash_v
98   END INTERFACE
99   
100   INTERFACE reallocate
101      MODULE PROCEDURE reallocate_iv,reallocate_im
102   END INTERFACE
103   
104   INTERFACE arth
105      MODULE PROCEDURE arth_i
106   END INTERFACE
107
108 CONTAINS
109 
110!------------------------------------------------------------------------------!
111! Description:
112! ------------
113!> Lagged Fibonacci generator combined with a Marsaglia shiftsequence.
114!> Returns as harvest a uniform random deviate between 0.0 and 1.0 (exclusive of the end point values).
115!> This generator has the same calling and initialization conventions as Fortran 90's random_number routine.
116!> Use random_seed_parallel to initialize or reinitialize to a particular sequence.
117!> The period of this generator is about 2.0 x 10^28, and it fully vectorizes.
118!> Validity of the integer model assumed by this generator is tested at initialization.
119!------------------------------------------------------------------------------!
120   SUBROUTINE ran0_s(harvest)
121
122      USE kinds
123     
124      IMPLICIT NONE
125     
126      REAL(wp), INTENT(OUT) :: harvest   !<
127     
128      IF  (lenran < 1) CALL ran_init(1)  !- Initialization routine in ran_state.
129     
130      !- Update Fibonacci generator, which has period p^2 + p + 1, p = 2^31 - 69.
131      rans = iran0 - kran0   
132     
133      IF  (rans < 0) rans = rans + 2147483579_isp
134     
135      iran0 = jran0
136      jran0 = kran0
137      kran0 = rans
138     
139      nran0 = ieor( nran0, ishft (nran0, 13) ) !- Update Marsaglia shift sequence with period 2^32 - 1.
140      nran0 = ieor( nran0, ishft (nran0, -17) )
141      nran0 = ieor( nran0, ishft (nran0, 5) )
142     
143      rans  = ieor( nran0, rans )   !- Combine the generators.
144     
145      harvest = amm * merge( rans, not(rans), rans < 0 ) !- Make the result positive definite (note that amm is negative).
146     
147   END SUBROUTINE ran0_s
148
149!------------------------------------------------------------------------------!
150! Description:
151! ------------
152!> Initialize or reinitialize the random generator state space to vectors of size length.
153!> The saved variable seq is hashed (via calls to the module routine ran_hash)
154!> to create unique starting seeds, different for each vector component.
155!------------------------------------------------------------------------------!
156   SUBROUTINE ran_init( length )
157   
158      USE kinds
159     
160      IMPLICIT NONE
161     
162      INTEGER(isp), INTENT(IN) ::  length   !<
163   
164      INTEGER(isp), PARAMETER:: hg=huge(1_isp)   !<
165      INTEGER(isp), PARAMETER:: hgm=-hg          !<
166      INTEGER(isp), PARAMETER:: hgng=hgm-1       !<
167     
168      INTEGER(isp) ::  new   !<
169      INTEGER(isp) ::  j     !<
170      INTEGER(isp) ::  hgt   !<
171     
172      IF ( length < lenran ) RETURN !- Simply return if enough space is already allocated.
173     
174      hgt = hg
175     
176      !- The following lines check that kind value isp is in fact a 32-bit integer
177      !- with the usual properties that we expect it to have (under negation and wrap-around addition).
178      !- If all of these tests are satisfied, then the routines that use this module are portable,
179      !- even though they go beyond Fortran 90's integer model.
180     
181      IF  ( hg /= 2147483647 ) CALL ran_error('ran_init: arith assump 1 fails')
182      IF  ( hgng >= 0 )        CALL ran_error('ran_init: arith assump 2 fails')
183      IF  ( hgt+1 /= hgng )    CALL ran_error('ran_init: arith assump 3 fails')
184      IF  ( not(hg) >= 0 )     CALL ran_error('ran_init: arith assump 4 fails')
185      IF  ( not(hgng) < 0 )    CALL ran_error('ran_init: arith assump 5 fails')
186      IF  ( hg+hgng >= 0 )     CALL ran_error('ran_init: arith assump 6 fails')
187      IF  ( not(-1_isp) < 0 )  CALL ran_error('ran_init: arith assump 7 fails')
188      IF  ( not(0_isp) >= 0 )  CALL ran_error('ran_init: arith assump 8 fails')
189      IF  ( not(1_isp) >= 0 )  CALL ran_error('ran_init: arith assump 9 fails')
190     
191      IF  ( lenran > 0) THEN                          !- Reallocate space, or ...
192     
193         ranseeds => reallocate( ranseeds, length, 5)
194         ranv => reallocate( ranv, length-1)
195         new = lenran+1
196         
197      ELSE                                            !- allocate space.
198     
199         ALLOCATE(ranseeds(length,5))
200         ALLOCATE(ranv(length-1))
201         new = 1   !- Index of first location not yet initialized.
202         amm = nearest(1.0_wp,-1.0_wp)/hgng
203         !- Use of nearest is to ensure that returned random deviates are strictly lessthan 1.0.
204         IF  (amm*hgng >= 1.0 .or. amm*hgng <= 0.0)                            &
205            CALL ran_error('ran_init: arith assump 10 fails')
206           
207      END IF 
208     
209      !- Set starting values, unique by seq and vector component.
210      ranseeds(new:,1) = seq
211      ranseeds(new:,2:5)=spread(arth(new,1,size(ranseeds(new:,1))),2,4)
212     
213      DO j=1,4   !- Hash them.
214         CALL ran_hash(ranseeds(new:,j), ranseeds(new:,j+1))
215      END DO
216     
217      WHERE (ranseeds (new: ,1:3) < 0)                                         & 
218         ranseeds(new: ,1:3)=not(ranseeds(new: ,1:3))  !- Enforce nonnegativity.
219         
220      WHERE (ranseeds(new: ,4:5) == 0) ranseeds(new: ,4:5)=1 !- Enforce nonzero.
221     
222      IF  (new == 1) THEN !- Set scalar seeds.
223     
224         iran0 = ranseeds(1,1)
225         jran0 = ranseeds(1,2)
226         kran0 = ranseeds(1,3)
227         mran0 = ranseeds(1,4)
228         nran0 = ranseeds(1,5)
229         rans  = nran0
230         
231      END IF
232     
233      IF  (length > 1) THEN   !- Point to vector seeds.
234     
235         iran => ranseeds(2:,1)
236         jran => ranseeds(2:,2)
237         kran => ranseeds(2:,3)
238         mran => ranseeds(2:,4)
239         nran => ranseeds(2:,5)
240         ranv = nran
241         
242      END IF
243     
244      lenran = length
245     
246   END SUBROUTINE ran_init
247
248!------------------------------------------------------------------------------!
249! Description:
250! ------------
251!> User interface to release the workspace used by the random number routines.
252!------------------------------------------------------------------------------!
253   SUBROUTINE ran_deallocate
254   
255      IF  ( lenran > 0 ) THEN
256     
257         DEALLOCATE(ranseeds, ranv)
258         NULLIFY(ranseeds, ranv, iran, jran, kran, mran, nran)
259         lenran = 0
260         
261      END IF
262     
263   END SUBROUTINE ran_deallocate
264
265!------------------------------------------------------------------------------!
266! Description:
267! ------------
268!> User interface for seeding the random number routines.
269!> Syntax is exactly like Fortran 90's random_seed routine,
270!> with one additional argument keyword: random_sequence, set to any integer
271!> value, causes an immediate new initialization, seeded by that integer.
272!------------------------------------------------------------------------------!
273   SUBROUTINE random_seed_parallel( random_sequence, state_size, put, get )
274   
275      IMPLICIT NONE
276     
277      INTEGER(isp), OPTIONAL, INTENT(IN)  ::  random_sequence   !<
278      INTEGER(isp), OPTIONAL, INTENT(OUT) ::  state_size        !<
279     
280      INTEGER(isp), DIMENSION(:), OPTIONAL, INTENT(IN)  ::  put   !<
281      INTEGER(isp), DIMENSION(:), OPTIONAL, INTENT(OUT) ::  get   !<
282     
283      IF  ( present(state_size) ) THEN
284     
285         state_size = 5 * lenran
286         
287      ELSE IF  ( present(put) ) THEN
288     
289         IF  ( lenran == 0 ) RETURN
290         
291         ranseeds = reshape( put,shape(ranseeds) )
292         
293         WHERE (ranseeds(:,1:3) < 0) ranseeds(: ,1:3) = not(ranseeds(: ,1:3))
294         !- Enforce nonnegativity and nonzero conditions on any user-supplied seeds.
295         
296         WHERE (ranseeds(:,4:5) == 0) ranseeds(:,4:5) = 1
297         
298         iran0 = ranseeds(1,1)
299         jran0 = ranseeds(1,2)
300         kran0 = ranseeds(1,3)
301         mran0 = ranseeds(1,4)
302         nran0 = ranseeds(1,5)
303         
304      ELSE IF  ( present(get) ) THEN
305     
306         IF  (lenran == 0) RETURN
307         
308         ranseeds(1,1:5) = (/ iran0,jran0,kran0,mran0,nran0 /)
309         get = reshape( ranseeds, shape(get) )
310         
311      ELSE IF  ( present(random_sequence) ) THEN
312     
313         CALL ran_deallocate
314         seq = random_sequence
315         
316      END IF
317     
318   END SUBROUTINE random_seed_parallel
319
320!------------------------------------------------------------------------------!
321! Description:
322! ------------
323!> DES-like hashing of two 32-bit integers, using shifts,
324!> xor's, and adds to make the internal nonlinear function.
325!------------------------------------------------------------------------------!
326   SUBROUTINE ran_hash_v( il, ir )
327   
328      IMPLICIT NONE
329     
330      INTEGER(isp), DIMENSION(:), INTENT(INOUT) ::  il   !<
331      INTEGER(isp), DIMENSION(:), INTENT(INOUT) ::  ir   !<
332     
333      INTEGER(isp), DIMENSION(size(il)) ::  is   !<
334     
335      INTEGER(isp) :: j   !<
336     
337      DO j=1,4
338     
339         is = ir
340         ir = ieor( ir, ishft(ir,5) ) + 1422217823
341         ir = ieor( ir, ishft(ir,-16) ) + 1842055030
342         ir = ieor( ir, ishft(ir,9) ) + 80567781
343         ir = ieor( il, ir )
344         il = is
345      END DO
346     
347   END SUBROUTINE ran_hash_v
348
349!------------------------------------------------------------------------------!
350! Description:
351! ------------
352!> User interface to process error-messages
353!> produced by the random_number_parallel module
354!------------------------------------------------------------------------------!
355   SUBROUTINE ran_error(string)
356   
357      CHARACTER(LEN=*), INTENT(IN) ::  string   !<
358     
359      write (*,*) 'Error in module random_number_parallel: ',string
360     
361      STOP 'Program terminated by ran_error'
362     
363   END SUBROUTINE ran_error
364
365!------------------------------------------------------------------------------!
366! Description:
367! ------------
368!> Reallocates the generators state space "ranseeds" to vectors of size length.
369!------------------------------------------------------------------------------!
370   FUNCTION reallocate_iv( p, n )
371   
372      INTEGER(isp), DIMENSION(:), POINTER ::  p               !<
373      INTEGER(isp), DIMENSION(:), POINTER ::  reallocate_iv   !<
374     
375      INTEGER(isp), INTENT(IN) ::  n   !<
376     
377      INTEGER(isp) ::  nold   !<
378      INTEGER(isp) ::  ierr   !<
379     
380      ALLOCATE(reallocate_iv(n),stat=ierr)
381     
382      IF (ierr /= 0) CALL                                                      &
383         ran_error('reallocate_iv: problem in attempt to allocate memory')
384         
385      IF (.not. associated(p)) RETURN
386     
387      nold = size(p)
388     
389      reallocate_iv(1:min(nold,n)) = p(1:min(nold,n))
390     
391      DEALLOCATE(p)
392     
393   END FUNCTION reallocate_iv
394   
395   FUNCTION reallocate_im( p, n, m )
396   
397      INTEGER(isp), DIMENSION(:,:), POINTER ::  p               !<
398      INTEGER(isp), DIMENSION(:,:), POINTER ::  reallocate_im   !<
399     
400      INTEGER(isp), INTENT(IN) ::  m   !<
401      INTEGER(isp), INTENT(IN) ::  n   !<
402     
403      INTEGER(isp) ::  mold   !<
404      INTEGER(isp) ::  nold   !<
405      INTEGER(isp) ::  ierr   !<
406     
407      ALLOCATE(reallocate_im(n,m),stat=ierr)
408     
409      IF (ierr /= 0) CALL                                                      &
410         ran_error('reallocate_im: problem in attempt to allocate memory')
411         
412      IF (.not. associated(p)) RETURN
413     
414      nold = size(p,1)
415      mold = size(p,2)
416     
417      reallocate_im(1:min(nold,n),1:min(mold,m)) =                             &
418         p(1:min(nold,n),1:min(mold,m))
419         
420      DEALLOCATE(p)
421     
422   END FUNCTION reallocate_im
423
424!------------------------------------------------------------------------------!
425! Description:
426! ------------
427!> Reallocates the generators state space "ranseeds" to vectors of size length.
428!------------------------------------------------------------------------------!
429   FUNCTION arth_i(first,increment,n)
430   
431      INTEGER(isp), INTENT(IN) ::  first       !<
432      INTEGER(isp), INTENT(IN) ::  increment   !<
433      INTEGER(isp), INTENT(IN) ::  n           !<
434     
435      INTEGER(isp), DIMENSION(n) ::  arth_i    !<
436     
437      INTEGER(isp) ::  k      !<
438      INTEGER(isp) ::  k2     !<
439      INTEGER(isp) ::  temp   !<
440     
441      INTEGER(isp), PARAMETER ::  npar_arth=16   !<
442      INTEGER(isp), PARAMETER ::  npar2_arth=8   !<
443     
444      IF (n > 0) arth_i(1) = first
445     
446      IF (n <= npar_arth) THEN
447     
448         DO k=2,n
449            arth_i(k) = arth_i(k-1)+increment
450         END DO
451         
452      ELSE
453     
454         DO k=2,npar2_arth
455            arth_i(k) = arth_i(k-1) + increment
456         END DO
457         
458         temp = increment * npar2_arth
459         k = npar2_arth
460         
461         DO
462            IF (k >= n) EXIT
463            k2 = k + k
464            arth_i(k+1:min(k2,n)) = temp + arth_i(1:min(k,n-k))
465            temp = temp + temp
466            k = k2
467         END DO
468         
469      END IF
470     
471   END FUNCTION arth_i
472
473END MODULE random_generator_parallel
Note: See TracBrowser for help on using the repository browser.