forked from pseudospectators/FLUSI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
save_fields.f90
1347 lines (1107 loc) · 49.2 KB
/
save_fields.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
! Wrapper for saving fields routine
subroutine save_fields_new(time,uk,u,vort,nlk,work)
use vars
implicit none
real(kind=pr),intent(in) :: time
complex(kind=pr),intent(in) :: uk(ca(1):cb(1),ca(2):cb(2),ca(3):cb(3),1:nd)
complex(kind=pr),intent(out):: nlk(ca(1):cb(1),ca(2):cb(2),ca(3):cb(3),1:nd)
real(kind=pr),intent(inout) :: work(ra(1):rb(1),ra(2):rb(2),ra(3):rb(3))
real(kind=pr),intent(inout) :: vort(ra(1):rb(1),ra(2):rb(2),ra(3):rb(3),1:nd)
real(kind=pr),intent(inout) :: u(ra(1):rb(1),ra(2):rb(2),ra(3):rb(3),1:nd)
select case(method(1:3))
case("fsi")
call save_fields_new_fsi(time,uk,u,vort,nlk,work)
case("mhd")
call save_fields_new_mhd(time,uk,u,vort,nlk,work)
case default
if (mpirank == 0) write(*,*) "Error! Unkonwn method in save_fields_new"
stop
end select
end subroutine save_fields_new
! Main save routine for fields for fsi. it computes missing values
! (such as p and vorticity) and stores the fields in several HDF5
! files.
! The latest version calls cal_nlk_fsi to avoid redudant code.
! note cal_nlk_fsi returns the NL+penal term in phys space in the work
! array "vort" which is why we have to recompute the vorticity
subroutine save_fields_new_fsi(time,uk,u,vort,nlk,work)
use mpi_header
use fsi_vars
implicit none
real(kind=pr),intent(in) :: time
complex(kind=pr),intent(in) :: uk(ca(1):cb(1),ca(2):cb(2),ca(3):cb(3),1:nd)
complex(kind=pr),intent(out):: nlk(ca(1):cb(1),ca(2):cb(2),ca(3):cb(3),1:nd)
real(kind=pr),intent(inout) :: work(ra(1):rb(1),ra(2):rb(2),ra(3):rb(3))
real(kind=pr),intent(inout) :: vort(ra(1):rb(1),ra(2):rb(2),ra(3):rb(3),1:nd)
real(kind=pr),intent(inout) :: u(ra(1):rb(1),ra(2):rb(2),ra(3):rb(3),1:nd)
character(len=17) :: name
integer :: i
!--Set up file name base
if ( save_only_one_period == "yes" ) then
! overwrite files from last period to save disk space
! i.e. t=1.05 is written to t=0.05, as well as 2.05 and 3.05
write(name,'(i5.5)') floor( (time-real(floor(time)))*100.d0 )
else
! name is just the time
write(name,'(i5.5)') floor(time*100.d0)
endif
name=trim(adjustl(name))
if (mpirank == 0 ) then
write(*,'("Saving data, time= ",es8.2,1x," flags= ",5(i1)," str=",A)') &
time, &
iSaveVelocity,iSaveVorticity,iSavePress,iSaveMask,iSaveSolidVelocity, &
trim(adjustl(name))
endif
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
call cal_nlk_fsi (time,0,nlk,uk,u,vort,work)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!-------------
! Velocity
!-------------
if (iSaveVelocity == 1) then
call Save_Field_HDF5(time,"./ux_"//name,u(:,:,:,1),"ux")
call Save_Field_HDF5(time,"./uy_"//name,u(:,:,:,2),"uy")
call Save_Field_HDF5(time,"./uz_"//name,u(:,:,:,3),"uz")
endif
!-------------
! Pressure
!-------------
if (iSavePress == 1) then
call fft3(nlk,vort) ! nlk now NL+penal term in Fourier space
! store the total pressure in the work array
call compute_pressure(nlk(:,:,:,1),nlk)
! total pressure in phys-space
call ifft(work,nlk(:,:,:,1))
! get actuall pressure (we're in the rotational formulation)
work = work - 0.5d0*( u(:,:,:,1)**2 + u(:,:,:,2)**2 + u(:,:,:,3)**2 )
call Save_Field_HDF5(time,'./p_'//name,work,"p")
endif
!-------------
! Vorticity
!-------------
if (iSaveVorticity==1) then
! cal_nlk overwrote the vorticity, recompute it
call curl(nlk(:,:,:,1),nlk(:,:,:,2),nlk(:,:,:,3),&
uk(:,:,:,1), uk(:,:,:,2), uk(:,:,:,3))
call ifft3(vort,nlk)
!-- Save Vorticity
if (iSaveVorticity == 1) then
call Save_Field_HDF5(time,"./vorx_"//name,vort(:,:,:,1),"vorx")
call Save_Field_HDF5(time,"./vory_"//name,vort(:,:,:,2),"vory")
call Save_Field_HDF5(time,"./vorz_"//name,vort(:,:,:,3),"vorz")
endif
endif
!-------------
! Mask
!-------------
if (iSaveMask == 1 .and. iPenalization == 1) then
mask = mask*eps
call Save_Field_HDF5(time,'./mask_'//name,mask,"mask")
mask = mask/eps
endif
!-------------
! solid velocity
!-------------
if (iSaveSolidVelocity == 1 .and. iPenalization == 1 .and. iMoving == 1) then
call Save_Field_HDF5(time,'./usx_'//name,us(:,:,:,1),"usx")
call Save_Field_HDF5(time,'./usy_'//name,us(:,:,:,2),"usy")
call Save_Field_HDF5(time,'./usz_'//name,us(:,:,:,3),"usz")
endif
if (mpirank==0) write(*,*) "Done saving."
end subroutine save_fields_new_fsi
! Write the field field_out to file filename, saving the name of the
! field, dsetname, (as well as time, eps, and resolution) to the
! metadata of the HDF file .
subroutine Save_Field_HDF5(time,filename,field_out,dsetname)
use mpi_header
use vars
use HDF5
implicit none
! The field to be written to disk:
real(kind=pr),intent(in) :: field_out(ra(1):rb(1),ra(2):rb(2),ra(3):rb(3))
integer, parameter :: rank = 3 ! data dimensionality (2D or 3D)
real (kind=pr), intent (in) :: time
character(len=*), intent (in) :: filename
character(len=*), intent (in) :: dsetname
integer(hid_t) :: file_id ! file identifier
integer(hid_t) :: dset_id ! dataset identifier
integer(hid_t) :: filespace ! dataspace identifier in file
integer(hid_t) :: memspace ! dataspace identifier in memory
integer(hid_t) :: plist_id ! property list identifier
! dataset dimensions in the file.
integer(hsize_t), dimension(rank) :: dimensions_file
! hyperslab dimensions
integer(hsize_t), dimension(rank) :: dimensions_local
! chunk dimensions
integer(hsize_t), dimension(rank) :: chunking_dims
! how many blocks to select from dataspace
integer(hsize_t), dimension(rank) :: count = 1
integer(hssize_t), dimension(rank) :: offset
! stride is spacing between elements, this is one here
integer(hsize_t), dimension(rank) :: stride = 1
integer :: error ! error flags
! HDF attribute variables
integer, parameter :: arank = 1
integer(hsize_t), DIMENSION(1) :: adims ! Attribute dimension
integer :: mpierror, i
real(kind=pr) :: t1 ! diagnostic used for performance analysis.
t1 = MPI_wtime()
!!! Tell HDF5 how our data is organized:
dimensions_file = (/nx,ny,nz/)
offset(1) = ra(1)
offset(2) = ra(2)
offset(3) = ra(3)
dimensions_local(1) = rb(1)-ra(1) +1
dimensions_local(2) = rb(2)-ra(2) +1
dimensions_local(3) = rb(3)-ra(3) +1
! each process knows how much data it has and where to store it.
! now, define the dataset chunking. Chunking is largest dimension in
! each direction
do i = 1, 3
call MPI_REDUCE(dimensions_local(i),chunking_dims(i),1, &
MPI_INTEGER8,MPI_MAX,0,&
MPI_COMM_WORLD,mpierror)
call MPI_BCAST(chunking_dims(i),1,MPI_INTEGER8,0, &
MPI_COMM_WORLD,mpierror )
enddo
!!! Set up the HDF data structures:
! Initialize HDF5 library and Fortran interfaces.
call h5open_f(error)
! Setup file access property list with parallel I/O access.
! this sets up a property list (plist_id) with standard values for
! FILE_ACCESS
call H5Pcreate_f(H5P_FILE_ACCESS_F, plist_id, error)
! Modify the property list and store the MPI IO comminucator
! information in the file access property list
call H5Pset_fapl_mpio_f(plist_id, MPI_COMM_WORLD, MPI_INFO_NULL, error)
! Create the file collectively. (existing files are overwritten)
call H5Fcreate_f(trim(adjustl(filename))//'.h5', H5F_ACC_TRUNC_F, &
file_id, error, access_prp = plist_id)
! this closes the property list plist_id (we'll re-use it)
call H5Pclose_f(plist_id, error)
! Create the data space for the dataset.
! Dataspace in the file: contains all data from all procs
call H5Screate_simple_f(rank, dimensions_file, filespace, error)
! dataspace in memory: contains only local data
call H5Screate_simple_f(rank, dimensions_local, memspace, error)
! Create chunked dataset.
! NB: chunking and hyperslab are unrelated
call H5Pcreate_f(H5P_DATASET_CREATE_F, plist_id, error)
call H5Pset_chunk_f(plist_id, rank, chunking_dims, error)
! Output files are single-precition
call H5Dcreate_f(file_id, dsetname, H5T_NATIVE_REAL, filespace, &
dset_id, error, plist_id)
call H5Sclose_f(filespace, error)
! Select hyperslab in the file.
call H5Dget_space_f(dset_id, filespace, error)
call H5Sselect_hyperslab_f (filespace, H5S_SELECT_SET_F, offset, count, &
error, stride, dimensions_local)
! Create property list for collective dataset write
call H5Pcreate_f(H5P_DATASET_XFER_F, plist_id, error)
call H5Pset_dxpl_mpio_f(plist_id, H5FD_MPIO_COLLECTIVE_F, error)
! Write the dataset collectively, double precision in memory
call H5Dwrite_f(dset_id, H5T_NATIVE_DOUBLE, field_out, dimensions_file, &
error, file_space_id = filespace, mem_space_id = memspace,&
xfer_prp = plist_id)
!!! Write the attributes to the HDF files.
! The attributes written are time, penalisation parameter,
! computational resolution, and physical domain size.
adims = (/1/)
call write_attribute_dble(adims,"time",(/time/),1,dset_id)
call write_attribute_dble(adims,"epsi",(/eps/),1,dset_id)
adims = (/3/)
call write_attribute_dble(adims,"domain_size",(/xl,yl,zl/),3,dset_id)
call write_attribute_int(adims,"nxyz",(/nx,ny,nz/),3,dset_id)
!!! Close dataspaces:
call H5Sclose_f(filespace, error)
call H5Sclose_f(memspace, error)
call H5Dclose_f(dset_id, error) ! Close the dataset.
call H5Pclose_f(plist_id, error) ! Close the property list.
call H5Fclose_f(file_id, error) ! Close the file.
call h5close_f(error) ! Close Fortran interfaces and HDF5 library.
! write the XMF data for all of the saved fields
if ((mpirank==0).and.(iSaveXMF==1)) then
! the filename contains a leading "./" which we must remove
call Write_XMF(time,&
trim(adjustl(filename(3:len(filename)))),&
trim(adjustl(dsetname))&
)
endif
time_save=time_save + MPI_wtime() - t1 ! performance analysis
end subroutine Save_Field_HDF5
! Generate an XMF file for paraview. Note: this is a single scalar
! field, no time-stepping or vectors are available but this allows to
! directly copy-paste a single field and load it into paraview without
! any effort.
subroutine Write_XMF(time,filename,dsetname)
use vars
implicit none
real (kind=pr), intent (in) :: time
character(len=*), intent (in) :: filename, dsetname
character(len=128) :: tmp_time, tmp_nxyz
write(tmp_time,'(es15.8)') time
! note index changes. paraview requirement.
write(tmp_nxyz,'(3(i4,1x))') nz,ny,nx
! note the XMF file goes also in the fields/ directory
open (14, file='./'//trim(adjustl(filename))//'.xmf', status='replace')
write(14,'(A)') '<?xml version="1.0" ?>'
write(14,'(A)') '<!DOCTYPE Xdmf SYSTEM "Xdmf.dtd" []>'
write(14,'(A)') '<Xdmf Version="2.0">'
write(14,'(A)') '<Domain>'
write(14,'(A)') '<Grid Name="FLUSI_cartesian_grid" GridType="Uniform">'
write(14,'(A)') ' <Time Value="'//trim(adjustl(tmp_time))//'" />'
write(14,'(A)') ' <Topology TopologyType="3DCoRectMesh" Dimensions="'&
//trim(adjustl(tmp_nxyz))//'"/>'
write(14,'(A)') ' '
write(14,'(A)') ' <Geometry GeometryType="Origin_DxDyDz">'
write(14,'(A)') ' <DataItem Dimensions="3" NumberType="Float" Format="XML">'
write(14,'(A)') ' 0 0 0'
write(14,'(A)') ' </DataItem>'
write(14,'(A)') ' <DataItem Dimensions="3" NumberType="Float" Format="XML">'
write(14,'(4x,3(es15.8,1x))') dx, dy, dz
write(14,'(A)') ' </DataItem>'
write(14,'(A)') ' </Geometry>'
write(14,'(A)') ' '
write(14,'(A)') ' <Attribute Name="'//trim(adjustl(dsetname)) &
//'" AttributeType="Scalar" Center="Node">'
write(14,'(A)') ' <DataItem Dimensions="'//trim(adjustl(tmp_nxyz))&
//'" NumberType="Float" Format="HDF">'
write(14,'(A)') ' '//trim(adjustl(filename))//'.h5:/'&
//trim(adjustl(dsetname))
write(14,'(A)') ' </DataItem>'
write(14,'(A)') ' </Attribute>'
write(14,'(A)') '</Grid>'
write(14,'(A)') '</Domain>'
write(14,'(A)') '</Xdmf>'
close (14)
end subroutine Write_XMF
! Write the restart file. nlk(...,0) and nlk(...,1) are saved, the
! time steps, and what else? FIXME: document what is saved.
subroutine Dump_Runtime_Backup(time,dt0,dt1,n1,it,nbackup,ub,nlk,work)
use mpi_header
use vars
use hdf5
implicit none
real(kind=pr),intent(inout) :: time,dt1,dt0
integer,intent(inout) :: n1,nbackup,it
complex(kind=pr),intent(in) :: ub(ca(1):cb(1),ca(2):cb(2),ca(3):cb(3),1:nd)
complex(kind=pr),intent(in)::nlk(ca(1):cb(1),ca(2):cb(2),ca(3):cb(3),1:nd,0:1)
real(kind=pr),intent(inout) :: work(ra(1):rb(1),ra(2):rb(2),ra(3):rb(3))
character(len=18) :: filename
real(kind=pr) :: t1
integer :: error ! error flags
integer(hid_t) :: file_id ! file identifier
integer(hid_t) :: plist_id ! property list identifier
t1=MPI_wtime() ! performance diagnostic
if(mpirank == 0) then
write(*,'("*** info: time=",es8.2," dumping runtime_backup",i1,".h5 to disk....")') time, nbackup
endif
! Create current filename:
write(filename,'("runtime_backup",i1,".h5")') nbackup
! Initialize HDF5 library and Fortran interfaces:
call h5open_f(error)
!!! Setup file access property list with parallel I/O access.
! Set up a property list ("plist_id") with standard values for
! FILE_ACCESS:
call H5Pcreate_f(H5P_FILE_ACCESS_F, plist_id, error)
! Modify the property list and store MPI IO comminucator information
! in the file access property list:
call H5Pset_fapl_mpio_f(plist_id, MPI_COMM_WORLD, MPI_INFO_NULL, error)
! Create the file collectively. (existing files are overwritten)
call H5Fcreate_f(filename, H5F_ACC_TRUNC_F, file_id, error, &
access_prp = plist_id)
! Close the property list (we'll re-use it)
call H5Pclose_f(plist_id, error)
! Write the fluid backup field:
call ifft(work,ub(:,:,:,1))
call Dump_Field_Backup(work,"ux",time,dt0,dt1,n1,it,file_id)
call ifft(work,ub(:,:,:,2))
call Dump_Field_Backup(work,"uy",time,dt0,dt1,n1,it,file_id)
call ifft(work,ub(:,:,:,3))
call Dump_Field_Backup(work,"uz",time,dt0,dt1,n1,it,file_id)
if(method == "mhd") then
! Write the MHD backup field:
call ifft(work,ub(:,:,:,4))
call Dump_Field_Backup(work,"bx",time,dt0,dt1,n1,it,file_id)
call ifft(work,ub(:,:,:,5))
call Dump_Field_Backup(work,"by",time,dt0,dt1,n1,it,file_id)
call ifft(work,ub(:,:,:,6))
call Dump_Field_Backup(work,"bz",time,dt0,dt1,n1,it,file_id)
endif
! Write the fluid nonlinear term backup:
call ifft(work,nlk(:,:,:,1,0))
call Dump_Field_Backup(work,"nlkx0",time,dt0,dt1,n1,it,file_id)
call ifft(work,nlk(:,:,:,2,0))
call Dump_Field_Backup(work,"nlky0",time,dt0,dt1,n1,it,file_id)
call ifft(work,nlk(:,:,:,3,0))
call Dump_Field_Backup(work,"nlkz0",time,dt0,dt1,n1,it,file_id)
call ifft(work,nlk(:,:,:,1,1))
call Dump_Field_Backup(work,"nlkx1",time,dt0,dt1,n1,it,file_id)
call ifft(work,nlk(:,:,:,2,1))
call Dump_Field_Backup(work,"nlky1",time,dt0,dt1,n1,it,file_id)
call ifft(work,nlk(:,:,:,3,1))
call Dump_Field_Backup(work,"nlkz1",time,dt0,dt1,n1,it,file_id)
if(method == "mhd") then
! Write the MHD backup field:
call ifft(work,nlk(:,:,:,4,0))
call Dump_Field_Backup(work,"bnlkx0",time,dt0,dt1,n1,it,file_id)
call ifft(work,nlk(:,:,:,5,0))
call Dump_Field_Backup(work,"bnlky0",time,dt0,dt1,n1,it,file_id)
call ifft(work,nlk(:,:,:,6,0))
call Dump_Field_Backup(work,"bnlkz0",time,dt0,dt1,n1,it,file_id)
call ifft(work,nlk(:,:,:,4,1))
call Dump_Field_Backup(work,"bnlkx1",time,dt0,dt1,n1,it,file_id)
call ifft(work,nlk(:,:,:,5,1))
call Dump_Field_Backup(work,"bnlky1",time,dt0,dt1,n1,it,file_id)
call ifft(work,nlk(:,:,:,6,1))
call Dump_Field_Backup(work,"bnlkz1",time,dt0,dt1,n1,it,file_id)
endif
! Close the file:
call H5Fclose_f(file_id, error)
! Close FORTRAN interfaces and HDF5 library:
call h5close_f(error)
nbackup = 1 - nbackup
time_bckp=time_bckp + MPI_wtime() -t1 ! Performance diagnostic
if(mpirank == 0) write(*,'("<<< info: done saving backup.")')
end subroutine Dump_Runtime_Backup
! This routine dumps a single field "field" as a dataset "dsetname" to
! a backup file "file_id". Attributes are stores in one attribute
! "bckp" which contains 8 values
subroutine Dump_Field_Backup(field,dsetname,time,dt0,dt1,n1,it,file_id)
use mpi_header
use vars
use hdf5
implicit none
real(kind=pr),intent(in) :: field(ra(1):rb(1),ra(2):rb(2),ra(3):rb(3))
real (kind=pr), intent (in) :: time,dt1,dt0
character(len=*), intent (in) :: dsetname
integer,intent(in) :: n1,it
integer(hid_t), intent(in) :: file_id ! file identifier
integer, parameter :: rank = 3 ! data dimensionality (2D or 3D)
integer(hid_t) :: dset_id ! dataset identifier
integer(hid_t) :: filespace ! dataspace identifier in file
integer(hid_t) :: memspace ! dataspace identifier in memory
integer(hid_t) :: plist_id ! property list identifier
! dataset dimensions in the file.
integer(hsize_t), dimension(rank) :: dimensions_file
integer(hsize_t), dimension(rank) :: dimensions_local
integer(hsize_t), dimension(rank) :: chunking_dims ! chunks dimensions
integer(hsize_t), dimension(rank) :: count = 1
integer(hssize_t), dimension(rank) :: offset
integer(hsize_t), dimension(rank) :: stride = 1
integer :: error, mpierror, i ! error flags
! what follows is for the attribute "time"
integer, parameter :: arank = 1
integer(hsize_t), DIMENSION(1) :: adims ! Attribute dimension
character(len=4) :: aname ! attribute name
real (kind=pr), dimension (:), allocatable :: attributes
dimensions_file = (/nx,ny,nz/)
dimensions_local(1) = rb(1)-ra(1) +1
dimensions_local(2) = rb(2)-ra(2) +1
dimensions_local(3) = rb(3)-ra(3) +1
! Offsets
offset(1) = ra(1)
offset(2) = ra(2)
offset(3) = ra(3)
! Each process knows how much data it has and where to store it.
! now, define the dataset chunking. Chunking is largest dimension in
! each direction
do i = 1, 3
call MPI_REDUCE(dimensions_local(i), chunking_dims(i),1, &
MPI_INTEGER8, MPI_MAX,0,MPI_COMM_WORLD,mpierror)
call MPI_BCAST(chunking_dims(i), 1, MPI_INTEGER8, 0, &
MPI_COMM_WORLD, mpierror)
enddo
! -----------------------------------
! Create the data space for the dataset.
! -----------------------------------
! Dataspace in the file: contains all data from all procs
call H5Screate_simple_f(rank, dimensions_file, filespace, error)
! dataspace in memory: contains only local data
call H5Screate_simple_f(rank, dimensions_local, memspace, error)
! Create chunked dataset.
call H5Pcreate_f(H5P_DATASET_CREATE_F, plist_id, error)
call H5Pset_chunk_f(plist_id, rank, chunking_dims, error)
call H5Dcreate_f(file_id, dsetname, H5T_NATIVE_DOUBLE, filespace, &
dset_id, error, plist_id)
call H5Sclose_f(filespace, error)
! Select hyperslab in the file.
call H5Dget_space_f(dset_id, filespace, error)
call H5Sselect_hyperslab_f (filespace, H5S_SELECT_SET_F, offset, count, &
error , stride, dimensions_local)
! Create property list for collective dataset write
call H5Pcreate_f(H5P_DATASET_XFER_F, plist_id, error)
call H5Pset_dxpl_mpio_f(plist_id, H5FD_MPIO_COLLECTIVE_F, error)
! Write the dataset collectively.
call H5Dwrite_f(dset_id, H5T_NATIVE_DOUBLE, field, dimensions_file, error, &
file_space_id = filespace, mem_space_id = memspace, xfer_prp = plist_id)
! ------
! Attributes (we save everything in one, all double. to be converted
! when reading (to integer)
! ------
adims = (/8/)
allocate (attributes(1:8))
aname = "bckp"
attributes = (/time,dt1,dt0,dble(n1),dble(it),dble(nx),dble(ny),dble(nz)/)
call write_attribute_dble(adims,aname,attributes,8,dset_id)
! Close dataspaces, dataset and property list
call H5Sclose_f(filespace, error)
call H5Sclose_f(memspace, error)
call H5Dclose_f(dset_id, error)
call H5Pclose_f(plist_id, error)
deallocate(attributes)
end subroutine Dump_Field_Backup
! Read in a single file that follows the naming convention
! this is a serial routine (parallel version below)
! note you need to know what dimension the file has,
! call fetch_attributes first
subroutine Read_Single_File_serial ( filename, field )
use mpi_header
use vars
use hdf5
implicit none
character(len=*),intent(in) :: filename
real(kind=pr), intent (out) :: field(0:nx-1,0:ny-1,0:nz-1)
integer, parameter :: rank = 3 ! data dimensionality (2D or 3D)
real (kind=pr) :: time,dt1,dt0, xl_file, yl_file, zl_file
character(len=80) :: dsetname
integer :: n1,it
integer :: nx_file,ny_file,nz_file, mpierror, i
integer(hid_t) :: file_id ! file identifier
integer(hid_t) :: dset_id ! dataset identifier
integer(hid_t) :: filespace ! dataspace identifier in file
integer(hid_t) :: memspace ! dataspace identifier in memory
integer(hid_t) :: plist_id ! property list identifier
! dataset dimensions in the file.
integer(hsize_t), dimension(rank) :: dimensions_file
integer(hsize_t), dimension(rank) :: dimensions_local ! chunks dimensions
integer(hsize_t), dimension(rank) :: chunking_dims ! chunks dimensions
integer(hsize_t), dimension(rank) :: count = 1
integer(hssize_t), dimension(rank) :: offset
integer(hsize_t), dimension(rank) :: stride = 1
integer :: error ! error flags
! what follows is for the attribute "time"
integer, parameter :: arank = 1
integer(hsize_t), dimension(1) :: adims ! Attribute dimension
integer(hid_t) :: aspace_id ! Attribute Dataspace identifier
!integer(hid_t) :: atype_id ! Attribute Dataspace identifier
integer(hid_t) :: attr_id ! Attribute identifier
character(len=4) :: aname ! attribute name
real (kind=pr), dimension (:), allocatable :: attributes
! the dataset is named the same way as the file: (this is convention)
dsetname = filename ( 1:index( filename, '_' )-1 )
if (mpisize>1) then
write (*,*) "this routine is currently serial only"
stop
endif
! check if file exist
call check_file_exists( filename )
! Initialize HDF5 library and Fortran interfaces.
call h5open_f(error)
! Setup file access property list with parallel I/O access. this
! sets up a property list ("plist_id") with standard values for
! FILE_ACCESS
call H5Pcreate_f(H5P_FILE_ACCESS_F, plist_id, error)
! this modifies the property list and stores MPI IO
! comminucator information in the file access property list
call H5Pset_fapl_mpio_f(plist_id, MPI_COMM_WORLD, MPI_INFO_NULL, error)
! open the file in parallel
call H5Fopen_f (filename, H5F_ACC_RDWR_F, file_id, error, plist_id)
! this closes the property list (we'll re-use it)
call H5Pclose_f(plist_id, error)
! Definition of memory distribution
dimensions_file = (/nx,ny,nz/)
dimensions_local(1) = nx
dimensions_local(2) = ny
dimensions_local(3) = nz
offset(1) = 0
offset(2) = 0
offset(3) = 0
chunking_dims(1) = nx
chunking_dims(2) = ny
chunking_dims(3) = nz
!----------------------------------------------------------------------------
! Read actual field from file (dataset)
!----------------------------------------------------------------------------
! dataspace in the file: contains all data from all procs
call H5Screate_simple_f(rank, dimensions_file, filespace, error)
! dataspace in memory: contains only local data
call H5Screate_simple_f(rank, dimensions_local, memspace, error)
! Create chunked dataset
call H5Pcreate_f(H5P_DATASET_CREATE_F, plist_id, error)
call H5Pset_chunk_f(plist_id, rank, chunking_dims, error)
! Open an existing dataset.
call H5Dopen_f(file_id, dsetname, dset_id, error)
! Select hyperslab in the file.
call H5Dget_space_f(dset_id, filespace, error)
call H5Sselect_hyperslab_f (filespace, H5S_SELECT_SET_F, offset, count, &
error , stride, dimensions_local)
! Create property list for collective dataset read
call H5Pcreate_f(H5P_DATASET_XFER_F, plist_id, error)
call H5Pset_dxpl_mpio_f(plist_id, H5FD_MPIO_COLLECTIVE_F, error)
call H5Dread_f( dset_id, H5T_NATIVE_DOUBLE, field, dimensions_local, error, &
mem_space_id = memspace, file_space_id = filespace, xfer_prp = plist_id )
call H5Sclose_f(filespace, error)
call H5Sclose_f(memspace, error)
call H5Pclose_f(plist_id, error) ! note the dataset remains opened
! Close dataset
call H5Dclose_f(dset_id, error)
call H5Fclose_f(file_id,error)
call H5close_f(error)
end subroutine Read_Single_File_serial
! Read in a single file that follows the naming convention
! note you need to know the dimensions and domain decomposition before
! calling it.
subroutine Read_Single_File ( filename, field )
use mpi_header
use vars
use hdf5
implicit none
character(len=*),intent(in) :: filename
real(kind=pr),&
dimension(ra(1):rb(1),ra(2):rb(2),ra(3):rb(3)),&
intent (out) :: field
integer, parameter :: rank = 3 ! data dimensionality (2D or 3D)
real (kind=pr) :: time,dt1,dt0, xl_file, yl_file, zl_file
character(len=80) :: dsetname
integer :: n1,it
integer :: nx_file,ny_file,nz_file, mpierror, i
integer(hid_t) :: file_id ! file identifier
integer(hid_t) :: dset_id ! dataset identifier
integer(hid_t) :: filespace ! dataspace identifier in file
integer(hid_t) :: memspace ! dataspace identifier in memory
integer(hid_t) :: plist_id ! property list identifier
! dataset dimensions in the file.
integer(hsize_t), dimension(rank) :: dimensions_file
integer(hsize_t), dimension(rank) :: dimensions_local ! chunks dimensions
integer(hsize_t), dimension(rank) :: chunking_dims ! chunks dimensions
integer(hsize_t), dimension(rank) :: count = 1
integer(hssize_t), dimension(rank) :: offset
integer(hsize_t), dimension(rank) :: stride = 1
integer :: error ! error flags
! what follows is for the attribute "time"
integer, parameter :: arank = 1
integer(hsize_t), DIMENSION(1) :: adims ! Attribute dimension
integer(hid_t) :: aspace_id ! Attribute Dataspace identifier
!integer(hid_t) :: atype_id ! Attribute Dataspace identifier
integer(hid_t) :: attr_id ! Attribute identifier
character(len=4) :: aname ! attribute name
real (kind=pr), dimension (:), allocatable :: attributes
logical :: exist1
! the dataset is named the same way as the file: (this is convention)
dsetname = filename ( 1:index( filename, '_' )-1 )
if (mpirank==0) then
write (*,'("Reading file ",A)') trim(adjustl(filename))
endif
!-----------------------------------------------------------------------------
! perform tests
!-----------------------------------------------------------------------------
call check_file_exists ( filename )
! fetch attributes from file to see if it is a good idea to load it
call Fetch_attributes( filename, dsetname,nx_file,ny_file,nz_file,&
xl_file,yl_file ,zl_file,time )
! if the resolutions do not match, yell and hang yourself
if ((nx.ne.nx_file).or.(ny.ne.ny_file).or.(nz.ne.nz_file)) then
if (mpirank == 0) then
write (*,'(A)') "read_single_file: ERROR " // trim(filename)
write (*,'("nx=",i4,"ny=",i4,"nz=",i4)') nx,ny,nz
write (*,'("but in file: nx=",i4,"ny=",i4,"nz=",i4)') nx_file,ny_file,nz_file
stop
endif
endif
! if the domain size doesn't match, proceed, but yell.
if ((xl.ne.xl_file).or.(yl.ne.yl_file).or.(zl.ne.zl_file)) then
if (mpirank == 0) then
write (*,'(A)') "read_single_file: WARNING " // trim(filename)
write (*,'("xl=",es12.4,"yl=",es12.4,"zl=",es12.4)')&
xl,yl,zl
write (*,'("but in file: xl=",es12.4,"yl=",es12.4,"zl=",es12.4)') &
xl_file,yl_file,zl_file
write (*,'(A)') "proceed, with fingers crossed."
endif
endif
!-----------------------------------------------------------------------------
! load the file
!-----------------------------------------------------------------------------
! Initialize HDF5 library and Fortran interfaces.
call h5open_f(error)
! Setup file access property list with parallel I/O access. this
! sets up a property list ("plist_id") with standard values for
! FILE_ACCESS
call H5Pcreate_f(H5P_FILE_ACCESS_F, plist_id, error)
! this modifies the property list and stores MPI IO
! comminucator information in the file access property list
call H5Pset_fapl_mpio_f(plist_id, MPI_COMM_WORLD, MPI_INFO_NULL, error)
! open the file in parallel
call H5Fopen_f (filename, H5F_ACC_RDWR_F, file_id, error, plist_id)
! this closes the property list (we'll re-use it)
call H5Pclose_f(plist_id, error)
! Definition of memory distribution
dimensions_file = (/nx,ny,nz/)
dimensions_local(1) = rb(1)-ra(1) +1
dimensions_local(2) = rb(2)-ra(2) +1
dimensions_local(3) = rb(3)-ra(3) +1
offset(1) = ra(1)
offset(2) = ra(2)
offset(3) = ra(3)
! Each process knows how much data it has and where to store it.
! now, define the dataset chunking. Chunking is largest dimension in
! each direction
do i = 1, 3
call MPI_REDUCE ( dimensions_local(i), chunking_dims(i),1, &
MPI_INTEGER8, MPI_MAX,0,MPI_COMM_WORLD,mpierror)
call MPI_BCAST ( chunking_dims(i), 1, MPI_INTEGER8, 0, &
MPI_COMM_WORLD, mpierror )
enddo
!----------------------------------------------------------------------------
! Read actual field from file (dataset)
!----------------------------------------------------------------------------
! dataspace in the file: contains all data from all procs
call H5Screate_simple_f(rank, dimensions_file, filespace, error)
! dataspace in memory: contains only local data
call H5Screate_simple_f(rank, dimensions_local, memspace, error)
! Create chunked dataset
call H5Pcreate_f(H5P_DATASET_CREATE_F, plist_id, error)
call H5Pset_chunk_f(plist_id, rank, chunking_dims, error)
! Open an existing dataset.
call H5Dopen_f(file_id, dsetname, dset_id, error)
! Select hyperslab in the file.
call H5Dget_space_f(dset_id, filespace, error)
call H5Sselect_hyperslab_f (filespace, H5S_SELECT_SET_F, offset, count, &
error , stride, dimensions_local)
! Create property list for collective dataset read
call H5Pcreate_f(H5P_DATASET_XFER_F, plist_id, error)
call H5Pset_dxpl_mpio_f(plist_id, H5FD_MPIO_COLLECTIVE_F, error)
call H5Dread_f( dset_id, H5T_NATIVE_DOUBLE, field, dimensions_local, error, &
mem_space_id = memspace, file_space_id = filespace, xfer_prp = plist_id )
call H5Sclose_f(filespace, error)
call H5Sclose_f(memspace, error)
call H5Pclose_f(plist_id, error) ! note the dataset remains opened
! Close dataset
call H5Dclose_f(dset_id, error)
call H5Fclose_f(file_id,error)
call H5close_f(error)
end subroutine Read_Single_File
! Load backup data from disk to initialize run for restart
subroutine Read_Runtime_Backup(filename,time,dt0,dt1,n1,it,uk,nlk,explin,work)
use mpi_header
use vars
use hdf5
implicit none
character(len=*),intent(in) :: filename
real(kind=pr),intent(out) :: time,dt1,dt0
integer,intent(out) :: n1,it
complex(kind=pr), intent(out) :: uk(ca(1):cb(1),ca(2):cb(2),ca(3):cb(3),1:nd)
complex(kind=pr),intent(out)::&
nlk(ca(1):cb(1),ca(2):cb(2),ca(3):cb(3),1:nd,0:1)
real(kind=pr),intent(out) :: explin(ca(1):cb(1),ca(2):cb(2),ca(3):cb(3),1:nf)
real(kind=pr),intent(inout) :: work(ra(1):rb(1),ra(2):rb(2),ra(3):rb(3))
integer :: error ! Error flag
integer(hid_t) :: file_id ! File identifier
integer(hid_t) :: plist_id ! Property list identifier
if(mpirank == 0) then
write(*,'("---------")')
write(*,'(A)') "!!! I'm trying to resume a backup file: "//filename
endif
call check_file_exists ( filename )
! Initialize HDF5 library and Fortran interfaces.
call h5open_f(error)
! Setup file access property list with parallel I/O access. this
! sets up a property list ("plist_id") with standard values for
! FILE_ACCESS
call H5Pcreate_f(H5P_FILE_ACCESS_F, plist_id, error)
! this modifies the property list and stores MPI IO
! comminucator information in the file access property list
call H5Pset_fapl_mpio_f(plist_id, MPI_COMM_WORLD, MPI_INFO_NULL, error)
! open the file in parallel
call H5Fopen_f (filename, H5F_ACC_RDWR_F, file_id, error, plist_id)
! this closes the property list (we'll re-use it)
call H5Pclose_f(plist_id, error)
! Read fluid backup field:
call Read_Field_Backup(work,"ux",time,dt0,dt1,n1,it,file_id)
call fft(uk(:,:,:,1),work)
call Read_Field_Backup(work,"uy",time,dt0,dt1,n1,it,file_id)
call fft(uk(:,:,:,2),work)
call Read_Field_Backup(work,"uz",time,dt0,dt1,n1,it,file_id)
call fft(uk(:,:,:,3),work)
if(method == "mhd") then
! Read MHD backup field:
call Read_Field_Backup(work,"bx",time,dt0,dt1,n1,it,file_id)
call fft(uk(:,:,:,4),work)
call Read_Field_Backup(work,"by",time,dt0,dt1,n1,it,file_id)
call fft(uk(:,:,:,5),work)
call Read_Field_Backup(work,"bz",time,dt0,dt1,n1,it,file_id)
call fft(uk(:,:,:,6),work)
endif
! Read fluid nonlinear source term backup:
call Read_Field_Backup(work,"nlkx0",time,dt0,dt1,n1,it,file_id)
call fft(nlk(:,:,:,1,0),work)
call Read_Field_Backup(work,"nlky0",time,dt0,dt1,n1,it,file_id)
call fft(nlk(:,:,:,2,0),work)
call Read_Field_Backup(work,"nlkz0",time,dt0,dt1,n1,it,file_id)
call fft(nlk(:,:,:,3,0),work)
call Read_Field_Backup(work,"nlkx1",time,dt0,dt1,n1,it,file_id)
call fft(nlk(:,:,:,1,1),work)
call Read_Field_Backup(work,"nlky1",time,dt0,dt1,n1,it,file_id)
call fft(nlk(:,:,:,2,1),work)
call Read_Field_Backup(work,"nlkz1",time,dt0,dt1,n1,it,file_id)
call fft(nlk(:,:,:,3,1),work)
if(method == "mhd") then
! Read MHD nonlinear source term backup too:
call Read_Field_Backup(work,"bnlkx0",time,dt0,dt1,n1,it,file_id)
call fft(nlk(:,:,:,4,0),work)
call Read_Field_Backup(work,"bnlky0",time,dt0,dt1,n1,it,file_id)
call fft(nlk(:,:,:,5,0),work)
call Read_Field_Backup(work,"bnlkz0",time,dt0,dt1,n1,it,file_id)
call fft(nlk(:,:,:,6,0),work)
call Read_Field_Backup(work,"bnlkx1",time,dt0,dt1,n1,it,file_id)
call fft(nlk(:,:,:,4,1),work)
call Read_Field_Backup(work,"bnlky1",time,dt0,dt1,n1,it,file_id)
call fft(nlk(:,:,:,5,1),work)
call Read_Field_Backup(work,"bnlkz1",time,dt0,dt1,n1,it,file_id)
call fft(nlk(:,:,:,6,1),work)
endif
call H5Fclose_f(file_id,error)
call H5close_f(error)
! It is important to have explin, because it won't be initialized
! if both time steps dt0 and dt1 match so we compute it here (TOMMY:
! are you sure about dt1??? TODO)
! FIXME: only compute if dt0=dt1?
call cal_vis(dt1,explin)
if(mpirank == 0) then
write(*,'("time=",es15.8," dt0=",es15.8)') time, dt0
write(*,'("!!! DONE READING BACKUP (thats good news!)")')
write(*,'("---------")')
endif
end subroutine Read_Runtime_Backup
! This routine reads a single field "dsetname" from a backup file
! "file_id". the field has the attribute "attributes", which is an 8x1
! array containing scalar backup information
subroutine Read_Field_Backup(field,dsetname,time,dt0,dt1,n1,it,file_id)
use mpi_header
use fsi_vars
use hdf5
implicit none
real(kind=pr),dimension(ra(1):rb(1),ra(2):rb(2),ra(3):rb(3)), &
intent(out) :: field
integer, parameter :: rank = 3 ! data dimensionality (2D or 3D)
real (kind=pr), intent (out) :: time,dt1,dt0
character(len=*), intent (in) :: dsetname
integer,intent(out) :: n1,it
integer :: nx_file,ny_file,nz_file, mpierror, i
integer(hid_t), intent(in) :: file_id ! file identifier
integer(hid_t) :: dset_id ! dataset identifier
integer(hid_t) :: filespace ! dataspace identifier in file
integer(hid_t) :: memspace ! dataspace identifier in memory
integer(hid_t) :: plist_id ! property list identifier
! dataset dimensions in the file.
integer(hsize_t), dimension(rank) :: dimensions_file
integer(hsize_t), dimension(rank) :: dimensions_local ! chunks dimensions
integer(hsize_t), dimension(rank) :: chunking_dims ! chunks dimensions
integer(hsize_t), dimension(rank) :: count = 1
integer(hssize_t), dimension(rank) :: offset
integer(hsize_t), dimension(rank) :: stride = 1
integer :: error ! error flags
! what follows is for the attribute "time"
integer, parameter :: arank = 1
integer(hsize_t), DIMENSION(1) :: adims ! Attribute dimension
integer(hid_t) :: aspace_id ! Attribute Dataspace identifier
!integer(hid_t) :: atype_id ! Attribute Dataspace identifier
integer(hid_t) :: attr_id ! Attribute identifier
character(len=4) :: aname ! attribute name
real (kind=pr), dimension (:), allocatable :: attributes
! Definition of memory distribution
dimensions_file = (/nx,ny,nz/)
dimensions_local(1) = rb(1)-ra(1) +1
dimensions_local(2) = rb(2)-ra(2) +1
dimensions_local(3) = rb(3)-ra(3) +1
offset(1) = ra(1)
offset(2) = ra(2)
offset(3) = ra(3)
! Each process knows how much data it has and where to store it.
! now, define the dataset chunking. Chunking is largest dimension in
! each direction
do i = 1, 3
call MPI_REDUCE ( dimensions_local(i), chunking_dims(i),1, &
MPI_INTEGER8, MPI_MAX,0,MPI_COMM_WORLD,mpierror)
call MPI_BCAST ( chunking_dims(i), 1, MPI_INTEGER8, 0, &
MPI_COMM_WORLD, mpierror )
enddo
!----------------------------------------------------------------------------
! Read actual field from file (dataset)
!----------------------------------------------------------------------------
! dataspace in the file: contains all data from all procs
call H5Screate_simple_f(rank, dimensions_file, filespace, error)