-
Notifications
You must be signed in to change notification settings - Fork 2
/
initfile.F90
770 lines (595 loc) · 21.1 KB
/
initfile.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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
!
! OAK, Ocean Assimilation Kit
! Copyright(c) 2002-2011 Alexander Barth and Luc Vandenblucke
!
! This program is free software; you can redistribute it and/or
! modify it under the terms of the GNU General Public License
! as published by the Free Software Foundation; either version 2
! of the License, or (at your option) any later version.
!
! This program is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! GNU General Public License for more details.
!
! You should have received a copy of the GNU General Public License
! along with this program; if not, write to the Free Software
! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
!
! include the fortran preprocessor definitions
#include "ppdef.h"
module InitFile
integer, parameter :: maxLength = 1024*4
interface getInitValue
module procedure getInitValue_integer, &
getInitValue_real, &
getInitValue_string, &
getInitValue_integer1D, &
getInitValue_real1D, &
getInitValue_string1D
end interface
interface presentInitValue
module procedure presentInitValue
end interface
character, parameter :: tab = char(9)
! integer :: stderr=6
contains
!--------------------------------------------------------------------
integer function freeunit()
integer unit
integer, parameter :: iu = 1000
logical used
used = .true.
unit = iu
do while (used)
unit = unit+1
inquire(unit,opened=used)
! if (used) write(stdout,*) unit,' used'
end do
! write(stdout,*) unit
freeunit = unit
end function
!--------------------------------------------------------------------
logical function iswhitespace(c)
character, intent(in) :: c
iswhitespace = c == ' ' .or. c == tab
end function iswhitespace
!--------------------------------------------------------------------
subroutine parseItem(buffer,first_index,last_index,error)
character(len=*), intent(in) :: buffer
integer, intent(inout) :: first_index
integer, intent(out) :: last_index
integer, intent(out) :: error
error = 0
! eat blanks
do while (iswhitespace(buffer(first_index:first_index)))
first_index = first_index + 1
if (first_index.gt.len(buffer)) exit
enddo
if (first_index.le.len(buffer)) then
select case (buffer(first_index:first_index))
! comments
case ('!','#')
first_index = len(buffer)+1
last_index = len(buffer)
case ('=',',',';','*','/')
last_index = first_index
case (']')
error = -1
case ('[')
last_index = first_index+1
do while ((last_index.le.len(buffer)).and. &
(buffer(last_index:last_index).ne.']'))
last_index = last_index + 1
end do
if (last_index.gt.len(buffer)) then
error = -1
end if
case ('''')
! for strings
last_index = first_index+1
do while ((last_index.le.len(buffer)).and. &
.not.((last_index.eq.len(buffer)).and.(buffer(last_index:last_index).eq.'''')).and. &
.not.((buffer(last_index:last_index).eq.'''').and.(buffer(last_index+1:last_index+1).ne.'''')))
if ((buffer(last_index:last_index).eq.'''').and.(buffer(last_index+1:last_index+1).eq.'''')) then
last_index = last_index + 2
else
last_index = last_index + 1
end if
end do
if (last_index.gt.len(buffer)) then
error = -1
end if
case default
last_index = first_index
do while ((last_index.lt.len(buffer)).and. &
(buffer(last_index+1:last_index+1).ne.'=').and. &
(buffer(last_index+1:last_index+1).ne.'!').and. &
(buffer(last_index+1:last_index+1).ne.'''').and. &
(buffer(last_index+1:last_index+1).ne.'[').and. &
(buffer(last_index+1:last_index+1).ne.',').and. &
(buffer(last_index+1:last_index+1).ne.';').and. &
(buffer(last_index+1:last_index+1).ne.']').and. &
(.not.iswhitespace(buffer(last_index+1:last_index+1))))
last_index = last_index + 1
enddo
end select
else
last_index = len(buffer)
endif
end subroutine parseItem
!--------------------------------------------------------------------
subroutine parseString(source,dest)
implicit none
character(len=*), intent(in) :: source
character(len=*), intent(out) :: dest
integer :: position
integer :: length
position = 1
length = 0
dest = ''
do while ((position.le.len(source)).and.(length.lt.len(dest)))
dest = dest(1:length)//source(position:position)
length = length+1
if (source(position:position).eq.'''') then
position = position +2
else
position = position + 1
endif
end do
end subroutine parseString
!--------------------------------------------------------------------
function countElements(string,error)
integer countElements
character (len=*), intent(in) :: string
integer, intent(out) :: error
integer :: length, first_index, last_index, ParseError, count
countElements = 0
length = len_trim(string)
error = 0
count = 0
if ((string(1:1).eq.'[').and.(string(length:length).eq.']')) then
length = len_trim(string(1:length-1))
first_index = 2
call parseItem(string,first_index,last_index,ParseError)
if ((ParseError.ne.0).or.(first_index.gt.last_index)) then
error = -1
return
endif
count = 1
do while (last_index.ne.length)
first_index = last_index+1
call parseItem(string,first_index,last_index,ParseError)
if ((ParseError.ne.0).or.(first_index.gt.last_index).or. &
(string(first_index:last_index).ne.',')) then
error = -1
return
else
count = count+1
end if
first_index = last_index+1
call parseItem(string,first_index,last_index,ParseError)
if ((ParseError.ne.0).or.(first_index.gt.last_index)) then
error = -1
return
endif
end do
else
error = -1
end if
countElements = count
end function countElements
!________________________________________________________________________________
!
subroutine hashVec(string,array,error,errormsg)
implicit none
character(len=*), intent(in) :: string
character(len=maxLength), pointer :: array(:)
integer, intent(out) :: error
character(len=*), intent(out) :: errormsg
integer :: dim, ParseError,fi,li,i
dim = countElements(string,ParseError)
if (ParseError.ne.0) then
write(errormsg,*) 'hashArray: parsing error in string "',trim(string),'"'
error = -2
return
end if
fi = 1;
call parseItem(string,fi,li,ParseError)
if (ParseError.ne.0) then
write(errormsg,*) 'hashArray: parsing error in string "',trim(string),'"'
error = -2
return
end if
allocate(array(dim))
fi = fi+1
call parseItem(string,fi,li,ParseError)
array(1) = string(fi:li)
do i=2,dim
fi=li+1
call parseItem(string,fi,li,ParseError)
fi=li+1
call parseItem(string,fi,li,ParseError)
array(i) = string(fi:li)
end do
end subroutine hashVec
!________________________________________________________________________________
!
logical function domatch(str,pattern)
implicit none
character(*), intent(in) :: str,pattern
# ifdef GMATCH
integer :: match
external match
domatch = match(trim(str)//char(0),trim(pattern)//char(0)).eq.1
# else
domatch = trim(str).eq.trim(pattern)
#endif
end function
!________________________________________________________________________________
!
subroutine getInitString(filename,variable,string,lnNumber,error,errormsg)
implicit none
character(len=*), intent(in) :: filename, variable
character(len=*), intent(out) :: string
integer, intent(out) :: lnNumber, error
character(len=*), intent(out) :: errormsg
! error = 0 every thing is fine
! error = -1 variable not found
! error = -2 syntax error in file
character(len=maxLength) :: line
integer iostat, lineNumber, ParseError
integer fi,li, iu
#ifdef _OPENMP
integer omp_get_thread_num
#endif
error = -1
lineNumber = 1
!$omp critical (reserveFreeUnit)
iu = freeunit()
! iu=20234+omp_get_thread_num()
! write(6,*) 'open iu',iu,omp_get_thread_num()
open(iu,file=filename,status='old')
! write(6,*) 'opened iu',iu,omp_get_thread_num()
read(iu,'(A)',iostat=iostat) line
do while (iostat.eq.0)
fi = 1
call parseItem(line,fi,li,ParseError)
if (ParseError.ne.0) then
write(errormsg,*) 'getInitString: parsing error at line number ', &
lineNumber,' in file "',trim(filename),'"'
error = -2
exit
end if
! if ((fi.le.len(line)).and.(line(fi:li).eq.trim(variable))) then
if ((fi.le.len(line)).and.(domatch(trim(variable),line(fi:li)))) then
error = 0
lnNumber = lineNumber
fi = li+1
call parseItem(line,fi,li,ParseError)
if (ParseError.ne.0) then
write(errormsg,*) 'getInitString: parsing error at line number ', &
lineNumber,' in file "',trim(filename),'"'
error = -2
exit
end if
if ((fi.gt.len(line)).or.(line(fi:li).ne.'=')) then
write(errormsg,*) 'getInitString: Missing "=" at line number ', &
lineNumber,' in file "',trim(filename),'"'
error = -2
exit
end if
fi = li+1
call parseItem(line,fi,li,ParseError)
if (ParseError.ne.0) then
write(errormsg,*) 'getInitString: parsing error at line number ', &
lineNumber,' in file "',trim(filename),'"'
error = -2
exit
end if
string = line(fi:li)
end if
read(iu,'(A)',iostat=iostat) line
lineNumber = lineNumber + 1
end do
!!$omp critical (reserveFreeUnit)
close(iu)
!$omp end critical (reserveFreeUnit)
!!$omp end critical
if (error.eq.-1) then
write(errormsg,*) 'getInitString: variable "',trim(variable),'" not found ', &
' in file "',trim(filename),'"'
end if
end subroutine getInitString
!________________________________________________________________________________
!
subroutine getInitValue_integer(filename,variable,value,err,default)
implicit none
character(len=*), intent(in) :: filename, variable
integer, intent(out) :: value
integer, intent(out), optional :: err
integer, intent(in), optional :: default
! error = 0 every thing is fine
! error = -1 variable not found
! error = -2 syntax error in file
character(len=maxLength) :: string
character(len=maxLength) :: errormsg
integer lineNumber, ConversionError, error
call getInitString(filename,variable,string,lineNumber,error,errormsg)
if (error.eq.0) then
read(string,*,iostat=ConversionError) value
if (ConversionError.ne.0) then
write(errormsg,*) 'getInitValue_integer: error at line ',lineNumber, &
' in file "',trim(filename),'". Integer expected but "', &
trim(string),'" found.'
error = -2
end if
end if
if (error.eq.-1.and.present(default)) then
value=default
error = 0
end if
! error handling
if (present(err)) then
! if an error occurs, it will be treated at the calling level
err = error
else
if (error.ne.0) then
write(stderr,*) trim(errormsg)
stop 'Program stoped in initfile.F90 around line 361'
end if
end if
end subroutine getInitValue_integer
!________________________________________________________________________________
!
subroutine getInitValue_real(filename,variable,value,err,default)
implicit none
character(len=*), intent(in) :: filename, variable
real, intent(out) :: value
integer, intent(out), optional :: err
real, intent(in), optional :: default
! error = 0 every thing is fine
! error = -1 variable not found
! error = -2 syntax error in file
character(len=maxLength) :: string
character(len=maxLength) :: errormsg
integer lineNumber, ConversionError, error
call getInitString(filename,variable,string,lineNumber,error,errormsg)
if (error.eq.0) then
read(string,*,iostat=ConversionError) value
if (ConversionError.ne.0) then
write(errormsg,*) 'getInitValue_real: error at line ',lineNumber, &
' in file "',trim(filename),'". Real expected but "', &
trim(string),'" found.'
error = -2
end if
end if
if (error.eq.-1.and.present(default)) then
value=default
error = 0
end if
! error handling
if (present(err)) then
! if an error occurs, it will be treated at the calling level
err = error
else
if (error.ne.0) then
write(stderr,*) trim(errormsg)
stop 'Program stoped in initfile.F90 around line 412'
end if
end if
end subroutine getInitValue_real
!________________________________________________________________________________
!
subroutine getInitValue_string(filename,variable,value,err,default)
implicit none
character(len=*), intent(in) :: filename, variable
character(len=*), intent(out) :: value
integer, intent(out), optional :: err
character(len=*), intent(in), optional :: default
! error = 0 every thing is fine
! error = -1 variable not found
! error = -2 syntax error in file
character(len=maxLength) :: string
character(len=maxLength) :: errormsg
integer lineNumber, ConversionError, error
call getInitString(filename,variable,string,lineNumber,error,errormsg)
if (error.eq.0) then
read(string,*,iostat=ConversionError) value
if (ConversionError.ne.0) then
write(errormsg,*) 'getInitValue_string: error at line ',lineNumber, &
' in file "',trim(filename),'". Character string expected but "', &
trim(string),'" found.'
error = -2
end if
end if
if (error.eq.-1.and.present(default)) then
value=default
error = 0
end if
! write(6,*) 'variable,value ',variable,value
! error handling
if (present(err)) then
! if an error occurs, it will be treated at the calling level
err = error
else
if (error.ne.0) then
write(stderr,*) trim(errormsg)
stop 'Program stoped in initfile.F90 around line 463'
end if
end if
end subroutine getInitValue_string
!________________________________________________________________________________
!
subroutine getInitValue_integer1D(filename,variable,value,err,default)
implicit none
character(len=*), intent(in) :: filename, variable
integer, pointer :: value(:)
integer, intent(out), optional :: err
integer, intent(in), optional :: default(:)
! error = 0 every thing is fine
! error = -1 variable not found
! error = -2 syntax error in file
character(len=maxLength) :: string
character(len=maxLength) :: errormsg
character(len=maxLength), pointer :: vec(:)
integer lineNumber, ConversionError, error, i
call getInitString(filename,variable,string,lineNumber,error,errormsg)
if (error.eq.0) then
call hashVec(string,vec,error,errormsg)
if (error.eq.0) then
allocate(value(size(vec)))
do i=1,size(vec)
read(vec(i),*,iostat=ConversionError) value(i)
if (ConversionError.ne.0) then
write(errormsg,*) 'getInitValue_integer1D: error at line ', &
lineNumber,' in file "',trim(filename),'"'
error = -2
exit
end if
end do
deallocate(vec)
end if
end if
if (error.eq.-1.and.present(default)) then
allocate(value(size(default)))
value=default
error = 0
end if
! error handling
if (present(err)) then
! if an error occurs, it will be treated at the calling level
err = error
else
if (error.ne.0) then
write(stderr,*) trim(errormsg)
stop 'Program stoped in initfile.F90 around line 525'
end if
end if
end subroutine getInitValue_integer1D
!________________________________________________________________________________
!
subroutine getInitValue_real1D(filename,variable,value,err,default)
implicit none
character(len=*), intent(in) :: filename, variable
real, pointer :: value(:)
integer, intent(out), optional :: err
real, intent(in), optional :: default(:)
! error = 0 every thing is fine
! error = -1 variable not found
! error = -2 syntax error in file
character(len=maxLength) :: string
character(len=maxLength) :: errormsg
character(len=maxLength), pointer :: vec(:)
integer lineNumber, ConversionError, error, i
call getInitString(filename,variable,string,lineNumber,error,errormsg)
if (error.eq.0) then
call hashVec(string,vec,error,errormsg)
if (error.eq.0) then
allocate(value(size(vec)))
do i=1,size(vec)
read(vec(i),*,iostat=ConversionError) value(i)
if (ConversionError.ne.0) then
write(errormsg,*) 'getInitValue_real1D: error at line ', &
lineNumber,' in file "',trim(filename),'"'
error = -2
exit
end if
end do
deallocate(vec)
end if
end if
if (error.eq.-1.and.present(default)) then
allocate(value(size(default)))
value=default
error = 0
end if
! error handling
if (present(err)) then
! if an error occurs, it will be treated at the calling level
err = error
else
if (error.ne.0) then
write(stderr,*) trim(errormsg)
stop 'Program stoped in initfile.F90 around line 525'
end if
end if
end subroutine getInitValue_real1D
!________________________________________________________________________________
!
subroutine getInitValue_string1D(filename,variable,value,err,default)
implicit none
character(len=*), intent(in) :: filename, variable
character(len=*), pointer :: value(:)
integer, intent(out), optional :: err
character(len=*), intent(in), optional :: default(:)
! error = 0 every thing is fine
! error = -1 variable not found
! error = -2 syntax error in file
character(len=maxLength) :: string
character(len=maxLength) :: errormsg
character(len=maxLength), pointer :: vec(:)
integer lineNumber, ConversionError, error, i
call getInitString(filename,variable,string,lineNumber,error,errormsg)
if (error.eq.0) then
call hashVec(string,vec,error,errormsg)
if (error.eq.0) then
allocate(value(size(vec)))
do i=1,size(vec)
read(vec(i),*,iostat=ConversionError) value(i)
if (ConversionError.ne.0) then
write(errormsg,*) 'getInitValue_string1D: error at line ', &
lineNumber,' in file "',trim(filename),'"'
error = -2
exit
end if
end do
deallocate(vec)
end if
end if
if (error.eq.-1.and.present(default)) then
allocate(value(size(default)))
value=default
error = 0
end if
! error handling
if (present(err)) then
! if an error occurs, it will be treated at the calling level
err = error
else
if (error.ne.0) then
write(stderr,*) trim(errormsg)
stop 'Program stoped in initfile.F90 around line 655'
end if
end if
end subroutine getInitValue_string1D
!________________________________________________________________________________
!
logical function presentInitValue(filename,variable,err)
implicit none
character(len=*), intent(in) :: filename, variable
integer, intent(out), optional :: err
! error = 0 every thing is fine
! error = -2 syntax error in file
character(len=maxLength) :: string
character(len=maxLength) :: errormsg
integer lineNumber, error
call getInitString(filename,variable,string,lineNumber,error,errormsg)
if (error.eq.0) then
presentInitValue = .true.
elseif (error.eq.-1) then
presentInitValue = .false.
else
presentInitValue = .false.
if (present(err)) then
err = error
else
write(stderr,*) trim(errormsg)
write(stderr,*) 'Program stoped in ',__FILE__,' at line ',__LINE__
ERROR_STOP
end if
end if
end function
!________________________________________________________________________________
!
end module InitFile