forked from johannesgerer/jburkardt-f
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathball_volume_quad.f90
1513 lines (1315 loc) · 32.8 KB
/
ball_volume_quad.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
program main
!*****************************************************************************80
!
!! MAIN is the main program for BALL_VOLUME_QUAD.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 03 October 2010
!
! Author:
!
! John Burkardt
!
implicit none
integer ( kind = 4 ) arg_num
integer ( kind = 4 ) dim
integer ( kind = 4 ) dim_num
integer ( kind = 4 ) dim_num2
real ( kind = 8 ) error
real ( kind = 8 ) estimate
real ( kind = 8 ) exact
real ( kind = 8 ), allocatable, dimension ( : ) :: fx
integer ( kind = 4 ) iarg
integer ( kind = 4 ) iargc
integer ( kind = 4 ) ierror
integer ( kind = 4 ) ios
integer ( kind = 4 ) last
integer ( kind = 4 ) point_num
integer ( kind = 4 ) point_num2
character ( len = 80 ) quad_filename
character ( len = 80 ) quad_r_filename
character ( len = 80 ) quad_w_filename
character ( len = 80 ) quad_x_filename
character ( len = 80 ) string
real ( kind = 8 ) volume
real ( kind = 8 ), allocatable, dimension ( : ) :: weight
real ( kind = 8 ), allocatable, dimension ( :, : ) :: x
real ( kind = 8 ), allocatable, dimension ( :, : ) :: x_range
call timestamp ( )
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'BALL_VOLUME_QUAD'
write ( *, '(a)' ) ' FORTRAN90 version'
write ( *, '(a)' ) ' Use a quadrature rule, applied to the M-dimensional'
write ( *, '(a)' ) ' hypercube, to estimate the volume of the '
write ( *, '(a)' ) ' M-dimensional unit sphere.'
write ( *, '(a)' ) ' '
write ( *, '(a)' ) ' We are integrating the function f(x), where f(x) is'
write ( *, '(a)' ) ' * 1 inside the sphere;'
write ( *, '(a)' ) ' * 0 outside the spere.'
write ( *, '(a)' ) ' '
write ( *, '(a)' ) ' The function f(x) is discontinuous. That suggests that'
write ( *, '(a)' ) ' as we use "better" and "better" quadrature rules, the'
write ( *, '(a)' ) ' volume estimate will actually get worse and worse.'
write ( *, '(a)' ) ' '
write ( *, '(a)' ) ' The user supplies the quadrature rule using three files,'
write ( *, '(a)' ) ' called the "R", "W" and "X" files:'
write ( *, '(a)' ) ' * the Mx2 R file contains the hyperrectangle of definition;'
write ( *, '(a)' ) ' * the Nx1 W file contains the weights.'
write ( *, '(a)' ) ' * the MxN X file contains the points.'
!
! Get the number of command line arguments.
!
arg_num = iargc ( )
!
! Get the quadrature file root name:
!
if ( 1 <= arg_num ) then
iarg = 1
call getarg ( iarg, quad_filename )
else
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'BALL_VOLUME_QUAD'
write ( *, '(a)' ) ' Enter the "root" name of the quadrature files.'
read ( *, '(a)' ) quad_filename
end if
!
! Create the names of:
! the quadrature X file;
! the quadrature W file;
! the quadrature R file;
!
quad_x_filename = trim ( quad_filename ) // '_x.txt'
quad_w_filename = trim ( quad_filename ) // '_w.txt'
quad_r_filename = trim ( quad_filename ) // '_r.txt'
!
! Summarize the input.
!
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'User input:'
write ( *, '(a)' ) ' Quadrature rule X file = "' &
// trim ( quad_x_filename ) // '".'
write ( *, '(a)' ) ' Quadrature rule W file = "' &
// trim ( quad_w_filename ) // '".'
write ( *, '(a)' ) ' Quadrature rule R file = "' &
// trim ( quad_r_filename ) // '".'
!
! Read the X file.
!
call r8mat_header_read ( quad_x_filename, dim_num, point_num )
write ( *, '(a)' ) ' '
write ( *, '(a,i8)' ) ' Spatial dimension = ', dim_num
write ( *, '(a,i8)' ) ' Number of points = ', point_num
allocate ( x(dim_num,point_num) )
call r8mat_data_read ( quad_x_filename, dim_num, point_num, x )
!
! Read the W file.
!
call r8mat_header_read ( quad_w_filename, dim_num2, point_num2 )
if ( dim_num2 /= 1 ) then
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'BALL_VOLUME_QUAD - Fatal error!'
write ( *, '(a)' ) ' The quadrature weight file should have exactly'
write ( *, '(a)' ) ' one value on each line.'
stop
end if
if ( point_num2 /= point_num ) then
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'BALL_VOLUME_QUAD - Fatal error!'
write ( *, '(a)' ) ' The quadrature weight file should have exactly'
write ( *, '(a)' ) ' the same number of lines as the abscissa file.'
stop
end if
allocate ( weight(point_num) )
call r8mat_data_read ( quad_w_filename, 1, point_num, weight )
!
! Read the R file.
!
call r8mat_header_read ( quad_r_filename, dim_num2, point_num2 )
if ( dim_num2 /= dim_num ) then
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'BALL_VOLUME_QUAD - Fatal error!'
write ( *, '(a)' ) ' The quadrature region file should have the'
write ( *, '(a)' ) ' same number of values on each line as the'
write ( *, '(a)' ) ' abscissa file does.'
stop
end if
if ( point_num2 /= 2 ) then
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'BALL_VOLUME_QUAD - Fatal error!'
write ( *, '(a)' ) ' The quadrature region file should have two lines.'
stop
end if
allocate ( x_range(dim_num,2) )
call r8mat_data_read ( quad_r_filename, dim_num, 2, x_range )
!
! Rescale the weights, and translate the abscissas.
!
volume = abs ( product ( x_range(1:dim_num,2) - x_range(1:dim_num,1) ) )
weight(1:point_num) = weight(1:point_num) / volume
do dim = 1, dim_num
x(dim,1:point_num) = ( x(dim,1:point_num) - x_range(dim,1) ) &
/ ( x_range(dim,2) - x_range(dim,1) )
end do
!
! Rescale X from [0,1] to [-1,1].
!
x(1:dim_num,1:point_num) = 2.0D+00 * x(1:dim_num,1:point_num) - 1.0D+00
volume = 2.0D+00**dim_num
weight(1:point_num) = weight(1:point_num) * volume
write ( *, '(a)' ) ' '
write ( *, '(a)' ) ' N Estimate Exact Error'
write ( *, '(a)' ) ' '
allocate ( fx(1:point_num) )
call sphere_indicator ( dim_num, point_num, x, fx )
estimate = dot_product ( weight, fx )
call sphere_unit_volume_nd ( dim_num, exact )
error = abs ( exact - estimate )
write ( *, '(2x,i8,2x,g14.6,2x,g14.6,2g14.6)' ) &
point_num, estimate, exact, error
deallocate ( fx )
deallocate ( weight )
deallocate ( x )
deallocate ( x_range )
!
! Terminate.
!
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'BALL_VOLUME_QUAD:'
write ( *, '(a)' ) ' Normal end of execution.'
write ( *, '(a)' ) ' '
call timestamp ( )
stop
end
subroutine ch_cap ( c )
!*****************************************************************************80
!
!! CH_CAP capitalizes a single character.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 19 July 1998
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Input/output, character C, the character to capitalize.
!
implicit none
character c
integer ( kind = 4 ) itemp
itemp = ichar ( c )
if ( 97 <= itemp .and. itemp <= 122 ) then
c = char ( itemp - 32 )
end if
return
end
function ch_eqi ( c1, c2 )
!*****************************************************************************80
!
!! CH_EQI is a case insensitive comparison of two characters for equality.
!
! Example:
!
! CH_EQI ( 'A', 'a' ) is .TRUE.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 28 July 2000
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Input, character C1, C2, the characters to compare.
!
! Output, logical CH_EQI, the result of the comparison.
!
implicit none
logical ch_eqi
character c1
character c1_cap
character c2
character c2_cap
c1_cap = c1
c2_cap = c2
call ch_cap ( c1_cap )
call ch_cap ( c2_cap )
if ( c1_cap == c2_cap ) then
ch_eqi = .true.
else
ch_eqi = .false.
end if
return
end
subroutine ch_to_digit ( c, digit )
!*****************************************************************************80
!
!! CH_TO_DIGIT returns the integer value of a base 10 digit.
!
! Example:
!
! C DIGIT
! --- -----
! '0' 0
! '1' 1
! ... ...
! '9' 9
! ' ' 0
! 'X' -1
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 04 August 1999
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Input, character C, the decimal digit, '0' through '9' or blank
! are legal.
!
! Output, integer ( kind = 4 ) DIGIT, the corresponding integer value.
! If C was 'illegal', then DIGIT is -1.
!
implicit none
character c
integer ( kind = 4 ) digit
if ( lge ( c, '0' ) .and. lle ( c, '9' ) ) then
digit = ichar ( c ) - 48
else if ( c == ' ' ) then
digit = 0
else
digit = -1
end if
return
end
subroutine file_column_count ( input_file_name, column_num )
!*****************************************************************************80
!
!! FILE_COLUMN_COUNT counts the number of columns in the first line of a file.
!
! Discussion:
!
! The file is assumed to be a simple text file.
!
! Most lines of the file is presumed to consist of COLUMN_NUM words,
! separated by spaces. There may also be some blank lines, and some
! comment lines,
! which have a "#" in column 1.
!
! The routine tries to find the first non-comment non-blank line and
! counts the number of words in that line.
!
! If all lines are blanks or comments, it goes back and tries to analyze
! a comment line.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 21 June 2001
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Input, character ( len = * ) INPUT_FILE_NAME, the name of the file.
!
! Output, integer ( kind = 4 ) COLUMN_NUM, the number of columns in the file.
!
implicit none
integer ( kind = 4 ) column_num
logical got_one
character ( len = * ) input_file_name
integer ( kind = 4 ) input_status
integer ( kind = 4 ) input_unit
character ( len = 1023 ) line
!
! Open the file.
!
call get_unit ( input_unit )
open ( unit = input_unit, file = input_file_name, status = 'old', &
form = 'formatted', access = 'sequential', iostat = input_status )
if ( input_status /= 0 ) then
column_num = -1
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'FILE_COLUMN_COUNT - Fatal error!'
write ( *, '(a,i8)' ) ' Could not open the input file "' &
// trim ( input_file_name ) // '" on unit ', input_unit
return
end if
!
! Read one line, but skip blank lines and comment lines.
!
got_one = .false.
do
read ( input_unit, '(a)', iostat = input_status ) line
if ( input_status /= 0 ) then
exit
end if
if ( len_trim ( line ) == 0 ) then
cycle
end if
if ( line(1:1) == '#' ) then
cycle
end if
got_one = .true.
exit
end do
if ( .not. got_one ) then
rewind ( input_unit )
do
read ( input_unit, '(a)', iostat = input_status ) line
if ( input_status /= 0 ) then
exit
end if
if ( len_trim ( line ) == 0 ) then
cycle
end if
got_one = .true.
exit
end do
end if
close ( unit = input_unit )
if ( .not. got_one ) then
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'FILE_COLUMN_COUNT - Warning!'
write ( *, '(a)' ) ' The file does not seem to contain any data.'
column_num = -1
return
end if
call s_word_count ( line, column_num )
return
end
subroutine file_row_count ( input_file_name, row_num )
!*****************************************************************************80
!
!! FILE_ROW_COUNT counts the number of row records in a file.
!
! Discussion:
!
! It does not count lines that are blank, or that begin with a
! comment symbol '#'.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 06 March 2003
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Input, character ( len = * ) INPUT_FILE_NAME, the name of the input file.
!
! Output, integer ( kind = 4 ) ROW_NUM, the number of rows found.
!
implicit none
integer ( kind = 4 ) bad_num
integer ( kind = 4 ) comment_num
integer ( kind = 4 ) ierror
character ( len = * ) input_file_name
integer ( kind = 4 ) input_status
integer ( kind = 4 ) input_unit
character ( len = 1023 ) line
integer ( kind = 4 ) record_num
integer ( kind = 4 ) row_num
call get_unit ( input_unit )
open ( unit = input_unit, file = input_file_name, status = 'old', &
iostat = input_status )
if ( input_status /= 0 ) then
row_num = -1;
ierror = 1
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'FILE_ROW_COUNT - Fatal error!'
write ( *, '(a,i8)' ) ' Could not open the input file "' // &
trim ( input_file_name ) // '" on unit ', input_unit
stop
end if
comment_num = 0
row_num = 0
record_num = 0
bad_num = 0
do
read ( input_unit, '(a)', iostat = input_status ) line
if ( input_status /= 0 ) then
ierror = record_num
exit
end if
record_num = record_num + 1
if ( line(1:1) == '#' ) then
comment_num = comment_num + 1
cycle
end if
if ( len_trim ( line ) == 0 ) then
comment_num = comment_num + 1
cycle
end if
row_num = row_num + 1
end do
close ( unit = input_unit )
return
end
subroutine get_unit ( iunit )
!*****************************************************************************80
!
!! GET_UNIT returns a free FORTRAN unit number.
!
! Discussion:
!
! A "free" FORTRAN unit number is an integer between 1 and 99 which
! is not currently associated with an I/O device. A free FORTRAN unit
! number is needed in order to open a file with the OPEN command.
!
! If IUNIT = 0, then no free FORTRAN unit could be found, although
! all 99 units were checked (except for units 5, 6 and 9, which
! are commonly reserved for console I/O).
!
! Otherwise, IUNIT is an integer between 1 and 99, representing a
! free FORTRAN unit. Note that GET_UNIT assumes that units 5 and 6
! are special, and will never return those values.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 18 September 2005
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Output, integer ( kind = 4 ) IUNIT, the free unit number.
!
implicit none
integer ( kind = 4 ) i
integer ( kind = 4 ) ios
integer ( kind = 4 ) iunit
logical lopen
iunit = 0
do i = 1, 99
if ( i /= 5 .and. i /= 6 .and. i /= 9 ) then
inquire ( unit = i, opened = lopen, iostat = ios )
if ( ios == 0 ) then
if ( .not. lopen ) then
iunit = i
return
end if
end if
end if
end do
return
end
subroutine r8mat_data_read ( input_filename, m, n, table )
!*****************************************************************************80
!
!! R8MAT_DATA_READ reads data from an R8MAT file.
!
! Discussion:
!
! An R8MAT is an array of R8 values.
!
! Discussion:
!
! The file may contain more than N points, but this routine will
! return after reading N of them.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 18 October 2008
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Input, character ( len = * ) INPUT_FILENAME, the name of the input file.
!
! Input, integer ( kind = 4 ) M, the spatial dimension.
!
! Input, integer ( kind = 4 ) N, the number of points.
!
! Output, real ( kind = 8 ) TABLE(M,N), the table data.
!
implicit none
integer ( kind = 4 ) m
integer ( kind = 4 ) n
integer ( kind = 4 ) ierror
character ( len = * ) input_filename
integer ( kind = 4 ) input_status
integer ( kind = 4 ) input_unit
integer ( kind = 4 ) j
character ( len = 255 ) line
real ( kind = 8 ) table(m,n)
real ( kind = 8 ) x(m)
ierror = 0
call get_unit ( input_unit )
open ( unit = input_unit, file = input_filename, status = 'old', &
iostat = input_status )
if ( input_status /= 0 ) then
ierror = 1
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'R8MAT_DATA_READ - Fatal error!'
write ( *, '(a,i8)' ) ' Could not open the input file "' // &
trim ( input_filename ) // '" on unit ', input_unit
stop
end if
j = 0
do while ( j < n )
read ( input_unit, '(a)', iostat = input_status ) line
if ( input_status /= 0 ) then
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'R8MAT_DATA_READ - Fatal error!'
write ( *, '(a)' ) ' Error while reading lines of data.'
write ( *, '(a,i8)' ) ' Number of values expected per line M = ', m
write ( *, '(a,i8)' ) ' Number of data lines read, J = ', j
write ( *, '(a,i8)' ) ' Number of data lines needed, N = ', n
stop
end if
if ( line(1:1) == '#' .or. len_trim ( line ) == 0 ) then
cycle
end if
call s_to_r8vec ( line, m, x, ierror )
if ( ierror /= 0 ) then
cycle
end if
j = j + 1
table(1:m,j) = x(1:m)
end do
close ( unit = input_unit )
return
end
subroutine r8mat_header_read ( input_filename, m, n )
!*****************************************************************************80
!
!! R8MAT_HEADER_READ reads the header from an R8MAT file.
!
! Discussion:
!
! An R8MAT is an array of R8 values.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 07 September 2004
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Input, character ( len = * ) INPUT_FILENAME, the name of the input file.
!
! Output, integer ( kind = 4 ) M, spatial dimension.
!
! Output, integer ( kind = 4 ) N, the number of points.
!
implicit none
character ( len = * ) input_filename
integer ( kind = 4 ) m
integer ( kind = 4 ) n
call file_column_count ( input_filename, m )
if ( m <= 0 ) then
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'R8MAT_HEADER_READ - Fatal error!'
write ( *, '(a)' ) ' There was some kind of I/O problem while trying'
write ( *, '(a)' ) ' to count the number of data columns in'
write ( *, '(a)' ) ' the file "' // trim ( input_filename ) // '".'
stop
end if
call file_row_count ( input_filename, n )
if ( n <= 0 ) then
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'R8MAT_HEADER_READ - Fatal error!'
write ( *, '(a)' ) ' There was some kind of I/O problem while trying'
write ( *, '(a)' ) ' to count the number of data rows in'
write ( *, '(a)' ) ' the file "' // trim ( input_filename ) // '".'
stop
end if
return
end
subroutine s_to_i4 ( s, ival, ierror, length )
!*****************************************************************************80
!
!! S_TO_I4 reads an I4 from a string.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 28 June 2000
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Input, character ( len = * ) S, a string to be examined.
!
! Output, integer ( kind = 4 ) IVAL, the integer value read from the string.
! If the string is blank, then IVAL will be returned 0.
!
! Output, integer ( kind = 4 ) IERROR, an error flag.
! 0, no error.
! 1, an error occurred.
!
! Output, integer ( kind = 4 ) LENGTH, the number of characters of S
! used to make IVAL.
!
implicit none
character c
integer ( kind = 4 ) i
integer ( kind = 4 ) ierror
integer ( kind = 4 ) isgn
integer ( kind = 4 ) istate
integer ( kind = 4 ) ival
integer ( kind = 4 ) length
character ( len = * ) s
ierror = 0
istate = 0
isgn = 1
ival = 0
do i = 1, len_trim ( s )
c = s(i:i)
!
! Haven't read anything.
!
if ( istate == 0 ) then
if ( c == ' ' ) then
else if ( c == '-' ) then
istate = 1
isgn = -1
else if ( c == '+' ) then
istate = 1
isgn = + 1
else if ( lle ( '0', c ) .and. lle ( c, '9' ) ) then
istate = 2
ival = ichar ( c ) - ichar ( '0' )
else
ierror = 1
return
end if
!
! Have read the sign, expecting digits.
!
else if ( istate == 1 ) then
if ( c == ' ' ) then
else if ( lle ( '0', c ) .and. lle ( c, '9' ) ) then
istate = 2
ival = ichar ( c ) - ichar ( '0' )
else
ierror = 1
return
end if
!
! Have read at least one digit, expecting more.
!
else if ( istate == 2 ) then
if ( lle ( '0', c ) .and. lle ( c, '9' ) ) then
ival = 10 * ival + ichar ( c ) - ichar ( '0' )
else
ival = isgn * ival
length = i - 1
return
end if
end if
end do
!
! If we read all the characters in the string, see if we're OK.
!
if ( istate == 2 ) then
ival = isgn * ival
length = len_trim ( s )
else
ierror = 1
length = 0
end if
return
end
subroutine s_to_r8 ( s, dval, ierror, length )
!*****************************************************************************80
!
!! S_TO_R8 reads an R8 from a string.
!
! Discussion:
!
! The routine will read as many characters as possible until it reaches
! the end of the string, or encounters a character which cannot be
! part of the number.
!
! Legal input is:
!
! 1 blanks,
! 2 '+' or '-' sign,
! 2.5 blanks
! 3 integer part,
! 4 decimal point,
! 5 fraction part,
! 6 'E' or 'e' or 'D' or 'd', exponent marker,
! 7 exponent sign,
! 8 exponent integer part,
! 9 exponent decimal point,
! 10 exponent fraction part,
! 11 blanks,
! 12 final comma or semicolon,
!
! with most quantities optional.
!
! Example:
!
! S DVAL
!
! '1' 1.0
! ' 1 ' 1.0
! '1A' 1.0
! '12,34,56' 12.0
! ' 34 7' 34.0
! '-1E2ABCD' -100.0
! '-1X2ABCD' -1.0
! ' 2E-1' 0.2
! '23.45' 23.45
! '-4.2E+2' -420.0
! '17d2' 1700.0
! '-14e-2' -0.14
! 'e2' 100.0
! '-12.73e-9.23' -12.73 * 10.0**(-9.23)
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 07 September 2004
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Input, character ( len = * ) S, the string containing the
! data to be read. Reading will begin at position 1 and
! terminate at the end of the string, or when no more
! characters can be read to form a legal real. Blanks,
! commas, or other nonnumeric data will, in particular,
! cause the conversion to halt.
!
! Output, real ( kind = 8 ) DVAL, the value read from the string.
!
! Output, integer ( kind = 4 ) IERROR, error flag.
! 0, no errors occurred.
! 1, 2, 6 or 7, the input number was garbled. The
! value of IERROR is the last type of input successfully
! read. For instance, 1 means initial blanks, 2 means
! a plus or minus sign, and so on.
!
! Output, integer ( kind = 4 ) LENGTH, the number of characters read
! to form the number, including any terminating
! characters such as a trailing comma or blanks.
!
implicit none
logical ch_eqi