-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkernelND.f90
4608 lines (4369 loc) · 147 KB
/
kernelND.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
!------------------------------------------------------------------------------!
! NDSPMHD: A Smoothed Particle (Magneto)Hydrodynamics code for (astrophysical) !
! fluid dynamics simulations in 1, 2 and 3 spatial dimensions. !
! !
! (c) 2002-2015 Daniel Price !
! !
! http://users.monash.edu.au/~dprice/ndspmhd !
! daniel.price@monash.edu -or- dprice@cantab.net (forwards to current address) !
! !
! NDSPMHD comes with ABSOLUTELY NO WARRANTY. !
! This is free software; and you are welcome to redistribute !
! it under the terms of the GNU General Public License !
! (see LICENSE file for details) and the provision that !
! this notice remains intact. If you modify this file, please !
! note section 2a) of the GPLv2 states that: !
! !
! a) You must cause the modified files to carry prominent notices !
! stating that you changed the files and the date of any change. !
! !
! ChangeLog: !
!------------------------------------------------------------------------------!
!-----------------------------------------------------------------------------
! This module contains everything needed for the SPH kernel
!
! contains:
! setkern : sets up kernel tables
! interpolate_kernel : interpolation function from kernel tables
! interpolate_kernels : interpolation function from kernel tables
! interpolate_softening : interpolation function for softening kernel tables
!
!-----------------------------------------------------------------------------
module kernels
use erbskernels, only:geterbskernel1,geterbskernel2
use csplinekernels, only:getcsplinekernel,getcsplinekernelder
use kernel_utils, only:differentiate !,normalise
implicit none
integer, parameter :: ikern=4000 ! dimensions of kernel table
integer :: ianticlump,neps
real, parameter, private :: pi = 3.141592653589
real, dimension(0:ikern) :: wij,grwij,wijalt,grwijalt
real, dimension(0:ikern) :: wijdrag,grwijdrag,grgrwijdrag
real :: dq2table,ddq2table,radkern2,radkern,eps
!--these variables for force softening only
real, dimension(0:ikern) :: potensoft,fsoft,dphidh
!--these variables needed for plotting and analysis only (not in rates etc)
real, dimension(0:ikern) :: grgrwij,grgrwijalt
character(len=100) :: kernelname,kernelnamealt,kernelnamedrag
public :: wij,grwij,grgrwij
public :: wijdrag,grwijdrag,grgrwijdrag
public :: setkern,interpolate_kernel,interpolate_kernels,interpolate_softening
public :: setkerndrag, interpolate_kerneldrag
private :: setkerntable
logical, public :: write_kernel_table = .false.
logical, public :: verbose = .true.
contains
!-----------------------------------------------------------------
! This is the interface routine (public) -- calls setkern once only
!
!-----------------------------------------------------------------
subroutine setkern(ikernel,ndim,ierr,r)
implicit none
integer, intent(in) :: ikernel, ndim
integer, intent(out) :: ierr
real, intent(in), optional :: r
if (present(r)) then
radkern = r
else
radkern = 2. ! default value for kernel radius
endif
!
!--setup kernel tables for primary kernel
!
call setkerntable(ikernel,ndim,wij,grwij,grgrwij,kernelname,ierr)
end subroutine setkern
!----------------------------------------------------------------------
! This is the interface routine (public) -- calls setkerndrag once only
!
!----------------------------------------------------------------------
subroutine setkerndrag(ikerneldrag,ndim,ierr)
implicit none
integer, intent(in) :: ikerneldrag, ndim
integer, intent(out) :: ierr
!
!--setup kernel tables for drag kernel
!
call setkerntable(ikerneldrag,ndim,wijdrag,grwijdrag,grgrwijdrag,kernelnamedrag,ierr)
end subroutine setkerndrag
!-----------------------------------------------------------------
! This is another interface routine (public) -- calls setkern twice
! for both usual kernel and alternative kernel
!
!-----------------------------------------------------------------
subroutine setkernels(ikernel,ikernelalt,ndim,ierr1,ierr2)
implicit none
integer, intent(in) :: ikernel,ikernelalt, ndim
integer, intent(out) :: ierr1,ierr2
radkern = 2. ! default value for kernel radius
!
!--setup kernel tables for primary kernel
!
call setkerntable(ikernel,ndim,wij,grwij,grgrwij,kernelname,ierr1)
!
!--setup kernel tables for alternative kernel
!
call setkerntable(ikernelalt,ndim,wijalt,grwijalt,grgrwijalt,kernelnamealt,ierr2)
end subroutine setkernels
!-----------------------------------------------------------------
! Sets up the tables for the kernel
! Returns kernel, and derivative.
!
! Default kernel is the cubic spline, but I have experimented
! with lots more.
!
!-----------------------------------------------------------------
subroutine setkerntable(ikernel,ndim,wkern,grwkern,grgrwkern,kernellabel,ierr)
implicit none ! define local variables
integer, intent(in) :: ikernel, ndim
real, intent(out), dimension(0:ikern) :: wkern,grwkern,grgrwkern
character(len=*), intent(out) :: kernellabel
integer, intent(out) :: ierr
integer :: i,j,npower,n,ncspline,ncspline1,ncspline2
real :: q,q2,q4,q3,q5,q6,q7,q8,cnormk,cnormkaniso,secondz
real :: term1,term2,term3,term4,term
real :: dterm1,dterm2,dterm3,dterm4
real :: ddterm1,ddterm2,ddterm3,ddterm4,w0,cnormkd(3)
real :: alpha,beta,gamma,a,b,c,d,e,f,u,u2,qs,wdenom,wint
real :: bb(0:5),K,theta,dbesj1,ddbesj1,costerm,sinterm
integer :: ierrf
integer, parameter :: lu = 55
cnormk = 0.0
wkern = 0.
grwkern = 0.
grgrwkern = 0.
fsoft = 1.
potensoft = 0.
ierr = 0
select case(ikernel)
case(2)
!
!--M5 quartic (auto-generated by kernels.py)
!
kernellabel = 'M_5 quartic'
radkern = max(radkern, 2.5)
radkern2 = radkern*radkern
dq2table = radkern2/real(ikern)
if (verbose) print*,' setting up quartic with radkern = ',radkern
select case(ndim)
case(1)
cnormk = 1./24.
case(2)
cnormk = 96./(1199.*pi)
case(3)
cnormk = 0.05/pi
end select
do i=0,ikern
q2 = i*dq2table
q = sqrt(q2)
if (q < 0.5) then
wkern(i) = 6.0*q2*q2 - 15.0*q2 + 14.375
grwkern(i) = q*(24.0*q2 - 30.0)
grgrwkern(i) = 72.0*q2 - 30.0
fsoft(i) = q*(144.*q2*q2 - 504.*q2 + 805.)/840.
potensoft(i) = q2*q2*q2/35. - 3.*q2*q2/20. + 23.*q2/48. - 1199./960.
dphidh(i) = -0.2*q2*q2*q2 + 0.75*q2*q2 - 1.4375*q2 + 1.24895833333333
elseif (q < 1.5) then
wkern(i) = -5.0*(-q + 1.5)**4 + (-q + 2.5)**4
grwkern(i) = -16.0*q2*q + 60.0*q2 - 60.0*q + 5.0
grgrwkern(i) = -48.0*q2 + 120.0*q - 60.0
fsoft(i) = (-768.*q**7 + 4480.*q2*q2*q2 - 8064.*q2*q2*q + 1680.*q2*q2 + 6160.*q2*q &
+ 1.)/(6720.*q2)
potensoft(i) = (-128.*q**7 + 896.*q2*q2*q2 - 2016.*q2*q2*q + 560.*q2*q2 + 3080.*q2*q - &
8386.*q - 1.)/(6720.*q)
dphidh(i) = 2.*q2*q2*q2/15. - 4.*q2*q2*q/5. + 3.*q2*q2/2. - q2*q/3. - 11.*q2/8. + &
599./480.
elseif (q < 2.5) then
wkern(i) = (-q + 2.5)**4
grwkern(i) = -4.0*(-q + 2.5)**3
grgrwkern(i) = 12.0*q2 - 60.0*q + 75.0
fsoft(i) = (384*q**7 - 4480.*q2*q2*q2 + 20160.*q2*q2*q - 42000.*q2*q2 + &
35000.*q2*q - 2185.)/(13440.*q2)
potensoft(i) = (64*q**7 - 896.*q2*q2*q2 + 5040.*q2*q2*q - 14000.*q2*q2 + 17500.*q2*q - &
21875.*q + 2185.)/(13440.*q)
dphidh(i) = -q2*q2*q2/30. + 2.*q2*q2*q/5. - 15.*q2*q2/8. + 25.*q2*q/6. - &
125.*q2/32. + 625./384.
else
wkern(i) = 0.0
grwkern(i) = 0.0
grgrwkern(i) = 0.0
fsoft(i) = 1./q2
potensoft(i) = -1.0/q
dphidh(i) = 0.0
endif
enddo
case(3)
!
!--this is the m_6 quintic spline (see e.g. morris 1996, phd thesis)
!
kernellabel = 'M_6 quintic'
radkern = max(radkern, 3.0)
radkern2 = radkern*radkern
dq2table = radkern2/real(ikern)
select case(ndim)
case(1)
cnormk = 1./120.
case(2)
cnormk = 7./(478*pi)
case(3)
cnormk = 1./(120.*pi)
end select
do i=0,ikern
q2 = i*dq2table
q4 = q2*q2
q6 = q4*q2
q8 = q4*q4
q = sqrt(q2)
term1 = -5.*(3.-q)**4.
if (q.lt.1.0) then
wkern(i) = 66.-60.*q2 + 30.*q4 - 10.*q4*q
grwkern(i) = term1 + 30*(2.-q)**4 - 75.*(1.-q)**4
grgrwkern(i) = 20.*(3.-q)**3 - 120.*(2.-q)**3 + 300.*(1.-q)**3
fsoft(i) = q*(-35.*q4*q + 120.*q4 - 336.*q2 + 616.)/840.
potensoft(i) = -q6*q/168. + q6/42. - q4/10. + 11.*q2/30. - 239./210.
dphidh(i) = q6*q/21. - q6/6. + q4/2. - 11.*q2/10. + 239./210.
elseif ((q.ge.1.0).and.(q.lt.2.0)) then
wkern(i) = (3.-q)**5 - 6.*(2.-q)**5
grwkern(i) = term1 + 30*(2.-q)**4
grgrwkern(i) = 20.*(3.-q)**3 - 120.*(2.-q)**3
fsoft(i) = (35*q8 - 360.*q6*q + 1400.*q6 - 2352.*q4*q + 1050.*q4 + 952.*q2*q + &
5.)/(1680.*q2)
potensoft(i) = (q*(5.*q6*q - 60.*q6 + 280.*q4*q - 588.*q4 + 350.*q2*q + 476.*q2 - &
1892.) - 5.)/(1680.*q)
dphidh(i) = -q6*q/42. + q6/4. - q4*q + 7.*q4/4. - 5.*q2*q/6. - 17.*q2/20. + &
473./420.
elseif ((q.ge.2.0).and.(q.le.3.0)) then
wkern(i) = (3.-q)**5
grwkern(i) = term1
grgrwkern(i) = 20.*(3.-q)**3
fsoft(i) = (-7.*q8 + 120.*q6*q - 840.*q6 + 3024.*q4*q - 5670.*q4 + 4536.*q2*q - &
507.)/(1680.*q2)
potensoft(i) = (q*(-q6*q + 20.*q6 - 168.*q4*q + 756.*q4 - 1890.*q2*q + 2268.*q2 - &
2916.) + 507.)/(1680.*q)
dphidh(i) = q6*q/210. - q6/12. + 3.*q4*q/5. - 9.*q4/4. + 9.*q2*q/2. - 81.*q2/20. + &
243./140.
else
wkern(i) = 0.0
grwkern(i) = 0.0
grgrwkern(i) = 0.
fsoft(i) = q**(-2.0)
potensoft(i) = -1.0/q
dphidh(i) = 0.0
endif
enddo
case(4)
!
!--Hexic M7 kernel, used to test the streaming instability
!
kernellabel = 'M_7 hexic'
radkern = max(radkern, 3.5)
radkern2 = radkern*radkern
dq2table = radkern2/real(ikern)
select case(ndim)
case(1)
cnormk = 1./720. !--=1/(7!):OK
case(2)
cnormk = 256./(113149.*pi)
case(3)
cnormk = 1./(840*pi)
end select
do i=0,ikern
q2 = i*dq2table
q = sqrt(q2)
q3 = q*q2
q4 = q2*q2
q5 = q3*q2
q6 = q3*q3
if (q.lt.0.5) then
wkern(i) = -20.*q6 + 105.*q4 - 288.75*q2+367.9375
grwkern(i) = -120.*q5+420.*q3-577.5*q
grgrwkern(i) = -600.*q4+1260.*q2-577.5
elseif ((q.ge.0.5) .and. (q.lt.1.5)) then
wkern(i) =15.*q6-105.*q5+236.25*q4-87.5*q3 &
-255.9375*q2-6.5625*q+368.484375
grwkern(i) = 90.*q5-525.*q4+945.*q3 &
-262.5*q2-511.875*q-6.5625
grgrwkern(i) = 450.*q4-2100.*q3+2835.*q2-525.*q-511.875
elseif ((q.ge.1.5).and. (q.lt.2.5)) then
wkern(i)=-6.*q6+84.*q5-472.5*q4+1330.*q3 &
-1850.625*q2+950.25*q+129.28125
grwkern(i) = -36.*q5+420.*q4-1890.*q3 &
+3990.*q2-3701.25*q+950.25
grgrwkern(i) = -180.*q4+1680.*q3-5670.*q2+7980.*q-3701.25
elseif ((q.ge.2.5).and. (q.lt.3.5)) then
wkern(i) =q6-21.*q5+183.75*q4-857.5*q3 &
+2250.9375*q2-3151.3125*q+1838.265625
grwkern(i) = 6.*q5-105.*q4+735.*q3-2572.5*q2 &
+4501.875*q-3151.3125
grgrwkern(i) = 30.*q4-420.*q3+2205.*q2-5145.*q+4501.875
else
wkern(i) = 0.
grwkern(i) = 0.
grgrwkern(i) = 0.
endif
enddo
case(5)
!
!--Heptic M8 kernel, used to test the streaming instability
!
kernellabel = 'M_8 heptic'
radkern = max(radkern, 4.0)
radkern2 = radkern*radkern
dq2table = radkern2/real(ikern)
select case(ndim)
case(1)
cnormk = 1./5040. !--=1/(7!):OK
case(2)
cnormk = 9./(29740.*pi)
case(3)
cnormk = 1./(6720.*pi)
end select
do i=0,ikern
q2 = i*dq2table
q = sqrt(q2)
q3 = q*q2
q4 = q2*q2
q5 = q3*q2
q6 = q3*q3
q7 = q6*q
if (q.lt.1.0) then
wkern(i) = 35.*q7 - 140.*q6 + 560.*q4 &
- 1680.*q2+ 2416.
grwkern(i) = 245.*q6 - 840.*q5 + 2240.*q3 - 3360.*q
grgrwkern(i) = 1470.*q5 - 4200.*q4 &
+ 6720.*q2 - 3360.
elseif ((q.ge.1.0) .and. (q.lt.2.0)) then
wkern(i) = -21.*q7 + 252.*q6 - 1176.*q5 + 2520.*q4 &
- 1960.*q3 - 504.*q2 - 392.*q + 2472.
grwkern(i) = -147.*q6 + 1512.*q5 - 5880.*q4 &
+ 10080.*q3 - 5880.*q2 - 1008.*q - 392.
grgrwkern(i) = -882.*q5 + 7560.*q4 &
- 23520.*q3 +30240.*q2 -11760.*q - 1008.
elseif ((q.ge.2.0).and. (q.lt.3.0)) then
wkern(i) = 7.*q7 - 140.*q6 + 1176.*q5 - 5320.*q4 &
+ 13720.*q3 - 19320.*q2 + 12152.*q - 1112.
grwkern(i) = 49.*q6 - 840.*q5 + 5880.*q4 &
- 21280.*q3 + 41160.*q2 - 38640.*q + 12152.
grgrwkern(i) = 294.*q5 - 4200.*q4 &
+ 23520.*q3 - 63840.*q2 + 82320.*q - 38640.
elseif ((q.ge.3.0).and. (q.lt.4.0)) then
wkern(i) = -q7 + 28.*q6 - 336.*q5 + 2240.*q4 &
- 8960.*q3 + 21504.*q2 - 28672.*q + 16384.
grwkern(i) = -7.*q6 + 168.*q5 - 1680.*q4 &
+ 8960.*q3 - 26880.*q2 + 43008.*q - 28672.
grgrwkern(i) = -42.*q5 + 840.*q4 &
- 6720.*q3 + 26880.*q2 - 53760.*q + 43008.
else
wkern(i) = 0.
grwkern(i) = 0.
grgrwkern(i) = 0.
endif
enddo
case(6,7)
!
!--this is the do-it-yourself quintic kernel (general class of quintic splines)
!
radkern = max(radkern, 2.0)
radkern2 = radkern*radkern
dq2table = radkern2/real(ikern)
gamma = 0.
if (ikernel.eq.5) then
beta = 0.5
alpha = 1.7 !!1.4
kernellabel = 'New quintic (1)'
elseif (ikernel.eq.6) then
beta = 0.7
alpha = 1.5
kernellabel = 'New quintic (2)'
else
!--match to cubic spline, ie w''(0) = -2
kernellabel = 'Cubic-like quintic'
beta = 0.85
!print*,' enter beta'
!read*,beta
if (verbose) print*,'beta = ',beta, ' calculating alpha'
term1 = (beta+2.)*(beta**3 - 2.*beta**2 - 4.*beta + 128)
alpha = -0.5*(4.*beta + beta**2 + 4. - sqrt(term1))/(beta + 2.)
term2 = (beta-2.)*(beta+2.)*(beta-alpha)*(beta+alpha)
dterm2 = (alpha+2)*(-alpha**2 + beta**2 - 4.)
q = 2.*alpha*(-alpha**2 - 2.*alpha + beta**2 - 4. + sqrt(term2))/dterm2
if (verbose) print*,' 3rd derivative zero at q = ',q
endif
c =0.
a = (-radkern**4 + (radkern**2 + c*gamma**2)*beta**2) &
/(alpha**2*(alpha**2-beta**2))
b = -(radkern**4 + a*alpha**4 + c*gamma**4)/(beta**4)
if (verbose) print*,'matching points = ',beta,alpha
select case(ndim)
case(1)
cnormk = 3./(a*alpha**6 + b*beta**6 + c*gamma**6 + radkern**6) ! for radkern = 2 and 1d
if (verbose) print*,'1d cnormk = ',cnormk,' a,b = ',a,b
case(2)
cnormk = 42./(2.*pi*(a*alpha**7 + b*beta**7 + c*gamma**7 + radkern**7))
if (verbose) print*,'2d cnormk = ',cnormk,' a,b = ',a,b,beta,alpha
case default
if (verbose) write(*,666)
ierr = 1
return
!stop
end select
do i=0,ikern
q2 = i*dq2table
q = sqrt(q2)
term1 = (radkern-q)**5
term2 = (alpha-q)**5
term3 = (beta-q)**5
term4 = (gamma-q)**5
dterm1 = -5*(radkern-q)**4
dterm2 = -5*(alpha-q)**4
dterm3 = -5*(beta-q)**4
dterm4 = -5*(gamma-q)**4
ddterm1 = 20*(radkern-q)**3
ddterm2 = 20*(alpha-q)**3
ddterm3 = 20*(beta-q)**3
ddterm4 = 20*(gamma-q)**3
if (q.lt.gamma) then
wkern(i) = term1 + a*term2 + b*term3 + c*term4
grwkern(i) = dterm1 + a*dterm2 + b*dterm3 + c*dterm4
grgrwkern(i) = ddterm1 + a*ddterm2 + b*ddterm3 + c*ddterm4
elseif ((q.ge.gamma).and.(q.lt.beta)) then
wkern(i) = term1 + a*term2 + b*term3
grwkern(i) = dterm1 + a*dterm2 + b*dterm3
grgrwkern(i) = ddterm1 + a*ddterm2 + b*ddterm3
elseif ((q.ge.beta).and.(q.lt.alpha)) then
wkern(i) = term1 + a*term2
grwkern(i) = dterm1 + a*dterm2
grgrwkern(i) = ddterm1 + a*ddterm2
elseif ((q.ge.alpha).and.(q.lt.radkern)) then
wkern(i) = term1
grwkern(i) = dterm1
grgrwkern(i) = ddterm1
else
wkern(i) = 0.0
grwkern(i) = 0.0
grgrwkern(i) = 0.
endif
enddo
case (8)
!
!--(1-r^2)^3
!
npower = 6
write(kernellabel,"(a,i1)") '(1-r^2)^',npower
radkern = max(radkern, 2.0)
radkern2 = radkern*radkern
dq2table = radkern2/real(ikern)
select case(ndim)
case(1)
select case(npower)
case(1)
cnormk = 0.5*3./(2.*radkern**3)
case(2)
cnormk = 0.5*15./(8.*radkern**5)
case(3)
cnormk = 0.5*35./(16.*radkern**7)
case(4)
cnormk = 0.5*315./(128.*radkern**9)
case(5)
cnormk = 0.5*693./(256.*radkern**11)
case(6)
cnormk = 0.5*3003./(1024.*radkern**13)
case(7)
cnormk = 0.5*6435./(2048.*radkern**15)
case(8)
cnormk = 0.5*109395./(32768.*radkern**17)
case default
cnormk = 0.
end select
case(2)
cnormk = 3./(64.*pi)
case(3)
cnormk = 105./(4096.*pi)
end select
do i=0,ikern
q2 = i*dq2table
q = sqrt(q2)
if (q.lt.radkern) then
wkern(i) = (radkern**2-q2)**npower
grwkern(i) = -2.*npower*q*(radkern**2-q2)**(npower-1)
grgrwkern(i) = (4.*npower*(npower-1)*q2*(radkern**2-q2)**(npower-2) &
- 2.*npower*(radkern**2-q2)**(npower-1))
else
wkern(i) = 0.
grwkern(i) = 0.
grgrwkern(i) = 0.
endif
enddo
case(9)
!
!--Better cubic (auto-generated by kernels.py)
!
kernellabel = 'Better cubic'
radkern = max(radkern, 2.0)
radkern2 = radkern*radkern
dq2table = radkern2/real(ikern)
select case(ndim)
case(1)
cnormk = 1.
case(2)
cnormk = 240./(119.*pi)
case(3)
cnormk = 4./(3.*pi)
end select
do i=0,ikern
q2 = i*dq2table
q4 = q2*q2
q6 = q4*q2
q8 = q4*q4
q = sqrt(q2)
if (q < 1.0) then
wkern(i) = 0.375*q2*q - 0.8125*q2 + 0.625
grwkern(i) = q*(1.125*q - 1.625)
grgrwkern(i) = 2.25*q - 1.625
fsoft(i) = q*(15.*q2*q - 39.*q2 + 50.)/45.
potensoft(i) = q4*q/15. - 13.*q4/60. + 5.*q2/9. - 119./90.
dphidh(i) = -2.*q4*q/5. + 13.*q4/12. - 5.*q2/3. + 119./90.
elseif (q < 2.0) then
wkern(i) = -0.125*q2*q + 0.8125*q2 - 1.75*q + 1.25
grwkern(i) = -0.375*q2 + 1.625*q - 1.75
grgrwkern(i) = -0.75*q + 1.625
fsoft(i) = (-5.*q6 + 39.*q4*q - 105.*q4 + 100.*q2*q - 3.)/(45.*q2)
potensoft(i) = (-4.*q6 + 39.*q4*q - 140.*q4 + 200.*q2*q - 272.*q + 12.)/(180.*q)
dphidh(i) = 2.*q4*q/15. - 13.*q4/12. + 28.*q2*q/9. - 10.*q2/3. + 68./45.
else
wkern(i) = 0.0
grwkern(i) = 0.0
grgrwkern(i) = 0.0
fsoft(i) = 1./q2
potensoft(i) = -1.0/q
dphidh(i) = 0.0
endif
enddo
case(10)
!
!--gaussian
!
kernellabel = 'Gaussian'
radkern = max(radkern, 10.0)
radkern2 = radkern*radkern
dq2table = radkern2/real(ikern)
select case(ndim)
case(1)
cnormk = 1./sqrt(pi)
case(2)
cnormk = 1./pi
case(3)
cnormk = 1./(pi*sqrt(pi))
end select
do i=0,ikern
q2 = i*dq2table
q = sqrt(q2)
if (q.lt.radkern) then
wkern(i) = exp(-q2)
grwkern(i) = -2.*q*wkern(i)
grgrwkern(i) = -2.*q*grwkern(i) - 2.*wkern(i)
else
wkern(i) = 0.
grwkern(i) = 0.
grgrwkern(i) = 0.
endif
enddo
case(11)
!
!--this is the usual spline based kernel modified for r/h < 2/3 to
! prevent particles from clumping (see thomas & couchman '92)
!
kernellabel = 'Thomas & Couchman anti-clumping'
radkern = max(radkern, 2.0) ! interaction radius of kernel
radkern2 = radkern*radkern
dq2table = radkern*radkern/real(ikern)
select case(ndim)
case(1)
cnormk = 2./3.
! cnormk = 54./85. ! normalisation constant
case(2)
cnormk = 10./(7.*pi)
case(3)
cnormk = 1./pi
end select
do i=0,ikern
q2 = i*dq2table
q = sqrt(q2)
if (q.lt.2./3.) then
wkern(i) = 1. - 1.5*q2 + 0.75*q*q2
! wkern(i) = 11./9. - q
grwkern(i) = -1.
grgrwkern(i) = 0.
elseif (q.le.1.0) then
wkern(i) = 1. - 1.5*q2 + 0.75*q*q2
grwkern(i) = -3.*q+ 2.25*q2
grgrwkern(i) = -3. + 4.5*q
elseif (q.le.2.0) then
wkern(i) = 0.25*(2.-q)**3.
grwkern(i) = -0.75*(2.-q)**2.
grgrwkern(i) = 1.5*(2.-q)
else
wkern(i) = 0.0
grwkern(i) = 0.0
grgrwkern(i) = 0.
endif
enddo
case(12)
!
!--this is the squashed quintic spline from bonet & kulesegaram
!
kernellabel = 'BK squashed quintic spline'
radkern = max(radkern, 2.0)
radkern2 = radkern*radkern
dq2table = radkern2/real(ikern)
select case(ndim)
case(1)
cnormk = 1./16.
case default
if (verbose) write(*,666)
ierr = 1
return
end select
do i=0,ikern
q2 = i*dq2table
q = sqrt(q2)
if (q.lt.1.0) then
wkern(i) = (2.-q)**5 - 16.*(1.-q)**5
grwkern(i) = -5.*(2.-q)**4 + 80.*(1.-q)**4.
grgrwkern(i) = 20.*(2.-q)**3 - 320.*(1.-q)**3.
elseif ((q.ge.1.0).and.(q.le.2.0)) then
wkern(i) = (2.-q)**5
grwkern(i) = -5.*(2.-q)**4
grgrwkern(i) = 20.*(2.-q)**3
else
wkern(i) = 0.0
grwkern(i) = 0.0
grgrwkern(i) = 0.
endif
enddo
case (13)
!
!--(1-r)^n - a peaked kernel (deriv non-zero at origin) - truly awful
!
npower = 4
write(kernellabel,"(a,i1)") '(2-r)^',npower
radkern = max(radkern, 2.0)
radkern2 = radkern*radkern
dq2table = radkern2/real(ikern)
select case(ndim)
case(1)
cnormk = 0.5*(npower+1)/radkern**(npower+1)
case(2,3)
if (verbose) write(*,666)
ierr = 1
return
!stop 'normalisation const not defined in kernel'
end select
do i=0,ikern
q2 = i*dq2table
q = sqrt(q2)
if (q.lt.radkern) then
wkern(i) = (radkern-q)**npower
grwkern(i) = -npower*(radkern-q)**(npower-1)
grgrwkern(i) = npower*(npower-1)*(radkern-q)**(npower-2)
else
wkern(i) = 0.
grwkern(i) = 0.
grgrwkern(i) = 0.
endif
enddo
case(14)
!
!--this is a modification of the cubic spline
!
kernellabel = 'Peaked cubic spline'
radkern = max(radkern, 2.0)
radkern2 = radkern*radkern
dq2table = radkern2/real(ikern)
select case(ndim)
case(1)
cnormk = 1./8.
case(2)
cnormk = 5./(16.*pi)
case(3)
cnormk = 15./(64.*pi)
case default
if (verbose) write(*,666)
ierr = 1
return
end select
do i=0,ikern
q2 = i*dq2table
q = sqrt(q2)
if (q.lt.2.0) then
wkern(i) = (2.-q)**3
grwkern(i) = -3.*(2.-q)**2
grgrwkern(i) = 6.*(2.-q)
else
wkern(i) = 0.0
grwkern(i) = 0.0
grgrwkern(i) = 0.
endif
enddo
case(15)
!
!--this is a modification of the cubic spline
!
kernellabel = 'Peaked cubic spline 2'
radkern = max(radkern, 2.0)
radkern2 = radkern*radkern
dq2table = radkern2/real(ikern)
select case(ndim)
case(1)
cnormk = 4./7.
case(2)
cnormk = 4./(3.*pi)
case(3)
cnormk = 30./(31.*pi)
case default
if (verbose) write(*,666)
ierr = 1
return
end select
do i=0,ikern
q2 = i*dq2table
q = sqrt(q2)
if (q.lt.1.0) then
wkern(i) = 0.25*(2.-q)**3 - 0.5*(1.-q)**3
grwkern(i) = -0.75*(2.-q)**2 + 1.5*(1.-q)**2
grgrwkern(i) = 1.5*q
elseif(q.lt.2.0) then
wkern(i) = 0.25*(2.-q)**3
grwkern(i) = -0.75*(2.-q)**2
grgrwkern(i) = 3. - 1.5*q
else
wkern(i) = 0.0
grwkern(i) = 0.0
grgrwkern(i) = 0.
endif
enddo
case(16)
!
!--another version of the peaked cubic spline
!
alpha = 1.0
!print*,'enter alpha,'
!read*,alpha
write(kernellabel,"(a,f6.2)") 'peaked cubic spline alpha = ',alpha
!! kernellabel = 'peaked cubic spline 3'
radkern = max(radkern, 2.0)
radkern2 = radkern*radkern
dq2table = radkern2/real(ikern)
select case(ndim)
case(1)
cnormk = 1./(1. + 0.25/alpha + 1.5*alpha - alpha**2)
case(2)
cnormk = -20.*alpha/(pi*(10.*alpha**3 - 20.*alpha**2 - alpha - 4.))
case(3)
cnormk = -30.*alpha/(pi*(20.*alpha**3 - 45.*alpha**2 + 4.*alpha - 10.))
case default
if (verbose) write(*,666)
ierr = 1
return
end select
do i=0,ikern
q2 = i*dq2table
q = sqrt(q2)
if (q.lt.alpha) then
wkern(i) = 0.25*(2.-q)**3 - 0.5/alpha*(alpha-q)**3
grwkern(i) = -0.75*(2.-q)**2 + 1.5/alpha*(alpha-q)**2
grgrwkern(i) = 1.5*(2.-q) - 3./alpha*(alpha-q)
elseif (q.lt.2.0) then
wkern(i) = 0.25*(2.-q)**3
grwkern(i) = -0.75*(2.-q)**2
grgrwkern(i) = 1.5*(2.-q)
else
wkern(i) = 0.0
grwkern(i) = 0.0
grgrwkern(i) = 0.
endif
enddo
case(17)
!
!--cosine**n kernel
!
kernellabel = 'cosine**n'
radkern = max(radkern, 2.0)
radkern2 = radkern*radkern
dq2table = radkern2/real(ikern)
n = 5
!print*,'enter n'
!read*,n
select case(ndim)
case(1)
select case(n)
case(3)
cnormk = 1./1.694930
case(4)
cnormk = 1./1.5
case(5)
cnormk = 1./1.359516
case(6)
cnormk = 1./1.249811
case(7)
cnormk = 1./1.163532
case(8)
cnormk = 1./1.095270
case default
cnormk = 1.
end select
case default
if (verbose) write(*,666)
ierr = 1
return
end select
do i=0,ikern
q2 = i*dq2table
q = sqrt(q2)
if (q.lt.radkern) then
wkern(i) = cos(0.25*pi*q)**n !!sin(0.5*pi*q)/q
grwkern(i) = -0.25*pi*n*(cos(0.25*pi*q))**(n-1)*sin(0.25*pi*q)
grgrwkern(i) = 1./16.*pi*pi*n* &
((n-1)*(cos(0.25*pi*q))**(n-2)*sin(0.25*pi*q)**2 - &
cos(0.25*pi*q)**n)
else
wkern(i) = 0.0
grwkern(i) = 0.0
grgrwkern(i) = 0.
endif
enddo
case(18)
!
!--Dirichlet kernel (sin(x)/x) (auto-generated by kernels.py)
!
kernellabel = 'Dirichlet [sin(q)/q]'
radkern = max(radkern, 2.0)
radkern2 = radkern*radkern
dq2table = radkern2/real(ikern)
select case(ndim)
case(1)
cnormk = 1./3.74
!cnormk = 0.5/(-Integral(sin(0.5*pi*q)/q, (q, 0.0)) + Integral(sin(0.5*pi*q)/q, (q, 2.0)))
case(2)
cnormk = 1./8.
case(3)
cnormk = 1./16.
end select
do i=0,ikern
q2 = i*dq2table
q4 = q2*q2
q6 = q4*q2
q8 = q4*q4
q = sqrt(q2)
if (q < epsilon(q)) then
wkern(i) = 0.5*pi
grwkern(i) = 0.
grgrwkern(i) = 0.
elseif (q < 2.0) then
wkern(i) = sin(0.5*pi*q)/q
grwkern(i) = (0.5*pi*q*cos(0.5*pi*q) - 1.0*sin(0.5*pi*q))/q2
grgrwkern(i) = (-0.25*pi**2*q2*sin(0.5*pi*q) - 1.0*pi*q*cos(0.5*pi*q) + &
2.0*sin(0.5*pi*q))/q2*q
fsoft(i) = (0.5*pi*q*cos(0.5*pi*q) - 1.0*sin(0.5*pi*q))/q2
potensoft(i) = (0.5*pi*q*cos(0.5*pi*q) - 1.0*sin(0.5*pi*q))/q2
dphidh(i) = (0.5*pi*q*cos(0.5*pi*q) - 1.0*sin(0.5*pi*q))/q2
else
wkern(i) = 0.0
grwkern(i) = 0.0
grgrwkern(i) = 0.0
fsoft(i) = 0.0
potensoft(i) = 0.0
dphidh(i) = 0.0
endif
enddo
case(19)
!
!--sin**2/x**2
!
kernellabel = '[sin(q)/q]**2'
radkern = max(radkern, 2.0)
radkern2 = radkern*radkern
dq2table = radkern2/real(ikern)
select case(ndim)
case(1)
cnormk = 1./4.35 !0.5/(-Integral(sin(0.5*pi*q)**2/q**2, (q, 0.0)) + Integral(sin(0.5*pi*q)**2/q**2, (q, 2.0)))
case(2)
cnormk = 1. !1.0/(-Integral(2.0*pi*sin(0.5*pi*q)**2/q, (q, 0.0)) + Integral(2.0*pi*sin(0.5*pi*q)**2/q, (q, 2.0)))
case(3)
cnormk = 0.25/pi
end select
do i=0,ikern
q2 = i*dq2table
q4 = q2*q2
q = sqrt(q2)
if (q.lt.radkern) then
term = 0.5*pi*q
if (q.gt.0.) then
wkern(i) = sin(0.5*pi*q)**2/q2
grwkern(i) = (1.0*pi*q*cos(0.5*pi*q) - 2.0*sin(0.5*pi*q))*sin(0.5*pi*q)/q2*q
grgrwkern(i) = (-1.0*pi**2*q2*sin(0.5*pi*q)**2 + 0.5*pi**2*q2 - &
4.0*pi*q*sin(0.5*pi*q)*cos(0.5*pi*q) + 6.0*sin(0.5*pi*q)**2)/q4
fsoft(i) = (1.0*pi*q*cos(0.5*pi*q) - 2.0*sin(0.5*pi*q))*sin(0.5*pi*q)/q2*q
potensoft(i) = (1.0*pi*q*cos(0.5*pi*q) - 2.0*sin(0.5*pi*q))*sin(0.5*pi*q)/q2*q
dphidh(i) = (1.0*pi*q*cos(0.5*pi*q) - 2.0*sin(0.5*pi*q))*sin(0.5*pi*q)/q2*q
else
wkern(i) = 0.5*pi
grwkern(i) = 0.
grgrwkern(i) = 0.
endif
else
wkern(i) = 0.0
grwkern(i) = 0.0
grgrwkern(i) = 0.
endif
enddo
case(20)
!
!--Jackson-Feyer de la Vallee Poussin Kernel
!
kernellabel = '[sin(q)/q]**4'
radkern = max(radkern, 2.0)
radkern2 = radkern*radkern
dq2table = radkern2/real(ikern)
select case(ndim)
case(1)
cnormk = 0.123558082
case(2)
cnormk = 0.2994570731/pi
case(3)
cnormk = 0.236804709/pi
case default
if (verbose) write(*,666)
ierr = 1
return
end select
do i=0,ikern
q2 = i*dq2table
q = sqrt(q2)
if (q.lt.radkern) then
term = 0.5*pi*q
if (q.gt.0.) then
wkern(i) = (sin(term)**4)/(q2*q2)
grwkern(i) = 2./(q2*q2)*(sin(term)**3*cos(term)*pi - 2.*sin(term)**4/q)