-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquasisymmetry_differentiation_matrix.f90
1307 lines (1110 loc) · 37.3 KB
/
quasisymmetry_differentiation_matrix.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
subroutine quasisymmetry_differentiation_matrix(N, xMin, xMax, option, quadrature_option, x, weights, ddx, d2dx2)
! Finite difference and spectral differentiation matrices and integration
! weights for a uniform grid.
!
! Created by Matt Landreman,
! Massachusetts Institute of Technology, Plasma Science & Fusion Center, 2012.
!
! Inputs:
! N = number of grid points.
! xMin = minimum value in the domain.
! xMax = maximum value in the domain.
! option = switch for controlling order of accuracy for differentiation
! and handling of endpoints.
! quadrature_option = switch for controlling the quadrature weights in a non-periodic domain.
!
! Options for option:
! 0 = The domain [xMin, xMax] is assumed to be periodic. A 3-point stencil
! is used everywhere. A grid point will be placed at xMin but not
! xMax.
! 1 = Same as option=0, except a grid point will be placed at xMax but not
! xMin.
! 2 = The domain [xMin, xMax] is assumed to be non-periodic. A 3-point
! stencil is used everywhere. The first and last row of the
! differentiation matrices will use one-sided differences, so they
! will each have a non-tridiagonal element.
! 3 = The same as option=2, except that the first differentiation matrix
! will use a 2-point 1-sided stencil for the first and last elements
! so the matrix is strictly tri-diagonal. The 2nd derivative matrix
! is the same as for option 2, since it is not possible to compute
! the 2nd derivative with only a 2-point stencil.
! 10 = The domain [xMin, xMax] is assumed to be periodic. A 5-point stencil
! is used everywhere. A grid point will be placed at xMin but not
! xMax. This option is like option=0 but more accurate.
! 11 = Same as option=10, except a grid point will be placed at xMax but
! not xMin. This option is like option=1 but more accurate.
! 12 = The domain [xMin, xMax] is assumed to be non-periodic. A 5-point
! stencil is used everywhere. The first two and last two rows of
! the differentiation matrices will then each have non-pentadiagonal
! elements.
! 13 = The same as option 12, except that 3-point stencils are used for the
! first and last rows of the differentiation matrices, and 4-point
! stencils are used for the 2nd and penultimate rows of the
! differentiation matrices. With this option, both differentiation
! matrices are strictly penta-diagonal.
! 14 = Similar to 12 and 13, except the first 2 rows are changed so there is never downwinding.
! 15 = Similar to 12 and 13, except the last 2 rows are changed so there is never upwinding.
! 16 = Similar to 12 and 13, except the first and last 2 rows are changed so there is never upwinding.
! 20 = The domain [xMin, xMax] is assumed to be periodic. Spectral
! differentiation matrices are returned. A grid point will be placed
! at xMin but not xMax.
! 21 = Same as option=20, except a grid point will be placed at xMax but not
! xMin.
! 30 = Periodic with a grid point at xMin but not xMax. Upwinding to the
! left. A 2-point stencil is used for the first derivative and a
! 3-point stencil is used for the second derivative.
! 31 = Periodic with a grid point at xMax but not xMin. Upwinding to the
! left. A 2-point stencil is used for the first derivative and a
! 3-point stencil is used for the second derivative.
! 32 = Aperiodic. Upwinding to the left. A 2-point stencil is used for the
! first derivative and a 3-point stencil is used for the second
! derivative. The top row of D and the top two rows of DD are zero.
! 40 = Same as 30 but upwinding to the right.
! 41 = Same as 31 but upwinding to the right.
! 42 = Same as 32 but upwinding to the right. The bottom row of D and the
! bottom two rows of DD are zero.
! 50 = Periodic with a grid point at xMin but not xMax. Upwinding to the
! left. A 3-point stencil is used for both derivatives.
! 51 = Periodic with a grid point at xMax but not xMin. Upwinding to the
! left. A 3-point stencil is used for both derivatives.
! 52 = Aperiodic. Upwinding to the left. A 3-point stencil is used for
! both derivatives. The top row of both matrices is all zero. The
! second row of D uses a 2-point stencil.
! 60 = Same as 50 but upwinding to the right.
! 61 = Same as 51 but upwinding to the right.
! 62 = Same as 52 but upwinding to the right. The bottom row of both
! derivative matrices is all zero. The penultimate row of D uses a
! 2-point stencil.
! 80 = Periodic with a grid point at xMin but not xMax. The first derivative is upwinded to the
! left. A stencil is used with 1 point on 1 side and 2 points on the
! other side. The second derivative is the same as in option 0.
! 81 = Same as 80 but with a grid point at xMax and not xMin.
! 82 = Like 80 but not periodic, with a grid point at both xMin and xMax.
! The top row of D is zero.
! 90 = Same as 80 but upwinding to the right.
! 91 = Same as 90 but with a grid point at xMax and not xMin.
! 92 = Like 90 but not periodic, with a grid point at both xMin and xMax.
! The bottom row of D is zero.
! 100 = Periodic with a grid point at xMin but not xMax.
! 1st derivative only. Upwinded to the left.
! Stencil has 1 point on 1 side, 3 points on the other side
! 101 = Same as 100, but with no grid point at xMin and with a grid point at xMax.
! 102 = Same as 100, but aperiodic, with grid points at both xMin and xMax.
! 110 = Periodic with a grid point at xMin but not xMax.
! 1st derivative only. Upwinded to the right.
! Stencil has 1 point on 1 side, 3 points on the other side
! 111 = Same as 110, but with no grid point at xMin and with a grid point at xMax.
! 112 = Same as 100, but aperiodic, with grid points at both xMin and xMax.
! 120 = Periodic with a grid point at xMin but not xMax.
! 1st derivative only. Upwinded to the left.
! Stencil has 2 points on 1 side, 3 points on the other side
! 121 = Same as 120, but with no grid point at xMin and with a grid point at xMax.
! 122 = Same as 120, but aperiodic, with grid points at both xMin and xMax.
! The first and last rows use at least a 5 point stencil, so there is
! no upwinding or backwards upwinding for 3 rows.
! 123 = Same as 122, but the first and last rows all are strictly upwinded
! so the diagonal is everywhere positive, except for the first row.
! 130 = Periodic with a grid point at xMin but not xMax.
! 1st derivative only. Upwinded to the right.
! Stencil has 2 points on 1 side, 3 points on the other side
! 131 = Same as 130, but with no grid point at xMin and with a grid point at xMax.
! 132 = Same as 130, but aperiodic, with grid points at both xMin and xMax.
! 133 = Same as 132, but the first and last rows all are strictly upwinded
! so the diagonal is everywhere negative, except for the last row.
! 140 = Fromm scheme, upwinding to the left. Periodic.
! 150 = Fromm scheme, upwinding to the right. Periodic.
! 160 = Return the 4th order ddx stencil (2 points on either side), and instead of returning d2dx2,
! return (dx)^3 * the d^4/dx^4 stencil of the same size in 'd2dx2'. This option is useful for making
! 3rd order stencils with varying levels of upwinding.
! 170 = Return the 6th order ddx stencil (3 points on either side), and instead of returning d2dx2,
! return (dx)^5 * the d^4/dx^6 stencil of the same size in 'd2dx2'. This option is useful for making
! 5th order stencils with varying levels of upwinding.
!
! Options for quadrature_option:
! 0 = Standard trapezoid rule: half weight at the first and last grid points.
! 1 = Numerical Recipes page 160 eq 4.1.12, also http://mathworld.wolfram.com/Newton-CotesFormulas.html eq (36). Error is O(h^3).
! 2 = http://mathworld.wolfram.com/Newton-CotesFormulas.html eq (37).
! 3 = Numerical recipes page 160 eq 4.1.14. Error is O(h^4).
! If the domain is periodic, quadrature_option is ignored and the quadrature weights will all be equal.
! Outputs:
! x = column vector with the grid points.
! weights = column vector with the weights for integration using the trapezoid rule.
! ddx = matrix for differentiation.
! d2dx2 = matrix for the 2nd derivative.
use stel_kinds
implicit none
integer, intent(in) :: N, option, quadrature_option
real(dp), intent(in) :: xMin, xMax
real(dp), intent(out), dimension(N) :: x, weights
real(dp), intent(out), dimension(N,N) :: ddx, d2dx2
integer :: i
real(dp) :: dx, dx2
real(dp) :: h
integer :: n1, n2
real(dp), allocatable :: topc(:), col1(:)
real(dp), parameter :: pi = 3.1415926535897932384626433d+0
! ***************************************************************
! Validate input
! ***************************************************************
if (N<1) then
print *,"Error! N must be at least 1."
print *,"N = ",N
stop
end if
if (xMin > xMax) then
print *,"Error! xMax must be larger than xMin"
print *,"xMax=",xMax
print *,"xMin=",xMin
stop
end if
if (xMin == xMax) then
print *,"Error! xMax cannot equal xMin"
print *,"xMin=xMax=",xMin
stop
end if
! ***************************************************************
! Set gridpoints
! ***************************************************************
select case (option)
case (2, 3, 12, 13, 14, 15, 16, 32, 42, 52, 62, 82, 92, 102, 112, 122, 123, 132, 133)
! Include points at both xMin and xMax:
x = [( (xMax-xMin)*i/(N-1)+xMin, i=0,N-1 )]
case (0,10,20,30,40,50,60,80,90,100,110,120,130,140,150,160,170)
! Include a point at xMin but not xMax:
x = [( (xMax-xMin)*i/(N)+xMin, i=0,N-1 )]
case (1,11,21,31,41,51,61,81,91,101,111,121,131,141,151,161,171)
! Include a point at xMax but not xMin:
x = [( (xMax-xMin)*i/(N)+xMin, i=1,N )]
case default
print *,"Error! Invalid value for option in uniformDiffMatrices, location 1:", option
stop
end select
dx=x(2)-x(1)
dx2=dx*dx
! ***************************************************************
! Set integration weights
! ***************************************************************
weights=dx
select case (option)
case (2, 3, 12, 13, 14, 15, 16, 32, 42, 52, 62, 82, 92, 102, 112, 122, 123, 132, 133)
! Grid is aperiodic
select case (abs(quadrature_option)) ! abs is so negative values will invoke the pseudoinverse method instead.
case (0)
! Standard trapezoid rule
weights(1)=dx/2
weights(N)=dx/2
case (1)
weights(1) = (dx*5)/12
weights(N) = (dx*5)/12
weights(2) = (dx*13)/12
weights(N-1) = (dx*13)/12
case (2)
weights(1) = (dx*2)/5
weights(N) = (dx*2)/5
weights(2) = (dx*11)/10
weights(N-1) = (dx*11)/10
case (3)
weights(1) = (dx*3)/8
weights(N) = (dx*3)/8
weights(2) = (dx*7)/6
weights(N-1) = (dx*7)/6
weights(3) = (dx*23)/24
weights(N-2) = (dx*23)/24
case default
print *,"Error! Unrecognized quadrature_option:",quadrature_option
stop
end select
end select
! ***************************************************************
! Fill the interior of the differentiation matrices
! ***************************************************************
ddx=0d+0
d2dx2=0d+0
select case (option)
case (0,1,2,3)
! 2nd order (3 point stencil):
if (N<3) then
print *,"Error! N must be at least 3 for the 3-point stencil methods"
stop
end if
do i=2,N-1
ddx(i,i+1)=1/(2*dx)
ddx(i,i-1)=-1/(2*dx)
d2dx2(i,i+1)=1/(dx2)
d2dx2(i,i)=-2/(dx2)
d2dx2(i,i-1)=1/(dx2)
end do
case (10,11,12,13, 14, 15, 16)
! 4th order (5 point stencil):
if (N<5) then
print *,"Error! N must be at least 5 for the 5-point stencil methods"
stop
end if
do i=3,N-2
ddx(i,i+2)=-1/(6*2*dx)
ddx(i,i+1)=4/(3*2*dx)
ddx(i,i-1)=-4/(3*2*dx)
ddx(i,i-2)=1/(6*2*dx)
d2dx2(i,i+2)=-1/(12*dx2)
d2dx2(i,i+1)=4/(3*dx2)
d2dx2(i,i)=-5/(2*dx2)
d2dx2(i,i-1)=4/(3*dx2)
d2dx2(i,i-2)=-1/(12*dx2)
end do
case (30,31,32)
! 2-point stencil for D and 3-point stencil for DD,
! upwinding to the left.
if (N<3) then
print *,"Error! N must be at least 3 for this option."
stop
end if
do i=2,N
ddx(i,i) = 1/dx
ddx(i,i-1) = -1/dx
end do
do i=3,N
d2dx2(i,i) = 1/dx2
d2dx2(i,i-1) = -2/dx2
d2dx2(i,i-2) = 1/dx2
end do
case (40,41,42)
! 2-point stencil for D and 3-point stencil for DD,
! upwinding to the right.
if (N<3) then
print *,"Error! N must be at least 3 for this option."
stop
end if
do i=1,N-1
ddx(i,i) = -1/dx
ddx(i,i+1) = 1/dx
end do
do i=1,N-2
d2dx2(i,i) = 1/dx2
d2dx2(i,i+1) = -2/dx2
d2dx2(i,i+2) = 1/dx2
end do
case (50,51,52)
! 3-point stencil for both D and DD,
! upwinding to the left.
if (N<3) then
print *,"Error! N must be at least 3 for this option."
stop
end if
do i=3,N
ddx(i,i) = (1.5d+0)/dx
ddx(i,i-1) = -2/dx
ddx(i,i-2) = 1/(2*dx)
d2dx2(i,i) = 1/dx2
d2dx2(i,i-1) = -2/dx2
d2dx2(i,i-2) = 1/dx2
end do
case (60,61,62)
! 3-point stencil for both D and DD,
! upwinding to the right.
if (N<3) then
print *,"Error! N must be at least 3 for this option."
stop
end if
do i=1,N-2
ddx(i,i) = -(1.5d+0)/dx
ddx(i,i+1) = 2/dx
ddx(i,i+2) = -1/(2*dx)
d2dx2(i,i) = 1/dx2
d2dx2(i,i+1) = -2/dx2
d2dx2(i,i+2) = 1/dx2
end do
case (20,22)
! Create spectral differentiation matrices.
! Here I've translated the Matlab fourdif.m routine from the
! DMSuite package by S.C. Reddy and J.A.C. Weideman, available at
! http://www.mathworks.com/matlabcentral/fileexchange/29
! or here:
! http://dip.sun.ac.za/~weideman/research/differ.html
h = 2*pi/N;
n1 = floor((N-1.0)/2)
n2 = ceiling((N-1.0)/2)
allocate(topc(n2))
allocate(col1(N))
! Create first derivative matrix:
col1(1)=0d+0
if (mod(N,2)==0) then
topc = [( 0.5d+0/tan(i*h/2), i=1,n2 )]
col1(2:n2+1) = topc
col1(n2+2:) = -topc(n1:1:-1)
do i=2,N,2
col1(i) = -col1(i)
end do
col1 = 2*pi/(xMax-xMin)*col1
! Create a toeplitz matrix:
do i=1,N
ddx(i,i:) = -col1(1:N+1-i)
ddx(i,1:i-1) = col1(i:2:-1)
end do
else
topc = [( 0.5d+0 / sin(i*h/2), i=1,n2 )]
col1(2:n2+1) = topc
col1(n2+2:) = topc(n1:1:-1)
do i=2,N,2
col1(i) = -col1(i)
end do
col1 = 2*pi/(xMax-xMin)*col1
! Create a toeplitz matrix:
do i=1,N
ddx(i,i:) = -col1(1:N+1-i)
ddx(i,1:i-1) = col1(i:2:-1)
end do
end if
! Create second derivative matrix:
if (mod(N,2)==0) then
col1(1)=-pi*pi/(3*h*h)-1d+0/6
topc = [( -(0.5d+0)/(sin(i*h/2)**2), i=1,n2 )]
col1(2:n2+1) = topc
col1(n2+2:) = topc(n1:1:-1)
do i=2,N,2
col1(i) = -col1(i)
end do
col1 = (2*pi/(xMax-xMin))**2 *col1
! Create a toeplitz matrix:
do i=1,N
d2dx2(i,i:) = col1(1:N+1-i)
d2dx2(i,1:i-1) = col1(i:2:-1)
end do
else
col1(1)=-pi*pi/(3*h*h) + 1d+0/12
topc = [( -(0.5d+0) / (sin(i*h/2) * tan(i*h/2)), i=1,n2 )]
col1(2:n2+1) = topc
col1(n2+2:) = -topc(n1:1:-1)
do i=2,N,2
col1(i) = -col1(i)
end do
col1 = (2*pi/(xMax-xMin))**2 *col1
! Create a toeplitz matrix:
do i=1,N
d2dx2(i,i:) = col1(1:N+1-i)
d2dx2(i,1:i-1) = col1(i:2:-1)
end do
end if
deallocate(topc)
case (80,81)
! 4 point stencil (upwinding, with 1 point on 1 side, and 2 points on the other side.)
if (N<5) then
print *,"Error! N must be at least 5 for 4 point stencil"
stop
end if
do i=1,N
ddx(i,modulo(i,N)+1) = 1/(3*dx)
ddx(i,i) = 1/(2*dx)
ddx(i,modulo(i-2,N)+1) = -1/(dx)
ddx(i,modulo(i-3,N)+1) = 1/(6*dx)
d2dx2(i,modulo(i,N)+1) = 1/(dx2)
d2dx2(i,i) = -2/(dx2)
d2dx2(i,modulo(i-2,N)+1) = 1/(dx2)
end do
case (82)
! 4 point stencil (upwinding, with 1 point on 1 side, and 2 points on the other side.)
if (N<5) then
print *,"Error! N must be at least 5 for 4 point stencil"
stop
end if
do i=3,N-1
ddx(i,i+1) = 1/(3*dx)
ddx(i,i) = 1/(2*dx)
ddx(i,i-1) = -1/(dx)
ddx(i,i-2) = 1/(6*dx)
end do
do i=2,N-1
d2dx2(i,i+1) = 1/(dx2)
d2dx2(i,i) = -2/(dx2)
d2dx2(i,i-1) = 1/(dx2)
end do
case (90,91)
! 4 point stencil (upwinding, with 1 point on 1 side, and 2 points on the other side.)
if (N<5) then
print *,"Error! N must be at least 5 for 4 point stencil"
stop
end if
do i=1,N
ddx(i,modulo(i-2,N)+1) = -1/(3*dx)
ddx(i,i) = -1/(2*dx)
ddx(i,modulo(i,N)+1) = 1/(dx)
ddx(i,modulo(i+1,N)+1) = -1/(6*dx)
d2dx2(i,modulo(i,N)+1) = 1/(dx2)
d2dx2(i,i) = -2/(dx2)
d2dx2(i,modulo(i-2,N)+1) = 1/(dx2)
end do
case (92)
! 4 point stencil (upwinding, with 1 point on 1 side, and 2 points on the other side.)
if (N<5) then
print *,"Error! N must be at least 5 for 4 point stencil"
stop
end if
do i=2,N-2
ddx(i,i-1) = -1/(3*dx)
ddx(i,i) = -1/(2*dx)
ddx(i,i+1) = 1/(dx)
ddx(i,i+2) = -1/(6*dx)
end do
do i=2,N-1
d2dx2(i,i+1) = 1/(dx2)
d2dx2(i,i) = -2/(dx2)
d2dx2(i,i-1) = 1/(dx2)
end do
case (100,101)
! upwinding, with 1 point on 1 side, and 3 points on the other side.
if (N<5) then
print *,"Error! N must be at least 5 for option 100,101"
stop
end if
do i=1,N
ddx(i,modulo(i+0,N)+1) = 1/(4*dx)
ddx(i,i) = 5/(6*dx)
ddx(i,modulo(i-2,N)+1) = -3/(2*dx)
ddx(i,modulo(i-3,N)+1) = 1/(2*dx)
ddx(i,modulo(i-4,N)+1) = -1/(12*dx)
end do
case (102)
! upwinding, with 1 point on 1 side, and 3 points on the other side.
if (N<5) then
print *,"Error! N must be at least 5 for option 102"
stop
end if
do i=4,N-1
ddx(i,i+1) = 1/(4*dx)
ddx(i,i) = 5/(6*dx)
ddx(i,i-1) = -3/(2*dx)
ddx(i,i-2) = 1/(2*dx)
ddx(i,i-3) = -1/(12*dx)
end do
case (110,111)
! upwinding, with 1 point on 1 side, and 2 points on the other side.
if (N<5) then
print *,"Error! N must be at least 5 for option 110,111"
stop
end if
do i=1,N
ddx(i,modulo(i-2,N)+1) = -1/(4*dx)
ddx(i,i) = -5/(6*dx)
ddx(i,modulo(i+0,N)+1) = 3/(2*dx)
ddx(i,modulo(i+1,N)+1) = -1/(2*dx)
ddx(i,modulo(i+2,N)+1) = 1/(12*dx)
end do
case (112)
! upwinding, with 1 point on 1 side, and 2 points on the other side.
if (N<5) then
print *,"Error! N must be at least 5 for option 112"
stop
end if
do i=2,N-3
ddx(i,i-1) = -1/(4*dx)
ddx(i,i) = -5/(6*dx)
ddx(i,i+1) = 3/(2*dx)
ddx(i,i+2) = -1/(2*dx)
ddx(i,i+3) = 1/(12*dx)
end do
case (120,121,122,123)
! upwinding, with 2 points on 1 side, and 3 points on the other side.
if (N<5) then
print *,"Error! N must be at least 5 for option 120,121,122,123"
stop
end if
do i=1,N
ddx(i,modulo(i+1,N)+1) = -1/(20*dx)
ddx(i,modulo(i+0,N)+1) = 1/(2*dx)
ddx(i,i) = 1/(3*dx)
ddx(i,modulo(i-2,N)+1) = -1/(dx)
ddx(i,modulo(i-3,N)+1) = 1/(4*dx)
ddx(i,modulo(i-4,N)+1) = -1/(30*dx)
end do
case (130,131,132,133)
! upwinding, with 2 points on 1 side, and 3 points on the other side.
if (N<5) then
print *,"Error! N must be at least 5 for option 130,131,132,133"
stop
end if
do i=1,N
ddx(i,modulo(i-3,N)+1) = 1/(20*dx)
ddx(i,modulo(i-2,N)+1) = -1/(2*dx)
ddx(i,i) = -1/(3*dx)
ddx(i,modulo(i+0,N)+1) = 1/(dx)
ddx(i,modulo(i+1,N)+1) = -1/(4*dx)
ddx(i,modulo(i+2,N)+1) = 1/(30*dx)
end do
case (140,141)
! Fromm scheme4 point stencil (upwinding, with 1 point on 1 side, and 2 points on the other side.)
if (N<5) then
print *,"Error! N must be at least 5 for 4 point stencil"
stop
end if
do i=1,N
ddx(i,modulo(i,N)+1) = 1/(4*dx)
ddx(i,i) = 3/(4*dx)
ddx(i,modulo(i-2,N)+1) = -5/(4*dx)
ddx(i,modulo(i-3,N)+1) = 1/(4*dx)
d2dx2(i,modulo(i,N)+1) = 1/(dx2)
d2dx2(i,i) = -2/(dx2)
d2dx2(i,modulo(i-2,N)+1) = 1/(dx2)
end do
case (150,151)
! Fromm scheme: 4 point stencil (upwinding, with 1 point on 1 side, and 2 points on the other side.)
if (N<5) then
print *,"Error! N must be at least 5 for 4 point stencil"
stop
end if
do i=1,N
ddx(i,modulo(i-2,N)+1) = -1/(4*dx)
ddx(i,i) = -3/(4*dx)
ddx(i,modulo(i,N)+1) = 5/(4*dx)
ddx(i,modulo(i+1,N)+1) = -1/(4*dx)
d2dx2(i,modulo(i,N)+1) = 1/(dx2)
d2dx2(i,i) = -2/(dx2)
d2dx2(i,modulo(i-2,N)+1) = 1/(dx2)
end do
case (160,161)
! Stencils with 2 points on either side. Return the usual centered ddx, but instead of returning d2dx2, return (dx)^3*d^4/dx^4.
if (N<5) then
print *,"Error! N must be at least 5 for option 120,121,122,123"
stop
end if
do i=1,N
ddx(i,modulo(i+1,N)+1) = -1/(12*dx)
ddx(i,modulo(i+0,N)+1) = 2/(3*dx)
ddx(i,i) = 0
ddx(i,modulo(i-2,N)+1) = -2/(3*dx)
ddx(i,modulo(i-3,N)+1) = 1/(12*dx)
d2dx2(i,modulo(i+1,N)+1) = 1/(dx)
d2dx2(i,modulo(i+0,N)+1) = -4/(dx)
d2dx2(i,i) = 6/dx
d2dx2(i,modulo(i-2,N)+1) = -4/(dx)
d2dx2(i,modulo(i-3,N)+1) = 1/(dx)
end do
case (170,171)
! Stencils with 3 points on either side. Return the usual centered ddx, but instead of returning d2dx2, return (dx)^5*d^6/dx^6.
if (N<5) then
print *,"Error! N must be at least 7 for option 170,171"
stop
end if
do i=1,N
ddx(i,modulo(i+2,N)+1) = 1/(60*dx)
ddx(i,modulo(i+1,N)+1) = -3/(20*dx)
ddx(i,modulo(i+0,N)+1) = 3/(4*dx)
ddx(i,i) = 0
ddx(i,modulo(i-2,N)+1) = -3/(4*dx)
ddx(i,modulo(i-3,N)+1) = 3/(20*dx)
ddx(i,modulo(i-4,N)+1) = -1/(60*dx)
d2dx2(i,modulo(i+2,N)+1) = 1/(dx)
d2dx2(i,modulo(i+1,N)+1) = -6/(dx)
d2dx2(i,modulo(i+0,N)+1) = 15/(dx)
d2dx2(i,i) = -20/dx
d2dx2(i,modulo(i-2,N)+1) = 15/(dx)
d2dx2(i,modulo(i-3,N)+1) = -6/(dx)
d2dx2(i,modulo(i-4,N)+1) = 1/(dx)
end do
end select
! ***************************************************************
! Handle endpoints of grid in differentiation matrices
! ***************************************************************
select case (option)
case (0,1)
ddx(1,N) = -1/(2*dx)
ddx(1,2) = 1/(2*dx)
ddx(N,1) = 1/(2*dx)
ddx(N,N-1) = -1/(2*dx)
d2dx2(1,1) = -2/dx2
d2dx2(N,N) = -2/dx2
d2dx2(1,N) = 1/dx2
d2dx2(1,2) = 1/dx2
d2dx2(N,1) = 1/dx2
d2dx2(N,N-1) = 1/dx2
case (2)
! 3-point stencil, aperiodic
ddx(1,1)=-1.5/dx
ddx(1,2)=2/dx
ddx(1,3)=-0.5/dx
ddx(N,N)=1.5/dx
ddx(N,N-1)=-2/dx
ddx(N,N-2)=0.5/dx
d2dx2(1,1)=1/dx2
d2dx2(1,2)=-2/dx2
d2dx2(1,3)=1/dx2
d2dx2(N,N)=1/dx2
d2dx2(N,N-1)=-2/dx2
d2dx2(N,N-2)=1/dx2
case (3)
! Aperiodic.
! 2-point stencil for the first and last rows of the first
! differentiation matrix, so the matrix is strictly tri-diagonal.
! The 2nd derivative matrix is the same as for option=0 (i.e. not
! strictly tri-diagonal) since it is not possible to approximate
! the 2nd derivative with a 2-point stencil.
ddx(1,1)=-1/dx
ddx(1,2)=1/dx
ddx(N,N)=1/dx
ddx(N,N-1)=-1/dx
d2dx2(1,1)=1/dx2
d2dx2(1,2)=-2/dx2
d2dx2(1,3)=1/dx2
d2dx2(N,N)=1/dx2
d2dx2(N,N-1)=-2/dx2
d2dx2(N,N-2)=1/dx2
case (10,11)
! 5-point stencil, periodic
ddx(1, N) = -(4.0d+0/3)/(2*dx)
ddx(1, N-1) = (1.0d+0/6)/(2*dx)
ddx(2, N) = (1.0d+0/6)/(2*dx)
ddx(N, 1) = (4.0d+0/3)/(2*dx)
ddx(N, 2) = -(1.0d+0/6)/(2*dx)
ddx(N-1, 1) = -(1.0d+0/6)/(2*dx)
d2dx2(1, N) = (4d+0/3)/dx2
d2dx2(1, N-1) = -(1d+0/12)/dx2
d2dx2(2, N) = -(1d+0/12)/dx2
d2dx2(N, 1) = (4d+0/3)/dx2
d2dx2(N, 2) = -(1d+0/12)/dx2
d2dx2(N-1, 1) = -(1d+0/12)/dx2
! i=1
ddx(1,1+2)=-1/(6*2*dx)
ddx(1,1+1)=4/(3*2*dx)
d2dx2(1,1+2)=-1/(12*dx2)
d2dx2(1,1+1)=4/(3*dx2)
d2dx2(1,1)=-5/(2*dx2)
! i=2
ddx(2,2+2)=-1/(6*2*dx)
ddx(2,2+1)=4/(3*2*dx)
ddx(2,2-1)=-4/(3*2*dx)
d2dx2(2,2+2)=-1/(12*dx2)
d2dx2(2,2+1)=4/(3*dx2)
d2dx2(2,2)=-5/(2*dx2)
d2dx2(2,2-1)=4/(3*dx2)
! i=N
ddx(N,N-1)=-4/(3*2*dx)
ddx(N,N-2)=1/(6*2*dx)
d2dx2(N,N)=-5/(2*dx2)
d2dx2(N,N-1)=4/(3*dx2)
d2dx2(N,N-2)=-1/(12*dx2)
! i=N-1
ddx(N-1,N-1+1)=4/(3*2*dx)
ddx(N-1,N-1-1)=-4/(3*2*dx)
ddx(N-1,N-1-2)=1/(6*2*dx)
d2dx2(N-1,N-1+1)=4/(3*dx2)
d2dx2(N-1,N-1)=-5/(2*dx2)
d2dx2(N-1,N-1-1)=4/(3*dx2)
d2dx2(N-1,N-1-2)=-1/(12*dx2)
case (12)
! 5 point stencil, aperiodic:
ddx(1,1)= -25/(12*dx)
ddx(1,2)= 4/(dx)
ddx(1,3)=-3/dx
ddx(1,4)=4/(3*dx)
ddx(1,5)=-1/(4*dx)
ddx(2,1)= -1/(4*dx)
ddx(2,2)= -5/(6*dx)
ddx(2,3)=3/(2*dx)
ddx(2,4)=-1/(2*dx)
ddx(2,5)=1/(12*dx)
ddx(N,N)= 25/(12*dx)
ddx(N,N-1)= -4/(dx)
ddx(N,N-2)=3/dx
ddx(N,N-3)=-4/(3*dx)
ddx(N,N-4)=1/(4*dx)
ddx(N-1,N)= 1/(4*dx)
ddx(N-1,N-1)= 5/(6*dx)
ddx(N-1,N-2)=-3/(2*dx)
ddx(N-1,N-3)=1/(2*dx)
ddx(N-1,N-4)=-1/(12*dx)
d2dx2(1,1)=35/(12*dx2)
d2dx2(1,2)=-26/(3*dx2)
d2dx2(1,3)=19/(2*dx2)
d2dx2(1,4)=-14/(3*dx2)
d2dx2(1,5)=11/(12*dx2)
d2dx2(2,1)=11/(12*dx2)
d2dx2(2,2)=-5/(3*dx2)
d2dx2(2,3)=1/(2*dx2)
d2dx2(2,4)=1/(3*dx2)
d2dx2(2,5)=-1/(12*dx2)
d2dx2(N,N)=35/(12*dx2)
d2dx2(N,N-1)=-26/(3*dx2)
d2dx2(N,N-2)=19/(2*dx2)
d2dx2(N,N-3)=-14/(3*dx2)
d2dx2(N,N-4)=11/(12*dx2)
d2dx2(N-1,N-0)=11/(12*dx2)
d2dx2(N-1,N-1)=-5/(3*dx2)
d2dx2(N-1,N-2)=1/(2*dx2)
d2dx2(N-1,N-3)=1/(3*dx2)
d2dx2(N-1,N-4)=-1/(12*dx2)
case (13)
! Aperiodic.
! 3-point stencil for the first and last rows of the
! differentiation matrices, and 4-point stencil for the 2nd and
! penultimate rows of the differentiation matrices, so the matrices
! are strictly penta-diagonal.
ddx(1,1)=-1.5/dx
ddx(1,2)=2/dx
ddx(1,3)=-0.5/dx
ddx(N,N)=1.5/dx
ddx(N,N-1)=-2/dx
ddx(N,N-2)=0.5/dx
ddx(2,1)=-1/(3*dx)
ddx(2,2)=-1/(2*dx)
ddx(2,3)=1/(dx)
ddx(2,4)=-1/(6*dx)
ddx(N-1,N-0)=1/(3*dx)
ddx(N-1,N-1)=1/(2*dx)
ddx(N-1,N-2)=-1/(dx)
ddx(N-1,N-3)=1/(6*dx)
d2dx2(1,1)=1/dx2
d2dx2(1,2)=-2/dx2
d2dx2(1,3)=1/dx2
d2dx2(N,N)=1/dx2
d2dx2(N,N-1)=-2/dx2
d2dx2(N,N-2)=1/dx2
! It turns out that the 4-point stencil for the second derivative
! has a weight of 0 for the most distant point, making it identical
! to the 3-point stencil:
d2dx2(2,1)=1/(dx2)
d2dx2(2,2)=-2/(dx2)
d2dx2(2,3)=1/(dx2)
d2dx2(2,4)=0
d2dx2(N-1,N-0)=1/(dx2)
d2dx2(N-1,N-1)=-2/(dx2)
d2dx2(N-1,N-2)=1/(dx2)
d2dx2(N-1,N-3)=0
case (14)
! 5 point stencil, aperiodic:
! Leave 1st row 0: there is no way to avoid upwinding.
! For 2nd row, use centered differences:
ddx(2,1)= -1/(2*dx)
ddx(2,3)= 1/(2*dx)
ddx(N,N)= 25/(12*dx)
ddx(N,N-1)= -4/(dx)
ddx(N,N-2)=3/dx
ddx(N,N-3)=-4/(3*dx)
ddx(N,N-4)=1/(4*dx)
ddx(N-1,N)= 1/(4*dx)
ddx(N-1,N-1)= 5/(6*dx)
ddx(N-1,N-2)=-3/(2*dx)
ddx(N-1,N-3)=1/(2*dx)
ddx(N-1,N-4)=-1/(12*dx)
d2dx2(1,1)=35/(12*dx2)
d2dx2(1,2)=-26/(3*dx2)
d2dx2(1,3)=19/(2*dx2)
d2dx2(1,4)=-14/(3*dx2)
d2dx2(1,5)=11/(12*dx2)
d2dx2(2,1)=11/(12*dx2)
d2dx2(2,2)=-5/(3*dx2)
d2dx2(2,3)=1/(2*dx2)
d2dx2(2,4)=1/(3*dx2)
d2dx2(2,5)=-1/(12*dx2)
d2dx2(N,N)=35/(12*dx2)
d2dx2(N,N-1)=-26/(3*dx2)
d2dx2(N,N-2)=19/(2*dx2)
d2dx2(N,N-3)=-14/(3*dx2)
d2dx2(N,N-4)=11/(12*dx2)
d2dx2(N-1,N-0)=11/(12*dx2)
d2dx2(N-1,N-1)=-5/(3*dx2)
d2dx2(N-1,N-2)=1/(2*dx2)
d2dx2(N-1,N-3)=1/(3*dx2)
d2dx2(N-1,N-4)=-1/(12*dx2)
case (15)
! 5 point stencil, aperiodic:
ddx(1,1)= -25/(12*dx)
ddx(1,2)= 4/(dx)
ddx(1,3)=-3/dx
ddx(1,4)=4/(3*dx)
ddx(1,5)=-1/(4*dx)
ddx(2,1)= -1/(4*dx)
ddx(2,2)= -5/(6*dx)
ddx(2,3)=3/(2*dx)
ddx(2,4)=-1/(2*dx)
ddx(2,5)=1/(12*dx)
! Penultimate row: use centered differences:
ddx(N-1,N) = 1/(2*dx)
ddx(N-1,N-2) = -1/(2*dx)
! Leave last row of ddx 0: there is no way to avoid upwinding.
d2dx2(1,1)=35/(12*dx2)
d2dx2(1,2)=-26/(3*dx2)
d2dx2(1,3)=19/(2*dx2)
d2dx2(1,4)=-14/(3*dx2)
d2dx2(1,5)=11/(12*dx2)
d2dx2(2,1)=11/(12*dx2)
d2dx2(2,2)=-5/(3*dx2)
d2dx2(2,3)=1/(2*dx2)
d2dx2(2,4)=1/(3*dx2)
d2dx2(2,5)=-1/(12*dx2)
d2dx2(N,N)=35/(12*dx2)
d2dx2(N,N-1)=-26/(3*dx2)
d2dx2(N,N-2)=19/(2*dx2)
d2dx2(N,N-3)=-14/(3*dx2)
d2dx2(N,N-4)=11/(12*dx2)
d2dx2(N-1,N-0)=11/(12*dx2)
d2dx2(N-1,N-1)=-5/(3*dx2)
d2dx2(N-1,N-2)=1/(2*dx2)
d2dx2(N-1,N-3)=1/(3*dx2)
d2dx2(N-1,N-4)=-1/(12*dx2)
case (16)
! 5 point stencil, aperiodic:
ddx(2,1)= -1/(2*dx)
ddx(2,3)= 1/(2*dx)
ddx(N-1,N) = 1/(2*dx)
ddx(N-1,N-2)=-1/(2*dx)
d2dx2(1,1)=35/(12*dx2)
d2dx2(1,2)=-26/(3*dx2)
d2dx2(1,3)=19/(2*dx2)
d2dx2(1,4)=-14/(3*dx2)
d2dx2(1,5)=11/(12*dx2)
d2dx2(2,1)=11/(12*dx2)
d2dx2(2,2)=-5/(3*dx2)
d2dx2(2,3)=1/(2*dx2)
d2dx2(2,4)=1/(3*dx2)
d2dx2(2,5)=-1/(12*dx2)
d2dx2(N,N)=35/(12*dx2)
d2dx2(N,N-1)=-26/(3*dx2)
d2dx2(N,N-2)=19/(2*dx2)
d2dx2(N,N-3)=-14/(3*dx2)
d2dx2(N,N-4)=11/(12*dx2)