forked from mabarnes/stella
-
Notifications
You must be signed in to change notification settings - Fork 0
/
finite_differences.f90
1234 lines (951 loc) · 33.5 KB
/
finite_differences.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
module finite_differences
implicit none
public :: first_order_upwind
public :: third_order_upwind
public :: fifth_order_upwind
public :: third_order_upwind_zed
public :: first_order_upwind_zed
public :: second_order_centered
public :: fourth_order_centered
public :: second_order_centered_zed
public :: four_point_triangle
public :: fd3pt, fd5pt
public :: d2_3pt
public :: fd_variable_upwinding_vpa
public :: fd_variable_upwinding_zed
public :: fd_cell_centres_zed, cell_centres_zed
interface fd3pt
module procedure fd3pt_real
module procedure fd3pt_real_array
module procedure fd3pt_complex_array
end interface
interface fd5pt
module procedure fd5pt_real
module procedure fd5pt_array
end interface
interface first_order_upwind
module procedure first_order_upwind_real
module procedure first_order_upwind_complex
end interface
interface third_order_upwind
module procedure third_order_upwind_complex
module procedure third_order_upwind_real
end interface
interface fifth_order_upwind
module procedure fifth_order_upwind_complex
module procedure fifth_order_upwind_real
end interface
interface tridag
module procedure tridag_real
module procedure tridag_complex
end interface
interface second_order_centered
module procedure second_order_centered_real
module procedure second_order_centered_complex
end interface
interface four_point_triangle
module procedure four_point_triangle_real
module procedure four_point_triangle_complex
end interface
interface fourth_order_centered
module procedure fourth_order_centered_real
module procedure fourth_order_centered_complex
end interface
interface second_order_centered_zed
module procedure second_order_centered_zed_real
module procedure second_order_centered_zed_complex
end interface
interface d2_3pt
module procedure d2_3pt_real
module procedure d2_3pt_complex
end interface
contains
subroutine first_order_upwind_real (llim, f, del, sgn, df)
implicit none
integer, intent (in) :: llim
real, dimension (llim:), intent (in) :: f
real, intent (in) :: del
integer, intent (in) :: sgn
real, dimension (llim:), intent (out) :: df
integer :: i, n, istart, iend
n = size(f)
if (sgn == -1) then
istart = llim
iend = llim+n-1
else
istart = llim+n-1
iend = llim
end if
! zero BC, 1st order accurate upwind
df(istart) = -f(istart)*sgn/del
do i = istart-sgn, iend, -sgn
df(i) = sgn*(f(i+sgn)-f(i))/del
end do
end subroutine first_order_upwind_real
subroutine first_order_upwind_complex (llim, f, del, sgn, df)
implicit none
integer, intent (in) :: llim
complex, dimension (llim:), intent (in) :: f
real, intent (in) :: del
integer, intent (in) :: sgn
complex, dimension (llim:), intent (out) :: df
integer :: i, n, istart, iend
n = size(f)
if (sgn == -1) then
istart = llim
iend = llim+n-1
else
istart = llim+n-1
iend = llim
end if
! zero BC, 1st order accurate upwind
df(istart) = -f(istart)*sgn/del
do i = istart-sgn, iend, -sgn
df(i) = sgn*(f(i+sgn)-f(i))/del
end do
end subroutine first_order_upwind_complex
subroutine third_order_upwind_complex (llim, f, del, sgn, df)
implicit none
integer, intent (in) :: llim
complex, dimension (llim:), intent (in) :: f
real, intent (in) :: del
integer, intent (in) :: sgn
complex, dimension (llim:), intent (out) :: df
integer :: i, n, istart, iend
n = size(f)
if (sgn == -1) then
istart = llim
iend = llim+n-1
else
istart = llim+n-1
iend = llim
end if
i = istart-sgn
! zero BC, 1st order accurate upwind
df(istart) = -f(istart)*sgn/del
! zero BC, 3rd order accurate upwind
df(i) = -sgn*(2.*f(i-sgn)+3.*f(i)-6.*f(i+sgn))/(6.*del)
! 1st order accurate upwind
df(iend) = sgn*(f(iend+sgn)-f(iend))/del
! 3rd order accurate upwind
do i = istart-2*sgn, iend+sgn, -sgn
df(i) = -sgn*(2.*f(i-sgn)+3*f(i)-6.*f(i+sgn)+f(i+2*sgn))/(6.*del)
end do
end subroutine third_order_upwind_complex
subroutine third_order_upwind_real (llim, f, del, sgn, df)
implicit none
integer, intent (in) :: llim
real, dimension (llim:), intent (in) :: f
real, intent (in) :: del
integer, intent (in) :: sgn
real, dimension (llim:), intent (out) :: df
integer :: i, n, istart, iend
n = size(f)
if (sgn == -1) then
istart = llim
iend = llim+n-1
else
istart = llim+n-1
iend = llim
end if
i = istart-sgn
! zero BC, 1st order accurate upwind
df(istart) = -f(istart)*sgn/del
! zero BC, 3rd order accurate upwind
df(i) = -sgn*(2.*f(i-sgn)+3.*f(i)-6.*f(i+sgn))/(6.*del)
! 1st order accurate upwind
df(iend) = sgn*(f(iend+sgn)-f(iend))/del
! 3rd order accurate upwind
do i = istart-2*sgn, iend+sgn, -sgn
df(i) = -sgn*(2.*f(i-sgn)+3*f(i)-6.*f(i+sgn)+f(i+2*sgn))/(6.*del)
end do
end subroutine third_order_upwind_real
subroutine fifth_order_upwind_complex (llim, f, del, sgn, df)
implicit none
integer, intent (in) :: llim
complex, dimension (llim:), intent (in) :: f
real, intent (in) :: del
integer, intent (in) :: sgn
complex, dimension (llim:), intent (out) :: df
integer :: i, n, istart, iend
n = size(f)
if (sgn == -1) then
istart = llim
iend = llim+n-1
else
istart = llim+n-1
iend = llim
end if
! zero BC, 1st order accurate upwind
df(istart) = -f(istart)*sgn/del
! zero BC, 3rd order accurate upwind
i = istart-sgn
df(i) = -sgn*(2.*f(i-sgn)+3.*f(i)-6.*f(i+sgn))/(6.*del)
! zero BC, 5th order accurate upwind
i = istart-2*sgn
df(i) = -sgn*(-3.*f(i-2*sgn)+30.*f(i-sgn)+20.*f(i)-60.*f(i+sgn)+15.*f(i+2*sgn))/(60.*del)
! 1st order accurate upwind
df(iend) = sgn*(f(iend+sgn)-f(iend))/del
! 3rd order accurate upwind
df(iend+sgn) = -sgn*(2.*f(iend)+3*f(iend+sgn)-6.*f(iend+2*sgn)+f(iend+3*sgn))/(6.*del)
! 5th order accurate upwind
do i = istart-3*sgn, iend+2*sgn, -sgn
df(i) = -sgn*(-3.*f(i-2*sgn)+30.*f(i-sgn)+20.*f(i)-60.*f(i+sgn)+15.*f(i+2*sgn)-2.*f(i+3*sgn)) &
/(60.*del)
end do
end subroutine fifth_order_upwind_complex
subroutine fifth_order_upwind_real (llim, f, del, sgn, df)
implicit none
integer, intent (in) :: llim
real, dimension (llim:), intent (in) :: f
real, intent (in) :: del
integer, intent (in) :: sgn
real, dimension (llim:), intent (out) :: df
integer :: i, n, istart, iend
n = size(f)
if (sgn == -1) then
istart = llim
iend = llim+n-1
else
istart = llim+n-1
iend = llim
end if
! zero BC, 1st order accurate upwind
df(istart) = -f(istart)*sgn/del
! zero BC, 3rd order accurate upwind
i = istart-sgn
df(i) = -sgn*(2.*f(i-sgn)+3.*f(i)-6.*f(i+sgn))/(6.*del)
! zero BC, 5th order accurate upwind
i = istart-2*sgn
df(i) = -sgn*(-3.*f(i-2*sgn)+30.*f(i-sgn)+20.*f(i)-60.*f(i+sgn)+15.*f(i+2*sgn))/(60.*del)
! 1st order accurate upwind
df(iend) = -sgn*(f(iend)-f(iend+sgn))/del
! 3rd order accurate upwind
df(iend+sgn) = -sgn*(2.*f(iend)+3*f(iend+sgn)-6.*f(iend+2*sgn)+f(iend+3*sgn))/(6.*del)
! 5th order accurate upwind
do i = istart-3*sgn, iend+2*sgn, -sgn
df(i) = -sgn*(-3.*f(i-2*sgn)+30.*f(i-sgn)+20.*f(i)-60.*f(i+sgn)+15.*f(i+2*sgn)-2.*f(i+3*sgn)) &
/(60.*del)
end do
end subroutine fifth_order_upwind_real
subroutine third_order_upwind_zed (llim, iseg, nseg, f, del, sgn, fl, fr, periodic, df)
implicit none
integer, intent (in) :: llim, iseg, nseg
complex, dimension (llim:), intent (in) :: f
real, intent (in) :: del
integer, intent (in) :: sgn
logical, intent (in) :: periodic
complex, dimension (:), intent (in) :: fl, fr
complex, dimension (llim:), intent (out) :: df
integer :: i, istart, iend, ulim
ulim = size(f)+llim-1
! if sgn > 0, then stream speed is negative
! so sweep from more positive to more negative zed
if (sgn > 0) then
if (iseg == nseg .and..not.periodic) then
i = ulim
df(i) = -f(i)/del
i = ulim-1
df(i) = -(2.*f(i-1)+3.*f(i)-6.*f(i+1))/(6.*del)
else
i = ulim
df(i) = -(2.*f(i-1)+3.*f(i)-6.*fr(1)+fr(2))/(6.*del)
i = ulim-1
df(i) = -(2.*f(i-1)+3.*f(i)-6.*f(i+1)+fr(1))/(6.*del)
end if
if (iseg == 1.and..not.periodic) then
i = llim
df(i) = (f(i+1)-f(i))/del
else
i = llim
df(i) = -(2.*fl(2)+3*f(i)-6.*f(i+1)+f(i+2))/(6.*del)
end if
istart = ulim
iend = llim
else
if (iseg == 1.and..not.periodic) then
i = llim
df(i) = f(i)/del
i = llim+1
df(i) = (2.*f(i+1)+3.*f(i)-6.*f(i-1))/(6.*del)
else
i = llim
df(i) = (2.*f(i+1)+3*f(i)-6.*fl(2)+fl(1))/(6.*del)
i = llim+1
df(i) = (2.*f(i+1)+3*f(i)-6.*f(i-1)+fl(2))/(6.*del)
end if
if (iseg == nseg.and..not.periodic) then
i = ulim
df(i) = (f(i)-f(i-1))/del
else
i = ulim
df(i) = (2.*fr(1)+3*f(i)-6.*f(i-1)+f(i-2))/(6.*del)
end if
istart = llim
iend = ulim
end if
! 3rd order accurate upwind
do i = istart-2*sgn, iend+sgn, -sgn
df(i) = -sgn*(2.*f(i-sgn)+3*f(i)-6.*f(i+sgn)+f(i+2*sgn))/(6.*del)
end do
end subroutine third_order_upwind_zed
subroutine first_order_upwind_zed (llim, iseg, nseg, f, del, sgn, fl, fr, periodic, df)
implicit none
integer, intent (in) :: llim, iseg, nseg
complex, dimension (llim:), intent (in) :: f
real, intent (in) :: del
integer, intent (in) :: sgn
logical, intent (in) :: periodic
complex, dimension (:), intent (in) :: fl, fr
complex, dimension (llim:), intent (out) :: df
integer :: i, istart, iend, ulim
ulim = size(f)+llim-1
! if sgn > 0, then stream speed is negative
! so sweep from more positive to more negative zed
if (sgn > 0) then
if (iseg == nseg.and..not.periodic) then
i = ulim
df(i) = -f(i)/del
i = ulim-1
df(i) = (f(i+1)-f(i))/del
else
i = ulim
df(i) = (fr(1)-f(i))/del
i = ulim-1
df(i) = (f(i+1)-f(i))/del
end if
i = llim
df(i) = (f(i+1)-f(i))/del
istart = ulim
iend = llim
else
if (iseg == 1.and..not.periodic) then
i = llim
df(i) = f(i)/del
i = llim+1
df(i) = (f(i)-f(i-1))/del
else
i = llim
df(i) = (f(i)-fl(2))/del
i = llim+1
df(i) = (f(i)-f(i-1))/del
end if
i = ulim
df(i) = (f(i)-f(i-1))/del
istart = llim
iend = ulim
end if
! 3rd order accurate upwind
do i = istart-2*sgn, iend+sgn, -sgn
df(i) = sgn*(f(i+sgn)-f(i))/del
end do
end subroutine first_order_upwind_zed
subroutine second_order_centered_real (llim, f, del, df)
implicit none
integer, intent (in) :: llim
real, dimension (llim:), intent (in) :: f
real, intent (in) :: del
real, dimension (llim:), intent (out) :: df
integer :: i, n, istart, iend
n = size(f)
istart = llim
iend = llim+n-1
! zero BC
df(istart) = f(istart+1) / (2.*del)
df(iend) =-f(iend-1) / (2.*del)
! 2nd order accurate centered
do i = istart+1, iend-1
df(i) = (f(i+1) - f(i-1)) / (2.*del)
end do
end subroutine second_order_centered_real
subroutine second_order_centered_complex (llim, f, del, df)
implicit none
integer, intent (in) :: llim
complex, dimension (llim:), intent (in) :: f
real, intent (in) :: del
complex, dimension (llim:), intent (out) :: df
integer :: i, n, istart, iend
n = size(f)
istart = llim
iend = llim+n-1
! zero BC
df(istart) = f(istart+1) / (2.*del)
df(iend) =-f(iend-1) / (2.*del)
! 2nd order accurate centered
do i = istart+1, iend-1
df(i) = (f(i+1) - f(i-1)) / (2.*del)
end do
end subroutine second_order_centered_complex
subroutine four_point_triangle_real (llim, f, del, df)
implicit none
integer, intent (in) :: llim
real, dimension (llim:), intent (in) :: f
real, intent (in) :: del
real, dimension (llim:), intent (out) :: df
integer :: i, n, istart, iend
n = size(f)
istart = llim
iend = llim+n-1
! zero BC
i=istart
df(i) = f(i+1) / (2.0*del)
i=istart+1
df(i) = (f(i+1)-f(i-1)) / (2.0*del)
i=istart+2
df(i) = (-2.*f(i+3)+9.*f(i+1)-9.*f(i-1)) / (18.0*del)
i=iend
df(i) = -f(i-1) / (2.0*del)
i=iend-1
df(i) = (f(i+1)-f(i-1)) / (2.0*del)
i=iend-2
df(i) = (9.*f(i+1)-9.*f(i-1)+2.*f(i-3)) / (18.0*del)
! 2nd order accurate centered
do i = istart+3, iend-3
df(i) = (-2.*f(i+3)+9.*f(i+1)-9.*f(i-1)+2.*f(i-3)) / (18.0*del)
end do
end subroutine four_point_triangle_real
subroutine four_point_triangle_complex (llim, f, del, df)
implicit none
integer, intent (in) :: llim
complex, dimension (llim:), intent (in) :: f
real, intent (in) :: del
complex, dimension (llim:), intent (out) :: df
integer :: i, n, istart, iend
n = size(f)
istart = llim
iend = llim+n-1
! zero BC
i=istart
df(i) = f(i+1) / (2.0*del)
i=istart+1
df(i) = (f(i+1)-f(i-1)) / (2.0*del)
i=istart+2
df(i) = (-2.*f(i+3)+9.*f(i+1)-9.*f(i-1)) / (18.0*del)
i=iend
df(i) = -f(i-1) / (2.0*del)
i=iend-1
df(i) = (f(i+1)-f(i-1)) / (2.0*del)
i=iend-2
df(i) = (9.*f(i+1)-9.*f(i-1)+2.*f(i-3)) / (18.0*del)
! 2nd order accurate centered
do i = istart+3, iend-3
df(i) = (-2.*f(i+3)+9.*f(i+1)-9.*f(i-1)+2.*f(i-3)) / (18.0*del)
end do
end subroutine four_point_triangle_complex
subroutine fourth_order_centered_real (llim, f, del, df)
implicit none
integer, intent (in) :: llim
real, dimension (llim:), intent (in) :: f
real, intent (in) :: del
real, dimension (llim:), intent (out) :: df
integer :: i, n, istart, iend
n = size(f)
istart = llim
iend = llim+n-1
! zero BC
! 2nd order accurate centered
df(istart) = f(istart + 1) / (2.*del)
df(iend) =-f(iend - 1) / (2.*del)
! 4th order accurate centered
df(istart+1) = (f(istart+3) - 8.*f(istart+2) + 8.*f(istart)) / (12.*del)
df(iend-1) = (-8*f(iend) + 8.*f(iend-2) - f(iend-3)) / (12.*del)
! 4th order accurate centered
do i = istart+2, iend-2
df(i) = (f(i+2)-8.*f(i+1) + 8.*f(i-1)-f(i-2)) / (12.*del)
end do
end subroutine fourth_order_centered_real
subroutine fourth_order_centered_complex (llim, f, del, df)
implicit none
integer, intent (in) :: llim
complex, dimension (llim:), intent (in) :: f
real, intent (in) :: del
complex, dimension (llim:), intent (out) :: df
integer :: i, n, istart, iend
n = size(f)
istart = llim
iend = llim+n-1
! zero BC
! 2nd order accurate centered
df(istart) = f(istart + 1) / (2.*del)
df(iend) =-f(iend - 1) / (2.*del)
! 4th order accurate centered
df(istart+1) = (f(istart+3) - 8.*f(istart+2) + 8.*f(istart))/ (12.*del)
df(iend-1) = (-8*f(iend) + 8.*f(iend-2) - f(iend-3)) / (12.*del)
! 4th order accurate centered
do i = istart+2, iend-2
df(i) = (f(i+2)-8.*f(i+1) + 8.*f(i-1)-f(i-2)) / (12.*del)
end do
end subroutine fourth_order_centered_complex
subroutine second_order_centered_zed_real (llim, iseg, nseg, f, del, sgn, fl, fr, periodic, df)
implicit none
integer, intent (in) :: llim, iseg, nseg
real, dimension (llim:), intent (in) :: f
integer, intent (in) :: sgn
real, intent (in) :: del
real, dimension (:), intent (in) :: fl, fr
logical, intent (in) :: periodic
real, dimension (llim:), intent (out) :: df
integer :: i, ulim
ulim = size(f)+llim-1
i = llim
if (iseg == 1 .and. sgn>0 .and..not.periodic) then
! sgn > 0 corresponds to negative advection speed
! upwind at boundary requires taking information from right
df(i) = (f(i+1)-f(i))/del
else
df(i) = 0.5*(f(i+1)-fl(2))/del
end if
i = ulim
if (iseg == nseg .and. sgn<0 .and..not.periodic) then
! sgn < 0 corresponds to positive advection speed
! upwind at boundary requires taking information from left
df(i) = (f(i)-f(i-1))/del
else
df(i) = 0.5*(fr(1)-f(i-1))/del
end if
do i = llim+1, ulim-1
df(i) = 0.5*(f(i+1)-f(i-1))/del
end do
end subroutine second_order_centered_zed_real
subroutine second_order_centered_zed_complex (llim, iseg, nseg, f, del, sgn, fl, fr, periodic, df)
implicit none
integer, intent (in) :: llim, iseg, nseg
complex, dimension (llim:), intent (in) :: f
integer, intent (in) :: sgn
real, intent (in) :: del
complex, dimension (:), intent (in) :: fl, fr
logical, intent (in) :: periodic
complex, dimension (llim:), intent (out) :: df
integer :: i, ulim
ulim = size(f)+llim-1
i = llim
if (iseg == 1 .and. sgn>0 .and..not.periodic) then
! sgn > 0 corresponds to negative advection speed
! upwind at boundary requires taking information from right
df(i) = (f(i+1)-f(i))/del
else
df(i) = 0.5*(f(i+1)-fl(2))/del
end if
i = ulim
if (iseg == nseg .and. sgn<0 .and..not.periodic) then
! sgn < 0 corresponds to positive advection speed
! upwind at boundary requires taking information from left
df(i) = (f(i)-f(i-1))/del
else
df(i) = 0.5*(fr(1)-f(i-1))/del
end if
do i = llim+1, ulim-1
df(i) = 0.5*(f(i+1)-f(i-1))/del
end do
end subroutine second_order_centered_zed_complex
subroutine second_order_centered_vpa (llim, f, del, df)
implicit none
integer, intent (in) :: llim
complex, dimension (llim:), intent (in) :: f
real, intent (in) :: del
complex, dimension (llim:), intent (out) :: df
integer :: i, ulim
ulim = size(f)+llim-1
i = llim
df(i) = 0.5*f(i+1)/del
i = ulim
df(i) = -0.5*f(i-1)/del
do i = llim+1, ulim-1
df(i) = 0.5*(f(i+1)-f(i-1))/del
end do
end subroutine second_order_centered_vpa
subroutine fd_cell_centres_zed (llim, f, del, sgn, fl, fr, df)
implicit none
integer, intent (in) :: llim
complex, dimension (llim:), intent (in) :: f
real, intent (in) :: del
integer, intent (in) :: sgn
complex, intent (in) :: fl, fr
complex, dimension (llim:), intent (out) :: df
integer :: i, ulim
ulim = size(f)+llim-1
if (sgn > 0) then
! if sgn > 0, then stream speed is negative
! so sweep from more positive to more negative zed
i = ulim
df(i) = (fr-f(i))/del
do i = ulim-1, llim, -1
df(i) = (f(i+1)-f(i))/del
end do
else
! if sgn < 0, then stream speed is positive
! so sweep from more negative to more positive zed
i = llim
df(i) = (f(i)-fl)/del
do i = llim+1, ulim
df(i) = (f(i)-f(i-1))/del
end do
end if
end subroutine fd_cell_centres_zed
! cell_centres_zed takes f at z grid locations
! and returns f at cell centres
! (with possible offset due to upwinding)
subroutine cell_centres_zed (llim, f, upwnd, sgn, fl, fr, fc)
implicit none
integer, intent (in) :: llim
complex, dimension (llim:), intent (in) :: f
real, intent (in) :: upwnd
integer, intent (in) :: sgn
complex, intent (in) :: fl, fr
complex, dimension (llim:), intent (out) :: fc
integer :: i, ulim
ulim = size(f)+llim-1
if (sgn > 0) then
! if sgn > 0, then stream speed is negative
! so sweep from more positive to more negative zed
i = ulim
fc(i) = 0.5*((1.-upwnd)*fr + (1.+upwnd)*f(i))
do i = ulim-1, llim, -1
fc(i) = 0.5*((1.-upwnd)*f(i+1) + (1.+upwnd)*f(i))
end do
else
! if sgn < 0, then stream speed is positive
! so sweep from more negative to more positive zed
i = llim
fc(i) = 0.5*((1.+upwnd)*f(i)+(1.-upwnd)*fl)
do i = llim+1, ulim
fc(i) = 0.5*((1.+upwnd)*f(i)+(1.-upwnd)*f(i-1))
end do
end if
end subroutine cell_centres_zed
subroutine fd_variable_upwinding_zed (llim, iseg, nseg, f, del, sgn, upwnd, fl, fr, periodic, df)
implicit none
integer, intent (in) :: llim, iseg, nseg
complex, dimension (llim:), intent (in) :: f
real, intent (in) :: del, upwnd
integer, intent (in) :: sgn
complex, dimension (:), intent (in) :: fl, fr
logical, intent (in) :: periodic
complex, dimension (llim:), intent (out) :: df
integer :: i, istart, iend, ulim
! if upwnd is zero or if vpa=0, then use centered differences
if (abs(upwnd) < epsilon(0.) .or. sgn == 0) then
call second_order_centered_zed (llim, iseg, nseg, f, del, sgn, fl, fr, periodic, df)
else
ulim = size(f)+llim-1
! if sgn > 0, then stream speed is negative
! so sweep from more positive to more negative zed
if (sgn > 0) then
if (iseg == nseg.and..not.periodic) then
i = ulim
df(i) = (0.5*(upwnd-1.)*f(i-1)-upwnd*f(i))/del
else
i = ulim
df(i) = (0.5*(upwnd-1.)*f(i-1)-upwnd*f(i)+0.5*(1.+upwnd)*fr(1))/del
end if
if (iseg == 1.and..not.periodic) then
i = llim
! at left boundary, must upwind fully as no info for f(i-1)
df(i) = (f(i+1)-f(i))/del
else
i = llim
df(i) = (0.5*(1.+upwnd)*f(i+1)-upwnd*f(i)+0.5*(upwnd-1.)*fl(2))/del
end if
istart = ulim
iend = llim
else
if (iseg == 1.and..not.periodic) then
i = llim
df(i) = (0.5*(1.-upwnd)*f(i+1)+upwnd*f(i))/del
else
i = llim
df(i) = (0.5*(1.-upwnd)*f(i+1)+upwnd*f(i)-0.5*(1.+upwnd)*fl(2))/del
end if
if (iseg == nseg.and..not.periodic) then
i = ulim
! if at rightmost zed, have no info for f(i+1) so must fully upwind
df(i) = (f(i)-f(i-1))/del
else
i = ulim
df(i) = (0.5*(1.-upwnd)*fr(1)+upwnd*f(i)-0.5*(1.+upwnd)*f(i-1))/del
end if
istart = llim
iend = ulim
end if
! mixed 2nd order centered and 1st order upwind scheme
do i = istart-sgn, iend+sgn, -sgn
df(i) = sgn*(0.5*(1.+upwnd)*f(i+sgn) - upwnd*f(i) + 0.5*(upwnd-1.)*f(i-sgn))/del
end do
end if
end subroutine fd_variable_upwinding_zed
subroutine fd_variable_upwinding_vpa (llim, f, del, sgn, upwnd, df)
implicit none
integer, intent (in) :: llim
complex, dimension (llim:), intent (in) :: f
real, intent (in) :: del, upwnd
integer, intent (in) :: sgn
complex, dimension (llim:), intent (out) :: df
integer :: i, istart, iend, ulim
! if upwnd is zero or if z=0, then use centered differences
if (abs(upwnd) < epsilon(0.) .or. sgn == 0) then
call second_order_centered_vpa (llim, f, del, df)
else
ulim = size(f)+llim-1
! if sgn > 0, then stream speed is negative
! so sweep from more positive to more negative zed
if (sgn > 0) then
istart = ulim
iend = llim
else
istart = llim
iend = ulim
end if
! zero_bc assumes that g -> zero beyond grid
! boundaries in vpa
df(istart) = sgn*(0.5*(upwnd-1.0)*f(istart-sgn)-upwnd*f(istart))/del
! as do not have info beyond grid boundary at end of sweep
! use pure upwinding
df(iend) = sgn*(f(iend+sgn)-f(iend))/del
! mixed centered and 1st order upwind scheme
do i = istart-sgn, iend+sgn, -sgn
df(i) = sgn*(0.5*(1.+upwnd)*f(i+sgn) - upwnd*f(i) + 0.5*(upwnd-1.)*f(i-sgn))/del
end do
end if
end subroutine fd_variable_upwinding_vpa
! only good for equally-spaced grid-pts
subroutine fd3pt_real (prof, profgrad, dr)
implicit none
real, dimension (:), intent (in) :: prof
real, dimension (:), intent (out) :: profgrad
real, intent (in) :: dr
integer :: ix, npts
real, dimension (:), allocatable :: aa, bb, cc
npts = size(prof)
allocate (aa(npts), bb(npts), cc(npts))
aa = 1.0 ; bb = 4.0 ; cc = 1.0
aa(1) = 0.0 ; bb(1) = 0.5 ; cc(1) = 0.5
aa(npts) = 0.5 ; bb(npts) = 0.5 ; cc(npts) = 0.0
do ix = 2, npts-1
profgrad(ix) = 3.0 * (prof(ix+1) - prof(ix-1)) / dr
end do
profgrad(1) = (prof(2)-prof(1))/dr
profgrad(npts) = (prof(npts)-prof(npts-1))/dr
call tridag (aa, bb, cc, profgrad)
deallocate (aa, bb, cc)
end subroutine fd3pt_real
subroutine fd3pt_real_array (prof, profgrad, dr)
implicit none
real, dimension (:), intent (in) :: prof, dr
real, dimension (:), intent (out) :: profgrad
integer :: ix, npts
real :: a, b, c
npts = size(prof)
do ix = 2, npts-1
profgrad(ix) = ((prof(ix)-prof(ix-1))*dr(ix)/dr(ix-1) &
+ (prof(ix+1)-prof(ix))*dr(ix-1)/dr(ix)) / (dr(ix-1)+dr(ix))
end do
ix = 1
a = -(2.*dr(1) + dr(2))/(dr(1)*(dr(1)+dr(2)))
b = (dr(1)+dr(2))/(dr(1)*dr(2))
c = -dr(1)/(dr(2)*(dr(1)+dr(2)))
profgrad(1) = a*prof(1)+b*prof(2)+c*prof(3)
ix = npts
a = dr(npts-1)/(dr(npts-2)*(dr(npts-2)+dr(npts-1)))
b = -(dr(npts-1)+dr(npts-2))/(dr(npts-2)*dr(npts-1))
c = (2.*dr(npts-1)+dr(npts-2))/(dr(npts-1)*(dr(npts-1)+dr(npts-2)))
profgrad(npts) = a*prof(npts-2) + b*prof(npts-1) + c*prof(npts)
end subroutine fd3pt_real_array
subroutine fd3pt_complex_array (prof, profgrad, dr)
implicit none
complex, dimension (:), intent (in) :: prof
real, dimension (:), intent (in) :: dr
complex, dimension (:), intent (out) :: profgrad
integer :: ix, npts
real :: a, b, c
npts = size(prof)
do ix = 2, npts-1
profgrad(ix) = ((prof(ix)-prof(ix-1))*dr(ix)/dr(ix-1) &
+ (prof(ix+1)-prof(ix))*dr(ix-1)/dr(ix)) / (dr(ix-1)+dr(ix))
end do
ix = 1
a = -(2.*dr(1) + dr(2))/(dr(1)*(dr(1)+dr(2)))
b = (dr(1)+dr(2))/(dr(1)*dr(2))
c = -dr(1)/(dr(2)*(dr(1)+dr(2)))
profgrad(1) = a*prof(1)+b*prof(2)+c*prof(3)
ix = npts
a = dr(npts-1)/(dr(npts-2)*(dr(npts-2)+dr(npts-1)))
b = -(dr(npts-1)+dr(npts-2))/(dr(npts-2)*dr(npts-1))
c = (2.*dr(npts-1)+dr(npts-2))/(dr(npts-1)*(dr(npts-1)+dr(npts-2)))
profgrad(npts) = a*prof(npts-2) + b*prof(npts-1) + c*prof(npts)
end subroutine fd3pt_complex_array