forked from mdolab/pyhyp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup3d.F90
1230 lines (1034 loc) · 48 KB
/
setup3d.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
subroutine setup(fileName, fileType)
!***DESCRIPTION
!
! Written by Gaetan Kenway
!
! Abstract: setup3d is the main routine for setting up the 3D
! elliptic or hyperbolic mesh extrusion problem. The only input
! is the filename of an ascii-formatted plot3d file and
! information about a possible mirror.
!
! Ney Secco (2015-2016): Added boundary conditions and CGNS interface
!
! Description of Arguments
! Input:
! fileName : char* array : Filename of plot3d or cgns file.
! fileType : integer : 1-CGNS 2-Plot3d
use communication
use hypData
use hypInput
implicit none
! Input Arguments
character*(*), intent(in) :: fileName
integer(kind=intType), intent(in) :: fileType
! Working parameters
real(kind=realType), dimension(:, :), allocatable :: uniquePts, allEdgeNodes
integer(kind=intType), dimension(:), allocatable :: link, ptInd, nFace, localFace
integer(kind=intType), dimension(:), allocatable :: nte, ntePtr
integer(kind=intType), dimension(:, :), allocatable :: fullnPtr
integer(kind=intType), dimension(:, :), allocatable :: nodeConn
integer(kind=intType), dimension(:, :), allocatable :: directedNodeConn
integer(kind=intType), dimension(:), allocatable :: nEdge, nDirectedEdge
integer(kind=intType) :: nBlocks, nUnique, corners(4), iCorner
integer(kind=intType) :: nodeTotal, node1, node2, node3, node4
integer(kind=intType) :: iNode, evenNodes, minLevel, skip
integer(kind=intType) :: NODE_MAX, CELL_MAX
integer(kind=intType) :: nFaceNeighbours, firstID, nodeID, nextElemID
integer(kind=intType) :: nodeToAdd, faceToCheck, nodeToFind
integer(kind=intType) :: i, j, k, ii, jj, iii, jjj, ierr, iStart, iEnd
integer(kind=intType) :: isize, ind, icell, idim, BCToSet, il, jl, iEdge, iBCToSet
logical :: found
logical :: bcTopoError
logical :: bcError
character(5) :: faceStr
integer(kind=intType), dimension(:), pointer :: lPtr1, lPtr2
real(kind=realType), dimension(:, :), pointer :: xPtr, xPtrRowInward
real(kind=realType) :: xSum(3), vals(2, 3)
integer(kind=intType) :: mask(2, 3), nn(2), mm(2), f(3), iFace, jp2
! Only the root processor reads the mesh and does the initial processing:
rootProc: if (myid == 0) then
fType: if (fileType .eq. cgnsFileType) then ! We have a CGNS file
call readCGNS(fileName)
else if (fileType .eq. plot3dFileType) then ! We have a Plot3d file
call readPlot3d(fileName)
else if (fileType .eq. patchInput) then
! Nothing extra to do.
end if fType
! Before continue, we may need to possibly coarsen the input
! grids.
! First figure out how many times we can coarsen to prevent
minLevel = 100 ! Arbitrarly large
do ii = 1, nPatch
minLevel = min(minLevel, mgLevel(patchIO(ii)%il))
minLevel = min(minLevel, mgLevel(patchIO(ii)%jl))
end do
if (coarsen > minLevel) then
print *, ' ERROR: User specified coarsen is ', coarsen
print *, ' Can only coarsen ', minLevel, 'levels.'
stop
end if
! We now know is it 'safe' to coarsen the supplied number of levels.
skip = 2**(coarsen - 1)
allocate (patches(nPatch))
do ii = 1, nPatch
! Update the sizes
patches(ii)%il = (patchIO(ii)%il - 1) / skip + 1
patches(ii)%jl = (patchIO(ii)%jl - 1) / skip + 1
! Allocate the final sizes
allocate (patches(ii)%X(3, patches(ii)%il, patches(ii)%jl))
allocate (patches(ii)%l_index(patches(ii)%il, patches(ii)%jl))
allocate (patches(ii)%weights(patches(ii)%il, patches(ii)%jl))
patches(ii)%weights(:, :) = zero
! Copy in the values
do j = 1, patches(ii)%jl
do i = 1, patches(ii)%il
patches(ii)%X(:, i, j) = patchIO(ii)%X(:, (i - 1) * skip + 1, (j - 1) * skip + 1)
end do
end do
! Deallocate X the patchIO memory.
deallocate (patchIO(ii)%X)
end do
deallocate (patchIO)
! Now we can create the final connectivity
nodeTotal = 0
faceTotal = 0
do ii = 1, nPatch
nodeTotal = nodeTotal + patches(ii)%il * patches(ii)%jl
faceTotal = faceTotal + (patches(ii)%il - 1) * (patches(ii)%jl - 1)
end do
allocate (allEdgeNodes(3, nodeTotal), uniquePts(3, nodeTotal), link(nodeTotal))
! Assign edge nodes to allEdgeNodes
iNode = 0
do ii = 1, nPatch
do j = 1, patches(ii)%jl, patches(ii)%jl - 1
do i = 1, patches(ii)%il
iNode = iNode + 1
allEdgeNodes(:, iNode) = patches(ii)%X(:, i, j)
end do
end do
do i = 1, patches(ii)%il, patches(ii)%il - 1
do j = 2, patches(ii)%jl - 1
iNode = iNode + 1
allEdgeNodes(:, iNode) = patches(ii)%X(:, i, j)
end do
end do
end do
! Now we will call pointReduce to remove any repeated edge nodes.
! link maps from allEdgeNodes to uniquePts
if (noPointReduce) then
uniquePts = allEdgeNodes
nUnique = iNode
do i = 1, iNode
link(i) = i
end do
else
call pointReduce(allEdgeNodes, iNode, nodeTol, uniquePts, link, nUnique)
end if
! Dump edge/corner into l_index
iNode = 0
do ii = 1, nPatch
do j = 1, patches(ii)%jl, patches(ii)%jl - 1
do i = 1, patches(ii)%il
iNode = iNode + 1
patches(ii)%l_index(i, j) = link(iNode)
end do
end do
do i = 1, patches(ii)%il, patches(ii)%il - 1
do j = 2, patches(ii)%jl - 1
iNode = iNode + 1
patches(ii)%l_index(i, j) = link(iNode)
end do
end do
end do
! Now fill in the interior points into uniquePts/l_index
iNode = nUnique !Up to here nUnique only has the number of unique edge nodes
do ii = 1, nPatch
do j = 2, patches(ii)%jl - 1
do i = 2, patches(ii)%il - 1
iNode = iNode + 1
uniquePts(:, iNode) = patches(ii)%X(:, i, j)
patches(ii)%l_index(i, j) = iNode
end do
end do
end do
nUnique = iNode ! Now nUnique has the number of unique edge and interior nodes
write (*, "(a)", advance="no") '#--------------------#'
print "(1x)"
write (*, "(a)", advance="no") " Total Nodes: "
write (*, "(I7,1x)", advance="no") nodeTotal
print "(1x)"
write (*, "(a)", advance="no") " Unique Nodes:"
write (*, "(I7,1x)", advance="no") nUnique
print "(1x)"
write (*, "(a)", advance="no") " Total Faces: "
write (*, "(I7,1x)", advance="no") faceTotal
print "(1x)"
write (*, "(a)", advance="no") '#--------------------#'
print "(1x)"
! Free up all allocated memory up to here:
deallocate (link, allEdgeNodes)
! Create the conn array
allocate (fullConn(4, faceTotal))
jj = 0
do ii = 1, nPatch
do j = 1, patches(ii)%jl - 1
do i = 1, patches(ii)%il - 1
jj = jj + 1
fullConn(:, jj) = (/ &
patches(ii)%l_index(i, j), &
patches(ii)%l_index(i + 1, j), &
patches(ii)%l_index(i + 1, j + 1), &
patches(ii)%l_index(i, j + 1)/)
end do
end do
end do
! Determine if the normals are consistent:
print *, 'Normal orientation check ...'
! Allocate and initialize matrix that stores nodal connectivity directions
! I'm assuming that the maximum number of connections of a single node
! is 10
allocate (directedNodeConn(10, nUnique))
allocate (nodeConn(10, nUnique))
allocate (nEdge(nUnique), nDirectedEdge(nUnique))
nodeConn = 0
directedNodeConn = 0
nEdge = 0
nDirectedEdge = 0
! Loop over each face and increment elementes from the connectivity
! matrix acording to the connectivity direction.
do jj = 1, faceTotal
! Get nodes indices for this face
node1 = fullConn(1, jj)
node2 = fullConn(2, jj)
node3 = fullConn(3, jj)
node4 = fullConn(4, jj)
! Assign connections acording to their orientations
! The two routines are in 3D_utilities.F90
call assignN2NDirected(directedNodeConn, nDirectedEdge, node1, node2, 10, nUnique)
call assignN2NDirected(directednodeConn, nDirectedEdge, node2, node3, 10, nUnique)
call assignN2NDirected(directedNodeConn, nDirectedEdge, node3, node4, 10, nUnique)
call assignN2NDirected(directedNodeConn, nDirectedEdge, node4, node1, 10, nUnique)
!Here we must add two edges for each
call assignN2N(nodeConn, nEdge, node1, node2, 10, nUnique)
call assignN2N(nodeConn, nEdge, node1, node4, 10, nUnique)
call assignN2N(nodeConn, nEdge, node2, node3, 10, nUnique)
call assignN2N(nodeConn, nEdge, node2, node1, 10, nUnique)
call assignN2N(nodeConn, nEdge, node3, node4, 10, nUnique)
call assignN2N(nodeConn, nEdge, node3, node2, 10, nUnique)
call assignN2N(nodeConn, nEdge, node4, node1, 10, nUnique)
call assignN2N(nodeConn, nEdge, node4, node3, 10, nUnique)
end do
! Now check if we flagged any node during the assignments
if (minval(directedNodeConn) .eq. -1) then
! Say that the normals are wrong
print *, ' ERROR: Normal directions may be wrong'
print *, ' Check the following coordinates:'
! Print coordinates of nodes were the problem occurs
do i = 1, nUnique
if (directedNodeConn(1, i) .eq. -1) then
print *, uniquePts(:, i)
end if
end do
stop
else
! Say everything is ok
print *, 'Normals are consistent!'
end if
! We just determined the number of nodes conected to each
! node. Now we determine the number of faces connected to each
! node. These two pieces of information will determine the
! topology of the node.
allocate (nFace(nUnique))
nFace(:) = 0
do i = 1, faceTotal
do ii = 1, 4
nFace(fullConn(ii, i)) = nFace(fullConn(ii, i)) + 1
end do
end do
! Here we can determine the topology of every node in the
! mesh. The topology refers to the number of faces and edges each
! node is connected to.
print *, 'Determining topology ...'
allocate (fullTopoType(nUnique))
allocate (fullBCType(2, nUnique))
allocate (fullBCVal(2, 2, nUnique)) ! We can have 2 BC, and each
! can have 2 values.
! Initialze these to internal node, with defaultBC
fullTopoType = topoInternal
fullBCType = BCDefault
fullBCVal = huge(one) ! If we accidently use this and shouldn't,
! this should cause problems.
do i = 1, nUnique
if (nEdge(i) == 2 .and. nFace(i) == 1) then
! This is a corner
fullTopoType(i) = topoCorner
else if (nEdge(i) == 2 .and. nFace(i) == 2) then
! This is a degenerate corner. We can treat this as an
! internal node by using diagonals
fullTopoType(i) = topoInternal
else if (nEdge(i) == 3 .and. nface(i) == 2) then
! This is a regular edge
fullTopoType(i) = topoEdge
else if (nEdge(i) == 4 .and. nFace(i) == 2) then
! This is a bow-tie. We don't do that either.
print *, "Come on. A bow-tie? Really? No, we can't do that'"
stop
else if (nEdge(i) == 4 .and. nFace(i) == 3) then
! This is an L corner. In theory, we should be able to do
! this...but not yet
fullTopoType(i) = topoLCorner
else if (nEdge(i) == nFace(i)) then
! This is an internal node so we're ok.
fullTopoType(i) = topoInternal
else
! This is some really wonky combination which we assume we
! can't deal with. Flag this node.
fullTopoType(i) = topoUnknown
end if
end do
! Now check if we flagged any nodes during the assignments
if (minval(fullTopoType) .eq. topoUnknown) then
print *, ' ERROR: Unknown topology encountered'
print *, ' Check the following coordinates:'
! Print coordinates of flagged nodes
do i = 1, nUnique
if (fullTopoType(i) .eq. topoUnknown) then
write (*, '(*(1x,g0))') uniquePts(:, i), '(node has', nEdge(i), 'edges and', nFace(i), 'faces)'
end if
end do
stop
else
! Say everything is ok
print *, 'Topology complete.'
end if
! The nte stucture is like this:
!
! ntePtr(i)
! |
! V
! | face 1 | position of node i on face 1 | face 2 | position of node i on face 2 | ...
!
! This is why ntePtr is 2*sum(nFace) long
! Allocate the nodeToElem (nte) data array and the ptr array:
allocate (nte(2 * sum(nFace)), ntePtr(nUnique + 1))
ntePtr(1) = 1
do i = 1, nUnique
ntePtr(i + 1) = ntePtr(i) + nFace(i) * 2
end do
! Now fill-up the array
nFace(:) = 0
do i = 1, faceTotal
do ii = 1, 4
iNode = fullConn(ii, i)
jj = nFace(iNode)
! We store the index of the face, and index that this node
! is on the face
nte(ntePtr(iNode) + jj * 2) = i
nte(ntePtr(iNode) + jj * 2 + 1) = ii
! We need to keep track of the number we've already added.
nFace(iNode) = nFace(iNode) + 1
end do
end do
! nFace is now no longer needed...all the necessary info is in ntePtr
deallocate (nFace)
! Determine the maximum number of nodal neighbours. Node
! max must be at least 6 for the 3-corner nodes with a
! total of 6 cross-diagonal neighbours
NODE_MAX = 6
CELL_MAX = 0
do i = 1, nUnique
NODE_MAX = max(NODE_MAX, (ntePtr(i + 1) - ntePtr(i)) / 2)
CELL_MAX = max(CELL_MAX, (ntePtr(i + 1) - ntePtr(i)) / 2)
end do
! Finally we can get the final node pointer structure we need:
allocate (fullnPtr(NODE_MAX + 1, nUnique), fullcPtr(CELL_MAX + 1, nUnique))
nodeLoop: do iNode = 1, nUnique
internalNode: if (fullTopoType(iNode) == topoInternal) then
nFaceNeighbours = (ntePtr(iNode + 1) - ntePtr(iNode)) / 2
! Low-neighbour count nodes get doubles, others get all neighbours
if (nFaceNeighbours == 2 .or. nFaceNeighbours == 3) then
fullnPtr(1, iNode) = nFaceNeighbours * 2
else
fullnPtr(1, iNode) = nFaceNeighbours
end if
fullcPtr(1, iNode) = nFaceNeighbours
firstID = nte(ntePtr(iNode))
nodeID = mod(nte(ntePtr(iNode) + 1), 4) + 1
nextElemID = -1
iii = 2
jjj = 2
nodeLoopCycle: do while (nextElemID /= firstID)
if (nextElemID == -1) &
nextElemID = firstID
! Append the next node along that edge:
nodeToAdd = fullConn(mod(nodeID - 1, 4) + 1, nextElemID)
fullnPtr(iii, iNode) = nodeToAdd
fullcPtr(jjj, iNode) = nextElemID
iii = iii + 1
jjj = jjj + 1
! Add the diagonal if not regular node
if (nFaceNeighbours == 3 .or. nFaceNeighbours == 2) then
fullnPtr(iii, iNode) = fullConn(mod(nodeID, 4) + 1, nextElemID)
iii = iii + 1
end if
! And we need to find the face that contains the following node:
nodeToFind = fullConn(mod(nodeID + 1, 4) + 1, nextElemID)
found = .False.
jj = -1
findNextElement: do while (.not. found)
jj = jj + 1
! Find the next face
faceToCheck = nte(ntePtr(iNode) + jj * 2)
if (faceToCheck /= nextElemID) then
! Check the 4 nodes on this face
do ii = 1, 4
if (fullConn(ii, faceToCheck) == nodeToFind) then
nextElemID = faceToCheck
nodeID = ii
found = .True.
end if
end do
end if
end do findNextElement
end do nodeLoopCycle
else
! Not an internal node. Set all values to zero so we know
! they have not been processed yet:
fullNPtr(:, iNode) = 0
fullCPtr(:, iNode) = 0
end if internalNode
end do nodeLoop
! Check to make sure that there are only edge and interneral
! topology nodes if unattachedEdgesAreSymmetry is true: This can
! only be used for mirrored cases where all non interior nodes
! MUST be edges
if (unattachedEdgesAreSymmetry) then
do i = 1, nUnique
if (fullTopoType(i) /= topoInternal .and. fullTopoType(i) /= topoEdge) then
print *, "ERROR: A free corner or other topology that is not an "
print *, "edge was detected with the unattachedEdgesAreSymmetry option."
print *, "This option can only be used for configurations that become "
print *, "closed when mirrored."
stop
end if
end do
end if
! We have now set the fullNodePointer (fullnPtr) and the
! fullCellPointer (fullCPtr) for all internal nodes. These were
! straightforward since by definition they are fully surrounded
! by elements. We now have to set the nodes on boundaries. Since
! we will need to know the local structured topology it is easier
! to loop through the edges of the patches themselves and push
! the information to global node.
! First figure out which of the BCDefault edges are actually
! physical edges and assign them to be BCSplay OR Symmetry if the
! 'unattachedEdgesAreSymmetry" is true.
patchLoop0: do ii = 1, nPatch
il = patches(ii)%il
jl = patches(ii)%jl
edgeLoop0: do iEdge = 1, 4
select case (iEdge)
case (iLow)
iSize = jl
lPtr1 => patches(ii)%l_index(1, jl:1:-1)
xPtr => patches(ii)%x(:, 1, jl:1:-1)
case (iHigh)
iSize = jl
lPtr1 => patches(ii)%l_index(il, :)
xPtr => patches(ii)%x(:, il, :)
case (jLow)
iSize = il
lPtr1 => patches(ii)%l_index(:, 1)
xPtr => patches(ii)%x(:, :, 1)
case (jHigh)
iSize = il
lPtr1 => patches(ii)%l_index(il:1:-1, jl)
xPtr => patches(ii)%x(:, il:1:-1, jl)
end select
! Just check the n=2 node:
if (iSize > 2) then
i = 2
iNode = lPtr1(i)
if (fullTopoType(iNode) == topoEdge .and. BCs(iEdge, ii) == BCDefault) then
if (unattachedEdgesAreSymmetry) then
! Determine which symm type we need:
xSum(1) = sum(abs(xptr(1, :)))
xSum(2) = sum(abs(xptr(2, :)))
xSum(3) = sum(abs(xptr(3, :)))
! take dimension with lowest val
i = minloc(xSum, 1)
! However, if there are two dimensions with both xSum components
! being 0, we have the fringe case where an edge is directly
! on top of the coordinate axis.
! We need to look at the normals to find out which direction
! has the actual symmetry plane.
! Note the mod indexing only works in a zero base.
if (((xSum(mod(i, 3) + 1) - xSum(i)) < 1.e-10) .or. &
((xSum(mod(i + 1, 3) + 1) - xSum(i)) < 1.e-10)) then
print *, ""
print *, "WARNING: 'unattachedEdgesAreSymmetry' may fail when an"
print *, "edge is coincident with a coordinate axis. Set the"
print *, "symmetry BCs manually with the 'BC' option if you"
print *, "encounter negative volumes near the symmetry plane."
print *, ""
! Get the row of coordinates one inboard from the edge of the patch.
select case (iEdge)
case (iLow)
xPtrRowInward => patches(ii)%x(:, 2, jl:1:-1)
case (iHigh)
xPtrRowInward => patches(ii)%x(:, il - 1, :)
case (jLow)
xPtrRowInward => patches(ii)%x(:, :, 2)
case (jHigh)
xPtrRowInward => patches(ii)%x(:, il:1:-1, jl - 1)
end select
! Sum the absolute values of the vectors going from the
! edge to the first inboard coordinates.
xSum(1) = sum(abs(xPtrRowInward(1, :) - xptr(1, :)))
xSum(2) = sum(abs(xPtrRowInward(2, :) - xptr(2, :)))
xSum(3) = sum(abs(xPtrRowInward(3, :) - xptr(3, :)))
! Locate the max index for this sum of vectors.
! This is normal to the symmetry plane.
i = maxloc(xSum, 1)
end if
! Set the symmetry plane BCs according to which coordinate
! index corresponded to the edge coordinates being 0.
if (i == 1) then
BCs(iEdge, ii) = BCXSymm
else if (i == 2) then
BCs(iEdge, ii) = BCYSymm
else if (i == 3) then
BCs(iEdge, ii) = BCZSymm
end if
else
! Otherwise it must be a splay
BCs(iEdge, ii) = BCSplay
end if
end if
end if
end do edgeLoop0
end do patchLoop0
nAverage = 0
! error flag for issues with BCs or topology
bcError = .False.
bcTopoError = .False.
patchLoop: do ii = 1, nPatch
il = patches(ii)%il
jl = patches(ii)%jl
edgeLoop: do iEdge = 1, 4
select case (iEdge)
case (iLow)
lPtr1 => patches(ii)%l_index(1, jl:1:-1)
lPtr2 => patches(ii)%l_index(2, jl:1:-1)
xPtr => patches(ii)%x(:, 1, jl:1:-1)
iSize = jl
faceStr = 'iLow '
case (iHigh)
lPtr1 => patches(ii)%l_index(il, :)
lPtr2 => patches(ii)%l_index(il - 1, :)
xPtr => patches(ii)%x(:, il, :)
iSize = jl
faceStr = 'iHigh '
case (jLow)
lPtr1 => patches(ii)%l_index(:, 1)
lPtr2 => patches(ii)%l_index(:, 2)
xPtr => patches(ii)%x(:, :, 1)
iSize = il
faceStr = 'jLow '
case (jHigh)
lPtr1 => patches(ii)%l_index(il:1:-1, jl)
lPtr2 => patches(ii)%l_index(il:1:-1, jl - 1)
xPtr => patches(ii)%x(:, il:1:-1, jl)
iSize = il
faceStr = 'jHigh'
end select
edgeNodeLoop: do i = 1, iSize
iNode = lPtr1(i)
checkTopoType: if (fullTopoType(iNode) == topoInternal) then
! We don't have anything to do for this, BUT if the
! boundary condition is anything but BCDefault than
! it means someone explictly specifid it which is an
! error since this isn't *actually* a boundary condition
if (BCs(iEdge, ii) /= BCDefault) then
bcError = .True.
101 format(a, a, a, I3, a)
print 101, 'ERROR: A boundary condition was specifed for ', &
trim(faceStr), ' patch on Block', ii, ' but it is not a physical boundary.'
end if
else if (fullTopoType(iNode) == topoEdge) then
! By definition , edge topology nodes must have 3 neighbours
fullnPtr(1, iNode) = 3
if (i > 1 .and. i < iSize) then
! Edge topology, and internal to an edge, we can
! just read off the neighbours.
fullnPtr(2, iNode) = lPtr1(i + 1)
fullnPtr(3, iNode) = lPtr2(i)
fullnPtr(4, iNode) = lPtr1(i - 1)
fullBCType(1, iNode) = BCs(iEdge, ii)
! Set the possible boundary condition value
call setBCVal(fullBCType(1, iNode), xPtr(:, i), &
fullBCVal(1, :, iNode))
else
! This is an edge topology, but at the logical
! corner of a block. If some other edge has
! already set the BC for us, we can just skip,
! since they did the work.
if (BCs(iedge, ii) /= bcDefault) then
if (i == 1) then
fullnPTr(2, iNode) = lPtr1(i + 1)
fullnPTr(3, iNode) = lPtr2(i)
call addMissing(nodeConn(1:3, iNode), fullnPtr(2, iNode), fullNPtr(3, iNode), &
fullNPtr(4, iNode))
else ! other end
fullnPTr(3, iNode) = lPtr2(i)
fullnPTr(4, iNode) = lPtr1(i - 1)
call addMissing(nodeConn(1:3, iNode), fullnPtr(4, iNode), fullNPtr(3, iNode), &
fullNPtr(2, iNode))
end if
if (BCs(iEdge, ii) > fullBCType(1, iNode)) then
fullBCType(1, iNode) = BCs(iEdge, ii)
! Set the possible boundary condition value
call setBCVal(fullBCType(1, iNode), xPtr(:, i), &
fullBCVal(1, :, iNode))
end if
end if
end if
! Setting the cell pointer is common
fullcPtr(1, iNode) = 2
fullcPtr(2, iNode) = nte(ntePtr(iNode))
fullcPtr(3, iNode) = nte(ntePtr(iNode) + 2)
else if (fullTopoType(iNode) == topoCorner) then
! Do a sanity check: This should only occur if i==1 or i==iSize
if (i /= 1 .and. i /= iSize) then
print *, 'ERROR: Unknown corner topology error detected.', i, isize
bcTopoError = .True.
end if
! By definition , corner topology nodes must have 2 neighbours
fullnPtr(1, iNode) = 2
if (i == 1) then
fullnPtr(2, iNode) = lPtr1(i + 1)
fullnPtr(3, iNode) = lPtr2(i)
iBCToSet = 2
else ! Must be iSize
fullnPtr(2, iNode) = lPtr2(i)
fullnPtr(3, iNode) = lPtr1(i - 1)
iBCToSet = 1
end if
! Set BC
fullBCType(iBCToSet, iNode) = BCs(iEdge, ii)
! Set the possible boundary condition value
call setBCVal(fullBCType(iBCToSet, iNode), xPtr(:, i), &
fullBCVal(iBCToSet, :, iNode))
! Set the cell pointer for this node
fullcPtr(1, iNode) = 1
fullcPtr(2, iNode) = nte(ntePtr(iNode))
end if checkTopoType
end do edgeNodeLoop
end do edgeLoop
! We now do a special check to see if corners have boundary
! conditions that would require the averaging procedure. This
! will happen if the two neighbour nodes are constrained to
! the same plane. For example, having two ySymm conditions.
corners(1) = patches(ii)%l_index(1, 1)
corners(2) = patches(ii)%l_index(il, 1)
corners(3) = patches(ii)%l_index(il, jl)
corners(4) = patches(ii)%l_index(1, jl)
do iCorner = 1, 4
i = corners(iCorner) ! Node index
nn(1) = fullnPtr(2, i) ! Neighbour 1 index
nn(2) = fullnPtr(3, i) ! Neighbour 2 index
if (fullTopoType(i) == topoCorner) then
! It is an actual corner topology
mask = 0
vals = zero
do jj = 1, 2 ! Loop over the two neighbours and check
! their BC Type
select case (fullBCType(1, nn(jj)))
case (BCXSymm, BCXConst)
! This means the following: the 'jj' neighbour
! constrains the 'X' direction with value given in
! fullBCVal. The mask(jj, 1) says the 'jj'
! neighbour is constrained in 'x', with the value
! given by vals(jj, 1)
mask(jj, 1) = 1
vals(jj, 1) = fullBCVal(1, 1, nn(jj))
case (BCYSymm, BCYConst)
mask(jj, 2) = 1
vals(jj, 2) = fullBCVal(1, 1, nn(jj))
case (BCZSymm, BCZConst)
mask(jj, 3) = 1
vals(jj, 3) = fullBCVal(1, 1, nn(jj))
case (BCXYConst)
mask(jj, 1) = 1
mask(jj, 2) = 1
vals(jj, 1) = fullBCVal(1, 1, nn(jj))
vals(jj, 2) = fullBCVal(1, 2, nn(jj))
case (BCYZConst)
mask(jj, 2) = 1
mask(jj, 3) = 1
vals(jj, 2) = fullBCVal(1, 1, nn(jj))
vals(jj, 3) = fullBCVal(1, 2, nn(jj))
case (BCXZConst)
mask(jj, 1) = 1
mask(jj, 3) = 1
vals(jj, 1) = fullBCVal(1, 1, nn(jj))
vals(jj, 3) = fullBCVal(1, 2, nn(jj))
end select
end do
! Loop over each coordinate direction.
do jj = 1, 3
! Is the same coordinate constrained in each neighbour
if (mask(1, jj) == mask(2, jj) .and. mask(1, jj) == 1) then
! The same coordinate is constrained. Now check if
! the value is the essentially the same
if (abs(vals(1, jj) - vals(2, jj)) < nodeTol) then
fullBCType(:, i) = BCAverage
nAverage = nAverage + 1
end if
end if
end do
end if
end do
end do patchLoop
! if there was an error in the BC setup stop
if (bcError) then
print *, 'pyHyp exited due to one or more issues with mesh boundary conditions. &
& See above error printouts.'
stop
end if
do i = 1, nUnique
! Special loop for the LCorners
if (fullTopoType(i) == topoLCorner) then
! This is quite complicated. It has 4 neighbours. Two
! of the neighbours have edge topology, two of the
! neighbours have internal topology.
!
! +---------p2----------+
! | | |
! | x | |
! | | |
! p3--------p0----------p1----->
! | |
! | |
! | |
! +---------p4
! |
! |
! \|/
! Determine the two that have internal
! topology. These must be part of facex
ii = 0
jj = 0
do j = 1, 4
if (fullTopoType(nodeConn(j, i)) == topoInternal) then
ii = ii + 1
nn(ii) = nodeConn(j, i) ! Nodes on diagonal face x
else
jj = jj + 1
mm(jj) = nodeConn(j, i) ! Nodes on edge
end if
end do
! And the three faces
f(1) = nte(ntePtr(i))
f(2) = nte(ntePtr(i) + 2)
f(3) = nte(ntePtr(i) + 4)
do iFace = 1, 3
! Check if nn(1) is followed by nn(2)
do j = 1, 4
jp2 = mod(j + 1, 4) + 1
if (nn(1) == fullConn(j, f(iFace)) .and. nn(2) == fullConn(jp2, f(iFace))) then
fullnPtr(3, i) = nn(1)
fullnPtr(4, i) = nn(2)
else if (nn(2) == fullConn(j, f(iFace)) .and. nn(1) == fullConn(jp2, f(iFace))) then
fullnPtr(3, i) = nn(2)
fullnPtr(4, i) = nn(1)
end if
end do
end do
! We have now set the 'p2' and 'p3' nodes. We have to
! identify p1 and p4 which should be fairly easy now:
! This takes the missing one and adds it to the first spot
call addMissing(directedNodeConn(1:3, i), fullnPtr(3, i), fullnPtr(4, i), fullnPtr(2, i))
! Add the only other node in mm
if (mm(1) == fullnPtr(2, i)) then
fullnPtr(5, i) = mm(2)
else
fullnPtr(5, i) = mm(1)
end if
! By definition , Lcorner topology nodes must have 4 neighbours
fullnPtr(1, i) = 4
! Set the cell pointer for this node
fullcPtr(1, i) = 3
fullcPtr(2, i) = f(1)
fullcPtr(3, i) = f(2)
fullcPtr(4, i) = f(3)
! We need to set the BC Data here too.
fullBCType(1, i) = fullBCType(1, fullnPtr(2, i))
fullBCVal(1, :, i) = fullBCVal(1, :, fullnPtr(2, i))
fullBCType(2, i) = fullBCType(1, fullnPtr(5, i))
fullBCVal(2, :, i) = fullBCVal(1, :, fullnPtr(5, i))
end if
end do
if (nAverage > 0) then
print *, 'Corner averaging activated for ', nAverage, ' nodes.'
end if
! Do a sanity check to make sure that all nodes have their
! fullnPtr populated.
do i = 1, nUnique
if (fullNPtr(1, i) == 0) then
print *, 'ERROR: There was a general error with topology computation for node:', uniquePts(:, i)
bcTopoError = .True.
end if
end do
! Another sanity check to make sure all nodes that are physically
! at edges have the required number of boundary conditions
! their fullnPtr populated.
do i = 1, nUnique
if (fullTopoType(i) == topoEdge .and. fullBCType(1, i) == BCDefault) then
print *, 'ERROR: There was a missing boundary condition for edge node:', uniquePts(:, i)
bcTopoError = .True.
else if (fullTopoType(i) == topoCorner .and. (fullBCType(1, i) == BCDefault .or. &
fullBCType(2, i) == BCDefault)) then
print *, 'ERROR: There was a missing boundary condition for corner node:', uniquePts(:, i)
bcTopoError = .True.
end if
end do
! if we ran into topology or BC errors, stop
if (bcTopoError) then
print *, 'ERROR: pyHyp exited due to one or more issues with mesh topology. See above error printouts.'
stop
end if
! Free up some more memory
deallocate (nte, ntePtr, directedNodeConn, nodeConn, nEdge, nDirectedEdge)
end if rootProc
! Now we can broadcast the required infomation to everyone. In fact
! all we need is the fullnPtr, fullcPtr and the full connectivity list
call MPI_Bcast(nUnique, 1, MPI_INTEGER, 0, hyp_comm_world, ierr)
call EChk(ierr, __FILE__, __LINE__)
call MPI_Bcast(NODE_MAX, 1, MPI_INTEGER, 0, hyp_comm_world, ierr)
call EChk(ierr, __FILE__, __LINE__)
call MPI_Bcast(CELL_MAX, 1, MPI_INTEGER, 0, hyp_comm_world, ierr)
call EChk(ierr, __FILE__, __LINE__)
call MPI_Bcast(faceTotal, 1, MPI_INTEGER, 0, hyp_comm_world, ierr)
call EChk(ierr, __FILE__, __LINE__)
call MPI_Bcast(nPatch, 1, MPI_INTEGER, 0, hyp_comm_world, ierr)
call EChk(ierr, __FILE__, __LINE__)
! Allocate space on all procs EXCEPT the root:
if (myid /= 0) then
allocate (fullConn(4, faceTotal))
allocate (fullnPtr(NODE_MAX + 1, nUnique))
allocate (fullcPtr(CELL_MAX + 1, nUnique))
allocate (fullTopoType(nUnique))
allocate (fullBCType(2, nUnique))
allocate (fullBCVal(2, 2, nUnique))
allocate (uniquePts(3, nUnique))
end if
! Actual broadcasts
call MPI_Bcast(fullConn, 4 * faceTotal, MPI_INTEGER, 0, hyp_comm_world, ierr)
call EChk(ierr, __FILE__, __LINE__)
call MPI_Bcast(fullnPtr, (NODE_MAX + 1) * nUnique, MPI_INTEGER, 0, hyp_comm_world, ierr)
call EChk(ierr, __FILE__, __LINE__)
call MPI_Bcast(fullcPtr, (CELL_MAX + 1) * nUnique, MPI_INTEGER, 0, hyp_comm_world, ierr)
call EChk(ierr, __FILE__, __LINE__)
call MPI_Bcast(uniquePts, 3 * nUnique, MPI_DOUBLE, 0, hyp_comm_world, ierr)
call EChk(ierr, __FILE__, __LINE__)
call MPI_Bcast(fullTopoType, nUnique, MPI_INTEGER, 0, hyp_comm_world, ierr)
call EChk(ierr, __FILE__, __LINE__)
call MPI_Bcast(fullBCType, 2 * nUnique, MPI_INTEGER, 0, hyp_comm_world, ierr)
call EChk(ierr, __FILE__, __LINE__)
call MPI_Bcast(fullBCVal, 2 * 2 * nUnique, MPI_DOUBLE, 0, hyp_comm_world, ierr)
call EChk(ierr, __FILE__, __LINE__)
! We now do "partitioning". It is essentially tries to divide up the
! nodes as evenly as possible (which is always possible up to the
! number of procs) without any regard to the number of cuts or
! communication costs.
evenNodes = nUnique / nProc
istart = myid * evenNodes + 1
iend = istart + evenNodes - 1