forked from dcordiercnrs/pcsaft-titan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
foul.f90
1705 lines (1286 loc) · 55.8 KB
/
foul.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
!------------------------------------------------------------
! foul - The Fortran Output Library
!------------------------------------------------------------
! Provides routines enabling Fortran programs to:
!
! - Write formatted console output using ANSI escape codes
! (see http://en.wikipedia.org/wiki/ANSI_escape_code
! for more information)
! - Convert numbers to strings in a variety of
! finely-controlled ways, including fully trimmed
! of whitespace characters
! - Time program execution and other processes with the
! highest accuracy provided by the system
!------------------------------------------------------------
! Copyright (C) 2010-2011 by Philipp Emanuel Weidmann
! E-Mail: [email protected]
!------------------------------------------------------------
! This library 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;
! version 3 of the License.
!
! This library 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.
!------------------------------------------------------------
! Version history:
!
! 0.7 (18/02/2011): - Added real_to_string_scientific_short,
! a function for formatting numbers in
! classic Fortran scientific notation
! - Added iif_* routines emulating the
! behaviour of C++'s iif
! - Added handling of NaN values to
! real_to_string_* functions
! - Fixed a rare bug where rounding
! changes the number of digits before
! the decimal seperator in
! real_to_string
!
! 0.6 (02/07/2010): - Added support for multiple timers
! that can be used concurrently
! - Improved modularization
! - Moved module foul_helpers to the
! start of the code. This is in
! compliance with the Fortran standard
! and should fix compatibility issues
! with older versions of GFortran
! (reported by Arjen Markus)
!
! 0.5 (08/06/2010): - Added start_timer, stop_timer,
! reset_timer, get_timer_count,
! functions for measuring execution
! times
! - Added formatting for section headers
! and footers (experimental)
! - Fixed a bug in get_escape_sequence
! that prevented certain style strings
! from being interpreted correctly
! - The output unit is now a global
! variable, making it easy to write
! to units other than the console
!
! 0.4 (06/06/2010): - Major rewrite of write_formatted,
! fixing all known bugs related to
! string length and whitespace issues
! and finally introducing
! FULL GFORTRAN COMPATIBILITY
! (Note: This breaks compatibility
! with calls made to previous
! versions of write_formatted)
! - Minor improvements and fixes in
! real_to_string_scientific
!
! 0.3 (04/06/2010): - Added real_to_string and
! real_to_string_scientific, powerful
! functions for formatting real values
! - Fixed an important bug in
! integer_to_string that would sometimes
! cause it to return an empty string
! - Other minor improvements and fixes
!
! 0.2 (01/06/2010): - Assumed-shape arrays used in
! write_formatted, eliminating the need
! to supply the array size
! (Note: This breaks compatibility
! with calls made to previous
! versions of write_formatted)
! - Reduced redundancy by introducing
! integer_to_string
!
! 0.1 (30/05/2010): - Initial release
! - Note: DOES NOT WORK WITH GFORTRAN YET;
! IFC is the only compiler
! confirmed to compile the example
! (string array length issue)
!------------------------------------------------------------
MODULE foul_helpers
CONTAINS
! TODO: Turn into FUNCTION?
!------------------------------------------------------------
! SUBROUTINE get_escape_sequence
!
! Helper function
! Generates an ANSI escape sequence from the
! supplied style string
!------------------------------------------------------------
! Input:
! style_string: String describing which styles to set
! (separated by space);
! see source code for supported styles
!
! Output:
! escape_sequence: ANSI escape sequence generated from
! the specified styles
!------------------------------------------------------------
SUBROUTINE get_escape_sequence(style_string, escape_sequence)
IMPLICIT NONE
CHARACTER (LEN = *), INTENT(IN) :: style_string
CHARACTER * 16, INTENT(OUT) :: escape_sequence
INTEGER :: i
CHARACTER * 32 :: style_substrings(16)
INTEGER :: style_substring_count
! Start sequence with command to clear any previous attributes
escape_sequence = CHAR(27) // '[0'
CALL split_string(TRIM(style_string), ' ', style_substrings, style_substring_count)
DO i = 1, style_substring_count
CALL lower_case(style_substrings(i))
SELECT CASE (TRIM(style_substrings(i)))
CASE ('bright')
escape_sequence = TRIM(escape_sequence) // ';1'
CASE ('faint')
escape_sequence = TRIM(escape_sequence) // ';2'
CASE ('italic')
escape_sequence = TRIM(escape_sequence) // ';3'
CASE ('underline')
escape_sequence = TRIM(escape_sequence) // ';4'
CASE ('blink_slow')
escape_sequence = TRIM(escape_sequence) // ';5'
CASE ('blink_fast')
escape_sequence = TRIM(escape_sequence) // ';6'
CASE ('black')
escape_sequence = TRIM(escape_sequence) // ';30'
CASE ('red')
escape_sequence = TRIM(escape_sequence) // ';31'
CASE ('green')
escape_sequence = TRIM(escape_sequence) // ';32'
CASE ('yellow')
escape_sequence = TRIM(escape_sequence) // ';33'
CASE ('blue')
escape_sequence = TRIM(escape_sequence) // ';34'
CASE ('magenta')
escape_sequence = TRIM(escape_sequence) // ';35'
CASE ('cyan')
escape_sequence = TRIM(escape_sequence) // ';36'
CASE ('white')
escape_sequence = TRIM(escape_sequence) // ';37'
CASE ('background_black')
escape_sequence = TRIM(escape_sequence) // ';40'
CASE ('background_red')
escape_sequence = TRIM(escape_sequence) // ';41'
CASE ('background_green')
escape_sequence = TRIM(escape_sequence) // ';42'
CASE ('background_yellow')
escape_sequence = TRIM(escape_sequence) // ';43'
CASE ('background_blue')
escape_sequence = TRIM(escape_sequence) // ';44'
CASE ('background_magenta')
escape_sequence = TRIM(escape_sequence) // ';45'
CASE ('background_cyan')
escape_sequence = TRIM(escape_sequence) // ';46'
CASE ('background_white')
escape_sequence = TRIM(escape_sequence) // ';47'
END SELECT
END DO
! Append end of sequence marker
escape_sequence = TRIM(escape_sequence) // 'm'
RETURN
END SUBROUTINE get_escape_sequence
!------------------------------------------------------------
! SUBROUTINE split_string
!
! Helper function
! Splits the supplied string along a delimiter
!------------------------------------------------------------
! Input:
! string: Variable-length character string that is
! to be split
! delimiter: Character along which to split
!
! Output:
! substrings: Array of substrings generated by split
! operation
! substring_count: Number of substrings generated
!------------------------------------------------------------
SUBROUTINE split_string(string, delimiter, substrings, substring_count)
IMPLICIT NONE
CHARACTER (LEN = *), INTENT(IN) :: string
CHARACTER, INTENT(IN) :: delimiter
CHARACTER (LEN = *), INTENT(OUT) :: substrings(*)
INTEGER, INTENT(OUT) :: substring_count
INTEGER :: start_position, end_position
start_position = 1
substring_count = 0
DO
end_position = INDEX(string(start_position:), delimiter)
substring_count = substring_count + 1
IF (end_position == 0) THEN
substrings(substring_count) = string(start_position:)
EXIT
ELSE
substrings(substring_count) = string(start_position : start_position + end_position - 2)
start_position = start_position + end_position
END IF
END DO
RETURN
END SUBROUTINE split_string
! TODO: Turn into FUNCTION
!------------------------------------------------------------
! SUBROUTINE lower_case
!
! Helper function
! Converts the supplied string to lower case
!------------------------------------------------------------
! Input:
! string: Variable-length character string that is
! to be converted
!
! Output:
! string: String converted to lower case; conversion
! is done in place
!------------------------------------------------------------
SUBROUTINE lower_case(string)
IMPLICIT NONE
CHARACTER (LEN = *), INTENT(INOUT) :: string
INTEGER :: i, character_code
DO i = 1, LEN_TRIM(string)
character_code = ICHAR(string(i : i))
IF (character_code >= 65 .AND. character_code <= 90) THEN
string(i : i) = CHAR(character_code + 32)
END IF
END DO
RETURN
END SUBROUTINE lower_case
!------------------------------------------------------------
! FUNCTION integer_to_string
!
! Converts the supplied integer to a character string
!------------------------------------------------------------
! Input:
! integer_value: Value to be converted
! string_length: Minimal length of resulting string;
! result will be right-adjusted if desired
! length is greater than actual length
!
! Output:
! Return value: String resulting from conversion
!------------------------------------------------------------
FUNCTION integer_to_string(integer_value, string_length)
IMPLICIT NONE
INTEGER, INTENT(IN) :: integer_value, string_length
! 1 + FLOOR(LOG10(REAL(ABS(integer_value)))) =
! number of digits needed to represent integer_value
!
! INT(SIGN(1, -integer_value) + 1) / 2) =
! 0, if integer_value is positive
! 1, if integer_value is negative
! (used to make room for sign)
CHARACTER (LEN = MAX(1 + FLOOR(LOG10(REAL(ABS(integer_value)))) + &
INT((SIGN(1, -integer_value) + 1) / 2), &
string_length, 1)) :: integer_to_string
CHARACTER * 16 :: string_buffer
WRITE(string_buffer, '(I16)') integer_value
string_buffer = ADJUSTL(string_buffer)
IF (LEN_TRIM(string_buffer) < LEN(integer_to_string)) THEN
integer_to_string = REPEAT(' ', LEN(integer_to_string) - LEN_TRIM(string_buffer)) // &
TRIM(string_buffer)
ELSE
integer_to_string = TRIM(string_buffer)
END IF
RETURN
END FUNCTION integer_to_string
!------------------------------------------------------------
! FUNCTION real_to_string
!
! Converts the supplied real to a character string
!------------------------------------------------------------
! Input:
! real_value: Value to be converted
! integer_length: Minimal length of the integral part in
! the resulting string; result will be
! right-adjusted if desired length is
! greater than actual length
! fractional_length: Length of the fractional part
! in the resulting string
!
! Output:
! Return value: String resulting from conversion
!------------------------------------------------------------
FUNCTION real_to_string(real_value, integer_length, fractional_length)
IMPLICIT NONE
REAL * 8, INTENT(IN) :: real_value
INTEGER, INTENT(IN) :: integer_length, fractional_length
! 1 + FLOOR(LOG10(ABS(real_value))) =
! number of digits needed to represent integral part of real_value
!
! INT(SIGN(1.0D0, -real_value) + 1) / 2) =
! 0, if real_value is positive
! 1, if real_value is negative
! (used to make room for sign)
CHARACTER (LEN = MAX(1 + FLOOR(LOG10(ABS(real_value))) + &
INT((SIGN(1.0D0, -real_value) + 1) / 2), &
integer_length, 1) + 1 + fractional_length) :: real_to_string
INTEGER :: digit_count
CHARACTER * 16 :: format_string
CHARACTER * 32 :: string_buffer
IF (ISNAN(real_value)) THEN
real_to_string = REPEAT(' ', LEN(real_to_string) - 3) // 'NaN'
RETURN
END IF
IF (real_value == 0.0D0) THEN
! Filter out zero to prevent additional digit (for sign)
! from being introduced (because SIGN(1.0D0, -0.0D0) = 1.0D0)
digit_count = 1
ELSE IF (ABS(real_value) < 1) THEN
! Using the logarithm to determine number of digits
! doesn't work for values < 1
digit_count = INT(((SIGN(1.0D0, -real_value) + 1) / 2)) + 1
ELSE
digit_count = INT(((SIGN(1.0D0, -real_value) + 1) / 2)) + 1 + &
FLOOR(LOG10(ABS(real_value)))
END IF
format_string = '(F' // integer_to_string(digit_count + 1 + fractional_length, 0) // &
'.' // integer_to_string(fractional_length, 0) // ')'
! Truncation is neccessary in order to catch a rare bug
! where rounding would change the numer of digits in front
! of the decimal separator
! WRITE(string_buffer, TRIM(format_string)) DBLE(INT(real_value * (10.0D0**fractional_length))) / &
! (10.0D0**fractional_length)
WRITE(string_buffer, TRIM(format_string)) real_value
string_buffer = ADJUSTL(string_buffer)
IF (LEN_TRIM(string_buffer) < LEN(real_to_string)) THEN
real_to_string = REPEAT(' ', LEN(real_to_string) - LEN_TRIM(string_buffer)) // &
TRIM(string_buffer)
ELSE
real_to_string = TRIM(string_buffer)
END IF
RETURN
END FUNCTION real_to_string
!------------------------------------------------------------
! FUNCTION real_to_string_scientific
!
! Converts the supplied real to a character string
! in scientific notation
!------------------------------------------------------------
! Input:
! real_value: Value to be converted
! integer_length: Minimal length of the integral part in
! the resulting string; result will be
! right-adjusted if desired length is
! greater than actual length
! fractional_length: Length of the fractional part
! in the resulting string
! exponent_length: Minimal length of the exponent part
! in the resulting string
!
! Output:
! Return value: String resulting from conversion
!------------------------------------------------------------
FUNCTION real_to_string_scientific(real_value, integer_length, fractional_length, exponent_length)
IMPLICIT NONE
REAL * 8, INTENT(IN) :: real_value
INTEGER, INTENT(IN) :: integer_length, fractional_length, exponent_length
! TODO: Correct exponent length!!!
CHARACTER (LEN = MAX(1 + INT((SIGN(1.0D0, -real_value) + 1) / 2), integer_length) + &
1 + fractional_length + 7 + exponent_length + 1) :: real_to_string_scientific
INTEGER :: exponent
REAL * 8 :: coefficient
IF (ISNAN(real_value)) THEN
real_to_string_scientific = REPEAT(' ', LEN(real_to_string_scientific) - 3) // 'NaN'
RETURN
END IF
IF (ABS(real_value) == 0.0D0) THEN
! No exponent neccessary
exponent = 0
ELSE
exponent = FLOOR(LOG10(ABS(real_value)))
END IF
coefficient = real_value / (10.0D0 ** REAL(exponent))
IF (exponent == 0) THEN
real_to_string_scientific = real_to_string(coefficient, integer_length, fractional_length) // &
REPEAT(' ', 7 + exponent_length + 1)
ELSE
real_to_string_scientific = real_to_string(coefficient, integer_length, fractional_length) // &
' x (10^' // integer_to_string(exponent, exponent_length) // ')'
END IF
RETURN
END FUNCTION real_to_string_scientific
!------------------------------------------------------------
! FUNCTION real_to_string_scientific_short
!
! Converts the supplied real to a character string
! in scientific notation using "pocket calculator"
! short form (e.g. "3.2E6")
!------------------------------------------------------------
! Input:
! real_value: Value to be converted
! integer_length: Minimal length of the integral part in
! the resulting string; result will be
! right-adjusted if desired length is
! greater than actual length
! fractional_length: Length of the fractional part
! in the resulting string
! exponent_length: Minimal length of the exponent part
! in the resulting string
!
! Output:
! Return value: String resulting from conversion
!------------------------------------------------------------
FUNCTION real_to_string_scientific_short(real_value, integer_length, &
fractional_length, exponent_length)
IMPLICIT NONE
REAL * 8, INTENT(IN) :: real_value
INTEGER, INTENT(IN) :: integer_length, fractional_length, exponent_length
! TODO: Correct exponent length!!!
CHARACTER (LEN = MAX(1 + INT((SIGN(1.0D0, -real_value) + 1) / 2), integer_length) + &
1 + fractional_length + 1 + exponent_length) :: real_to_string_scientific_short
INTEGER :: exponent
REAL * 8 :: coefficient
CHARACTER (LEN = exponent_length) :: exponent_string
IF (ISNAN(real_value)) THEN
real_to_string_scientific_short = REPEAT(' ', LEN(real_to_string_scientific_short) - 3) // 'NaN'
RETURN
END IF
IF (ABS(real_value) == 0.0D0) THEN
! No exponent neccessary
exponent = 0
ELSE
exponent = FLOOR(LOG10(ABS(real_value)))
END IF
coefficient = real_value / (10.0D0 ** REAL(exponent))
IF (exponent == 0) THEN
real_to_string_scientific_short = REPEAT(' ', 1 + exponent_length) // &
real_to_string(coefficient, integer_length, fractional_length)
ELSE
IF (exponent < 0) THEN
exponent_string = integer_to_string(-exponent, 0)
exponent_string = '-' // REPEAT('0', exponent_length - LEN_TRIM(exponent_string) - 1) // &
exponent_string
ELSE
exponent_string = integer_to_string(exponent, 0)
exponent_string = REPEAT('0', exponent_length - LEN_TRIM(exponent_string)) // &
exponent_string
END IF
real_to_string_scientific_short = real_to_string(coefficient, integer_length, fractional_length) // &
'E' // exponent_string
END IF
RETURN
END FUNCTION real_to_string_scientific_short
END MODULE foul_helpers
!------------------------------------------------------------
MODULE foul_iif
INTERFACE iif
MODULE PROCEDURE iif_integer
MODULE PROCEDURE iif_real
MODULE PROCEDURE iif_string
END INTERFACE
CONTAINS
!------------------------------------------------------------
! FUNCTION iif_integer
!
! Returns one of two integer values depending on
! how an expression evaluates
!------------------------------------------------------------
! Input:
! expression: Boolean-valued expression to test for
! true_value: Value to return if expression evaluates
! to .TRUE.
! false_value: Value to return if expression evaluates
! to .FALSE.
!
! Output:
! Return value: true_value or false_value (see above)
!------------------------------------------------------------
INTEGER FUNCTION iif_integer(expression, true_value, false_value)
IMPLICIT NONE
LOGICAL, INTENT(IN) :: expression
INTEGER, INTENT(IN) :: true_value, false_value
IF (expression) THEN
iif_integer = true_value
ELSE
iif_integer = false_value
END IF
RETURN
END FUNCTION iif_integer
!------------------------------------------------------------
! FUNCTION iif_real
!
! Returns one of two real values depending on
! how an expression evaluates
!------------------------------------------------------------
! Input:
! expression: Boolean-valued expression to test for
! true_value: Value to return if expression evaluates
! to .TRUE.
! false_value: Value to return if expression evaluates
! to .FALSE.
!
! Output:
! Return value: true_value or false_value (see above)
!------------------------------------------------------------
REAL * 8 FUNCTION iif_real(expression, true_value, false_value)
IMPLICIT NONE
LOGICAL, INTENT(IN) :: expression
REAL * 8, INTENT(IN) :: true_value, false_value
IF (expression) THEN
iif_real = true_value
ELSE
iif_real = false_value
END IF
RETURN
END FUNCTION iif_real
!------------------------------------------------------------
! FUNCTION iif_string
!
! Returns one of two string values depending on
! how an expression evaluates
!------------------------------------------------------------
! Input:
! expression: Boolean-valued expression to test for
! true_value: Value to return if expression evaluates
! to .TRUE.
! false_value: Value to return if expression evaluates
! to .FALSE.
!
! Output:
! Return value: true_value or false_value (see above)
!------------------------------------------------------------
FUNCTION iif_string(expression, true_value, false_value)
IMPLICIT NONE
LOGICAL, INTENT(IN) :: expression
CHARACTER (LEN = *), INTENT(IN) :: true_value, false_value
CHARACTER (LEN = MAX(LEN(true_value), LEN(false_value))) :: iif_string
IF (expression) THEN
iif_string = true_value
ELSE
iif_string = false_value
END IF
RETURN
END FUNCTION iif_string
END MODULE foul_iif
!------------------------------------------------------------
MODULE foul_timing
IMPLICIT NONE
INTEGER, PARAMETER :: maximum_timers = 256
LOGICAL :: timer_running(maximum_timers) = .FALSE.
REAL :: timer_start_time(maximum_timers) = 0.0
REAL :: timer_count(maximum_timers) = 0.0
CONTAINS
!------------------------------------------------------------
! FUNCTION get_time
!
! Helper function
! Returns the most accurate time value
! available on the system
!------------------------------------------------------------
! Input:
! None
!
! Output:
! Return value: CPU / system time in seconds
!------------------------------------------------------------
REAL FUNCTION get_time()
IMPLICIT NONE
REAL :: processor_time
INTEGER :: system_time, cycles_per_second
! Supported by Fortran 95 and later
! according to the PGI Fortran Reference
CALL CPU_TIME(processor_time)
IF (processor_time == -1.0) THEN
! CPU time not available
! => try obtaining system time
CALL SYSTEM_CLOCK(system_time, cycles_per_second)
IF (cycles_per_second == 0) THEN
! System time not available
get_time = 0.0
ELSE
get_time = REAL(system_time) / REAL(cycles_per_second)
END IF
ELSE
get_time = processor_time
END IF
RETURN
END FUNCTION get_time
!------------------------------------------------------------
! SUBROUTINE start_timer
!
! (Re)starts a timer; if reset_timer wasn't
! called before, the count will resume from the value
! that the timer last stopped at
!------------------------------------------------------------
! Input:
! timer_id: ID of the timer to start
! (OPTIONAL; defaults to 1)
!
! Output:
! None
!------------------------------------------------------------
SUBROUTINE start_timer(timer_id)
IMPLICIT NONE
INTEGER, INTENT(IN), OPTIONAL :: timer_id
INTEGER :: timer_id_dummy
IF (PRESENT(timer_id)) THEN
timer_id_dummy = timer_id
ELSE
! Default to first timer
timer_id_dummy = 1
END IF
IF (.NOT. timer_running(timer_id_dummy)) THEN
timer_running(timer_id_dummy) = .TRUE.
timer_start_time(timer_id_dummy) = get_time()
END IF
RETURN
END SUBROUTINE start_timer
!------------------------------------------------------------
! SUBROUTINE stop_timer
!
! Stops a timer pending reset or restart
!------------------------------------------------------------
! Input:
! timer_id: ID of the timer to stop
! (OPTIONAL; defaults to 1)
!
! Output:
! None
!------------------------------------------------------------
SUBROUTINE stop_timer(timer_id)
IMPLICIT NONE
INTEGER, INTENT(IN), OPTIONAL :: timer_id
INTEGER :: timer_id_dummy
IF (PRESENT(timer_id)) THEN
timer_id_dummy = timer_id
ELSE
! Default to first timer
timer_id_dummy = 1
END IF
IF (timer_running(timer_id_dummy)) THEN
timer_running(timer_id_dummy) = .FALSE.
! Add time elapsed since timer start to timer count
timer_count(timer_id_dummy) = timer_count(timer_id_dummy) + &
(get_time() - timer_start_time(timer_id_dummy))
END IF
RETURN
END SUBROUTINE stop_timer
!------------------------------------------------------------
! SUBROUTINE reset_timer
!
! Resets the count of a timer
!------------------------------------------------------------
! Input:
! timer_id: ID of the timer to reset
! (OPTIONAL; defaults to 1)
!
! Output:
! None
!------------------------------------------------------------
SUBROUTINE reset_timer(timer_id)
IMPLICIT NONE
INTEGER, INTENT(IN), OPTIONAL :: timer_id
INTEGER :: timer_id_dummy
IF (PRESENT(timer_id)) THEN
timer_id_dummy = timer_id
ELSE
! Default to first timer
timer_id_dummy = 1
END IF
! Leaving timer running on reset makes no sense
! and has the potential to create problems
timer_running = .FALSE.
timer_start_time = 0.0
timer_count = 0.0
RETURN
END SUBROUTINE reset_timer
!------------------------------------------------------------
! FUNCTION get_timer_count
!
! Returns the current count of a timer
!------------------------------------------------------------
! Input:
! timer_id: ID of the timer to query
! (OPTIONAL; defaults to 1)
!
! Output:
! Return value: Timer count in seconds
!------------------------------------------------------------
REAL FUNCTION get_timer_count(timer_id)
IMPLICIT NONE
INTEGER, INTENT(IN), OPTIONAL :: timer_id
INTEGER :: timer_id_dummy
IF (PRESENT(timer_id)) THEN
timer_id_dummy = timer_id
ELSE
! Default to first timer
timer_id_dummy = 1
END IF
IF (timer_running(timer_id_dummy)) THEN
! Timer running => add time elapsed since timer
! start to returned timer count
get_timer_count = timer_count(timer_id_dummy) + &
(get_time() - timer_start_time(timer_id_dummy))
ELSE
get_timer_count = timer_count(timer_id_dummy)
END IF
RETURN
END FUNCTION get_timer_count
END MODULE foul_timing
!------------------------------------------------------------
MODULE foul
USE foul_helpers
USE foul_iif
USE foul_timing
IMPLICIT NONE
! If set to .FALSE., output will consist only of standard text,
! allowing the escape characters to be switched off in
! environments which don't support them
LOGICAL :: use_escape_codes = .TRUE.
! Width (in characters) of the section delimiters
! created by start_section and end_section
INTEGER :: section_width = 60
! Unit the output will be written to; the default (6)
! indicates the standard output unit, usually the console
INTEGER :: output_unit = 6
CONTAINS
!------------------------------------------------------------
! SUBROUTINE write_formatted
!
! Outputs the supplied strings on a single line,
! formatted using the supplied styles
!------------------------------------------------------------
! Input:
! text_1: First string to output
! style_1: String describing which styles to set when
! outputting text_1 (separated by space);
! see get_escape_sequence for supported styles
! [...] (up to 24 text strings supported)
!
! Output:
! None
!------------------------------------------------------------
SUBROUTINE write_formatted(text_1, style_1, text_2, style_2, text_3, style_3, &
text_4, style_4, text_5, style_5, text_6, style_6, &
text_7, style_7, text_8, style_8, text_9, style_9, &
text_10, style_10, text_11, style_11, text_12, style_12, &
text_13, style_13, text_14, style_14, text_15, style_15, &
text_16, style_16, text_17, style_17, text_18, style_18, &
text_19, style_19, text_20, style_20, text_21, style_21, &
text_22, style_22, text_23, style_23, text_24, style_24)
IMPLICIT NONE
! TODO: Decrease redundancy using preprocessing directives?
! Suggestions welcome!
CHARACTER (LEN = *), INTENT(IN), OPTIONAL :: &
text_1, style_1, text_2, style_2, text_3, style_3, &