-
Notifications
You must be signed in to change notification settings - Fork 0
/
lateration.f90
972 lines (904 loc) · 33.2 KB
/
lateration.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
module lateration
implicit none
! 2017/11/23
! This code is released on the following Public Domain license :
! The unlicense.
! Version 1.0
! This version of the code corresponds to the paper submitted in GJI
! It is written in Fortran90.
! The unstructured mesh is defined by the type "mesh" composed by
! "Nnodes" vertices and "Ncells" faces. The coordinates of the vertices
! in the 3D cartesian space are "px, py, pz". "cell" is the index array
! defining the faces. Its dimension is "Ncells*3" because the code works
! on triangular faces, defined by three vertices.
! Warning : The indices of the vertex start to 1.
! main routines :
! - one source versus all the vertices of the mesh
! onevsall2d(amesh,dist,fast)
! this is the main routine.
! in : "amesh" is an unstructured mesh
! in : "fast" is a logical. Put it to false to trigger a search
! for diffraction point. If your mesh is simple, fast=.true.
! should be enough.
! inout : "dist" is the distance array. Its dimension is "Nnodes".
! This array needs to be allocated and initialized first.
! pre_onevsall2d_onvertex(amesh,k,dist)
! this routine helps the users to initialize the distance array when
! the source is on a vertex
! in : "amesh" is an unstructured mesh
! in : "k" is the index of the vertex (1 =< k =< Nnodes)
! out : "dist" is the output distance array. This routine allocates it, initializes
! it to infinity except for dist(k)=0.
! pre_onevsall2d_offvertex(amesh,dist,v,d)
! Same routine as above but in the case where the source is not on a vertex but
! on a face.
! in : "amesh" is an unstructured mesh
! in : "v" is an index array of dimension 3 indicating the indices of the vertices
! defining the face where is located the source
! in : "d" is a small distance array of dimension 3 which for each vertex in "v"
! indicates the distance of this vertex to the source.
! out : "dist" is the output distance array. This routine allocates it, initializes
! it to infinity except for dist(v)=d.
! - compute a distance matrix (fast option always true)
! allvsall2d(amesh,dist)
! in : "amesh" is an unstructured mesh
! out : "dist" is the output distance matrix of dimensions (Nnodes*Nnodes)
!
! Compilation : compile this fortran module with a Fortran90 compiler. This code has been
! tested on linux and Freebsd systems with a gnu gfortran compiler.
!
! For any questions or problems : andre.herrero at ingv.it
type mesh
integer :: Nnodes,Ncells
real, dimension(:), allocatable :: px,py,pz
integer, dimension(:,:), allocatable :: cell
end type mesh
type listn
integer :: idnode
type(listn), pointer :: previous
type(listn), pointer :: next
end type listn
type container
type(listn), pointer :: ptr
end type container
type point
real :: x,y,z
end type point
type path
integer :: cellid
type(point) :: pos
type(path), pointer :: next
end type path
real, parameter :: infinity=1.e32
logical :: verbose=.false.
contains
!###############################################################################
subroutine allvsall2d(amesh,distarray)
type(mesh) :: amesh
real, dimension(:,:), allocatable :: distarray
type(container), dimension(amesh%Nnodes) :: ntoc
type(listn), pointer :: ntodo,cellcur,pcur,last
real :: d01,d02,d12,r1,r2,dface,dedge,dtest
integer, dimension(2) :: otherninc
integer :: i,j,k,toggle
logical :: begin,hasbeenupdated
! intialisation of dist array
! allocation
allocate(distarray(amesh%Nnodes,amesh%Nnodes))
! set to infinity
distarray=infinity
! trace to zero
do i=1,amesh%Nnodes
distarray(i,i)=0.
enddo
! computing distance to neighbor nodes cell by cell
do i=1,amesh%Ncells
! distance 1-2
call updclosedist(amesh,distarray,i,1,2)
! distance 2-3
call updclosedist(amesh,distarray,i,2,3)
! distance 3-1
call updclosedist(amesh,distarray,i,3,1)
enddo
! computing the node-to-cell array ntoc
call compntoc(amesh,ntoc)
! main loop on nodes - nodes is a starting point
if (verbose) write(*,*) 'starting main loop on nodes'
do k=1,amesh%Nnodes
if (verbose) write(*,*) '##############################################################'
if (verbose) write(*,*) 'distance from node #',k,'/',amesh%Nnodes
call printperc(k,amesh%Nnodes)
! initializing node todo list
if (verbose) write(*,*) 'entering vicinode'
call vicinode(amesh,k,ntoc(k)%ptr,ntodo)
! loop on the to do list
if (verbose) call printlist(ntodo)
if (verbose) write(*,*) 'entering in the ntodo list management loop'
do while (associated(ntodo))
! searching the cells attached to the first element of the to do list
if (verbose) write(*,*) 'state of ntodo :'
if (verbose) call printlist(ntodo)
if (verbose) write(*,*) 'propagating distance from node #',ntodo%idnode
if (verbose) write(*,*) 'its own distance is :',distarray(k,ntodo%idnode)
hasbeenupdated=.true.
toggle=2
do while(hasbeenupdated)
hasbeenupdated=.false.
! switch the toggle
toggle=3-toggle
if (verbose) then
if (toggle==1) write(*,*) 'seep forward on cellist'
if (toggle==2) write(*,*) 'seep backward on cellist'
endif
if (toggle==1) then
pcur=>ntoc(ntodo%idnode)%ptr
else
pcur=>last
endif
do while (associated(pcur))
! find the two complemantory nodes in the current cell
if (verbose) write(*,*) ' working on cell #',pcur%idnode
call givencomp(amesh,otherninc,pcur,ntodo)
if (verbose) write(*,*) ' complemantory nodes : ',otherninc
! loop on the two complemantory nodes
do i=1,2
if (verbose) write(*,'(a4,4(i2.2))') 'code',k,ntodo%idnode,pcur%idnode,otherninc(i)
if (verbose) write(*,*) ' working on complemantory nodes :',otherninc(i)
! symmetry check
if (verbose) write(*,*) ' symmetry check:'
if (verbose) write(*,*) otherninc(i),'<',k,' ???'
if (otherninc(i) < k) then
if (verbose) write(*,*) ' yes'
if (verbose) write(*,*) 'is distarray(k,otherninc(i)) > distarray(otherninc(i),k) ?'
if (verbose) write(*,*) distarray(k,otherninc(i)),' > ',distarray(otherninc(i),k)
if (distarray(k,otherninc(i)) > distarray(otherninc(i),k)) then
if (verbose) write(*,*) ' yes then take the complimentary'
distarray(k,otherninc(i))=distarray(otherninc(i),k)
call updatelist(otherninc(i),ntodo)
endif
cycle
endif
! edge propagation
dedge=distarray(k,ntodo%idnode)+distarray(ntodo%idnode,otherninc(i))
if (verbose) write(*,*) 'dedge : ',distarray(k,ntodo%idnode),'+',distarray(ntodo%idnode,otherninc(i))
if (verbose) write(*,*) 'dedge : ',dedge
! face propagation
if (distarray(k,otherninc(3-i)) /= infinity) then
if (verbose) write(*,*) 'computing dface because ',distarray(k,otherninc(3-i)),' is not infinity'
d12=distarray(ntodo%idnode,otherninc(3-i))
d01=distarray(otherninc(i),ntodo%idnode)
d02=distarray(otherninc(i),otherninc(3-i))
if (verbose) write(*,*) 'd12,d01,d02 :'
if (verbose) write(*,*) d12,d01,d02
r1=distarray(k,ntodo%idnode)
r2=distarray(k,otherninc(3-i))
if (verbose) write(*,*) 'r1,r2 : ',r1,r2
dface=dcircle(d12,d01,d02,r1,r2)
if (verbose) write(*,*) 'dface : ',dface
else
dface=infinity
endif
dtest=min(dedge,dface)
! dtest=dedge
if (verbose) write(*,*) 'dtest=min(dedge,dface) : ',dtest
if (verbose) write(*,*) 'dtest < distance between',k,' and ',otherninc(i)
if (verbose) write(*,*) dtest,distarray(k,otherninc(i))
if (dtest < distarray(k,otherninc(i))) then
if (verbose) write(*,*) 'better !'
! distance is better : if not in the list, add it
distarray(k,otherninc(i))=dtest
if (verbose) write(*,*) 'updatelist with :',otherninc(i)
call updatelist(otherninc(i),ntodo)
if (verbose) write(*,*) 'state of ntodo :'
if (verbose) call printlist(ntodo)
endif
enddo
! moving on the vicinity list
if (toggle==1) then
last=>pcur
pcur=>pcur%next
else
pcur=>pcur%previous
endif
enddo
enddo
! cancelling the cell vicinity list
! removing the first element of the to do list
if (associated(ntodo%next)) then
ntodo=>ntodo%next
deallocate(ntodo%previous)
nullify(ntodo%previous)
else
deallocate(ntodo)
nullify(ntodo)
endif
enddo
enddo
! deallocating ntoc
call deallocntoc(ntoc,amesh%Nnodes)
end subroutine allvsall2d
!###############################################################################
subroutine pre_onevsall2d_offvertex(amesh,distarray,v,d)
! initialisation of onevsall2d in case of a unique source at the
! distance d(1), d(2) and d(3) from vertice v(1), v(2) and v(3) respectively of the SAME face.
! Use this example to create your own routine to initialize distarray
! for a generic multi source problem with n points and not only 3.
type(mesh) :: amesh
real, dimension(:), allocatable :: distarray
integer, dimension(3) :: v
real, dimension(3) :: d
integer :: i
type(listn), pointer :: pcur
! allocation
allocate(distarray(amesh%Nnodes))
! initialisation to infinity
distarray=infinity
! initialisation of distarray and ntodo
do i=1,3
distarray(v(i))=d(i)
enddo
return
end subroutine pre_onevsall2d_offvertex
!###############################################################################
subroutine pre_onevsall2d_onvertex(amesh,k,distarray)
! initialisation of onevsall2d in case of a unique source on a vertex k
type(mesh) :: amesh
real, dimension(:), allocatable :: distarray
integer :: k
! allocation
allocate(distarray(amesh%Nnodes))
! initialisation to infinity
distarray=infinity
! initialization of distance at k to zero
distarray(k)=0.
return
end subroutine pre_onevsall2d_onvertex
!###############################################################################
subroutine onevsall2d(amesh,dist,fast)
! Compute the distance, in an array format, for all the vertices of the mesh
! amesh which distance is not set to infinity.
! Warning : dist should be initialized before to infinity for all the
! vertices except those representing the sources.
type(mesh) :: amesh
real, dimension(amesh%Nnodes) :: dist
logical :: fast
type(container), dimension(amesh%Nnodes) :: ntoc
type(listn), pointer :: cellcur,pcur,last,pmin
type(listn), pointer :: ntodo
real :: d13,d23,d12,r1,r2,dface,dedge,dtest,db
integer, dimension(2) :: otherninc
integer :: i,j,toggle,idiff,waitfordiff
logical :: begin,hasbeenupdated,reloop,firstrun,diffoccur
integer :: nswp,mxswp
real, dimension(amesh%Nnodes) :: distarray
logical, dimension(amesh%Nnodes) :: checksecondary
integer, parameter :: waitdiffthres=25
! mxswp=0
! computing the node-to-cell array ntoc
call compntoc(amesh,ntoc)
! initialize the secondary distance array
distarray=dist
checksecondary=.false.
! initialization of ntodo from non infinite values inside distarray
begin=.true.
do i=1,amesh%Nnodes
if (distarray(i) > .9*infinity) cycle
! if a vertex has a null distance, it is not a secondary diffraction point
if (distarray(i) < epsilon(distarray(i))) checksecondary(i)=.true.
if (begin) then
allocate(ntodo)
nullify(ntodo%previous)
nullify(ntodo%next)
pcur=>ntodo
begin=.false.
else
allocate(pcur%next)
nullify(pcur%next%next)
pcur%next%previous=>pcur
pcur=>pcur%next
endif
pcur%idnode=i
enddo
! initializing reloop to true
! The condition is changed or not at the end of the loop
! The fast first loop is mandatory with a null distance offset
reloop=.true.
firstrun=.true.
diffoccur=.false.
db=0.
do while (reloop)
! loop on the to do list
if (verbose) call printlist(ntodo)
if (verbose) write(*,*) 'entering in the ntodo list management loop'
do while (associated(ntodo))
if (verbose) write(*,*) 'state of ntodo :'
if (verbose) call printlist(ntodo)
! searching the cells attached to the first element of the to do list
call lookformin(ntodo,distarray,amesh%Nnodes,pmin)
if (verbose) write(*,*) 'propagating distance from node #',pmin%idnode
if (verbose) write(*,*) 'its own distance is :',distarray(pmin%idnode)
hasbeenupdated=.true.
toggle=2
! nswp=0
do while (hasbeenupdated)
! nswp=nswp+1
hasbeenupdated=.false.
toggle=3-toggle
if (verbose) then
if (toggle==1) write(*,*) 'seep forward on cellist'
if (toggle==2) write(*,*) 'seep backward on cellist'
endif
if (toggle==1) then
pcur=>ntoc(pmin%idnode)%ptr
else
pcur=>last
endif
do while (associated(pcur))
! find the two complemantory nodes in the current cell
if (verbose) write(*,*) ' working on cell #',pcur%idnode
call givencomp(amesh,otherninc,pcur,pmin)
if (verbose) write(*,*) ' complemantory nodes : ',otherninc
! loop on the two complemantory nodes
do i=1,2
if (verbose) write(*,'(a4,3(i4.4))') 'code',pmin%idnode,pcur%idnode,otherninc(i)
if (verbose) write(*,*) ' working on complemantory nodes :',otherninc(i)
! edge propagation
dedge=distarray(pmin%idnode)+donedge(amesh,pmin%idnode,otherninc(i))
if (verbose) write(*,*) 'dedge : ',distarray(pmin%idnode),'+',donedge(amesh,pmin%idnode,otherninc(i))
if (verbose) write(*,*) 'dedge : ',dedge
! face propagation
if (distarray(otherninc(3-i)) /= infinity) then
if (verbose) write(*,*) 'computing dface because ',distarray(otherninc(3-i)),' is not infinity'
d12=donedge(amesh,pmin%idnode,otherninc(3-i))
d13=donedge(amesh,otherninc(i),pmin%idnode)
d23=donedge(amesh,otherninc(i),otherninc(3-i))
if (verbose) write(*,*) 'd12,d13,d23 :'
if (verbose) write(*,*) d12,d13,d23
r1=distarray(pmin%idnode)
r2=distarray(otherninc(3-i))
if (verbose) write(*,*) 'r1,r2 : ',r1,r2
dface=dcircle(d12,d13,d23,r1,r2)
if (verbose) write(*,*) 'dface : ',dface
else
dface=infinity
endif
dtest=min(dedge,dface)
! dtest=dedge
if (verbose) write(*,*) 'dtest=min(dedge,dface) : ',dtest
if (verbose) write(*,*) 'dtest < actual minimum on ',otherninc(i)
if (verbose) write(*,*) dtest,distarray(otherninc(i))
if (dtest < distarray(otherninc(i))) then
hasbeenupdated=.true.
if (verbose) write(*,*) 'better !'
! distance is better : if not in the list, add it
distarray(otherninc(i))=dtest
call updatelist(otherninc(i),ntodo)
if (verbose) write(*,*) 'updatelist with :',otherninc(i)
if (verbose) write(*,*) 'state of ntodo :'
if (verbose) call printlist(ntodo)
endif
enddo ! complementary loop on cell
! moving on the vicinity list
if (toggle==1) then
last=>pcur
pcur=>pcur%next
else
pcur=>pcur%previous
endif
enddo ! neighbor cell to the current node pmin
enddo ! sweep loop
! mxswp=max(mxswp,nswp)
! diff case and no diffraction has been detected yet
if (.not.firstrun.and..not.diffoccur) then
if (distarray(pmin%idnode)+dist(idiff) < dist(pmin%idnode)) then
! diffraction detected
diffoccur=.true.
else
! no diffraction
waitfordiff=waitfordiff-1
if (waitfordiff == 0) then
! countdown expired for diffraction. hard exit on ntodo list to pass to another
! potential secondary source
call wipe(ntodo)
exit
endif
endif
endif
! removing the treated element pmin of the to do list
call removepminfromthelist(pmin,ntodo)
enddo ! ntodo loop
! decision to reloop or not
! fast case
if (fast) then
dist=distarray
! first case of general exit : only one run is requested
reloop=.false.
cycle
endif
if (firstrun) then
dist=distarray
else
if (diffoccur) then
! a secondary diff has occured
do i=1,amesh%Nnodes
dist(i)=min(distarray(i)+dist(idiff),dist(i))
enddo
endif
endif
idiff=nextdiffid(dist,amesh%Nnodes,checksecondary)
! second case of general exit : all the potential secondary source have been investigated.
if (idiff == 0) then
reloop=.false.
cycle
endif
! preparation for the next loop
checksecondary(idiff)=.true.
waitfordiff=waitdiffthres
diffoccur=.false.
! At this point notodo is deallocated. reallocate it with idiff
allocate(ntodo)
nullify(ntodo%previous)
nullify(ntodo%next)
ntodo%idnode=idiff
! reinitialize distarray
distarray=infinity
distarray(idiff)=0.
firstrun=.false.
enddo ! end reloop section
! deallocating ntoc
call deallocntoc(ntoc,amesh%Nnodes)
end subroutine onevsall2d
!###############################################################################
subroutine wipe(ntodo)
type(listn), pointer :: ntodo
type(listn), pointer :: p1,p2
if (.not.associated(ntodo)) then
write(*,*) "warning... ntodo is unassociated already"
return
endif
if (.not.associated(ntodo%next)) then
deallocate(ntodo)
return
endif
p2=>ntodo
do while (associated(p2%next))
p1=>p2
p2=>p2%next
deallocate(p1)
enddo
deallocate(p2)
end subroutine wipe
!###############################################################################
function nextdiffid(dist,n,checksecondary)
! find the minimum value in dist skipping the vertex checked yet and returns
! its position (id)
integer :: nextdiffid,n
real, dimension(n) :: dist
logical, dimension(n) :: checksecondary
integer :: i
real :: vmin
nextdiffid=0
vmin=infinity
do i=1,n
if (checksecondary(i)) cycle
if (dist(i) < vmin) then
vmin=dist(i)
nextdiffid=i
endif
enddo
return
end function nextdiffid
!###############################################################################
subroutine lookformin(ntodo,distarray,n,pmin)
type(listn), pointer :: pmin,ntodo
integer :: n
real, dimension(n) :: distarray
type(listn), pointer :: pcur
real :: vmin
pcur=>ntodo
vmin=distarray(pcur%idnode)
pmin=>pcur
do while (associated(pcur%next))
pcur=>pcur%next
if (distarray(pcur%idnode) < vmin) then
vmin=distarray(pcur%idnode)
pmin=>pcur
endif
enddo
return
end subroutine lookformin
!###############################################################################
subroutine removepminfromthelist(pmin,ntodo)
type(listn), pointer :: ntodo,pmin,pn,pp
if (associated(pmin%previous).and.associated(pmin%next)) then
if (verbose) write(*,*) 'remove pmin case1'
pn=>pmin%next
pp=>pmin%previous
deallocate(pp%next)
pp%next=>pn
pn%previous=>pp
return
endif
if (.not.associated(pmin%previous).and..not.associated(pmin%next)) then
if (verbose) write(*,*) 'remove pmin case2'
nullify(ntodo)
return
endif
if (.not.associated(pmin%previous)) then
if (verbose) write(*,*) 'remove pmin case3'
ntodo=>ntodo%next
deallocate(ntodo%previous)
nullify(ntodo%previous)
return
endif
! last case : pmin points on the last element of ntodo list
if (verbose) write(*,*) 'remove pmin case4'
pp=>pmin%previous
deallocate(pp%next)
nullify(pp%next)
return
end subroutine removepminfromthelist
!###############################################################################
subroutine printperc(a,b)
integer :: a,b
! write(*,*) a,b
write(*,'(a1,$)') char(8)
write(*,'(a1,$)') char(8)
write(*,'(a1,$)') char(8)
! write(*,*) a,b
if (a == b) then
write(*,'(a4)') '100%'
else
write(*,'(i2.2,a1,$)') int(100.*(float(a)/float(b))),'%'
endif
return
end subroutine printperc
!###############################################################################
subroutine printlist(alist)
type(listn), pointer :: alist
type(listn), pointer :: pcur
pcur=>alist
do while (associated(pcur))
write(*,'(i5,$)') pcur%idnode
pcur=>pcur%next
enddo
write(*,*)
end subroutine printlist
!###############################################################################
subroutine updatelist(anode,alist)
! Check the presence of a node (anode) in a list (alist).
! If the node is not present, it is added to the list at its end.
type(listn), pointer :: alist
integer :: anode
type(listn), pointer :: pcur,last
logical :: isinit
! is anode in alist ?
isinit=.false.
pcur=>alist
do while (associated(pcur))
isinit=(pcur%idnode == anode)
last=>pcur
pcur=>pcur%next
! if yes exit
if (isinit) return
enddo
! if no add anode to alist at the end
allocate(last%next)
last%next%previous=>last
nullify(last%next%next)
last=>last%next
last%idnode=anode
return
end subroutine updatelist
!###############################################################################
function isinit(anode,alist)
! Check the presence of a node (anode) in a list (alist).
type(listn), pointer :: alist
integer :: anode
logical :: isinit
type(listn), pointer :: pcur
isinit=.false.
pcur=>alist
do while (associated(pcur))
isinit=(pcur%idnode == anode)
if (isinit) return
pcur=>pcur%next
enddo
return
end function isinit
!###############################################################################
function donedge(amesh,i,j)
type(mesh) :: amesh
integer :: i,j
real :: donedge
donedge=sqrt((amesh%px(i)-amesh%px(j))**2+&
(amesh%py(i)-amesh%py(j))**2+&
(amesh%pz(i)-amesh%pz(j))**2)
return
end function donedge
!###############################################################################
subroutine updclosedist(amesh,distarray,k,i,j)
type(mesh) :: amesh
real, dimension(:,:), allocatable :: distarray
integer :: i,j,k
if (distarray(amesh%cell(k,i),amesh%cell(k,j)) == infinity) then
distarray(amesh%cell(k,i),amesh%cell(k,j))=&
sqrt((amesh%px(amesh%cell(k,i))-amesh%px(amesh%cell(k,j)))**2+&
(amesh%py(amesh%cell(k,i))-amesh%py(amesh%cell(k,j)))**2+&
(amesh%pz(amesh%cell(k,i))-amesh%pz(amesh%cell(k,j)))**2)
! reciprocity
distarray(amesh%cell(k,j),amesh%cell(k,i))=distarray(amesh%cell(k,i),amesh%cell(k,j))
endif
end subroutine updclosedist
!###############################################################################
subroutine givencomp(amesh,otherninc,pc,pa)
type(mesh) :: amesh
integer, dimension(2) :: otherninc
type(listn), pointer :: pc,pa
integer :: i,k
k=1
do i=1,3
if (amesh%cell(pc%idnode,i) /= pa%idnode) then
otherninc(k)=amesh%cell(pc%idnode,i)
k=k+1
endif
enddo
return
end subroutine givencomp
!###############################################################################
function dplane(d12,d13,d23,r1,r2)
real :: dplane,d12,d13,d23,r1,r2
real :: x,y,xc,yc
real :: dr,xn,yn,dn,d13n,d23n
! position of v3
x=(d12**2-d23**2+d13**2)/(2*d12)
y=sqrt(max(d13**2-x**2,0.))
! difference of distance at the base
dr=abs(r1-r2)
! computation of the new base length
dn=sqrt(d12**2.-dr**2.)
if (r1 < r2) then
! case where v2 remains and V1 is changed
xn=(d12**2-dn**2+dr**2)/(2*d12)
yn=(sqrt(max(dr**2-xn**2,0.)))
d13n=sqrt((x-xn)**2.+(y-yn)**2.)
! reposition of v3 in the new frame
x=(dn**2-d23**2+d13n**2)/(2*dn)
if (x > 0 .and. x < dn) then
y=sqrt(max(d13n**2-x**2,0.))
dplane=r2+y
else
dplane=infinity
endif
else
! case where v1 remains and V2 is changed
xn=(d12**2-dr**2+dn**2)/(2*d12)
yn=(sqrt(max(dn**2-xn**2,0.)))
d23n=sqrt((x-xn)**2.+(y-yn)**2.)
! reposition of v3 in the new frame
x=(dn**2-d23n**2+d13**2)/(2*dn)
if (x > 0 .and. x < dn) then
y=sqrt(max(d13**2-x**2,0.))
dplane=r1+y
else
dplane=infinity
endif
endif
end function dplane
!###############################################################################
function dcircle(d12,d13,d23,r1,r2)
! computes the coordinates in 2D plane of three vertices V1,V2,V3 making the
! assumptions that V1 is the origin (0,0), V2 is on the x axis (0,d12). The
! first part gives the coordinates x,y of V3.
! Same set of equations are used to estimate the origin of a point distant by
! r1 and r2 from V1 and V2 respectively (xc,+/-yc). The function returns the
! distance between (xc,-yc) and V0 (x,y)
real :: dcircle,d12,d13,d23,r1,r2
real :: x,y,xc,yc,a
logical :: line
! by definition the first lateration computing the V3 coordinates is stable
! if the mesh is not ill formed
x=(d12**2-d23**2+d13**2)/(2*d12)
y=sqrt(max(d13**2-x**2,0.))
if (verbose) write(*,*) 'dcircle - x,y :',x,y
! The second lateration may encounter instability when r1 and r2 are small.
! First test : is it a line source ?
line=(r1 < epsilon(r1) .and. r2 < epsilon(r2))
! if r1 OR r2 are null, no trilateration
if ((r1 < epsilon(r1) .or. r2 < epsilon(r2)).and..not.line) then
dcircle=infinity
if (verbose) write(*,*) 'dcircle - r1 or r2 are null'
return
endif
! in case of a line case, dcircle = y if x is in the gate
if (line) then
if (x < 0. .or. x > d12) then
dcircle=infinity
if (verbose) write(*,*) 'line case but x is not in the gate'
else
dcircle=y
endif
return
endif
! This second test may be useless for distance but to be safe ....
if (abs(r1-r2) < epsilon(r1) .and. r1 < d12*.5) then
dcircle=infinity
return
endif
xc=(d12**2-r2**2+r1**2)/(2*d12)
yc=sqrt(max(r1**2-xc**2,0.))
if (verbose) write(*,*) 'dcircle - xc,yc :',xc,yc
! The gate test. a is the x value of the virtual path at the abscisse axis crossing.
! It must lay between the two vertices, i.e. passing throuth the gate.
a=abs(xc-x)/(1+(yc/y))
if (x < xc) then
a=x+a
else
a=x-a
endif
if (a < 0. .or. a > d12) then
dcircle=infinity
if (verbose) write(*,*) 'dcircle - a : ',a
if (verbose) write(*,*) 'dcircle - a outside range'
return
endif
! Finally ....
dcircle=sqrt((x-xc)**2+(y+yc)**2.) ! remember ... distance to (xc,-yc)
return
end function dcircle
!###############################################################################
subroutine vicinode(amesh,anode,celllist,nodelist)
! Compute the neightbor nodes list (nodelist) of a given node (anode)
! and its given neighbor cell list (celllist).
type(mesh) :: amesh
type(listn), pointer :: celllist,nodelist
integer :: anode
type(listn), pointer :: curcell,curnode
logical :: begin
integer :: i,j
if (verbose) write(*,*) 'celllist%idnode',celllist%idnode
begin=.true.
curcell=>celllist
do while (associated(curcell))
do i=1,3
if (amesh%cell(curcell%idnode,i).ne.anode) then
if (begin) then
allocate(nodelist)
nullify(nodelist%previous)
nullify(nodelist%next)
curnode=>nodelist
curnode%idnode=amesh%cell(curcell%idnode,i)
begin=.false.
else
if (.not.isinit(amesh%cell(curcell%idnode,i),nodelist)) then
allocate(curnode%next)
curnode%next%previous=>curnode
nullify(curnode%next%next)
curnode=>curnode%next
curnode%idnode=amesh%cell(curcell%idnode,i)
endif
endif
endif
enddo
curcell=>curcell%next
enddo
end subroutine vicinode
!###############################################################################
subroutine compntoc(amesh,ntoc)
! compute a node to cell array. ntoc is a list array. it is defined by an array
! pointing at the first element of the cell list.
! warning : but fortran does not understand pointer arrays, so I use a container.
! ntoc is not tecnically a pointer array but an array of container containing a
! pointer.
! "last" is a pointer array which, for each node, points at the end of the cell list.
type(mesh) :: amesh
type(container), dimension(amesh%Nnodes) :: ntoc
type(container), dimension(amesh%Nnodes) :: last
integer :: i,j
logical :: begin
if (verbose) write(*,*) 'entering in compntoc'
! initialisation at null
do i=1,amesh%Nnodes
nullify(ntoc(i)%ptr)
nullify(last(i)%ptr)
enddo
! loop on the cell number
do i=1,amesh%Ncells
! for each cell, loop on the nodes forming the cell
do j=1,3
! if the cell list associated to the point amesh%cell(i,j) is not started, do it
! and point "last" to it
if (.not.associated(ntoc(amesh%cell(i,j))%ptr)) then
allocate(ntoc(amesh%cell(i,j))%ptr)
nullify(ntoc(amesh%cell(i,j))%ptr%next)
nullify(ntoc(amesh%cell(i,j))%ptr%previous)
last(amesh%cell(i,j))%ptr=>ntoc(amesh%cell(i,j))%ptr
else
! else add an element to the list after the last element of the list
allocate(last(amesh%cell(i,j))%ptr%next)
nullify(last(amesh%cell(i,j))%ptr%next%next)
last(amesh%cell(i,j))%ptr%next%previous=>last(amesh%cell(i,j))%ptr
last(amesh%cell(i,j))%ptr=>last(amesh%cell(i,j))%ptr%next
endif
! add the cell i to the cell list for the node amesh%cell(i,j)
last(amesh%cell(i,j))%ptr%idnode=i
! if (verbose) write(*,*) 'last(amesh%cell(i,j))%ptr%idnode',last(amesh%cell(i,j))%ptr%idnode
enddo
enddo
if (verbose) write(*,*) 'check on cell list on node :',amesh%Nnodes/2
if (verbose) call printlist(ntoc(amesh%Nnodes/2)%ptr)
if (verbose) write(*,*) 'ntoc(500)%ptr%idnode',ntoc(500)%ptr%idnode
end subroutine compntoc
!###############################################################################
subroutine deallocntoc(ntoc,n)
integer :: n
type(container), dimension(n) :: ntoc
type(listn), pointer :: p1,p2
integer :: i
do i=1,n
p2=>ntoc(i)%ptr
if (.not.associated(p2)) then
write(*,*) "warning... node #",i," is cell orphan"
cycle
endif
do while (associated(p2%next))
p1=>p2
p2=>p2%next
deallocate(p1)
enddo
enddo
end subroutine deallocntoc
!###############################################################################
subroutine dumpmeshvtk(dev,amesh)
type(mesh) :: amesh
integer :: dev
integer :: i,j
write(dev,'(a26)') '# vtk DataFile Version 2.0'
write(dev,'(a8)') 'distance'
write(dev,'(a5)') 'ASCII'
write(dev,'(a16)') 'DATASET POLYDATA'
write(dev,'(a7,i10,a6)') 'POINTS ',amesh%Nnodes,' float'
do i=1,amesh%Nnodes
write(dev,*) amesh%px(i),amesh%py(i),amesh%pz(i)
enddo
write(dev,'(a9,2i10)') 'POLYGONS ',amesh%Ncells,amesh%Ncells*4
do i=1,amesh%Ncells
write(dev,'(a2,$)') '3 '
write(dev,*) (amesh%cell(i,j)-1,j=1,3)
enddo
return
end subroutine dumpmeshvtk
!###############################################################################
subroutine dumpnodeattributevtk(dev,amesh,field,attname,init)
type(mesh) :: amesh
real, dimension(amesh%Nnodes) :: field
integer :: dev
character*(*) :: attname
logical :: init
integer :: i
if (init) write(dev,'(a11,i5)') 'POINT_DATA ',amesh%Nnodes
write(dev,'(a8,a,a8)') 'SCALARS ',trim(attname),' float 1'
write(dev,'(a20)') 'LOOKUP_TABLE default'
do i=1,amesh%Nnodes
write(dev,*) field(i)
enddo
return
end subroutine dumpnodeattributevtk
!###############################################################################
subroutine dumpcellattributevtk(dev,amesh,field,attname,init)
type(mesh) :: amesh
real, dimension(amesh%Ncells) :: field
integer :: dev
character*(*) :: attname
logical :: init
integer :: i
if (init) write(dev,'(a10,i5)') 'CELL_DATA ',amesh%Ncells
write(dev,'(a8,a,a8)') 'SCALARS ',trim(attname),' float 1'
write(dev,'(a20)') 'LOOKUP_TABLE default'
do i=1,amesh%Ncells
write(dev,*) field(i)
enddo
return
end subroutine dumpcellattributevtk
!###############################################################################
end module lateration