-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfinitedifference.jl
4149 lines (3686 loc) · 118 KB
/
finitedifference.jl
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
import ..Utilities: PlusHalf, half
import UnrolledUtilities: unrolled_map
const AllFiniteDifferenceSpace =
Union{Spaces.FiniteDifferenceSpace, Spaces.ExtrudedFiniteDifferenceSpace}
const AllFaceFiniteDifferenceSpace = Union{
Spaces.FaceFiniteDifferenceSpace,
Spaces.FaceExtrudedFiniteDifferenceSpace,
}
const AllCenterFiniteDifferenceSpace = Union{
Spaces.CenterFiniteDifferenceSpace,
Spaces.CenterExtrudedFiniteDifferenceSpace,
}
left_idx(space::AllCenterFiniteDifferenceSpace) =
left_center_boundary_idx(space)
right_idx(space::AllCenterFiniteDifferenceSpace) =
right_center_boundary_idx(space)
left_idx(space::AllFaceFiniteDifferenceSpace) = left_face_boundary_idx(space)
right_idx(space::AllFaceFiniteDifferenceSpace) = right_face_boundary_idx(space)
left_center_boundary_idx(space::AllFiniteDifferenceSpace) = 1
right_center_boundary_idx(space::AllFiniteDifferenceSpace) = size(
Spaces.local_geometry_data(Spaces.space(space, Spaces.CellCenter())),
4,
)
left_face_boundary_idx(space::AllFiniteDifferenceSpace) = half
right_face_boundary_idx(space::AllFiniteDifferenceSpace) =
size(
Spaces.local_geometry_data(Spaces.space(space, Spaces.CellFace())),
4,
) - half
left_face_boundary_idx(arg) = left_face_boundary_idx(axes(arg))
right_face_boundary_idx(arg) = right_face_boundary_idx(axes(arg))
left_center_boundary_idx(arg) = left_center_boundary_idx(axes(arg))
right_center_boundary_idx(arg) = right_center_boundary_idx(axes(arg))
# unlike getidx, we allow extracting the face local geometry from the center space, and vice-versa
Base.@propagate_inbounds function Geometry.LocalGeometry(
space::AllFiniteDifferenceSpace,
idx::Integer,
hidx,
)
v = idx
if Topologies.isperiodic(Spaces.vertical_topology(space))
v = mod1(v, Spaces.nlevels(space))
end
i, j, h = hidx
local_geom =
Grids.local_geometry_data(Spaces.grid(space), Grids.CellCenter())
return @inbounds local_geom[CartesianIndex(i, j, 1, v, h)]
end
Base.@propagate_inbounds function Geometry.LocalGeometry(
space::AllFiniteDifferenceSpace,
idx::PlusHalf,
hidx,
)
v = idx + half
if Topologies.isperiodic(Spaces.vertical_topology(space))
v = mod1(v, Spaces.nlevels(space))
end
i, j, h = hidx
local_geom = Grids.local_geometry_data(Spaces.grid(space), Grids.CellFace())
return @inbounds local_geom[CartesianIndex(i, j, 1, v, h)]
end
"""
AbstractBoundaryCondition
An abstract type for boundary conditions for [`FiniteDifferenceOperator`](@ref)s.
Subtypes should define:
- [`boundary_width`](@ref)
- [`stencil_left_boundary`](@ref)
- [`stencil_right_boundary`](@ref)
"""
abstract type AbstractBoundaryCondition end
strip_space(bc::AbstractBoundaryCondition, parent_space) =
hasproperty(bc, :val) ?
unionall_type(typeof(bc))(strip_space(bc.val, parent_space)) : bc
"""
NullBoundaryCondition()
This is used as a placeholder when no other boundary condition can be applied.
"""
struct NullBoundaryCondition <: AbstractBoundaryCondition end
"""
SetValue(val)
Set the value at the boundary to be `val`. In the case of gradient operators,
this will set the input value from which the gradient is computed.
"""
struct SetValue{S} <: AbstractBoundaryCondition
val::S
end
"""
SetGradient(val)
Set the gradient at the boundary to be `val`. In the case of gradient operators
this will set the output value of the gradient.
"""
struct SetGradient{S} <: AbstractBoundaryCondition
val::S
end
"""
SetDivergence(val)
Set the divergence at the boundary to be `val`.
"""
struct SetDivergence{S} <: AbstractBoundaryCondition
val::S
end
"""
SetCurl(val)
Set the curl at the boundary to be `val`.
"""
struct SetCurl{S} <: AbstractBoundaryCondition
val::S
end
"""
Extrapolate()
Set the value at the boundary to be the same as the closest interior point.
"""
struct Extrapolate <: AbstractBoundaryCondition end
"""
FirstOrderOneSided()
Use a first-order up/down-wind scheme to compute the value at the boundary.
"""
struct FirstOrderOneSided <: AbstractBoundaryCondition end
"""
ThirdOrderOneSided()
Use a third-order up/down-wind scheme to compute the value at the boundary.
"""
struct ThirdOrderOneSided <: AbstractBoundaryCondition end
abstract type Location end
abstract type Boundary <: Location end
abstract type BoundaryWindow <: Location end
struct Interior <: Location end
struct LeftBoundaryWindow{name} <: BoundaryWindow end
struct RightBoundaryWindow{name} <: BoundaryWindow end
"""
FiniteDifferenceOperator
An abstract type for finite difference operators. Instances of this should define:
- [`return_eltype`](@ref)
- [`return_space`](@ref)
- [`stencil_interior_width`](@ref)
- [`stencil_interior`](@ref)
See also [`AbstractBoundaryCondition`](@ref) for how to define the boundaries.
"""
abstract type FiniteDifferenceOperator <: AbstractOperator end
return_eltype(::FiniteDifferenceOperator, arg) = eltype(arg)
# boundary width error fallback
@noinline invalid_boundary_condition_error(op_type::Type, bc_type::Type) =
error("Boundary `$bc_type` is not supported for operator `$op_type`")
boundary_width(
op::FiniteDifferenceOperator,
bc::AbstractBoundaryCondition,
args...,
) = invalid_boundary_condition_error(typeof(op), typeof(bc))
get_boundary(
op::FiniteDifferenceOperator,
::LeftBoundaryWindow{name},
) where {name} =
hasproperty(op.bcs, name) ? getproperty(op.bcs, name) :
NullBoundaryCondition()
get_boundary(
op::FiniteDifferenceOperator,
::RightBoundaryWindow{name},
) where {name} =
hasproperty(op.bcs, name) ? getproperty(op.bcs, name) :
NullBoundaryCondition()
has_boundary(
op::FiniteDifferenceOperator,
::LeftBoundaryWindow{name},
) where {name} = hasproperty(op.bcs, name)
has_boundary(
op::FiniteDifferenceOperator,
::RightBoundaryWindow{name},
) where {name} = hasproperty(op.bcs, name)
strip_space(op::FiniteDifferenceOperator, parent_space) =
unionall_type(typeof(op))(
NamedTuple{keys(op.bcs)}(
strip_space_args(values(op.bcs), parent_space),
),
)
abstract type AbstractStencilStyle <: Fields.AbstractFieldStyle end
# the .f field is an operator
struct StencilStyle <: AbstractStencilStyle end
struct ColumnStencilStyle <: AbstractStencilStyle end
AbstractStencilStyle(::ClimaComms.AbstractCPUDevice) = ColumnStencilStyle
"""
StencilBroadcasted{Style}(op, args[,axes[, work]])
This is similar to a `Base.Broadcast.Broadcasted` object.
This is returned by `Base.Broadcast.broadcasted(op::FiniteDifferenceOperator)`.
"""
struct StencilBroadcasted{Style, Op, Args, Axes} <: OperatorBroadcasted{Style}
op::Op
args::Args
axes::Axes
end
StencilBroadcasted{Style}(
op::Op,
args::Args,
axes::Axes = nothing,
) where {Style, Op, Args, Axes} =
StencilBroadcasted{Style, Op, Args, Axes}(op, args, axes)
Adapt.adapt_structure(to, sbc::StencilBroadcasted{Style}) where {Style} =
StencilBroadcasted{Style}(
Adapt.adapt(to, sbc.op),
Adapt.adapt(to, sbc.args),
Adapt.adapt(to, sbc.axes),
)
function Base.Broadcast.instantiate(sbc::StencilBroadcasted)
op = sbc.op
# recursively instantiate the arguments to allocate intermediate work arrays
args = instantiate_args(sbc.args)
# axes: same logic as Broadcasted
if sbc.axes isa Nothing # Not done via dispatch to make it easier to extend instantiate(::Broadcasted{Style})
axes = Base.axes(sbc)
else
axes = sbc.axes
if axes !== Base.axes(sbc)
Base.Broadcast.check_broadcast_axes(axes, args...)
end
end
Style = AbstractStencilStyle(ClimaComms.device(axes))
return StencilBroadcasted{Style}(op, args, axes)
end
function Base.Broadcast.instantiate(
bc::Base.Broadcast.Broadcasted{<:AbstractStencilStyle},
)
# recursively instantiate the arguments to allocate intermediate work arrays
args = instantiate_args(bc.args)
# axes: same logic as Broadcasted
if bc.axes isa Nothing # Not done via dispatch to make it easier to extend instantiate(::Broadcasted{Style})
axes = Base.Broadcast.combine_axes(args...)
else
axes = bc.axes
Base.Broadcast.check_broadcast_axes(axes, args...)
end
Style = AbstractStencilStyle(ClimaComms.device(axes))
return Base.Broadcast.Broadcasted{Style}(bc.f, args, axes)
end
function strip_space(sbc::StencilBroadcasted{Style}, parent_space) where {Style}
current_space = axes(sbc)
new_space = placeholder_space(current_space, parent_space)
return StencilBroadcasted{Style}(
strip_space(sbc.op, current_space),
strip_space_args(sbc.args, current_space),
new_space,
)
end
"""
return_eltype(::Op, fields...)
Defines the element type of the result of operator `Op`
"""
function return_eltype end
"""
stencil_interior_width(::Op, args...)
Defines the width of the interior stencil for the operator `Op` with the given
arguments. Returns a tuple of 2-tuples: each 2-tuple should be the lower and
upper bounds of the index offsets of the stencil for each argument in the
stencil.
## Example
```
stencil(::Op, arg1, arg2) = ((-half, 1+half), (0,0))
```
implies that at index `i`, the stencil accesses `arg1` at `i-half`, `i+half` and
`i+1+half`, and `arg2` at index `i`.
"""
function stencil_interior_width end
"""
stencil_interior(::Op, loc, space, idx, args...)
Defines the stencil of the operator `Op` in the interior of the domain at `idx`;
`args` are the input arguments.
"""
function stencil_interior end
"""
boundary_width(::Op, ::BC, args...)
Defines the width of a boundary condition `BC` on an operator `Op`. This is the
number of locations that are used in a modified stencil. Either this function,
or [`left_interior_idx`](@ref) and [`right_interior_idx`](@ref) should be
defined for a specific `Op`/`BC` combination.
"""
function boundary_width end
"""
stencil_left_boundary(::Op, ::BC, loc, idx, args...)
Defines the stencil of operator `Op` at `idx` near the left boundary, with boundary condition `BC`.
"""
function stencil_left_boundary end
"""
stencil_right_boundary(::Op, ::BC, loc, idx, args...)
Defines the stencil of operator `Op` at `idx` near the right boundary, with boundary condition `BC`.
"""
function stencil_right_boundary end
abstract type InterpolationOperator <: FiniteDifferenceOperator end
function assert_no_bcs(op, kwargs)
length(kwargs) == 0 && return nothing
error("InterpolateF2C does not accept boundary conditions.")
end
function assert_valid_bcs(op, kwargs, valid_bcs)
for bc in values(values(kwargs))
@assert any(valid_bc -> bc isa valid_bc, valid_bcs) "$op only supports boundary conditions:\n\n\t $valid_bcs.\n\n BCs given:\n\n\t $(values(values(kwargs)))\n"
end
return nothing
end
"""
InterpolateF2C()
Interpolate from face to center mesh. No boundary conditions are required
(or supported).
"""
struct InterpolateF2C{BCS <: @NamedTuple{}} <: InterpolationOperator
bcs::BCS
end
function InterpolateF2C(; kwargs...)
assert_no_bcs("InterpolateF2C", kwargs)
InterpolateF2C((NamedTuple()))
end
return_space(::InterpolateF2C, space::AllFaceFiniteDifferenceSpace) =
Spaces.space(space, Spaces.CellCenter())
stencil_interior_width(::InterpolateF2C, arg) = ((-half, half),)
Base.@propagate_inbounds function stencil_interior(
::InterpolateF2C,
loc,
space,
idx,
hidx,
arg,
)
a⁺ = getidx(space, arg, loc, idx + half, hidx)
a⁻ = getidx(space, arg, loc, idx - half, hidx)
RecursiveApply.rdiv(a⁺ ⊞ a⁻, 2)
end
boundary_width(::InterpolateF2C, ::AbstractBoundaryCondition) = 0
"""
I = InterpolateC2F(;boundaries..)
I.(x)
Interpolate a center-valued field `x` to faces, using the stencil
```math
I(x)[i] = \\frac{1}{2} (x[i+\\tfrac{1}{2}] + x[i-\\tfrac{1}{2}])
```
Supported boundary conditions are:
- [`SetValue(x₀)`](@ref): set the value at the boundary face to be `x₀`. On the
left boundary the stencil is
```math
I(x)[\\tfrac{1}{2}] = x₀
```
- [`SetGradient(v)`](@ref): set the value at the boundary such that the gradient
is `v`. At the left boundary the stencil is
```math
I(x)[\\tfrac{1}{2}] = x[1] - \\frac{1}{2} v³
```
- [`Extrapolate`](@ref): use the closest interior point as the boundary value.
At the left boundary the stencil is
```math
I(x)[\\tfrac{1}{2}] = x[1]
```
"""
struct InterpolateC2F{BCS} <: InterpolationOperator
bcs::BCS
function InterpolateC2F(; kwargs...)
assert_valid_bcs(
"InterpolateC2F",
kwargs,
(SetValue, SetGradient, Extrapolate),
)
new{typeof(NamedTuple(kwargs))}(NamedTuple(kwargs))
end
InterpolateC2F(bcs) = InterpolateC2F(; bcs...)
end
return_space(::InterpolateC2F, space::AllCenterFiniteDifferenceSpace) =
Spaces.space(space, Spaces.CellFace())
stencil_interior_width(::InterpolateC2F, arg) = ((-half, half),)
Base.@propagate_inbounds function stencil_interior(
::InterpolateC2F,
loc,
space,
idx,
hidx,
arg,
)
a⁺ = getidx(space, arg, loc, idx + half, hidx)
a⁻ = getidx(space, arg, loc, idx - half, hidx)
RecursiveApply.rdiv(a⁺ ⊞ a⁻, 2)
end
boundary_width(::InterpolateC2F, ::AbstractBoundaryCondition) = 1
Base.@propagate_inbounds function stencil_left_boundary(
::InterpolateC2F,
bc::SetValue,
loc,
space,
idx,
hidx,
arg,
)
@assert idx == left_face_boundary_idx(space)
getidx(space, bc.val, loc, nothing, hidx)
end
Base.@propagate_inbounds function stencil_right_boundary(
::InterpolateC2F,
bc::SetValue,
loc,
space,
idx,
hidx,
arg,
)
@assert idx == right_face_boundary_idx(space)
getidx(space, bc.val, loc, nothing, hidx)
end
Base.@propagate_inbounds function stencil_left_boundary(
::InterpolateC2F,
bc::SetGradient,
loc,
space,
idx,
hidx,
arg,
)
@assert idx == left_face_boundary_idx(space)
a⁺ = getidx(space, arg, loc, idx + half, hidx)
v₃ = Geometry.covariant3(
getidx(space, bc.val, loc, nothing, hidx),
Geometry.LocalGeometry(space, idx, hidx),
)
a⁺ ⊟ RecursiveApply.rdiv(v₃, 2)
end
Base.@propagate_inbounds function stencil_right_boundary(
::InterpolateC2F,
bc::SetGradient,
loc,
space,
idx,
hidx,
arg,
)
@assert idx == right_face_boundary_idx(space)
a⁻ = getidx(space, arg, loc, idx - half, hidx)
v₃ = Geometry.covariant3(
getidx(space, bc.val, loc, nothing, hidx),
Geometry.LocalGeometry(space, idx, hidx),
)
a⁻ ⊞ RecursiveApply.rdiv(v₃, 2)
end
Base.@propagate_inbounds function stencil_left_boundary(
::InterpolateC2F,
bc::Extrapolate,
loc,
space,
idx,
hidx,
arg,
)
@assert idx == left_face_boundary_idx(space)
a⁺ = getidx(space, arg, loc, idx + half, hidx)
a⁺
end
Base.@propagate_inbounds function stencil_right_boundary(
::InterpolateC2F,
bc::Extrapolate,
loc,
space,
idx,
hidx,
arg,
)
@assert idx == right_face_boundary_idx(space)
a⁻ = getidx(space, arg, loc, idx - half, hidx)
a⁻
end
"""
L = LeftBiasedC2F(;boundaries)
L.(x)
Interpolate a center-value field to a face-valued field from the left.
```math
L(x)[i] = x[i-\\tfrac{1}{2}]
```
Only the left boundary condition should be set. Currently supported is:
- [`SetValue(x₀)`](@ref): set the value to be `x₀` on the boundary.
```math
L(x)[\\tfrac{1}{2}] = x_0
```
"""
struct LeftBiasedC2F{BCS} <: InterpolationOperator
bcs::BCS
function LeftBiasedC2F(; kwargs...)
assert_valid_bcs("LeftBiasedC2F", kwargs, (SetValue,))
new{typeof(NamedTuple(kwargs))}(NamedTuple(kwargs))
end
LeftBiasedC2F(bcs) = LeftBiasedC2F(; bcs...)
end
return_space(::LeftBiasedC2F, space::AllCenterFiniteDifferenceSpace) =
Spaces.space(space, Spaces.CellFace())
stencil_interior_width(::LeftBiasedC2F, arg) = ((-half, -half),)
Base.@propagate_inbounds stencil_interior(
::LeftBiasedC2F,
loc,
space,
idx,
hidx,
arg,
) = getidx(space, arg, loc, idx - half, hidx)
left_interior_idx(
space::AbstractSpace,
::LeftBiasedC2F,
::AbstractBoundaryCondition,
arg,
) = left_idx(space) + 1
right_interior_idx(
space::AbstractSpace,
::LeftBiasedC2F,
::AbstractBoundaryCondition,
arg,
) = right_idx(space)
Base.@propagate_inbounds function stencil_left_boundary(
::LeftBiasedC2F,
bc::SetValue,
loc,
space,
idx,
hidx,
arg,
)
@assert idx == left_face_boundary_idx(space)
getidx(space, bc.val, loc, nothing, hidx)
end
"""
L = LeftBiasedF2C(;boundaries)
L.(x)
Interpolate a face-value field to a center-valued field from the left.
```math
L(x)[i+\\tfrac{1}{2}] = x[i]
```
Only the left boundary condition should be set. Currently supported is:
- [`SetValue(x₀)`](@ref): set the value to be `x₀` on the boundary.
```math
L(x)[1] = x_0
```
"""
struct LeftBiasedF2C{BCS} <: InterpolationOperator
bcs::BCS
function LeftBiasedF2C(; kwargs...)
assert_valid_bcs("LeftBiasedF2C", kwargs, (SetValue,))
new{typeof(NamedTuple(kwargs))}(NamedTuple(kwargs))
end
LeftBiasedF2C(bcs) = LeftBiasedF2C(; bcs...)
end
return_space(::LeftBiasedF2C, space::AllFaceFiniteDifferenceSpace) =
Spaces.space(space, Spaces.CellCenter())
stencil_interior_width(::LeftBiasedF2C, arg) = ((-half, -half),)
Base.@propagate_inbounds stencil_interior(
::LeftBiasedF2C,
loc,
space,
idx,
hidx,
arg,
) = getidx(space, arg, loc, idx - half, hidx)
left_interior_idx(
space::AbstractSpace,
::LeftBiasedF2C,
::AbstractBoundaryCondition,
arg,
) = left_idx(space)
right_interior_idx(
space::AbstractSpace,
::LeftBiasedF2C,
::AbstractBoundaryCondition,
arg,
) = right_idx(space)
left_interior_idx(space::AbstractSpace, ::LeftBiasedF2C, ::SetValue, arg) =
left_idx(space) + 1
Base.@propagate_inbounds function stencil_left_boundary(
::LeftBiasedF2C,
bc::SetValue,
loc,
space,
idx,
hidx,
arg,
)
@assert idx == left_center_boundary_idx(space)
getidx(space, bc.val, loc, nothing, hidx)
end
"""
L = LeftBiased3rdOrderC2F(;boundaries)
L.(x)
Interpolate a center-value field to a face-valued field from the left, using a 3rd-order reconstruction.
```math
L(x)[i] = \\left(-2 x[i-\\tfrac{3}{2}] + 10 x[i-\\tfrac{1}{2}] + 4 x[i+\\tfrac{1}{2}] \\right) / 12
```
Only the left boundary condition should be set. Currently supported is:
- [`SetValue(x₀)`](@ref): set the value to be `x₀` on the boundary.
```math
L(x)[\\tfrac{1}{2}] = x_0
```
"""
struct LeftBiased3rdOrderC2F{BCS} <: InterpolationOperator
bcs::BCS
function LeftBiased3rdOrderC2F(; kwargs...)
assert_valid_bcs("LeftBiased3rdOrderC2F", kwargs, (SetValue,))
new{typeof(NamedTuple(kwargs))}(NamedTuple(kwargs))
end
LeftBiased3rdOrderC2F(bcs) = LeftBiased3rdOrderC2F(; bcs...)
end
return_space(::LeftBiased3rdOrderC2F, space::AllCenterFiniteDifferenceSpace) =
Spaces.space(space, Spaces.CellFace())
stencil_interior_width(::LeftBiased3rdOrderC2F, arg) = ((-half - 1, half + 1),)
Base.@propagate_inbounds stencil_interior(
::LeftBiased3rdOrderC2F,
loc,
space,
idx,
hidx,
arg,
) =
(
-2 * getidx(space, arg, loc, idx - 1 - half, hidx) +
10 * getidx(space, arg, loc, idx - half, hidx) +
4 * getidx(space, arg, loc, idx + half, hidx)
) / 12
left_interior_idx(
space::AbstractSpace,
::LeftBiased3rdOrderC2F,
::AbstractBoundaryCondition,
arg,
) = left_idx(space) + 2
right_interior_idx(
space::AbstractSpace,
::LeftBiased3rdOrderC2F,
::AbstractBoundaryCondition,
arg,
) = right_idx(space) - 1
Base.@propagate_inbounds function stencil_left_boundary(
::LeftBiased3rdOrderC2F,
bc::SetValue,
loc,
space,
idx,
hidx,
arg,
)
@assert idx == left_face_boundary_idx(space)
getidx(space, bc.val, loc, nothing, hidx)
end
"""
L = LeftBiased3rdOrderF2C(;boundaries)
L.(x)
Interpolate a face-value field to a center-valued field from the left, using a 3rd-order reconstruction.
```math
L(x)[i+\\tfrac{1}{2}] = \\left(-2 x[i-1] + 10 x[i] + 4 x[i+1] \\right) / 12
```
Only the left boundary condition should be set. Currently supported is:
- [`SetValue(x₀)`](@ref): set the value to be `x₀` on the boundary.
```math
L(x)[1] = x_0
```
"""
struct LeftBiased3rdOrderF2C{BCS} <: InterpolationOperator
bcs::BCS
function LeftBiased3rdOrderF2C(; kwargs...)
assert_valid_bcs("LeftBiased3rdOrderF2C", kwargs, (SetValue,))
new{typeof(NamedTuple(kwargs))}(NamedTuple(kwargs))
end
LeftBiased3rdOrderF2C(bcs) = LeftBiased3rdOrderF2C(; bcs...)
end
return_space(::LeftBiased3rdOrderF2C, space::AllFaceFiniteDifferenceSpace) =
Spaces.space(space, Spaces.CellCenter())
stencil_interior_width(::LeftBiased3rdOrderF2C, arg) = ((-half - 1, half + 1),)
Base.@propagate_inbounds stencil_interior(
::LeftBiased3rdOrderF2C,
loc,
space,
idx,
hidx,
arg,
) =
(
-2 * getidx(space, arg, loc, idx - 1 - half, hidx) +
10 * getidx(space, arg, loc, idx - half, hidx) +
4 * getidx(space, arg, loc, idx + half, hidx)
) / 12
left_interior_idx(
space::AbstractSpace,
::LeftBiased3rdOrderF2C,
::AbstractBoundaryCondition,
arg,
) = left_idx(space) + 1
right_interior_idx(
space::AbstractSpace,
::LeftBiased3rdOrderF2C,
::AbstractBoundaryCondition,
arg,
) = right_idx(space)
Base.@propagate_inbounds function stencil_left_boundary(
::LeftBiased3rdOrderF2C,
bc::SetValue,
loc,
space,
idx,
hidx,
arg,
)
@assert idx == left_center_boundary_idx(space)
getidx(space, bc.val, loc, nothing, hidx)
end
"""
R = RightBiasedC2F(;boundaries)
R.(x)
Interpolate a center-valued field to a face-valued field from the right.
```math
R(x)[i] = x[i+\\tfrac{1}{2}]
```
Only the right boundary condition should be set. Currently supported is:
- [`SetValue(x₀)`](@ref): set the value to be `x₀` on the boundary.
```math
R(x)[n+\\tfrac{1}{2}] = x_0
```
"""
struct RightBiasedC2F{BCS} <: InterpolationOperator
bcs::BCS
function RightBiasedC2F(; kwargs...)
assert_valid_bcs("RightBiasedC2F", kwargs, (SetValue,))
new{typeof(NamedTuple(kwargs))}(NamedTuple(kwargs))
end
RightBiasedC2F(bcs) = RightBiasedC2F(; bcs...)
end
return_space(::RightBiasedC2F, space::AllCenterFiniteDifferenceSpace) =
Spaces.space(space, Spaces.CellFace())
stencil_interior_width(::RightBiasedC2F, arg) = ((half, half),)
Base.@propagate_inbounds stencil_interior(
::RightBiasedC2F,
loc,
space,
idx,
hidx,
arg,
) = getidx(space, arg, loc, idx + half, hidx)
left_interior_idx(
space::AbstractSpace,
::RightBiasedC2F,
::AbstractBoundaryCondition,
arg,
) = left_idx(space)
right_interior_idx(
space::AbstractSpace,
::RightBiasedC2F,
::AbstractBoundaryCondition,
arg,
) = right_idx(space) - 1
Base.@propagate_inbounds function stencil_right_boundary(
::RightBiasedC2F,
bc::SetValue,
loc,
space,
idx,
hidx,
arg,
)
@assert idx == right_face_boundary_idx(space)
getidx(space, bc.val, loc, nothing, hidx)
end
"""
R = RightBiasedF2C(;boundaries)
R.(x)
Interpolate a face-valued field to a center-valued field from the right.
```math
R(x)[i] = x[i+\\tfrac{1}{2}]
```
Only the right boundary condition should be set. Currently supported is:
- [`SetValue(x₀)`](@ref): set the value to be `x₀` on the boundary.
```math
R(x)[n+\\tfrac{1}{2}] = x_0
```
"""
struct RightBiasedF2C{BCS} <: InterpolationOperator
bcs::BCS
function RightBiasedF2C(; kwargs...)
assert_valid_bcs("RightBiasedF2C", kwargs, (SetValue,))
new{typeof(NamedTuple(kwargs))}(NamedTuple(kwargs))
end
RightBiasedF2C(bcs) = RightBiasedF2C(; bcs...)
end
return_space(::RightBiasedF2C, space::AllFaceFiniteDifferenceSpace) =
Spaces.space(space, Spaces.CellCenter())
stencil_interior_width(::RightBiasedF2C, arg) = ((half, half),)
Base.@propagate_inbounds stencil_interior(
::RightBiasedF2C,
loc,
space,
idx,
hidx,
arg,
) = getidx(space, arg, loc, idx + half, hidx)
left_interior_idx(
space::AbstractSpace,
::RightBiasedF2C,
::AbstractBoundaryCondition,
arg,
) = left_idx(space)
right_interior_idx(
space::AbstractSpace,
::RightBiasedF2C,
::AbstractBoundaryCondition,
arg,
) = right_idx(space)
right_interior_idx(space::AbstractSpace, ::RightBiasedF2C, ::SetValue, arg) =
right_idx(space) - 1
Base.@propagate_inbounds function stencil_right_boundary(
::RightBiasedF2C,
bc::SetValue,
loc,
space,
idx,
hidx,
arg,
)
@assert idx == right_center_boundary_idx(space)
getidx(space, bc.val, loc, nothing, hidx)
end
"""
R = RightBiased3rdOrderC2F(;boundaries)
R.(x)
Interpolate a center-valued field to a face-valued field from the right, using a 3rd-order reconstruction.
```math
R(x)[i] = \\left(4 x[i-\\tfrac{1}{2}] + 10 x[i+\\tfrac{1}{2}] -2 x[i+\\tfrac{3}{2}] \\right) / 12
```
Only the right boundary condition should be set. Currently supported is:
- [`SetValue(x₀)`](@ref): set the value to be `x₀` on the boundary.
```math
R(x)[n+\\tfrac{1}{2}] = x_0
```
"""
struct RightBiased3rdOrderC2F{BCS} <: InterpolationOperator
bcs::BCS
function RightBiased3rdOrderC2F(; kwargs...)
assert_valid_bcs("RightBiased3rdOrderC2F", kwargs, (SetValue,))
new{typeof(NamedTuple(kwargs))}(NamedTuple(kwargs))
end
RightBiased3rdOrderC2F(bcs) = RightBiased3rdOrderC2F(; bcs...)
end
return_space(::RightBiased3rdOrderC2F, space::AllCenterFiniteDifferenceSpace) =
Spaces.space(space, Spaces.CellFace())
stencil_interior_width(::RightBiased3rdOrderC2F, arg) = ((-half - 1, half + 1),)
Base.@propagate_inbounds stencil_interior(
::RightBiased3rdOrderC2F,
loc,
space,
idx,
hidx,
arg,
) =
(
4 * getidx(space, arg, loc, idx - half, hidx) +
10 * getidx(space, arg, loc, idx + half, hidx) -
2 * getidx(space, arg, loc, idx + half + 1, hidx)
) / 12
boundary_width(::RightBiased3rdOrderC2F, ::SetValue) = 1
Base.@propagate_inbounds function stencil_right_boundary(
::RightBiased3rdOrderC2F,
bc::SetValue,
loc,
space,
idx,
hidx,
arg,
)
@assert idx == right_face_boundary_idx(space)
getidx(space, bc.val, loc, nothing, hidx)
end
"""
R = RightBiased3rdOrderF2C(;boundaries)
R.(x)