-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathErrorCriteria.f90
1999 lines (1799 loc) · 115 KB
/
ErrorCriteria.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 container for `ErrorCriteria` class
module ErrorCriteriaModule
use ErrorHandlerModule
use ErrorInstanceModule
implicit none
private
! Set kind parameters for use with real variables, as per the convention
! recommended in, e.g., http://fortranwiki.org/fortran/show/Real+precision
! and N. S. Clerman and W. Spector, Modern Fortran (2012).
integer, parameter :: dp = selected_real_kind(15,307) !! Double precision, 15 digits, 1e307
integer, parameter :: qp = selected_real_kind(33,4931) !! Quadruple precision, 33 digits, 1e4931
!> ErrorCrtiera extends ErrorHandler and defines a number of common "criteria" used
!! for error checking, such as checking whether a number falls between given bounds.
!! Criteria functions expedite the error checking process with intuitive function calls
!! returning pre-defined ErrorInstances.
!! Fortran's lack of out-of-the-box key=>value arrays (e.g., dictionaries, hash
!! tables, lists) makes this seemingly trivial task a bit complex. To save overcomplicating
!! things and using, e.g., third-party linked lists, we'll specify that each criterion is
!! aligned with a specific array index, and the codes/message/criticality arrays below are
!! filed following this specification:
!! Array index Function name Default code
!! 1 nonZero 101
!! 2 zero 102
!! 3 lessThan 103
!! 4 greaterThan 104
!! 5 limit 105
!! 6 notEqual 106
!! 7 equal 107
!! 8 positive 108
!! 9 negative 109
type, public, extends(ErrorHandler) :: ErrorCriteria
! Current error criteria codes, messages and criticality will be used to keep track of the
! codes/messages/criticality that relate to the error criteria in amongst other error codes.
integer, allocatable :: currentErrorCriteriaCodes(:)
character(len=256), allocatable :: currentErrorCriteriaNames(:)
character(len=256), allocatable :: currentErrorCriteriaMessages(:)
logical, allocatable :: currentErrorCriteriaIsCritical(:)
! Default error criteria codes and messages that are used if none others specified.
integer :: defaultErrorCriteriaCodes(9)
character(len=256) :: defaultErrorCriteriaNames(9)
character(len=256) :: defaultErrorCriteriaMessages(9)
logical :: defaultErrorCriteriaIsCritical(9)
! Tolerance to accept when equating numbers (how far away from the criteria
! is the value allowed to be to pass).
real :: epsilon = 1.0e-5
contains
! Initialising and setters
procedure, public :: init => initErrorCriteria ! Overload Errorhandler init procedure
procedure, public :: setEpsilon
! Getters and adding
procedure, public :: getCodeFromCriterionName
procedure, public :: getIndexFromCriterionName
procedure, public :: addErrorCriterion
procedure, public :: addErrorCriteria
! Removing ErrorInstances: removeErrorInstance and removeErrorInstances are
! bound to remove generic in parent. Here, we overload then to check that
! the errors being removed aren't in the current error criteria.
procedure, public :: removeErrorInstance => removeErrorInstanceCheckCriteria
procedure, public :: removeMultipleErrorInstances => removeMultipleErrorInstancesCheckCriteria
! Modify error criteria codes
procedure, public :: modifyErrorCriteriaCodes
generic, public :: modifyErrorCriterionCode => modifyErrorCriterionCodeByIndex, modifyErrorCriterionCodeByName
procedure, public :: modifyErrorCriterionCodeByIndex
procedure, public :: modifyErrorCriterionCodeByName
! The criteria functions
generic, public :: limit => integerLimit, realLimit, realDPLimit, realQPLimit
generic, public :: nonZero => integerNonZero, realNonZero, realDPNonZero, realQPNonZero
generic, public :: zero => integerZero, realZero, realDPZero, realQPZero
generic, public :: lessThan => integerLessThan, realLessThan, realDPLessThan, realQPLessThan
generic, public :: greaterThan => integerGreaterThan, realGreaterThan, realDPGreaterThan, realQPGreaterThan
generic, public :: notEqual => integerNotEqual, realNotEqual, realDPNotEqual, realQPNotEqual
generic, public :: equal => integerEqual, realEqual, realDPEqual, realQPEqual
generic, public :: positive => integerPositive, realPositive, realDPPositive, realQPPositive
generic, public :: negative => integerNegative, realNegative, realDPNegative, realQPNegative
! Limit
procedure, private :: integerLimit
procedure, private :: realLimit
procedure, private :: realDPLimit
procedure, private :: realQPLimit
! Non-zero
procedure, private :: integerNonZero
procedure, private :: realNonZero
procedure, private :: realDPNonZero
procedure, private :: realQPNonZero
! Zero
procedure, private :: integerZero
procedure, private :: realZero
procedure, private :: realDPZero
procedure, private :: realQPZero
! Less than
procedure, private :: integerLessThan
procedure, private :: realLessThan
procedure, private :: realDPLessThan
procedure, private :: realQPLessThan
! Greater than
procedure, private :: integerGreaterThan
procedure, private :: realGreaterThan
procedure, private :: realDPGreaterThan
procedure, private :: realQPGreaterThan
! Not equal
procedure, private :: integerNotEqual
procedure, private :: realNotEqual
procedure, private :: realDPNotEqual
procedure, private :: realQPNotEqual
! Equal
procedure, private :: integerEqual
procedure, private :: realEqual
procedure, private :: realDPEqual
procedure, private :: realQPEqual
! Positive
procedure, private :: integerPositive
procedure, private :: realPositive
procedure, private :: realDPPositive
procedure, private :: realQPPositive
! Negative
procedure, private :: integerNegative
procedure, private :: realNegative
procedure, private :: realDPNegative
procedure, private :: realQPNegative
end type
contains
!> Initialise the ErrorCriteria and at the same time initialise
!! the parent ErrorHandler, setting custom errors.
subroutine initErrorCriteria(this, &
errors, &
criticalPrefix, &
warningPrefix, &
messageSuffix, &
bashColors, &
printErrorCode, &
triggerWarnings, &
on)
class(ErrorCriteria), intent(inout) :: this !! This ErrorCriteria instance
type(ErrorInstance), intent(in), optional :: errors(:) !! Custom defined errors
character(len=*), intent(in), optional :: criticalPrefix !! Prefix to critical error messages
character(len=*), intent(in), optional :: warningPrefix !! Prefix to warning error messages
character(len=*), intent(in), optional :: messageSuffix !! Suffix to error messages
logical, intent(in), optional :: bashColors !! Should prefixes be colored in bash shells?
logical, intent(in), optional :: printErrorCode !! Should error messages be prefixed with the error code?
logical, intent(in), optional :: triggerWarnings !! Should warnings be printing on trigger?
logical, intent(in), optional :: on !! Should the ErrorHandler output errors?
! Initialise the parent ErrorHandler
call this%ErrorHandler%init( &
errors=errors, &
criticalPrefix=criticalPrefix, &
warningPrefix=warningPrefix, &
messageSuffix=messageSuffix, &
bashColors=bashColors, &
triggerWarnings=triggerWarnings, &
printErrorCode=printErrorCode, &
on=on &
)
! Define the default error criteria. Messages will be overidden by specific
! criteria functions to provide more detail, but they're present here in case
! criteria errors triggered directly by user.
this%defaultErrorCriteriaCodes = [101,102,103,104,105,106,107,108,109]
this%defaultErrorCriteriaNames = [character(len=256) :: &
"nonZero", &
"zero", &
"lessThan", &
"greaterThan", &
"limit", &
"notEqual", &
"equal", &
"positive", &
"negative" &
]
this%defaultErrorCriteriaMessages = [character(len=256) :: &
"Value must be non-zero.", &
"Value must be zero.", &
"Value must be less than criteria.", &
"Value must be greater than criteria.", &
"Value must be between limit criteria.", &
"Value must not equal criteria.", &
"Value must equal criteria.", &
"Value must be positive.", &
"Value must be negative." &
]
this%defaultErrorCriteriaIsCritical = .true.
! Add the default error criteria codes
call this%ErrorHandler%add(&
codes = this%defaultErrorCriteriaCodes, &
messages = this%defaultErrorCriteriaMessages, &
areCritical = this%defaultErrorCriteriaIsCritical &
)
! Set the current error criteria to the current
allocate(this%currentErrorCriteriaCodes, source=this%defaultErrorCriteriaCodes)
allocate(this%currentErrorCriteriaNames, source=this%defaultErrorCriteriaNames)
allocate(this%currentErrorCriteriaMessages, source=this%defaultErrorCriteriaMessages)
allocate(this%currentErrorCriteriaIsCritical, source=this%defaultErrorCriteriaIsCritical)
end subroutine
!> Set epsilon, the tolerance allowed when equating
!! values/criteria, to account for imprecision in floating
!! point numbers.
subroutine setEpsilon(this, epsilon)
class(ErrorCriteria), intent(inout) :: this !! This ErrorCriteria instance
real, intent(in) :: epsilon !! The tolerance allowed
this%epsilon = epsilon
end subroutine
!> Modify the error codes for the error criteria. Defaults given
!! in the docs for the ErrorCriteria derived type, as well as the
!! indices required for the codes parameter. Must be the same size
!! as the number of criteria.
subroutine modifyErrorCriteriaCodes(this, codes)
class(ErrorCriteria) :: this !! This ErrorCriteria instance
integer, intent(in) :: codes(:) !! The new codes
if (size(codes) /= size(this%currentErrorCriteriaCodes)) then
error stop "Error modifying error criteria codes: Array of new codes doesn't match number of criteria functions."
end if
! Stop if we haven't initialised the error handler
call this%stopIfNotInitialised
! Remove the old codes (use the ErrorHandler's method to avoid throwing
! error that the error code is an error criteria one)
call this%ErrorHandler%remove(codes=this%currentErrorCriteriaCodes)
! Add the new ones
call this%ErrorHandler%add( &
codes=codes, &
messages=this%currentErrorCriteriaMessages, &
areCritical=this%currentErrorCriteriaIsCritical &
)
! Update the current error criteria codes array
this%currentErrorCriteriaCodes = codes
end subroutine
!> Modify criterion error at given array index, with the array
!! index corresponding to the functions according to the docs for
!! the ErrorCriteria derived type.
subroutine modifyErrorCriterionCodeByIndex(this, index, newCode)
class(ErrorCriteria) :: this !! This ErrorCriteria instance
integer, intent(in) :: index !! Array index to change code at
integer, intent(in) :: newCode !! New error code
! Stop if we haven't initialised the error handler
call this%stopIfNotInitialised
if (index > size(this%currentErrorCriteriaCodes)) then
error stop "Error modifying error criterion code by index: Index doesn't match any criteria functions."
end if
! Remove the old code (use the ErrorHandler's method to avoid throwing
! error that the error code is an error criteria one).
call this%ErrorHandler%remove(code=this%currentErrorCriteriaCodes(index))
! Add the new code
call this%ErrorHandler%add( &
code=newCode, &
message=this%currentErrorCriteriaMessages(index), &
isCritical=this%currentErrorCriteriaIsCritical(index) &
)
! Update the current error criteria codes array
this%currentErrorCriteriaCodes(index) = newCode
end subroutine
!> Modify criterion error code with given criterion name, with the array
!! index corresponding to the functions according to the docs for
!! the ErrorCriteria derived type.
subroutine modifyErrorCriterionCodeByName(this, name, newCode)
class(ErrorCriteria) :: this !! This ErrorCriteria instance
character(len=*), intent(in) :: name !! Function name to change the code for
integer, intent(in) :: newCode !! New error code
integer :: index ! The array index corresponding to the function name
! Stop if we haven't initialised the error handler
call this%stopIfNotInitialised
! Get the index of the named error criterion in the error criteria array,
! then use the modifyErrorCriterionCodeByIndex method to modify
index = this%getIndexFromCriterionName(name)
if (index == 0) then
error stop "Error modifying error criterion code by name: Name doesn't match any criteria functions."
end if
call this%modifyErrorCriterionCodeByIndex(index, newCode)
end subroutine
!> Get the error code from the criterion function name
function getCodeFromCriterionName(this, name) result(code)
class(ErrorCriteria), intent(in) :: this !! This ErrorCriteria instance
character(len=*), intent(in) :: name !! Function name to get code for
integer :: code !! The error code
integer :: index ! The index corersponding to the function name
! Stop if we haven't initialised the error handler
call this%stopIfNotInitialised
! Get the index from the name, and use that to get the code
index = this%getIndexFromCriterionName(name)
if (lbound(this%currentErrorCriteriaCodes,1) <= index .and. ubound(this%currentErrorCriteriaCodes,1) >= index) then
code = this%currentErrorCriteriaCodes(index)
else
code = 0 ! Not found
end if
end function
!> Get the index of the error criteria array for a given criterion name
function getIndexFromCriterionName(this, name) result(index)
class(ErrorCriteria), intent(in) :: this !! This ErrorCriteria instance
character(len=*), intent(in) :: name !! Function name to get the index for
integer :: index !! Index for the given function name
integer :: i ! Loop iterator
! Stop if we haven't initialised the error handler
call this%stopIfNotInitialised
index = 0 ! Default in case criterion doesn't exist
! Loop through the criteria names
do i=1, size(this%currentErrorCriteriaNames)
if (name==this%currentErrorCriteriaNames(i)) index = i
end do
end function
!> Remove an ErrorInstance from the errors array, but check if
!! it is a code for an error criterion first. If it is, throw an error
!! message to that effect and suggest using modify() instead.
subroutine removeErrorInstanceCheckCriteria(this, code)
class(ErrorCriteria) :: this !! This ErrorCriteria instance
integer, intent(in) :: code !! The code to remove
integer :: i ! Loop iterator
! Stop if we haven't initialised the error handler
call this%stopIfNotInitialised
! Firstly, check if the code specified is an error criteria
! code, and if so, throw an error.
do i=1, size(this%currentErrorCriteriaCodes)
if (this%currentErrorCriteriaCodes(i) == code) then
write(*,'(a,i5,a)') "ERROR: Trying to remove error code that is used in error criteria: ", code, "."
write(*,'(a)') "Use modifyErrorCriterionCode() instead."
error stop 1
end if
end do
! If error isn't an error criteria, then remove using normal method
call this%ErrorHandler%remove(code)
end subroutine
!> Remove multiple ErrorInstances from the errors array, but check if
!! any of the codes are for an error criterion first. If they are, throw an error
!! message to that effect and suggest using modify() instead.
subroutine removeMultipleErrorInstancesCheckCriteria(this, codes)
class(ErrorCriteria) :: this !! This ErrorCriteria instance
integer, intent(in) :: codes(:) !! The array of codes to remove
integer :: i ! Loop iterator
! Loop through the codes and try and remove one-by-one.
! removeErrorInstance checks if initialised.
do i=1, size(codes)
call this%removeErrorInstance(codes(i))
end do
end subroutine
!> Add a new error criterion to the list of current error criteria and
!! list of errors. A separate criterion function(s) must accompany this
!! new criterion.
subroutine addErrorCriterion(this, code, name, message, isCritical)
class(ErrorCriteria) :: this !! This ErrorCriteria instance
integer, intent(in) :: code !! Error code for the new criterion
character(len=*) :: name !! Name of this criterion
character(len=*) :: message !! Error message for this criterion
logical :: isCritical !! If triggered, is it critical?
! Add the error to the errors array
call this%ErrorHandler%add(&
code = code, &
message = message, &
isCritical = isCritical &
)
! Reallocate the current error criteria to include the newly added one
deallocate(this%currentErrorCriteriaCodes)
allocate(this%currentErrorCriteriaCodes, source=[this%defaultErrorCriteriaCodes,code])
deallocate(this%currentErrorCriteriaNames)
allocate(this%currentErrorCriteriaNames, source=[this%defaultErrorCriteriaNames,name])
deallocate(this%currentErrorCriteriaMessages)
allocate(this%currentErrorCriteriaMessages, source=[this%defaultErrorCriteriaMessages,message])
deallocate(this%currentErrorCriteriaIsCritical)
allocate(this%currentErrorCriteriaIsCritical, source=[this%defaultErrorCriteriaIsCritical,isCritical])
end subroutine
!> Add an array of new error criteria to the list of current error criteria
!! and list of errors. Separate criteria functions must accompany these
!! new criteria.
subroutine addErrorCriteria(this, codes, names, messages, areCritical)
class(ErrorCriteria) :: this !! This ErrorCriteria instance
integer, intent(in) :: codes(:) !! Error codes for the new criteria
character(len=*) :: names(:) !! Names for the criteria
character(len=*) :: messages(:) !! Error messages
logical :: areCritical(:) !! Are the errors critical?
! Add the errors to the errors array
call this%ErrorHandler%add( &
codes = codes, &
messages = messages, &
areCritical = areCritical &
)
! Reallocate the current error criteria to include the newly added ones
deallocate(this%currentErrorCriteriaCodes)
allocate(this%currentErrorCriteriaCodes, source=[this%defaultErrorCriteriaCodes,codes])
deallocate(this%currentErrorCriteriaNames)
allocate(this%currentErrorCriteriaNames, source=[this%defaultErrorCriteriaNames,names])
deallocate(this%currentErrorCriteriaMessages)
allocate(this%currentErrorCriteriaMessages, source=[this%defaultErrorCriteriaMessages,messages])
deallocate(this%currentErrorCriteriaIsCritical)
allocate(this%currentErrorCriteriaIsCritical, source=[this%defaultErrorCriteriaIsCritical,areCritical])
end subroutine
!--------------------!
!-- CRITERIA TESTS --!
!--------------------!
!-- LIMIT --!
!> Test whether an integer value falls within a limit. If only upper
!! or lower bounds are specified, the value must be less than
!! or greater than the limit (respetively).
function integerLimit(this, value, lbound, ubound, message, traceMessage) result(error)
class(ErrorCriteria), intent(in) :: this !! The ErrorHandler class
integer, intent(in) :: value !! The value to test
integer, intent(in), optional :: lbound !! The lower bound of the limit
integer, intent(in), optional :: ubound !! The upper bound of the limit
character(len=*), intent(in), optional :: message !! Overwrite the standard error message
character(len=*), intent(in), optional :: traceMessage !! Message to display for error trace (if any)
type(ErrorInstance) :: error !! The error to return
logical :: pass ! Does the value pass the test?
character(len=100) :: charValue ! Character variable to store value in
character(len=100) :: charLbound ! Character variable to store lbound in
character(len=100) :: charUbound ! Character variable to store ubound in
! Stop the program running if ErrorHandler not initialised
call this%stopIfNotInitialised()
pass = .false.
! Check if value between limits, or if only one limit provided,
! check if value greater than/less than
if (present(lbound) .and. present(ubound)) then
! Stop if lbound greater than upper bound
if (lbound > ubound) error stop "Error criteria limit specified with lower bound greater than upper bound."
if (value >= lbound .and. value <= ubound) pass = .true.
else if (present(lbound)) then
if (value >= lbound) pass = .true.
else if (present(ubound)) then
if (value <= ubound) pass = .true.
end if
! If value doesn't pass test, get the error to return, compose the message
! and add specified point to trace. Else, return no error
if (.not. pass) then
error = this%getErrorFromCode(this%getCodeFromCriterionName('limit'))
write(charValue,*) value
! Customise the error message, based on whether user has provided a message
if (present(message)) then
error%message = message &
// " Given value: " // trim(adjustl(charValue)) // "."
else
if (present(lbound) .and. present(ubound)) then
write(charLbound,*) lbound
write(charUbound,*) ubound
error%message = "Value must be between " &
// trim(adjustl(charLbound)) // " and " // trim(adjustl(charUbound)) // ". " &
// "Given value: " // trim(adjustl(charValue)) // "."
! Message for greater than test
else if (present(lbound)) then
write(charLbound,*) lbound
error%message = "Value must be greater than " &
// trim(adjustl(charLbound)) // ". " &
// "Given value: " // trim(adjustl(charValue)) // "."
! Message for less than test
else if (present(ubound)) then
write(charUbound,*) ubound
error%message = "Value must be less than " &
// trim(adjustl(charUbound)) // ". " &
// "Given value: " // trim(adjustl(charValue)) // "."
end if
end if
! Add trace message
if (present(traceMessage)) call error%addToTrace(traceMessage)
else
error = this%getNoError()
end if
end function
!> Test whether a real value falls within a limit. If only upper
!! or lower bounds are specified, the value must be less than
!! or greater than the limit (respetively).
function realLimit(this, value, lbound, ubound, message, traceMessage) result(error)
class(ErrorCriteria), intent(in) :: this !! The ErrorHandler class
real, intent(in) :: value !! The value to test
real, intent(in), optional :: lbound !! The lower bound of the limit
real, intent(in), optional :: ubound !! The upper bound of the limit
character(len=*), intent(in), optional :: message !! Overwrite the standard error message
character(len=*), intent(in), optional :: traceMessage !! Message to display for error trace (if any)
type(ErrorInstance) :: error !! The error to return
logical :: pass ! Does the value pass the test?
character(len=100) :: charValue ! Character variable to store value in
character(len=100) :: charLbound ! Character variable to store lbound in
character(len=100) :: charUbound ! Character variable to store ubound in
! Stop the program running if ErrorHandler not initialised
call this%stopIfNotInitialised()
pass = .false.
! Check if value between limits, or if only one limit provided,
! check if value greater than/less than
if (present(lbound) .and. present(ubound)) then
! Stop if lbound greater than upper bound
if (lbound > ubound) error stop "Error criteria limit specified with lower bound greater than upper bound."
if (value >= lbound .and. value <= ubound) pass = .true.
else if (present(lbound)) then
if (value >= lbound) pass = .true.
else if (present(ubound)) then
if (value <= ubound) pass = .true.
end if
! If value doesn't pass test, get the error to return, compose the message
! and add specified point to trace. Else, return no error
if (.not. pass) then
error = this%getErrorFromCode(this%getCodeFromCriterionName('limit'))
write(charValue,*) value
! Customise the error message, based on whether user has provided a message
if (present(message)) then
error%message = message &
// " Given value: " // trim(adjustl(charValue)) // "."
else
if (present(lbound) .and. present(ubound)) then
write(charLbound,*) lbound
write(charUbound,*) ubound
error%message = "Value must be between " &
// trim(adjustl(charLbound)) // " and " // trim(adjustl(charUbound)) // ". " &
// "Given value: " // trim(adjustl(charValue)) // "."
! Message for greater than test
else if (present(lbound)) then
write(charLbound,*) lbound
error%message = "Value must be greater than " &
// trim(adjustl(charLbound)) // ". " &
// "Given value: " // trim(adjustl(charValue)) // "."
! Message for less than test
else if (present(ubound)) then
write(charUbound,*) ubound
error%message = "Value must be less than " &
// trim(adjustl(charUbound)) // ". " &
// "Given value: " // trim(adjustl(charValue)) // "."
end if
end if
if (present(traceMessage)) call error%addToTrace(traceMessage)
else
error = this%getNoError()
end if
end function
!> Test whether a real(dp) value falls within a limit. If only upper
!! or lower bounds are specified, the value must be less than
!! or greater than the limit (respetively).
function realDPLimit(this, value, lbound, ubound, message, traceMessage) result(error)
class(ErrorCriteria), intent(in) :: this !! The ErrorHandler class
real(dp), intent(in) :: value !! The value to test
real(dp), intent(in), optional :: lbound !! The lower bound of the limit
real(dp), intent(in), optional :: ubound !! The upper bound of the limit
character(len=*), intent(in), optional :: message !! Overwrite the standard error message
character(len=*), intent(in), optional :: traceMessage !! Message to display for error trace (if any)
type(ErrorInstance) :: error !! The error to return
logical :: pass ! Does the value pass the test?
character(len=100) :: charValue ! Character variable to store value in
character(len=100) :: charLbound ! Character variable to store lbound in
character(len=100) :: charUbound ! Character variable to store ubound in
! Stop the program running if ErrorHandler not initialised
call this%stopIfNotInitialised()
pass = .false.
! Check if value between limits, or if only one limit provided,
! check if value greater than/less than
if (present(lbound) .and. present(ubound)) then
! Stop if lbound greater than upper bound
if (lbound > ubound) error stop "Error criteria limit specified with lower bound greater than upper bound."
if (value >= lbound .and. value <= ubound) pass = .true.
else if (present(lbound)) then
if (value >= lbound) pass = .true.
else if (present(ubound)) then
if (value <= ubound) pass = .true.
end if
! If value doesn't pass test, get the error to return, compose the message
! and add specified point to trace. Else, return no error
if (.not. pass) then
error = this%getErrorFromCode(this%getCodeFromCriterionName('limit'))
write(charValue,*) value
! Customise the error message, based on whether user has provided a message
if (present(message)) then
error%message = message &
// " Given value: " // trim(adjustl(charValue)) // "."
else
if (present(lbound) .and. present(ubound)) then
write(charLbound,*) lbound
write(charUbound,*) ubound
error%message = "Value must be between " &
// trim(adjustl(charLbound)) // " and " // trim(adjustl(charUbound)) // ". " &
// "Given value: " // trim(adjustl(charValue)) // "."
! Message for greater than test
else if (present(lbound)) then
write(charLbound,*) lbound
error%message = "Value must be greater than " &
// trim(adjustl(charLbound)) // ". " &
// "Given value: " // trim(adjustl(charValue)) // "."
! Message for less than test
else if (present(ubound)) then
write(charUbound,*) ubound
error%message = "Value must be less than " &
// trim(adjustl(charUbound)) // ". " &
// "Given value: " // trim(adjustl(charValue)) // "."
end if
end if
if (present(traceMessage)) call error%addToTrace(traceMessage)
else
error = this%getNoError()
end if
end function
!> Test whether a real(qp) value falls within a limit. If only upper
!! or lower bounds are specified, the value must be less than
!! or greater than the limit (respetively).
function realQPLimit(this, value, lbound, ubound, message, traceMessage) result(error)
class(ErrorCriteria), intent(in) :: this !! The ErrorHandler class
real(qp), intent(in) :: value !! The value to test
real(qp), intent(in), optional :: lbound !! The lower bound of the limit
real(qp), intent(in), optional :: ubound !! The upper bound of the limit
character(len=*), intent(in), optional :: message !! Overwrite the standard error message
character(len=*), intent(in), optional :: traceMessage !! Message to display for error trace (if any)
type(ErrorInstance) :: error !! The error to return
logical :: pass ! Does the value pass the test?
character(len=100) :: charValue ! Character variable to store value in
character(len=100) :: charLbound ! Character variable to store lbound in
character(len=100) :: charUbound ! Character variable to store ubound in
! Stop the program running if ErrorHandler not initialised
call this%stopIfNotInitialised()
pass = .false.
! Check if value between limits, or if only one limit provided,
! check if value greater than/less than
if (present(lbound) .and. present(ubound)) then
! Stop if lbound greater than upper bound
if (lbound > ubound) error stop "Error criteria limit specified with lower bound greater than upper bound."
if (value >= lbound .and. value <= ubound) pass = .true.
else if (present(lbound)) then
if (value >= lbound) pass = .true.
else if (present(ubound)) then
if (value <= ubound) pass = .true.
end if
! If value doesn't pass test, get the error to return, compose the message
! and add specified point to trace. Else, return no error
if (.not. pass) then
error = this%getErrorFromCode(this%getCodeFromCriterionName('limit'))
write(charValue,*) value
! Customise the error message, based on whether user has provided a message
if (present(message)) then
error%message = message &
// " Given value: " // trim(adjustl(charValue)) // "."
else
if (present(lbound) .and. present(ubound)) then
write(charLbound,*) lbound
write(charUbound,*) ubound
error%message = "Value must be between " &
// trim(adjustl(charLbound)) // " and " // trim(adjustl(charUbound)) // ". " &
// "Given value: " // trim(adjustl(charValue)) // "."
! Message for greater than test
else if (present(lbound)) then
write(charLbound,*) lbound
error%message = "Value must be greater than " &
// trim(adjustl(charLbound)) // ". " &
// "Given value: " // trim(adjustl(charValue)) // "."
! Message for less than test
else if (present(ubound)) then
write(charUbound,*) ubound
error%message = "Value must be less than " &
// trim(adjustl(charUbound)) // ". " &
// "Given value: " // trim(adjustl(charValue)) // "."
end if
end if
if (present(traceMessage)) call error%addToTrace(traceMessage)
else
error = this%getNoError()
end if
end function
!-- NON-ZERO --!
!> Test whether an integer is non-zero.
function integerNonZero(this, value, epsilon, message, traceMessage) result(error)
class(ErrorCriteria), intent(in) :: this !! The ErrorHandler class
integer, intent(in) :: value !! The value to test
real, optional, intent(in) :: epsilon !! How close to zero can the value be?
character(len=*), optional, intent(in) :: message !! Overwrite the standard error message
character(len=*), intent(in), optional :: traceMessage !! Message to display for error trace (if any)
type(ErrorInstance) :: error !! The error to return
logical :: pass ! Does the value pass the test?
character(len=100) :: charValue ! Character variable to store value in
! Stop the program running if ErrorHandler not initialised
call this%stopIfNotInitialised()
pass = .false.
! Check if value not equal to 0
if (value /= 0) pass = .true.
! If value doesn't pass test, get the error to return, compose the message
! and add specified point to trace. Else, return no error
if (.not. pass) then
error = this%getErrorFromCode(this%getCodeFromCriterionName('nonZero')) ! Get the non-zero error
write(charValue,*) value
! Customise the error message, based on whether user has provided a message
if (present(message)) then
error%message = message &
// " Given value: " // trim(adjustl(charValue)) // "."
else
error%message = "Value must be non-zero. " &
// "Given value: " // trim(adjustl(charValue)) // "."
end if
if (present(traceMessage)) call error%addToTrace(traceMessage)
else
error = this%getNoError()
end if
end function
!> Test whether a real number is non-zero, or further than a specified
!! amount (epsilon) from zero.
function realNonZero(this, value, epsilon, message, traceMessage) result(error)
class(ErrorCriteria), intent(in) :: this !! The ErrorHandler class
real, intent(in) :: value !! The value to test
real, optional, intent(in) :: epsilon !! How close to zero can the value be?
character(len=*), optional, intent(in) :: message !! Overwrite the standard error message
character(len=*), intent(in), optional :: traceMessage !! Message to display for error trace (if any)
type(ErrorInstance) :: error !! The error to return
logical :: pass ! Does the value pass the test?
character(len=100) :: charValue ! Character variable to store value in
! Stop the program running if ErrorHandler not initialised
call this%stopIfNotInitialised()
pass = .true.
! Check if the value is outside of 0 +- epsilon.
if (present(epsilon)) then
if (abs(value) <= epsilon) pass = .false.
else
if (abs(value) <= this%epsilon) pass = .false.
end if
! If value doesn't pass test, get the error to return, compose the message
! and add specified point to trace. Else, return no error
if (.not. pass) then
error = this%getErrorFromCode(this%getCodeFromCriterionName('nonZero')) ! Get the non-zero error
write(charValue,*) value
! Customise the error message, based on whether user has provided a message
if (present(message)) then
error%message = message &
// " Given value: " // trim(adjustl(charValue)) // "."
else
error%message = "Value must be non-zero. " &
// "Given value: " // trim(adjustl(charValue)) // "."
end if
if (present(traceMessage)) call error%addToTrace(traceMessage)
else
error = this%getNoError()
end if
end function
!> Test whether a real(dp) number is non-zero, or further than a specified
!! amount (epsilon) from zero.
function realDPNonZero(this, value, epsilon, message, traceMessage) result(error)
class(ErrorCriteria), intent(in) :: this !! The ErrorHandler class
real(dp), intent(in) :: value !! The value to test
real, optional, intent(in) :: epsilon !! How close to zero can the value be?
character(len=*), optional, intent(in) :: message !! Overwrite the standard error message
character(len=*), intent(in), optional :: traceMessage !! Message to display for error trace (if any)
type(ErrorInstance) :: error !! The error to return
logical :: pass ! Does the value pass the test?
character(len=100) :: charValue ! Character variable to store value in
! Stop the program running if ErrorHandler not initialised
call this%stopIfNotInitialised()
pass = .true.
! Check if the value is outside of 0 +- epsilon.
if (present(epsilon)) then
if (abs(value) <= epsilon) pass = .false.
else
if (abs(value) <= this%epsilon) pass = .false.
end if
! If value doesn't pass test, get the error to return, compose the message
! and add specified point to trace. Else, return no error
if (.not. pass) then
error = this%getErrorFromCode(this%getCodeFromCriterionName('nonZero')) ! Get the non-zero error
write(charValue,*) value
! Customise the error message, based on whether user has provided a message
if (present(message)) then
error%message = message &
// " Given value: " // trim(adjustl(charValue)) // "."
else
error%message = "Value must be non-zero. " &
// "Given value: " // trim(adjustl(charValue)) // "."
end if
if (present(traceMessage)) call error%addToTrace(traceMessage)
else
error = this%getNoError()
end if
end function
!> Test whether a real(qp) number is non-zero, or further than a specified
!! amount (epsilon) from zero.
function realQPNonZero(this, value, epsilon, message, traceMessage) result(error)
class(ErrorCriteria), intent(in) :: this !! The ErrorHandler class
real(qp), intent(in) :: value !! The value to test
real, optional, intent(in) :: epsilon !! How close to zero can the value be?
character(len=*), optional, intent(in) :: message !! Overwrite the standard error message
character(len=*), intent(in), optional :: traceMessage !! Message to display for error trace (if any)
type(ErrorInstance) :: error !! The error to return
logical :: pass ! Does the value pass the test?
character(len=100) :: charValue ! Character variable to store value in
! Stop the program running if ErrorHandler not initialised
call this%stopIfNotInitialised()
pass = .true.
! Check if the value is outside of 0 +- epsilon.
if (present(epsilon)) then
if (abs(value) <= epsilon) pass = .false.
else
if (abs(value) <= this%epsilon) pass = .false.
end if
! If value doesn't pass test, get the error to return, compose the message
! and add specified point to trace. Else, return no error
if (.not. pass) then
error = this%getErrorFromCode(this%getCodeFromCriterionName('nonZero'))
write(charValue,*) value
! Customise the error message, based on whether user has provided a message
if (present(message)) then
error%message = message &
// " Given value: " // trim(adjustl(charValue)) // "."
else
error%message = "Value must be non-zero. " &
// "Given value: " // trim(adjustl(charValue)) // "."
end if
if (present(traceMessage)) call error%addToTrace(traceMessage)
else
error = this%getNoError()
end if
end function
!-- ZERO --!
!> Test whether an integer is zero.
function integerZero(this, value, epsilon, message, traceMessage) result(error)
class(ErrorCriteria), intent(in) :: this !! The ErrorHandler class
integer, intent(in) :: value !! The value to test
real, optional, intent(in) :: epsilon !! How far from zero can the value be?
character(len=*), optional, intent(in) :: message !! Overwrite the standard error message
character(len=*), intent(in), optional :: traceMessage !! Message to display for error trace (if any)
type(ErrorInstance) :: error !! The error to return
logical :: pass ! Does the value pass the test?
character(len=100) :: charValue ! Character variable to store value in
! Stop the program running if ErrorHandler not initialised
call this%stopIfNotInitialised()
pass = .false.
! Check if value is 0
if (value == 0) pass = .true.
! If value doesn't pass test, get the error to return, compose the message
! and add specified point to trace. Else, return no error
if (.not. pass) then
error = this%getErrorFromCode(this%getCodeFromCriterionName('zero'))
write(charValue,*) value
! Customise the error message, based on whether user has provided a message
if (present(message)) then
error%message = message &
// " Given value: " // trim(adjustl(charValue)) // "."
else
error%message = "Value must be zero. " &
// "Given value: " // trim(adjustl(charValue)) // "."
end if
if (present(traceMessage)) call error%addToTrace(traceMessage)
else
error = this%getNoError()
end if
end function
!> Test whether a real is within epsilon of zero.
function realZero(this, value, epsilon, message, traceMessage) result(error)
class(ErrorCriteria), intent(in) :: this !! The ErrorHandler class
real, intent(in) :: value !! The value to test
real, optional, intent(in) :: epsilon !! How far from zero can the value be?
character(len=*), optional, intent(in) :: message !! Overwrite the standard error message
character(len=*), intent(in), optional :: traceMessage !! Message to display for error trace (if any)
type(ErrorInstance) :: error !! The error to return
logical :: pass ! Does the value pass the test?
character(len=100) :: charValue ! Character variable to store value in
! Stop the program running if ErrorHandler not initialised
call this%stopIfNotInitialised()
pass = .true.
! Check if the value is outside of 0 +- epsilon
if (present(epsilon)) then
if (abs(value) >= epsilon) pass = .false.
else
if (abs(value) >= this%epsilon) pass = .false.
end if
! If value doesn't pass test, get the error to return, compose the message
! and add specified point to trace. Else, return no error
if (.not. pass) then
error = this%getErrorFromCode(this%getCodeFromCriterionName('zero'))
write(charValue,*) value
! Customise the error message, based on whether user has provided a message
if (present(message)) then
error%message = message &
// " Given value: " // trim(adjustl(charValue)) // "."
else
error%message = "Value must be zero. " &
// "Given value: " // trim(adjustl(charValue)) // "."
end if
if (present(traceMessage)) call error%addToTrace(traceMessage)
else
error = this%getNoError()
end if
end function
!> Test whether a real(dp) is within epsilon of zero.
function realDPZero(this, value, epsilon, message, traceMessage) result(error)
class(ErrorCriteria), intent(in) :: this !! The ErrorHandler class
real(dp), intent(in) :: value !! The value to test
real, optional, intent(in) :: epsilon !! How far from zero can the value be?
character(len=*), optional, intent(in) :: message !! Overwrite the standard error message
character(len=*), intent(in), optional :: traceMessage !! Message to display for error trace (if any)
type(ErrorInstance) :: error !! The error to return
logical :: pass ! Does the value pass the test?
character(len=100) :: charValue ! Character variable to store value in
! Stop the program running if ErrorHandler not initialised
call this%stopIfNotInitialised()
pass = .true.
! Check if the value is outside of 0 +- epsilon
if (present(epsilon)) then
if (abs(value) >= epsilon) pass = .false.
else
if (abs(value) >= this%epsilon) pass = .false.
end if
! If value doesn't pass test, get the error to return, compose the message
! and add specified point to trace. Else, return no error
if (.not. pass) then
error = this%getErrorFromCode(this%getCodeFromCriterionName('zero'))
write(charValue,*) value
! Customise the error message, based on whether user has provided a message
if (present(message)) then
error%message = message &
// " Given value: " // trim(adjustl(charValue)) // "."
else
error%message = "Value must be zero. " &
// "Given value: " // trim(adjustl(charValue)) // "."
end if
if (present(traceMessage)) call error%addToTrace(traceMessage)
else
error = this%getNoError()
end if
end function
!> Test whether a real(qp) is within epsilon of zero.
function realQPZero(this, value, epsilon, message, traceMessage) result(error)
class(ErrorCriteria), intent(in) :: this !! The ErrorHandler class
real(qp), intent(in) :: value !! The value to test
real, optional, intent(in) :: epsilon !! How far from zero can the value be?
character(len=*), optional, intent(in) :: message !! Overwrite the standard error message
character(len=*), intent(in), optional :: traceMessage !! Message to display for error trace (if any)
type(ErrorInstance) :: error !! The error to return
logical :: pass ! Does the value pass the test?
character(len=100) :: charValue ! Character variable to store value in