-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.f90
556 lines (505 loc) · 14.3 KB
/
utils.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
module utils
! Various general utilities.
! Based on a code by John E. Pask, LLNL.
use types, only: dp
implicit none
private
public upcase, lowcase, whitechar, blank, num_strings, getstring, &
stop_error, arange, loadtxt, savetxt, newunit, assert, str, init_random, &
zeros, mesh_exp, linspace, clock, strfmt, get_int_arg, get_float_arg, &
allocate_mold
interface str
module procedure str_int, str_real, str_real_n
end interface
interface strfmt
module procedure strfmt_int, strfmt_real
end interface
interface allocate_mold
module procedure allocate_mold_real3d
module procedure allocate_mold_real4d
module procedure allocate_mold_complex3d
end interface
contains
function upcase(s) result(t)
! Returns string 's' in uppercase
character(*), intent(in) :: s
character(len(s)) :: t
integer :: i, diff
t = s; diff = ichar('A')-ichar('a')
do i = 1, len(t)
if (ichar(t(i:i)) >= ichar('a') .and. ichar(t(i:i)) <= ichar('z')) then
! if lowercase, make uppercase
t(i:i) = char(ichar(t(i:i)) + diff)
end if
end do
end function
function lowcase(s) result(t)
! Returns string 's' in lowercase
character(*), intent(in) :: s
character(len(s)) :: t
integer :: i, diff
t = s; diff = ichar('A')-ichar('a')
do i = 1, len(t)
if (ichar(t(i:i)) >= ichar('A') .and. ichar(t(i:i)) <= ichar('Z')) then
! if uppercase, make lowercase
t(i:i) = char(ichar(t(i:i)) - diff)
end if
end do
end function
logical function whitechar(char) ! white character
! returns .true. if char is space (32) or tab (9), .false. otherwise
character, intent(in) :: char
if (iachar(char) == 32 .or. iachar(char) == 9) then
whitechar = .true.
else
whitechar = .false.
end if
end function
logical function blank(string)
! Returns true if string contains only white characters
character(*), intent(in) :: string
integer :: i
do i = 1, len(string)
if (.not. whitechar(string(i:i))) exit
end do
blank = (i>len(string))
end function
integer function num_strings(s) result(n)
! Returns number of substrings contained in input string 's' delimited
! by white space.
character(*), intent(in) :: s ! input string
character(len(s)+2) :: t ! temporary string to facilitate analysis
integer :: i
t = " " // s // " "
n = 0
do i = 1, len(t)-1
if (whitechar(t(i:i)) .and. .not. whitechar(t(i+1:i+1))) n = n + 1
end do
end function
!--------------------------------------------------------------------------------------------------!
subroutine getstring(s,is,ss)
! Returns first substring ss in string s, delimited by white space, starting at
! index is in s. If ss is found, is is set to (index of last character of ss in
! s) + 1; else is is set to 0. If is is out of range on input, routine
! terminates with is = -1.
character(*), intent(in) :: s ! input string
integer, intent(inout) :: is ! on input: starting index for search for ss in
! s on output: (index of last character of ss in
! s) + 1
character(*), intent(out) :: ss ! first substring in s, starting from index is
character(len(s)+1) :: t ! temporary string to facilitate search
integer i, i1, i2
logical prevwhite, curwhite
if (is <= 0 .or. is > len(s)) then
ss = ""; is = -1; return
end if
t = s // " "
if (is == 1) then
prevwhite = .true.
else
prevwhite = whitechar(t(is-1:is-1))
end if
i1 = 0; i2 = 0
do i = is, len(t)
curwhite = whitechar(t(i:i))
if (prevwhite .and. .not. curwhite) i1 = i ! beginning of substring
if (i1>0 .and. curwhite) then ! end of substring
i2 = i-1; exit
end if
prevwhite=curwhite
end do
if (i2 > 0) then
ss = t(i1:i2); is = i2+1
else
ss = ""; is = 0
end if
end subroutine
integer function newunit(unit) result(n)
! Returns lowest i/o unit number not in use (to be used in older compilers).
!
! Starting at 10 to avoid lower numbers which are sometimes reserved.
! Note: largest valid unit number may be system-dependent.
!
! Arguments
! ---------
!
! If present, the new unit will be returned into it
integer, intent(out), optional :: unit
!
! Example
! -------
!
! integer :: u
! open(newunit(u), file="log.txt", status="old")
! read(u, *) a, b
! close(u)
!
! In new compilers, just use the "newunit" keyword argument:
!
! integer :: u
! open(newunit=u, file="log.txt", status="old")
! read(u, *) a, b
! close(u)
logical inuse
integer, parameter :: nmin=10 ! avoid lower numbers which are sometimes reserved
integer, parameter :: nmax=999 ! may be system-dependent
do n = nmin, nmax
inquire(unit=n, opened=inuse)
if (.not. inuse) then
if (present(unit)) unit=n
return
end if
end do
call stop_error("newunit ERROR: available unit not found.")
end function
subroutine stop_error(msg)
! Aborts the program with nonzero exit code
!
! The statement "stop msg" will return 0 exit code when compiled using
! gfortran. stop_error() uses the statement "stop 1" which returns an exit code
! 1 and a print statement to print the message.
!
! Example
! -------
!
! call stop_error("Invalid argument")
character(len=*) :: msg ! Message to print on stdout
print *, msg
stop 1
end subroutine
subroutine loadtxt(filename, d)
! Loads a 2D array from a text file.
!
! Arguments
! ---------
!
! Filename to load the array from
character(len=*), intent(in) :: filename
! The array 'd' will be automatically allocated with the correct dimensions
real(dp), allocatable, intent(out) :: d(:, :)
!
! Example
! -------
!
! real(dp), allocatable :: data(:, :)
! call loadtxt("log.txt", data) ! 'data' will be automatically allocated
!
! Where 'log.txt' contains for example::
!
! 1 2 3
! 2 4 6
! 8 9 10
! 11 12 13
! ...
!
character :: c
integer :: s, ncol, nrow, ios, i
logical :: lastwhite
real(dp) :: r
open(newunit(s), file=filename, status="old")
! determine number of columns
ncol = 0
lastwhite = .true.
do
read(s, '(a)', advance='no', iostat=ios) c
if (ios /= 0) exit
if (lastwhite .and. .not. whitechar(c)) ncol = ncol + 1
lastwhite = whitechar(c)
end do
rewind(s)
! determine number or rows
nrow = 0
do
read(s, *, iostat=ios) r
if (ios /= 0) exit
nrow = nrow + 1
end do
rewind(s)
allocate(d(nrow, ncol))
do i = 1, nrow
read(s, *) d(i, :)
end do
close(s)
end subroutine
subroutine savetxt(filename, d)
! Saves a 2D array into a textfile.
!
! Arguments
! ---------
!
character(len=*), intent(in) :: filename ! File to save the array to
real(dp), intent(in) :: d(:, :) ! The 2D array to save
!
! Example
! -------
!
! real(dp) :: data(3, 2)
! call savetxt("log.txt", data)
integer :: s, i
open(newunit(s), file=filename, status="replace")
do i = 1, size(d, 1)
write(s, *) d(i, :)
end do
close(s)
end subroutine
subroutine arange(a, b, dx, u)
! Returns an array u = [a, a+dx, a+2*dx, ..., b-dx]
!
! Arguments
! ---------
!
real(dp), intent(in) :: a, b, dx
real(dp), allocatable, intent(out) :: u(:)
!
! Example
! -------
!
! real(dp), allocatable :: u(:)
! call arange(1, 5, 1, u) ! u = [1, 2, 3, 4]
integer :: n, i
n = int((b-a) / dx)
allocate(u(n))
do i = 1, n
u(i) = a + (i-1)*dx
end do
end subroutine
function zeros(n) result(x)
integer, intent(in) :: n
real(dp) :: x(n)
x = 0
end function
subroutine assert(condition)
! If condition == .false., it aborts the program.
!
! Arguments
! ---------
!
logical, intent(in) :: condition
!
! Example
! -------
!
! call assert(a == 5)
if (.not. condition) call stop_error("Assert failed.")
end subroutine
pure integer function str_int_len(i) result(sz)
! Returns the length of the string representation of 'i'
integer, intent(in) :: i
integer, parameter :: MAX_STR = 100
character(MAX_STR) :: s
! If 's' is too short (MAX_STR too small), Fortan will abort with:
! "Fortran runtime error: End of record"
write(s, '(i0)') i
sz = len_trim(s)
end function
pure function str_int(i) result(s)
! Converts integer "i" to string
integer, intent(in) :: i
character(len=str_int_len(i)) :: s
write(s, '(i0)') i
end function
pure integer function str_real_len(r, fmt) result(sz)
! Returns the length of the string representation of 'i'
real(dp), intent(in) :: r
character(len=*), intent(in) :: fmt
integer, parameter :: MAX_STR = 100
character(MAX_STR) :: s
! If 's' is too short (MAX_STR too small), Fortan will abort with:
! "Fortran runtime error: End of record"
write(s, fmt) r
sz = len_trim(s)
end function
pure function str_real(r) result(s)
! Converts the real number "r" to string with 7 decimal digits.
real(dp), intent(in) :: r
character(len=*), parameter :: fmt="(f0.6)"
character(len=str_real_len(r, fmt)) :: s
write(s, fmt) r
end function
pure function str_real_n(r, n) result(s)
! Converts the real number "r" to string with 'n' decimal digits.
real(dp), intent(in) :: r
integer, intent(in) :: n
character(len=str_real_len(r, "(f0." // str_int(n) // ")")) :: s
write(s, "(f0." // str_int(n) // ")") r
end function
pure integer function strfmt_int_len(fm, i) result(sz)
! Returns the length of the string representation of 'i'
character(len=*), intent(in) :: fm
integer, intent(in) :: i
integer, parameter :: MAX_STR = 100
character(MAX_STR) :: s
! If 's' is too short (MAX_STR too small), Fortan will abort with:
! "Fortran runtime error: End of record"
write(s, fm) i
sz = len_trim(s)
end function
pure function strfmt_int(fm, i) result(s)
! Converts integer "i" to string
character(len=*), intent(in) :: fm
integer, intent(in) :: i
character(len=strfmt_int_len(fm, i)) :: s
write(s, fm) i
end function
pure integer function strfmt_real_len(fm, r) result(sz)
! Returns the length of the string representation of 'r'
character(len=*), intent(in) :: fm
real(dp), intent(in) :: r
integer, parameter :: MAX_STR = 100
character(MAX_STR) :: s
! If 's' is too short (MAX_STR too small), Fortan will abort with:
! "Fortran runtime error: End of record"
write(s, fm) r
sz = len_trim(s)
end function
pure function strfmt_real(fm, r) result(s)
! Converts real "r" to string
character(len=*), intent(in) :: fm
real(dp), intent(in) :: r
character(len=strfmt_real_len(fm, r)) :: s
write(s, fm) r
end function
subroutine init_random()
! Initializes the random number generator based on the system's time.
integer :: i, n, clock
integer, allocatable :: seed(:)
call random_seed(size=n)
allocate(seed(n))
call system_clock(count=clock)
seed = clock + 37 * [(i - 1, i = 1, n)]
call random_seed(put=seed)
end subroutine
function linspace(a, b, n) result(s)
real(dp), intent(in) :: a, b
integer, intent(in) :: n
real(dp) :: s(n)
s = mesh_exp(a, b, 1.0_dp, n-1)
end function
function mesh_exp(r_min, r_max, a, N) result(mesh)
! Generates exponential mesh of N elements on [r_min, r_max]
!
! Arguments
! ---------
!
! The domain [r_min, r_max], the mesh will contain both endpoints:
real(dp), intent(in) :: r_min, r_max
!
! The fraction of the rightmost vs. leftmost elements of the mesh (for a > 1
! this means the "largest/smallest"); The only requirement is a > 0. For a == 1
! a uniform mesh will be returned:
real(dp), intent(in) :: a
!
! The number of elements in the mesh:
integer, intent(in) :: N
!
! Returns
! -------
!
! The generated mesh:
real(dp) :: mesh(N+1)
!
! Note: Every exponential mesh is fully determined by the set of parameters
! (r_min, r_max, a, N). Use the get_mesh_exp_params() subroutine to obtain them
! from the given mesh.
!
! Example
! -------
!
! real(dp) :: r(11)
! r = mesh_exp(0._dp, 50._dp, 1e9_dp, 10)
integer :: i
real(dp) :: alpha, beta
if (a < 0) then
call stop_error("mesh_exp: a > 0 required")
else if (abs(a - 1) < 1e-16_dp) then
alpha = (r_max - r_min) / N
do i = 1, N+1
mesh(i) = alpha * (i-1.0_dp) + r_min
end do
else
if (N > 1) then
beta = log(a) / (N-1)
alpha = (r_max - r_min) / (exp(beta*N) - 1)
do i = 1, N+1
mesh(i) = alpha * (exp(beta*(i-1)) - 1) + r_min
end do
else if (N == 1) then
mesh(1) = r_min
mesh(2) = r_max
else
call stop_error("mesh_exp: N >= 1 required")
end if
end if
end function
real(dp) function clock() result(r)
use openmp, only: omp_get_wtime, with_openmp
if (with_openmp()) then
r = omp_get_wtime()
else
call cpu_time(r)
end if
end function
integer function get_int_arg(i) result(r)
integer, intent(in) :: i
integer, parameter :: maxlen=32
character(len=maxlen) :: arg
integer :: s
if (.not. (0 < i .and. i <= command_argument_count())) then
call stop_error("get_int_arg: `i` must satisfy `0 < i <= arg_count`.")
end if
call get_command_argument(i, arg, status=s)
if (s == -1) then
call stop_error("get_int_arg: Argument too long, increase `maxlen`.")
else if (s > 0) then
call stop_error("get_int_arg: Argument retrieval failed.")
else if (s /= 0) then
call stop_error("get_int_arg: Unknown error.")
end if
read(arg, *, iostat=s) r
if (s /= 0) then
call stop_error("get_int_arg: Failed to convert an argument to integer.")
end if
end function
real(dp) function get_float_arg(i) result(r)
integer, intent(in) :: i
integer, parameter :: maxlen=32
character(len=maxlen) :: arg
integer :: s
if (.not. (0 < i .and. i <= command_argument_count())) then
call stop_error("get_int_arg: `i` must satisfy `0 < i <= arg_count`.")
end if
call get_command_argument(i, arg, status=s)
if (s == -1) then
call stop_error("get_int_arg: Argument too long, increase `maxlen`.")
else if (s > 0) then
call stop_error("get_int_arg: Argument retrieval failed.")
else if (s /= 0) then
call stop_error("get_int_arg: Unknown error.")
end if
read(arg, *, iostat=s) r
if (s /= 0) then
call stop_error("get_int_arg: Failed to convert an argument to a float.")
end if
end function
subroutine allocate_mold_real3d(A, B)
real(dp), allocatable, intent(out) :: A(:,:,:)
real(dp), intent(in) :: B(:,:,:)
! Equivalent to allocate(A, mold=B). Use this with compilers that do not
! support the F2008 syntax yet.
allocate(A(size(B,1), size(B,2), size(B,3)))
end subroutine
subroutine allocate_mold_real4d(A, B)
real(dp), allocatable, intent(out) :: A(:,:,:,:)
real(dp), intent(in) :: B(:,:,:,:)
! Equivalent to allocate(A, mold=B). Use this with compilers that do not
! support the F2008 syntax yet.
allocate(A(size(B,1), size(B,2), size(B,3), size(B,4)))
end subroutine
subroutine allocate_mold_complex3d(A, B)
complex(dp), allocatable, intent(out) :: A(:,:,:)
complex(dp), intent(in) :: B(:,:,:)
! Equivalent to allocate(A, mold=B). Use this with compilers that do not
! support the F2008 syntax yet.
allocate(A(size(B,1), size(B,2), size(B,3)))
end subroutine
end module