-
Notifications
You must be signed in to change notification settings - Fork 0
/
MOD_TOOLS.f90
1089 lines (935 loc) · 36.5 KB
/
MOD_TOOLS.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
! some tools from idolikecfd.
module mod_constants
implicit none
private
public :: p2
public :: zero, one, two, three, four, five, six, seven, eight, nine
public :: ten, eleven
public :: half, third, fourth, fifth, sixth, two_third, four_third
public :: three_fourth, twelfth, pi, one_twentyfourth
integer , parameter :: sp = kind(1.0)
integer , parameter :: p2 = selected_real_kind(2*precision(1.0_sp))
real(p2), parameter :: zero = 0.0_p2, &
one = 1.0_p2, &
two = 2.0_p2, &
three = 3.0_p2, &
four = 4.0_p2, &
five = 5.0_p2, &
six = 6.0_p2, &
seven = 7.0_p2, &
eight = 8.0_p2, &
nine = 9.0_p2, &
ten = 10.0_p2, &
eleven = 11.0_p2, &
half = 0.5_p2, &
third = 1.0_p2/ 3.0_p2, &
fourth = 1.0_p2/ 4.0_p2, &
fifth = 1.0_p2/ 5.0_p2, &
sixth = 1.0_p2/ 6.0_p2, &
two_third = 2.0_p2/ 3.0_p2, &
four_third = 4.0_p2/ 3.0_p2, &
three_fourth = 3.0_p2/ 4.0_p2, &
twelfth = 1.0_p2/12.0_p2, &
one_twentyfourth = 1.0_p2/24.0_p2
real(p2), parameter :: pi = 3.141592653589793238_p2
end module mod_constants
!* 2. module grid_data_type
!*
!* This module defines custom grid data types for unstructured grids
!*
!* NOTE: These data types are designed to make it easier to understand the code.
!* They may not be the best in terms of efficiency.
!*
!* NOTE: Custom grid data types (derived types) are very useful.
!* For example, if I declare a variable, "a", by the statemant:
!* type(node_type), dimension(100) :: a
!* The variable, a, is a 1D array each component of which contains all data
!* defined as below. These data can be accessed by %, e.g.,
!* a(1)%x, a(1)%y, a(1)%nghbr(1:nnghbrs), etc.
!* In C-programming, this type of data is called "structure", I think.
module mod_grid_data_type
use mod_constants, only : p2
implicit none
private
public :: node_type
public :: elm_type
public :: edge_type
public :: bgrid_type
public :: face_type
public :: jac_type
!----------------------------------------------------------
! Data type for nodal quantities (used for node-centered schemes)
! Note: Each node has the following data.
!----------------------------------------------------------
type node_type
! to be read from a grid file
real(p2) :: x, y !nodal coordinates
real(p2) :: z !nodal depth
! to be constructed in the code
integer :: nnghbrs !number of neighbors
integer, dimension(:), pointer :: nghbr !list of neighbors
integer :: nelms !number of elements
integer, dimension(:), pointer :: elm !list of elements
real(p2) :: vol !dual-cell volume
integer :: bmark !Boundary mark
integer :: nbmarks !# of boundary marks
! to be computed in the code
!Below are arrays always allocated.
real(p2), dimension(:) , pointer :: u !conservative variables
real(p2), dimension(:) , pointer :: uexact !conservative variables
real(p2), dimension(:,:), pointer :: gradu !gradient of u
real(p2), dimension(:) , pointer :: res !residual (rhs)
real(p2) :: ar ! Control volume aspect ratio
real(p2), dimension(:) , pointer :: lsq2x2_cx ! Linear LSQ coefficient for wx
real(p2), dimension(:) , pointer :: lsq2x2_cy ! Linear LSQ coefficient for wy
real(p2), dimension(:) , pointer :: lsq5x5_cx ! Quadratic LSQ coefficient for wx
real(p2), dimension(:) , pointer :: lsq5x5_cy ! Quadratic LSQ coefficient for wy
real(p2), dimension(: ), pointer :: dx, dy ! Extra data used by Quadratic LSQ
real(p2), dimension(:,:), pointer :: dw ! Extra data used by Quadratic LSQ
!Below are optional: Pointers need to be allocated in the main program if necessary.
real(p2), dimension(:) , pointer :: du !change in conservative variables
real(p2), dimension(:) , pointer :: w !primitive variables(optional)
real(p2), dimension(:,:), pointer :: gradw !gradient of w
real(p2) :: phi !limiter function (0 <= phi <= 1)
real(p2) :: dt !local time step
real(p2) :: wsn !Half the max wave speed at face
real(p2), dimension(:), pointer :: r_temp ! For GCR implementation
real(p2), dimension(:), pointer :: u_temp ! For GCR implementation
real(p2), dimension(:), pointer :: w_temp ! For GCR implementation
end type node_type
!----------------------------------------------------------
! Data type for element/cell quantities (used for cell-centered schemes)
! Note: Each element has the following data.
!----------------------------------------------------------
type elm_type
! to be read from a grid file
integer :: nvtx !number of vertices
integer, dimension(:), pointer :: vtx !list of vertices
integer :: imt ! material of element
real(p2) :: z !nodal depth
! to be constructed in the code
integer :: nnghbrs !number of neighbors
integer, dimension(:), pointer :: nghbr !list of neighbors
real(p2) :: x, y !cell center coordinates
real(p2) :: radius !radius of circle in the triangle
real(p2) :: vol !cell volume
integer, dimension(:) , pointer :: edge !list of edges
real(p2), dimension(:) , pointer :: u !conservative variables
real(p2), dimension(:) , pointer :: uexact !conservative variables
!NotUsed real(p2), dimension(:) , pointer :: du !change in conservative variables
real(p2), dimension(:,:), pointer :: gradu !gradient of u
real(p2), dimension(:) , pointer :: res !residual (rhs)
real(p2) :: dt !local time step
real(p2) :: wsn !
integer :: bmark !Boundary mark
integer :: nvnghbrs !number of vertex neighbors
integer, dimension(:), pointer :: vnghbr !list of vertex neighbors
real(p2) :: ar !Element volume aspect ratio
real(p2), dimension(:) , pointer :: lsq2x2_cx!Linear LSQ coefficient for ux
real(p2), dimension(:) , pointer :: lsq2x2_cy!Linear LSQ coefficient for uy
end type elm_type
!----------------------------------------------------------
! Data type for edge quantities (used for node-centered scheemes)
! Note: Each edge has the following data.
!----------------------------------------------------------
type edge_type
! to be constructed in the code
integer :: n1, n2 !associated nodes
integer :: e1, e2 !associated elements
real(p2), dimension(2) :: dav !unit directed-area vector
real(p2) :: da !magnitude of the directed-area vector
real(p2), dimension(2) :: ev !unit edge vector
real(p2) :: e !magnitude of the edge vector
integer :: kth_nghbr_of_1 !neighbor index
integer :: kth_nghbr_of_2 !neighbor index
end type edge_type
!----------------------------------------------------------
! Data type for boundary quantities (for both node/cell-centered schemes)
! Note: Each boundary segment has the following data.
!----------------------------------------------------------
type bgrid_type
! to be read from a boundary grid file
character(80) :: bc_type !type of boundary condition
integer :: nbnodes !# of boundary nodes
integer, dimension(:), pointer :: bnode !list of boundary nodes
! to be constructed in the code
integer :: nbfaces !# of boundary faces
real(p2), dimension(:), pointer :: bfnx !x-component of the face outward normal
real(p2), dimension(:), pointer :: bfny !y-component of the face outward normal
real(p2), dimension(:), pointer :: bfn !magnitude of the face normal vector
real(p2), dimension(:), pointer :: bnx !x-component of the outward normal
real(p2), dimension(:), pointer :: bny !y-component of the outward normal
real(p2), dimension(:), pointer :: bn !magnitude of the normal vector
integer , dimension(:), pointer :: belm !list of elm adjacent to boundary face
integer , dimension(:), pointer :: kth_nghbr_of_1
integer , dimension(:), pointer :: kth_nghbr_of_2
end type bgrid_type
!----------------------------------------------------------
! Data type for face quantities (used for cell-centered schemes)
!
! A face is defined by a line segment connecting two nodes.
! The directed area is defined as a normal vector to the face,
! pointing in the direction from e1 to e2.
!
! n2
! o------------o
! . \ .
! . \ e2 .
! . e1 \ .
! . \ . Directed area is positive: n1 -> n2
! o----------o e1: left element
! n1 e2: right element (e2 > e1 or e2 = 0)
!
! Note: Each face has the following data.
!----------------------------------------------------------
type face_type
! to be constructed in the code (NB: boundary faces are excluded.)
integer :: n1, n2 !associated nodes
integer :: e1, e2 !associated elements
real(p2), dimension(2) :: dav !unit directed-area vector
real(p2) :: da !magnitude of the directed-area vector
end type face_type
type jac_type
! to be constructed in the code
real(p2), dimension(:,:) , pointer :: diag ! diagonal block of Jacobian matrix
real(p2), dimension(:,:,:), pointer :: off ! off-diagonal block of Jacobian matrix
end type jac_type
end module mod_grid_data_type
!* 3. module my_main_data
!*
!* This module defines the main data that will be used in the code.
!
module mod_my_main_data
use mod_constants , only : p2, one
use mod_grid_data_type, only : node_type, elm_type, edge_type, bgrid_type, face_type, jac_type
implicit none
private
public :: nnodes, node
public :: ntria, nelms, elm
public :: nedges, edge
public :: nbound, bound
public :: nfaces, face
public :: nq
public :: gradient_type, gradient_weight, gradient_weight_p
public :: inviscid_flux, inviscid_jac
public :: M_inf, rho_inf, u_inf, v_inf, p_inf
public :: gamma
public :: tolerance, max_iterations
public :: jac
public :: iteration_method
public :: CFL, CFLexp, CFL1, CFL2, CFL_ramp_steps
public :: sweeps
! Parameters
!Number of equtaions/variables in the target equtaion.
integer :: nq
!LSQ gradient related parameteres:
character(80) :: gradient_type ! "linear"; or for node-centered schemes can use "quadratic2"
character(80) :: gradient_weight ! "none" or "inverse_distance"
real(p2) :: gradient_weight_p ! 1.0 or any other real value
!Scheme parameters
character(80) :: inviscid_flux !Numerial flux for the inviscid terms (Euler)
!Reference quantities
real(p2) :: M_inf, rho_inf, u_inf, v_inf, p_inf
!Ratio of specific heats = 1.4 fpr air
real(p2) :: gamma = 1.4_p2
!Parameter for explicit scheme
real(p2) :: CFLexp ! Input CFL number for explicit RK2 scheme
!Implicit solver parameters
integer :: max_iterations ! Maximum number of iterations
real(p2) :: tolerance ! Tolerance for steady convergence
real(p2) :: CFL ! Actual CFL number
character(80) :: iteration_method ! explicit or implicit
character(80) :: inviscid_jac ! Inviscid flux for Jacobian
real(p2) :: CFL1, CFL2 ! Initial and terminal CFL number for ramping
integer :: CFL_ramp_steps ! Number of iterations to reach CFL2 from CFL1
integer :: sweeps ! Number of GS relaxation
! Node data
integer :: nnodes !total number of nodes
type(node_type), dimension(:), pointer :: node !array of nodes
! Element data (element=cell)
integer :: ntria !total number of triangler elements
integer :: nelms !total number of elements
type(elm_type), dimension(:), pointer :: elm !array of elements
! Edge data
integer :: nedges !total number of edges
type(edge_type), dimension(:), pointer :: edge !array of edges
! Boundary data
integer :: nbound !total number of boundary types
type(bgrid_type), dimension(:), pointer :: bound !array of boundary segments
! Face data (cell-centered scheme only)
integer :: nfaces !total number of cell-faces
type(face_type), dimension(:), pointer :: face !array of cell-faces
! Matrix data
type(jac_type), dimension(:) , pointer :: jac !array of edges
end module mod_my_main_data
!********************************************************************************
!* 4. module my_allocation
!*
!* This module defines some useful subroutines used for dynamic allocation.
!*
!* - my_alloc_int_ptr : Allocate/reallocate an integer 1D array
!* - my_alloc_p2_ptr : Allcoate/reallocate a real 1D array
!* - my_alloc_p2_matrix_ptr : Allcoate/reallocate a real 2D array
!*
!*
!*
!* written by Dr. Katate Masatsuka (info[at]cfdbooks.com),
!*
!* the author of useful CFD books, "I do like CFD" (http://www.cfdbooks.com).
!*
!* This is Version 0 (July 2015).
!* This F90 code is written and made available for an educational purpose.
!* This file may be updated in future.
!*
!********************************************************************************
module mod_my_allocation
implicit none
private
public :: my_alloc_int_ptr
public :: my_alloc_p2_ptr
public :: my_alloc_p2_matrix_ptr
contains
!********************************************************************************
!* This subroutine is useful to expand or shrink integer arrays.
!*
!* Array, x, will be allocated if the requested dimension is 1 (i.e., n=1)
!* Array, x, will be expanded to the requested dimension, n, if (n > dim(x)).
!* Array, x, will be shrinked to the requested dimension, n, if (n < dim(x)).
!*
!********************************************************************************
subroutine my_alloc_int_ptr(x,n)
implicit none
integer, intent(in) :: n
integer, dimension(:), pointer :: x
integer, dimension(:), pointer :: temp
integer :: i
if (n <= 0) then
write(*,*) "my_alloc_int_ptr received non-positive dimension. Stop."
stop
endif
! If not allocated, allocate and return
if (.not.(associated(x))) then
allocate(x(n))
return
endif
! If reallocation, create a pointer with a target of new dimension.
allocate(temp(n))
temp = 0
! (1) Expand the array dimension
if ( n > size(x) ) then
do i = 1, size(x)
temp(i) = x(i)
end do
! (2) Shrink the array dimension: the extra data, x(n+1:size(x)), discarded.
else
do i = 1, n
temp(i) = x(i)
end do
endif
! Destroy the target of x
! deallocate(x)
! Re-assign the pointer
x => temp
return
end subroutine my_alloc_int_ptr
!********************************************************************************
!********************************************************************************
!* This subroutine is useful to expand or shrink real arrays.
!*
!* Array, x, will be allocated if the requested dimension is 1 (i.e., n=1)
!* Array, x, will be expanded to the requested dimension, n, if (n > dim(x)).
!* Array, x, will be shrinked to the requested dimension, n, if (n < dim(x)).
!*
!********************************************************************************
subroutine my_alloc_p2_ptr(x,n)
use mod_constants , only : p2
implicit none
integer, intent(in) :: n
real(p2), dimension(:), pointer :: x
real(p2), dimension(:), pointer :: temp
integer :: i
if (n <= 0) then
write(*,*) "my_alloc_int_ptr received non-positive dimension. Stop."
stop
endif
! If not allocated, allocate and return
if (.not.(associated(x))) then
allocate(x(n))
return
endif
! If reallocation, create a pointer with a target of new dimension.
allocate(temp(n))
temp = 0
! (1) Expand the array dimension
if ( n > size(x) ) then
do i = 1, size(x)
temp(i) = x(i)
end do
! (2) Shrink the array dimension: the extra data, x(n+1:size(x)), discarded.
else
do i = 1, n
temp(i) = x(i)
end do
endif
! Destroy the target of x
deallocate(x)
! Re-assign the pointer
x => temp
return
end subroutine my_alloc_p2_ptr
!********************************************************************************
!* This subroutine is useful to expand or shrink real arrays.
!*
!* Array, x, will be allocated if the requested dimension is 1 (i.e., n=1)
!* Array, x, will be expanded to the requested dimension, n, if (n > dim(x)).
!* Array, x, will be shrinked to the requested dimension, n, if (n < dim(x)).
!*
!********************************************************************************
subroutine my_alloc_p2_matrix_ptr(x,n,m)
use mod_constants , only : p2
implicit none
integer, intent(in) :: n, m
real(p2), dimension(:,:), pointer :: x
real(p2), dimension(:,:), pointer :: temp
integer :: i, j
if (n <= 0) then
write(*,*) "my_alloc_int_ptr received non-positive dimension. Stop."
stop
endif
! If not allocated, allocate and return
if (.not.(associated(x))) then
allocate(x(n,m))
return
endif
! If reallocation, create a pointer with a target of new dimension.
allocate(temp(n,m))
temp = 0.0_p2
do i = 1, min(n, size(x,1))
do j = 1, min(m, size(x,2))
temp(i,j) = x(i,j)
end do
end do
! Destroy the target of x
deallocate(x)
! Re-assign the pointer
x => temp
return
end subroutine my_alloc_p2_matrix_ptr
end module mod_my_allocation
!********************************************************************************
!* 5. module edu2d_grid_data
!*
!* This module contians subroutines used for reading a grid, constructing
!* additional grid data, and check the grid data.
!*
!* - my_alloc_int_ptr : Allocate/reallocate an integer 1D array
!* - my_alloc_p2_ptr : Allcoate/reallocate a real 1D array
!* - my_alloc_p2_matrix_ptr : Allcoate/reallocate a real 2D array
!*
!*
!* Public subroutines:
!*
!* - read_grid : Read a grid file, allocate necessary data.
!* - construct_grid_data : Construct additional data, allocate more data.
!* - check_grid_data : Check the whole grid data.
!*
!* Private functions and subroutines:
!*
!* - tri_area : Computed a triangle area
!* - check_skewness_nc : Compute the skewness (e*n).
!* - compute_ar : Compute aspect ratio at node and element.
!*
!*
!*
!* written by Dr. Katate Masatsuka (info[at]cfdbooks.com),
!*
!* the author of useful CFD books, "I do like CFD" (http://www.cfdbooks.com).
!********************************************************************************
!* Read the grid and the exact solution.
!* ------------------------------------------------------------------------------
module mod_grid_data
private
public :: read_grid
public :: construct_grid_data
!public :: check_grid_data
contains
!********************************************************************************
!* Data to be read and stored:
!*
!* 1. Some numbers
!* nnodes = Number of nodes
!* ntria = Number of triangular elements
!* nquad = Number of quadrilateral elements
!* nelms = Total number of elements (=ntria+nquad)
!*
!* 2. Element data:
!* elm(1:nelms)%nvtx = Number of vertices of each element
!* elm(1:nelms)%vtx(:) = Pointer to vertices of each element
!*
!* 3. Node data: nodes are stored in a 1D array
!* node(1:nnodes)%x = x-coordinate of the nodes
!* node(1:nnodes)%y = y-coordinate of the nodes
!*
!* 4. Boundary Data:
!* nbound = Number of boundary segments
!* bound(1:nbound)%nbnodes = Number of nodes in each segment
!* bound(1:nbound)%bnode(:) = List of node numbers for each segment
!* bound(1:nbound)%bc_type = Boundary condition name for each segment
!* bound(1:nbound)%bc_type = Boundary condition name for each segment
!*
!********************************************************************************
subroutine read_grid(datafile_grid_in, datafile_bcmap_in)
use mod_constants , only : p2
use mod_my_main_data, only : nnodes, node, ntria, nelms, elm, nbound, bound
implicit none
character(80), intent(in) :: datafile_grid_in, datafile_bcmap_in
!Local variables
integer :: i, j, os, dummy_int
character(len=3):: TEMPSTRING
integer(p2) :: TEMPINI
! integer(p2) :: nnodes,ntria
!--------------------------------------------------------------------------------
! 1. Read grid file>: datafile_grid_in
ntria=0
nnodes=0
write(*,*) "Reading the grid file....", TRIM(datafile_grid_in)
! Open the input file.
open(unit=1, file=datafile_grid_in, status="unknown", iostat=os)
read(1,*) TEMPSTRING
read(1,*) TEMPSTRING
read(1,*) TEMPSTRING
read(1,*) TEMPSTRING
read(1,*) TEMPSTRING
do while (.not. eof(1))
read(1,*) TEMPSTRING
if(TEMPSTRING.eq.'GE') then
ntria=ntria+1
else
nnodes=nnodes+1
endif
enddo
close(1)
nelms=ntria
allocate(node(nnodes))
allocate(elm( nelms))
open(unit=1, file=datafile_grid_in, status="unknown", iostat=os)
read(1,*) TEMPSTRING
read(1,*) TEMPSTRING
read(1,*) TEMPSTRING
read(1,*) TEMPSTRING
read(1,*) TEMPSTRING
do i=1,nelms
elm(i)%nvtx=3
allocate(elm(i)%vtx(3))
read(1,*) TEMPSTRING,TEMPINI,elm(i)%vtx(1),TEMPINI,elm(i)%vtx(2),TEMPINI,elm(i)%vtx(3),TEMPINI,TEMPINI,TEMPINI,elm(i)%imt,TEMPINI
enddo
do i=1,nnodes
read(1,*) TEMPSTRING,TEMPINI,node(i)%x,node(i)%y,node(i)%z
enddo
close(1)
! Write out the grid data.
write(*,*)
write(*,*) " Total numbers:"
write(*,*) " nodes = ", nnodes
write(*,*) " triangles = ", ntria
write(*,*)
!
! attention ! this part are for boundary condition read
! left empty first 2016-1-16 22:53:28
!
end subroutine read_grid
!********************************************************************************
!* Construct the grid data:
!*
!* The following data, needed for NCFV method, will be constructed based on the
!* data read from the grid file.
!*
!* 1. Element data:
!* elm(:)%nnghbrs = Number of element neighbors of each element
!* elm(:)%nghbr(:) = List of element neighbors of each element
!* elm(:)%x = x-coordinate of the centroid
!* elm(:)%y = y-coordinate of the centroid
!* elm(:)%vol = Volume of the element
!*
!*
!* 2. Node data:
!* node(:)%nnghbrs = Number of node neighbors of each node
!* node(:)%nghbr(:)= List of node neighbors of each node
!* node(:)%nelms = Number of adjacent elements of each node
!* node(:)%elm = List of adjacent elements of each node
!* node(:)%vol = Volume of the dual volume around each node
!*
!* 3. Edge data:
!* edge(:)%n1, n2 = End nodes of each edge (edge points n1 -> n2)
!* edge(:)%e1, e2 = Left and right elements of each edge
!* edge(:)%dav = Unit directed area vector of each edge
!* edge(:)%da = Magnitude of the directed area vector for each edge
!* edge(:)%ev = Unit edge vector of each edge (vector n1 -> n2)
!* edge(:)%e = Magnitude of the edge vector for each edge
!*
!*
!* 4. Boudnary data
!* bound(:)%bnx = Outward normal at boundary nodes (x-component of unit vector)
!* bound(:)%bny = Outward normal at boundary nodes (y-component of unit vector)
!* bound(:)%bn = Magnitude of (bnx,bny)
!* NOTE: In this code, the above normal vector at boundary nodes is computed by
!* a quadratic fit. It is sufficiently accuarte for 3rd-order schemes.
!* See http://www.hiroakinishikawa.com/My_papers/nishikawa_jcp2015v281pp518-555_preprint.pdf
!* for details on the quadratic approximation for computing more accurate normals.
!* bound(:)%bfnx = Outward normal at boundary nodes (x-component of unit vector)
!* bound(:)%bfny = Outward normal at boundary nodes (y-component of unit vector)
!* bound(:)%bfn = Magnitude of (bfnx,bfny)
!* bound(:)%belm = Element to which the boundary face belongs
!*
!********************************************************************************
subroutine construct_grid_data
use mod_my_main_data , only : nnodes, node, nelms, elm, nedges, edge, nbound, bound, face, nfaces
use mod_constants , only : p2, zero, half, third, two
use mod_my_allocation, only : my_alloc_int_ptr, my_alloc_p2_ptr, my_alloc_p2_matrix_ptr
implicit none
!Local variables
integer :: i, j, k, ii, in, im, jelm, v1, v2, v3, v4
real(p2) :: x1, x2, x3, x4, y1, y2, y3, y4, xm, ym, xc, yc
real(p2) :: xj, yj, xm1, ym1, xm2, ym2, dsL,dsR,dx,dy
logical :: found
integer :: vL, vR, n1, n2, e1, e2
integer :: vt1, vt2, ielm
integer :: ave_nghbr, min_nghbr, max_nghbr, imin, imax
integer :: iedge
real(p2) :: ds
! Some initialization
v2 = 0
vL = 0
im = 0
jelm = 0
write(*,*) "Constructing grid data...."
! Initializations
do i = 1, nnodes
node(i)%nelms = 0
end do
nedges = 0
!--------------------------------------------------------------------------------
! Loop over elements and construct the fololowing data.
!
! 1. Surrounding elements: node(:)%nelms, node(:)%elm(:)
!
! Example: Node i is surrounded by the eleemnts, 23, 101, 13, 41.
! node(i)%nelms = 4
! node(i)%elm(1) = 23
! node(i)%elm(2) = 13
! node(i)%elm(3) = 41
! node(i)%elm(4) = 101
!
! o-------o-------------o
! / | . |
! / 23 | 41 |
! o----------o-------------o
! \ i \ |
! \ 101 \ 13 |
! \ \ |
! o----------o---------o
!
! 2. Element quantities : elm(:)%x,elm(:)%y,elm(:)%vol
!
! o-----------o
! \ | o
! \ (x,y)| / \
! \ . | / \
! \ | / . \ (x,y): centroid coordinates
! \ | / (x,y) \ vol: volume of element
! o-----o o---------o
elements : do i = 1, nelms
v1 = elm(i)%vtx(1)
v2 = elm(i)%vtx(2)
v3 = elm(i)%vtx(3)
x1 = node(v1)%x
x2 = node(v2)%x
x3 = node(v3)%x
y1 = node(v1)%y
y2 = node(v2)%y
y3 = node(v3)%y
! Distribute the element index to nodes.
node(v1)%nelms = node(v1)%nelms + 1
call my_alloc_int_ptr(node(v1)%elm, node(v1)%nelms)
node(v1)%elm(node(v1)%nelms) = i
node(v2)%nelms = node(v2)%nelms + 1
call my_alloc_int_ptr(node(v2)%elm, node(v2)%nelms)
node(v2)%elm(node(v2)%nelms) = i
node(v3)%nelms = node(v3)%nelms + 1
call my_alloc_int_ptr(node(v3)%elm, node(v3)%nelms)
node(v3)%elm(node(v3)%nelms) = i
! Triangle centroid and volume
! radius of circle in the triangle
! r=2*area/(a+b+c)
elm(i)%x = third*(x1+x2+x3)
elm(i)%y = third*(y1+y2+y3)
elm(i)%vol = tri_area(x1,x2,x3,y1,y2,y3)
elm(i)%radius=two*elm(i)%vol/ &
& (sqrt((x2-x1)**2+(y2-y1)**2)+ &
& sqrt((x3-x1)**2+(y3-y1)**2)+ &
& sqrt((x2-x3)**2+(y2-y3)**2))
end do elements
! Median dual volume
do i = 1, nnodes
node(i)%vol = zero
end do
elementsv : do i = 1, nelms
v1 = elm(i)%vtx(1)
v2 = elm(i)%vtx(2)
v3 = elm(i)%vtx(3)
node(v1)%vol = node(v1)%vol + third*elm(i)%vol
node(v2)%vol = node(v2)%vol + third*elm(i)%vol
node(v3)%vol = node(v3)%vol + third*elm(i)%vol
end do elementsv
!--------------------------------------------------------------------------------
! Loop over elements 2
!
! Allocate elm(:)%nghbr(:) : elm(:)%nnghrs, elm(:)%nghr(:)
! Construct element nghbr data: elm(:)%nghbr(:)
! Order of neighbor elements [e1,e2,e3,..] are closely related to
! the order of vertices [v1,v2,v3,..] (see below).
!
! o------o
! | |
! v4| e1 |v3 v3
! o-----o------o------o o---------o------------o
! | | | | . . . .
! | e2 | | e4 | . e2 . . e1 .
! o-----o------o------o . . . .
! v1 | .v2 v1 o---------o v2
! | e3 . . e3 .
! | . . .
! | . . .
! | . o
! o
!
! Allocate the neighbor array
do i = 1, nelms
elm(i)%nnghbrs = 3
allocate(elm(i)%nghbr(3))
end do
elements2 : do i = 1, nelms
elm_vertex : do k = 1, elm(i)%nvtx
! Begin constructing the element-neighbor data
! Get the face of the element i:
!
! vL vR
! o------o
! / |
! / |
! o---------o
!
if (k < elm(i)%nvtx) vL = elm(i)%vtx(k+1)
if (k == elm(i)%nvtx) vL = elm(i)%vtx(1)
vR = elm(i)%vtx(k)
! Loop over the surrounding elements of the node vR,
! and find the element neighbor from them.
found = .false.
elms_around_vR : do j = 1, node(vR)%nelms
jelm = node(vR)%elm(j)
edge_matching : do ii = 1, elm(jelm)%nvtx
v1 = elm(jelm)%vtx(ii)
if (ii > 1) v2 = elm(jelm)%vtx(ii-1)
if (ii == 1) v2 = elm(jelm)%vtx(elm(jelm)%nvtx)
if (v1==vR .and. v2==vL) then
found = .true.
im = ii+1
if (im > elm(jelm)%nvtx) im = im - elm(jelm)%nvtx
exit edge_matching
endif
end do edge_matching
if (found) exit elms_around_vR
end do elms_around_vR
in = k + 2
if (in > elm(i)%nvtx) in = in - elm(i)%nvtx
if (found) then
elm( i)%nghbr(in) = jelm
elm(jelm)%nghbr(im) = i
else
elm( i)%nghbr(in) = 0
endif
end do elm_vertex
end do elements2
!--------------------------------------------------------------------------------
! Edge-data for node-centered (edge-based) scheme.
!
! Loop over elements 3
! Construct edge data: edge(:)%n1, n2, e1, e2.
! Edge points from node n1 to node n2.
!
! n2
! o------------o
! . \ .
! . \ e2 .
! . e1 \ .
! . \ . Directed area is positive: n1 -> n2
! o----------o e1: left element
! n1 e2: right element (e2 > e1 or e2 = 0)
! First count the number of edges.
!
! NOTE: Count edges only if the neighbor element number is
! greater than the current element (i) to avoid double
! count. Zero element number indicates that it is outside
! the domain (boundary face).
elements0 : do i = 1, nelms
v1 = elm(i)%vtx(1)
v2 = elm(i)%vtx(2)
v3 = elm(i)%vtx(3)
if ( elm(i)%nghbr(3) > i .or. elm(i)%nghbr(3)==0 ) then
nedges = nedges + 1
endif
if ( elm(i)%nghbr(1) > i .or. elm(i)%nghbr(1)==0 ) then
nedges = nedges + 1
endif
if ( elm(i)%nghbr(2) > i .or. elm(i)%nghbr(2)==0 ) then
nedges = nedges + 1
endif
end do elements0
! Allocate the edge array.
allocate(edge(nedges))
nedges = 0
edge(:)%e1 = 0
edge(:)%e2 = 0
! Construct the edge data:
! two end nodes (n1, n2), and left and right elements (e1, e2)
elements3 : do i = 1, nelms
v1 = elm(i)%vtx(1)
v2 = elm(i)%vtx(2)
v3 = elm(i)%vtx(3)
if ( elm(i)%nghbr(3) > i .or. elm(i)%nghbr(3)==0 ) then
nedges = nedges + 1
edge(nedges)%n1 = v1
edge(nedges)%n2 = v2
edge(nedges)%e1 = i
edge(nedges)%e2 = elm(i)%nghbr(3)
endif
if ( elm(i)%nghbr(1) > i .or. elm(i)%nghbr(1)==0 ) then
nedges = nedges + 1
edge(nedges)%n1 = v2
edge(nedges)%n2 = v3
edge(nedges)%e1 = i
edge(nedges)%e2 = elm(i)%nghbr(1)
endif
if ( elm(i)%nghbr(2) > i .or. elm(i)%nghbr(2)==0 ) then
nedges = nedges + 1
edge(nedges)%n1 = v3
edge(nedges)%n2 = v1
edge(nedges)%e1 = i
edge(nedges)%e2 = elm(i)%nghbr(2)
endif
end do elements3
! Loop over edges
! Construct edge vector and directed area vector.
!
! Edge vector is a simple vector pointing from n1 to n2.
! For each edge, add the directed area vector (dav) from
! the left and right elements.
!
! n2
! o-----------o-----------o
! | dav | dav |
! | ^ | ^ |
! | | | | |
! | c - - - m - - -c |
! | | |
! | | | m: edge midpoint
! | | | c: element centroid
! o-----------o-----------o
! n1
!
edges : do i = 1, nedges
n1 = edge(i)%n1
n2 = edge(i)%n2
e1 = edge(i)%e1
e2 = edge(i)%e2
xm = half*( node(n1)%x + node(n2)%x )
ym = half*( node(n1)%y + node(n2)%y )
edge(i)%dav = zero
! Contribution from the left element
if (e1 > 0) then
xc = elm(e1)%x
yc = elm(e1)%y
edge(i)%dav(1) = -(ym-yc)
edge(i)%dav(2) = xm-xc
endif
! Contribution from the right element
if (e2 > 0) then
xc = elm(e2)%x
yc = elm(e2)%y
edge(i)%dav(1) = edge(i)%dav(1) -(yc-ym)
edge(i)%dav(2) = edge(i)%dav(2) + xc-xm
endif