-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathEC_grid.f90
2168 lines (1859 loc) · 75.7 KB
/
EC_grid.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
!===============================================================================
! One of Andy Nowacki's Fortran utility modules for dealing with seismic
! anisotropy and other problems.
!
! Andy Nowacki <[email protected]>
!
! See the file LICENCE for licence details.
!===============================================================================
module EC_grid
!==============================================================================
! Module containing routines for manipulating regularly-spaced grids of elastic
! constants. Defines a type for them and can do various things such as load
! these structures into memory, write out to and read from files.
!
! Andy Nowacki, University of Bristol
! 2011/02
!
! UPDATES:
! * 2011-03-16 Adding a type for grids with real coordinates and spacing.
! This will require a stricter file format, as the limits can't
! be determined just by reading the file and comparing
! coordinates (STILL TO DO).
! * 2011-07-26 Implemented and checked the binary EC_grid file functions
! EC_grid_load_bin and EC_grid_write_bin. These aren't very
! quick, so should consider C routines instead...
! * 2012-03-01 Begun implementation of new EC_grid types which include
! density. This breaks backward compatibility with old binary
! and ASCII grid files, but these can be converted fairly
! easily.
! * 2012-11-20 Begin testing of EC_gridr routines, with only binary files
! allowed at this stage. Introduce a 'nver' variable at start
! of binary files to distinguish from assumed-integer.
!-------------------------------------------------------------------------------
! FORTRAN BINARY FORMAT FILES
!
! These are written in one of two ways. The correct way is as follows:
! [1] nver,npts,nx,ny,nz,x1,x2,y1,y2,z1,z2,dx,dy,dz
! [2+] c11,c12...c16,c22...c66 (loops over z slowest, x fastest)
!
! nver is 1 for explicit integer coordinates, and 2 for real coordinates.
! (Real coordinates are the default.)
!
! If nver is not given (i.e., the first field of the first line is npts), then
! it is assumed that the binary file describes an assumed-integer coordinate frame.
! The format is:
! [1] npts,nx,ny,nz,ix1,ix2,iy1,iy2,iz1,iz2,idx,idy,idz
! [2+] (as above)
!
! This will be deprecated in the future.
!-------------------------------------------------------------------------------
implicit none
! ** size constants
integer, parameter, private :: i4 = selected_int_kind(9) ! long int
integer, parameter, private :: r4 = selected_real_kind(6,37) ! SP
integer, parameter, private :: r8 = selected_real_kind(15,307) ! DP
! ** precision selector
integer, parameter, private :: rs = r8
! ** maths constants and other useful things
real(rs), parameter, private :: pi = 3.141592653589793238462643_rs
integer, parameter, private :: big_p_integer = 2147483647
integer, parameter, private :: big_n_integer = -2147483647
! ** IO constants
integer, parameter, private :: lu_stdin = 5
integer, parameter, private :: lu_stdout = 6
integer, parameter, private :: lu_stderr = 0
! ** Version constants for explicit real and binary grid types (used internally)
integer, parameter, private :: nver_assumed_i = 0
integer, parameter, private :: nver_i = 1
integer, parameter, private :: nver_r = 2
! ** Version constants for external use
integer, parameter, public :: EC_grid_nver_assumed_i = nver_assumed_i
integer, parameter, public :: EC_grid_nver_i = nver_i
integer, parameter, public :: EC_grid_nver_r = nver_r
! ** Limit on maximum size of arrays: currently 2 GB
real, parameter, private :: MAX_GRID_SIZE_IN_BYTES = 2.*1024.**3
!------------------------------------------------------------------------------
! Define the ECgrid type: assumed integer coordinates
type :: ECgrid
integer(i4) :: npts,nx,ny,nz,ix1,ix2,iy1,iy2,iz1,iz2,idx,idy,idz
real(r8),allocatable :: ecs(:,:,:,:,:)
real(r8),allocatable :: x(:),y(:),z(:)
end type ECgrid
! ECgridi: explicitly integer coordinates
type :: ECgridi
integer(i4) :: npts,nx,ny,nz,ix1,ix2,iy1,iy2,iz1,iz2,idx,idy,idz
real(r8),allocatable :: ecs(:,:,:,:,:)
real(r8),allocatable :: x(:),y(:),z(:)
end type ECgridi
! ECgridr type: real coordinates
type :: ECgridr
integer(i4) :: npts,nx,ny,nz
real(r8) :: x1,x2,y1,y2,z1,z2,dx,dy,dz
real(r8),allocatable :: ecs(:,:,:,:,:)
real(r8),allocatable :: x(:),y(:),z(:)
end type ECgridr
!------------------------------------------------------------------------------
! Define the ECRgrid types: These also include density
! ECRgrid: assumed real coordinates
type :: ECRgrid
integer(i4) :: npts,nx,ny,nz
real(r8) :: x1,x2,y1,y2,z1,z2,dx,dy,dz
real(r8),allocatable :: ecs(:,:,:,:,:)
real(r8),allocatable :: rho(:,:,:)
real(r8),allocatable :: x(:),y(:),z(:)
end type ECRgrid
! ECRgridi: explicit integer coordinates
type :: ECRgridi
integer(i4) :: npts,nx,ny,nz,ix1,ix2,iy1,iy2,iz1,iz2,idx,idy,idz
real(r8),allocatable :: ecs(:,:,:,:,:)
real(r8),allocatable :: rho(:,:,:)
real(r8),allocatable :: x(:),y(:),z(:)
end type ECRgridi
!-------------------------------------------------------------------------------
CONTAINS
!$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
! ASSMUED INTEGER COORDINATES
!$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
!===============================================================================
subroutine EC_grid_new(ix1,ix2,idx,iy1,iy2,idy,iz1,iz2,idz,grid)
!===============================================================================
! Create a new grid
implicit none
type(ECgrid),intent(out) :: grid
integer,intent(in) :: ix1,ix2,idx,iy1,iy2,idy,iz1,iz2,idz
integer :: k
if ((ix1>=ix2).or.(iy1>=iy2).or.(iz1>=iz2).or.(idx<=0).or.(idy<=0).or.(idz<=0)) then
write(lu_stderr,'(a)') 'EC_grid_new: problem with array limits or spacing.'
write(lu_stderr,'(a)') ' ix1<ix2, iy1<iy2, iz1<iz1, (idx,idy,idz)>0'
stop
endif
grid % ix1 = ix1 ; grid % ix2 = ix2
grid % iy1 = iy1 ; grid % iy2 = iy2
grid % iz1 = iz1 ; grid % iz2 = iz2
grid % idx = idx
grid % idy = idy
grid % idz = idz
! Calcuate array dimensions
grid % nx = (ix2 - ix1) / idx + 1
grid % ny = (iy2 - iy1) / idy + 1
grid % nz = (iz2 - iz1) / idz + 1
grid % npts = grid%nx * grid%ny * grid%nz
if (allocated(grid%ecs)) deallocate(grid%ecs)
if (allocated(grid%x)) deallocate(grid%x)
if (allocated(grid%y)) deallocate(grid%y)
if (allocated(grid%z)) deallocate(grid%z)
! Allocate memory
allocate(grid%ecs(grid%nx,grid%ny,grid%nz,6,6))
allocate(grid%x(grid%nx), grid%y(grid%ny), grid%z(grid%nz))
! Fill in dimensions
grid%ecs = 0.
do k=0,grid%nx-1; grid % x(k+1) = real(ix1) + real(idx) * real(k); enddo
do k=0,grid%ny-1; grid % y(k+1) = real(iy1) + real(idy) * real(k); enddo
do k=0,grid%nz-1; grid % z(k+1) = real(iz1) + real(idz) * real(k); enddo
return
end subroutine EC_grid_new
!-------------------------------------------------------------------------------
!==============================================================================
subroutine EC_grid_load(ecs_file,grid,necs,quiet)
!==============================================================================
! Load a 3D grid of elastic constants into memory.
! Elastic constants should be oriented according to the grid, where
! x // 1 , y // 2 , z // 3,
! assuming a right-handed coordinate system. Later tranformations can be performed.
implicit none
type(ECgrid),intent(out) :: grid
character(len=*),intent(in) :: ecs_file
integer :: ix,iy,iz,ix1,ix2,iy1,iy2,iz1,iz2,idx,idy,idz,&
itempx,itempy,itempz,nx,ny,nz,npts,i,j,k,nec
integer,optional,intent(in) :: necs
logical,optional,intent(in) :: quiet
logical :: silent
real(rs) :: ecs_in(6,6),multiplier
integer :: iostatus
! Check optional arguments
if (present(necs)) then
if (necs /=21 .and. necs /= 36) stop 'EC_grid_load: nec must be 21 or 36'
nec = necs
else
nec = 36
endif
silent = .false.
if (present(quiet)) then
silent = quiet
endif
! Open file
open(20,file=ecs_file,status='old',iostat=iostatus)
if (iostatus /= 0) then
write(0,'(a,a)') 'Problem opening ECs file ',ecs_file
stop
endif
iostatus = 0
ix1 = big_p_integer ; ix2 = big_n_integer
iy1 = big_p_integer ; iy2 = big_n_integer
iz1 = big_p_integer ; iz2 = big_n_integer
npts = 0
! Read limits of data and find dimensions
do while (iostatus == 0)
read(20,fmt=*,iostat=iostatus) ix,iy,iz
if (iostatus /= 0) exit
if (ix < ix1) ix1 = ix
if (ix > ix2) ix2 = ix
if (iy < iy1) iy1 = iy
if (iy > iy2) iy2 = iy
if (iz < iz1) iz1 = iz
if (iz > iz2) iz2 = iz
npts = npts + 1
enddo
if (.not.silent) then
write(0,'(a)') '=================================='
write(0,'(a)') 'Dimensions of box are:'
write(0,'(a,2i5)') ' x: ',ix1,ix2
write(0,'(a,2i5)') ' y: ',iy1,iy2
write(0,'(a,2i5)') ' z: ',iz1,iz2
write(0,'(a)') '=================================='
endif
! Start again and work out the dimensions
rewind(20)
! Count the number of points with the same x and z as the first line.
read(20,*) itempx,itempy,itempz
ny = 1
do i=2,npts
read(20,fmt=*,iostat=iostatus) ix,iy,iz
if (ix == itempx .and. iz == itempz) ny = ny + 1
enddo
! Count the number of x at this y and z
rewind(20)
read(20,*) itempx,itempy,itempz
nx = 1
do i=2,npts
read(20,fmt=*,iostat=iostatus) ix,iy,iz
if (iy == itempy .and. iz == itempz) nx = nx + 1
enddo
! Count the number of z at this x and y: just for now, to check!
rewind(20)
read(20,*) itempx,itempy,itempz
nz = 1
do i=2,npts
read(20,fmt=*,iostat=iostatus) ix,iy,iz
if (ix == itempx .and. iy == itempy) nz = nz + 1
enddo
! Sanity check
if (nx*ny*nz /= npts) then
write(0,'(a)') 'load_ecs_cart: Number of points in EC file does not match dimensions!'
stop
endif
idx = (ix2 - ix1) / (nx-1)
idy = (iy2 - iy1) / (ny-1)
idz = (iz2 - iz1) / (nz-1)
if (.not.silent) then
write(0,'(3(a,i4))') 'Grid size is ',nx,' x ',ny,' x ',nz
write(0,'(3(a,i3))') 'Grid step size is ',idx,' x ',idy,' x ',idz
write(0,'(a,i6)') 'Total number of ECs is ',npts
write(0,'(a/)') '=================================='
endif
! Allocate space for the ECs
allocate(grid%ecs(nx,ny,nz,6,6),stat=iostatus)
allocate(grid%x(nx), grid%y(ny), grid%z(nz))
if (iostatus /= 0) stop 'EC_grid_load: error allocating space for ECs'
! Read each line and fill the array
rewind(20)
do k=1,npts
if (nec == 36) then
read(20,*) itempx,itempy,itempz,ecs_in
else if (nec == 21) then
read(20,*) itempx,itempy,itempz,((ecs_in(i,j),j=i,6),i=1,6)
do i=1,6
do j=1,6
ecs_in(j,i) = ecs_in(i,j)
enddo
enddo
endif
! For the first layer, determine whether we're in Pa or GPa; issue a warning regardless.
if (k == 1) then
if (ecs_in(1,1) > 1.d5) then
multiplier = 1.d0
if (.not.silent) &
write(0,'(a)') ' EC_grid_load: Input Cij file appears to be in Pa.'
else
multiplier = 1.d9
if (.not.silent) &
write(0,'(a)') ' EC_grid_load: Input Cij file appears to be in GPa.'
endif
endif
! Fill the array with the constants in Pa
grid%ecs((itempx-ix1+idx)/idx,&
(itempy-iy1+idy)/idy,&
(itempz-iz1+idz)/idz,:,:) = ecs_in(:,:) * multiplier
enddo
close(20)
grid%npts = nx*ny*nz
grid % nx = nx ; grid % ny = ny ; grid % nz = nz
grid % idx = idx ; grid % idy = idy ; grid % idz = idz
grid % ix1 = ix1 ; grid % ix2 = ix2
grid % iy1 = iy1 ; grid % iy2 = iy2
grid % iz1 = iz1 ; grid % iz2 = iz2
! Fill in dimensions
do k=0,nx-1; grid % x(k+1) = real(ix1) + real(idx) * real(k); enddo
do k=0,ny-1; grid % y(k+1) = real(iy1) + real(idy) * real(k); enddo
do k=0,nz-1; grid % z(k+1) = real(iz1) + real(idz) * real(k); enddo
end subroutine EC_grid_load
!-------------------------------------------------------------------------------
!===============================================================================
subroutine EC_grid_inquire(ecs_file,grid,quiet)
!===============================================================================
! Find out about a grid and return its dimensions. Does not allocate memory or
! read in the array or coordinates, however
implicit none
type(ECgrid),intent(out) :: grid
character(len=*),intent(in) :: ecs_file
integer :: ix,iy,iz,ix1,ix2,iy1,iy2,iz1,iz2,idx,idy,idz,&
itempx,itempy,itempz,nx,ny,nz,npts,i
logical,optional,intent(in) :: quiet
logical :: silent
integer :: iostatus
! Check optional arguments
silent = .false.
if (present(quiet)) then
silent = quiet
endif
! Open file
open(20,file=ecs_file,status='old',iostat=iostatus)
if (iostatus /= 0) then
write(0,'(a,a)') 'EC_grid_inquire: Problem opening ECs file ',ecs_file
stop
endif
iostatus = 0
ix1 = big_p_integer ; ix2 = big_n_integer
iy1 = big_p_integer ; iy2 = big_n_integer
iz1 = big_p_integer ; iz2 = big_n_integer
npts = 0
! Read limits of data and find dimensions
do while (iostatus == 0)
read(20,fmt=*,iostat=iostatus) ix,iy,iz
if (iostatus /= 0) exit
if (ix < ix1) ix1 = ix
if (ix > ix2) ix2 = ix
if (iy < iy1) iy1 = iy
if (iy > iy2) iy2 = iy
if (iz < iz1) iz1 = iz
if (iz > iz2) iz2 = iz
npts = npts + 1
enddo
if (.not.silent) then
write(lu_stdout,'(a)') '=================================='
write(lu_stdout,'(a)') 'Dimensions of box are:'
write(lu_stdout,'(a,i0,x,i0)') ' x: ',ix1,ix2
write(lu_stdout,'(a,i0,x,i0)') ' y: ',iy1,iy2
write(lu_stdout,'(a,i0,x,i0)') ' z: ',iz1,iz2
write(lu_stdout,'(a)') '=================================='
endif
! Start again and work out the dimensions
rewind(20)
! Count the number of points with the same x and z as the first line.
read(20,*) itempx,itempy,itempz
ny = 1
do i=2,npts
read(20,fmt=*,iostat=iostatus) ix,iy,iz
if (ix == itempx .and. iz == itempz) ny = ny + 1
enddo
! Count the number of x at this y and z
rewind(20)
read(20,*) itempx,itempy,itempz
nx = 1
do i=2,npts
read(20,fmt=*,iostat=iostatus) ix,iy,iz
if (iy == itempy .and. iz == itempz) nx = nx + 1
enddo
! Count the number of z at this x and y: just for now, to check!
rewind(20)
read(20,*) itempx,itempy,itempz
nz = 1
do i=2,npts
read(20,fmt=*,iostat=iostatus) ix,iy,iz
if (ix == itempx .and. iy == itempy) nz = nz + 1
enddo
! Sanity check
if (nx*ny*nz /= npts) then
write(0,'(a)') 'EC_grid_inquire: Number of points in EC file does not match dimensions!'
stop
endif
idx = (ix2 - ix1) / (nx-1)
idy = (iy2 - iy1) / (ny-1)
idz = (iz2 - iz1) / (nz-1)
if (.not.silent) then
write(lu_stdout,'(3(a,i0))') 'Grid size is ',nx, ' x ',ny, ' x ',nz
write(lu_stdout,'(3(a,i0))') 'Grid spacing is ',idx,' x ',idy,' x ',idz
write(lu_stdout,'(a,i0)') 'Total number of ECs is ',npts
write(lu_stdout,'(a/)') '=================================='
endif
! Fill in return values for grid, apart from coordinates and ecs
grid % nx = nx ; grid % ny = ny ; grid % nz = nz
grid % idx = idx ; grid % idy = idy ; grid % idz = idz
grid % ix1 = ix1 ; grid % ix2 = ix2
grid % iy1 = iy1 ; grid % iy2 = iy2
grid % iz1 = iz1 ; grid % iz2 = iz2
end subroutine EC_grid_inquire
!-------------------------------------------------------------------------------
!===============================================================================
subroutine EC_grid_inquire_bin(ecs_file,grid,quiet)
!===============================================================================
! Get information about a binary grid file
implicit none
type(ECgrid),intent(inout) :: grid
character(len=*),intent(in) :: ecs_file
logical,intent(in),optional :: quiet
logical :: silent
integer :: iostatus
silent = .true.
if (present(quiet)) silent = quiet
iostatus = 0
open(20,file=trim(ecs_file),iostat=iostatus,form='unformatted',status='old')
if (iostatus /= 0) then
write(lu_stderr,'(a,a)') &
'EC_grid_inquire_bin: cannot open file for reading: ', trim(ecs_file)
stop
endif
! Read header line
read(20,iostat=iostatus) grid%npts, grid%nx, grid%ny, grid%nz, grid%ix1, grid%ix2, &
grid%iy1, grid%iy2, grid%iz1, grid%iz2, grid%idx, grid%idy, grid%idz
close(20)
if (iostatus /= 0) then
write(lu_stderr,'(a,a)') &
'EC_grid_inquire_bin: cannot read header line for binary file ',trim(ecs_file)
stop
endif
! If desired, print out information
if (.not.silent) then
write(lu_stdout,'(a)') '=================================='
write(lu_stdout,'(a)') 'Dimensions of box are:'
write(lu_stdout,'(a,i0,x,i0)') ' x: ',grid%ix1,grid%ix2
write(lu_stdout,'(a,i0,x,i0)') ' y: ',grid%iy1,grid%iy2
write(lu_stdout,'(a,i0,x,i0)') ' z: ',grid%iz1,grid%iz2
write(lu_stdout,'(a)') '=================================='
write(lu_stdout,'(3(a,i0))') 'Grid size is ',grid%nx, ' x ',grid%ny, ' x ',grid%nz
write(lu_stdout,'(3(a,i0))') 'Grid spacing is ',grid%idx,' x ',grid%idy,' x ',grid%idz
write(lu_stdout,'(a,i0)') 'Total number of ECs is ',grid%npts
write(lu_stdout,'(a/)') '=================================='
endif
end subroutine EC_grid_inquire_bin
!-------------------------------------------------------------------------------
!===============================================================================
subroutine EC_grid_write(fname,grid,necs)
!===============================================================================
! Write the EC_grid type to an ASCII text file.
! Assumes output is all 36 elastic constants, otherwise can specify 21.
implicit none
type(ECgrid),intent(in) :: grid
character(len=*),intent(in) :: fname
character(len=80) :: fmt
integer,optional,intent(in) :: necs
integer :: iostatus,ix,iy,iz,kx,ky,kz,i,j,nec
integer :: lenx,leny,lenz
! Check for requested output format; default to all 36 constants to be written out.
if (present(necs)) then
if (necs /= 21 .and. necs /= 36) stop 'EC_grid_write: nec must be 21 or 36'
nec = necs
else
nec = 36
endif
open(20,file=trim(fname),iostat=iostatus)
if (iostatus /= 0) stop 'EC_grid_write: error opening file for writing'
! Determine output format; minimum length is 5
lenx = 5 ; leny = lenx ; lenz = lenx
if (int(log10(real(grid%ix1))+1.) > lenx) lenx = int(log10(real(grid%ix1))+1.)
if (int(log10(real(grid%ix2))+1.) > lenx) lenx = int(log10(real(grid%ix2))+1.)
if (int(log10(real(grid%iy1))+1.) > leny) leny = int(log10(real(grid%iy1))+1.)
if (int(log10(real(grid%iy2))+1.) > leny) leny = int(log10(real(grid%iy2))+1.)
if (int(log10(real(grid%iz1))+1.) > lenz) lenz = int(log10(real(grid%iz1))+1.)
if (int(log10(real(grid%iz2))+1.) > lenz) lenz = int(log10(real(grid%iz2))+1.)
write(fmt,'(3(a4,i3),a1,i2,a)') ' (i',lenx,',x,i',leny,',x,i',lenz,',',nec,'e24.16)'
do kx=1,grid%nx
ix = grid%ix1 + grid%idx*(kx-1)
do ky=1,grid%ny
iy = grid%iy1 + grid%idy*(ky-1)
do kz=1,grid%nz
iz = grid%iz1 + grid%idz*(kz-1)
if (nec == 21) then
write(20,fmt) ix,iy,iz,((grid%ecs(kx,ky,kz,i,j),j=i,6),i=1,6)
else
write(20,fmt) ix,iy,iz,grid%ecs(kx,ky,kz,:,:)
endif
enddo
enddo
enddo
close(20)
end subroutine EC_grid_write
!-------------------------------------------------------------------------------
!===============================================================================
subroutine EC_grid_load_bin(fname,grid,quiet)
!===============================================================================
! Load the ECgrid type from a binary file
implicit none
character(len=*),intent(in) :: fname
type(ECgrid),intent(inout) :: grid
logical,intent(in),optional :: quiet
logical :: silent
integer :: iostatus,ix,iy,iz,i,j,k
silent=.true.
if (present(quiet)) silent=quiet
! open file
open(20,file=fname,iostat=iostatus,form='unformatted',status='old')
if (iostatus /= 0) then
write(lu_stderr,'(a,a)') &
'EC_grid_load_bin: cannot open file for reading: ',fname
stop
endif
! Read header line
read(20) grid%npts, grid%nx, grid%ny, grid%nz, grid%ix1, grid%ix2, &
grid%iy1, grid%iy2, grid%iz1, grid%iz2, grid%idx, grid%idy, grid%idz
! Deallocate memory if reusing existing grid object and dimensions are different
if (allocated(grid%ecs)) then
if (size(grid%ecs,1) /= grid%nx .or. &
size(grid%ecs,2) /= grid%ny .or. &
size(grid%ecs,3) /= grid%nz) deallocate(grid%ecs)
endif
if (allocated(grid%x)) then
if (size(grid%x) /= grid%nx) deallocate(grid%x)
endif
if (allocated(grid%y)) then
if (size(grid%y) /= grid%ny) deallocate(grid%y)
endif
if (allocated(grid%z)) then
if (size(grid%z) /= grid%nz) deallocate(grid%z)
endif
! Allocate memory and first check this grid isn't too big
if (real(grid%nx)*real(grid%ny)*real(grid%nz)*real(r8) > MAX_GRID_SIZE_IN_BYTES) then
write(lu_stderr,'(a,i0,a)') 'EC_grid_load_bin: Allocated size of grid is ',&
real(grid%nx)*real(grid%ny)*real(grid%nz)*real(r8)/real(1024)**2,' MB: stopping.'
stop
endif
if (.not.allocated(grid%ecs)) allocate(grid%ecs(grid%nx,grid%ny,grid%nz,6,6))
if (.not.allocated(grid%x)) allocate(grid%x(grid%nx))
if (.not.allocated(grid%y)) allocate(grid%y(grid%ny))
if (.not.allocated(grid%z)) allocate(grid%z(grid%nz))
! Load constants
do iz=1,grid%nz
do iy=1,grid%ny
do ix=1,grid%nx
read(20) ((grid%ecs(ix,iy,iz,i,j),j=i,6),i=1,6)
do i=1,6; do j=i,6
grid%ecs(ix,iy,iz,j,i) = grid%ecs(ix,iy,iz,i,j)
enddo; enddo
enddo
enddo
enddo
close(20)
! Fill in dimensions
do k=0,grid%nx-1; grid % x(k+1) = real(grid%ix1) + real(grid%idx) * real(k); enddo
do k=0,grid%ny-1; grid % y(k+1) = real(grid%iy1) + real(grid%idy) * real(k); enddo
do k=0,grid%nz-1; grid % z(k+1) = real(grid%iz1) + real(grid%idz) * real(k); enddo
if (.not.silent) then
write(lu_stdout,'(a)') '=================================='
write(lu_stdout,'(a)') 'Dimensions of box are:'
write(lu_stdout,'(a,i0,x,i0)') ' x: ',grid%ix1,grid%ix2
write(lu_stdout,'(a,i0,x,i0)') ' y: ',grid%iy1,grid%iy2
write(lu_stdout,'(a,i0,x,i0)') ' z: ',grid%iz1,grid%iz2
write(lu_stdout,'(a)') '=================================='
write(lu_stdout,'(3(a,i0))') 'Grid size is ',grid%nx, ' x ',grid%ny, ' x ',grid%nz
write(lu_stdout,'(3(a,i0))') 'Grid spacing is ',grid%idx,' x ',grid%idy,' x ',grid%idz
write(lu_stdout,'(a,i0)') 'Total number of ECs is ',grid%npts
write(lu_stdout,'(a/)') '=================================='
endif
end subroutine EC_grid_load_bin
!------------------------------------------------------------------------------
!===============================================================================
subroutine EC_grid_write_bin(fname,grid)
!===============================================================================
! Write the ECgrid type to a binary, direct-access file.
! Format is:
! [1] npts,nx,ny,nz,ix1,ix2,iy1,iy2,iz1,iz2,idx,idy,idz
! [2+] c11,c12...c16,c22...c66 (loops over z slowest, x fastest)
implicit none
type(ECgrid),intent(in) :: grid
character(len=*),intent(in) :: fname
integer :: iostatus,ix,iy,iz,i,j
open(20,file=fname,iostat=iostatus,form='unformatted')
if (iostatus /= 0) then
write(lu_stderr,'(a,a)') &
'EC_grid_write_bin: Cannot open file for writing: ',trim(fname)
stop
endif
! Write the header line
write(20) grid%npts, grid%nx, grid%ny, grid%nz, grid%ix1, grid%ix2, &
grid%iy1, grid%iy2, grid%iz1, grid%iz2, grid%idx, grid%idy, grid%idz
! Write the elastic constants
do iz=1,grid%nz
do iy=1,grid%ny
do ix=1,grid%nx
write(20) ((grid%ecs(ix,iy,iz,i,j),j=i,6),i=1,6)
enddo
enddo
enddo
close(20)
end subroutine EC_grid_write_bin
!------------------------------------------------------------------------------
!==============================================================================
subroutine EC_grid_dump(grid,necs)
!===============================================================================
! Write the EC_grid type to stdout.
! Assumes output is all 36 elastic constants, otherwise can specify 21.
implicit none
type(ECgrid),intent(in) :: grid
character(len=80) :: fmt
integer,optional,intent(in) :: necs
integer :: ix,iy,iz,kx,ky,kz,i,j,nec
integer :: lenx,leny,lenz
! Check for requested output format; default to all 36 constants to be written out.
if (present(necs)) then
if (necs /= 21 .and. necs /= 36) stop 'EC_grid_dump: necs must be 21 or 36'
nec = necs
else
nec = 36
endif
! Determine output format; minimum length is 5
lenx = 5 ; leny = lenx ; lenz = lenx
if (int(log10(real(grid%ix1))+2.) > lenx) lenx = int(log10(real(grid%ix1))+2.)
if (int(log10(real(grid%ix2))+2.) > lenx) lenx = int(log10(real(grid%ix2))+2.)
if (int(log10(real(grid%iy1))+2.) > leny) leny = int(log10(real(grid%iy1))+2.)
if (int(log10(real(grid%iy2))+2.) > leny) leny = int(log10(real(grid%iy2))+2.)
if (int(log10(real(grid%iz1))+2.) > lenz) lenz = int(log10(real(grid%iz1))+2.)
if (int(log10(real(grid%iz2))+2.) > lenz) lenz = int(log10(real(grid%iz2))+2.)
write(fmt,'(3(a4,i3),a1,i2,a)') ' (i',lenx,',x,i',leny,',x,i',lenz,',',nec,'e24.16)'
write(fmt,'(a,i2,a)') '(3(i0,x),',nec,'e24.16)'
do kx=1,grid%nx
ix = grid%ix1 + grid%idx*(kx-1)
do ky=1,grid%ny
iy = grid%iy1 + grid%idy*(ky-1)
do kz=1,grid%nz
iz = grid%iz1 + grid%idz*(kz-1)
if (nec == 21) then
write(*,fmt) ix,iy,iz,((grid%ecs(kx,ky,kz,i,j),j=i,6),i=1,6)
else
write(*,fmt) ix,iy,iz,grid%ecs(kx,ky,kz,:,:)
endif
enddo
enddo
enddo
end subroutine EC_grid_dump
!------------------------------------------------------------------------------
!==============================================================================
subroutine EC_grid_clone(in,out)
! Create a new grid of ECs from another
implicit none
type(ECgrid),intent(in) :: in
type(ECgrid),intent(out) :: out
out % npts = in % npts
out % nx = in % nx
out % ny = in % ny
out % nz = in % nz
out % ix1 = in % ix1
out % ix2 = in % ix2
out % iy1 = in % iy1
out % iy2 = in % iy2
out % iz1 = in % iz1
out % iz2 = in % iz2
out % idx = in % idx
out % idy = in % idy
out % idz = in % idz
allocate(out%ecs(out%nx, out%ny, out%nz, 6, 6))
allocate(out%x(out%nx), out%y(out%ny), out%z(out%nz))
out % ecs = in % ecs
out % x = in % x
out % y = in % y
out % z = in % z
end subroutine EC_grid_clone
!------------------------------------------------------------------------------
!==============================================================================
subroutine EC_grid_delete(grid)
!==============================================================================
! Zeros out all values and deallocates the space for the arrays
implicit none
type(ECgrid),intent(inout) :: grid
grid % npts = 0
grid % nx = 0
grid % ny = 0
grid % nz = 0
grid % ix1 = 0
grid % ix2 = 0
grid % iy1 = 0
grid % iy2 = 0
grid % iz1 = 0
grid % iz2 = 0
grid % idx = 0
grid % idy = 0
grid % idz = 0
deallocate(grid%ecs)
deallocate(grid%x)
deallocate(grid%y)
deallocate(grid%z)
end subroutine EC_grid_delete
!------------------------------------------------------------------------------
!==============================================================================
function EC_grid_check_necs(fname)
!==============================================================================
! Determines the number of ECs per line in an EC_grid-type file
implicit none
character(len=*),intent(in) :: fname
integer :: EC_grid_check_necs
real(rs) :: x,y,z, test(21)
real(rs),parameter:: test_comparison=1.d0
integer :: iostatus
! Test for which way round the input file is
open(20,file=fname,status='old',iostat=iostatus)
if (iostatus /= 0) then
write(0,'(a,a)') 'EC_grid_check_necs: can''t open file:',trim(fname)
stop
endif
! Compare the values in the 4th column with that in the 10th (i.e. the second and
! seventh columns of the ECs):
! if n=36, they should be the same (have read c12 and c21). Otherwise, n=21.
! If this is true by accident, the constants are in completely the wrong fromat anyway.
test(1) = 0.
do while (abs(test(1)) < test_comparison)
read(20,*) x,y,z,test
enddo
if (abs(test(2) - test(7)) < test_comparison) then
EC_grid_check_necs = 36
else
EC_grid_check_necs = 21
endif
close(20)
end function EC_grid_check_necs
!-------------------------------------------------------------------------------
!===============================================================================
subroutine EC_grid_check_type_bin(file,nver,quiet)
!===============================================================================
! Check whether a file represents a binary (1) implicit integer, (2) explicit
! integer or (3) explicit-real ECgrid type file.
implicit none
character(len=*),intent(in) :: file
integer,intent(out) :: nver
logical,optional,intent(in) :: quiet
logical :: silent
integer :: iostatus
! Check for verbosity level
if (present(quiet)) then
silent = quiet
else
silent = .true.
endif
open(20,file=file,iostat=iostatus,form='unformatted',status='old')
if (iostatus /= 0) then
write(0,'(a,a)') 'EC_grid_check_type_bin: can''t open file: ',trim(file)
stop
endif
! Read first item in file header
read(20,iostat=iostatus) nver
if (iostatus /= 0) then
write(0,'(a,a,a)') 'EC_grid_check_type_bin: can''t read first item in header of ',&
'binary file ',trim(file)
stop
endif
close(20)
! Check version
if (nver /= nver_i .and. nver /= nver_r) nver = nver_assumed_i
! Write this out if we want
if (.not.silent) then
select case (nver)
case(nver_assumed_i)
write(lu_stdout,'(a,a,a)') 'EC_grid_check_type_bin: file "',trim(file),&
' is type "assumed-integer-coordinates"'
case(nver_i)
write(lu_stdout,'(a,a,a)') 'EC_grid_check_type_bin: file "',trim(file),&
' is type "explicit-integer-coordinates"'
case(nver_r)
write(lu_stdout,'(a,a,a)') 'EC_grid_check_type_bin: file "',trim(file),&
' is type "explicit-real-coordinates"'
end select
endif
end subroutine EC_grid_check_type_bin
!-------------------------------------------------------------------------------
!===============================================================================
subroutine EC_grid_dump_file_bin(file,necs)
!===============================================================================
! Dump a binary grid file straight from the file to the screen
implicit none
character(len=*), intent(in) :: file
integer,intent(in),optional :: necs
type(ECgridr) :: gridr
type(ECgrid) :: grid
type(ECgridi) :: gridi
integer :: nver
! Get the file type
call EC_grid_check_type_bin(file,nver)
! Call the relevant routine to load and dump the file
if (nver == nver_assumed_i) then
call EC_grid_load_bin(file,grid)
if (present(necs)) then
call EC_grid_dump(grid,necs=necs)
else
call EC_grid_dump(grid)
endif
else if (nver == nver_i) then
call EC_gridi_load_bin(file,gridi)
if (present(necs)) then
call EC_gridi_dump(gridi,necs=necs)
else
call EC_gridi_dump(gridi)
endif
else if (nver == nver_r) then
call EC_gridr_load_bin(file,gridr)
if (present(necs)) then
call EC_gridr_dump(gridr,necs=necs)
else
call EC_gridr_dump(gridr)
endif
endif
end subroutine EC_grid_dump_file_bin
!-------------------------------------------------------------------------------
!===============================================================================
subroutine EC_grid_to_gridr(gridr,grid,gridi)
!===============================================================================
! Converts assumed- or explicit-integer dimensioned grids into ones with real
! dimensions. Supply either grid or gridi via optional arguments.
implicit none
type(ECgridr),intent(out) :: gridr
type(ECgrid),intent(in),optional :: grid
type(ECgridi),intent(in),optional :: gridi
real(r8) :: x1,x2,y1,y2,z1,z2,dx,dy,dz
! Check for correct options
if (.not.present(grid).and..not.present(gridi)) then
write(lu_stderr,'(a)') 'EC_grid_to_gridr: Supply either assumed-int or explicit-int input grids.'
stop
else if (present(grid).and.present(gridi)) then
write(lu_stderr,'(a)') 'EC_grid_to_gridr: Only one of assumed-ont or explicit-int grids can be supplied.'
stop
endif
! Get limits for new grid, create new one, and copy constants across
if (present(grid)) then
x1 = real(grid%ix1); x2 = real(grid%ix2)
y1 = real(grid%iy1); y2 = real(grid%iy2)
z1 = real(grid%iz1); z2 = real(grid%iz2)
dx = real(grid%idx); dy = real(grid%idy); dz = real(grid%idz)
call EC_gridr_new(x1,x2,dx,y1,y2,dy,z1,z2,dz,gridr)
gridr%ecs = grid%ecs
else
x1 = real(gridi%ix1); x2 = real(gridi%ix2)
y1 = real(gridi%iy1); y2 = real(gridi%iy2)
z1 = real(gridi%iz1); z2 = real(gridi%iz2)
dx = real(gridi%idx); dy = real(gridi%idy); dz = real(gridi%idz)
call EC_gridr_new(x1,x2,dx,y1,y2,dy,z1,z2,dz,gridr)
gridr%ecs = gridi%ecs
endif
end subroutine EC_grid_to_gridr
!-------------------------------------------------------------------------------
!$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
! EXPLICIT INTEGER COORDINATES
!$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$