-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathyaml_settings.F90
1918 lines (1671 loc) · 76.8 KB
/
yaml_settings.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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module yaml_settings
use iso_fortran_env, only: error_unit
use yaml_types, only: yaml_real_kind => real_kind, type_yaml_node => type_node, type_yaml_null => type_null, &
type_yaml_scalar => type_scalar, type_yaml_dictionary => type_dictionary, type_yaml_list => type_list, &
type_yaml_list_item => type_list_item, type_yaml_error => type_error, type_yaml_key_value_pair => type_key_value_pair, &
string_lower
use yaml, only: yaml_parse => parse, yaml_error_length => error_length
implicit none
private
public type_settings, type_option, option, type_settings_create, type_header, type_scalar_value
public type_dictionary_populator, type_list_populator, type_settings_node, type_key_value_pair, type_list_item
public type_real_setting, type_logical_setting, type_integer_setting, type_string_setting
public type_real_setting_create, type_logical_setting_create, type_integer_setting_create, type_string_setting_create
public format_real, format_integer, error_reporter_proc, default_minimum_real, default_maximum_real
integer, parameter :: rk = yaml_real_kind
real(rk), parameter :: default_minimum_real = -huge(1._rk)
real(rk), parameter :: default_maximum_real = huge(1._rk)
integer, parameter :: default_minimum_integer = -huge(1)
integer, parameter :: default_maximum_integer = huge(1)
integer, parameter :: yaml_indent = 3
integer, parameter :: xml_indent = 3
integer, parameter, public :: display_inherit = -1
integer, parameter, public :: display_minimum = 0
integer, parameter, public :: display_normal = 1
integer, parameter, public :: display_advanced = 2
integer, parameter, public :: display_hidden = 3
interface
subroutine error_reporter_proc(message)
character(len=*), intent(in) :: message
end subroutine
end interface
type type_value
character(len=:), allocatable :: long_name
character(len=:), allocatable :: description
class (type_yaml_node), pointer :: backing_store_node => null()
character(len=:), allocatable :: path ! repurposed 2024-03-28 to hold path to yaml file
class (type_settings_node), pointer :: node => null()
procedure(error_reporter_proc), pointer, nopass :: error_reporter => null()
class (type_value), pointer :: parent => null()
integer :: display = display_inherit
contains
procedure :: write_schema => value_write_schema
procedure :: write_yaml => value_write_yaml
procedure :: get_maximum_depth => value_get_maximum_depth
procedure :: is_visible => value_is_visible
procedure :: get_yaml_style => value_get_yaml_style
procedure :: create_child
procedure :: get_path
procedure :: ignore_node
generic :: ignore => ignore_node
procedure :: finalize => value_finalize
procedure :: report_error
procedure :: dummy => report_error ! workaround for nvfortran/aocc (likely PGI) bug
end type type_value
type type_settings_node
class (type_value), pointer :: value => null()
logical :: own_value = .true.
contains
procedure :: set_value => node_set_value
end type
type, extends(type_settings_node) :: type_key_value_pair
character(len=:), allocatable :: key
character(len=:), allocatable :: name
logical :: accessed = .false.
logical :: from_populator = .false.
integer :: order = 0
type (type_key_value_pair), pointer :: next => null()
end type
type, extends(type_settings_node) :: type_list_item
integer :: index
type (type_list_item), pointer :: next => null()
end type
type, abstract :: type_dictionary_populator
contains
procedure (dictionary_populator_create), deferred :: create
end type
type, abstract :: type_list_populator
contains
procedure :: set_length => list_populator_set_length
procedure (list_populator_create), deferred :: create
end type
interface
recursive subroutine list_populator_create(self, index, item)
import type_list_populator, type_list_item
class (type_list_populator), intent(inout) :: self
integer, intent(in) :: index
type (type_list_item), intent(inout) :: item
end subroutine
recursive subroutine dictionary_populator_create(self, pair)
import type_dictionary_populator, type_key_value_pair
class (type_dictionary_populator), intent(inout) :: self
type (type_key_value_pair), intent(inout) :: pair
end subroutine
end interface
type, extends(type_value) :: type_settings
class (type_yaml_dictionary), pointer :: backing_store => null()
type (type_key_value_pair), pointer :: first => null()
contains
procedure :: write_schema => settings_write_schema
procedure :: write_yaml => settings_write_yaml
procedure :: get_maximum_depth => settings_get_maximum_depth
procedure :: is_visible => settings_is_visible
procedure :: get_yaml_style => settings_get_yaml_style
procedure :: load
procedure :: take_values
procedure :: save
procedure :: write_schema_file
procedure :: get_real2
procedure :: get_integer2
procedure :: get_logical2
procedure :: get_string2
procedure :: get_real
procedure :: get_integer
procedure :: get_logical
procedure :: get_string
procedure :: get_child
procedure :: attach_child
procedure :: get_list
procedure :: get_node
procedure :: check_all_used
generic :: get => get_real2, get_integer2, get_logical2, get_string2
procedure :: populate => settings_populate
procedure :: ignore_child
generic :: ignore => ignore_child
procedure :: finalize
procedure :: finalize_store
end type type_settings
type, abstract, extends(type_value) :: type_scalar_value
character(:),allocatable :: units
logical :: has_default = .false.
contains
procedure (scalar_value_as_string), deferred :: as_string
procedure (scalar_value_at_default), deferred :: at_default
procedure :: write_yaml => scalar_value_write_yaml
procedure :: get_comment => scalar_value_get_comment
procedure :: get_maximum_depth => scalar_value_get_maximum_depth
procedure :: is_visible => scalar_value_is_visible
end type type_scalar_value
abstract interface
function scalar_value_as_string(self, use_default) result(string)
import type_scalar_value
class (type_scalar_value), intent(in) :: self
logical, intent(in) :: use_default
character(len=:), allocatable :: string
end function
logical function scalar_value_at_default(self)
import type_scalar_value
class (type_scalar_value), intent(in) :: self
end function
end interface
type type_option
integer :: value
character(:), allocatable :: long_name
character(:), allocatable :: key
end type
type, extends(type_value) :: type_list
type (type_list_item), pointer :: first => null()
contains
procedure :: write_schema => list_write_schema
procedure :: write_yaml => list_write_yaml
procedure :: get_maximum_depth => list_get_maximum_depth
procedure :: get_yaml_style => list_get_yaml_style
procedure :: is_visible => list_is_visible
procedure :: finalize => list_finalize
end type
type, extends(type_scalar_value) :: type_integer_setting
integer, pointer :: pvalue => null()
integer :: value
integer :: default = 0
integer :: minimum = default_minimum_integer
integer :: maximum = default_maximum_integer
type (type_option), allocatable :: options(:)
contains
procedure :: as_string => integer_as_string
procedure :: write_schema => integer_write_schema
procedure :: get_comment => integer_get_comment
procedure :: at_default => integer_at_default
end type
type, extends(type_scalar_value) :: type_real_setting
real(rk), pointer :: pvalue => null()
real(rk) :: value
real(rk) :: default = 0.0_rk
real(rk) :: minimum = default_minimum_real
real(rk) :: maximum = default_maximum_real
real(rk) :: scale_factor = 1.0_rk
contains
procedure, nopass :: create => type_real_setting_create
procedure :: as_string => real_as_string
procedure :: write_schema => real_write_schema
procedure :: get_comment => real_get_comment
procedure :: at_default => real_at_default
end type
type, extends(type_scalar_value) :: type_logical_setting
logical, pointer :: pvalue => null()
logical :: value
logical :: default = .true.
contains
procedure, nopass :: create => type_logical_setting_create
procedure :: as_string => logical_as_string
procedure :: write_schema => logical_write_schema
procedure :: at_default => logical_at_default
end type
type, extends(type_scalar_value) :: type_string_setting
character(:), pointer :: pvalue => null()
character(:), allocatable :: value
character(:), allocatable :: default
contains
procedure :: as_string => string_as_string
procedure :: write_schema => string_write_schema
procedure :: at_default => string_at_default
end type
type type_line
character(:), allocatable :: text
type (type_line), pointer :: next => null()
end type
type type_header
type (type_line), pointer :: first_line => null()
contains
procedure :: append => header_append
end type
contains
type (type_option) function option(value, long_name, key)
integer, intent(in) :: value
character(len=*), intent(in) :: long_name
character(len=*), optional, intent(in) :: key
option%value = value
option%long_name = long_name
if (present(key)) option%key = key
end function option
recursive subroutine value_write_schema(self, unit, name, indent)
class (type_value), intent(in) :: self
integer, intent(in) :: unit, indent
character(len=*), intent(in) :: name
stop 'value_write_schema not overridden'
end subroutine
recursive subroutine value_write_yaml(self, unit, indent, comment_depth, header, display)
class (type_value), intent(in) :: self
integer, intent(in) :: unit
integer, intent(in) :: indent
integer, intent(in) :: comment_depth
logical, intent(in) :: header
integer, intent(in) :: display
stop 'value_write_yaml not overridden'
end subroutine
recursive function value_get_maximum_depth(self, name, display) result(maxdepth)
class (type_value), intent(in) :: self
character(len=*), intent(in) :: name
integer, intent(in) :: display
integer :: maxdepth
maxdepth = -1
stop 'value_get_maximum_depth not overridden'
end function
recursive function value_is_visible(self, display) result(visible)
class (type_value), intent(in) :: self
integer, intent(in) :: display
logical :: visible
visible = display >= get_display(self)
end function
integer function value_get_yaml_style(self, display)
class (type_value), intent(in) :: self
integer, intent(in) :: display
value_get_yaml_style = -1
end function
function get_path(self, include_file) result(path)
class (type_value), target, intent(in) :: self
logical, optional, intent(in) :: include_file
character(len=:), allocatable :: path
logical :: include_file_
class (type_value), pointer :: value
character(len=8) :: strindex
include_file_ = .true.
if (present(include_file)) include_file_ = include_file
path = ''
value => self
do while (associated(value))
if (allocated(value%path) .and. include_file_) then
if (len(value%path) > 0) then
path = value%path // ':' // path
return
end if
end if
if (.not. associated(value%node)) return
select type (node => value%node)
class is (type_key_value_pair)
path = '/' // node%name // path
class is (type_list_item)
write (strindex,'(i0)') node%index
path = '[' // trim(strindex) // ']' // path
end select
value => value%parent
end do
end function
recursive subroutine value_finalize(self)
class (type_value), intent(inout) :: self
end subroutine
integer function settings_get_yaml_style(self, display)
class (type_settings), intent(in) :: self
integer, intent(in) :: display
type (type_key_value_pair), pointer :: pair
settings_get_yaml_style = yaml_indent
pair => self%first
do while (associated(pair))
if (pair%from_populator .or. pair%value%is_visible(display)) return
pair => pair%next
end do
! No children are visible - this is an empty block indicated by -2
settings_get_yaml_style = -2
end function
integer function list_get_yaml_style(self, display)
class (type_list), intent(in) :: self
integer, intent(in) :: display
list_get_yaml_style = 0
if (.not. associated(self%first)) list_get_yaml_style = -2
end function
subroutine load(self, path, unit, error_reporter)
class (type_settings), intent(inout) :: self
character(len=*), intent(in) :: path
integer, intent(in) :: unit
procedure(error_reporter_proc), optional :: error_reporter
class (type_yaml_node), pointer :: root
character(len=yaml_error_length) :: error
if (present(error_reporter)) self%error_reporter => error_reporter
root => yaml_parse(path, unit, error)
if (error /= '') call self%report_error(trim(error))
self%path = trim(path)
self%backing_store_node => root
call settings_set_data(self)
end subroutine load
subroutine take_values(self, other)
class (type_settings), intent(inout) :: self
class (type_settings), intent(inout) :: other
self%error_reporter => other%error_reporter
self%backing_store_node => other%backing_store_node
other%backing_store_node => null()
other%backing_store => null()
call settings_set_data(self)
end subroutine take_values
function ignore_child(self, name) result(found)
class (type_settings), intent(inout) :: self
character(len=*), intent(in) :: name
logical :: found
class (type_yaml_dictionary), pointer :: parent
class (type_yaml_node), pointer :: node
integer :: islash, istart_
found = .false.
istart_ = 1
parent => self%backing_store
do while (associated(parent))
islash = index(name(istart_:), '/')
if (islash == 0) exit
node => parent%get(name(istart_:istart_+islash-2))
select type (node)
class is(type_yaml_dictionary)
parent => node
class default
parent => null()
end select
istart_ = istart_ + islash
end do
if (associated(parent)) then
node => parent%get(name(istart_:))
if (associated(node)) then
found = .true.
call node%set_accessed(.true., .true.)
end if
end if
end function
logical function ignore_node(self)
class (type_value), intent(inout) :: self
if (associated(self%backing_store_node)) &
call self%backing_store_node%set_accessed(.true., .true.)
ignore_node = .true.
end function
logical function check_all_used(self, finalize_store)
class (type_settings), intent(inout) :: self
logical, optional, intent(in) :: finalize_store
logical :: finalize_store_
integer :: n
finalize_store_ = .true.
if (present(finalize_store)) finalize_store_ = finalize_store
n = 0
if (associated(self%backing_store_node)) then
if (self%backing_store_node%path /= '') &
call self%report_error('BUG: check_all_used can only be called on settings initialized by load')
call node_check(self%backing_store_node, n)
end if
if (finalize_store_) call self%finalize_store()
check_all_used = n == 0
contains
recursive subroutine node_check(self, n)
class (type_yaml_node), intent(in) :: self
integer, intent(inout) :: n
type (type_yaml_list_item), pointer :: item
type (type_yaml_key_value_pair), pointer :: pair
select type (self)
class is (type_yaml_dictionary)
pair => self%first
do while (associated(pair))
if (.not. pair%accessed) then
n = n + 1
if (n == 1) write (error_unit,*) 'ERROR: the following setting(s) were not recognized:'
write (error_unit,*) '- ' // trim(pair%value%path)
else
call node_check(pair%value, n)
end if
pair => pair%next
end do
class is (type_yaml_list)
item => self%first
do while (associated(item))
call node_check(item%node, n)
item => item%next
end do
end select
end subroutine
end function check_all_used
subroutine save(self, path, unit, display, header)
class (type_settings), intent(in) :: self
character(len=*), intent(in) :: path
integer, intent(in) :: unit
integer, optional, intent(in) :: display
type (type_header), optional, intent(in) :: header
integer :: ios
type (type_line), pointer :: line
integer :: display_
integer :: comment_depth
open(unit=unit, file=path, action='write', encoding='UTF-8', status='replace', iostat=ios)
if (ios /= 0) call self%report_error('Failed to open '//path//' for writing.')
if (present(header)) then
line => header%first_line
do while (associated(line))
write (unit,'("# ",a)') line%text
line => line%next
end do
end if
display_ = display_hidden
if (present(display)) display_ = display
comment_depth = self%get_maximum_depth('', display_) + 1
call self%write_yaml(unit, 0, comment_depth, header=.false., display=display_)
end subroutine save
subroutine write_schema_file(self, path, unit, version)
class (type_settings), intent(in) :: self
character(len=*), intent(in) :: path
integer, intent(in) :: unit
character(len=*), intent(in) :: version
integer :: ios
type (type_key_value_pair),pointer :: pair
open(unit=unit, file=path, action='write', status='replace', iostat=ios)
if (ios /= 0) call self%report_error('Failed to open '//path//' for writing.')
write (unit,'(a)') '<?xml version="1.0" ?>'
write (unit,'(a,a,a)') '<element name="scenario" label="scenario" version="', version, '" namelistextension=".nml"&
& xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../core/scenario-1.0.xsd">'
pair => self%first
do while (associated(pair))
call pair%value%write_schema(unit, pair%name, xml_indent)
pair => pair%next
end do
write (unit,'(a)') '</element>'
end subroutine write_schema_file
recursive function get_node(self, name, treat_as_path, istart, order) result(pair)
class (type_settings), intent(inout), target :: self
character(len=*), intent(in) :: name
logical, optional, intent(in) :: treat_as_path
integer, optional, intent(out) :: istart
integer, optional, intent(in) :: order
class (type_key_value_pair), pointer :: pair
integer :: istart_
class (type_settings), pointer :: settings
logical :: treat_as_path_
integer :: islash
integer :: order_
istart_ = 1
settings => self
order_ = 0
if (present(order)) order_ = order
treat_as_path_ = .true.
if (present(treat_as_path)) treat_as_path_ = treat_as_path
if (treat_as_path_) then
do
islash = index(name(istart_:), '/')
if (islash == 0) exit
settings => get_child(settings, name(istart_:istart_+islash-2), treat_as_path=.false.)
istart_ = istart_ + islash
end do
end if
if (present(istart)) istart = istart_
call get(settings, name(istart_:))
contains
subroutine get(self, name)
class (type_settings), intent(inout), target :: self
character(len=*), intent(in) :: name
character(len=len(name)) :: key
type (type_key_value_pair), pointer :: previous
key = string_lower(name)
! First try to find an existing pair with this key.
previous => null()
pair => self%first
do while (associated(pair))
if (pair%key == key) then
if (pair%accessed) return
exit
end if
if (pair%accessed .and. order_ >= pair%order) previous => pair
pair => pair%next
end do
if (.not. associated(pair)) then
! Key not found - create a new key-setting pair and insert after all previously accessed settings (if any)
allocate(pair)
pair%key = key
allocate(type_value::pair%value)
pair%value%parent => self
pair%value%node => pair
if (associated(self%backing_store)) pair%value%backing_store_node => self%backing_store%get(key, case_sensitive=.false.)
end if
pair%name = name
pair%accessed = .true.
pair%order = order_
if (associated(previous)) then
pair%next => previous%next
previous%next => pair
else
pair%next => self%first
self%first => pair
end if
end subroutine
end function get_node
function get_real(self, name, long_name, units, default, minimum, maximum, scale_factor, description, display) result(value)
class (type_settings), intent(inout) :: self
character(len=*), intent(in) :: name
character(len=*), intent(in) :: long_name
character(len=*), intent(in) :: units
real(rk), optional, intent(in) :: default
real(rk), optional, intent(in) :: minimum
real(rk), optional, intent(in) :: maximum
real(rk), optional, intent(in) :: scale_factor
character(len=*),optional, intent(in) :: description
integer, optional, intent(in) :: display
real(rk) :: value
class (type_real_setting), pointer :: setting
class (type_key_value_pair), pointer :: pair
pair => self%get_node(name)
setting => type_real_setting_create(pair, long_name, units, &
default, minimum, maximum, scale_factor, description, display=display)
value = setting%pvalue
end function get_real
subroutine get_real2(self, target, name, long_name, units, default, minimum, maximum, scale_factor, description, display)
class (type_settings),intent(inout) :: self
real(rk), target :: target
character(len=*), intent(in) :: name
character(len=*), intent(in) :: long_name
character(len=*), intent(in) :: units
real(rk), optional, intent(in) :: default
real(rk), optional, intent(in) :: minimum
real(rk), optional, intent(in) :: maximum
real(rk), optional, intent(in) :: scale_factor
character(len=*),optional, intent(in) :: description
integer, optional, intent(in) :: display
class (type_real_setting), pointer :: setting
class (type_key_value_pair), pointer :: pair
pair => self%get_node(name)
setting => type_real_setting_create(pair, long_name, units, &
default, minimum, maximum, scale_factor, description, target=target, display=display)
end subroutine
function type_real_setting_create(node, long_name, units, default, minimum, maximum, scale_factor, &
description, target, display) result(setting)
class (type_settings_node), intent(inout) :: node
character(len=*), intent(in) :: long_name
character(len=*), intent(in) :: units
real(rk), optional, intent(in) :: default
real(rk), optional, intent(in) :: minimum
real(rk), optional, intent(in) :: maximum
real(rk), optional, intent(in) :: scale_factor
character(len=*),optional, intent(in) :: description
real(rk), target, optional :: target
integer, optional, intent(in) :: display
class (type_real_setting), pointer :: setting
logical :: reuse_value
select type (value => node%value)
class is (type_real_setting)
setting => value
reuse_value = .true.
class default
allocate(setting)
call node%set_value(setting)
reuse_value = .false.
end select
if (present(target)) then
setting%pvalue => target
else
setting%pvalue => setting%value
end if
setting%long_name = long_name
if (units /= '') setting%units = units
if (present(minimum)) setting%minimum = minimum
if (present(maximum)) setting%maximum = maximum
if (present(scale_factor)) setting%scale_factor = scale_factor
if (present(display)) setting%display = display
if (present(default)) then
if (default < setting%minimum) call setting%report_error('Default value of setting '//setting%get_path()// &
' lies below prescribed minimum.')
if (default > setting%maximum) call setting%report_error('Default value of setting '//setting%get_path()// &
' exceeds prescribed maximum.')
setting%has_default = .true.
setting%default = default
end if
if (present(description)) setting%description = description
if (associated(setting%backing_store_node)) then
call real_set_data(setting, setting%backing_store_node)
elseif (.not. reuse_value) then
if (setting%has_default) then
setting%pvalue = setting%default * setting%scale_factor
else
call setting%report_error('No value specified for setting '//setting%get_path()//'; cannot continue because&
& this parameter does not have a default value either.')
end if
end if
end function type_real_setting_create
subroutine real_set_data(self, backing_store_node)
class (type_real_setting), intent(inout) :: self
class (type_yaml_node), intent(in) :: backing_store_node
logical :: success
select type (backing_store_node)
class is (type_yaml_scalar)
self%pvalue = backing_store_node%to_real(self%pvalue, success)
if (.not. success) call self%report_error(self%get_path()//' is set to "'//trim(backing_store_node%string)// &
'", which cannot be interpreted as a real number.')
class default
call self%report_error('Setting '//self%get_path()//' must be a real number.')
end select
if (self%pvalue < self%minimum) call self%report_error('Value specified for parameter '//self%get_path()// &
' lies below prescribed minimum.')
if (self%pvalue > self%maximum) call self%report_error('Value specified for parameter '//self%get_path()// &
' exceeds prescribed maximum.')
self%pvalue = self%pvalue * self%scale_factor
end subroutine
function get_integer(self, name, long_name, units, default, minimum, maximum, options, description, display) result(value)
class (type_settings), intent(inout) :: self
character(len=*), intent(in) :: name
character(len=*), intent(in) :: long_name
character(len=*), optional, intent(in) :: units
integer, optional, intent(in) :: default
integer, optional, intent(in) :: minimum
integer, optional, intent(in) :: maximum
type (type_option),optional, intent(in) :: options(:)
character(len=*), optional, intent(in) :: description
integer, optional, intent(in) :: display
integer :: value
class (type_integer_setting), pointer :: setting
class (type_key_value_pair), pointer :: pair
pair => self%get_node(name)
setting => type_integer_setting_create(pair, long_name, units, default, minimum, maximum, &
options, description, display=display)
value = setting%pvalue
end function
subroutine get_integer2(self, target, name, long_name, units, default, minimum, maximum, options, description, display)
class (type_settings), intent(inout) :: self
integer, target :: target
character(len=*), intent(in) :: name
character(len=*), intent(in) :: long_name
character(len=*), optional, intent(in) :: units
integer, optional, intent(in) :: default
integer, optional, intent(in) :: minimum
integer, optional, intent(in) :: maximum
type (type_option),optional, intent(in) :: options(:)
character(len=*), optional, intent(in) :: description
integer, optional, intent(in) :: display
class (type_integer_setting), pointer :: setting
class (type_key_value_pair), pointer :: pair
pair => self%get_node(name)
setting => type_integer_setting_create(pair, long_name, units, default, minimum, maximum, &
options, description, target=target, display=display)
end subroutine
function type_integer_setting_create(node, long_name, units, default, minimum, maximum, options, &
description, target, display) result(setting)
class (type_settings_node), intent(inout) :: node
character(len=*), intent(in) :: long_name
character(len=*), optional, intent(in) :: units
integer, optional, intent(in) :: default
integer, optional, intent(in) :: minimum
integer, optional, intent(in) :: maximum
type (type_option),optional, intent(in) :: options(:)
character(len=*), optional, intent(in) :: description
integer, target, optional :: target
integer, optional, intent(in) :: display
class (type_integer_setting), pointer :: setting
logical :: reuse_value
integer :: ioption, ioption2, ivalue
logical :: found
select type (value => node%value)
class is (type_integer_setting)
setting => value
reuse_value = .true.
class default
allocate(setting)
call node%set_value(setting)
reuse_value = .false.
end select
if (present(target)) then
setting%pvalue => target
else
setting%pvalue => setting%value
end if
setting%long_name = long_name
if (present(units)) setting%units = units
if (present(minimum)) setting%minimum = minimum
if (present(maximum)) setting%maximum = maximum
if (present(display)) setting%display = display
if (present(options)) then
do ioption = 1, size(options)
do ioption2 = ioption + 1, size(options)
if (options(ioption)%value == options(ioption2)%value) call setting%report_error( &
'Setting '//setting%get_path()//' has multiple options with the same integer value.')
end do
end do
if (allocated(setting%options)) deallocate(setting%options)
allocate(setting%options(size(options)))
! Order options according to value
ioption = 1
do ivalue = minval(options(:)%value), maxval(options(:)%value)
do ioption2 = 1, size(options)
if (options(ioption2)%value == ivalue) then
setting%options(ioption) = options(ioption2)
if (allocated(setting%options(ioption)%key)) &
setting%options(ioption)%key = string_lower(setting%options(ioption)%key)
ioption = ioption + 1
exit
end if
end do
end do
end if
if (present(default)) then
if (default < setting%minimum) call setting%report_error('Default value of setting '//setting%get_path()// &
' lies below prescribed minimum.')
if (default > setting%maximum) call setting%report_error('Default value of setting '//setting%get_path()// &
' exceeds prescribed maximum.')
if (allocated(setting%options)) then
found = .false.
do ioption = 1, size(setting%options)
if (default == setting%options(ioption)%value) found = .true.
end do
if (.not.found) call setting%report_error('Default value of setting '//setting%get_path()// &
' does not correspond to any known option.')
end if
setting%has_default = .true.
setting%default = default
end if
if (present(description)) setting%description = description
if (associated(setting%backing_store_node)) then
call integer_set_data(setting, setting%backing_store_node)
elseif (.not. reuse_value) then
if (setting%has_default) then
setting%pvalue = setting%default
else
call setting%report_error('No value specified for setting '//setting%get_path()//'; cannot continue because&
& it does not have a default value either.')
end if
end if
end function type_integer_setting_create
subroutine integer_set_data(self, backing_store_node)
class (type_integer_setting), intent(inout) :: self
class (type_yaml_node), intent(in) :: backing_store_node
logical :: success
integer :: ioption
character(len=:), allocatable :: str, stroptions
character(len=8) :: strtmp
select type (backing_store_node)
class is (type_yaml_scalar)
success = .false.
if (allocated(self%options)) then
str = string_lower(trim(backing_store_node%string))
do ioption = 1, size(self%options)
if (str == string_lower(self%options(ioption)%long_name)) then
! Value matches long name of option
success = .true.
elseif (allocated(self%options(ioption)%key)) then
! Option has a key; check if value matches that
if (str == self%options(ioption)%key) success = .true.
end if
if (success) then
self%pvalue = self%options(ioption)%value
exit
end if
end do
end if
if (.not. success) self%pvalue = backing_store_node%to_integer(self%pvalue, success)
if (.not. success) then
if (allocated(self%options)) then
! Include list of allowed options in error message
stroptions = ''
do ioption = 1, size(self%options)
write (strtmp,'(i0)') self%options(ioption)%value
if (allocated(self%options(ioption)%key)) then
str = self%options(ioption)%key // ' (' // trim(strtmp) // ')'
if (self%options(ioption)%key /= string_lower(self%options(ioption)%long_name)) then
stroptions = stroptions // achar(10) // '- ' // str // ' = ' // self%options(ioption)%long_name
else
stroptions = stroptions // achar(10) // '- ' // str
end if
else
stroptions = stroptions // achar(10) // '- ' // trim(strtmp) // ' = ' // self%options(ioption)%long_name
end if
end do
call self%report_error(self%get_path()//' is set to "'//trim(backing_store_node%string)// &
'", which is not one of the valid options:' // stroptions)
else
call self%report_error(self%get_path()//' is set to "'//trim(backing_store_node%string)// &
'", which cannot be interpreted as an integer number.')
end if
end if
class default
call self%report_error('Setting '//self%get_path()//' must be an integer number.')
end select
if (self%pvalue < self%minimum) call self%report_error('Value specified for setting '//self%get_path()// &
' lies below prescribed minimum.')
if (self%pvalue > self%maximum) call self%report_error('Value specified for setting '//self%get_path()// &
' exceeds prescribed maximum.')
if (allocated(self%options)) then
success = .false.
do ioption = 1, size(self%options)
if (self%pvalue == self%options(ioption)%value) success = .true.
end do
if (.not. success) call self%report_error('Value specified for setting '//self%get_path()// &
' does not correspond to any known option.')
end if
end subroutine integer_set_data
function get_logical(self, name, long_name, default, description, display) result(value)
class (type_settings), intent(inout) :: self
character(len=*), intent(in) :: name
character(len=*), intent(in) :: long_name
logical, optional, intent(in) :: default
character(len=*), optional, intent(in) :: description
integer, optional, intent(in) :: display
logical :: value
class (type_logical_setting), pointer :: setting
class (type_key_value_pair), pointer :: pair
pair => self%get_node(name)
setting => type_logical_setting_create(pair, long_name, default, description, display=display)
value = setting%pvalue
end function get_logical
subroutine get_logical2(self, target, name, long_name, default, description, display)
class (type_settings), intent(inout) :: self
logical, target :: target
character(len=*), intent(in) :: name
character(len=*), intent(in) :: long_name
logical, optional, intent(in) :: default
character(len=*), optional, intent(in) :: description
integer, optional, intent(in) :: display
class (type_logical_setting), pointer :: setting
class (type_key_value_pair), pointer :: pair
pair => self%get_node(name)
setting => type_logical_setting_create(pair, long_name, default, description, &
target=target, display=display)
end subroutine get_logical2
function type_logical_setting_create(node, long_name, default, description, target, display) result(setting)
class (type_settings_node), intent(inout) :: node
character(len=*), intent(in) :: long_name
logical, optional, intent(in) :: default
character(len=*), optional, intent(in) :: description
logical, target, optional :: target
integer, optional, intent(in) :: display
class (type_logical_setting), pointer :: setting
logical :: reuse_value
select type (value => node%value)
class is (type_logical_setting)
setting => value
reuse_value = .true.
class default
allocate(setting)
call node%set_value(setting)
reuse_value = .false.
end select
if (present(target)) then
setting%pvalue => target
else
setting%pvalue => setting%value
end if
setting%long_name = long_name
if (present(display)) setting%display = display
if (present(default)) then
setting%has_default = .true.
setting%default = default