forked from SPECFEM/specfem3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhdf5_manager.F90
More file actions
5056 lines (4273 loc) · 189 KB
/
hdf5_manager.F90
File metadata and controls
5056 lines (4273 loc) · 189 KB
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
!=====================================================================
!
! S p e c f e m 3 D
! -----------------
!
! Main historical authors: Dimitri Komatitsch and Jeroen Tromp
! CNRS, France
! and Princeton University, USA
! (there are currently many more authors!)
! (c) October 2017
!
! This program 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; either version 3 of the License, or
! (at your option) any later version.
!
! This program 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.
!
! You should have received a copy of the GNU General Public License along
! with this program; if not, write to the Free Software Foundation, Inc.,
! 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
!
!=====================================================================
!-------------------------------------------------------------------------------
!> Tools for parallel HDF5 support,
!> contains wrapper subroutines for common hdf5 calls
!
! The idea for these HDF5 tools is to convert all database files to a HDF5-format
! for better scalability to very large simulations.
! It is complementary to ADIOS2 support in case those libraries are not installed/wanted.
! These HDF5 files should avoid bottlenecks on HPC clusters due to the possible higher number of file outputs
! in our simulations (database files, seismograms, visualization snapshots, etc.).
! note: HDF5 library calls use a format like "h5**do_something***_f()"
! our own wrapper functions in this module will rather use something like "h5_***do_something_***()",
! and higher-level routines called in other Fortran routines like "***do_something_h5***()",
! to better distinguish between library functions and wrappers.
!
! version info:
! - initial version, April 2023:
! Masaru Nagaso (main developer, all initial hdf5 functionality)
! Daniel Peter (just adding Masaru-san's original implementation, sometimes renaming things to more specfem-coherent ways)
!
!-------------------------------------------------------------------------------
!
! parallel HDF5 file I/O routines
!
module manager_hdf5
! class-like module for HDF5 routines
use constants, only: CUSTOM_REAL, MAX_STRING_LEN
#if defined(USE_HDF5)
use hdf5
#endif
implicit none
private
! public routines
! also to act as empty stubs (for missing HDF5 compilation support)
public :: write_attenuation_file_hdf5, read_attenuation_file_hdf5
public :: write_checkmesh_data_hdf5, write_checkmesh_xdmf_hdf5
! functional interface
public :: h5_initialize
#if defined(USE_HDF5)
! only w/ HDF5 compilation
public :: &
h5_finalize
public :: &
h5_create_file, &
h5_open_file, &
h5_close_file, &
h5_create_group, &
h5_open_group, &
h5_close_group, &
h5_create_subgroup, &
h5_open_subgroup, &
h5_open_or_create_group, &
h5_close_subgroup
public :: &
h5_open_dataset, &
h5_open_dataset2, &
h5_close_dataset
public :: &
h5_add_attribute_i, &
h5_set_mpi_info, &
h5_set_buffer_size, &
h5_set_sieve_buffer_size, &
h5_set_group_name, &
h5_gather_dsetsize
public :: &
h5_create_file_p, &
h5_create_file_p_collect, &
h5_open_file_p, &
h5_open_file_p_collect, &
h5_close_file_p, &
h5_create_file_prop_list, &
h5_close_prop_list, &
h5_close_prop_list_nocheck, &
h5_open_group_prop_list, &
h5_create_group_prop_list, &
h5_close_group_prop_list, &
h5_create_dataset_prop_list, &
h5_create_dataset_gen, &
h5_create_dataset_gen_in_group, &
h5_check_dataset_exists, &
h5_create_group_p, &
h5_open_group_p
public :: &
h5_read_attribute_p, &
h5_read_dataset_p, &
h5_read_dataset_p_scalar, &
h5_read_dataset_scalar_collect_hyperslab, &
h5_read_dataset_collect_hyperslab_in_group, &
h5_read_dataset_collect_hyperslab
public :: &
i2c
public :: &
h5_write_dataset, &
h5_write_dataset_no_group, &
h5_write_dataset_p, &
h5_write_dataset_p_a, &
h5_write_dataset_1d_to_2d_r_collect_hyperslab, &
h5_write_dataset_2d_to_3d_r_collect_hyperslab, &
h5_write_dataset_collect_hyperslab_in_group, &
h5_write_dataset_collect_hyperslab
! private routines
private :: &
h5_check_arr_dim, &
h5_check_collective, &
h5_check_group
private :: &
create_dataset_collect, &
bool_array2integer, &
int_array2bool
! generic interface to read dataset
interface h5_read_dataset_p
module procedure h5_read_dataset_p_1d_l ! logical
module procedure h5_read_dataset_p_1d_i ! integer
module procedure h5_read_dataset_p_2d_i
module procedure h5_read_dataset_p_3d_i
module procedure h5_read_dataset_p_4d_i
module procedure h5_read_dataset_p_1d_r ! real
module procedure h5_read_dataset_p_2d_r
module procedure h5_read_dataset_p_3d_r
module procedure h5_read_dataset_p_4d_r
module procedure h5_read_dataset_p_5d_r
module procedure h5_read_dataset_p_2d_d ! double
module procedure h5_read_dataset_p_2d_c ! char
end interface h5_read_dataset_p
! generic interface to read scalar dataset
interface h5_read_dataset_p_scalar
module procedure h5_read_dataset_p_scalar_i
module procedure h5_read_dataset_p_scalar_r
end interface h5_read_dataset_p_scalar
! generic interface to read dataset in collective mode
interface h5_read_dataset_scalar_collect_hyperslab
module procedure h5_read_dataset_scalar_i_collect_hyperslab
module procedure h5_read_dataset_scalar_r_collect_hyperslab
end interface h5_read_dataset_scalar_collect_hyperslab
! generic interface to read dataset in collective mode
interface h5_read_dataset_collect_hyperslab_in_group
module procedure h5_read_dataset_1d_r_collect_hyperslab_in_group
module procedure h5_read_dataset_2d_i_collect_hyperslab_in_group
end interface h5_read_dataset_collect_hyperslab_in_group
! generic interface to read dataset in collective mode
interface h5_read_dataset_collect_hyperslab
module procedure h5_read_dataset_1d_l_collect_hyperslab ! logical
module procedure h5_read_dataset_1d_i_collect_hyperslab ! integer
module procedure h5_read_dataset_1d_i64_collect_hyperslab ! integer 64-bit
module procedure h5_read_dataset_2d_i_collect_hyperslab
module procedure h5_read_dataset_3d_i_collect_hyperslab
module procedure h5_read_dataset_4d_i_collect_hyperslab
module procedure h5_read_dataset_1d_r_collect_hyperslab ! real
module procedure h5_read_dataset_2d_r_collect_hyperslab
module procedure h5_read_dataset_3d_r_collect_hyperslab
module procedure h5_read_dataset_4d_r_collect_hyperslab
module procedure h5_read_dataset_5d_r_collect_hyperslab
module procedure h5_read_dataset_2d_d_collect_hyperslab ! double
end interface h5_read_dataset_collect_hyperslab
! generic interface to write dataset
interface h5_write_dataset
module procedure h5_write_dataset_1d_i ! integer
module procedure h5_write_dataset_2d_i
module procedure h5_write_dataset_1d_d ! double
module procedure h5_write_dataset_2d_d
module procedure h5_write_dataset_1d_c ! char
module procedure h5_write_dataset_2d_c
module procedure h5_write_dataset_2d_r ! real
module procedure h5_write_dataset_4d_r
end interface h5_write_dataset
interface h5_write_dataset_no_group
module procedure h5_write_dataset_1d_i_no_group
module procedure h5_write_dataset_1d_d_no_group
module procedure h5_write_dataset_1d_c_no_group
module procedure h5_write_dataset_2d_r_no_group
end interface h5_write_dataset_no_group
! generic interface to write dataset
interface h5_write_dataset_p
module procedure h5_write_dataset_p_1d_l ! logical
module procedure h5_write_dataset_p_1d_i ! integer
module procedure h5_write_dataset_p_2d_i
module procedure h5_write_dataset_p_3d_i
module procedure h5_write_dataset_p_4d_i
module procedure h5_write_dataset_p_1d_r ! real
module procedure h5_write_dataset_p_2d_r
module procedure h5_write_dataset_p_3d_r
module procedure h5_write_dataset_p_4d_r
module procedure h5_write_dataset_p_5d_r
module procedure h5_write_dataset_p_2d_d ! double
end interface h5_write_dataset_p
interface h5_write_dataset_p_a
module procedure h5_write_dataset_p_1d_ia
module procedure h5_write_dataset_p_2d_ia
end interface h5_write_dataset_p_a
! generic interface to write dataset in collective mode
interface h5_write_dataset_collect_hyperslab_in_group
module procedure h5_write_dataset_2d_i_collect_hyperslab_in_group
module procedure h5_write_dataset_1d_r_collect_hyperslab_in_group
end interface h5_write_dataset_collect_hyperslab_in_group
! generic interface to write dataset in collective mode
interface h5_write_dataset_collect_hyperslab
module procedure h5_write_dataset_1d_l_collect_hyperslab ! logical
module procedure h5_write_dataset_1d_i_collect_hyperslab ! integer
module procedure h5_write_dataset_1d_i64_collect_hyperslab ! integer 64-bit
module procedure h5_write_dataset_2d_i_collect_hyperslab
module procedure h5_write_dataset_3d_i_collect_hyperslab
module procedure h5_write_dataset_4d_i_collect_hyperslab
module procedure h5_write_dataset_1d_r_collect_hyperslab ! real
module procedure h5_write_dataset_2d_r_collect_hyperslab
module procedure h5_write_dataset_3d_r_collect_hyperslab
module procedure h5_write_dataset_4d_r_collect_hyperslab
module procedure h5_write_dataset_5d_r_collect_hyperslab
module procedure h5_write_dataset_2d_d_collect_hyperslab ! double
end interface h5_write_dataset_collect_hyperslab
! generic interface to create dataset
interface h5_create_dataset_gen
module procedure h5_create_dataset_gen_int
module procedure h5_create_dataset_gen_int64
end interface h5_create_dataset_gen
! object-oriented interface
! (Fortran 2003 standard style)
!
! usage example:
! subroutine **
! ..
! use manager_hdf5, only: h5io
! type(h5io) :: h5
! ..
! h5 = h5io() ! calls the object constructor, i.e. h5io_constructor()
! call h5%open(fname) ! object function call
! ! or in a single call:
! ! h5 = h5io(fname)
! call h5%write("x",store_val_x_all)
! call h5%close()
! ..
! end subroutine ! h5 goes out of scope, will call the object destructor, i.e., h5io_destructor()
!
type, public :: h5io
private
logical :: is_initialized = .false.
integer(HID_T) :: f_id = -1, g_id = -1, d_id = -1
character(len=MAX_STRING_LEN) :: filename = ""
contains
procedure :: open => h5io_open_file
procedure :: close => h5io_close_file
generic :: write => h5io_write_dataset_i, h5io_write_dataset_r
procedure, private :: h5io_write_dataset_i
procedure, private :: h5io_write_dataset_r
final :: h5io_destructor
end type h5io
! object constructor
! called by: h5 = h5io()
interface h5io
module procedure :: h5io_constructor
module procedure :: h5io_constructor_from_file
end interface h5io
! module parameters
! ids
integer(HID_T) :: file_id, group_id, parent_group_id, dataset_id
integer(HID_T) :: mem_dspace_id, file_dspace_id ! for collective IO
! parallel process
integer(HID_T) :: plist_id, fplist_id, gplist_id
! string array
! initial string length for network/station names (to be determined later)
integer(SIZE_T) :: str_len = 16
integer(HID_T) :: str_type
! maximum string length for passing character array undef_mat_prop
integer(SIZE_T), parameter :: str_len_max = MAX_STRING_LEN
integer(HID_T) :: str_type_max
type(c_ptr) :: f_ptr
! MPI info
integer :: this_rank, this_info, this_comm, total_proc
! io unit
integer, parameter :: xdmf_mesh = 190
! mesh nodes
public :: xdmf_mesh_nnodes
integer :: xdmf_mesh_nnodes
! function call errors
integer :: error
! class-wide private variables
character(len=256) :: file_path
character(len=256) :: store_group_name ! groupname for debug
public :: name_database_hdf5
! store HDF5 output file name
character(len=MAX_STRING_LEN) :: name_database_hdf5
#endif
contains
!-------------------------------------------------------------------------------
!
! public HDF5 wrapper routines (also available without hdf5 compilation support)
!
!-------------------------------------------------------------------------------
subroutine h5_initialize()
#if defined(USE_HDF5)
use constants, only: MAX_LENGTH_NETWORK_NAME, MAX_LENGTH_STATION_NAME
#endif
implicit none
#if defined(USE_HDF5)
! initialize Fortran interface
call h5open_f(error)
call check_error()
! prepare string array type
if (MAX_LENGTH_STATION_NAME >= MAX_LENGTH_NETWORK_NAME) then
str_len = MAX_LENGTH_STATION_NAME
else
str_len = MAX_LENGTH_NETWORK_NAME
endif
call h5tcopy_f(H5T_Fortran_S1, str_type, error)
call check_error()
call h5tset_size_f(str_type, str_len, error)
call check_error()
! string array type for undef_mat_prop array
call h5tcopy_f(H5T_Fortran_S1, str_type_max, error)
call check_error()
call h5tset_size_f(str_type_max, str_len_max, error)
call check_error()
#else
! no HDF5 compilation support
! compilation without HDF5 support
print *
print *, "Error: HDF5 routine h5_initialize() called without HDF5 Support."
print *, "To enable HDF5 support, reconfigure with --with-hdf5 flag."
print *
! safety stop
stop 'Error HDF5 manager: h5_initialize() intitialization called without compilation support'
#endif
end subroutine h5_initialize
!-------------------------------------------------------------------------------
!
! higher level utilities
!
!-------------------------------------------------------------------------------
subroutine write_attenuation_file_hdf5(factor_common, scale_factor, factor_common_kappa, scale_factor_kappa)
#if defined(USE_HDF5)
use shared_parameters, only: NPROC, LOCAL_PATH,H5_COL
use constants, only: myrank,N_SLS,NGLLX,NGLLY,NGLLZ
#endif
implicit none
real(kind=CUSTOM_REAL), allocatable, dimension(:,:,:,:,:) :: factor_common
real(kind=CUSTOM_REAL), allocatable, dimension(:,:,:,:) :: scale_factor
real(kind=CUSTOM_REAL), allocatable, dimension(:,:,:,:,:) :: factor_common_kappa
real(kind=CUSTOM_REAL), allocatable, dimension(:,:,:,:) :: scale_factor_kappa
#if defined(USE_HDF5)
integer, dimension(4) :: dims
integer :: nspec
! offset arrays
integer, dimension(0:NPROC-1) :: offset_nspec
! hdf5 valiables
character(len=64) :: filename, dset_name, tempstr
dims = shape(scale_factor)
nspec = dims(4)
! prepare offset arrays
call gather_all_all_singlei(nspec,offset_nspec,NPROC) ! n spec in each proc
tempstr = "/external_mesh.h5"
filename = LOCAL_PATH(1:len_trim(LOCAL_PATH))//trim(tempstr)
! initialize h5 object
call h5_initialize()
! prepare dataset
if (myrank == 0) then
call h5_open_file(filename)
dset_name = "scale_factor"
call h5_create_dataset_gen(dset_name, (/NGLLX,NGLLY,NGLLZ,sum(offset_nspec)/), 4, CUSTOM_REAL)
dset_name = "scale_factor_kappa"
call h5_create_dataset_gen(dset_name, (/NGLLX,NGLLY,NGLLZ,sum(offset_nspec)/), 4, CUSTOM_REAL)
dset_name = "factor_common"
call h5_create_dataset_gen(dset_name, (/N_SLS,NGLLX,NGLLY,NGLLZ,sum(offset_nspec)/), 5, CUSTOM_REAL)
dset_name = "factor_common_kappa"
call h5_create_dataset_gen(dset_name, (/N_SLS,NGLLX,NGLLY,NGLLZ,sum(offset_nspec)/), 5, CUSTOM_REAL)
call h5_close_file()
endif
call synchronize_all()
! write
call h5_open_file_p_collect(filename)
dset_name = "scale_factor"
call h5_write_dataset_4d_r_collect_hyperslab(dset_name, &
scale_factor,(/0,0,0,sum(offset_nspec(0:myrank-1))/),H5_COL)
dset_name = "scale_factor_kappa"
call h5_write_dataset_4d_r_collect_hyperslab(dset_name, &
scale_factor_kappa,(/0,0,0,sum(offset_nspec(0:myrank-1))/),H5_COL)
dset_name = "factor_common"
call h5_write_dataset_5d_r_collect_hyperslab(dset_name, &
factor_common,(/0,0,0,0,sum(offset_nspec(0:myrank-1))/),H5_COL)
dset_name = "factor_common_kappa"
call h5_write_dataset_5d_r_collect_hyperslab(dset_name, &
factor_common_kappa,(/0,0,0,0,sum(offset_nspec(0:myrank-1))/),H5_COL)
call h5_close_file_p()
call h5_finalize()
#else
! no compilation support
! to avoid compiler warnings
real(kind=CUSTOM_REAL) :: dummy
dummy = factor_common(1,1,1,1,1)
dummy = factor_common_kappa(1,1,1,1,1)
dummy = scale_factor(1,1,1,1)
dummy = scale_factor_kappa(1,1,1,1)
! safety stop
stop 'Error HDF5 manager: write_attenuation_file_hdf5() called without compilation support'
#endif
end subroutine write_attenuation_file_hdf5
!
!-------------------------------------------------------------------------------
!
subroutine read_attenuation_file_hdf5(factor_common, scale_factor, factor_common_kappa, scale_factor_kappa)
#if defined(USE_HDF5)
use shared_parameters, only: NPROC, LOCAL_PATH, H5_COL
use constants, only: myrank
#endif
implicit none
real(kind=CUSTOM_REAL), allocatable, dimension(:,:,:,:,:) :: factor_common
real(kind=CUSTOM_REAL), allocatable, dimension(:,:,:,:) :: scale_factor
real(kind=CUSTOM_REAL), allocatable, dimension(:,:,:,:,:) :: factor_common_kappa
real(kind=CUSTOM_REAL), allocatable, dimension(:,:,:,:) :: scale_factor_kappa
#if defined(USE_HDF5)
! offset array
integer, dimension(0:NPROC-1) :: offset_nspec
! hdf5 valiables
character(len=64) :: fname, tempstr
tempstr = "/external_mesh.h5"
fname = LOCAL_PATH(1:len_trim(LOCAL_PATH))//trim(tempstr)
! initialize h5 object
call h5_initialize()
! open file
call h5_open_file_p_collect(fname)
! read offset array
call h5_read_dataset_collect_hyperslab("offset_nspec",offset_nspec, (/0/), H5_COL)
call h5_read_dataset_collect_hyperslab("scale_factor", scale_factor, &
(/0,0,0,sum(offset_nspec(0:myrank-1))/), H5_COL)
call h5_read_dataset_collect_hyperslab("scale_factor_kappa", scale_factor_kappa, &
(/0,0,0,sum(offset_nspec(0:myrank-1))/), H5_COL)
call h5_read_dataset_collect_hyperslab("factor_common", factor_common, &
(/0,0,0,0,sum(offset_nspec(0:myrank-1))/), H5_COL)
call h5_read_dataset_collect_hyperslab("factor_common_kappa", factor_common_kappa, &
(/0,0,0,0,sum(offset_nspec(0:myrank-1))/), H5_COL)
call h5_close_file_p()
call h5_finalize()
#else
! no compilation support
! to avoid compiler warnings
real(kind=CUSTOM_REAL) :: dummy
dummy = factor_common(1,1,1,1,1)
dummy = factor_common_kappa(1,1,1,1,1)
dummy = scale_factor(1,1,1,1)
dummy = scale_factor_kappa(1,1,1,1)
! safety stop
stop 'Error HDF5 manager: read_attenuation_file_hdf5() called without compilation support'
#endif
end subroutine read_attenuation_file_hdf5
!
!-------------------------------------------------------------------------------
!
subroutine write_checkmesh_data_hdf5(dset_name,dump_array)
#if defined(USE_HDF5)
use shared_parameters, only: LOCAL_PATH, NPROC, H5_COL
use constants, only: myrank
#endif
implicit none
real(kind=CUSTOM_REAL),dimension(:), intent(in) :: dump_array
character(len=MAX_STRING_LEN), intent(in) :: dset_name
#if defined(USE_HDF5)
character(len=MAX_STRING_LEN) :: filename
integer, dimension(0:NPROC-1) :: offset
! MPI variables
integer :: info, comm
! hdf5 valiables
character(len=64) :: tempstr
! flag if dataset exists
logical :: exists
! saves mesh file external_mesh.h5
tempstr = "/external_mesh.h5"
filename = LOCAL_PATH(1:len_trim(LOCAL_PATH))//trim(tempstr)
! get MPI parameters
call world_get_comm(comm)
call world_get_info_null(info)
! initialize h5 object
call h5_initialize()
call h5_set_mpi_info(comm, info, myrank, NPROC)
! get offset info
call gather_all_all_singlei(size(dump_array), offset, NPROC)
! make dataset
if (myrank == 0) then
call h5_open_file(filename)
! check if dataset exists
exists = .false.
call h5_check_dataset_exists(dset_name, exists)
if (.not. exists) then
call h5_create_dataset_gen(dset_name, (/sum(offset(:))/), 1, CUSTOM_REAL)
endif
call h5_close_file()
endif
call synchronize_all()
! open file (usually *_collect can be used with H5_COL = .false.)
! but I got a strange error on some systems, so I use if statement here
if (H5_COL) then
call h5_open_file_p_collect(filename)
else
call h5_open_file_p(filename)
endif
call h5_write_dataset_collect_hyperslab(dset_name, dump_array, (/sum(offset(0:myrank-1))/), H5_COL)
call h5_close_file_p()
call h5_finalize()
#else
! no compilation support
! to avoid compiler warnings
real(kind=CUSTOM_REAL) :: dummy
character(len=1) :: c_dummy
dummy = dump_array(1)
c_dummy = dset_name(1:1)
! safety stop
stop 'Error HDF5 manager: write_checkmesh_data_hdf5() called without compilation support'
#endif
end subroutine write_checkmesh_data_hdf5
!
!-------------------------------------------------------------------------------
!
subroutine write_checkmesh_xdmf_hdf5(NSPEC_AB)
#if defined(USE_HDF5)
use constants, only: myrank
use shared_parameters
#endif
implicit none
integer, intent(in) :: NSPEC_AB
#if defined(USE_HDF5)
character(len=MAX_STRING_LEN) :: fname_xdmf_checkmesh,fname_h5_database_xdmf,fname_h5_extmesh_xdmf
integer, dimension(0:NPROC-1) :: nelms, nnodes
character(len=20) :: type_str,nelm_str,nnode_str
! gather number of elements in each proc
call gather_all_singlei(NSPEC_AB, nelms, NPROC)
! count and gather the number of control nodes in each proc
call gather_all_singlei(xdmf_mesh_nnodes, nnodes, NPROC)
if (myrank == 0) then
! writeout xdmf file for surface movie
fname_xdmf_checkmesh = trim(LOCAL_PATH)//"/checkmesh.xmf"
fname_h5_database_xdmf = "./Database.h5" ! relative to checkmesh.xmf file
fname_h5_extmesh_xdmf = "./external_mesh.h5"
open(unit=xdmf_mesh, file=trim(fname_xdmf_checkmesh), recl=512)
! definition of topology and geometry
! refer only control nodes (8 or 27) as a coarse output
! data array need to be extracted from full data array on GLL points
write(xdmf_mesh,'(a)') '<?xml version="1.0" ?>'
write(xdmf_mesh,*) '<!DOCTYPE Xdmf SYSTEM "Xdmf.dtd" []>'
write(xdmf_mesh,*) '<Xdmf xmlns:xi="http://www.w3.org/2003/XInclude" Version="3.0">'
write(xdmf_mesh,*) '<Domain>'
write(xdmf_mesh,*) '<!-- mesh info -->'
!write(xdmf_mesh,*) '<Grid Name="mesh" GridType="Collection" CollectionType="Spatial">'
nelm_str = i2c(sum(nelms(:)))
nnode_str = i2c(sum(nnodes(:)))
write(xdmf_mesh,*) '<Grid Name="mesh">'
write(xdmf_mesh,*) '<Topology TopologyType="Mixed" NumberOfElements="'//trim(nelm_str)//'">'
write(xdmf_mesh,*) '<DataItem ItemType="Uniform" Format="HDF" NumberType="Int" Precision="4" Dimensions="'&
//trim(nelm_str)//' '//trim(i2c(8+1))//'">'
write(xdmf_mesh,*) ' '//trim(fname_h5_database_xdmf)//':/elm_conn_xdmf'
write(xdmf_mesh,*) '</DataItem>'
write(xdmf_mesh,*) '</Topology>'
write(xdmf_mesh,*) '<Geometry GeometryType="XYZ">'
write(xdmf_mesh,*) '<DataItem ItemType="Uniform" Format="HDF" NumberType="Float" Precision="'&
//trim(i2c(CUSTOM_REAL))//'" Dimensions="'//trim(nnode_str)//' 3">'
write(xdmf_mesh,*) ' '//trim(fname_h5_database_xdmf)//':/nodes_coords'
write(xdmf_mesh,*) '</DataItem>'
write(xdmf_mesh,*) '</Geometry>'
type_str = "res_Courant_number"
write(xdmf_mesh,*) '<Attribute Name="'//trim(type_str)//'" AttributeType="Scalar" Center="Cell">'
write(xdmf_mesh,*) '<DataItem ItemType="Uniform" Format="HDF" NumberType="Float" Precision="'&
//trim(i2c(CUSTOM_REAL))//'" Dimensions="'//trim(nelm_str)//'">'
write(xdmf_mesh,*) ' '//trim(fname_h5_extmesh_xdmf)//':/'//trim(type_str)
write(xdmf_mesh,*) '</DataItem>'
write(xdmf_mesh,*) '</Attribute>'
type_str = "res_minimum_period"
write(xdmf_mesh,*) '<Attribute Name="'//trim(type_str)//'" AttributeType="Scalar" Center="Cell">'
write(xdmf_mesh,*) '<DataItem ItemType="Uniform" Format="HDF" NumberType="Float" Precision="'&
//trim(i2c(CUSTOM_REAL))//'" Dimensions="'//trim(nelm_str)//'">'
write(xdmf_mesh,*) ' '//trim(fname_h5_extmesh_xdmf)//':/'//trim(type_str)
write(xdmf_mesh,*) '</DataItem>'
write(xdmf_mesh,*) '</Attribute>'
!write(xdmf_mesh,*) '</Grid>'
write(xdmf_mesh,*) '</Grid>'
write(xdmf_mesh,*) '</Domain>'
write(xdmf_mesh,*) '</Xdmf>'
close(xdmf_mesh)
endif
#else
! no compilation support
! to avoid compiler warnings
integer :: dummy
dummy = NSPEC_AB
! safety stop
stop 'Error HDF5 manager: write_checkmesh_xdmf_hdf5() called without compilation support'
#endif
end subroutine write_checkmesh_xdmf_hdf5
!-------------------------------------------------------------------------------
!
! HDF5 wrapper routines (only available with HDF5 compilation support)
!
!-------------------------------------------------------------------------------
#if defined(USE_HDF5)
! only available with HDF5 compilation support
function i2c(k) result(str)
! "Convert an integer to string."
implicit none
integer, intent(in) :: k
character(len=20) str
write (str, "(i20)") k
str = adjustl(str)
end function i2c
!
!-------------------------------------------------------------------------------
!
subroutine h5_finalize()
implicit none
! close Fortran interface
call h5close_f(error)
call check_error()
end subroutine h5_finalize
!
!-------------------------------------------------------------------------------
!
subroutine h5_check_collective(dataset_name)
implicit none
character(len=*), intent(in) :: dataset_name
integer :: io_mode
call h5pget_mpio_actual_io_mode_f(plist_id,io_mode,error)
if (error /= 0) write(*,*) 'hdf5 get_mpio_actual_io_mode failed for ',trim(dataset_name)
!if (io_mode == H5D_MPIO_NO_COLLECTIVE_F) print *, &
! "collective read/write not possible for dataset: ", dataset_name
call check_error()
end subroutine h5_check_collective
!
!-------------------------------------------------------------------------------
!
subroutine h5_check_arr_dim(dim)
implicit none
integer(kind=HSIZE_T), dimension(:), intent(in) :: dim
integer :: i
! if one of the dimension is 0, cancel space_id and file_id
! loop all elements in dim
do i = 1, size(dim)
if (dim(i) == 0) then
call h5sselect_none_f(mem_dspace_id, error)
call check_error()
call h5sselect_none_f(file_dspace_id, error)
call check_error()
endif
enddo
end subroutine h5_check_arr_dim
!
!-------------------------------------------------------------------------------
!
subroutine h5_create_file(fpath_in)
implicit none
character(len=*), intent(in) :: fpath_in
! set the target file path
file_path = fpath_in
call h5fcreate_f(trim(file_path), H5F_ACC_TRUNC_F, file_id, error)
if (error /= 0) then
print *,'Error file create: ', trim(file_path)
print *
print *,'check if path exists: ', trim(file_path)
stop 'Error file create h5'
endif
end subroutine h5_create_file
!
!-------------------------------------------------------------------------------
!
subroutine h5_open_file(fpath_in)
implicit none
character(len=*), intent(in) :: fpath_in
! set the target file path
file_path = fpath_in
call h5fopen_f(trim(file_path), H5F_ACC_RDWR_F, file_id, error)
if (error /= 0) then
print *, 'Error file open: ', trim(file_path)
stop 'Error file open h5'
endif
end subroutine h5_open_file
!
!-------------------------------------------------------------------------------
!
subroutine h5_close_file()
implicit none
call h5fclose_f(file_id, error)
if (error /= 0) write(*,*) 'error while closing a file.'
call check_error()
end subroutine h5_close_file
!
!-------------------------------------------------------------------------------
!
subroutine h5_close_file_p()
implicit none
call h5_close_file_prop_list()
call h5fclose_f(file_id, error)
if (error /= 0) write(*,*) 'error while closing a file.'
call check_error()
end subroutine h5_close_file_p
!
!-------------------------------------------------------------------------------
!
subroutine h5_create_group(group_name)
implicit none
character(len=*), intent(in) :: group_name
call h5gcreate_f(file_id, trim(group_name), group_id, error)
if (error /= 0) write(*,*) 'error while creating a group, ', group_name
call check_error()
call h5gclose_f(group_id, error)
if (error /= 0) write(*,*) 'error while closing a group, ' , group_name
call check_error()
end subroutine h5_create_group
!
!-------------------------------------------------------------------------------
!
subroutine h5_create_subgroup(group_name)
implicit none
character(len=*), intent(in) :: group_name
integer(HID_T) :: temp_group_id
call h5gcreate_f(group_id, trim(group_name), temp_group_id, error)
if (error /= 0) write(*,*) 'error while creating a subgroup, ', group_name
call check_error()
call h5gclose_f(temp_group_id, error)
if (error /= 0) write(*,*) 'error while closing a subgroup, ' , group_name
call check_error()
end subroutine h5_create_subgroup
!
!-------------------------------------------------------------------------------
!
subroutine h5_open_group(group_name)
implicit none
character(len=*), intent(in) :: group_name
! open group
call h5gopen_f(file_id, trim(group_name), group_id, error) ! group open
if (error /= 0) write(*,*) 'hdf5 open group failed for ', trim(group_name)
call check_error()
! stores name
store_group_name = group_name
end subroutine h5_open_group
!
!-------------------------------------------------------------------------------
!
subroutine h5_open_subgroup(group_name)
implicit none
character(len=*), intent(in) :: group_name
integer(HID_T) :: temp_group_id
! open subgroup
call h5gopen_f(group_id, trim(group_name), temp_group_id, error) ! group open
if (error /= 0) write(*,*) 'hdf5 open subgroup failed for ', trim(group_name)
call check_error()
! put the group id of the first level becomes parent group
! only while the group at second level exists
parent_group_id = group_id
group_id = temp_group_id
end subroutine h5_open_subgroup
!
!-------------------------------------------------------------------------------
!
subroutine h5_open_or_create_group(group_name)
! Check if group with the given name exists. Create it if it doesn't,
! open it if it does.
implicit none
character(len=*), intent(in) :: group_name
! Variable for checking if a group exists or not
logical :: group_exists
! check group
call h5_check_group(group_name, group_exists)
! open or create
if (group_exists) then
call h5gopen_f(file_id, group_name, group_id, error)
else
call h5gcreate_f(file_id, group_name, group_id, error)
endif
call check_error()
end subroutine h5_open_or_create_group
!
!-------------------------------------------------------------------------------
!
subroutine h5_check_group(group_name,group_exists)
! Check if group with the given name exists. Create it if it doesn't,
! open it if it does.
implicit none
character(len=*), intent(in) :: group_name
logical, intent(out) :: group_exists
! Variable for checking if a group exists or not
group_exists = .false.
! check
call h5lexists_f(file_id, group_name, group_exists, error)
call check_error()
end subroutine h5_check_group
!
!-------------------------------------------------------------------------------
!
subroutine h5_close_group()
implicit none
call h5gclose_f(group_id, error) ! group open
if (error /= 0) write(*,*) 'hdf5 close group failed'
call check_error()
end subroutine h5_close_group
!
!-------------------------------------------------------------------------------
!
subroutine h5_close_subgroup()
implicit none
call h5gclose_f(group_id, error) ! group open
if (error /= 0) write(*,*) 'hdf5 close group failed'
call check_error()
! stores group