ROMS
Loading...
Searching...
No Matches
nrutil.F
Go to the documentation of this file.
1 MODULE nrutil
2!
3!git $Id$
4!================================================== Hernan G. Arango ===
5! Copyright (c) 2002-2025 The ROMS Group !
6! Licensed under a MIT/X style license !
7! See License_ROMS.md !
8!=======================================================================
9! !
10! Numerical Recipies Utility. !
11! !
12! Adapted from Numerical Recepies. !
13! !
14! Press, W.H., S.A. Teukolsky, W.T. Vetterling, and B.P. Flannery, !
15! 1996: Numerical Recipes in Fortran 90, The Art of Parallel !
16! Scientific Computing, 2nd Edition, Cambridge Univ. Press. !
17! !
18!=======================================================================
19!
20 USE mod_kinds
21
22 implicit none
23
24 PUBLIC
25
26 integer(i8b), parameter :: npar_arth = 16
27 integer(i8b), parameter :: npar2_arth = 8
28
29 INTERFACE array_copy
30 MODULE PROCEDURE array_copy_r, array_copy_d, array_copy_i
31 END INTERFACE
32
33 INTERFACE arth
34 MODULE PROCEDURE arth_r, arth_d, arth_i
35 END INTERFACE
36
37 INTERFACE reallocate
38 MODULE PROCEDURE reallocate_rv, reallocate_rm, &
41 END INTERFACE
42
43 INTERFACE gasdev
44 SUBROUTINE gasdev_s (harvest)
45 USE mod_kinds
46 real(r8), intent(out) :: harvest
47 END SUBROUTINE gasdev_s
48!
49 SUBROUTINE gasdev_v (harvest)
50 USE mod_kinds
51 real(r8), dimension(:), intent(out) :: harvest
52 END SUBROUTINE gasdev_v
53 END INTERFACE
54
55 INTERFACE ran1
56 SUBROUTINE ran1_s (harvest)
57 USE mod_kinds
58 real(r8), intent(out) :: harvest
59 END SUBROUTINE ran1_s
60!
61 SUBROUTINE ran1_v (harvest)
62 USE mod_kinds
63 real(r8), dimension(:), intent(out) :: harvest
64 END SUBROUTINE ran1_v
65 END INTERFACE
66
67 CONTAINS
68
69 SUBROUTINE array_copy_r (src, dest, n_copied, n_not_copied)
70!
71!=======================================================================
72! !
73! Copy single precision array where size of source not known in !
74! advance. !
75! !
76!=======================================================================
77!
78! Imported variable declarations.
79!
80 real(r4), intent(in) :: src(:)
81 real(r4), intent(out) :: dest(:)
82
83 integer(i8b), intent(out) :: n_copied, n_not_copied
84!
85!-----------------------------------------------------------------------
86! Copy single precision array.
87!-----------------------------------------------------------------------
88!
89 n_copied=min(SIZE(src), SIZE(dest))
90 n_not_copied=SIZE(src)-n_copied
91 dest(1:n_copied)=src(1:n_copied)
92
93 RETURN
94 END SUBROUTINE array_copy_r
95
96 SUBROUTINE array_copy_d (src, dest, n_copied, n_not_copied)
97!
98!=======================================================================
99! !
100! Copy double precision array where size of source not known in !
101! advance. !
102! !
103!=======================================================================
104!
105! Imported variable declarations.
106!
107 real(dp), intent(in) :: src(:)
108 real(dp), intent(out) :: dest(:)
109
110 integer(i8b), intent(out) :: n_copied, n_not_copied
111!
112!-----------------------------------------------------------------------
113! Copy double precision array.
114!-----------------------------------------------------------------------
115!
116 n_copied=min(SIZE(src), SIZE(dest))
117 n_not_copied=size(src)-n_copied
118 dest(1:n_copied)=src(1:n_copied)
119
120 RETURN
121 END SUBROUTINE array_copy_d
122
123 SUBROUTINE array_copy_i (src, dest, n_copied, n_not_copied)
124!
125!=======================================================================
126! !
127! Copy integer array where size of source not known in advance. !
128! !
129!=======================================================================
130!
131! Imported variable declarations.
132!
133 integer(i8b), intent(in) :: src(:)
134 integer(i8b), intent(out) :: dest(:)
135
136 integer(i8b), intent(out) :: n_copied, n_not_copied
137!
138!-----------------------------------------------------------------------
139! Copy integer array.
140!-----------------------------------------------------------------------
141!
142 n_copied=min(size(src),size(dest))
143 n_not_copied=size(src)-n_copied
144 dest(1:n_copied)=src(1:n_copied)
145
146 RETURN
147 END SUBROUTINE array_copy_i
148
149 FUNCTION arth_r (first, increment, n)
150!
151!=======================================================================
152! !
153! Array function returning an arithmetic progression, single !
154! precision. !
155! !
156!=======================================================================
157!
158! Imported variable declarations.
159!
160 integer(i8b), intent(in) :: n
161
162 real(r4), intent(in) :: first, increment
163 real(r4), dimension(n) :: arth_r
164!
165! Local variable declarations.
166!
167 integer(i8b) :: k, k2
168
169 real(r4) :: temp
170!
171!----------------------------------------------------------------------
172! Set arithmetic progression.
173!----------------------------------------------------------------------
174!
175 IF (n.gt.0) arth_r(1)=first
176 IF (n.le.npar_arth) THEN
177 DO k=2,n
178 arth_r(k)=arth_r(k-1)+increment
179 END DO
180 ELSE
181 DO k=2,npar2_arth
182 arth_r(k)=arth_r(k-1)+increment
183 END DO
184 temp=increment*npar2_arth
185 k=npar2_arth
186 DO
187 IF (k.ge.n) EXIT
188 k2=k+k
189 arth_r(k+1:min(k2,n))=temp+arth_r(1:min(k,n-k))
190 temp=temp+temp
191 k=k2
192 END DO
193 END IF
194
195 RETURN
196 END FUNCTION arth_r
197
198 FUNCTION arth_d (first, increment, n)
199!
200!=======================================================================
201! !
202! Array function returning an arithmetic progression, double !
203! precision. !
204! !
205!=======================================================================
206!
207! Imported variable declarations.
208!
209 integer(i8b), intent(in) :: n
210
211 real(dp), intent(in) :: first, increment
212 real(dp), dimension(n) :: arth_d
213!
214! Local variable declarations.
215!
216 integer(i8b) :: k, k2
217
218 real(dp) :: temp
219!
220!----------------------------------------------------------------------
221! Set arithmetic progression.
222!----------------------------------------------------------------------
223!
224 IF (n.gt.0) arth_d(1)=first
225 IF (n.le.npar_arth) THEN
226 DO k=2,n
227 arth_d(k)=arth_d(k-1)+increment
228 END DO
229 ELSE
230 DO k=2,npar2_arth
231 arth_d(k)=arth_d(k-1)+increment
232 END DO
233 temp=increment*npar2_arth
234 k=npar2_arth
235 DO
236 IF (k.ge.n) EXIT
237 k2=k+k
238 arth_d(k+1:min(k2,n))=temp+arth_d(1:min(k,n-k))
239 temp=temp+temp
240 k=k2
241 END DO
242 END IF
243
244 RETURN
245 END FUNCTION arth_d
246
247 FUNCTION arth_i (first, increment, n)
248!
249!=======================================================================
250! !
251! Integer array function returning an arithmetic progression. !
252! !
253!=======================================================================
254!
255! Imported variable declarations.
256!
257 integer(i8b), intent(in) :: first, increment, n
258 integer(i8b), dimension(n) :: arth_i
259!
260! Local variable declarations.
261!
262 integer(i8b) :: k, k2, temp
263!
264!----------------------------------------------------------------------
265! Set arithmetic progression.
266!----------------------------------------------------------------------
267!
268 IF (n.gt.0) arth_i(1)=first
269 IF (n.le.npar_arth) THEN
270 DO k=2,n
271 arth_i(k)=arth_i(k-1)+increment
272 END DO
273 ELSE
274 DO k=2,npar2_arth
275 arth_i(k)=arth_i(k-1)+increment
276 END DO
277 temp=increment*npar2_arth
278 k=npar2_arth
279 DO
280 IF (k.ge.n) EXIT
281 k2=k+k
282 arth_i(k+1:min(k2,n))=temp+arth_i(1:min(k,n-k))
283 temp=temp+temp
284 k=k2
285 END DO
286 END IF
287
288 RETURN
289 END FUNCTION arth_i
290
291 SUBROUTINE nrerror (string)
292!
293!=======================================================================
294! !
295! Report an error message and the die. !
296! !
297!=======================================================================
298!
299 USE mod_iounits, ONLY : stdout
300!
301! Imported variable declarations.
302!
303 character(len=*), intent(in) :: string
304!
305!-----------------------------------------------------------------------
306! Report error message to standard output and terminate execution.
307!-----------------------------------------------------------------------
308!
309 WRITE (stdout,10) string, 'program terminated by NRERROR'
310 10 FORMAT (/,1x,a,/20x,a)
311 stop
312
313 END SUBROUTINE nrerror
314
315 FUNCTION reallocate_rv (p, n)
316!
317!=======================================================================
318! !
319! Reallocate a pointer of a single precision vector to a new size, !
320! preserving its previous content. !
321! !
322!=======================================================================
323!
324! Imported variable declarations.
325!
326 real(r4), pointer :: p(:)
327 real(r4), pointer :: reallocate_rv(:)
328
329 integer(i8b), intent(in) :: n
330!
331! Local variable declarations.
332!
333 integer(i8b) :: nold, ierr
334!
335!-----------------------------------------------------------------------
336! Reallocate pointer for a single precision vector.
337!-----------------------------------------------------------------------
338!
339 ALLOCATE (reallocate_rv(n), stat=ierr)
340 IF (ierr.ne.0) &
341 & CALL nrerror ('REALLOCATE_RV: error while allocating memory')
342 IF (.not.ASSOCIATED(p)) RETURN
343 nold=SIZE(p)
344 reallocate_rv(1:min(nold,n))=p(1:min(nold,n))
345 DEALLOCATE (p)
346
347 RETURN
348 END FUNCTION reallocate_rv
349
350 FUNCTION reallocate_iv (p,n)
351!
352!=======================================================================
353! !
354! Reallocate a pointer of a integer vector to a new size, preserving !
355! its previous content. !
356! !
357!=======================================================================
358!
359! Imported variable declarations.
360!
361 integer(i8b), pointer :: p(:)
362 integer(i8b), pointer :: reallocate_iv(:)
363
364 integer(i8b), intent(in) :: n
365!
366! Local variable declarations.
367!
368 integer(i8b) :: nold, ierr
369!
370!-----------------------------------------------------------------------
371! Reallocate pointer for a integer vector.
372!-----------------------------------------------------------------------
373!
374 ALLOCATE (reallocate_iv(n), stat=ierr)
375 IF (ierr.ne.0) &
376 & CALL nrerror ('REALLOCATE_IV: error while allocating memory')
377 IF (.not.ASSOCIATED(p)) RETURN
378 nold=SIZE(p)
379 reallocate_iv(1:min(nold,n))=p(1:min(nold,n))
380 DEALLOCATE (p)
381
382 RETURN
383 END FUNCTION reallocate_iv
384
385 FUNCTION reallocate_hv (p, n)
386!
387!=======================================================================
388! !
389! Reallocate a pointer of a character vector to a new size, !
390! preserving its previous content. !
391! !
392!=======================================================================
393!
394! Imported variable declarations.
395!
396 character (len=1), pointer :: p(:)
397 character (len=1), pointer :: reallocate_hv(:)
398
399 integer(i8b), intent(in) :: n
400!
401! Local variable declarations.
402!
403 integer(i8b) :: nold, ierr
404!
405!-----------------------------------------------------------------------
406! Reallocate pointer for a integer vector.
407!-----------------------------------------------------------------------
408!
409 ALLOCATE (reallocate_hv(n),stat=ierr)
410 IF (ierr.ne.0) &
411 & CALL nrerror ('REALLOCATE_HV: error while allocating memory')
412 IF (.not.ASSOCIATED(p)) RETURN
413 nold=SIZE(p)
414 reallocate_hv(1:min(nold,n))=p(1:min(nold,n))
415 DEALLOCATE (p)
416
417 RETURN
418 END FUNCTION reallocate_hv
419
420 FUNCTION reallocate_rm (p, n, m)
421!
422!=======================================================================
423! !
424! Reallocate a pointer of a single precision matrix to a new size, !
425! preserving its previous content. !
426! !
427!=======================================================================
428!
429! Imported variable declarations.
430!
431 real(r4), pointer :: p(:,:)
432 real(r4), pointer :: reallocate_rm(:,:)
433
434 integer(i8b), intent(in) :: n, m
435!
436! Local variable declarations.
437!
438 integer(i8b) :: nold, mold, ierr
439!
440!-----------------------------------------------------------------------
441! Reallocate pointer for a single precision matrix.
442!-----------------------------------------------------------------------
443!
444 ALLOCATE (reallocate_rm(n,m), stat=ierr)
445 IF (ierr.ne.0) &
446 & CALL nrerror ('REALLOCATE_RM: error while allocating memory')
447 IF (.not.ASSOCIATED(p)) RETURN
448 nold=SIZE(p,1)
449 mold=SIZE(p,2)
450 reallocate_rm(1:min(nold,n),1:min(mold,m))= &
451 & p(1:min(nold,n),1:min(mold,m))
452 DEALLOCATE (p)
453
454 RETURN
455 END FUNCTION reallocate_rm
456
457 FUNCTION reallocate_im (p, n, m)
458!
459!=======================================================================
460! !
461! Reallocate a pointer of a integer matrix to a new size, preserving !
462! its previous content. !
463! !
464!=======================================================================
465!
466! Imported variable declarations.
467!
468 integer(i8b), pointer :: p(:,:)
469 integer(i8b), pointer :: reallocate_im(:,:)
470
471 integer(i8b), intent(in) :: n, m
472!
473! Local variable declarations.
474!
475 integer(i8b) :: nold, mold, ierr
476!
477!-----------------------------------------------------------------------
478! Reallocate pointer for a integer matrix.
479!-----------------------------------------------------------------------
480!
481 ALLOCATE (reallocate_im(n,m), stat=ierr)
482 IF (ierr.ne.0) &
483 & CALL nrerror ('REALLOCATE_IM: error while allocating memory')
484 IF (.not.ASSOCIATED(p)) RETURN
485 nold=SIZE(p,1)
486 mold=SIZE(p,2)
487 reallocate_im(1:min(nold,n),1:min(mold,m))= &
488 & p(1:min(nold,n),1:min(mold,m))
489 DEALLOCATE (p)
490
491 RETURN
492 END FUNCTION reallocate_im
493
494 END MODULE nrutil
495
subroutine gasdev_s(harvest)
Definition gasdev.F:3
subroutine gasdev_v(harvest)
Definition gasdev.F:75
integer stdout
Definition nrutil.F:1
integer(i8b), parameter npar_arth
Definition nrutil.F:26
real(r4) function, dimension(:), pointer reallocate_rv(p, n)
Definition nrutil.F:316
subroutine array_copy_r(src, dest, n_copied, n_not_copied)
Definition nrutil.F:70
subroutine array_copy_d(src, dest, n_copied, n_not_copied)
Definition nrutil.F:97
character(len=1) function, dimension(:), pointer reallocate_hv(p, n)
Definition nrutil.F:386
subroutine nrerror(string)
Definition nrutil.F:292
integer(i8b) function, dimension(:,:), pointer reallocate_im(p, n, m)
Definition nrutil.F:458
real(r4) function, dimension(n) arth_r(first, increment, n)
Definition nrutil.F:150
integer(i8b), parameter npar2_arth
Definition nrutil.F:27
real(dp) function, dimension(n) arth_d(first, increment, n)
Definition nrutil.F:199
integer(i8b) function, dimension(:), pointer reallocate_iv(p, n)
Definition nrutil.F:351
subroutine array_copy_i(src, dest, n_copied, n_not_copied)
Definition nrutil.F:124
integer(i8b) function, dimension(n) arth_i(first, increment, n)
Definition nrutil.F:248
real(r4) function, dimension(:,:), pointer reallocate_rm(p, n, m)
Definition nrutil.F:421
subroutine ran1_s(harvest)
Definition ran1.F:3
subroutine ran1_v(harvest)
Definition ran1.F:82