-
Notifications
You must be signed in to change notification settings - Fork 3
/
chmatmod.f90
3026 lines (2847 loc) · 80.2 KB
/
chmatmod.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
! Copyright 2021, Christian Hafner
!
! This file is part of OpenMaXwell.
!
! OpenMaXwell is free software: you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
! OpenMaXwell is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! GNU General Public License for more details.
! You should have received a copy of the GNU General Public License
! along with OpenMaXwell. If not, see <http://www.gnu.org/licenses/>.
MODULE CHMAT
! mathematics and auxiliary things
USE CHDLG
Interface Operator (.inv.)
Module Procedure Protected_Inv
Module Procedure Protected_CInv
end Interface
Interface Operator (.int.)
Module Procedure Protected_Int
Module Procedure Protected_CInt
end Interface
Interface Operator (.sqr.)
Module Procedure Protected_Sqr
Module Procedure Protected_CSqr
end Interface
Interface Operator (.srt.)
Module Procedure Protected_Srt
Module Procedure Protected_CSrt
end Interface
Interface Operator (.log.)
Module Procedure Protected_Log
Module Procedure Protected_CLog
end Interface
Interface Operator (.exp.)
Module Procedure Protected_Exp
Module Procedure Protected_CExp
end Interface
Interface Operator (.cos.)
Module Procedure Protected_Cos
Module Procedure Protected_CCos
end Interface
Interface Operator (.sin.)
Module Procedure Protected_Sin
Module Procedure Protected_CSin
end Interface
Interface Operator (.add.)
Module Procedure Protected_Add
Module Procedure Protected_CAdd
end Interface
Interface Operator (.sub.)
Module Procedure Protected_Sub
Module Procedure Protected_CSub
end Interface
Interface Operator (.mul.)
Module Procedure Protected_Mul
Module Procedure Protected_CMul
end Interface
Interface Operator (.div.)
Module Procedure Protected_Div
Module Procedure Protected_CDiv
end Interface
Interface Operator (.pow.)
Module Procedure Protected_Pow
Module Procedure Protected_CPow
end Interface
Interface Operator (.ata.)
Module Procedure Protected_Ata
end Interface
SAVE
Real(8), Parameter :: Pi=3.1415926535898d0,Eps0=8.85418781762038985d-12,Mue0=1.25663706143591730d-6,Qe=1.60217656535d-19
Integer(4) nzeile
! Formula
!DEC$ IF DEFINED (DEMO)
Integer(4), Parameter :: mForA=10
!DEC$ ELSE
Integer(4), Parameter :: mForA=50
!DEC$ ENDIF
CONTAINS
! protected operations and functions
Function Protected_Inv(a1)
Real(8), Intent(In) :: a1
Real(8) :: Protected_Inv
if(dabs(a1).lt.pSmall) then
Protected_Inv=dsign(pBig,a1)
else
Protected_Inv=1.0d0/a1
end if
end Function Protected_Inv
Function Protected_Int(a1)
Real(8), Intent(In) :: a1
Real(8) :: Protected_Int
if(dabs(a1).gt.2.0d9) then
Protected_Int=dsign(2.0d9,a1)
else
Protected_Int=dble(nint(a1,4))
end if
end Function Protected_Int
Function Protected_Sqr(a1)
Real(8), Intent(In) :: a1
Real(8) :: Protected_Sqr
if(dabs(a1).gt.1.0d150) then
Protected_Sqr=pBig
else
Protected_Sqr=a1*a1
end if
end Function Protected_Sqr
Function Protected_Srt(a1)
Real(8), Intent(In) :: a1
Real(8) :: Protected_Srt
if(a1.gt.pSmall) then
Protected_Srt=dsqrt(a1)
else
Protected_Srt=0.0d0
end if
end Function Protected_Srt
Function Protected_Log(a1)
Real(8), Intent(In) :: a1
Real(8) :: Protected_Log
if(a1.gt.pSmall) then
Protected_Log=dlog(a1)
else
Protected_Log=dlog(pSmall)
end if
end Function Protected_Log
Function Protected_Exp(a1)
Real(8), Intent(In) :: a1
Real(8) :: Protected_Exp
if(a1.lt.700.0d0) then
if(a1.gt.-700.0d0) then
Protected_Exp=dexp(a1)
else
Protected_Exp=dexp(-700.0d0)
end if
else
Protected_Exp=dexp(700.0d0)
end if
end Function Protected_Exp
Function Protected_Cos(a1)
Real(8), Intent(In) :: a1
Real(8) :: Protected_Cos
Real(8) dum,p2
p2=2.0_8*Pi
dum=dmod(dabs(a1),p2)
Protected_Cos=dcos(dum)
end Function Protected_Cos
Function Protected_Sin(a1)
Real(8), Intent(In) :: a1
Real(8) :: Protected_Sin
Real(8) dum,p2
p2=2.0_8*Pi
dum=dmod(dabs(a1),p2)
if(a1.lt.0.0d0) then
Protected_Sin=-dsin(dum)
else
Protected_Sin=dsin(dum)
end if
end Function Protected_Sin
Function Protected_Add(a1,a2)
Real(8), Intent(In) :: a1,a2
Real(8) :: Protected_Add
Real(8) dum
if((dabs(a1).gt.1.0d299).and.(dabs(a2).gt.1.0d299)) then
dum=(a1*1.0d-150)+(a2*1.0d-150)
if(dabs(dum).gt.1.0d150) then
if(dum.gt.0.0d0) then
Protected_Add=pBig
else
Protected_Add=nBig
end if
else
Protected_Add=dum*1.0d150
end if
else
Protected_Add=a1+a2
end if
end Function Protected_Add
Function Protected_Sub(a1,a2)
Real(8), Intent(In) :: a1,a2
Real(8) :: Protected_Sub
Real(8) dum
dum=-a2
Protected_Sub=Protected_Add(a1,dum)
end Function Protected_Sub
Function Protected_Mul(a1,a2)
Real(8), Intent(In) :: a1,a2
Real(8) :: Protected_Mul
Real(8) aa1,aa2,dum
aa1=dabs(a1)
aa2=dabs(a2)
if((aa1.lt.1.0d150).and.(aa2.lt.1.0d150)) then
Protected_Mul=a1*a2
else if(aa1.gt.aa2) then
dum=(a1*pSmall)*a2
if(dabs(dum).lt.1.0d0) then
Protected_Mul=pBig*dum
else
Protected_Mul=dsign(pBig,dum)
end if
else
dum=(a2*pSmall)*a1
if(dabs(dum).lt.1.0d0) then
Protected_Mul=pBig*dum
else
Protected_Mul=dsign(pBig,dum)
end if
end if
end Function Protected_Mul
Function Protected_Div(a1,a2)
Real(8), Intent(In) :: a1,a2
Real(8) :: Protected_Div
Real(8) dum
if(dabs(a1).lt.pSmall) then
if(dabs(a2).lt.pSmall) then
Protected_Div=dsign(1.0d0,a1)*dsign(1.0d0,a2)
else
dum=1.0d0/a2
Protected_Div=Protected_Mul(a1,dum)
end if
else
dum=Protected_Inv(a2)
Protected_Div=Protected_Mul(a1,dum)
end if
end Function Protected_Div
Function Protected_Pow(a1,a2)
Real(8), Intent(In) :: a1,a2
Real(8) :: Protected_Pow
Real(8) dum
dum=Protected_Mul(Protected_Log(a1),a2)
Protected_Pow=Protected_Exp(dum)
end Function Protected_Pow
Function Protected_Ata(a1,a2)
Real(8), Intent(In) :: a1,a2
Real(8) :: Protected_Ata
if((dabs(a1).lt.pSmall).and.(dabs(a2).lt.pSmall)) then
Protected_Ata=0.0d0
else
Protected_Ata=datan2(a1,a2)
end if
end Function Protected_Ata
Function Protected_CInv(a1)
Complex(8), Intent(In) :: a1
Complex(8) :: Protected_CInv
if(cdabs(a1).lt.pSmall) then
Protected_CInv=DCmplx(pBig,pBig)
else
Protected_CInv=1.0d0/a1
end if
end Function Protected_CInv
Function Protected_CInt(a1)
Complex(8), Intent(In) :: a1
Complex(8) :: Protected_CInt
if(cdabs(a1).gt.2.0d9) then
Protected_CInt=DCmplx(2.0d9,2.0d9)
else
Protected_CInt=DCmplx(nint(Dble(a1),4),nint(DImag(a1),4))
end if
end Function Protected_CInt
Function Protected_CSqr(a1)
Complex(8), Intent(In) :: a1
Complex(8) :: Protected_CSqr
if(cdabs(a1).gt.1.0d150) then
Protected_CSqr=pBig
else
Protected_CSqr=a1*a1
end if
end Function Protected_CSqr
Function Protected_CSrt(a1)
Complex(8), Intent(In) :: a1
Complex(8) :: Protected_CSrt
if(cdabs(a1).gt.pSmall) then
Protected_CSrt=cdsqrt(a1)
else
Protected_CSrt=DCmplx(0.0d0,0.0d0)
end if
end Function Protected_CSrt
Function Protected_CLog(a1)
Complex(8), Intent(In) :: a1
Complex(8) :: Protected_CLog
if(cdabs(a1).gt.pSmall) then
Protected_CLog=cdlog(a1)
else
Protected_CLog=cdlog(dCmplx(pSmall,0.0))
end if
end Function Protected_CLog
Function Protected_CExp(a1)
Complex(8), Intent(In) :: a1
Complex(8) :: Protected_CExp
if(cdabs(a1).lt.700.0d0) then
if(cdabs(a1).gt.-700.0d0) then
Protected_CExp=cdexp(a1)
else
Protected_CExp=DCmplx(0.0d0,0.0d0)
end if
else
Protected_CExp=DCmplx(dexp(700.0d0),0.0d0)
end if
end Function Protected_CExp
Function Protected_CCos(a1)
Complex(8), Intent(In) :: a1
Complex(8) :: Protected_CCos
Protected_CCos=cdcos(a1)
end Function Protected_CCos
Function Protected_CSin(a1)
Complex(8), Intent(In) :: a1
Complex(8) :: Protected_CSin
Protected_CSin=cdsin(a1)
end Function Protected_CSin
Function Protected_CAdd(a1,a2)
Complex(8), Intent(In) :: a1,a2
Complex(8) :: Protected_CAdd
Complex(8) dum
if((cdabs(a1).gt.1.0d299).and.(cdabs(a2).gt.1.0d299)) then
dum=(a1*1.0d-150)+(a2*1.0d-150)
if(cdabs(dum).gt.1.0d150) then
Protected_CAdd=DCmplx(pBig,0.0d0)
else
Protected_CAdd=dum*1.0d150
end if
else
Protected_CAdd=a1+a2
end if
end Function Protected_CAdd
Function Protected_CSub(a1,a2)
Complex(8), Intent(In) :: a1,a2
Complex(8) :: Protected_CSub
Complex(8) dum
dum=-a2
Protected_CSub=Protected_CAdd(a1,dum)
end Function Protected_CSub
Function Protected_CMul(a1,a2)
Complex(8), Intent(In) :: a1,a2
Complex(8) :: Protected_CMul
Complex(8) dum
Real(8) aa1,aa2
aa1=cdabs(a1)
aa2=cdabs(a2)
if((aa1.lt.1.0d150).and.(aa2.lt.1.0d150)) then
Protected_CMul=a1*a2
else if(aa1.gt.aa2) then
dum=(a1*pSmall)*a2
if(cdabs(dum).lt.1.0d0) then
Protected_CMul=pBig*dum
else
Protected_CMul=DCmplx(pBig,0.0d0)
end if
else
dum=(a2*pSmall)*a1
if(cdabs(dum).lt.1.0d0) then
Protected_CMul=pBig*dum
else
Protected_CMul=DCmplx(pBig,0.0d0)
end if
end if
end Function Protected_CMul
Function Protected_CDiv(a1,a2)
Complex(8), Intent(In) :: a1,a2
Complex(8) :: Protected_CDiv
Complex(8) dum
if(cdabs(a1).lt.pSmall) then
if(cdabs(a2).lt.pSmall) then
Protected_CDiv=DCmplx(1.0d0,0.0d0)
else
dum=1.0d0/a2
Protected_CDiv=Protected_CMul(a1,dum)
end if
else
dum=Protected_CInv(a2)
Protected_CDiv=Protected_CMul(a1,dum)
end if
end Function Protected_CDiv
Function Protected_CPow(a1,a2)
Complex(8), Intent(In) :: a1,a2
Complex(8) :: Protected_CPow
Complex(8) dum
dum=Protected_CMul(Protected_CLog(a1),a2)
Protected_CPow=Protected_CExp(dum)
end Function Protected_CPow
! random numbers
Function CHRnd(a1,a2,iseed)
Real(8), Intent(In) :: a1,a2
Real(8) :: CHRnd
Real(4), Save :: dum
Integer(4), Optional :: iseed
if(Present(iseed)) then
if(iseed.gt.0) then
call random_seed(iseed)
else
call random_seed()
end if
end if
call random_number(dum)
CHRnd=a1+dble(dum)*(a2-a1)
end Function CHRnd
! 2D geometric objects
Subroutine Cart2Pol(x,y,r,f)
Real(8) x,y,r,f,v(2)
v(1)=x
v(2)=y
r=r2Vec_Length(v)
f=Protected_Ata(y,x)
end Subroutine Cart2Pol
Subroutine Pol2Cart(r,f,x,y)
Real(8) x,y,r,f
x=r*dcos(f)
y=r*dsin(f)
end Subroutine Pol2Cart
Subroutine DistPtPt(xA,yA,xB,yB,d)
Real(8) xA,yA,xB,yB,d,v(2)
v(1)=xB-xA
v(2)=yB-yA
d=r2Vec_Length(v)
end Subroutine DistPtPt
Real(8) Function DPtPt(xA,yA,xB,yB)
Real(8) xA,yA,xB,yB,v(2)
v(1)=xB-xA
v(2)=yB-yA
dPtPt=r2Vec_Length(v)
end Function DPtPt
Subroutine DistPtCircle(xP,yP,xO,yO,r,d,xN,yN,sN,lRSide)
Real(8) xP,yP,xO,yO,xN,yN,sN,r,rP,fP,d,v(2)
Logical lRSide
v(1)=xP-xO
v(2)=yP-yO
call Cart2Pol(v(1),v(2),rP,fP)
if(fP.lt.0.0d0) fP=fP+2.0d0*Pi
d=rP-r
xN=xO+r*dcos(fP)
yN=yO+r*dsin(fP)
sN=r*fP
lRSide=.true.
if(d.lt.0.0d0) then
lRSide=.false.
d=-d
end if
end Subroutine DistPtCircle
Subroutine Dist3DPtFiniteLine(P,A,B,Pn,aA,d)
! get distance d of point P from the nearest point Pn on the finite line from A to B and the distance aA of A from Pn
Real(8) P(3),A(3),B(3),Pn(3),e(3),AB,aA,d
Pn=B-A ! abuse Pn as B-A
AB=r3Vec_Length(Pn)
if(AB.lt.pSmall) then ! safety if zero length
e(1)=1.0d0
e(2)=0.0d0
e(3)=0.0d0
else
e=PN/AB
end if
Pn=P-A
aA=dot_product(Pn,e)
if(aA.lt.0.0d0) then
Pn=A
else if(aA.gt.AB) then
Pn=B
else
Pn=A+aA*e
end if
d=r3Vec_Length(P-Pn)
end Subroutine Dist3DPtFiniteLine
Subroutine Dist3DPtInfiniteLine(P,O,e,Pn,aO,d)
! get distance d of point P from the nearest point Pn on the infinite line with origin O and unit direction e
Real(8) P(3),O(3),Pn(3),e(3),aO,d
Pn=P-O
aO=dot_product(Pn,e)
Pn=O+aO*e
d=r3Vec_Length(P-Pn)
end Subroutine Dist3DPtInfiniteLine
Subroutine DistPtLine3D(P,z0,dz,Pn,dsPn,d)
! get distance d of point P from the nearest point Pn on the 3D line on the z axis from z0 to z0+dz
Real(8) P(3),Pn(3),z0,dz,d
Pn(1:2)=0.0d0
if(P(3).lt.z0) then
Pn(3)=z0
else if(P(3).gt.z0+dz) then
Pn(3)=z0+dz
else
Pn(3)=P(3)
end if
dsPn=Pn(3)
d=r3Vec_Length(P-Pn)
end Subroutine DistPtLine3D
Subroutine DistPtRing3D(P,spac,r,f0,da,Pn,dsPn,d)
! get distance d of point P from the nearest point Pn on the 3D ring with coordinate system "spac"
Real(8) P(3),spac(3,0:3),r,f0,da,Ps(3),Pn(3),dsPn,d
call vGlob2Loc(P,spac,Ps)
call DistPtRing(Ps,r,f0,da,Pn,dsPn,d)
Ps=Pn
call vGlob2Loc(Ps,spac,Pn)
end Subroutine DistPtRing3D
Subroutine DistPtRing(P,r,f0,da,Pn,dsPn,d)
! get distance d of point P from the nearest point Pn on the 3D ring around the z axis in the xy plane
! f0 in radians
Real(8) P(3),Pn(3),r,f0,da,dsPn,d
Integer(2) iOrient
Logical lRSide
Pn(3)=0.0d0
dsPn=0.0d0
if(dabs(da).lt.1.0d-100) then ! point in xy plane
Pn(1)=r*dcos(f0)
Pn(2)=r*dsin(f0)
else if(dabs(r).lt.1.0d-100) then ! point in origin
Pn(1:2)=0.0d0
else
iOrient=1_2
if(da.lt.0.0d0) iOrient=-1_2
call DistPtArc(P(1),P(2),0.0d0,0.0d0,r*dcos(f0),r*dsin(f0),da,2.0d300,iOrient,d,Pn(1),Pn(2),dsPn,lRSide)
end if
d=r3Vec_Length(P-Pn)
end Subroutine DistPtRing
Subroutine DistPtSpiral3D(P,spac,x0,y0,dr,da,dz,dmx,Pn,d)
! get distance d of point P from the nearest point Pn on the 3D spiral with coordinate system "spac"
Real(8) P(3),spac(3,0:3),x0,y0,dr,da,dz,dmx,d,Ps(3),Pn(3)
call vGlob2Loc(P,spac,Ps)
call DistPtSpiral(Ps,x0,y0,dr,da,dz,dmx,Pn,d)
Ps=Pn
call vGlob2Loc(Ps,spac,Pn)
end Subroutine DistPtSpiral3D
Subroutine DistPtSpiral(P,x0,y0,dr,da,dz,dmx,Pn,d)
! get distance d of point P from the nearest point Pn on the 3D spiral around the z axis, starting in the xy plane
! not very accurate, if d>dmx: rough estimate
! angles: radians!
Real(8) P(3),Pn(3),x0,y0,z0,dr,da,dz,dsPn,d,rP,fP,r0,f0,zO,rN,zN,sN,A(3),B(3),f1,f2,da12,dr12,dz12,r,f,z,Pl(3),x, &
& y1,y2,y3,al,sN2,d1,x1,x2,x3,P1(3),P2(3),P3(3),ri,fi,zi,ri1,fi1,zi1,Pi2,dmx
Integer(4) i,n,n2,il
Logical lRSide,lZero
if(dabs(da).lt.1.0d-100) then ! straight line in 3D space
A(1)=x0
A(2)=y0
A(3)=0.0d0
fP=datan2(y0,x0)
r0=dsqrt(x0**2+y0**2)+dr
B(1)=r0*dcos(fP)
B(2)=r0*dsin(fP)
B(3)=dz
call Dist3DPtFiniteLine(P,A,B,Pn,dsPn,d)
return
end if
if(dabs(dz).lt.1.0d-100) then ! in xy plane
Pn(3)=0.0d0
if(dabs(dr).lt.1.0d-100) then ! simple arc in xy plane
r0=dsqrt(x0**2+y0**2)
fP=datan2(y0,x0)
call DistPtRing(P,r0,fP,da,Pn,dsPn,d)
return
end if
end if
r0=sqrt(x0**2+y0**2) ! spiral start radius in xy plane
f0=Protected_Ata(y0,x0) ! spiral start angle
z0=0.0d0
rP=sqrt(P(2)**2+P(1)**2)
fP=0.0d0
if(rP.gt.1.0d-300) fP=datan2(P(2),P(1))
if(dabs(dr).lt.1.0d-100) then ! cone is cylinder (dz=0 already tested)
zO=-1.0d300
call DistPtLine(rP,P(3),r0,0.0d0,r0,dz,0.0d0,d,rN,zN,sN2,lRSide,lZero,al,sN) ! find point on cylinder line in the section plane spanned by P and z axis
else ! real cone
zO=-r0*dz/dr
call DistPtLine(rP,P(3),r0,0.0d0,r0+dr,dz,0.0d0,d,rN,zN,sN2,lRSide,lZero,al,sN) ! find point on cone line in the section plane spanned by P and z axis
end if
if(d.gt.dmx) then ! rough estimate
Pn(1)=rN*dcos(fP)
Pn(2)=rN*dsin(fP)
Pn(3)=zN
end if
if(dabs(da).lt.2.0d0*Pi) then ! short spiral
n=max(10_4,Int4(40.0d0*Pi/dabs(da)))
dr12=dr/Dble(n)
da12=da/Dble(n)
dz12=dz/Dble(n)
call NextMinDistPtSpiral(P,r0,f0,z0,0.5d0*dr,0.5d0*da,0.5d0*dz,n,Pn,d,i,ri,fi,zi) ! upwards
call NextMinDistPtSpiral(P,r0+dr,f0+da,z0+dz,-0.5d0*dr,-0.5d0*da,-0.5d0*dz,n,Pl,d1,i1,ri1,fi1,zi1) ! downwards
if(d1.lt.d) then ! use smaller distance
d=d1
Pn=Pl
ri=ri1
fi=fi1
zi=zi1
end if
else ! long spiral: find first nearest point on circular cone that contains the spiral
! define start angle f1 and end angle f2 for search
Pi2=Pi*2.0d0
if(da.lt.0.0d0) Pi2=-Pi2
if(sN.le.0.0d0) then ! "below": search first interval 0...2Pi
N=60
da12=Pi2/Dble(n)
dr12=dr*da12/da
dz12=dz*da12/da
call NextMinDistPtSpiral(P,r0,f0,z0,dr*Pi2/da,Pi2,dz*Pi2/da,n,Pn,d,i,ri,fi,zi)
else if(sN.ge.al) then ! "above"
N=60
da12=2.0d0*Pi/Dble(n)
dr12=dr*da12/da
dz12=dz*da12/da
call NextMinDistPtSpiral(P,r0+dr,f0+da,z0+dz,-dr*Pi2/da,-Pi2,-dz*Pi2/da,n,Pn,d,i,ri,fi,zi)
else ! "in between"
if(dabs(dz).gt.dabs(dr)) then
fPs=zN*da/dz ! angle of nearest point Pn from z value
else
fPs=(rN-r0)*da/dr ! angle of nearest point Pn from r value
end if
sN=dsqrt((Pi2*dr/da)**2+(Pi2*dz/da)**2)
i=Int4(fPs/(2.0d0*Pi))
d1=1.0d300
fPs=fP+Dble(i)*2.0d0*Pi
do il=-5,5
f=fP+Dble(2*i+il)*Pi
if(((da.gt.0.0d0).and.(f.lt.f0)).or.((da.lt.0.0d0).and.(f.gt.f0))) Cycle
if(((da.gt.0.0d0).and.(f.gt.f0+da)).or.((da.lt.0.0d0).and.(f.lt.f0+da))) Cycle
r=r0+dr*(f-f0)/da
z=dz*(f-f0)/da
Pl(1)=r*dcos(f)
Pl(2)=r*dsin(f)
Pl(3)=z
d=r3Vec_Length(P-Pl)
if(d.lt.d1) then
d1=d
fPs=f
Pn=Pl
end if
end do
! check end points
Pl(1)=r0*dcos(f0)
Pl(2)=r0*dsin(f0)
Pl(3)=0.0d0
d=r3Vec_Length(P-Pl)
if(d.lt.d1) then
d1=d
fPs=f0
Pn=Pl
end if
Pl(1)=(r0+dr)*dcos(f0+da)
Pl(2)=(r0+dr)*dsin(f0+da)
Pl(3)=dz
d=r3Vec_Length(P-Pl)
if(d.lt.d1) then
d1=d
fPs=f0+da
Pn=Pl
end if
n=60
n2=n
d=d1
if(dabs(d/sN).lt.1.0d0) then ! point P is close: search in angular distance sN depending on d and start radius
sN=d/(5.0d0*dsqrt(Pn(1)**2+Pn(2)**2))
else ! not close, search a bit more than +-2Pi
sN=2.2d0*Pi/Dble(n)
end if
f1=fPs+dble(n)*sN ! search intervals
f2=fPs-dble(n2)*sN
sN2=sN
if(da.gt.0.0d0) then ! reduce n if search intervals too big and adapt sN,sN2
if(f1.gt.f0+da) then
n=max(0_4,Int4((f0+da-fPs)/sN))
if(n.gt.0) sN=(f0+da-fPs)/Dble(n)
end if
if(f2.lt.f0) then
n2=max(0_4,Int4((fPs-f0)/sN2))
if(n2.gt.0) sN2=(fPs-f0)/Dble(n2)
end if
else
if(f1.gt.f0) then
n=max(0_4,Int4((f0-fPs)/sN))
if(n.gt.0) sN=(f0-fPs)/Dble(n)
end if
if(f2.lt.f0+da) then
n2=max(0_4,Int4((fPs-f0-da)/sN2))
if(n2.gt.0) sN2=(fPs-f0-da)/Dble(n2)
end if
end if
f=fPs ! start search starting from f, r, z
r=r0+dr*(fPs-f0)/da
z=dz*(fPs-f0)/da
da12=sN
dr12=dr*da12/da
dz12=dz*da12/da
call NextMinDistPtSpiral(P,r,f,z,dr12*Dble(n),da12*Dble(n),dz12*Dble(n),n,Pn,d,i,ri,fi,zi) ! upwards
da12=sN2
dr12=dr*da12/da
dz12=dz*da12/da
call NextMinDistPtSpiral(P,r,f,z,-dr12*Dble(n2),-da12*Dble(n2),-dz12*Dble(n2),-n2,Pl,d1,i1,ri1,fi1,zi1) ! downwards
if(d1.lt.d) then ! use smaller distance
d=d1
Pn=Pl
ri=ri1
fi=fi1
zi=zi1
end if
end if
end if
! refine search using parabolic interpolation
da12=min(dabs(0.5d0*da12),dabs(1.999999d0*da))
x1=fi-da12
x3=fi+da12
if(da.gt.0.0d0) then
if(x1.lt.f0) then
x1=f0
else if(x3.gt.f0+da) then
x3=f0+da
end if
else
if(x3.gt.f0) then
x3=f0
else if(x1.lt.f0+da) then
x1=f0+da
end if
end if
da12=0.5d0*dabs(x3-x1)
x2=0.5d0*(x1+x3)
f=x1
r=ri+dr*(f-fi)/da
z=zi+dz*(f-fi)/da
P1(1)=r*dcos(f)
P1(2)=r*dsin(f)
P1(3)=z
y1=r3Vec_Length(P-P1)
f=x2
r=ri+dr*(f-fi)/da
z=zi+dz*(f-fi)/da
P2(1)=r*dcos(f)
P2(2)=r*dsin(f)
P2(3)=z
y2=r3Vec_Length(P-P2)
f=x3
r=ri+dr*(f-fi)/da
z=zi+dz*(f-fi)/da
P3(1)=r*dcos(f)
P3(2)=r*dsin(f)
P3(3)=z
y3=r3Vec_Length(P-P3)
x=ParabolaOpt(x1,x2,x3,y1,y2,y3) ! parabolic interpolation
if(((da.gt.0.0d0).and.(x.ge.f0).and.(x.le.f0+da)).or.((da.lt.0.0d0).and.(x.le.f0).and.(x.ge.f0+da))) then ! f is insinde
r=ri+dr*(x-fi)/da
z=zi+dz*(x-fi)/da
Pl(1)=r*dcos(x)
Pl(2)=r*dsin(x)
Pl(3)=z
d1=r3Vec_Length(P-Pl)
else
d1=2.0d300
end if
if((d1.lt.d).and.(d1.lt.y1).and.(d1.lt.y2).and.(d1.lt.y3)) then ! take shortest distance out of d,d1,y1,y2,y3
Pn=Pl
d=d1
else if((y1.lt.d).and.(y1.lt.d1).and.(y1.lt.y2).and.(y1.lt.y3)) then
Pn=P1
d=y1
x=x1
else if((y2.lt.d).and.(y2.lt.d1).and.(y2.lt.y1).and.(y2.lt.y3)) then
Pn=P2
d=y2
x=x2
else if((y3.lt.d).and.(y3.lt.d1).and.(y3.lt.y1).and.(y3.lt.y2)) then
Pn=P3
d=y3
x=x3
else
x=fi
end if
if((dabs((y1-y2).div.y2).gt.1.0d-3).or.(dabs((y3-y2).div.y2).gt.1.0d-3)) then ! not flat: try second refinement
fi=x
da12=min(2.0d0*dabs(d/ri),0.5d0*dabs(da12))
x1=fi-dabs(da12)
x3=fi+dabs(da12)
if(da.gt.0.0d0) then
if(x1.lt.f0) then
x1=f0
else if(x3.gt.f0+da) then
x3=f0+da
end if
else
if(x3.gt.f0) then
x3=f0
else if(x1.lt.f0+da) then
x1=f0+da
end if
end if
da12=0.5d0*dabs(x3-x1)
x2=0.5d0*(x1+x3)
f=x1
r=ri+dr*(f-fi)/da
z=zi+dz*(f-fi)/da
P1(1)=r*dcos(f)
P1(2)=r*dsin(f)
P1(3)=z
y1=r3Vec_Length(P-P1)
f=x2
r=ri+dr*(f-fi)/da
z=zi+dz*(f-fi)/da
P2(1)=r*dcos(f)
P2(2)=r*dsin(f)
P2(3)=z
y2=r3Vec_Length(P-P2)
f=x3
r=ri+dr*(f-fi)/da
z=zi+dz*(f-fi)/da
P3(1)=r*dcos(f)
P3(2)=r*dsin(f)
P3(3)=z
y3=r3Vec_Length(P-P3)
x=ParabolaOpt(x1,x2,x3,y1,y2,y3) ! parabolic interpolation
if(((da.gt.0.0d0).and.(x.ge.f0).and.(x.le.f0+da)).or.((da.lt.0.0d0).and.(x.le.f0).and.(x.ge.f0+da))) then ! f is insinde
r=ri+dr*(x-fi)/da
z=zi+dz*(x-fi)/da
Pl(1)=r*dcos(x)
Pl(2)=r*dsin(x)
Pl(3)=z
d1=r3Vec_Length(P-Pl)
else
d1=2.0d300
end if
if((d1.lt.d).and.(d1.lt.y1).and.(d1.lt.y2).and.(d1.lt.y3)) then ! take shortest distance out of d,d1,y1,y2,y3
Pn=Pl
d=d1
else if((y1.lt.d).and.(y1.lt.d1).and.(y1.lt.y2).and.(y1.lt.y3)) then
Pn=P1
d=y1
x=x1
else if((y2.lt.d).and.(y2.lt.d1).and.(y2.lt.y1).and.(y2.lt.y3)) then
Pn=P2
d=y2
x=x2
else if((y3.lt.d).and.(y3.lt.d1).and.(y3.lt.y1).and.(y3.lt.y2)) then
Pn=P3
d=y3
x=x3
else
x=fi
end if
end if
end Subroutine DistPtSpiral
Subroutine NextMinDistPtSpiral(P,r0,a0,z0,dr,da,dz,ni,Pn,dmin,imin,ri,fi,zi)
! search first local minimum distance dmin of Point from spiral
! if ni<0: do not start upwards
! angles a0,da in radians!
Real(8) P(3),r0,a0,z0,dr,da,dz,Pn(3),Pl(3),P1(3),dmin,dmax,d,r,a,z,drN,daN,dzN,ri,fi,zi,d1,r1,a1,z1
Integer(4) ni,n,i,imax,imin
n=abs(ni)
drN=dr/Dble(n)
daN=da/Dble(n)
dzN=dz/Dble(n)
r1=r0
a1=a0
z1=z0
P1(1)=r1*dcos(a1)
P1(2)=r1*dsin(a1)
P1(3)=z1
d1=r3Vec_Length(P-P1)
dmin=d1
imin=0
Pn=P1
ri=r1
fi=a1
zi=z1
r=r1+drN
a=a1+daN
z=z1+dzn
Pl(1)=r*dcos(a)
Pl(2)=r*dsin(a)
Pl(3)=z
d=r3Vec_Length(P-Pl)
if(d.gt.dmin) then ! start upwards (find maximum before oing down to minmum
if(ni.lt.0) return
dmax=d
imax=1
do i=2,n
r=r+drN
a=a+daN
z=z+dzn
Pl(1)=r*dcos(a)
Pl(2)=r*dsin(a)
Pl(3)=z
d=r3Vec_Length(P-Pl)
if(d.gt.dmax) then
dmax=d
imax=i
else
exit
end if
end do
dmin=dmax
else ! start downwards
dmin=d
imin=1
Pn=Pl
imax=1
ri=r
fi=a
zi=z
end if
do i=imax+1,n
r=r+drN
a=a+daN
z=z+dzn
Pl(1)=r*dcos(a)
Pl(2)=r*dsin(a)
Pl(3)=z
d=r3Vec_Length(P-Pl)
if(d.lt.dmin) then
dmin=d
imin=i
Pn=Pl
ri=r
fi=a
zi=z
else
exit
end if
end do
if(d1.lt.dmin) then
dmin=d1
imin=0
Pn=P1
ri=r1
fi=a1
zi=z1
end if
end Subroutine NextMinDistPtSpiral
Real(8) Function ParabolaOpt01(x2s,y1,y2,y3)
Real(8) y1,y2,y3,x2s,xo,b,c
c=((y3-y1)*x2s+y1-y2)/(x2s*(1.0d0-x2s))
b=y3-y1-c
xo=-0.5d0*b/c
ParabolaOpt01=xo
end Function ParabolaOpt01
Real(8) Function ParabolaOpt(x1,x2,x3,y1,y2,y3)
Real(8) x1,x2,x3,y1,y2,y3,x2s,xo
x2s=(x2-x1)/(x3-x1)
xo=ParabolaOpt01(x2s,y1,y2,y3)
ParabolaOpt=x1+xo*(x3-x1)
end Function ParabolaOpt