-
Notifications
You must be signed in to change notification settings - Fork 7
/
lpt_drog.F90
executable file
·2268 lines (1927 loc) · 67.9 KB
/
lpt_drog.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
! The routines in this file have been adapted from drog2dsp_hybrid.F90.
SUBROUTINE LPT_Drog_TimeStep
USE LPT_Comm_Module
USE LPT_Data_Module, ONLY: DiameterEffective, &
& DT1 => TimeStepSizes, &
& JJDR => PartElemJ, &
& LLDR => PartElemL, &
& NDR => NumParticlesLocal, &
& NOMP_MAP => MapOpenMP, &
& NumParticlesGlobal, &
& NumTrackingSnaps, &
& PartDiameterLocal, &
& PartDomainLocal, &
& ParticleInputMethod, &
& ParticleSourceX, &
& ParticleSourceY, &
& ParticleSourceZ, &
& SimulationLength, &
& T1 => Time1, &
& T2 => Time2, &
& XDR => PartLonLocal, &
& YDR => PartLatLocal, &
& ZDR => PartDepthLocal
USE LPT_Read_Module, ONLY: LPT_Read_From_Particle_File, &
& LPT_Read_Update_Velocities
USE LPT_Write_Module, ONLY: LPT_Write_Particle_Snap
IMPLICIT NONE
CHARACTER(LEN=100) :: JunkC
CHARACTER(LEN=100) :: JunkC1
INTEGER :: I
INTEGER :: II
INTEGER :: IMOD
INTEGER :: IT
INTEGER :: JJ
INTEGER :: JNEW
INTEGER :: LL
INTEGER :: LNEW
INTEGER :: IMAP
REAL(8) :: DDT
REAL(8) :: DIAM
REAL(8) :: DNEW
REAL(8) :: Random
REAL(8) :: UNEWS
REAL(8) :: VNEWS
REAL(8) :: WNEWS
REAL(8) :: XO
REAL(8) :: YO
REAL(8) :: ZO
#if VERBOSE > 1
CALL LPT_Print(0,"INFO","Entering the LPT_Drog_TimeStep routine.")
#endif
!pck 10/04/13 - Write Initial Locations
CALL LPT_Write_Particle_Snap
T1 = T1 + SimulationLength/DBLE(NumTrackingSnaps)
T2 = T2 + SimulationLength/DBLE(NumTrackingSnaps)
DO I=1,NumTrackingSnaps
#if VERBOSE > 0
WRITE(JunkC,'(I24)') I
WRITE(JunkC1,'(I24)') NumTrackingSnaps
CALL LPT_Print(0,"INFO","Beginning time snap " &
& //TRIM(ADJUSTL(JunkC))//" out of " &
& //TRIM(ADJUSTL(JunkC1))//".")
#endif
! Update the velocities to the correct time.
CALL LPT_Read_Update_Velocities(I)
! Add particles from the source term, if necessary.
IF(INDEX(ParticleInputMethod,"READFROMFILE").GT.0)THEN
CALL LPT_Read_From_Particle_File(I)
ELSEIF(INDEX(ParticleInputMethod,"SOURCE").GT.0)THEN
DO II=1,10
CALL RANDOM_NUMBER(Random)
IMOD = MOD(I,NumTrackerCores)
IF(IMOD.EQ.0) IMOD = NumTrackerCores
DO IT=1,NumTrackerCores
IF(MyRank.EQ.TrackerRanks(IT).AND.IMOD.EQ.IT)THEN
! Select a particle size at random.
DNEW = 0.000050D0+Random*(0.000300D0-0.000050D0)
! DNEW = DiameterEffective
CALL LPT_Oil_Source(ParticleSourceX, &
& ParticleSourceY,ParticleSourceZ,DNEW, &
& NumParticlesGlobal+1,0)
ENDIF
ENDDO
ENDDO
ENDIF
! Loop over the particles.
IF(AmTrackerCore)THEN
#if VERBOSE > 0
IF(MyRank.EQ.0)THEN
IF(SUM(PartDomainLocal).GE.100)THEN
WRITE(*,'(A,$)') "LPT: INFO: Tracking the particle " &
& //"locations: +"
ELSE
CALL LPT_Print(0,"INFO","Tracking the particle " &
& //"locations.")
ENDIF
ENDIF
#endif
!$ TIME_TC = TIME_TC - omp_get_wtime()
!$OMP PARALLEL DEFAULT (SHARED) PRIVATE(IMAP,II,XO,YO,JJ,UNEWS,VNEWS,JNEW,DDT)
!$OMP DO
DO IMAP=1,NDR
! II = NOMP_MAP(IMAP)
II = IMAP
XO = XDR(II)
YO = YDR(II)
ZO = ZDR(II)
JJ = JJDR(II)
LL = LLDR(II)
DDT = DT1(II)
IF(ALLOCATED(PartDiameterLocal))THEN
DIAM = PartDiameterLocal(II)
ELSE
DIAM = -99999.D0
ENDIF
! IF (XO<-999998.d0.and.YO<-999999.d0) CYCLE ! cjt
IF(PartDomainLocal(II).EQ.0) CYCLE
! Determine the components of flow at (XO,YO).
CALL LPT_Drog_VELS(JJ,LL,XO,YO,ZO,UNEWS,VNEWS,WNEWS,DIAM)
!TRACK THIS PARTICLE FROM TIME T1 TO T2
CALL LPT_Drog_TRACK(JJ,JNEW,LL,LNEW,XO,YO,ZO, &
& UNEWS,VNEWS,WNEWS,T1,T2,DDT,II,DIAM)
XDR(II) = XO
YDR(II) = YO
ZDR(II) = ZO
JJDR(II) = JNEW
LLDR(II) = LNEW
DT1(II) = DDT
#if VERBOSE > 0
IF(MyRank.EQ.0)THEN
IF(SUM(PartDomainLocal).GE.100)THEN
CALL LPT_Progress(IMAP,SUM(PartDomainLocal))
ENDIF
ENDIF
#endif
ENDDO
!$OMP END DO
!$OMP END PARALLEL
!$ TIME_TC = TIME_TC + omp_get_wtime()
ENDIF
! Write the particle locations to the output file.
CALL LPT_Write_Particle_Snap
T1 = T1 + SimulationLength/DBLE(NumTrackingSnaps)
T2 = T2 + SimulationLength/DBLE(NumTrackingSnaps)
ENDDO
#if VERBOSE > 1
CALL LPT_Print(0,"INFO","Exiting the LPT_Drog_TimeStep routine.")
#endif
! We're done here.
RETURN
END SUBROUTINE
! The following routine is called immediately after the
! unstructured mesh is read. We read the element-to-vertex
! connectivity table from the ADCIRC mesh. This routine creates
! the element-to-element and vertex-to-element connectivity
! tables, and it also determines the boundary segment information
! that is needed to prevent the particles from leaving the domain.
SUBROUTINE LPT_Drog_MAK_NEINFO
USE LPT_Data_Module, ONLY: NMEL => NumElems, &
& NMND => NumVerts, &
& ELEMS => MeshConnEV, &
& ICEE => MeshConnEE, &
& ICNE => MeshConnVE, &
& NSEG => NumBoundarySegments, &
& IBSEG => BoundarySegmentVerts, &
& ISEGF => BoundarySegmentFollowing, &
& ISEGP => BoundarySegmentPreceding, &
& IBSEGEL => BoundarySegmentElement
USE LPT_Comm_Module, ONLY: MyRank
IMPLICIT NONE
INTEGER:: I, J, M, N, I1, I2, M1, M2, N1, N2, N3, ISEG, maxne
INTEGER,ALLOCATABLE :: NPROP(:)
INTEGER, PARAMETER:: MAP(4) = (/1, 2, 3, 1 /)
#if VERBOSE > 1
CALL LPT_Print(0,"INFO","Entering the LPT_MAK_NEINFO routine.")
#endif
ALLOCATE(NPROP(1:NMND))
! Vertex-to-element adjacency information.
NPROP(1:NMND) = 0
#if VERBOSE > 2
IF(MyRank.EQ.0)THEN
WRITE(*,'(A,$)') "LPT: INFO: Computing the number of " &
& //"elements connected to each vertex: +"
ENDIF
#endif
DO M = 1, NMEL
DO J = 1, 3
NPROP(ELEMS(M,J)) = NPROP(ELEMS(M,J)) + 1
ENDDO
#if VERBOSE > 2
IF(MyRank.EQ.0)THEN
CALL LPT_Progress(M,NMEL)
ENDIF
#endif
ENDDO
maxne = maxval(NPROP(1:NMND)) + 1
ALLOCATE( ICEE(1:NMEL,1:3), ICNE(1:NMND,1:maxne) )
ICNE(1:NMND,1:maxne) = 0
#if VERBOSE > 2
IF(MyRank.EQ.0)THEN
WRITE(*,'(A,$)') "LPT: INFO: Computing the vertex-to-element " &
& //"connectivity table: +"
ENDIF
#endif
DO M = 1, NMEL
DO J = 1, 3
ICNE(ELEMS(M,J),1) = ICNE(ELEMS(M,J),1) + 1
ICNE(ELEMS(M,J),ICNE(ELEMS(M,J),1)+1) = M
ENDDO
#if VERBOSE > 2
IF(MyRank.EQ.0)THEN
CALL LPT_Progress(M,NMEL)
ENDIF
#endif
ENDDO
! Element-to-element adjacency information.
! 1
! |\
! (1)| \(3)
! |__\
! 2 (2)3
ICEE(1:NMEL,1:3) = 0
#if VERBOSE > 2
IF(MyRank.EQ.0)THEN
WRITE(*,'(A,$)') "LPT: INFO: Computing the element-to-" &
& //"element connectivity table: +"
ENDIF
#endif
DO M = 1, NMEL
NM_LOOP:DO J = 1, 3
DO I1 = 1, ICNE(ELEMS(M,MAP(J)),1)
M1 = ICNE(ELEMS(M,MAP(J)),I1+1)
IF( M1 == M ) CYCLE
DO I2 = 1, ICNE(ELEMS(M,MAP(J+1)),1)
M2 = ICNE(ELEMS(M,MAP(J+1)),I2+1)
IF( M1 == M2 ) THEN
ICEE(M,J) = M1
CYCLE NM_LOOP
ENDIF
ENDDO
ENDDO
ENDDO NM_LOOP
#if VERBOSE > 2
IF(MyRank.EQ.0)THEN
CALL LPT_Progress(M,NMEL)
ENDIF
#endif
ENDDO
#if VERBOSE > 2
IF(MyRank.EQ.0)THEN
WRITE(*,'(A,$)') "LPT: INFO: Determining which vertices are " &
& //"on boundaries: +"
ENDIF
#endif
NPROP(1:NMND)=0
DO I = 1, NMEL
N1 = ELEMS(I,1)
N2 = ELEMS(I,2)
N3 = ELEMS(I,3)
NPROP(N1) = NPROP(N1) + N2 - N3
NPROP(N2) = NPROP(N2) + N3 - N1
NPROP(N3) = NPROP(N3) + N1 - N2
#if VERBOSE > 2
IF(MyRank.EQ.0)THEN
CALL LPT_Progress(I,NMEL)
ENDIF
#endif
ENDDO
! Count total number of vertices on boundary.
I = 0
DO N = 1, NMND
IF( NPROP(N) == 0 ) CYCLE
I = I +1
ENDDO
ALLOCATE( IBSEG(1:I*2,1:2),IBSEGEL(1:I*2) )
! Compute the boundary segments, which occur in an element
! that has a zero in the element-to-element adjacency table.
! The bouundary segments are matched with adjacent elements.
#if VERBOSE > 2
IF(MyRank.EQ.0)THEN
WRITE(*,'(A,$)') "LPT: INFO: Determining the boundary " &
& //"segments: +"
ENDIF
#endif
ISEG = 0
DO M = 1, NMEL
DO J = 1, 3
IF( ICEE(M,J) == 0 ) then
ISEG = ISEG + 1
IBSEG(ISEG,1) = ELEMS(M,MAP(J))
IBSEG(ISEG,2) = ELEMS(M,MAP(J+1))
IBSEGEL(ISEG) = M
ENDIF
ENDDO
#if VERBOSE > 2
IF(MyRank.EQ.0)THEN
CALL LPT_Progress(M,NMEL)
ENDIF
#endif
ENDDO
#if VERBOSE > 2
IF(MyRank.EQ.0)THEN
WRITE(*,'(A,$)') "LPT: INFO: Connecting the boundary " &
& //"segments: +"
ENDIF
#endif
NSEG = ISEG
ALLOCATE( ISEGP(1:NSEG),ISEGF(1:NSEG) )
ISEGP(:) = 0
ISEGF(:) = 0
DO I = 1, NSEG
DO J = I+1, NSEG
IF( IBSEG(I,1) == IBSEG(J,2) ) then
ISEGP(I) = J
ISEGF(J) = I
ENDIF
IF( IBSEG(I,2) == IBSEG(J,1) ) then
ISEGF(I) = J
ISEGP(J) = I
ENDIF
ENDDO
#if VERBOSE > 2
IF(MyRank.EQ.0)THEN
CALL LPT_Progress(I,NSEG)
ENDIF
#endif
ENDDO
DEALLOCATE(NPROP)
#if VERBOSE > 1
CALL LPT_Print(0,"INFO","Exiting the LPT_Drog_MAK_NEINFO " &
& //"routine.")
#endif
! We're done here.
RETURN
END SUBROUTINE LPT_Drog_MAK_NEINFO
! The following routine contains computations that are performed
! after the input files have been read, but before the start
! of the time-stepping.
SUBROUTINE LPT_Drog_Initialize
USE LPT_Comm_Module
USE LPT_Data_Module, ONLY: A => ShapeFunc_A, &
& A0 => ShapeFunc_A0, &
& AR => ElementArea_AR, &
& B => ShapeFunc_B, &
& DT1 => TimeStepSizes, &
& ELEMS => MeshConnEV, &
& ICURBS => PartBoundary, &
& JJDR => PartElemJ, &
& KODE => PartBoundaryCode, &
& LLDR => PartElemL, &
& MinimumTimeStep, &
& NDR => NumParticlesLocal, &
& NMEL => NumElems, &
& NMND => NumVerts, &
& NOMP_MAP => MapOpenMP, &
& NumTrackingSnaps, &
& PartDomainLocal, &
& SimulationLength, &
& StartingTime, &
& T => ElementArea_T, &
& T1 => Time1, &
& T2 => Time2, &
& X => MeshLon, &
& XDR => PartLonLocal, &
& Y => MeshLat, &
& YDR => PartLatLocal, &
& ZDR => PartDepthLocal
USE LPT_Data_Lattice_Table
#ifdef KDTREE
USE LPT_KDTREE_Module, ONLY: LPT_KDTREE_Search
#endif
IMPLICIT NONE
CHARACTER(LEN=100) :: JunkC
CHARACTER(LEN=100) :: JunkC1
INTEGER :: I
INTEGER :: ICHECK
INTEGER :: III
INTEGER :: J
INTEGER :: N
INTEGER :: N1
INTEGER :: N2
INTEGER :: N3
REAL(8) :: XSTART
REAL(8) :: YSTART
REAL(8) :: ZSTART
#if VERBOSE > 1
CALL LPT_Print(0,"INFO","Entering the LPT_Drog_Initialize " &
& //"routine.")
#endif
! Convert other times to seconds.
SimulationLength = SimulationLength * 3600.D0
StartingTime = StartingTime
MinimumTimeStep = MinimumTimeStep * 3600.D0
! Establish the limits of integration.
T1 = StartingTime
T2 = StartingTime + SimulationLength/FLOAT(NumTrackingSnaps)
#ifdef MPI
! The dedicated reader/writer cores should not execute the code
! remaining in this routine.
IF(UseReaderCore.AND.(MyRank.EQ.0))THEN
RETURN
ENDIF
IF(UseWriterCore.AND.(MyRank+1.EQ.NumRanks))THEN
RETURN
ENDIF
#endif
! Compute area coordinates for element interpolation functions.
#if VERBOSE > 2
IF(MyRank.EQ.0)THEN
WRITE(*,'(A,$)') "LPT: INFO: Preparing the element " &
& //"interpolation functions: +"
ENDIF
#endif
ALLOCATE( A(1:NMEL,1:3), B(1:NMEL,1:3), A0(1:NMEL,1:2) )
ALLOCATE( AR(1:NMEL), T(1:NMEL,1:3) )
DO J=1,NMEL
N1 = ELEMS(J,1)
N2 = ELEMS(J,2)
N3 = ELEMS(J,3)
A(J,1) = X(N3) - X(N2)
A(J,2) = X(N1) - X(N3)
A(J,3) = X(N2) - X(N1)
B(J,1) = Y(N2) - Y(N3)
B(J,2) = Y(N3) - Y(N1)
B(J,3) = Y(N1) - Y(N2)
A0(J,1) = 0.5* (X(N2)*Y(N3)-X(N3)*Y(N2))
A0(J,2) = 0.5* (X(N3)*Y(N1)-X(N1)*Y(N3))
AR(J) = 0.5* (A(J,2)*B(J,1)-A(J,1)*B(J,2))
T(J,1) = A0(J,1)*2.E0
T(J,2) = A0(J,2)*2.E0
T(J,3) = (2.E0*AR(J)-T(J,1)-T(J,2))
#ifdef DEBUG
IF(AR(J).LE.0)THEN
WRITE(JunkC,'(I24)') J
CALL LPT_Print(MyRank,"FATAL ERROR","Element number " &
& //TRIM(ADJUSTL(JunkC))//" has a non-positive area.")
END IF
#endif
#if VERBOSE > 2
IF(MyRank.EQ.0)THEN
CALL LPT_Progress(J,NMEL)
ENDIF
#endif
ENDDO
#if VERBOSE > 1
CALL LPT_Print(0,"INFO","The element areas were computed.")
#endif
! Make the search table.
CALL LPT_Drog_MAKE_STAB(NMEL,ELEMS,NMND,X,Y)
! Only execute the rest of this routine if an initial set
! of particles has also been read. Otherwise, this information
! will be developed in the Oil_Source routine.
IF(NDR.EQ.0)THEN
RETURN
ENDIF
! Find the starting element for each particle.
#if VERBOSE > 2
IF(MyRank.EQ.0)THEN
WRITE(*,'(A,$)') "LPT: INFO: Finding the starting element " &
& //"for each of the particles: +"
ENDIF
#endif
IF(.NOT.ALLOCATED(JJDR)) ALLOCATE(JJDR(NDR))
IF(.NOT.ALLOCATED(LLDR)) ALLOCATE(LLDR(NDR))
IF(ALLOCATED(PartDomainLocal)) DEALLOCATE(PartDomainLocal)
ALLOCATE(PartDomainLocal(1:NDR))
PartDomainLocal(:) = 0
DO III=1,NDR
XSTART = XDR(III)
YSTART = YDR(III)
ZSTART = ZDR(III)
CALL LPT_Drog_Find_Element(XSTART,YSTART,ZSTART, &
& JJDR(III),LLDR(III),ICHECK)
#if VERBOSE > 2
IF(MyRank.EQ.0)THEN
CALL LPT_Progress(III,NDR)
ENDIF
#endif
PartDomainLocal(III) = ICHECK
IF(ICHECK.EQ.1) CYCLE
#ifdef DEBUG
WRITE(JunkC,'(I24)') III
WRITE(JunkC1,'(I24)') MyRank
CALL LPT_Print(MyRank,"WARNING","Tracker core " &
& //TRIM(ADJUSTL(JunkC1))//" could not find the " &
& //"starting element for local particle number " &
& //TRIM(ADJUSTL(JunkC))//".")
#endif
ENDDO
IF(.NOT.ALLOCATED(KODE)) ALLOCATE(KODE(NDR))
IF(.NOT.ALLOCATED(ICURBS)) ALLOCATE(ICURBS(NDR))
KODE(1:NDR) = 0
ICURBS(1:NDR) = 0
! Establish the default step size.
IF(.NOT.ALLOCATED(DT1)) ALLOCATE(DT1(NDR))
DO III=1,NDR
DT1(III) = DABS(SimulationLength/FLOAT(NumTrackingSnaps)/10.d0)
END DO
#if VERBOSE > 1
CALL LPT_Print(0,"INFO","Exiting the LPT_Drog_Initialize " &
& //"routine.")
#endif
! We're done here.
RETURN
END SUBROUTINE
! The following routine finds the horizontal and vertical elements
! containing a particle, given its coordinates.
SUBROUTINE LPT_Drog_Find_Element(XSTART,YSTART,ZSTART, &
JJ,LL,ICHECK)
USE LPT_Data_Lattice_Table
USE LPT_Data_Module, ONLY: NDIV => LatticeSearchBins
IMPLICIT NONE
INTEGER,INTENT(OUT) :: ICHECK
INTEGER,INTENT(INOUT) :: JJ
INTEGER,INTENT(INOUT) :: LL
REAL(8),INTENT(IN) :: XSTART
REAL(8),INTENT(IN) :: YSTART
REAL(8),INTENT(IN) :: ZSTART
INTEGER :: I
INTEGER :: J
INTEGER :: IND
INTEGER :: IX
INTEGER :: IY
#ifdef KDTREE
ICHECK = 0
CALL LPT_KDTREE_Search(XSTART,YSTART,I,IND)
IF(IND.EQ.1)THEN
JJ = I
ICHECK = 1
CALL LPT_Drog_Find_Vertical_Element(I,JJ,XSTART,YSTART,ZSTART)
LL = I
ENDIF
#else
IX = INT( (XSTART-XMIN(1))/DX(1) ) + 1
IY = INT( (YSTART-XMIN(2))/DX(2) ) + 1
IX = MAX(0,IX)
IX = MIN(NDIV+1,IX)
IY = MAX(0,IY)
IY = MIN(NDIV+1,IY)
ICHECK = 0
DO J=1,NE_PIECE(IX,IY)
I=NE_PIECE_LIST(J+NE_PIECE_INDEX(IX,IY))
CALL LPT_Drog_BELEL(I,XSTART,YSTART,IND)
IF(IND.EQ.1)THEN
JJ = I
ICHECK = 1
CALL LPT_Drog_Find_Vertical_Element(I,JJ,XSTART,YSTART,ZSTART)
LL = I
EXIT
END IF
ENDDO
#endif
! We're done here.
RETURN
END SUBROUTINE
! The following routine makes the lattice searching table.
! It is called by LPT_Drog_Initialize.
SUBROUTINE LPT_Drog_MAKE_STAB(NMEL,ELEMS,NMND,X,Y)
USE LPT_Comm_Module, ONLY: MyRank
USE LPT_Data_Lattice_Table
IMPLICIT NONE
INTEGER,INTENT(IN) :: ELEMS(NMEL,3)
INTEGER,INTENT(IN) :: NMEL
INTEGER,INTENT(IN) :: NMND
REAL(8),INTENT(IN) :: X(NMND)
REAL(8),INTENT(IN) :: Y(NMND)
INTEGER :: I
INTEGER :: ISTART
INTEGER :: IX(3)
INTEGER :: IY(3)
INTEGER :: J
INTEGER :: M
INTEGER :: Multiplier
INTEGER :: N
REAL(8) :: XMAX(2)
#if VERBOSE > 1
CALL LPT_Print(0,"INFO","Entering the LPT_Drog_MAKE_STAB " &
& //"routine.")
#endif
XMAX(1) = MAXVAL(X(1:NMND))
XMIN(1) = MINVAL(X(1:NMND))
XMAX(2) = MAXVAL(Y(1:NMND))
XMIN(2) = MINVAL(Y(1:NMND))
DO I = 1, 2
XMAX(I) = XMAX(I) + 1.D0
DX(I) = ( XMAX(I) - XMIN(I) ) / DBLE(NDIV)
ENDDO
Multiplier = 0
100 CONTINUE
Multiplier = Multiplier + 2
IF(ALLOCATED(NE_PIECE_LIST)) DEALLOCATE(NE_PIECE_LIST)
ALLOCATE(NE_PIECE_LIST(Multiplier*NMEL))
! Search piece index.
#if VERBOSE > 2
IF(MyRank.EQ.0)THEN
WRITE(*,'(A,$)') "LPT: INFO: Determining the number of " &
& //"mesh elements in each lattice piece: +"
ENDIF
#endif
IF(.NOT.ALLOCATED(NE_PIECE))THEN
ALLOCATE(NE_PIECE(0:NDIV+1,0:NDIV+1))
ENDIF
NE_PIECE(0:NDIV+1,0:NDIV+1) = 0
DO M = 1, NMEL
DO J = 1, 3
N = ELEMS(M,J)
IX(J) = INT( (X(N)-XMIN(1)) / DX(1) ) + 1
IY(J) = INT( (Y(N)-XMIN(2)) / DX(2) ) + 1
ENDDO
DO I = MINVAL(IX(1:3)), MAXVAL(IX(1:3))
DO J = MINVAL(IY(1:3)), MAXVAL(IY(1:3))
NE_PIECE(I,J) = NE_PIECE(I,J) + 1
ENDDO
ENDDO
#if VERBOSE > 2
IF(MyRank.EQ.0)THEN
CALL LPT_Progress(M,NMEL)
ENDIF
#endif
ENDDO
IF(.NOT.ALLOCATED(NE_PIECE_INDEX))THEN
ALLOCATE(NE_PIECE_INDEX(1:NDIV,1:NDIV))
ENDIF
ISTART = 0
DO I = 1, NDIV
DO J = 1, NDIV
NE_PIECE_INDEX(I,J) = ISTART
ISTART = ISTART + NE_PIECE(I,J)
ENDDO
ENDDO
! Make piece table.
#if VERBOSE > 2
IF(MyRank.EQ.0)THEN
WRITE(*,'(A,$)') "LPT: INFO: Placing the mesh elements in " &
& //"each lattice piece: +"
ENDIF
#endif
NE_PIECE(1:NDIV,1:NDIV) = 0
DO M = 1, NMEL
DO J = 1, 3
N = ELEMS(M,J)
IX(J) = INT( (X(N)-XMIN(1)) / DX(1) ) + 1
IY(J) = INT( (Y(N)-XMIN(2)) / DX(2) ) + 1
ENDDO
DO I = MINVAL(IX(1:3)), MAXVAL(IX(1:3))
DO J = MINVAL(IY(1:3)), MAXVAL(IY(1:3))
! Count total number of elements in NE_PIECE(I,J).
NE_PIECE(I,J) = NE_PIECE(I,J) + 1
IF((NE_PIECE(I,J)+NE_PIECE_INDEX(I,J)).GT. &
& SIZE(NE_PIECE_LIST))THEN
#if VERBOSE > 2
IF(MyRank.EQ.0)THEN
WRITE(*,'(A)') "+"
ENDIF
#endif
GOTO 100
ENDIF
! Store element number in NE_PIECE(I,J).
NE_PIECE_LIST(NE_PIECE(I,J)+NE_PIECE_INDEX(I,J)) = M
ENDDO
ENDDO
#if VERBOSE > 2
IF(MyRank.EQ.0)THEN
CALL LPT_Progress(M,NMEL)
ENDIF
#endif
ENDDO
#if VERBOSE > 1
CALL LPT_Print(0,"INFO","Exiting the LPT_Drog_MAKE_STAB " &
& //"routine.")
#endif
! We're done here.
RETURN
END SUBROUTINE LPT_Drog_MAKE_STAB
! The following routine determines whether the point (XP,YP)
! lies within element J or on its boundaries. If it does,
! then NFLAG=1; otherwise, NFLAG=0.
SUBROUTINE LPT_Drog_BELEL(J,XP,YP,NFLAG)
USE LPT_Data_Module, ONLY: ELEMS => MeshConnEV, &
& X => MeshLon, &
& Y => MeshLat
IMPLICIT NONE
INTEGER :: I
INTEGER,INTENT(IN) :: J
INTEGER :: K
INTEGER,INTENT(OUT) :: NFLAG
REAL(8) :: CROSSPROD
REAL(8) :: D
REAL(8) :: DELX
REAL(8) :: DELY
REAL(8) :: THETA
REAL(8) :: VX(3)
REAL(8) :: VY(3)
REAL(8) :: XLOCAL(3)
REAL(8),INTENT(IN) :: XP
REAL(8) :: YLOCAL(3)
REAL(8),INTENT(IN) :: YP
! Extract the local vertex coordinates.
XLOCAL(1)=X(ELEMS(J,1))
XLOCAL(2)=X(ELEMS(J,2))
XLOCAL(3)=X(ELEMS(J,3))
YLOCAL(1)=Y(ELEMS(J,1))
YLOCAL(2)=Y(ELEMS(J,2))
YLOCAL(3)=Y(ELEMS(J,3))
! Calculate the x- and y-components of vectors pointing from (XP,YP)
! to each vertex on the element.
DO I=1,3
DELX=XLOCAL(I)-XP
DELY=YLOCAL(I)-YP
D=DSQRT(DELX**2.0+DELY**2.0)
THETA=DATAN2(DELY,DELX)
VX(I)=D*DCOS(THETA)
VY(I)=D*DSIN(THETA)
ENDDO
! Determine if the point is on the element by calculating the
! cross-products of neighboring vectors in a direction which will
! yield all non-negative numbers if (X,Y) is on the element.
NFLAG=1
DO I=1,3
K=I+1
IF(I.EQ.3)K=1
CROSSPROD=VX(I)*VY(K)-VY(I)*VX(K)
IF(CROSSPROD.LT.0.D0)THEN
NFLAG=0
EXIT
ENDIF
ENDDO
! We're done here.
RETURN
END SUBROUTINE LPT_Drog_BELEL
! The following routine determines the vertical element
! in which a particle is located.
SUBROUTINE LPT_Drog_Find_Vertical_Element(Layer,J,X,Y,Z)
USE LPT_Data_Module, ONLY: MeshDepth, &
& NumLayers, &
& Sigma
IMPLICIT NONE
! Argument variables.
INTEGER,INTENT(IN) :: J
INTEGER,INTENT(OUT) :: Layer
REAL(8),INTENT(IN) :: X
REAL(8),INTENT(IN) :: Y
REAL(8),INTENT(INOUT) :: Z
! Internal variables.
INTEGER :: IL
REAL(8) :: Depths(NumLayers)
REAL(8) :: LPT_Drog_Interpolate
! If we are using a two-dimensional velocity field,
! then there can only be one layer.
IF(NumLayers.EQ.1)THEN
Layer = 1
RETURN
ENDIF
! Interpolate the depths for the layers
! at the horizontal location.
Depths(1) = LPT_Drog_Interpolate(J,X,Y,MeshDepth)
DO IL=2,NumLayers
Depths(IL) = Depths(1) + (0.D0-Depths(1)) &
& * (Sigma(IL)-Sigma(1))/(Sigma(NumLayers)-Sigma(1))
ENDDO
! If the particle has moved below the sea floor,
! then bump it up and return.
IF(Z.LT.Depths(1))THEN
Z = Depths(1)
Layer = 1
RETURN
ENDIF
! If the particle has moved above the sea surface,
! then bump it down and return.
IF(Z.GT.Depths(NumLayers))THEN
Z = Depths(NumLayers)
Layer = NumLayers - 1
RETURN
ENDIF
! Otherwise, find the element within the water column.
DO IL=1,NumLayers-1
IF((Depths(IL).LE.Z).AND.(Z.LE.Depths(IL+1)))THEN
Layer = IL
EXIT
ENDIF
ENDDO
! We're done here.
RETURN
END SUBROUTINE LPT_Drog_Find_Vertical_Element
! The following routine calls related functions to determine
! the components of velocity at a point (X,Y,Z).
SUBROUTINE LPT_Drog_VELS(J,L,X,Y,Z,UF,VF,WF,DIAM)
USE LPT_Data_Module, ONLY: BuoyancyMethod, &
& MeshDepth, &
& NumLayers, &
& Sigma, &
& UNEW => VelU, &
& VNEW => VelV, &
& WNEW => VelW
IMPLICIT NONE
INTEGER,INTENT(IN) :: J
INTEGER,INTENT(IN) :: L
REAL(8), INTENT(IN) :: DIAM
REAL(8), INTENT(INOUT) :: UF
REAL(8), INTENT(INOUT) :: VF
REAL(8), INTENT(INOUT) :: WF
REAL(8), INTENT(IN) :: X
REAL(8), INTENT(IN) :: Y
REAL(8), INTENT(IN) :: Z
REAL(8) :: LPT_Drog_Interpolate
INTEGER :: L1
INTEGER :: L2
REAL(8) :: Multiplier
REAL(8) :: UL1
REAL(8) :: UL2
REAL(8) :: VelocityTerminal
REAL(8) :: VL1
REAL(8) :: VL2
REAL(8) :: WL1
REAL(8) :: WL2
REAL(8) :: ZDP
REAL(8) :: ZL1
REAL(8) :: ZL2
! If the velocity field is two-dimensional,
! then return the depth-averaged velocities.
IF(NumLayers.EQ.1)THEN
UF = LPT_Drog_Interpolate(J,X,Y,UNEW(:,1))
VF = LPT_Drog_Interpolate(J,X,Y,VNEW(:,1))
WF = 0.D0
! Otherwise we need to interpolate a three-dimensional
! velocity field to a specific depth in the water column.
ELSE
IF(L.NE.NumLayers)THEN
L1 = L
L2 = L+1
ELSE
L1 = L-1
L2 = L
ENDIF
! Find the velocities at the layer below the particle.
UL1 = LPT_Drog_Interpolate(J,X,Y,UNEW(:,L1))
VL1 = LPT_Drog_Interpolate(J,X,Y,VNEW(:,L1))
WL1 = LPT_Drog_Interpolate(J,X,Y,WNEW(:,L1))
! Find the velocities at the layer above the particle.
UL2 = LPT_Drog_Interpolate(J,X,Y,UNEW(:,L2))
VL2 = LPT_Drog_Interpolate(J,X,Y,VNEW(:,L2))
WL2 = LPT_Drog_Interpolate(J,X,Y,WNEW(:,L2))
! Interpolate vertically to the depth of the particle.
ZDP = LPT_Drog_Interpolate(J,X,Y,MeshDepth(:))
ZL1 = ZDP + (0.D0 - ZDP) * &
& (Sigma(L1)-Sigma(1))/(Sigma(NumLayers)-Sigma(1))
ZL2 = ZDP + (0.D0 - ZDP) * &
& (Sigma(L2)-Sigma(1))/(Sigma(NumLayers)-Sigma(1))
Multiplier = (Z-ZL1)/(ZL2-ZL1)
UF = UL1 + (UL2-UL1) * Multiplier
VF = VL1 + (VL2-VL1) * Multiplier
WF = WL1 + (WL2-WL1) * Multiplier