/
mesh.f90
2094 lines (1673 loc) · 61.7 KB
/
mesh.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
! MODULE: mesh
! AUTHOR: Jouni Makitalo
! DESCRIPTION:
! Loading and manipulating meshes consisting of triangles and tetrahedra.
! Supports the msh-format exported by Gmsh and the neutral mesh format of Netgen.
! Contains also functions for splitting a mesh into submeshes and various mesh manipulation routines.
MODULE mesh
USE aux
USE linalg
IMPLICIT NONE
INTRINSIC NINT
INTEGER, PARAMETER :: meshver = 4
INTEGER, PARAMETER :: mesh_bnd_none = 1,&
mesh_bnd_xplane = 2,&
mesh_bnd_yplane = 3,&
mesh_bnd_zplane = 4,&
mesh_bnd_prdx1 = 5,&
mesh_bnd_prdx2 = 6,&
mesh_bnd_prdy1 = 7,&
mesh_bnd_prdy2 = 8,&
mesh_bnd_rz1 = 9,&
mesh_bnd_rz2 = 10
TYPE node
REAL (KIND=dp), DIMENSION(3) :: p
!INTEGER, DIMENSION(:), POINTER :: face_indices, node_indices
INTEGER :: parent_index
!INTEGER :: bnd, nbnd
END TYPE node
TYPE face
REAL (KIND=dp), DIMENSION(3) :: n, cp
REAL (KIND=dp), DIMENSION(3,3) :: s, m
INTEGER, DIMENSION(3) :: node_indices
INTEGER, DIMENSION(3) :: edge_indices
REAL (KIND=dp) :: area, pd
INTEGER :: id
INTEGER :: parent_index
END TYPE face
TYPE line
INTEGER, DIMENSION(2) :: node_indices
INTEGER :: id
END TYPE line
TYPE edge
INTEGER, DIMENSION(2) :: node_indices, bnode_indices, face_indices
REAL (KIND=dp) :: length
! REAL (KIND=dp), DIMENSION(2) :: rwgDiv
INTEGER :: bnd
INTEGER :: parent_index
INTEGER :: couple_index
INTEGER, DIMENSION(:,:), ALLOCATABLE :: child_indices ! (:,1)=submesh, (:,2)=local edge
END TYPE edge
! Interior face.
TYPE solid_face
INTEGER, DIMENSION(3) :: node_indices
INTEGER, DIMENSION(2) :: solid_indices, bnode_indices
INTEGER :: face_index ! -1 if not a boundary
REAL (KIND=dp) :: area
END TYPE solid_face
TYPE solid
INTEGER, DIMENSION(4) :: node_indices
INTEGER, DIMENSION(4) :: solid_face_indices
REAL (KIND=dp) :: volume
INTEGER :: id
END TYPE solid
TYPE mesh_container
TYPE(node), DIMENSION(:), ALLOCATABLE :: nodes
TYPE(face), DIMENSION(:), ALLOCATABLE :: faces
TYPE(line), DIMENSION(:), ALLOCATABLE :: lines
TYPE(edge), DIMENSION(:), ALLOCATABLE :: edges
TYPE(solid), DIMENSION(:), ALLOCATABLE :: solids
TYPE(solid_face), DIMENSION(:), ALLOCATABLE :: solid_faces
INTEGER :: nnodes, nfaces, nlines, nedges, nsolids, nsolid_faces
REAL (KIND=dp) :: avelen
END TYPE mesh_container
CONTAINS
FUNCTION has_mesh_bnd(mesh, bnd) RESULT(res)
TYPE(mesh_container), INTENT(IN) :: mesh
INTEGER, INTENT(IN) :: bnd
LOGICAL :: res
INTEGER :: n
res = .FALSE.
DO n=1,mesh%nedges
IF(mesh%edges(n)%bnd==bnd) THEN
res = .TRUE.
EXIT
END IF
END DO
END FUNCTION has_mesh_bnd
FUNCTION adjacent_face(mesh, faceind, edgeind) RESULT(res)
TYPE(mesh_container), INTENT(IN) :: mesh
INTEGER, INTENT(IN) :: faceind, edgeind
INTEGER :: res, gedgeind
gedgeind = mesh%faces(faceind)%edge_indices(edgeind)
IF(mesh%edges(gedgeind)%face_indices(1)==faceind) THEN
res = mesh%edges(gedgeind)%face_indices(2)
ELSE IF(mesh%edges(gedgeind)%face_indices(2)==faceind) THEN
res = mesh%edges(gedgeind)%face_indices(1)
ELSE
WRITE(*,*) 'Could not determine adjacent face!'
STOP
END IF
END FUNCTION adjacent_face
FUNCTION local_edge_index(mesh, faceind, gedgeind) RESULT(ledgeind)
TYPE(mesh_container), INTENT(IN) :: mesh
INTEGER, INTENT(IN) :: faceind, gedgeind
INTEGER :: ledgeind
IF(mesh%faces(faceind)%edge_indices(1)==gedgeind) THEN
ledgeind = 1
ELSE IF(mesh%faces(faceind)%edge_indices(2)==gedgeind) THEN
ledgeind = 2
ELSE IF(mesh%faces(faceind)%edge_indices(3)==gedgeind) THEN
ledgeind = 3
ELSE
WRITE(*,*) 'Invalid edge index request (nlsurf.f90)!'
STOP
END IF
END FUNCTION local_edge_index
FUNCTION average_edge_length(mesh) RESULT(res)
TYPE(mesh_container), INTENT(IN) :: mesh
INTEGER :: n
REAL (KIND=dp) :: l
REAL (KIND=dp) :: res
res = 0.0_dp
DO n=1,mesh%nedges
l = normr(mesh%nodes(mesh%edges(n)%node_indices(1))%p -&
mesh%nodes(mesh%edges(n)%node_indices(2))%p)
res = res + l
END DO
res = res/mesh%nedges
END FUNCTION average_edge_length
SUBROUTINE classify_edges(mesh, id, bnd)
TYPE(mesh_container), INTENT(INOUT) :: mesh
INTEGER, INTENT(IN) :: id, bnd
INTEGER :: n, m, nbnd
nbnd = 0
DO n=1,mesh%nedges
DO m=1,mesh%nlines
IF(mesh%lines(m)%id==id .AND.&
cmp_pairs(mesh%edges(n)%node_indices, mesh%lines(m)%node_indices)) THEN
mesh%edges(n)%bnd = bnd
nbnd = nbnd + 1
END IF
END DO
END DO
WRITE(*,'(A,I0,A)') ' Classified ', nbnd, ' boundary edges'
END SUBROUTINE classify_edges
FUNCTION cmp_real_pairs(a, b, eps) RESULT(res)
REAL (KIND=dp), DIMENSION(2), INTENT(IN) :: a, b
REAL (KIND=dp), INTENT(IN) :: eps
LOGICAL :: res
res = .FALSE.
IF((ABS(a(1)-b(1))<eps .AND. ABS(a(2)-b(2))<eps) .OR.&
(ABS(a(1)-b(2))<eps .AND. ABS(a(2)-b(1))<eps)) THEN
res = .TRUE.
END IF
END FUNCTION cmp_real_pairs
SUBROUTINE determine_edge_couples(mesh, eps)
TYPE(mesh_container), INTENT(INOUT) :: mesh
REAL (KIND=dp), INTENT(IN) :: eps
INTEGER :: n, m, numx, numy
REAL (KIND=dp), DIMENSION(3) :: a1, a2, b1, b2
WRITE(*,*) 'Determining edge couples.'
numx = 0
numy = 0
mesh%edges(:)%couple_index = -1
! Determine couples in x-boundaries.
DO n=1,mesh%nedges
IF(mesh%edges(n)%bnd/=mesh_bnd_prdx1) THEN
CYCLE
END IF
DO m=1,mesh%nedges
IF(mesh%edges(m)%bnd/=mesh_bnd_prdx2) THEN
CYCLE
END IF
a1 = mesh%nodes(mesh%edges(n)%node_indices(1))%p
a2 = mesh%nodes(mesh%edges(n)%node_indices(2))%p
b1 = mesh%nodes(mesh%edges(m)%node_indices(1))%p
b2 = mesh%nodes(mesh%edges(m)%node_indices(2))%p
IF(cmp_real_pairs((/a1(2),a2(2)/), (/b1(2),b2(2)/), eps) .AND.&
cmp_real_pairs((/a1(3),a2(3)/), (/b1(3),b2(3)/), eps)) THEN
mesh%edges(n)%couple_index = m
mesh%edges(m)%couple_index = n
numx = numx + 1
END IF
END DO
END DO
! Determine couples in y-boundaries.
DO n=1,mesh%nedges
IF(mesh%edges(n)%bnd/=mesh_bnd_prdy1) THEN
CYCLE
END IF
DO m=1,mesh%nedges
IF(mesh%edges(m)%bnd/=mesh_bnd_prdy2) THEN
CYCLE
END IF
a1 = mesh%nodes(mesh%edges(n)%node_indices(1))%p
a2 = mesh%nodes(mesh%edges(n)%node_indices(2))%p
b1 = mesh%nodes(mesh%edges(m)%node_indices(1))%p
b2 = mesh%nodes(mesh%edges(m)%node_indices(2))%p
IF(cmp_real_pairs((/a1(1),a2(1)/), (/b1(1),b2(1)/), eps) .AND.&
cmp_real_pairs((/a1(3),a2(3)/), (/b1(3),b2(3)/), eps)) THEN
mesh%edges(n)%couple_index = m
mesh%edges(m)%couple_index = n
numy = numy + 1
END IF
END DO
END DO
! Make sure that the edge couples have basis functions with the same orientation,
! so that they can be given equal expansion coefficients.
DO n=1,mesh%nedges
IF(mesh%edges(n)%bnd/=mesh_bnd_prdx1 .AND. mesh%edges(n)%bnd/=mesh_bnd_prdy1) THEN
CYCLE
END IF
m = mesh%edges(n)%couple_index
IF(mesh%edges(n)%face_indices(1)/=-1 .AND. mesh%edges(m)%face_indices(1)/=-1) THEN
CALL swap(mesh%edges(m)%face_indices(1), mesh%edges(m)%face_indices(2))
CALL swap(mesh%edges(m)%bnode_indices(1), mesh%edges(m)%bnode_indices(2))
END IF
IF(mesh%edges(n)%face_indices(2)/=-1 .AND. mesh%edges(m)%face_indices(2)/=-1) THEN
CALL swap(mesh%edges(m)%face_indices(1), mesh%edges(m)%face_indices(2))
CALL swap(mesh%edges(m)%bnode_indices(1), mesh%edges(m)%bnode_indices(2))
END IF
END DO
WRITE(*,'(A,I0,A,I0,A)') 'Found ', numx, ' x-couples and ', numy, ' y-couples.'
END SUBROUTINE determine_edge_couples
SUBROUTINE invert_faces(mesh, id)
TYPE(mesh_container), INTENT(INOUT) :: mesh
INTEGER, INTENT(IN) :: id
INTEGER :: n
DO n=1,mesh%nfaces
IF(mesh%faces(n)%id==id) THEN
mesh%faces(n)%node_indices(1:3) = mesh%faces(n)%node_indices((/3,2,1/))
END IF
END DO
END SUBROUTINE invert_faces
FUNCTION extract_submesh(mesh, ids, vol_ids) RESULT(submesh)
TYPE(mesh_container), INTENT(IN) :: mesh
INTEGER, DIMENSION(:), INTENT(IN) :: ids
INTEGER, DIMENSION(:), POINTER, INTENT(IN) :: vol_ids
TYPE(mesh_container) :: submesh
INTEGER :: n, m, nfaces, nsolids
LOGICAL, DIMENSION(mesh%nnodes) :: nmask
INTEGER, DIMENSION(mesh%nnodes) :: local_node_indices
nmask(:) = .FALSE.
! Compute the number of faces for this submesh.
nfaces = 0
DO n=1,mesh%nfaces
IF(COUNT(ids==mesh%faces(n)%id)/=0) THEN
nfaces = nfaces + 1
DO m=1,3
nmask(mesh%faces(n)%node_indices(m)) = .TRUE.
END DO
END IF
END DO
submesh%nfaces = nfaces
ALLOCATE(submesh%faces(1:nfaces))
! Compute the number of solids for this submesh.
IF(ASSOCIATED(vol_ids)) THEN
nsolids = 0
DO n=1,mesh%nsolids
IF(COUNT(vol_ids==mesh%solids(n)%id)/=0) THEN
nsolids = nsolids + 1
DO m=1,4
nmask(mesh%solids(n)%node_indices(m)) = .TRUE.
END DO
END IF
END DO
submesh%nsolids = nsolids
ALLOCATE(submesh%solids(1:nsolids))
ELSE
submesh%nsolids = 0
END IF
! Copy the vertices.
local_node_indices(:) = -1
submesh%nnodes = COUNT(nmask==.TRUE.)
ALLOCATE(submesh%nodes(1:submesh%nnodes))
m = 0
DO n=1,mesh%nnodes
IF(nmask(n)) THEN
m = m + 1
submesh%nodes(m)%p = mesh%nodes(n)%p
submesh%nodes(m)%parent_index = n
local_node_indices(n) = m
END IF
END DO
! Copy the faces.
m = 0
DO n=1,mesh%nfaces
IF(COUNT(ids==mesh%faces(n)%id)/=0) THEN
m = m + 1
submesh%faces(m)%node_indices(1:3) = local_node_indices(mesh%faces(n)%node_indices(1:3))
submesh%faces(m)%parent_index = n
submesh%faces(m)%id = mesh%faces(n)%id
END IF
END DO
! Copy the solids.
IF(ASSOCIATED(vol_ids)) THEN
m = 0
DO n=1,mesh%nsolids
IF(COUNT(vol_ids==mesh%solids(n)%id)/=0) THEN
m = m + 1
submesh%solids(m)%node_indices(1:4) = &
local_node_indices(mesh%solids(n)%node_indices(1:4))
submesh%solids(m)%id = mesh%solids(n)%id
END IF
END DO
END IF
submesh%nlines = 0
submesh%nedges = 0
submesh%nsolid_faces = 0
END FUNCTION extract_submesh
SUBROUTINE submesh_edge_connectivity(mesh, submeshes)
TYPE(mesh_container), INTENT(INOUT) :: mesh
TYPE(mesh_container), DIMENSION(:), INTENT(INOUT) :: submeshes
INTEGER :: n, m, l
INTEGER, DIMENSION(2) :: node_indices
INTEGER, DIMENSION(1:mesh%nedges) :: nchild
INTEGER, DIMENSION(1:SIZE(submeshes),1:2) :: tmp
INTEGER :: ge1, ge2, le1, le2, sm1, sm2
WRITE(*,*) 'Establishing submesh-parent edge connectivity.'
nchild(:) = 0
! Allocate reservoir for child edge indices.
DO n=1,mesh%nedges
ALLOCATE(mesh%edges(n)%child_indices(1:SIZE(submeshes),1:2))
END DO
! Determine child-parent edge connectivity.
DO n=1,mesh%nedges
DO m=1,SIZE(submeshes)
DO l=1,submeshes(m)%nedges
node_indices = submeshes(m)%nodes(submeshes(m)%edges(l)%node_indices(1:2))%parent_index
! Compare (parent) node indices of the edges.
IF(cmp_pairs(mesh%edges(n)%node_indices, node_indices)) THEN
submeshes(m)%edges(l)%parent_index = n
nchild(n) = nchild(n) + 1
mesh%edges(n)%child_indices(nchild(n),1) = m
mesh%edges(n)%child_indices(nchild(n),2) = l
END IF
END DO
END DO
END DO
! Trim child edge index arrays.
DO n=1,mesh%nedges
IF(nchild(n)<2) THEN
WRITE(*,*) 'Submesh decomposition is inconsistent (invalid child edges)!'
STOP
END IF
tmp(1:nchild(n),:) = mesh%edges(n)%child_indices(1:nchild(n),:)
DEALLOCATE(mesh%edges(n)%child_indices)
ALLOCATE(mesh%edges(n)%child_indices(1:nchild(n),1:2))
mesh%edges(n)%child_indices(1:nchild(n),1:2) = tmp(1:nchild(n),1:2)
END DO
! Inherit edge properties to submeshes.
DO ge1=1,mesh%nedges
ge2 = mesh%edges(ge1)%couple_index
DO m=1,SIZE(mesh%edges(ge1)%child_indices,1)
sm1 = mesh%edges(ge1)%child_indices(m,1)
le1 = mesh%edges(ge1)%child_indices(m,2)
submeshes(sm1)%edges(le1)%bnd = mesh%edges(ge1)%bnd
IF(ge2/=-1) THEN
DO l=1,SIZE(mesh%edges(ge2)%child_indices,1)
sm2 = mesh%edges(ge2)%child_indices(l,1)
le2 = mesh%edges(ge2)%child_indices(l,2)
IF(sm2==sm1) THEN
submeshes(sm1)%edges(le1)%couple_index = le2
submeshes(sm2)%edges(le2)%couple_index = le1
EXIT
END IF
END DO
END IF
END DO
END DO
END SUBROUTINE submesh_edge_connectivity
SUBROUTINE orient_basis(mesh, submeshes)
TYPE(mesh_container), INTENT(IN) :: mesh
TYPE(mesh_container), DIMENSION(:), INTENT(INOUT) :: submeshes
INTEGER :: n, m, l, k, nchild
LOGICAL, DIMENSION(1:SIZE(submeshes)) :: oriented
INTEGER :: pf1, pf2, nf1, nf2
WRITE(*,*) 'Orienting submesh basis functions'
! Orient the boundary basis functions of the submeshes.
! Make sure that the edge couples have basis functions with the same orientation,
! so that they can be given equal expansion coefficients.
DO k=1,SIZE(submeshes)
DO n=1,submeshes(k)%nedges
IF(submeshes(k)%edges(n)%bnd/=mesh_bnd_prdx1 .AND.&
submeshes(k)%edges(n)%bnd/=mesh_bnd_prdy1) THEN
CYCLE
END IF
m = submeshes(k)%edges(n)%couple_index
IF(submeshes(k)%edges(n)%face_indices(1)/=-1 .AND.&
submeshes(k)%edges(m)%face_indices(1)/=-1) THEN
CALL swap(submeshes(k)%edges(m)%face_indices(1), submeshes(k)%edges(m)%face_indices(2))
CALL swap(submeshes(k)%edges(m)%bnode_indices(1), submeshes(k)%edges(m)%bnode_indices(2))
END IF
IF(submeshes(k)%edges(n)%face_indices(2)/=-1 .AND.&
submeshes(k)%edges(m)%face_indices(2)/=-1) THEN
CALL swap(submeshes(k)%edges(m)%face_indices(1), submeshes(k)%edges(m)%face_indices(2))
CALL swap(submeshes(k)%edges(m)%bnode_indices(1), submeshes(k)%edges(m)%bnode_indices(2))
END IF
END DO
END DO
DO n=1,mesh%nedges
nchild = SIZE(mesh%edges(n)%child_indices,1)
oriented(:) = .FALSE.
oriented(1) = .TRUE.
! This loop is a limited iteration until all basis functions
! assigned to edge n are oriented.
DO m=1,nchild
! Loop through child edges of parent edge n. Skip the reference l==1.
DO l=2,nchild
! If basis function of local edge l is oriented, skip.
IF(oriented(l)) THEN
CYCLE
END IF
! Compare local edge l to oriented local edges k.
DO k=1,nchild
IF(oriented(k)==.FALSE.) THEN
CYCLE
END IF
! Determine local face indices of basis functions.
pf1 = submeshes(mesh%edges(n)%child_indices(l,1))%edges(mesh%edges(n)%child_indices(l,2))%face_indices(1)
nf1 = submeshes(mesh%edges(n)%child_indices(l,1))%edges(mesh%edges(n)%child_indices(l,2))%face_indices(2)
pf2 = submeshes(mesh%edges(n)%child_indices(k,1))%edges(mesh%edges(n)%child_indices(k,2))%face_indices(1)
nf2 = submeshes(mesh%edges(n)%child_indices(k,1))%edges(mesh%edges(n)%child_indices(k,2))%face_indices(2)
! Transform local indices into parent indices.
IF(pf1/=-1) THEN
pf1 = submeshes(mesh%edges(n)%child_indices(l,1))%faces(pf1)%parent_index
END IF
IF(nf1/=-1) THEN
nf1 = submeshes(mesh%edges(n)%child_indices(l,1))%faces(nf1)%parent_index
END IF
IF(pf2/=-1) THEN
pf2 = submeshes(mesh%edges(n)%child_indices(k,1))%faces(pf2)%parent_index
END IF
IF(nf2/=-1) THEN
nf2 = submeshes(mesh%edges(n)%child_indices(k,1))%faces(nf2)%parent_index
END IF
! If necessary, invert the basis function of edge l.
IF(pf1==pf2 .OR. nf1==nf2) THEN
CALL swap(submeshes(mesh%edges(n)%child_indices(l,1))%edges(mesh%edges(n)%child_indices(l,2))%face_indices(1),&
submeshes(mesh%edges(n)%child_indices(l,1))%edges(mesh%edges(n)%child_indices(l,2))%face_indices(2))
CALL swap(submeshes(mesh%edges(n)%child_indices(l,1))%edges(mesh%edges(n)%child_indices(l,2))%bnode_indices(1),&
submeshes(mesh%edges(n)%child_indices(l,1))%edges(mesh%edges(n)%child_indices(l,2))%bnode_indices(2))
END IF
IF(pf1==pf2 .OR. nf1==nf2 .OR. pf1==nf2 .OR. nf1==pf2) THEN
oriented(l) = .TRUE.
EXIT
END IF
END DO
END DO
IF(COUNT(oriented==.TRUE.)==nchild) THEN
EXIT
END IF
END DO
IF(m==nchild) THEN
WRITE(*,*) 'Basis orientation was unsuccessful!'
EXIT
END IF
END DO
END SUBROUTINE orient_basis
SUBROUTINE get_bnd_edges(mesh, bnd, nse, edgeind)
TYPE(mesh_container), INTENT(IN) :: mesh
INTEGER, INTENT(IN) :: bnd
INTEGER, INTENT(OUT) :: nse
INTEGER, INTENT(INOUT), DIMENSION(mesh%nedges) :: edgeind
INTEGER :: n
nse = 0
DO n=1,mesh%nedges
IF(mesh%edges(n)%bnd==bnd) THEN
nse = nse + 1
edgeind(nse) = n
END IF
END DO
END SUBROUTINE get_bnd_edges
FUNCTION get_mesh_element_lines(filename) RESULT(lines)
CHARACTER (LEN=256), INTENT(IN) :: filename
INTEGER, DIMENSION(:,:), POINTER :: lines
INTEGER :: nelements, element_number, iovar, fid = 10, n
CHARACTER (LEN=256) :: lineid
OPEN(fid, FILE=TRIM(filename), ACTION='READ', IOSTAT=iovar)
IF(iovar>0) THEN
WRITE(*,*) 'Could not open mesh file ', TRIM(filename), '!'
STOP
END IF
iovar = 0
DO WHILE(iovar==0)
READ(fid, *, IOSTAT=iovar) lineid
IF(lineid=='$Elements') THEN
READ(fid, *, IOSTAT=iovar) nelements
ALLOCATE(lines(nelements,1:2))
DO n=1,nelements
READ(fid, *, IOSTAT=iovar) element_number, lines(n,1), lines(n,2)
END DO
READ(fid, *, IOSTAT=iovar) lineid
IF(lineid/='$EndElements') THEN
WRITE(*,*) 'Could not find $EndElements specifier!'
STOP
END IF
END IF
END DO
CLOSE(fid)
END FUNCTION get_mesh_element_lines
FUNCTION load_mesh_gmsh(filename) RESULT(mesh)
CHARACTER (LEN=*), INTENT(IN) :: filename
CHARACTER (LEN=256) :: lineid, line
TYPE(mesh_container) :: mesh
INTEGER :: fid = 10, iovar, nnodes, node_number, n,&
nelements, element_number, element_type, cface, cline, csolid, ntags
INTEGER, DIMENSION(10) :: element_data
INTEGER, DIMENSION(:,:), POINTER :: element_types
REAL (KIND=dp), DIMENSION(3) :: np
CHARACTER (LEN=3) :: mshver
element_types => get_mesh_element_lines(filename)
OPEN(fid, FILE=TRIM(filename), ACTION='READ', IOSTAT=iovar)
IF(iovar>0) THEN
WRITE(*,*) 'Could not open mesh file!'
STOP
END IF
iovar = 0
DO WHILE(iovar==0)
READ(fid, *, IOSTAT=iovar) lineid
IF(lineid=='$MeshFormat') THEN
READ(fid, '(A3)', IOSTAT=iovar) mshver
IF(mshver/='2.1' .AND. mshver/='2.2') THEN
WRITE(*,*) 'Mesh version is unsupported!'
CLOSE(fid)
STOP
END IF
WRITE(*,'(A,A3)') 'Mesh version: ', mshver
READ(fid, *, IOSTAT=iovar) lineid
IF(lineid/='$EndMeshFormat') THEN
WRITE(*,*) 'Could not find $MeshFormatNodes specifier!'
STOP
END IF
ELSE IF(lineid=='$Nodes') THEN
READ(fid, *, IOSTAT=iovar) nnodes
ALLOCATE(mesh%nodes(nnodes))
DO n=1,nnodes
READ(fid, *, IOSTAT=iovar) node_number, np(1:3)
mesh%nodes(node_number)%p = np
END DO
READ(fid, *, IOSTAT=iovar) lineid
IF(lineid/='$EndNodes') THEN
WRITE(*,*) 'Could not find $EndNodes specifier!'
STOP
END IF
mesh%nnodes = nnodes
ELSE IF(lineid=='$Elements') THEN
mesh%nfaces = COUNT(element_types(:,1)==2)
mesh%nlines = COUNT(element_types(:,1)==1)
mesh%nsolids = COUNT(element_types(:,1)==4)
ALLOCATE(mesh%faces(1:mesh%nfaces))
IF(mesh%nlines/=0) THEN
ALLOCATE(mesh%lines(1:mesh%nlines))
END IF
IF(mesh%nsolids/=0) THEN
ALLOCATE(mesh%solids(1:mesh%nsolids))
END IF
READ(fid, *, IOSTAT=iovar) nelements
cface = 0
cline = 0
csolid = 0
DO n=1,nelements
ntags = element_types(n,2)
IF(element_types(n,1)==2) THEN
cface = cface + 1
READ(fid, *, IOSTAT=iovar) element_data(1:(ntags+6))
mesh%faces(cface)%id = element_data(4)
mesh%faces(cface)%node_indices(1:3) = element_data((4+ntags):(6+ntags))
ELSE IF(element_types(n,1)==1) THEN
cline = cline + 1
READ(fid, *, IOSTAT=iovar) element_data(1:(ntags+5))
mesh%lines(cline)%id = element_data(4)
mesh%lines(cline)%node_indices(1:2) = element_data((4+ntags):(5+ntags))
ELSE IF(element_types(n,1)==4) THEN
csolid = csolid + 1
READ(fid, *, IOSTAT=iovar) element_data(1:(ntags+7))
mesh%solids(csolid)%id = element_data(4)
mesh%solids(csolid)%node_indices(1:4) = element_data((ntags+4):(ntags+7))
ELSE
READ(fid, *, IOSTAT=iovar) element_data(1)
END IF
END DO
READ(fid, *, IOSTAT=iovar) lineid
IF(lineid/='$EndElements') THEN
WRITE(*,*) 'Could not find $EndElements specifier!'
STOP
END IF
END IF
END DO
DEALLOCATE(element_types)
CLOSE(fid)
! Change vertex index rotation.
! DO n=1,mesh%nfaces
! CALL swap(mesh%faces(n)%node_indices(2), mesh%faces(n)%node_indices(3))
! END DO
!OPEN(10, FILE='nodes', ACTION='write')
!DO n=1,mesh%nnodes
! WRITE(10,'(I5,3E15.3)') n, mesh%nodes(n)%p
!END DO
!CLOSE(10)
!OPEN(10, FILE='faces', ACTION='write')
!DO n=1,mesh%nfaces
! WRITE(10,'(I5,3I5)') n, mesh%faces(n)%node_indices
!END DO
!CLOSE(10)
END FUNCTION load_mesh_gmsh
FUNCTION load_mesh_neutral(filename) RESULT(mesh)
CHARACTER (LEN=*), INTENT(IN) :: filename
TYPE(mesh_container) :: mesh
INTEGER :: fid = 10, n, iovar, bndid
OPEN(fid, FILE=TRIM(filename), ACTION='READ', IOSTAT=iovar)
IF(iovar>0) THEN
WRITE(*,*) 'Could not open mesh file!'
STOP
END IF
iovar = 0
! Load nodes.
READ(fid, *, IOSTAT=iovar) mesh%nnodes
ALLOCATE(mesh%nodes(mesh%nnodes))
DO n=1,mesh%nnodes
READ(fid, *, IOSTAT=iovar) mesh%nodes(n)%p(1:3)
END DO
! Load solids (tetrahedra).
READ(fid, *, IOSTAT=iovar) mesh%nsolids
IF(mesh%nsolids>0) THEN
ALLOCATE(mesh%solids(mesh%nsolids))
DO n=1,mesh%nsolids
mesh%solids(n)%id = 1
READ(fid, *, IOSTAT=iovar) mesh%solids(n)%node_indices(1:4)
END DO
END IF
! Load faces (triangles).
READ(fid, *, IOSTAT=iovar) mesh%nfaces
ALLOCATE(mesh%faces(mesh%nfaces))
DO n=1,mesh%nfaces
mesh%faces(n)%id = 1
READ(fid, *, IOSTAT=iovar) bndid, mesh%faces(n)%node_indices(1:3)
END DO
CLOSE(fid)
mesh%nlines = 0
END FUNCTION load_mesh_neutral
FUNCTION load_mesh(filename) RESULT(mesh)
CHARACTER (LEN=*), INTENT(IN) :: filename
TYPE(mesh_container) :: mesh
CHARACTER (LEN=3) :: ext
ext = getext(filename)
IF(ext=='msh') THEN
mesh = load_mesh_gmsh(filename)
ELSE IF(ext=='nmf') THEN
mesh = load_mesh_neutral(filename)
ELSE
WRITE(*,*) 'Unrecognized mesh file extension!'
STOP
END IF
mesh%nsolid_faces = 0
WRITE(*,*) 'Mesh file loaded successfully.'
WRITE(*,'(A,I0,:,A)') ' - Read ', mesh%nnodes, ' nodes'
WRITE(*,'(A,I0,:,A)') ' - Read ', mesh%nfaces, ' faces'
WRITE(*,'(A,I0,:,A)') ' - Read ', mesh%nlines, ' lines'
WRITE(*,'(A,I0,:,A)') ' - Read ', mesh%nsolids, ' solids'
END FUNCTION load_mesh
SUBROUTINE delete_mesh(mesh)
TYPE(mesh_container), INTENT(INOUT) :: mesh
INTEGER :: n
IF(ALLOCATED(mesh%nodes)) THEN
DEALLOCATE(mesh%nodes)
END IF
IF(ALLOCATED(mesh%faces)) THEN
DEALLOCATE(mesh%faces)
END IF
IF(ALLOCATED(mesh%lines)) THEN
DEALLOCATE(mesh%lines)
END IF
IF(ALLOCATED(mesh%edges)) THEN
DO n=1,mesh%nedges
IF(ALLOCATED(mesh%edges(n)%child_indices)) THEN
DEALLOCATE(mesh%edges(n)%child_indices)
END IF
END DO
DEALLOCATE(mesh%edges)
END IF
IF(ALLOCATED(mesh%solids)) THEN
DEALLOCATE(mesh%solids)
END IF
IF(ALLOCATED(mesh%solid_faces)) THEN
DEALLOCATE(mesh%solid_faces)
END IF
END SUBROUTINE delete_mesh
SUBROUTINE order(p,q)
INTEGER :: p,q,temp
IF(p>q) THEN
temp = p
p = q
q = temp
END IF
END SUBROUTINE order
SUBROUTINE sort(array, n)
INTEGER, DIMENSION(:), INTENT(INOUT) :: array
INTEGER, INTENT(IN) :: n
INTEGER :: i, j
DO i=1, n
DO j=n, i+1, -1
CALL order(array(j-1), array(j))
END DO
END DO
END SUBROUTINE sort
SUBROUTINE swap(a,b)
INTEGER, INTENT(INOUT) :: a,b
INTEGER :: tmp
tmp = a
a = b
b = tmp
END SUBROUTINE swap
SUBROUTINE sort3(array)
INTEGER, DIMENSION(3), INTENT(INOUT) :: array
INTEGER :: tmp
IF(array(1)<=array(2) .AND. array(1)<=array(3)) THEN
IF(array(3)<=array(2)) THEN
CALL swap(array(2), array(3))
END IF
ELSE IF(array(2)<=array(1) .AND. array(2)<=array(3)) THEN
CALL swap(array(1), array(2))
IF(array(3)<=array(2)) THEN
CALL swap(array(2), array(3))
END IF
ELSE IF(array(3)<=array(1) .AND. array(3)<=array(2)) THEN
CALL swap(array(1), array(3))
IF(array(3)<=array(2)) THEN
CALL swap(array(2), array(3))
END IF
END IF
END SUBROUTINE sort3
SUBROUTINE append_integer(array, element)
INTEGER, DIMENSION(:), POINTER :: array
INTEGER, DIMENSION(:), ALLOCATABLE :: temp
INTEGER, INTENT(IN) :: element
INTEGER :: s
s = SIZE(array)
IF(ASSOCIATED(array)==.FALSE.) THEN
ALLOCATE(array(1))
ELSE
ALLOCATE(temp(s))
temp = array
DEALLOCATE(array)
ALLOCATE(array(s+1))
array(1:SIZE(temp)) = temp
DEALLOCATE(temp)
END IF
array(SIZE(array)) = element
END SUBROUTINE append_integer
FUNCTION test_inclusion(array, element) RESULT(res)
INTEGER, DIMENSION(:), POINTER :: array
INTEGER, INTENT(IN) :: element
INTEGER :: n
LOGICAL :: res
res = .FALSE.
IF(ASSOCIATED(array)==.FALSE.) THEN
RETURN
END IF
DO n=1,SIZE(array)
IF(array(n)==element) THEN
res = .TRUE.
RETURN
END IF
END DO
END FUNCTION test_inclusion
FUNCTION test_inclusion2(array, element) RESULT(res)
INTEGER, DIMENSION(:) :: array
INTEGER, INTENT(IN) :: element
INTEGER :: n
LOGICAL :: res
res = .FALSE.
DO n=1,SIZE(array)
IF(array(n)==element) THEN
res = .TRUE.
RETURN
END IF
END DO
END FUNCTION test_inclusion2
FUNCTION cmp_pairs(pair1, pair2) RESULT(res)
INTEGER, DIMENSION(2), INTENT(IN) :: pair1, pair2
LOGICAL :: res
res = .FALSE.
IF((pair1(1)==pair2(1) .AND. pair1(2)==pair2(2)) .OR.&
(pair1(1)==pair2(2) .AND. pair1(2)==pair2(1))) THEN
res = .TRUE.
END IF
END FUNCTION cmp_pairs
FUNCTION cmp_triplets(triplet1, triplet2) RESULT(res)
INTEGER, DIMENSION(3), INTENT(IN) :: triplet1, triplet2
LOGICAL :: res
INTEGER, PARAMETER, DIMENSION(3,6) :: indices = (/1,2,3, 1,3,2, 2,1,3, 2,3,1, 3,1,2, 3,2,1/)
INTEGER :: i
res = .FALSE.
DO i=1,6
IF(triplet1(1)==triplet2(indices(1,i)) .AND.&
triplet1(2)==triplet2(indices(2,i)) .AND.&
triplet1(3)==triplet2(indices(3,i))) THEN
res = .TRUE.
RETURN
END IF
END DO
END FUNCTION cmp_triplets
FUNCTION get_free_index(indices, dublet) RESULT(res)
INTEGER, DIMENSION(3), INTENT(IN) :: indices
INTEGER, DIMENSION(2), INTENT(IN) :: dublet
INTEGER :: res
IF(cmp_pairs(dublet, indices((/1,2/)))) THEN
res = indices(3)
ELSE IF(cmp_pairs(dublet, indices((/2,3/)))) THEN
res = indices(1)
ELSE IF(cmp_pairs(dublet, indices((/1,3/)))) THEN