ROMS
Loading...
Searching...
No Matches
white_noise.F
Go to the documentation of this file.
1#include "cppdefs.h"
3!
4!git $Id$
5!================================================== Hernan G. Arango ===
6! Copyright (c) 2002-2025 The ROMS Group !
7! Licensed under a MIT/X style license !
8! See License_ROMS.md !
9!=======================================================================
10! !
11! These routines generates "white noise" arrays with random numbers !
12! over specified range. These random numbers are scaled to insure, !
13! approximately, zero mean expectation and unit variance. !
14! !
15! Several random number generation schemes are allowed: !
16! !
17! Rscheme = 0 Use F90 intrinsic random numbers generator, !
18! 0 <= R < 1 !
19! Rscheme = 1 Random deviates with Gaussian distribution, !
20! -1 < R < 1 !
21! !
22! Routines: !
23! !
24! white_noise1d Random numbers for 1D arrays !
25! white_noise2d Random numbers for 2D arrays !
26! white_noise2d_bry Random numbers for 2D boundary arrays !
27! white_noise3d Random numbers for 3D arrays !
28! white_noise3d_bry Random numbers for 3D boundary arrays !
29! !
30!=======================================================================
31!
32 USE mod_kinds
33
34 implicit none
35
36 PRIVATE
37
38 PUBLIC :: white_noise1d
39 PUBLIC :: white_noise2d
40 PUBLIC :: white_noise2d_bry
41#ifdef SOLVE3D
42 PUBLIC :: white_noise3d
43 PUBLIC :: white_noise3d_bry
44#endif
45
46 CONTAINS
47!
48!***********************************************************************
49 SUBROUTINE white_noise1d (ng, model, Rscheme, &
50 & Imin, Imax, LBi, UBi, &
51 & Rmin, Rmax, R)
52!***********************************************************************
53!
54 USE mod_param
55 USE mod_parallel
56 USE mod_scalars
57!
58 USE nrutil, ONLY : gasdev
59!
60! Imported variable declarations.
61!
62 integer, intent(in) :: ng, model, rscheme
63 integer, intent(in) :: imin, imax, lbi, ubi
64
65 real(r8), intent(out) :: rmin, rmax
66
67#ifdef ASSUMED_SHAPE
68 real(r8), intent(out) :: r(lbi:)
69#else
70 real(r8), intent(out) :: r(lbi:ubi)
71#endif
72!
73! Local variable declarations.
74!
75 integer :: i, ic
76
77 real(r8), parameter :: fac = 2.0_r8 * 1.73205080756887720_r8
78
79 real(r8), dimension((UBi-LBi+1)) :: random
80!
81!-----------------------------------------------------------------------
82! Generate an array of random numbers.
83!-----------------------------------------------------------------------
84!
85! Initialize output array.
86!
87 DO i=lbi,ubi
88 r(i)=0.0_r8
89 END DO
90!
91! F90 intrinsic pseudorandom numbers with an uniform distribution
92! over the range 0 <= R < 1.
93!
94 IF (rscheme.eq.0) THEN
95 CALL random_number (r(lbi:))
96!
97! Scale (fac=2*SQRT(3)) the random number to insure the expectation
98! mean to be approximately zero, E(R)=0, and the expectation variance
99! to be approximately unity, E(R^2)=1 (Bennett, 2002; book page 72).
100!
101! Recall that,
102!
103! E(R) = sum[R f(R)]
104!
105! where F(R) is the random variable probability distribution.
106!
107 DO i=imin,imax
108 r(i)=fac*(r(i)-0.5_r8)
109 END DO
110 rmin=minval(r)
111 rmax=maxval(r)
112!
113! Random deviates with a Gaussian (normal) distribuiton over the
114! range from -1 to 1.
115!
116 ELSE IF (rscheme.eq.1) THEN
117 IF (master) THEN
118 CALL gasdev (random)
119 rmin=minval(random)
120 rmax=maxval(random)
121 END IF
122 ic=0
123 DO i=imin,imax
124 ic=ic+1
125 r(i)=random(ic)
126 END DO
127 END IF
128
129 RETURN
130 END SUBROUTINE white_noise1d
131
132!
133!***********************************************************************
134 SUBROUTINE white_noise2d (ng, model, gtype, Rscheme, &
135 & Imin, Imax, Jmin, Jmax, &
136 & LBi, UBi, LBj, UBj, &
137 & Rmin, Rmax, R)
138!***********************************************************************
139!
140 USE mod_param
141 USE mod_parallel
142 USE mod_scalars
143!
144#ifdef DISTRIBUTE
145 USE distribute_mod, ONLY : mp_scatter2d
146#endif
147 USE nrutil, ONLY : gasdev
148!
149! Imported variable declarations.
150!
151 integer, intent(in) :: ng, model, gtype, rscheme
152 integer, intent(in) :: imin, imax, jmin, jmax
153 integer, intent(in) :: lbi, ubi, lbj, ubj
154
155 real(r8), intent(out) :: rmin, rmax
156
157 real(r8), intent(out) :: r(lbi:ubi,lbj:ubj)
158!
159! Local variable declarations.
160!
161 integer :: npts, i, ic, j
162
163 real(r8), parameter :: fac = 2.0_r8 * 1.73205080756887720_r8
164
165 real(r8), allocatable :: random(:)
166!
167!-----------------------------------------------------------------------
168! Generate an array of random numbers.
169!-----------------------------------------------------------------------
170!
171! Initialize output array.
172!
173 DO j=lbj,ubj
174 DO i=lbi,ubi
175 r(i,j)=0.0_r8
176 END DO
177 END DO
178!
179! F90 intrinsic pseudorandom numbers with an uniform distribution
180! over the range 0 <= R < 1.
181!
182 IF (rscheme.eq.0) THEN
183 CALL random_number (r(lbi:,lbj:))
184!
185! Scale (fac=2*SQRT(3)) the random number to insure the expectation
186! mean to be approximately zero, E(R)=0, and the expectation variance
187! to be approximately unity, E(R^2)=1 (Bennett, 2002; book page 72).
188!
189! Recall that,
190!
191! E(R) = sum[R f(R)]
192!
193! where F(R) is the random variable probability distribution.
194!
195 DO j=jmin,jmax
196 DO i=imin,imax
197 r(i,j)=fac*(r(i,j)-0.5_r8)
198 END DO
199 END DO
200 rmin=minval(r)
201 rmax=maxval(r)
202!
203! Random deviates with a Gaussian (normal) distribuiton with zero
204! mean and unit variance.
205!
206 ELSE IF (rscheme.eq.1) THEN
207 npts=(lm(ng)+2)*(mm(ng)+2)
208 IF (.not.allocated(random)) THEN
209 allocate ( random(npts+2) )
210 END IF
211 IF (master) THEN
212 CALL gasdev (random)
213 rmin=random(1)
214 rmax=random(1)
215 DO ic=1,npts
216 rmin=min(rmin,random(ic))
217 rmax=max(rmax,random(ic))
218 END DO
219 END IF
220#ifdef DISTRIBUTE
221 npts=SIZE(random)
222 CALL mp_scatter2d (ng, model, lbi, ubi, lbj, ubj, &
223 & nghostpoints, gtype, rmin, rmax, &
224# if defined READ_WATER && defined MASKING
225 & (lm(ng)+2)*(mm(ng)+2), &
226 & scalars(ng)%IJwater(:,gtype), &
227# endif
228 & npts, random, r)
229#else
230 ic=0
231 DO j=jmin,jmax
232 DO i=imin,imax
233 ic=ic+1
234 r(i,j)=random(ic)
235 END DO
236 END DO
237#endif
238 IF (allocated(random)) THEN
239 deallocate (random)
240 END IF
241 END IF
242
243 RETURN
244 END SUBROUTINE white_noise2d
245
246!
247!***********************************************************************
248 SUBROUTINE white_noise2d_bry (ng, tile, model, boundary, Rscheme, &
249 & Imin, Imax, &
250 & LBij, UBij, &
251 & Rmin, Rmax, R)
252!***********************************************************************
253!
254 USE mod_param
255 USE mod_parallel
256 USE mod_scalars
257!
258#ifdef DISTRIBUTE
259 USE distribute_mod, ONLY : mp_bcastf
261#endif
262 USE nrutil, ONLY : gasdev
263!
264! Imported variable declarations.
265!
266 integer, intent(in) :: ng, tile, model, boundary, rscheme
267 integer, intent(in) :: imin, imax
268 integer, intent(in) :: lbij, ubij
269
270 real(r8), intent(out) :: rmin, rmax
271
272#ifdef ASSUMED_SHAPE
273 real(r8), intent(out) :: r(lbij:)
274#else
275 real(r8), intent(out) :: r(lbij:ubij)
276#endif
277!
278! Local variable declarations.
279!
280 integer :: ioff, i, ic
281
282 real(r8), parameter :: fac = 2.0_r8 * 1.73205080756887720_r8
283
284 real(r8), dimension(UBij-LBij+1) :: random
285!
286!-----------------------------------------------------------------------
287! Generate an array of random numbers.
288!-----------------------------------------------------------------------
289!
290! Initialize output array.
291!
292 DO i=lbij,ubij
293 r(i)=0.0_r8
294 END DO
295!
296! F90 intrinsic pseudorandom numbers with an uniform distribution
297! over the range 0 <= R < 1.
298!
299 IF (rscheme.eq.0) THEN
300 CALL random_number (r(lbij:))
301!
302! Scale (fac=2*SQRT(3)) the random number to insure the expectation
303! mean to be approximately zero, E(R)=0, and the expectation variance
304! to be approximately unity, E(R^2)=1 (Bennett, 2002; book page 72).
305!
306! Recall that,
307!
308! E(R) = sum[R f(R)]
309!
310! where F(R) is the random variable probability distribution.
311!
312 DO i=imin,imax
313 r(i)=fac*(r(i)-0.5_r8)
314 END DO
315 rmin=minval(r)
316 rmax=maxval(r)
317!
318! Random deviates with a Gaussian (normal) distribuiton with zero
319! mean and unit variance.
320!
321 ELSE IF (rscheme.eq.1) THEN
322 IF (master) THEN
323 CALL gasdev (random)
324 END IF
325#ifdef DISTRIBUTE
326 CALL mp_bcastf (ng, model, random)
327#endif
328 rmin=minval(random)
329 rmax=maxval(random)
330
331 ioff=1-lbij
332 DO i=imin,imax
333 ic=i+ioff
334 r(i)=random(ic)
335 END DO
336#ifdef DISTRIBUTE
337 CALL mp_exchange2d_bry (ng, tile, model, 1, boundary, &
338 & lbij, ubij, &
339 & nghostpoints, &
340 & ewperiodic(ng), nsperiodic(ng), &
341 & r)
342#endif
343 END IF
344
345 RETURN
346 END SUBROUTINE white_noise2d_bry
347
348
349#ifdef SOLVE3D
350!
351!***********************************************************************
352 SUBROUTINE white_noise3d (ng, model, gtype, Rscheme, &
353 & Imin, Imax, Jmin, Jmax, &
354 & LBi, UBi, LBj, UBj, LBk, UBk, &
355 & Rmin, Rmax, R)
356!***********************************************************************
357!
358 USE mod_param
359 USE mod_parallel
360 USE mod_scalars
361!
362# ifdef DISTRIBUTE
363 USE distribute_mod, ONLY : mp_scatter3d
364# endif
365 USE nrutil, ONLY : gasdev
366!
367! Imported variable declarations.
368!
369 integer, intent(in) :: ng, model, gtype, rscheme
370 integer, intent(in) :: imin, imax, jmin, jmax
371 integer, intent(in) :: lbi, ubi, lbj, ubj, lbk, ubk
372
373 real(r8), intent(out) :: rmin, rmax
374
375# ifdef ASSUMED_SHAPE
376 real(r8), intent(out) :: r(lbi:,lbj:,lbk:)
377# else
378 real(r8), intent(out) :: r(lbi:ubi,lbj:ubj,lbk:ubk)
379# endif
380!
381! Local variable declarations.
382!
383 integer :: npts, i, ic, j, k
384
385 real(r8), parameter :: fac = 2.0_r8 * 1.73205080756887720_r8
386
387 real(r8), allocatable :: random(:)
388!
389!-----------------------------------------------------------------------
390! Generate an array of random numbers.
391!-----------------------------------------------------------------------
392!
393! Initialize output array.
394!
395 DO k=lbk,ubk
396 DO j=lbj,ubj
397 DO i=lbi,ubi
398 r(i,j,k)=0.0_r8
399 END DO
400 END DO
401 END DO
402!
403! F90 intrinsic pseudorandom numbers with an uniform distribution
404! over the range 0 <= R < 1.
405!
406 IF (rscheme.eq.0) THEN
407 CALL random_number (r(lbi:,lbj:,lbk:))
408!
409! Scale (fac=2*SQRT(3)) the random number to insure the expectation
410! mean to be approximately zero, E(R)=0, and the expectation variance
411! to be approximately unity, E(R^2)=1.
412!
413! Recall that,
414!
415! E(R) = sum[R f(R)]
416!
417! where F(R) is the random variable probability distribution
418!
419 DO k=lbk,ubk
420 DO j=jmin,jmax
421 DO i=imin,imax
422 r(i,j,k)=fac*(r(i,j,k)-0.5_r8)
423 END DO
424 END DO
425 END DO
426 rmin=minval(r)
427 rmax=maxval(r)
428!
429! Random deviates with a Gaussian (normal) distribution with zero
430! mean and unit variance.
431!
432 ELSE IF (rscheme.eq.1) THEN
433 npts=(lm(ng)+2)*(mm(ng)+2)*(ubk-lbk+1)
434 IF (.not.allocated(random)) THEN
435 allocate ( random(npts+2) )
436 END IF
437 IF (master) THEN
438 CALL gasdev (random)
439 rmin=random(1)
440 rmax=random(1)
441 DO ic=1,npts
442 rmin=min(rmin,random(ic))
443 rmax=max(rmax,random(ic))
444 END DO
445 END IF
446# ifdef DISTRIBUTE
447 npts=SIZE(random)
448 CALL mp_scatter3d (ng, model, lbi, ubi, lbj, ubj, lbk, ubk, &
449 & nghostpoints, gtype, rmin, rmax, &
450# if defined READ_WATER && defined MASKING
451 & (lm(ng)+2)*(mm(ng)+2), &
452 & scalars(ng)%IJwater(:,gtype), &
453# endif
454 & npts, random, r)
455# else
456 ic=0
457 DO k=lbk,ubk
458 DO j=jmin,jmax
459 DO i=imin,imax
460 ic=ic+1
461 r(i,j,k)=random(ic)
462 END DO
463 END DO
464 END DO
465# endif
466 IF (allocated(random)) THEN
467 deallocate (random)
468 END IF
469 END IF
470
471 RETURN
472 END SUBROUTINE white_noise3d
473
474!
475!***********************************************************************
476 SUBROUTINE white_noise3d_bry (ng, tile, model, boundary, Rscheme, &
477 & Imin, Imax, &
478 & LBij, UBij, LBk, UBk, &
479 & Rmin, Rmax, R)
480!***********************************************************************
481!
482 USE mod_param
483 USE mod_parallel
484 USE mod_scalars
485!
486# ifdef DISTRIBUTE
487 USE distribute_mod, ONLY : mp_bcastf
489# endif
490 USE nrutil, ONLY : gasdev
491!
492! Imported variable declarations.
493!
494 integer, intent(in) :: ng, tile, model, boundary, rscheme
495 integer, intent(in) :: imin, imax
496 integer, intent(in) :: lbij, ubij, lbk, ubk
497
498 real(r8), intent(out) :: rmin, rmax
499
500# ifdef ASSUMED_SHAPE
501 real(r8), intent(out) :: r(lbij:,lbk:)
502# else
503 real(r8), intent(out) :: r(lbij:ubij,lbk:ubk)
504# endif
505!
506! Local variable declarations.
507!
508 integer :: ilen, ioff, i, ic, k, kc
509
510 real(r8), parameter :: fac = 2.0_r8 * 1.73205080756887720_r8
511
512 real(r8), dimension((UBij-LBij+1)*(UBk-LBk+1)) :: random
513!
514!-----------------------------------------------------------------------
515! Generate an array of random numbers.
516!-----------------------------------------------------------------------
517!
518! Initialize output array.
519!
520 DO k=lbk,ubk
521 DO i=lbij,ubij
522 r(i,k)=0.0_r8
523 END DO
524 END DO
525!
526! F90 intrinsic pseudorandom numbers with an uniform distribution
527! over the range 0 <= R < 1.
528!
529 IF (rscheme.eq.0) THEN
530 CALL random_number (r(lbij:,lbk:))
531!
532! Scale (fac=2*SQRT(3)) the random number to insure the expectation
533! mean to be approximately zero, E(R)=0, and the expectation variance
534! to be approximately unity, E(R^2)=1 (Bennett, 2002; book page 72).
535!
536! Recall that,
537!
538! E(R) = sum[R f(R)]
539!
540! where F(R) is the random variable probability distribution.
541!
542 DO k=lbk,ubk
543 DO i=imin,imax
544 r(i,k)=fac*(r(i,k)-0.5_r8)
545 END DO
546 END DO
547 rmin=minval(r)
548 rmax=maxval(r)
549!
550! Random deviates with a Gaussian (normal) distribuiton with zero
551! mean and unit variance.
552!
553 ELSE IF (rscheme.eq.1) THEN
554 IF (master) THEN
555 CALL gasdev (random)
556 END IF
557# ifdef DISTRIBUTE
558 CALL mp_bcastf (ng, model, random)
559# endif
560 rmin=minval(random)
561 rmax=maxval(random)
562
563 ilen=ubij-lbij+1
564 ioff=1-lbij
565
566 DO k=lbk,ubk
567 kc=(k-lbk)*ilen
568 DO i=imin,imax
569 ic=i+ioff+kc
570 r(i,k)=random(ic)
571 END DO
572 END DO
573# ifdef DISTRIBUTE
574 CALL mp_exchange3d_bry (ng, tile, model, 1, boundary, &
575 & lbij, ubij, lbk, ubk, &
576 & nghostpoints, &
577 & ewperiodic(ng), nsperiodic(ng), &
578 & r)
579# endif
580 END IF
581
582 RETURN
583 END SUBROUTINE white_noise3d_bry
584#endif
585
586 END MODULE white_noise_mod
subroutine mp_scatter3d(ng, model, lbi, ubi, lbj, ubj, lbk, ubk, nghost, gtype, amin, amax, nwpts, ij_water, npts, a, awrk)
subroutine mp_scatter2d(ng, model, lbi, ubi, lbj, ubj, nghost, gtype, amin, amax, nwpts, ij_water, npts, a, awrk)
integer, parameter r8
Definition mod_kinds.F:28
logical master
integer nghostpoints
Definition mod_param.F:710
integer, dimension(:), allocatable lm
Definition mod_param.F:455
integer, dimension(:), allocatable mm
Definition mod_param.F:456
logical, dimension(:), allocatable ewperiodic
logical, dimension(:), allocatable nsperiodic
type(t_scalars), dimension(:), allocatable scalars
Definition mod_scalars.F:65
subroutine mp_exchange2d_bry(ng, tile, model, nvar, boundary, lbij, ubij, nghost, ew_periodic, ns_periodic, a, b, c, d)
subroutine mp_exchange3d_bry(ng, tile, model, nvar, boundary, lbij, ubij, lbk, ubk, nghost, ew_periodic, ns_periodic, a, b, c, d)
Definition nrutil.F:1
subroutine, public white_noise3d(ng, model, gtype, rscheme, imin, imax, jmin, jmax, lbi, ubi, lbj, ubj, lbk, ubk, rmin, rmax, r)
subroutine, public white_noise3d_bry(ng, tile, model, boundary, rscheme, imin, imax, lbij, ubij, lbk, ubk, rmin, rmax, r)
subroutine, public white_noise1d(ng, model, rscheme, imin, imax, lbi, ubi, rmin, rmax, r)
Definition white_noise.F:52
subroutine, public white_noise2d_bry(ng, tile, model, boundary, rscheme, imin, imax, lbij, ubij, rmin, rmax, r)
subroutine, public white_noise2d(ng, model, gtype, rscheme, imin, imax, jmin, jmax, lbi, ubi, lbj, ubj, rmin, rmax, r)