-
Notifications
You must be signed in to change notification settings - Fork 1
/
fsm3d.f90
2183 lines (2172 loc) · 88.7 KB
/
fsm3d.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
! !
!========================================================================================!
! !
!> @brief Solves the eikonal equation using the fast sweeping method described
!> by Zhao (2004) and Detrixhe, Gibou, and Min (2012).
!>
!> @param[in] maxit max number of iterations in fast sweeping method
!> @param[in] nx number of x grid points in domain
!> @param[in] ny number of y grid points in domain
!> @param[in] nz number of z grid points in domain
!> @param[in] tol the iterative method will terminate if the maximum update
!> to the traveltimes from iteration k, to iteration k + 1
!> is less than tol (tol is specified in seconds)
!> @param[in] lstruct level structure corresponding to the Cuthill-Mckee ordering
!> @param[in] lupd if true then update the i'th node. otherwise, the input
!> nodal value will not be altered
!>
!> @param[inout] u on input contains the initialized traveltimes at the
!> boundary conditions.
!> on exit contains the traveltimes to all nodes in the grid
!>
!> @param[out] ierr 0 indicates success
!>
!> @author Ben Baker
!>
!> @copyright Apache 2 license
!>
SUBROUTINE EIKONAL3D_FSM(iverb, maxit, nx, ny, nz, h, tol, &
lstruct, lupd, slow, &
u, ierr)
USE EIKONAL3D_TYPES, ONLY : levelType
USE EIKONAL3D_MODULE, ONLY : EVAL_UPDATE3D
IMPLICIT NONE
TYPE(levelType), INTENT(IN) :: lstruct
DOUBLE PRECISION, DIMENSION(:), INTENT(IN) :: slow
DOUBLE PRECISION, INTENT(IN) :: h, tol
INTEGER, INTENT(IN) :: iverb, maxit, nx, ny, nz
LOGICAL, DIMENSION(:), INTENT(IN) :: lupd
INTEGER, INTENT(OUT) :: ierr
DOUBLE PRECISION, DIMENSION(:), INTENT(INOUT) :: u
! local variables
DOUBLE PRECISION, ALLOCATABLE :: u0(:)
DOUBLE PRECISION t1, t2
INTEGER i, i1, i2, isweep, k, lconv, level, nnl, nxyz
LOGICAL lrevx, lrevy, lrevz
LOGICAL, PARAMETER :: evalSweep(24) = [.FALSE., .FALSE., .FALSE., &
.TRUE., .FALSE., .FALSE., &
.FALSE., .TRUE., .FALSE., &
.TRUE., .TRUE., .FALSE., &
.FALSE., .FALSE., .TRUE., &
.TRUE., .FALSE., .TRUE., &
.FALSE., .TRUE., .TRUE., &
.TRUE., .TRUE., .TRUE.]
!----------------------------------------------------------------------------------!
!
! initialize
ierr = 0
nxyz = nx*ny*nz
ALLOCATE(u0(nxyz))
u0(:) = u(:)
! loop on the iterations
DO 1000 k=1,maxit
IF (iverb > 2) THEN
WRITE(*,*) 'eikonal_fsm: Beginning sweep:', k
ENDIF
CALL CPU_TIME(t1)
! apply the sweeps
DO 2000 isweep=1,8
lrevx = evalSweep(3*(isweep-1)+1)
lrevy = evalSweep(3*(isweep-1)+2)
lrevz = evalSweep(3*(isweep-1)+3)
! for each level perform the parallel update
DO 3000 level=1,lstruct%nlevels
nnl = lstruct%nlevels
i1 = lstruct%level_ptr(level)
i2 = lstruct%level_ptr(level+1) - 1
nnl = lstruct%nnl(level)
CALL EVAL_UPDATE3D(nx, ny, nz, &
lrevx, lrevy, lrevz, &
h, nnl, lstruct%ixyz_level(i1:i2), &
lupd, slow, &
u, ierr)
3000 CONTINUE
2000 CONTINUE
! test the convergence of the iteration
lconv = 0
DO 1005 i=1,nxyz
IF (ABS(u0(i) - u(i)) < tol) lconv = lconv + 1
u0(i) = u(i)
1005 CONTINUE
IF (iverb > 2) THEN
CALL CPU_TIME(t2)
WRITE(*,*) 'eikononal_fsm: Sweep time:', t2 - t1
ENDIF
IF (lconv == nxyz) EXIT
1000 CONTINUE
IF (ALLOCATED(u0)) DEALLOCATE(u0)
RETURN
END SUBROUTINE EIKONAL3D_FSM
! !
!========================================================================================!
! !
SUBROUTINE EIKONAL3D_FSM_MPI(sweepComm, blockComm, &
iverb, maxit, &
nx_loc, ny_loc, nz_loc, &
h, tol, &
lstruct, ghosts, &
lghost_node, lupd_loc, &
slow_loc, uloc, ierr)
USE MPI
USE EIKONAL3D_MODULE, ONLY : EIKONAL_EXCHANGE, EVAL_UPDATE3D
USE EIKONAL3D_TYPES, ONLY : ghostType, levelType
IMPLICIT NONE
TYPE(levelType), INTENT(IN) :: lstruct
TYPE(ghostType), DIMENSION(:), INTENT(INOUT) :: ghosts
DOUBLE PRECISION, DIMENSION(:), INTENT(IN) :: slow_loc
DOUBLE PRECISION, INTENT(IN) :: h, tol
LOGICAL, DIMENSION(:), INTENT(IN) :: lghost_node, lupd_loc
INTEGER, INTENT(IN) :: blockComm, sweepComm, iverb, maxit, nx_loc, ny_loc, nz_loc
DOUBLE PRECISION, DIMENSION(:), INTENT(INOUT) :: uloc
INTEGER, INTENT(OUT) :: ierr
! local variables
DOUBLE PRECISION, ALLOCATABLE :: u0loc(:)
DOUBLE PRECISION t1, t2
INTEGER i, i1, i2, isweep, k, ksweep, level, mpierr, &
my_blockid, my_sweepid, myid, nblocks, nconv, nnl, &
nxyz, nxyz_loc, nbuf, nsg
LOGICAL lconv, lrevx, lrevy, lrevz
INTEGER, PARAMETER :: master = 0
LOGICAL, PARAMETER :: evalSweep(24) = [.FALSE., .FALSE., .FALSE., &
.TRUE., .FALSE., .FALSE., &
.FALSE., .TRUE., .FALSE., &
.TRUE., .TRUE., .FALSE., &
.FALSE., .FALSE., .TRUE., &
.TRUE., .FALSE., .TRUE., &
.FALSE., .TRUE., .TRUE., &
.TRUE., .TRUE., .TRUE.]
!----------------------------------------------------------------------------------!
!
! get MPI information
CALL MPI_COMM_SIZE(blockComm, nblocks, mpierr) ! number of blocks in domain
CALL MPI_COMM_RANK(blockComm, my_blockid, mpierr) ! my block number
CALL MPI_COMM_SIZE(sweepComm, nsg, mpierr) ! number of sweep groups
! CALL MPI_COMM_RANK(sweepComm, my_sweepid, mpierr) ! my sweep number
nsg = 1
my_sweepid = 0
myid = my_blockid + my_sweepid
nxyz_loc = nx_loc*ny_loc*nz_loc
nbuf = 0
DO i=1,nxyz_loc
IF (.NOT.lghost_node(i)) nbuf = nbuf + 1
ENDDO
CALL MPI_ALLREDUCE(nbuf, nxyz, 1, MPI_INTEGER, MPI_SUM, blockComm, mpierr)
ALLOCATE(u0loc(MAX(nxyz_loc, 1)))
u0loc(:) = uloc(:)
lconv = .FALSE.
! loop on max iterations
DO 1000 k=1,maxit
IF (myid == master .AND. iverb > 2) THEN
WRITE(*,*) 'eikonal3d_fsm_mpi: Beginning iteration:', k
ENDIF
t1 = MPI_WTIME()
! parallel loop on sweeps
DO 2000 ksweep=1,8,nsg
isweep = (ksweep - 1)*nsg + my_sweepid + 1
IF (isweep > 8) GOTO 2500
! scatter grid
IF (nsg > 1) THEN
ENDIF
lrevx = evalSweep(3*(isweep-1)+1)
lrevy = evalSweep(3*(isweep-1)+2)
lrevz = evalSweep(3*(isweep-1)+3)
! for each level perform the parallel update
DO 3000 level=1,lstruct%nlevels
! evaluate the sweep
nnl = lstruct%nlevels
i1 = lstruct%level_ptr(level)
i2 = lstruct%level_ptr(level+1) - 1
nnl = lstruct%nnl(level)
CALL EVAL_UPDATE3D(nx_loc, ny_loc, nz_loc, &
lrevx, lrevy, lrevz, &
h, nnl, lstruct%ixyz_level(i1:i2), &
lupd_loc, slow_loc, &
uloc, ierr)
! swap with neighbors
3000 CONTINUE
CALL EIKONAL_EXCHANGE(blockComm, ghosts, uloc)
2500 CONTINUE ! get next sweep
! take the minimum from each grid
IF (nsg > 1) THEN
! ubuff(:) = uloc(:)
! MPI_ALLREDUCE(ubuff, u, nx_loc*ny_loc*nz_loc, &
! MPI_DOUBLE_PRECISION, MPI_MIN, sweepComm)
ENDIF
2000 CONTINUE
! convergence test
nbuf = 0
DO 11 i=1,nxyz_loc
IF (.NOT.lghost_node(i) .AND. ABS(uloc(i) - u0loc(i)) < tol) nbuf = nbuf + 1
u0loc(i) = uloc(i)
11 CONTINUE
CALL MPI_ALLREDUCE(nbuf, nconv, 1, MPI_INTEGER, MPI_SUM, blockComm, mpierr)
t2 = MPI_WTIME()
IF (myid == master .AND. iverb > 2) THEN
WRITE(*,*) 'eikonal3d_fsm_mpi: Sweep time (s)', t2 - t1
ENDIF
IF (nconv == nxyz) THEN
lconv = .TRUE.
EXIT
ENDIF
1000 CONTINUE
IF (myid == master .AND. iverb > 2) THEN
IF (lconv) THEN
WRITE(*,*) 'eikonal3d_fsm_mpi: Number of iterations for convergence:', k
ELSE
WRITE(*,*) 'eikonal3d_fsm_mpi: Failed to converge after iterations', maxit
ENDIF
ENDIF
DEALLOCATE(u0loc)
RETURN
END
! !
!========================================================================================!
! !
SUBROUTINE MAKE_LEVEL_STRUCT(nx, ny, nz, lstruct, ierr)
USE EIKONAL3D_TYPES, ONLY : levelType
IMPLICIT NONE
INTEGER, INTENT(IN) :: nx, ny, nz
TYPE(levelType), INTENT(OUT) :: lstruct
! local variables
INTEGER, ALLOCATABLE :: linit(:)
TYPE two2d_array
INTEGER, ALLOCATABLE :: dim2(:)
END TYPE
TYPE(two2d_array), ALLOCATABLE :: ixyz_level(:)
INTEGER, INTENT(OUT) :: ierr
INTEGER i1, i2, ijk, ix, iy, iz, j1, j2, k1, k2, level, np
ierr = 0
lstruct%nlevels = nx + ny + nz - 2
ALLOCATE(lstruct%level_ptr(lstruct%nlevels+1))
ALLOCATE(lstruct%nnl(lstruct%nlevels))
ALLOCATE(ixyz_level(lstruct%nlevels))
lstruct%level_ptr(:) = 0
ALLOCATE(linit(nx*ny*nz))
lstruct%level_ptr(1) = 1
linit(:) = 0
DO 1 level=2,nx+ny+nz-1
k1 = MAX(0, level - 2 - (nx - 1) - (ny - 1))
k1 = MIN(k1, nz - 1)
k2 = MIN(nz - 1, level - 2)
np = 0
DO 2 iz=k1,k2
j1 = MAX(0, level - 2 - iz - (nx - 1))
j1 = MIN(j1, ny - 1)
j2 = MIN(ny - 1, level - 2 - iz)
DO 3 iy=j1,j2
i1 = MAX(0, level - 2 - iz - iy)
i1 = MIN(i1, nx - 1)
i2 = MIN(nx - 1, level - 2 - iz - iy)
DO 4 ix=i1,i2
np = np + 1
ijk = iz*nx*ny + iy*nx + ix + 1
linit(ijk) = linit(ijk) + 1
4 CONTINUE
3 CONTINUE
2 CONTINUE
lstruct%nnl(level-1) = np
lstruct%level_ptr(level) = lstruct%level_ptr(level-1) + 3*np
IF (.NOT.ALLOCATED(ixyz_level(level-1)%dim2)) THEN
ALLOCATE(ixyz_level(level-1)%dim2(3*np))
ENDIF
np = 0
DO 11 iz=k1,k2
j1 = MAX(0, level - 2 - iz - (nx - 1))
j1 = MIN(j1, ny - 1)
j2 = MIN(ny - 1, level - 2 - iz)
DO 12 iy=j1,j2
i1 = MAX(0, level - 2 - iz - iy)
i1 = MIN(i1, nx - 1)
i2 = MIN(nx - 1, level - 2 - iz - iy)
DO 13 ix=i1,i2
np = np + 1
ixyz_level(level-1)%dim2(3*(np-1)+1) = ix + 1
ixyz_level(level-1)%dim2(3*(np-1)+2) = iy + 1
ixyz_level(level-1)%dim2(3*(np-1)+3) = iz + 1
13 CONTINUE
12 CONTINUE
11 CONTINUE
1 CONTINUE
ALLOCATE(lstruct%ixyz_level(lstruct%level_ptr(lstruct%nlevels+1)-1))
lstruct%ixyz_level(:) = 0
! copy the level structure
DO 21 level=2,nx+ny+nz-1
i1 = lstruct%level_ptr(level-1)
i2 = lstruct%level_ptr(level) - 1
lstruct%ixyz_level(i1:i2) = ixyz_level(level-1)%dim2(:)
DEALLOCATE(ixyz_level(level-1)%dim2)
21 CONTINUE
IF (MINVAL(linit) < 1 .OR. MAXVAL(linit) > 1) THEN
WRITE(*,*) 'Failed to initialize node'
ierr = 1
ENDIF
DEALLOCATE(ixyz_level)
DEALLOCATE(linit)
RETURN
END
! !
!========================================================================================!
! !
!> @brief Frees the level structure
!>
!> @param[inout] level on input this is the level structure.
!> on output all memory has been freed from level.
!>
!> @author Ben Baker
!>
!> @copyright Apache 2 license
!>
SUBROUTINE FREE_LEVEL_STRUCT(level)
USE EIKONAL3D_TYPES, ONLY : levelType
IMPLICIT NONE
TYPE(levelType), INTENT(INOUT) :: level
IF (ALLOCATED(level%ixyz_level)) DEALLOCATE(level%ixyz_level)
IF (ALLOCATED(level%nnl)) DEALLOCATE(level%nnl)
IF (ALLOCATED(level%level_ptr)) DEALLOCATE(level%level_ptr)
level%nlevels = 0
RETURN
END
! !
!========================================================================================!
! !
!> @brief Frees the memory on the local model structure
!>
!> @param[inout] model_loc on input this is the local model structure.
!> on output all memroy has bene freed from model_loc.
!>
!> @author Ben Baker
!>
SUBROUTINE FREE_LOCAL_MODEL_STRUCT(model_loc)
USE EIKONAL3D_TYPES, ONLY : localModelType
USE EIKONAL3D_MODULE, ONLY : FREE_LEVEL_STRUCT
IMPLICIT NONE
TYPE(localModelType), INTENT(INOUT) :: model_loc
CALL FREE_LEVEL_STRUCT(model_loc%lstruct)
IF (ALLOCATED(model_loc%slow)) DEALLOCATE(model_loc%slow)
IF (ALLOCATED(model_loc%u)) DEALLOCATE(model_loc%u)
IF (ALLOCATED(model_loc%l2g_node)) DEALLOCATE(model_loc%l2g_node)
IF (ALLOCATED(model_loc%lghost_node)) DEALLOCATE(model_loc%lghost_node)
IF (ALLOCATED(model_loc%lupd)) DEALLOCATE(model_loc%lupd)
model_loc%nx = 0
model_loc%ny = 0
model_loc%nz = 0
model_loc%nxyz = 0
model_loc%myblock = 0
RETURN
END
! !
!========================================================================================!
! !
!> @brief Frees the ghost communication structure
!>
!> @param[inout] ghosts on input contains the ghost structure.
!> on output all memory freed from the ghost structure.
!>
!> @author Ben Baker
!>
!> @copyright Apache 2 license
!>
SUBROUTINE FREE_GHOST_STRUCT(ghosts)
USE EIKONAL3D_TYPES, ONLY : ghostType
IMPLICIT NONE
TYPE(ghostType), INTENT(INOUT), ALLOCATABLE :: ghosts(:)
INTEGER i
IF (ALLOCATED(ghosts)) THEN
DO 1 i=1,SIZE(ghosts)
IF (ALLOCATED(ghosts(i)%send_buff)) DEALLOCATE(ghosts(i)%send_buff)
IF (ALLOCATED(ghosts(i)%recv_buff)) DEALLOCATE(ghosts(i)%recv_buff)
IF (ALLOCATED(ghosts(i)%isend_dest)) DEALLOCATE(ghosts(i)%isend_dest)
IF (ALLOCATED(ghosts(i)%irecv_dest)) DEALLOCATE(ghosts(i)%irecv_dest)
ghosts(i)%nsend = 0
ghosts(i)%nrecv = 0
ghosts(i)%mysrc = 0
ghosts(i)%mydest = 0
ghosts(i)%send_request = 0
ghosts(i)%recv_request = 0
ghosts(i)%lrecv = .FALSE.
1 CONTINUE
DEALLOCATE(ghosts)
ENDIF
RETURN
END
! !
!========================================================================================!
! !
!> @brief Updates all nodes in a level
!>
!> @param[in] nx number of x grid points in domain
!> @param[in] ny number of y grid points in domain
!> @param[in] nz number of z grid points in domain
!> @param[in] lrevx if true then the x index is to be reversed (ix <- nx + 1 - ix)
!> @param[in] lrevy if true then the y index is to be reversed (iy <- ny + 1 - iy)
!> @param[in] lrevz if true then the z index is to be reversed (iz <- nz + 1 - iz)
!> @param[in] lupd if true then the i'th node will be updated [nx*ny*nz]
!> @param[in] slow slowness (s/m) at each grid points [nx*ny*nz]
!>
!> @param[inout] u on input these are the traveltimes (s) at each grid point in
!> the level.
!> on output these are the new traveltimes at each grid point
!> in the level [nx*ny*nz]
!>
!> @param[out] ierr 0 indicates success
!>
!> @author Ben Baker
!>
!> @copyright Apache 2 license
!>
SUBROUTINE EVAL_UPDATE3D(nx, ny, nz, &
lrevx, lrevy, lrevz, &
h, nnl, ixyz_level, &
lupd, slow, &
u, ierr)
USE EIKONAL3D_MODULE, ONLY : UPDATE3D
IMPLICIT NONE
DOUBLE PRECISION, INTENT(IN) :: h
INTEGER, INTENT(IN) :: nnl, nx, ny, nz
LOGICAL, INTENT(IN) :: lrevx, lrevy, lrevz
DOUBLE PRECISION, INTENT(IN), DIMENSION(:) :: slow
INTEGER, INTENT(IN), DIMENSION(:) :: ixyz_level
LOGICAL, INTENT(IN), DIMENSION(:) :: lupd
DOUBLE PRECISION, INTENT(INOUT), DIMENSION(:) :: u
INTEGER, INTENT(OUT) :: ierr
! local variables
INTEGER ierr_all, ierr1, ijk, ip, ix, iy, iz
ierr_all = 0
!$OMP PARALLEL DO DEFAULT(none) &
!$OMP PRIVATE(ijk, ierr1, ix, iy, iz) &
!$OMP SHARED(h, ixyz_level, lrevx, lrevy, lrevz, lupd, nnl, nx, ny, nz, slow, u) &
!$OMP REDUCTION(+:ierr_all)
DO 1 ip=1,nnl
ix = ixyz_level(3*(ip-1)+1)
iy = ixyz_level(3*(ip-1)+2)
iz = ixyz_level(3*(ip-1)+3)
IF (lrevx) ix = nx + 1 - ix
IF (lrevy) iy = ny + 1 - iy
IF (lrevz) iz = nz + 1 - iz
ijk = (iz - 1)*nx*ny + (iy - 1)*nx + ix
IF (lupd(ijk)) THEN
CALL UPDATE3D(nx, ny, nz, ix, iy, iz, h, slow, u, ierr1)
ierr_all = ierr_all + ierr1
ENDIF
1 CONTINUE ! loop on levels
ierr = ierr_all
RETURN
END
! !
!========================================================================================!
! !
SUBROUTINE UPDATE3D(nx, ny, nz, ix, iy, iz, h, slow, u, ierr)
USE EIKONAL3D_MODULE, ONLY : GET_UXMIN3D, GET_UYMIN3D, GET_UZMIN3D, &
SOLVE_HAMILTONIAN3D
IMPLICIT NONE
DOUBLE PRECISION, DIMENSION(:), INTENT(IN) :: slow
INTEGER, INTENT(IN) :: ix, iy, iz, nx, ny, nz
DOUBLE PRECISION, INTENT(IN) :: h
DOUBLE PRECISION, DIMENSION(:), INTENT(INOUT) :: u
INTEGER, INTENT(OUT) :: ierr
DOUBLE PRECISION fijkh, ubar, uxmin, uymin, uzmin
INTEGER ijk
ierr = 0
ijk = (iz - 1)*nx*ny + (iy - 1)*nx + ix
fijkh = slow(ijk)*h
uxmin = GET_UXMIN3D(nx, ny, ix, iy, iz, u)
uymin = GET_UYMIN3D(nx, ny, ix, iy, iz, u)
uzmin = GET_UZMIN3D(nx, ny, nz, ix, iy, iz, u)
ubar = SOLVE_HAMILTONIAN3D(uxmin, uymin, uzmin, fijkh, ierr)
u(ijk) = MIN(u(ijk), ubar)
RETURN
END
! !
!========================================================================================!
! !
DOUBLE PRECISION FUNCTION GET_UXMIN3D(nx, ny, ix, iy, iz, u)
IMPLICIT NONE
DOUBLE PRECISION, DIMENSION(:), INTENT(IN) :: u
INTEGER, INTENT(IN) :: ix, iy, iz, nx, ny
INTEGER ijk, im, ip
ijk = (iz - 1)*nx*ny + (iy - 1)*nx + ix
im = ijk - 1
ip = ijk + 1
IF (ix > 1 .AND. ix < nx) THEN
get_uxmin3d = MIN(u(im), u(ip))
ELSE
IF (ix == 1) THEN
get_uxmin3d = MIN(u(ijk), u(ip))
ELSE
get_uxmin3d = MIN(u(im), u(ijk))
ENDIF
ENDIF
RETURN
END
! !
!========================================================================================!
! !
DOUBLE PRECISION FUNCTION GET_UYMIN3D(nx, ny, ix, iy, iz, u)
IMPLICIT NONE
DOUBLE PRECISION, DIMENSION(:), INTENT(IN) :: u
INTEGER, INTENT(IN) :: ix, iy, iz, nx, ny
INTEGER ijk, jm, jp
ijk = (iz - 1)*nx*ny + (iy - 1)*nx + ix
jm = ijk - nx
jp = ijk + nx
IF (iy > 1 .AND. iy < ny) THEN
get_uymin3d = MIN(u(jm), u(jp))
ELSE
IF (iy == 1) THEN
get_uymin3d = MIN(u(ijk), u(jp))
ELSE
get_uymin3d = MIN(u(jm), u(ijk))
ENDIF
ENDIF
RETURN
END
! !
!========================================================================================!
! !
DOUBLE PRECISION FUNCTION GET_UZMIN3D(nx, ny, nz, ix, iy, iz, u)
IMPLICIT NONE
DOUBLE PRECISION, DIMENSION(:), INTENT(IN) :: u
INTEGER, INTENT(IN) :: ix, iy, iz, nx, ny, nz
INTEGER ijk, km, kp
ijk = (iz - 1)*nx*ny + (iy - 1)*nx + ix
km = ijk - nx*ny
kp = ijk + nx*ny
IF (iz > 1 .AND. iz < nz) THEN
get_uzmin3d = MIN(u(km), u(kp))
ELSE
IF (iz == 1) THEN
get_uzmin3d = MIN(u(ijk), u(kp))
ELSE
get_uzmin3d = MIN(u(km), u(ijk))
ENDIF
ENDIF
RETURN
END
! !
!========================================================================================!
! !
!> @brief Sorts three numbers, a, b, c, into ascending order a1, a2, a3
!>
!> @param[in] a first number to sort
!> @param[in] b second number to sort
!> @param[in] c third number to sort
!>
!> @param[out] a1 smallest of a, b, and c
!> @param[out] a2 intermediate value of a, b, and c
!> @param[out] a3 largest of a, b, and c
!>
!> @copyright Apache 2 license
!>
SUBROUTINE SORT3(a, b, c, a1, a2, a3)
IMPLICIT NONE
DOUBLE PRECISION, INTENT(IN) :: a, b, c
DOUBLE PRECISION, INTENT(OUT) :: a1, a2, a3
LOGICAL lab, lac, lbc
!----------------------------------------------------------------------------------!
!
! compute comparisons
lab = .TRUE.
lac = .TRUE.
lbc = .TRUE.
IF (a > b) lab = .FALSE.
IF (a > c) lac = .FALSE.
IF (b > c) lbc = .FALSE.
! sort input - a is the smallest
IF (lab .AND. lac) THEN !a <= b .AND. a <= c) THEN
! a <= b <= c
IF (lbc) THEN !b <= c) THEN
a1 = a
a2 = b
a3 = c
ELSE ! a <= c <= b
a1 = a
a2 = c
a3 = b
ENDIF
! b is the smallest
ELSEIF (.NOT. lab .AND. lbc) THEN !b <= a .AND. b <= c) THEN
! b <= a <= c
IF (lac) THEN !a <= c) THEN
a1 = b
a2 = a
a3 = c
ELSE ! b <=c <= a
a1 = b
a2 = c
a3 = a
ENDIF
! c is the smallest
ELSE
! c <= a <= b
IF (lab) THEN !a <= b) THEN
a1 = c
a2 = a
a3 = b
ELSE ! c <= b <= a
a1 = c
a2 = b
a3 = a
ENDIF
ENDIF
RETURN
END
! !
!========================================================================================!
! !
!> @brief Solves the 2D Hamiltonian (Equation 2.4 of Zhao 2004)
!>
!> @author Ben Baker
!>
!> @copyright Apache 2 license
!>
DOUBLE PRECISION FUNCTION SOLVE_HAMILTONIAN2D(a, b, fijh)
USE EIKONAL3D_MODULE, ONLY : half, two
IMPLICIT NONE
DOUBLE PRECISION, INTENT(IN) :: a, b, fijh
DOUBLE PRECISION xbar, amb, arg
amb = a - b
IF (ABS(amb) < fijh) THEN
arg = two*fijh*fijh - amb*amb
xbar = half*(a + b + SQRT(arg))
ELSE
xbar = MIN(a, b) + fijh
ENDIF
solve_hamiltonian2D = xbar
RETURN
END
! !
!========================================================================================!
! !
!> @brief Solves the 3D Hamiltonian (Equation 2.5 and 2.6 of Zhao 2004.)
!>
!> @author Ben Baker
!>
!> @copyright Apache 2 license
!>
DOUBLE PRECISION FUNCTION SOLVE_HAMILTONIAN3D(a, b, c, fijkh, ierr)
USE EIKONAL3D_MODULE, ONLY : SOLVE_HAMILTONIAN2D, four, half, u_nan, &
third, two_third, zero
IMPLICIT NONE
DOUBLE PRECISION, INTENT(IN) :: a, b, c, fijkh
INTEGER, INTENT(OUT) :: ierr
DOUBLE PRECISION xtilde, a1, a2, a3, qb, qc, disc
DOUBLE PRECISION, PARAMETER :: a4 = u_nan
ierr = 0
solve_hamiltonian3d = u_nan
! Order a, b, and c
CALL SORT3(a, b, c, a1, a2, a3)
! Check on type of estimate solution we could find before working
IF (a1 == u_nan) RETURN
! p == 1
xtilde = a1 + fijkh
IF (xtilde > a2) THEN
! Set p == 2 and solve (x - a1)^2 + (x - a2)^2 = fij^2 h^2
xtilde = SOLVE_HAMILTONIAN2D(a1, a2, fijkh)
IF (xtilde > a3) THEN
! Set p == 3 and solve
! (x - a1)^2 + (x - a2)^2 + (x - a3)^2 = fij^2 h^2
! 3x**2 - 2x(a1 + a2 + a3) + a1^2 + a2^2 + a3^3 - fij^2 h^2 = 0
qb =-two_third*(a1 + a2 + a3)
qc = (a1*a1 + a2*a2 + a3*a3 - fijkh*fijkh)*third
disc = qb*qb - four*qc
! Complex root
IF (disc < zero) ierr = 1
xtilde = half*(-qb + SQRT(disc))
! Negative traveltime
IF (xtilde < zero) ierr = 2
IF (xtilde < a4) THEN
solve_hamiltonian3d = xtilde
RETURN
ENDIF
ELSE
solve_hamiltonian3d = xtilde
RETURN
ENDIF
ELSE
solve_hamiltonian3d = xtilde
RETURN
ENDIF
ierr = 3
RETURN
END
! !
!========================================================================================!
! !
SUBROUTINE EIKONAL_SOURCE_INDEX(nx, x0, dx, xs, isx)
USE EIKONAL3D_MODULE, ONLY : half
IMPLICIT NONE
DOUBLE PRECISION, INTENT(IN) :: x0, dx, xs
INTEGER, INTENT(IN) :: nx
INTEGER, INTENT(OUT) :: isx
IF (xs <= x0) THEN
isx = 1
ELSEIF (xs >= x0 + FLOAT(nx - 1)*dx) THEN
isx = nx
ELSE
isx = INT( (xs - x0)/dx + half ) + 1
ENDIF
RETURN
END
! !
!========================================================================================!
! !
SUBROUTINE EIKONAL_INIT_GRID(nx, isx, x0, dx, xs, ixloc, ierr)
IMPLICIT NONE
DOUBLE PRECISION, INTENT(IN) :: x0, dx, xs
INTEGER, INTENT(IN) :: nx, isx
INTEGER, INTENT(OUT) :: ixloc(3), ierr
DOUBLE PRECISION xs_est
INTEGER i, npinit
ierr = 0
ixloc(:) =-1
xs_est = x0 + FLOAT(isx - 1)*dx
IF (xs_est > xs) THEN
npinit = 2
ixloc(1) = isx - 1
ixloc(2) = isx
ELSE IF (xs_est < xs) THEN
npinit = 2
ixloc(1) = isx
ixloc(2) = isx + 1
ELSE
npinit = 0
IF (isx > 0) THEN
npinit = npinit + 1
ixloc(npinit) = isx - 1
ENDIF
npinit = npinit + 1
ixloc(npinit) = isx
IF (isx < nx - 1) THEN
npinit = npinit + 1
ixloc(npinit) = isx + 1
ENDIF
ENDIF
! make sure source is in bounds
DO 1 i=1,npinit
IF (ixloc(i) < 1 .OR. ixloc(i) > nx) THEN
WRITE(*,*) 'Source initialization error'
ierr = 1
ENDIF
1 CONTINUE
RETURN
END
! !
!========================================================================================!
! !
!> @brief Sets the boundary conditions (sources) and traveltimes in an around
!> the source
SUBROUTINE EIKONAL3D_SETBCS(nx, ny, nz, nsrc, &
dx, dy, dz, x0, y0, z0, &
ts, xs, ys, zs, slow, &
lisbc, u, ierr)
USE EIKONAL3D_MODULE, ONLY : EIKONAL_INIT_GRID, EIKONAL_SOURCE_INDEX, u_nan
IMPLICIT NONE
DOUBLE PRECISION, DIMENSION(:), INTENT(IN) :: slow, ts, xs, ys, zs
DOUBLE PRECISION, INTENT(IN) :: dx, dy, dz, x0, y0, z0
INTEGER, INTENT(IN) :: nx, ny, nz, nsrc
DOUBLE PRECISION, DIMENSION(:), INTENT(OUT) :: u
LOGICAL, DIMENSION(:), INTENT(OUT) :: lisbc
INTEGER, INTENT(OUT) :: ierr
! local variables
INTEGER, ALLOCATABLE :: isx(:), isy(:), isz(:)
DOUBLE PRECISION d, x, y, z
INTEGER ixloc(3), iyloc(3), izloc(3), isrc, i, ijk, ix, iy, iz, j, k
ALLOCATE(isx(nsrc))
ALLOCATE(isy(nsrc))
ALLOCATE(isz(nsrc))
ierr = 0
lisbc(:) = .FALSE.
u(:) = u_nan
! locate closest source in grid
DO 1 isrc=1,nsrc
CALL EIKONAL_SOURCE_INDEX(nx, x0, dx, xs(isrc), isx(isrc))
CALL EIKONAL_SOURCE_INDEX(ny, y0, dy, ys(isrc), isy(isrc))
CALL EIKONAL_SOURCE_INDEX(nz, z0, dz, zs(isrc), isz(isrc))
1 CONTINUE
! compute the times around the source
ixloc(:) =-1
iyloc(:) =-1
izloc(:) =-1
DO 11 isrc=1,nsrc
CALL EIKONAL_INIT_GRID(nx, isx(isrc), x0, dx, xs(isrc), ixloc, ierr)
IF (ierr /= 0) THEN
WRITE(*,*) 'eikonal3d_setbcs: Error setting ixloc'
GOTO 500
ENDIF
CALL EIKONAL_INIT_GRID(ny, isy(isrc), y0, dy, ys(isrc), iyloc, ierr)
IF (ierr /= 0) THEN
WRITE(*,*) 'eikonal3d_setbcs: Error setting iyloc'
GOTO 500
ENDIF
CALL EIKONAL_INIT_GRID(nz, isz(isrc), z0, dz, zs(isrc), izloc, ierr)
IF (ierr /= 0) THEN
WRITE(*,*) 'eikonal3d_setbcs: Error setting izloc'
GOTO 500
ENDIF
DO 12 i=1,3
IF (ixloc(i) ==-1) CYCLE
DO 13 j=1,3
IF (iyloc(j) ==-1) CYCLE
DO 14 k=1,3
IF (izloc(k) ==-1) CYCLE
ix = ixloc(i)
iy = iyloc(j)
iz = izloc(k)
ijk = (iz - 1)*nx*ny + (iy - 1)*nx + ix
x = x0 + FLOAT(ix - 1)*dx
y = y0 + FLOAT(iy - 1)*dy
z = z0 + FLOAT(iz - 1)*dz
d = SQRT( (xs(isrc) - x)**2 + (ys(isrc) - y)**2 + (zs(isrc) - z)**2 )
! collocate
IF (ABS(d) < 1.d-10) THEN
u(ijk) = ts(isrc) + d*slow(ijk)
ELSE ! take minimum
u(ijk) = MIN(u(ijk), ts(isrc) + d*slow(ijk))
ENDIF
lisbc(ijk) = .TRUE.
14 CONTINUE
13 CONTINUE
12 CONTINUE
11 CONTINUE
500 CONTINUE
IF (ALLOCATED(isx)) DEALLOCATE(isx)
IF (ALLOCATED(isy)) DEALLOCATE(isy)
IF (ALLOCATED(isz)) DEALLOCATE(isz)
RETURN
END
! !
!========================================================================================!
! !
!> @brief Convenience function - when given a grid point returns the (i,jk) index in
!> index in (x, y, z) such that: igrd = (k-1)*nx*ny + (j-1)*nx + i
!>
!> @param[in] igrd grid point number [1,nx*ny*nz]
!> @param[in] nx number of x grid points in domain
!> @param[in] ny number of y grid points in domain
!> @param[in] nz number of z grid points in domain
!>
!> @param[out] i corresponding i grid point in x
!> @param[out] j corresponding j grid point in y
!> @param[out] k corresponding k grid point in z
!> @param[out] ierr 0 indicates success
!>
!> @author Ben Baker
!>
!> @copyright Apache 2 license
!>
SUBROUTINE EIKONAL_GRD2IJK(igrd, nx, ny, nz, i, j, k, ierr)
IMPLICIT NONE
INTEGER, INTENT(IN) :: igrd, nx, ny, nz
INTEGER, INTENT(OUT) :: i, j, k, ierr
INTEGER igrd1, nxy
ierr = 0
igrd1 = igrd - 1
nxy = nx*ny
k = igrd1/(nxy)
j = (igrd1 - k*nxy)/nx
i = igrd1 - k*nxy - j*nx
i = i + 1
j = j + 1
k = k + 1
IF (i < 1 .OR. i > nx) ierr = ierr + 1
IF (j < 1 .OR. j > ny) ierr = ierr + 1
IF (k < 1 .OR. k > nz) ierr = ierr + 1
IF ((k - 1)*nxy + (j - 1)*nx + i /= igrd) ierr = ierr + 1
RETURN
END
! !
!========================================================================================!
! !
!> @brief Exchanges values in the ghosts communication region. This is a blocking
!> version and should only be used for testing.
!>
!> @param[in] comm MPI communicator
!>
!> @param[inout] ghosts on input contains the sending and recieving information
!> and preallocated workspace for this process to all other
!> processes in communicator.
!> on output the buffers have been filled but they should
!> not be accessed by another subroutine. [mpi communicator size]
!> @param[inout] u on input contains the nodes this process has updated.
!> on output contains the ndoes this process needs and others
!> have updated.
!>
!> @author Ben Baker
!>
!> @copyright Apache 2 license
!>
SUBROUTINE EIKONAL_EXCHANGE_BLOCKING(comm, ghosts, u)
USE EIKONAL3D_TYPES, ONLY : ghostType
USE MPI
IMPLICIT NONE
INTEGER, INTENT(IN) :: comm
TYPE(ghostType), DIMENSION(:), INTENT(INOUT) :: ghosts
DOUBLE PRECISION, DIMENSION(:), INTENT(INOUT) :: u
! local variables
INTEGER stat(MPI_STATUS_SIZE), i, j, k, mpierr, myid, nblocks
!----------------------------------------------------------------------------------!
!
! get mpi information
CALL MPI_COMM_SIZE(comm, nblocks, mpierr)
CALL MPI_COMM_RANK(comm, myid, mpierr)
DO 11 i=1,nblocks
IF (i - 1 == myid) THEN
DO 12 j=1,nblocks
IF (i == j) CYCLE ! don't talk to myself
! require there be something to send
IF (ghosts(j)%nsend > 0) THEN
! extract elements of u onto buffer
DO 13 k=1,ghosts(j)%nsend
ghosts(j)%send_buff(k) = u(ghosts(j)%isend_dest(k))
13 CONTINUE
! send it
CALL MPI_SEND(ghosts(j)%send_buff, ghosts(j)%nsend, &
MPI_DOUBLE_PRECISION, ghosts(j)%mydest, myid, &
comm, mpierr)
ENDIF
12 CONTINUE
ELSE
! receive data (if i'm expecting it)
IF (ghosts(i)%nrecv > 0) THEN
CALL MPI_RECV(ghosts(i)%recv_buff, ghosts(i)%nrecv, &
MPI_DOUBLE_PRECISION, ghosts(i)%mysrc, &
MPI_ANY_TAG, comm, stat, mpierr)
ENDIF
ENDIF
CALL MPI_BARRIER(comm, mpierr)
11 CONTINUE
DO 21 i=1,nblocks
IF (ghosts(i)%nrecv > 0) THEN
DO 22 k=1,ghosts(i)%nrecv
u(ghosts(i)%irecv_dest(k)) = ghosts(i)%recv_buff(k)
22 CONTINUE
ENDIF
21 CONTINUE
RETURN
END
! !
!========================================================================================!
! !
!> @brief Exchanges values in the ghosts communication region
!>
!> @param[in] comm MPI communicator
!>
!> @param[inout] ghosts on input contains the sending and recieving information
!> and preallocated workspace for this process to all other
!> processes in communicator.
!> on output the buffers have been filled but they should
!> not be accessed by another subroutine. [mpi communicator size]
!> @param[inout] u on input contains the nodes this process has updated.
!> on output contains the ndoes this process needs and others
!> have updated.
!>
!> @author Ben Baker
!>
!> @copyright Apache 2 license
!>
SUBROUTINE EIKONAL_EXCHANGE(comm, ghosts, u)
USE EIKONAL3D_TYPES, ONLY : ghostType
USE MPI
IMPLICIT NONE
INTEGER, INTENT(IN) :: comm
TYPE(ghostType), DIMENSION(:), INTENT(INOUT) :: ghosts
DOUBLE PRECISION, DIMENSION(:), INTENT(INOUT) :: u
! local variables
INTEGER stat(MPI_STATUS_SIZE), i, irecv, j, k, mpierr, myid, nblocks, nsend, nrecv
!----------------------------------------------------------------------------------!
!
! get mpi information
CALL MPI_COMM_SIZE(comm, nblocks, mpierr)
CALL MPI_COMM_RANK(comm, myid, mpierr)
! initialize
DO 1 i=1,nblocks
ghosts(i)%send_request =-1
ghosts(i)%recv_request =-1
ghosts(i)%lrecv = .FALSE.
1 CONTINUE
! send out buffers for sending and receiver
nsend = 0
nrecv = 0
DO 11 i=1,nblocks
IF (i - 1 == myid) THEN
DO 12 j=1,nblocks
IF (i == j) CYCLE ! don't talk to myself
! require there be something to send
IF (ghosts(j)%nsend > 0) THEN
! extract elements of u onto buffer