-
Notifications
You must be signed in to change notification settings - Fork 1
/
Faces.f90
910 lines (814 loc) · 27.2 KB
/
Faces.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
module FacesModule
!> @file Faces.f90
!> Provides a primitive data structure and methods for creating faces of polyhedral meshes.
!>
!> @author Peter Bosler, Sandia National Laboratories Center for Computing Research
!>
!>
!> @defgroup Faces Faces module
!> @brief Faces of polyhedral meshes connect to vertices and edges.
!> @{
use NumberKindsModule
use STDIntVectorModule
use LoggerModule
use ParticlesModule
use EdgesModule
use PlaneGeomModule
use SphereGeomModule
implicit none
private
public Faces
public New, Delete, Copy
public FaceArea, FaceCenterPhysCoord, FaceCenterLagCoord
public DivideQuadFace, DivideTriFace, InsertFace
public WriteFacesToVTKPolygons, WriteFacesToMatlab, WriteFaceAreaToVTKCellData
public positiveEdge
public FaceCentroid, TriFaceArea, QuadFaceArea
public LogStats, PrintDebugInfo
public SharedEdge, ParticleOppositeTriEdge
public CountParents
!> @class Faces
!> @brief Designed to accomodate quadrilateral and triangular faces.
!> Each faces knows the indices (to a Particles object) of its vertices and center and the indices (to an Edges object)
!> of its edges.
!> Faces are recursively divided to provide desired spatial resolution, and stored in the induced quadtree.
!>
type Faces
integer(kint), pointer :: centerParticle(:) => null()
integer(kint), pointer :: vertices(:,:) => null()
integer(kint), pointer :: edges(:,:) => null()
logical(klog), pointer :: hasChildren(:) => null()
integer(kint), pointer :: children(:,:) => null()
integer(kint), pointer :: parent(:) => null()
integer(kint) :: faceKind
integer(kint) :: N
integer(kint) :: N_Active
integer(kint) :: N_Max
contains
final :: deletePrivate
end type
interface New
module procedure newPrivate
end interface
interface Delete
module procedure deletePrivate
end interface
interface Copy
module procedure copyPrivate
end interface
interface LogStats
module procedure LogStatsPrivate
end interface
interface PrintDebugInfo
module procedure PrintDebugPrivate
end interface
interface CountParents
module procedure countParentFaces
end interface
!
!----------------
! Logging
!----------------
!
logical(klog), save :: logInit = .FALSE.
type(Logger) :: log
character(len=28), save :: logKey = 'Faces'
integer(kint), parameter :: logLevel = DEBUG_LOGGING_LEVEL
character(len=MAX_STRING_LENGTH) :: logstring
contains
subroutine PrintDebugPrivate( self )
type(Faces), intent(in) :: self
integer(kint) :: i, j
print *, " Faces DEBUG info : "
print *, "faces.N = ", self%N
print *, "faces.N_Max = ", self%N_Max
print *, "faces.N_Active = ", self%N_Active
print *, "faces.faceKind = ", self%faceKind
print *, "n divided faces = ", count(self%hasChildren)
print *, "faces.centerParticle = "
do i = 1, self%N_Max
print *, self%centerParticle(i)
enddo
print *, " faces.vertices = "
do i = 1, self%N_Max
do j = 1, size(self%vertices,1)
write(6,'(I6)',advance='NO') self%vertices(j,i)
enddo
print *, " "
enddo
print *, " "
print *, "faces.edges = "
do i = 1, self%N_Max
do j = 1, size(self%edges,1)
write(6,'(I6)',advance='NO') self%edges(j,i)
enddo
print *, " "
enddo
print *, " "
print *, "faces tree = "
do i = 1, self%N_Max
if ( self%hasChildren(i) ) then
write(6,'(A,4X)',advance ='NO') 'T'
else
write(6,'(A,4X)',advance='NO' ) 'F'
endif
do j = 1, 4
write(6, '(I6)', advance='NO') self%children(j,i)
enddo
print *, self%parent(i)
enddo
end subroutine
subroutine LogStatsPrivate(self, aLog)
type(Faces), intent(in) :: self
type(Logger), intent(inout) :: aLog
call LogMessage(aLog, TRACE_LOGGING_LEVEL, logkey, " Faces stats : ")
call StartSection(aLog)
call LogMessage(alog, TRACE_LOGGING_LEVEL, "faces.N = ", self%N)
call LogMessage(aLog, TRACE_LOGGING_LEVEL, "faces.N_Max = ", self%N_Max )
call LogMessage(aLog, TRACE_LOGGING_LEVEL, "faces.N_Active = ", self%N_Active)
call LogMessage(aLog, TRACE_LOGGING_LEVEL, "n divided faces = ", count(self%hasChildren))
if ( self%faceKind == TRI_PANEL ) then
call LogMessage(aLog, TRACE_LOGGING_LEVEL, "faceKind = ", "TRI_PANEL")
elseif ( self%faceKind == QUAD_PANEL ) then
call LogMessage(aLog, TRACE_LOGGING_LEVEL, "faceKind = ", "QUAD_PANEL")
else
call LogMessage(aLog, TRACE_LOGGING_LEVEL, "faceKind = ", "invalid faceKind.")
endif
call EndSection(aLog)
end subroutine
subroutine newPrivate( self, faceKind, nMax )
type(Faces), intent(out) :: self
integer(kint), intent(in) :: faceKind
integer(kint), intent(in) :: nMax
if ( .NOT. logInit ) call InitLogger(log, procRank)
if ( nMax <= 0 ) then
call LogMessage(log, ERROR_LOGGING_LEVEL, logKey, " NewFaces ERROR : invalid nMax.")
return
endif
if ( faceKind == QUAD_PANEL ) then
allocate(self%vertices(4,nMax))
allocate(self%edges(4,nMax))
self%vertices = 0
self%edges = 0
elseif ( faceKind == TRI_PANEL ) then
allocate(self%vertices(3,nMax))
allocate(self%edges(3,nMax))
self%vertices = 0
self%edges = 0
else
call LogMessage(log, ERROR_LOGGING_LEVEL, logKey, " NewFaces ERROR : invalid face kind.")
return
endif
allocate(self%centerParticle(nMax))
self%centerParticle = 0
allocate(self%children(4,nMax))
self%children = 0
allocate(self%hasChildren(nMax))
self%hasChildren = .FALSE.
allocate(self%parent(nMax))
self%parent = 0
self%N = 0
self%N_Active = 0
self%N_Max = nMax
self%faceKind = faceKind
end subroutine
subroutine deletePrivate(self)
type(Faces), intent(inout) :: self
if ( associated(self%vertices)) deallocate(self%vertices)
if ( associated(self%edges)) deallocate(self%edges)
if ( associated(self%centerParticle)) deallocate(self%centerParticle)
if ( associated(self%children)) deallocate(self%children)
if ( associated(self%hasChildren)) deallocate(self%hasChildren)
if ( associated(self%parent)) deallocate(self%parent)
end subroutine
subroutine copyPrivate( dest, source )
type(Faces), intent(inout) :: dest
type(Faces), intent(in) :: source
!
integer(kint) :: j
if ( dest%N_Max < source%N ) then
call LogMessage(log, ERROR_LOGGING_LEVEL, logkey, " CopyFaces ERROR : not enough memory.")
return
endif
if ( dest%faceKind /= source%faceKind) then
call LogMessage(log, ERROR_LOGGING_LEVEL, logkey, " CopyFaces ERROR : face kind mismatch.")
return
endif
dest%N = source%N
dest%N_Active = source%N_Active
do j = 1, source%N
dest%vertices(:,j) = source%vertices(:,j)
dest%edges(:,j) = source%vertices(:,j)
dest%centerParticle(j) = source%centerParticle(j)
dest%children(:,j) = source%children(:,j)
dest%hasChildren(j) = source%hasChildren(j)
dest%parent(j) = source%parent(j)
enddo
end subroutine
function FaceCenterPhysCoord( self, faceIndex, aparticles)
real(kreal) :: FaceCenterPhysCoord(3)
type(Faces), intent(in) :: self
integer(kint) :: faceIndex
type(Particles), intent(in) :: aParticles
FaceCenterPhysCoord(1) = aParticles%x(self%centerParticle(faceIndex))
FaceCenterPhysCoord(2) = aParticles%y(self%centerParticle(faceIndex))
if ( associated(aParticles%z) ) then
FaceCenterPhysCoord(3) = aParticles%z(self%centerParticle(faceIndex))
else
FaceCenterPhysCoord(3) = 0.0_kreal
endif
end function
function FaceCenterLagCoord( self, faceIndex, aparticles)
real(kreal) :: FaceCenterLagCoord(3)
type(Faces), intent(in) :: self
integer(kint) :: faceIndex
type(Particles), intent(in) :: aParticles
FaceCenterLagCoord(1) = aParticles%x0(self%centerParticle(faceIndex))
FaceCenterLagCoord(2) = aParticles%y0(self%centerParticle(faceIndex))
if ( associated(aParticles%z0) ) then
FaceCenterLagCoord(3) = aParticles%z0(self%centerParticle(faceIndex))
else
FaceCenterLagCoord(3) = 0.0_kreal
endif
end function
function countParentFaces( self, index )
integer(kint) :: countParentFaces
type(Faces), intent(in) :: self
integer(kint), intent(in) :: index
!
logical(klog) :: keepGoing
integer(kint) :: parentIndex
countParentFaces = 0
keepGoing = ( self%parent(index) > 0 )
parentIndex = self%parent(index)
do while ( keepGoing )
countParentFaces = countParentFaces + 1
parentIndex = self%parent( parentIndex )
keepGoing = ( parentIndex > 0 )
enddo
end function
function SharedEdge( self, face1, face2 )
integer(kint) :: SharedEdge
type(Faces), intent(in) :: self
integer(kint), intent(in) :: face1, face2
!
integer(kint) :: i, j, shareCount
SharedEdge = 0
shareCount = 0
do i = 1, 3
do j = 1, 3
if ( self%edges(i, face1) == self%edges(j, face2) ) then
shareCount = shareCount + 1
SharedEdge = self%edges(j,face2)
endif
enddo
enddo
if ( shareCount > 1 ) then
call LogMessage(log, ERROR_LOGGING_LEVEL, trim(logkey)//" SharedEdge ERROR at face ", face1)
SharedEdge = 0
endif
end function
function ParticleOppositeTriEdge( self, faceIndex, edgeIndex )
integer(kint) :: ParticleOppositeTriEdge
type(Faces), intent(in) :: self
integer(kint), intent(in) :: faceIndex
integer(kint), intent(in) :: edgeIndex
!
integer(kint) :: i, foundEdge
foundEdge = 0
ParticleOppositeTriEdge = 0
do i = 1, 3
if ( self%edges(i, faceIndex) == edgeIndex ) then
foundEdge = i
exit
endif
enddo
if ( foundEdge == 1 ) then
ParticleOppositeTriEdge = self%vertices(3,faceIndex)
elseif ( foundEdge == 2 ) then
ParticleOppositeTriEdge = self%vertices(1,faceIndex)
elseif ( foundEdge == 3 ) then
ParticleOppositeTriEdge = self%vertices(2,faceIndex)
else
call LogMessage(log, ERROR_LOGGING_LEVEL, trim(logKey)//" ParticleOppositeTriEdge ERROR : ", "edge not found.")
endif
end function
subroutine WriteFacesToVTKPolygons( self, fileunit )
type(Faces), intent(in) :: self
integer(kint), intent(in) :: fileunit
!
integer(kint) :: i, j, nCells, cellListSize, nVerts
if ( self%faceKind == TRI_PANEL ) then
nVerts = 3
elseif ( self%faceKind == QUAD_PANEL ) then
nVerts = 4
endif
nCells = nVerts * self%N_Active
cellListSize = 4 * nCells
write(fileunit, '(A,I8,A,I8)') "POLYGONS ", nCells, " ", cellListSize
do i = 1, self%N
if ( .NOT. self%hasChildren(i) ) then
do j = 1, nVerts
write(fileunit,'(4I10)') 3, self%vertices(j,i)-1, self%vertices( mod(j,nVerts) + 1, i)-1, self%centerParticle(i)-1
enddo
endif
enddo
end subroutine
subroutine WriteFaceAreaToVTKCellData(self, aParticles, fileunit )
type(Faces), intent(in) :: self
type(Particles), intent(in) :: aParticles
integer(kint), intent(in) :: fileunit
!
integer(kint) :: i, j, nCells, nVerts
if ( self%faceKind == TRI_PANEL ) then
nVerts = 3
elseif ( self%faceKind == QUAD_PANEL ) then
nVerts = 4
endif
nCells = nVerts * self%N_Active
write(fileunit,'(A,I8)') "CELL_DATA ", nCells
write(fileunit,'(A)') "SCALARS faceArea double 1"
write(fileunit,'(A)') "LOOKUP_TABLE default"
do i = 1, self%N
if ( .NOT. self%hasChildren(i) ) then
do j = 1, nVerts
write(fileunit,*) aParticles%area( self%centerParticle(i) )
enddo
endif
enddo
end subroutine
subroutine WriteFacesToMatlab( self, fileunit )
type(Faces), intent(in) :: self
integer(kint), intent(in) :: fileunit
!
integer(kint) :: i, j, nVerts
if ( self%faceKind == TRI_PANEL ) then
nVerts = 3
elseif ( self%faceKind == QUAD_PANEL ) then
nVerts = 4
else
nVerts = 0
endif
write(fileunit, '(A)', advance='NO') "faceVerts = ["
do j = 1, nVerts - 1
write(fileunit,'(I8,A)', advance = 'NO') self%vertices(j,1), ", "
enddo
write(fileunit,'(I8,A)') self%vertices(nVerts,1), "; ..."
do i = 2, self%N-1
do j = 1, nVerts - 1
write(fileunit,'(I8,A)',advance = 'NO') self%vertices(j,i), ", "
enddo
write(fileunit,'(I8,A)' ) self%vertices(nVerts,i), "; ..."
enddo
do j = 1, nVerts - 1
write(fileunit,'(I8,A)',advance='NO') self%vertices(j, self%N), ", "
enddo
write(fileunit,'(I8,A)' ) self%vertices(nVerts,self%N), "]; "
write(fileunit, '(A)', advance='NO') "faceEdges = ["
do j = 1, nVerts - 1
write(fileunit,'(I8,A)', advance = 'NO') self%edges(j,1), ", "
enddo
write(fileunit,'(I8,A)') self%edges(nVerts,1), "; ..."
do i = 2, self%N-1
do j = 1, nVerts - 1
write(fileunit,'(I8,A)',advance = 'NO') self%edges(j,i), ", "
enddo
write(fileunit,'(I8,A)' ) self%edges(nVerts,i), "; ..."
enddo
do j = 1, nVerts - 1
write(fileunit,'(I8,A)',advance='NO') self%edges(j, self%N), ", "
enddo
write(fileunit,'(I8,A)' ) self%edges(nVerts,self%N), "]; "
write(fileunit,'(A)',advance='NO') "faceHasChildren = ["
if ( self%hasChildren(1) ) then
write(fileunit,*) 1, ", ..."
else
write(fileunit,*) 0, ", ..."
endif
do i = 2, self%N-1
if ( self%hasChildren(i) ) then
write(fileunit,*) 1, ", ..."
else
write(fileunit,*) 0, ", ..."
endif
enddo
if ( self%hasChildren(self%N) ) then
write(fileunit,'(I4)',advance='NO') 1
else
write(fileunit,'(I4)',advance='NO') 0
endif
write(fileunit,'(A)') "];"
write(fileunit,*) "faceCenterParticle = [", self%centerParticle(1), ", ..."
do i = 2, self%N-1
write(fileunit,*) self%centerParticle(i), ", ..."
enddo
write(fileunit,*) self%centerParticle(self%N), "]; "
end subroutine
subroutine DivideQuadFace( self, faceIndex, aParticles, anEdges )
type(Faces), intent(inout) :: self
integer(kint), intent(in) :: faceIndex
type(Particles), intent(inout) :: aParticles
type(Edges), intent(inout) :: anEdges
!
integer(kint) :: i, j, newFaceVerts(4,4), newFaceEdges(4,4)
integer(kint) :: parentEdge, childEdge1, childEdge2
real(kreal) :: quadCoords(3,4), lagQuadCoords(3,4), newFaceCenters(3,4), newFaceLagCenters(3,4)
if ( self%faceKind /= QUAD_PANEL ) then
call LogMessage(log, ERROR_LOGGING_LEVEL, logkey, " DivideQuadFace ERROR : invalid faceKind.")
return
endif
if ( self%N_Max < self%N + 4 ) then
call LogMessage(log, ERROR_LOGGING_LEVEL, logkey, " DivideQuadFace ERROR : not enough memory.")
return
endif
newFaceVerts = 0
newFaceEdges = 0
!
! connect parent vertices to child faces
!
do i = 1, 4
newFaceVerts(i,i) = self%vertices(i,faceIndex)
enddo
!
! loop over parent edges
!
do i = 1, 4
parentEdge = self%edges(i, faceIndex)
if ( anEdges%hasChildren(parentEdge) ) then
!
! parent edge was already divided by adjacent panel
!
childEdge1 = anEdges%child1(parentEdge)
childEdge2 = anEdges%child2(parentEdge)
else
!
! parent edge needs to be divided
!
childEdge1 = anEdges%N + 1
childEdge2 = anEdges%N + 2
call DivideEdge(anEdges, parentEdge, aParticles)
endif
!
! connect child edges to new child faces
!
if ( positiveEdge(anEdges, faceIndex, parentEdge) ) then
newFaceEdges(i, i ) = childEdge1
anEdges%leftFace(childEdge1) = self%N + i
newFaceEdges(i, mod(i,4) + 1) = childEdge2
anEdges%leftFace(childEdge2) = self%N + mod(i,4) + 1
else
newFaceEdges(i,i) = childEdge2
anEdges%rightFace(childEdge2) = self%N + i
newFaceEdges(i, mod(i,4) + 1) = childEdge1
anEdges%rightFace(childEdge1) = self%N + mod(i,4) + 1
endif
newFaceVerts( mod(i,4) + 1, i ) = anEdges%dest(childEdge1)
newFaceVerts( i, mod(i,4) + 1 ) = anEdges%dest(childEdge1)
enddo
!
! change parent center particle to vertex of new child panels
!
do i = 1, 4
newFaceVerts( mod(i+1,4) + 1, i) = self%centerParticle(faceIndex)
enddo
!
! debugging : check vertex connectivity
!
do i = 1, 4
do j = 1, 4
if ( newFaceVerts(j,i) < 1 ) then
write(logstring,*) " vertex connectivity ERROR at parent face ", faceIndex, ", child ", i, ", vertex ", j
call LogMessage(log, ERROR_LOGGING_LEVEL,logkey//" DivideQuadFace :",logstring)
endif
enddo
enddo
!
! create new interior edges
!
call InsertEdge( anEdges, aParticles, newFaceVerts(2,1), newFaceVerts(3,1), self%N+1, self%N+2)
newFaceEdges(2,1) = anEdges%N
newFaceEdges(4,2) = anEdges%N
call InsertEdge( anEdges, aParticles, newFaceVerts(1,3), newFaceVerts(4,3), self%N+4, self%N+3)
newFaceEdges(4,3) = anEdges%N
newFaceEdges(2,4) = anEdges%N
call InsertEdge( anEdges, aParticles, newFaceVerts(3,2), newFaceVerts(4,2), self%N+2, self%N+3)
newFaceEdges(3,2) = anEdges%N
newFaceEdges(1,3) = anEdges%N
call InsertEdge( anEdges, aParticles, newFaceVerts(2,4), newFaceVerts(1,4), self%N+1, self%N+4)
newFaceEdges(1,4) = anEdges%N
newFaceEdges(3,1) = anEdges%N
!
! debugging : check edge connectivity
!
do i = 1, 4
do j = 1, 4
if ( newFaceEdges(j,i) < 1 ) then
write(logstring,*) " edge connectivity ERROR at parent face ", faceIndex, ", child ", i, ", vertex ", j
call LogMessage(log, ERROR_LOGGING_LEVEL, logkey//" DivideQuadFace :",logstring)
endif
enddo
enddo
!
! create new center particles for child faces
!
quadCoords = 0.0_kreal
lagQuadCoords = 0.0_kreal
newFaceCenters = 0.0_kreal
newFaceLagCenters = 0.0_kreal
do i = 1, 4
do j = 1, 4
quadCoords(:,j) = PhysCoord(aParticles, newFaceVerts(j,i))
lagQuadCoords(:,j) = LagCoord(aParticles, newFaceVerts(j,i))
enddo
if ( aParticles%geomKind == PLANAR_GEOM ) then
newFaceCenters(1:2,i) = QuadCentroid( quadCoords(1:2,1), quadCoords(1:2,2), quadCoords(1:2,3), quadCoords(1:2,4))
newFaceLagCenters(1:2,i) = QuadCentroid( lagQuadCoords(1:2,1), lagQuadCoords(1:2,2), lagQuadCoords(1:2,3), lagQuadCoords(1:2,4))
elseif ( aParticles%geomKind == SPHERE_GEOM ) then
newFaceCenters(:,i) = SphereQuadCenter( quadCoords(:,1), quadCoords(:,2), quadCoords(:,3), quadCoords(:,4))
newFaceLagCenters(:,i) = SphereQuadCenter( lagQuadCoords(:,1), lagQuadCoords(:,2), lagQuadCoords(:,3), lagQuadCoords(:,4))
else
call LogMessage(log,ERROR_LOGGING_LEVEL,logkey//" DivideQuadFace : ", "ERROR geomKind not implemented.")
return
endif
enddo
!
! create child faces
!
do i = 1, 4
call InsertParticle( aParticles, newFaceCenters(:,i), newFaceLagCenters(:,i))
call MakeParticleActive(aParticles, aParticles%N)
self%centerParticle(self%N+i) = aParticles%N
self%vertices(:,self%N+i) = newFaceVerts(:,i)
self%edges(:,self%N+i) = newFaceEdges(:,i)
self%children(i,faceIndex) = self%N + i
self%parent(self%N+i) = faceIndex
aParticles%area(self%centerParticle(self%N+i)) = QuadFaceArea(self, self%N+i, aParticles)
enddo
call MakeParticlePassive(aParticles, self%centerParticle(faceIndex) )
aParticles%area(self%centerParticle(faceIndex)) = 0.0_kreal
self%hasChildren(faceIndex) = .TRUE.
self%N = self%N + 4
self%N_Active = self%N_Active + 3
end subroutine
subroutine DivideTriFace( self, faceIndex, aParticles, anEdges )
type(Faces), intent(inout) :: self
integer(kint), intent(in) :: faceIndex
type(Particles), intent(inout) :: aParticles
type(Edges), intent(inout) :: anEdges
!
integer(kint) :: i, j, newFaceVerts(3,4), newFaceEdges(3,4)
integer(kint) :: parentEdge, childEdge1, childEdge2
real(kreal) :: triCoords(3,3), lagTriCoords(3,3), newFaceCenters(3,4), newFaceLagCenters(3,4)
if ( self%faceKind /= TRI_PANEL ) then
call LogMessage(log, ERROR_LOGGING_LEVEL, logkey//" DivideTriFace ERROR : "," invalid faceKind.")
return
endif
if ( self%N_Max < self%N + 4 ) then
call LogMessage(log, ERROR_LOGGING_LEVEL, logkey//" DivideTriFace ERROR : ", " not enough memory.")
return
endif
newFaceVerts = 0
newFaceEdges = 0
!
! connect parent vertices to child faces
!
do i = 1, 3
newFaceVerts(i,i) = self%vertices(i,faceIndex)
enddo
!
! loop over parent edges
!
do i = 1, 3
parentEdge = self%edges(i,faceIndex)
if ( anEdges%hasChildren(parentEdge) ) then
!
! parent edge already divided by adjacent panel
!
childEdge1 = anEdges%child1(parentEdge)
childEdge2 = anEdges%child2(parentEdge)
else
!
! divide parent edge
!
childEdge1 = anEdges%N + 1
childEdge2 = anEdges%N + 2
call DivideEdge(anEdges, parentEdge, aParticles)
endif
!
! connect child edges to new child faces
!
if ( positiveEdge( anEdges, faceIndex, parentEdge ) ) then
newFaceEdges(i, i) = childEdge1
anEdges%leftFace(childEdge1) = self%N + i
newFaceEdges(i, mod(i,3)+1) = childEdge2
anEdges%leftFace(childEdge2) = self%N + mod(i,3) + 1
else
newFaceEdges(i,i) = childEdge2
anEdges%rightFace(childEdge2) = self%N + i
newFaceEdges(i,mod(i,3)+1) = childEdge1
anEdges%rightFace(childEdge1) = self%N + mod(i,3) + 1
endif
newFaceVerts( i, mod(i,3) + 1) = anEdges%dest(childEdge1)
newFaceVerts( mod(i,3) + 1, i) = anEdges%dest(childEdge1)
enddo
newFaceVerts(:,4) = [ newFaceVerts(3,2), newFaceVerts(1,3), newFaceVerts(2,1) ]
!
! debugging : check vertex connectivity
!
do i = 1, 4
do j = 1, 3
if ( newFaceVerts(j,i) < 1 ) then
write(logstring,*) " vertex connectivity ERROR at parent face ", faceIndex, ", child ", i, ", vertex ",j
call LogMessage(log,ERROR_LOGGING_LEVEL,logkey//" DivideTriFace :", logString )
endif
enddo
enddo
!
! create new interior edges
!
call InsertEdge( anEdges, aParticles, newFaceVerts(1,4), newFaceVerts(2,4), self%N+4, self%N+3)
newFaceEdges(1,4) = anEdges%N
newFaceEdges(1,3) = anEdges%N
call InsertEdge( anEdges, aParticles, newFaceVerts(2,4), newFaceVerts(3,4), self%N+4, self%N+1)
newFaceEdges(2,4) = anEdges%N
newFaceEdges(2,1) = anEdges%N
call InsertEdge( anEdges, aParticles, newFaceVerts(3,4), newFaceVerts(1,4), self%N+4, self%N+2)
newFaceEdges(3,4) = anEdges%N
newFaceEdges(3,2) = anEdges%N
!
! debugging : check edge connectivity
!
do i = 1, 4
do j = 1, 3
if ( newFaceEdges(j,i) < 1 ) then
write(logstring,*) " edge connectivity ERROR at parent face ", faceIndex, ", child ", i, ", vertex ", j
call LogMessage(log, ERROR_LOGGING_LEVEL, logkey//" DivideTriFace :", logString)
endif
enddo
enddo
!
! create new center particles for child faces 1:3
!
triCoords = 0.0_kreal
lagTriCoords = 0.0_kreal
newFaceCenters = 0.0_kreal
newFaceLagCenters = 0.0_kreal
do i = 1, 4
do j = 1, 3
triCoords(:,j) = PhysCoord(aParticles, newFaceVerts(j,i))
lagTriCoords(:,j)=LagCoord(aParticles, newFaceVerts(j,i))
enddo
if ( aParticles%geomKind == PLANAR_GEOM ) then
newFaceCenters(1:2,i) = TriCentroid( triCoords(1:2,1), triCoords(1:2,2), triCoords(1:2,3) )
newFaceLagCenters(1:2,i) = TriCentroid( lagTriCoords(1:2,1), lagTriCoords(1:2,2), lagTriCoords(1:2,3))
elseif ( aParticles%geomKind == SPHERE_GEOM ) then
newFaceCenters(:,i) = SphereTriCenter( triCoords(:,1), triCoords(:,2), triCoords(:,3))
newFaceLagCenters(:,i) = SphereTriCenter(lagTriCoords(:,1),lagTriCoords(:,2), lagTriCoords(:,3))
else
call LogMessage(log, ERROR_LOGGING_LEVEL, logkey//" DivideTriFace ERROR : "," geomkind not implemented.")
return
endif
enddo
!
! create child faces
!
do i = 1, 3
call InsertParticle( aParticles, newFaceCenters(:,i), newFaceLagCenters(:,i))
call MakeParticleActive( aParticles, aParticles%N)
self%centerParticle( self%N + i) = aParticles%N
self%vertices(:,self%N+i) = newFaceVerts(:,i)
self%edges(:,self%N+i) = newFaceEdges(:,i)
self%children(i,faceIndex) = self%N+i
self%parent(self%N+i) = faceIndex
aParticles%area(self%centerParticle(self%N+i)) = TriFaceArea(self, self%N+i, aParticles)
enddo
! child 4 is special case : reuse parent face center particle
self%centerParticle( self%N + 4 ) = self%centerParticle(faceIndex)
self%vertices(:,self%N+4) = newFaceVerts(:,4)
self%edges(:,self%N + 4) = newFaceEdges(:,4)
self%children(4,faceIndex) = self%N + 4
self%parent(self%N+4) = faceIndex
aParticles%area(self%centerParticle(self%N+4)) = TriFaceArea(self, self%N+4, aParticles)
self%hasChildren(faceIndex) = .TRUE.
self%N = self%N + 4
self%N_Active = self%N_Active + 3
end subroutine
function FaceArea( self, index, aParticles, anEdges )
real(kreal) :: FaceArea
type(Faces), intent(in) :: self
integer(kint), intent(in) :: index
type(Particles), intent(in) :: aParticles
type(Edges), intent(in) :: anEdges
!
type(STDIntVector) :: leafEdges
integer(kint) :: i, nEdges
nEdges = self%faceKind
FaceArea = 0.0_kreal
do i = 1, nEdges
call GetLeafEdgesFromParent( anEdges, self%edges(i, index), leafEdges )
FaceArea = FaceArea + AreaFromLeafEdges( anEdges, aParticles, self%centerParticle(index), &
leafEdges%integers(1:leafEdges%N), leafEdges%N)
enddo
end function
subroutine InsertFace( self, centerParticle, vertIndices, edgeIndices )
type(Faces), intent(inout) :: self
integer(kint), intent(in) :: centerParticle
integer(kint), intent(in) :: vertIndices(:)
integer(kint), intent(in) :: edgeIndices(:)
if ( self%N+1 > self%N_Max ) then
call LogMessage(log, ERROR_LOGGING_LEVEL, logkey," InsertFace ERROR : not enough memory.")
return
endif
if ( size(vertIndices) /= size(edgeIndices) .OR. size(vertIndices) /= size(self%vertices,1) ) then
call LogMessage(log, ERROR_LOGGING_LEVEL, logkey, " InsertFace ERROR : vertex and edge size mismatch.")
return
endif
self%centerParticle(self%N+1) = centerParticle
self%vertices(:,self%N+1) = vertIndices
self%edges(:,self%N+1) = edgeIndices
self%N = self%N + 1
end subroutine
function QuadFaceArea( self, index, aParticles )
real(kreal) :: QuadFaceArea
type(Faces), intent(in) :: self
integer(kint), intent(in) :: index
type(Particles), intent(in) :: aParticles
!
integer(kint) :: i
real(kreal) :: centerVec(3), v1(3), v2(3)
QuadFaceArea = 0.0_kreal
centerVec = PhysCoord(aParticles, self%centerParticle(index))
if ( aParticles%geomKind == PLANAR_GEOM ) then
do i = 1, 4
v1 = PhysCoord(aParticles, self%vertices(i, index))
v2 = PhysCoord(aParticles, self%vertices(mod(i,4) + 1, index))
QuadFaceArea = QuadFaceArea + TriArea(v1(1:2), centerVec(1:2), v2(1:2) )
enddo
elseif ( aParticles%geomKind == SPHERE_GEOM ) then
do i = 1, 4
v1 = PhysCoord(aParticles, self%vertices(i, index))
v2 = PhysCoord(aParticles, self%vertices(mod(i,4) + 1, index))
QuadFaceArea = QuadFaceArea + SphereTriArea( v1, centerVec, v2 )
enddo
endif
end function
function TriFaceArea( self, index, aParticles )
real(kreal) :: TriFaceArea
type(Faces), intent(in) :: self
integer(kint), intent(in) :: index
type(Particles), intent(in) :: aParticles
!
integer(kint) :: i
real(kreal) :: centerVec(3), v1(3), v2(3)
TriFaceArea = 0.0_kreal
centerVec = PhysCoord(aParticles, self%centerParticle(index))
if ( aParticles%geomKind == PLANAR_GEOM ) then
do i = 1, 3
v1 = PhysCoord(aParticles, self%vertices(i, index))
v2 = PhysCoord(aParticles, self%vertices(mod(i,3) + 1, index))
TriFaceArea = TriFaceArea + TriArea(v1(1:2), centerVec(1:2), v2(1:2) )
enddo
elseif ( aParticles%geomKind == SPHERE_GEOM ) then
do i = 1, 3
v1 = PhysCoord(aParticles, self%vertices(i, index))
v2 = PhysCoord(aParticles, self%vertices(mod(i,3) + 1, index))
TriFaceArea = TriFaceArea + SphereTriArea( v1, centerVec, v2 )
enddo
endif
end function
function FaceCentroid(self, index, aParticles )
real(kreal) :: FaceCentroid(3)
type(Faces), intent(in) :: self
integer(kint), intent(in) :: index
type(Particles), intent(in) :: aParticles
!
integer(kint) :: nVerts, i
!call LogMessage(log,DEBUG_LOGGING_LEVEL,trim(logKey)//" FaceCentroid : ", "entering.")
FaceCentroid = 0.0_kreal
nVerts = 0
if ( self%faceKind == TRI_PANEL ) then
nVerts = 3
elseif( self%faceKind == QUAD_PANEL) then
nVerts = 4
endif
do i = 1, nVerts
FaceCentroid(1) = FaceCentroid(1) + aParticles%x( self%vertices(i,index) )
FaceCentroid(2) = FaceCentroid(2) + aParticles%y( self%vertices(i,index) )
if ( aParticles%geomKind /= PLANAR_GEOM ) &
FaceCentroid(3) = FaceCentroid(3) + aParticles%z(self%vertices(i,index))
enddo
FaceCentroid = FaceCentroid / real(nVerts,kreal)
if ( aParticles%geomKind == SPHERE_GEOM ) then
FaceCentroid = FaceCentroid / sqrt(sum(FaceCentroid*FaceCentroid)) * SphereRadius
endif
end function
subroutine InitLogger(aLog,rank)
! Initialize a logger for this module and processor
type(Logger), intent(out) :: aLog
integer(kint), intent(in) :: rank
write(logKey,'(A,A,I0.3,A)') trim(logKey),'_',rank,' : '
call New(aLog,logLevel)
logInit = .TRUE.
end subroutine
!> @}
end module