-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathgrid_dataset.f90
1116 lines (993 loc) · 25.2 KB
/
grid_dataset.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 GRID_DATASET.
!
! Discussion:
!
! GRID_DATASET generates a grid dataset and writes it to a file.
!
! Interesting features of this problem are the determination
! of the side of a grid that will generate "about" N points,
! the method of dropping the extra points at random, and the
! ability to center the grid inside the unit hypercube in a
! number of ways.
!
! Usage:
!
! grid_dataset ( m, n, seed, center )
!
! where
!
! * M, the spatial dimension,
! * N, the number of points to generate,
! * SEED, the seed, a positive integer.
! * CENTER, the grid centering option.
! 1: 0/( N-1) ... ( N-1)/( N-1)
! 2: 1/( N+1) ... N /( N+1)
! 3: 0/ N ... ( N-1)/ N
! 4: 1/ N ... N / N
! 5: 1/(2*N) ... (2*N-1)/(2*N )
!
! The program generates the data, writes it to the file
!
! grid_M_N_CENTER.txt
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 12 December 2009
!
! Author:
!
! John Burkardt
!
implicit none
integer ( kind = 4 ) arg_num
integer ( kind = 4 ) center
integer ( kind = 4 ) iarg
integer ( kind = 4 ) iargc
integer ( kind = 4 ) ierror
integer ( kind = 4 ) ios
integer ( kind = 4 ) last
integer ( kind = 4 ) m
integer ( kind = 4 ) n
character ( len = 255 ) output_filename
real ( kind = 8 ), allocatable :: r(:,:)
integer ( kind = 4 ) seed
character ( len = 255 ) string
call timestamp ( )
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'GRID_DATASET'
write ( *, '(a)' ) ' FORTRAN90 version'
write ( *, '(a)' ) ' Generate a grid dataset.'
!
! Get the number of command line arguments.
!
arg_num = iargc ( )
!
! Get the spatial dimension M.
!
if ( 1 <= arg_num ) then
iarg = 1
call getarg ( iarg, string )
call s_to_i4 ( string, m, ierror, last )
else
write ( *, '(a)' ) ' '
write ( *, '(a)' ) ' Enter the spatial dimension M (1 or greater)'
read ( *, * ) m
end if
write ( *, '(a)' ) ' '
write ( *, '(a,i8)' ) ' Spatial dimension M = ', m
!
! Get the number of points N.
!
if ( 2 <= arg_num ) then
iarg = 2
call getarg ( iarg, string )
call s_to_i4 ( string, n, ierror, last )
else
write ( *, '(a)' ) ' '
write ( *, '(a)' ) ' Enter the number of points N (1 or greater)'
read ( *, * ) n
end if
write ( *, '(a,i8)' ) ' Number of points N = ', n
!
! Get the seed, SEED
!
if ( 3 <= arg_num ) then
iarg = 3
call getarg ( iarg, string )
call s_to_i4 ( string, seed, ierror, last )
else
write ( *, '(a)' ) ' '
write ( *, '(a)' ) ' Enter the seed SEED (1 or greater)'
read ( *, * ) seed
end if
write ( *, '(a,i12)' ) ' SEED = ', seed
if ( seed == 0 ) then
call get_seed ( seed )
write ( *, '(a)' ) ' '
write ( *, '(a,i12)' ) ' Chosen value of SEED = ', seed
end if
!
! Get CENTER.
!
if ( 4 <= arg_num ) then
iarg = 4
call getarg ( iarg, string )
call s_to_i4 ( string, center, ierror, last )
else
write ( *, '(a)' ) ' '
write ( *, '(a)' ) ' Enter CENTER, the grid centering option.'
write ( *, '(a)' ) ' Normal values are between 1 and 5:'
write ( *, '(a)' ) ' 1: 0/( N-1) ... ( N-1)/( N-1)'
write ( *, '(a)' ) ' 2: 1/( N+1) ... N /( N+1)'
write ( *, '(a)' ) ' 3: 0/ N ... ( N-1)/ N'
write ( *, '(a)' ) ' 4: 1/ N ... N / N'
write ( *, '(a)' ) ' 5: 1/(2*N) ... (2*N-1)/(2*N )'
read ( *, *, iostat = ios ) center
end if
write ( *, '(a,i8)' ) ' CENTER = ', center
!
! Compute the data.
!
allocate ( r(1:m,1:n) )
call grid_generate ( m, n, center, seed, r )
!
! Write the data to a file.
!
write ( output_filename, '(a,i2.2,a,i5.5,a,i1,a)' ) &
'grid_', m, '_', n, '_', center, '.txt'
call r8mat_write ( output_filename, m, n, r )
write ( *, '(a)' ) ' '
write ( *, '(a)' ) ' The grid data was written to the file "' &
// trim ( output_filename ) // '".'
!
! Free memory.
!
deallocate ( r )
!
! Terminate.
!
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'GRID_DATASET'
write ( *, '(a)' ) ' Normal end of execution.'
write ( *, '(a)' ) ' '
call timestamp ( )
stop
end
subroutine get_seed ( seed )
!*****************************************************************************80
!
!! GET_SEED returns a seed for the random number generator.
!
! Discussion:
!
! The seed depends on the current time, and ought to be (slightly)
! different every millisecond. Once the seed is obtained, a random
! number generator should be called a few times to further process
! the seed.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 02 August 2004
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Output, integer ( kind = 4 ) SEED, a pseudorandom seed value.
!
implicit none
integer ( kind = 4 ) seed
real ( kind = 8 ) temp
character ( len = 10 ) time
character ( len = 8 ) today
integer ( kind = 4 ) values(8)
character ( len = 5 ) zone
call date_and_time ( today, time, zone, values )
temp = 0.0D+00
temp = temp + real ( values(2) - 1, kind = 8 ) / 11.0D+00
temp = temp + real ( values(3) - 1, kind = 8 ) / 30.0D+00
temp = temp + real ( values(5), kind = 8 ) / 23.0D+00
temp = temp + real ( values(6), kind = 8 ) / 59.0D+00
temp = temp + real ( values(7), kind = 8 ) / 59.0D+00
temp = temp + real ( values(8), kind = 8 ) / 999.0D+00
temp = temp / 6.0D+00
do while ( temp <= 0.0D+00 )
temp = temp + 1.0D+00
end do
do while ( 1.0D+00 < temp )
temp = temp - 1.0D+00
end do
seed = int ( real ( huge ( 1 ), kind = 8 ) * temp )
!
! Never use a seed of 0 or maximum integer.
!
if ( seed == 0 ) then
seed = 1
end if
if ( seed == huge ( 1 ) ) then
seed = seed - 1
end if
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 grid_generate ( dim_num, n, center, seed, r )
!*****************************************************************************80
!
!! GRID_GENERATE generates a grid dataset.
!
! Discussion:
!
! N points are needed in a DIM_NUM-dimensional space.
!
! The points are to lie on a uniform grid of side N_SIDE.
!
! Unless the N = N_SIDE**DIM_NUM for some N_SIDE, we can't use all the
! points on a grid. What we do is find the smallest N_SIDE
! that's big enough, and randomly omit some points.
!
! If N_SIDE is 4, then the choices in 1D are:
!
! A: 0, 1/3, 2/3, 1
! B: 1/5, 2/5, 3/5, 4/5
! C: 0, 1/4, 2/4, 3/4
! D: 1/4, 2/4, 3/4, 1
! E: 1/8, 3/8, 5/8, 7/8
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 17 May 2003
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Input, integer ( kind = 4 ) DIM_NUM, the spatial dimension.
!
! Input, integer ( kind = 4 ) N, the number of points to generate.
!
! Input, integer ( kind = 4 ) CENTER, specifies the 1D grid centering:
! 1: first point is 0.0, last point is 1.0;
! 2: first point is 1/(N+1), last point is N/(N+1);
! 3: first point is 0, last point is (N-1)/N;
! 4: first point is 1/N, last point is 1;
! 5: first point is 1/(2*N), last point is (2*N-1)/(2*N);
!
! Input/output, integer ( kind = 4 ) SEED, the random number seed.
!
! Output, real ( kind = 8 ) R(DIM_NUM,N), the points.
!
implicit none
integer ( kind = 4 ) dim_num
integer ( kind = 4 ) n
integer ( kind = 4 ) center
integer ( kind = 4 ) j
integer ( kind = 4 ) n_grid
integer ( kind = 4 ) n_side
real ( kind = 8 ), dimension ( dim_num, n ) :: r
integer ( kind = 4 ) rank
integer ( kind = 4 ) rank_list(n)
integer ( kind = 4 ) seed
integer ( kind = 4 ) tuple(dim_num)
!
! Find the dimension of the smallest grid with N points.
!
call grid_side ( dim_num, n, n_side )
!
! We need to select N points out of N_SIDE**DIM_NUM set.
!
n_grid = n_side**dim_num
!
! Generate a random subset of N items from a set of size N_GRID.
!
call ksub_random2 ( n_grid, n, seed, rank_list )
!
! Must make one dummy call to TUPLE_NEXT_FAST with RANK = 0.
!
rank = 0
call tuple_next_fast ( n_side, dim_num, rank, tuple )
!
! Now generate the appropriate indices, and "center" them.
!
do j = 1, n
rank = rank_list(j) - 1
call tuple_next_fast ( n_side, dim_num, rank, tuple )
if ( center == 1 ) then
r(1:dim_num,j) = real ( tuple(1:dim_num) - 1, kind = 8 ) &
/ real ( n_side - 1, kind = 8 )
else if ( center == 2 ) then
r(1:dim_num,j) = real ( tuple(1:dim_num), kind = 8 ) &
/ real ( n_side + 1, kind = 8 )
else if ( center == 3 ) then
r(1:dim_num,j) = real ( tuple(1:dim_num) - 1, kind = 8 ) &
/ real ( n_side, kind = 8 )
else if ( center == 4 ) then
r(1:dim_num,j) = real ( tuple(1:dim_num), kind = 8 ) &
/ real ( n_side, kind = 8 )
else if ( center == 5 ) then
r(1:dim_num,j) = real ( 2 * tuple(1:dim_num) - 1, kind = 8 ) &
/ real ( 2 * n_side, kind = 8 )
end if
end do
return
end
subroutine grid_side ( dim_num, n, n_side )
!*****************************************************************************80
!
!! GRID_SIDE finds the smallest grid containing at least N points.
!
! Discussion:
!
! Each coordinate of the grid will have N_SIDE distinct values.
! Thus the total number of points in the grid is N_SIDE**DIM_NUM.
! This routine seeks the smallest N_SIDE such that N <= N_SIDE**DIM_NUM.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 16 May 2003
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Input, integer ( kind = 4 ) DIM_NUM, the spatial dimension.
!
! Input, integer ( kind = 4 ) N, the number of points to generate.
!
! Output, integer ( kind = 4 ) N_SIDE, the length of one side of the smallest
! grid in DIM_NUM dimensions that contains at least N points.
!
implicit none
integer ( kind = 4 ) dim_num
real ( kind = 8 ) exponent
integer ( kind = 4 ) n
integer ( kind = 4 ) n_side
if ( n <= 0 ) then
n_side = 0
return
end if
if ( dim_num <= 0 ) then
n_side = -1
return
end if
exponent = 1.0D+00 / real ( dim_num, kind = 8 )
n_side = int ( ( real ( n, kind = 8 ) )**exponent )
if ( n_side**dim_num < n ) then
n_side = n_side + 1
end if
return
end
function i4_huge ( )
!*****************************************************************************80
!
!! I4_HUGE returns a "huge" I4.
!
! Discussion:
!
! On an IEEE 32 bit machine, I4_HUGE should be 2**31 - 1, and its
! bit pattern should be
!
! 01111111111111111111111111111111
!
! In this case, its numerical value is 2147483647.
!
! Using the Dec/Compaq/HP Alpha FORTRAN compiler FORT, I could
! use I4_HUGE() and HUGE interchangeably.
!
! However, when using the G95, the values returned by HUGE were
! not equal to 2147483647, apparently, and were causing severe
! and obscure errors in my random number generator, which needs to
! add I4_HUGE to the seed whenever the seed is negative. So I
! am backing away from invoking HUGE, whereas I4_HUGE is under
! my control.
!
! Explanation: because under G95 the default integer type is 64 bits!
! So HUGE ( 1 ) = a very very huge integer indeed, whereas
! I4_HUGE ( ) = the same old 32 bit big value.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 26 January 2007
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Output, integer ( kind = 4 ) I4_HUGE, a "huge" I4.
!
implicit none
integer ( kind = 4 ) i4
integer ( kind = 4 ) i4_huge
i4_huge = 2147483647
return
end
subroutine ksub_random2 ( n, k, seed, a )
!*****************************************************************************80
!
!! KSUB_RANDOM2 selects a random subset of size K from a set of size N.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 30 April 2003
!
! Author:
!
! Original FORTRAN77 version by Albert Nijenhuis, Herbert Wilf.
! FORTRAN90 version by John Burkardt.
!
! Reference:
!
! A Nijenhuis and H Wilf,
! Combinatorial Algorithms,
! Academic Press, 1978, second edition,
! ISBN 0-12-519260-6.
!
! Parameters:
!
! Input, integer ( kind = 4 ) N, the size of the set.
!
! Input, integer ( kind = 4 ) K, the size of the subset, between 0 and N.
!
! Input/output, integer ( kind = 4 ) SEED, a seed for the random
! number generator.
!
! Output, integer ( kind = 4 ) A(K), the indices of the selected elements.
!
implicit none
integer ( kind = 4 ) k
integer ( kind = 4 ) a(k)
integer ( kind = 4 ) available
integer ( kind = 4 ) candidate
real ( kind = 8 ) r8_uniform_01
integer ( kind = 4 ) have
integer ( kind = 4 ) n
integer ( kind = 4 ) need
real ( kind = 8 ) r
integer ( kind = 4 ) seed
if ( k < 0 .or. n < k ) then
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'KSUB_RANDOM2 - Fatal error!'
write ( *, '(a,i8)' ) ' N = ', n
write ( *, '(a,i8)' ) ' K = ', k
write ( *, '(a)' ) ' but 0 <= K <= N is required!'
stop
end if
if ( k == 0 ) then
return
end if
need = k
have = 0
available = n
candidate = 0
do
candidate = candidate + 1
r = r8_uniform_01 ( seed )
if ( real ( available, kind = 8 ) * r <= real ( need, kind = 8 ) ) then
need = need - 1
have = have + 1
a(have) = candidate
if ( need <= 0 ) then
exit
end if
end if
available = available - 1
end do
return
end
function r8_uniform_01 ( seed )
!*****************************************************************************80
!
!! R8_UNIFORM_01 returns a unit pseudorandom R8.
!
! Discussion:
!
! An R8 is a real ( kind = 8 ) value.
!
! For now, the input quantity SEED is an integer ( kind = 4 ) variable.
!
! This routine implements the recursion
!
! seed = 16807 * seed mod ( 2**31 - 1 )
! r8_uniform_01 = seed / ( 2**31 - 1 )
!
! The integer arithmetic never requires more than 32 bits,
! including a sign bit.
!
! If the initial seed is 12345, then the first three computations are
!
! Input Output R8_UNIFORM_01
! SEED SEED
!
! 12345 207482415 0.096616
! 207482415 1790989824 0.833995
! 1790989824 2035175616 0.947702
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 05 July 2006
!
! Author:
!
! John Burkardt
!
! Reference:
!
! Paul Bratley, Bennett Fox, Linus Schrage,
! A Guide to Simulation,
! Springer Verlag, pages 201-202, 1983.
!
! Pierre L'Ecuyer,
! Random Number Generation,
! in Handbook of Simulation,
! edited by Jerry Banks,
! Wiley Interscience, page 95, 1998.
!
! Bennett Fox,
! Algorithm 647:
! Implementation and Relative Efficiency of Quasirandom
! Sequence Generators,
! ACM Transactions on Mathematical Software,
! Volume 12, Number 4, pages 362-376, 1986.
!
! Peter Lewis, Allen Goodman, James Miller
! A Pseudo-Random Number Generator for the System/360,
! IBM Systems Journal,
! Volume 8, pages 136-143, 1969.
!
! Parameters:
!
! Input/output, integer ( kind = 4 ) SEED, the "seed" value, which should
! NOT be 0. On output, SEED has been updated.
!
! Output, real ( kind = 8 ) R8_UNIFORM_01, a new pseudorandom variate,
! strictly between 0 and 1.
!
implicit none
integer ( kind = 4 ) k
real ( kind = 8 ) r8_uniform_01
integer ( kind = 4 ) seed
if ( seed == 0 ) then
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'R8_UNIFORM_01 - Fatal error!'
write ( *, '(a)' ) ' Input value of SEED = 0.'
stop
end if
k = seed / 127773
seed = 16807 * ( seed - k * 127773 ) - k * 2836
if ( seed < 0 ) then
seed = seed + 2147483647
end if
!
! Although SEED can be represented exactly as a 32 bit integer,
! it generally cannot be represented exactly as a 32 bit real number!
!
r8_uniform_01 = real ( seed, kind = 8 ) * 4.656612875D-10
return
end
subroutine r8mat_write ( output_filename, m, n, table )
!*****************************************************************************80
!
!! R8MAT_WRITE writes an R8MAT file.
!
! Discussion:
!
! An R8MAT is an array of R8 values.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 31 May 2009
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Input, character ( len = * ) OUTPUT_FILENAME, the output file name.
!
! Input, integer ( kind = 4 ) M, the spatial dimension.
!
! Input, integer ( kind = 4 ) N, the number of points.
!
! Input, real ( kind = 8 ) TABLE(M,N), the table data.
!
implicit none
integer ( kind = 4 ) m
integer ( kind = 4 ) n
integer ( kind = 4 ) j
character ( len = * ) output_filename
integer ( kind = 4 ) output_status
integer ( kind = 4 ) output_unit
character ( len = 30 ) string
real ( kind = 8 ) table(m,n)
!
! Open the file.
!
call get_unit ( output_unit )
open ( unit = output_unit, file = output_filename, &
status = 'replace', iostat = output_status )
if ( output_status /= 0 ) then
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'R8MAT_WRITE - Fatal error!'
write ( *, '(a,i8)' ) ' Could not open the output file "' // &
trim ( output_filename ) // '" on unit ', output_unit
output_unit = -1
stop
end if
!
! Create a format string.
!
! For less precision in the output file, try:
!
! '(', m, 'g', 14, '.', 6, ')'
!
if ( 0 < m .and. 0 < n ) then
write ( string, '(a1,i8,a1,i8,a1,i8,a1)' ) '(', m, 'g', 24, '.', 16, ')'
!
! Write the data.
!
do j = 1, n
write ( output_unit, string ) table(1:m,j)
end do
end if
!
! Close the file.
!
close ( unit = output_unit )
return
end
subroutine s_to_i4 ( s, value, ierror, length )
!*****************************************************************************80
!
!! S_TO_I4 reads an integer value from a string.
!
! Discussion:
!
! Instead of ICHAR, we now use the IACHAR function, which
! guarantees the ASCII collating sequence.
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 12 January 2009
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! Input, character ( len = * ) S, a string to be examined.
!
! Output, integer ( kind = 4 ) VALUE, the integer value read from the string.
! If the string is blank, then VALUE 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 the integer.
!
implicit none
character c
integer ( kind = 4 ) i
integer ( kind = 4 ) ierror
integer ( kind = 4 ) isgn
integer ( kind = 4 ) length
character ( len = * ) s
integer ( kind = 4 ) state
character :: TAB = achar ( 9 )
integer ( kind = 4 ) value
value = 0
ierror = 0
length = 0
state = 0
isgn = 1
do i = 1, len_trim ( s )
c = s(i:i)
!
! STATE = 0, haven't read anything.
!
if ( state == 0 ) then
if ( c == ' ' .or. c == TAB ) then
else if ( c == '-' ) then
state = 1
isgn = -1
else if ( c == '+' ) then
state = 1
isgn = +1
else if ( lle ( '0', c ) .and. lle ( c, '9' ) ) then
state = 2
value = iachar ( c ) - iachar ( '0' )
else
ierror = 1
return
end if
!
! STATE = 1, have read the sign, expecting digits or spaces.
!
else if ( state == 1 ) then
if ( c == ' ' .or. c == TAB ) then
else if ( lle ( '0', c ) .and. lle ( c, '9' ) ) then
state = 2
value = iachar ( c ) - iachar ( '0' )
else
ierror = 1
return
end if
!
! STATE = 2, have read at least one digit, expecting more.
!
else if ( state == 2 ) then
if ( lle ( '0', c ) .and. lle ( c, '9' ) ) then
value = 10 * value + iachar ( c ) - iachar ( '0' )
else
value = isgn * value
ierror = 0
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 ( state == 2 ) then
value = isgn * value
ierror = 0
length = len_trim ( s )
else
value = 0
ierror = 1
length = 0
end if
return
end
subroutine timestamp ( )
!*****************************************************************************80
!
!! TIMESTAMP prints the current YMDHMS date as a time stamp.
!
! Example:
!
! 31 May 2001 9:45:54.872 AM
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 06 August 2005
!
! Author:
!
! John Burkardt
!
! Parameters:
!
! None
!
implicit none
character ( len = 8 ) ampm
integer ( kind = 4 ) d
integer ( kind = 4 ) h
integer ( kind = 4 ) m
integer ( kind = 4 ) mm
character ( len = 9 ), parameter, dimension(12) :: month = (/ &
'January ', 'February ', 'March ', 'April ', &
'May ', 'June ', 'July ', 'August ', &
'September', 'October ', 'November ', 'December ' /)
integer ( kind = 4 ) n
integer ( kind = 4 ) s
integer ( kind = 4 ) values(8)
integer ( kind = 4 ) y
call date_and_time ( values = values )
y = values(1)
m = values(2)
d = values(3)
h = values(5)