Skip to content
This repository was archived by the owner on Jul 19, 2023. It is now read-only.

Commit e833891

Browse files
committed
Added half offset
1 parent 3690385 commit e833891

2 files changed

Lines changed: 31 additions & 31 deletions

File tree

src/DiffEqOperators.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ end
7373
export MatrixFreeOperator
7474
export AnalyticalJacVecOperator, JacVecOperator, getops
7575
export AbstractDerivativeOperator, DerivativeOperator,
76-
CenteredDifference, UpwindDifference, CompleteCenteredDifference, CompleteUpwindDifference, nonlinear_diffusion, nonlinear_diffusion!,
76+
CenteredDifference, UpwindDifference, CompleteCenteredDifference, CompleteUpwindDifference, CompleteHalfCenteredDifference, nonlinear_diffusion, nonlinear_diffusion!,
7777
GradientOperator, Gradient, CurlOperator, Curl, DivergenceOperator, Divergence
7878
export DirichletBC, Dirichlet0BC, NeumannBC, Neumann0BC, RobinBC, GeneralBC, MultiDimBC, PeriodicBC,
7979
MultiDimDirectionalBC, ComposedMultiDimBC

src/derivative_operators/derivative_operator.jl

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ function CenteredDifference{N}(derivative_order::Int,
133133
low_boundary_x = [zero(T); cumsum(dx[1:boundary_stencil_length-1])]
134134
high_boundary_x = cumsum(dx[end-boundary_stencil_length+1:end])
135135
# Because it's a N x (N+2) operator, the last stencil on the sides are the [b,0,x,x,x,x] stencils, not the [0,x,x,x,x,x] stencils, since we're never solving for the derivative at the boundary point.
136-
deriv_spots = (-div(stencil_length,2)+1) : -1>
136+
deriv_spots = (-div(stencil_length,2)+1) : -1
137137

138138
stencil_coefs = [convert(SVector{stencil_length, T}, calculate_weights(derivative_order, zero(T), generate_coordinates(i, stencil_x, dummy_x, dx))) for i in interior_x]
139139
_low_boundary_coefs = SVector{boundary_stencil_length, T}[convert(SVector{boundary_stencil_length, T},
@@ -163,12 +163,13 @@ function CenteredDifference{N}(derivative_order::Int,
163163
)
164164
end
165165

166+
167+
# TODO: Set weight to zero if its below a certain threshold
166168
"""
167169
A helper function to compute the coefficients of a derivative operator including the boundary coefficients in the centered scheme.
168170
"""
169171
function CompleteCenteredDifference(derivative_order::Int,
170-
approximation_order::Int, dx::T,
171-
len::Int, coeff_func=1) where {T<:Real,N}
172+
approximation_order::Int, dx::T) where {T<:Real,N}
172173
@assert approximation_order>1 "approximation_order must be greater than 1."
173174
stencil_length = derivative_order + approximation_order - 1 + (derivative_order+approximation_order)%2
174175
boundary_stencil_length = derivative_order + approximation_order
@@ -186,22 +187,22 @@ function CompleteCenteredDifference(derivative_order::Int,
186187
low_boundary_coefs = convert(SVector{boundary_point_count},vcat(_low_boundary_coefs))
187188

188189
# _high_boundary_coefs = SVector{boundary_stencil_length, T}[convert(SVector{boundary_stencil_length, T}, (1/dx^derivative_order) * calculate_weights(derivative_order, oneunit(T)*x0, reverse(right_boundary_x))) for x0 in R_boundary_deriv_spots]
189-
high_boundary_coefs = convert(SVector{boundary_point_count},reverse(map(reverse, _low_boundary_coefs*(-1)^derivative_order)))
190+
high_boundary_coefs = convert(SVector{boundary_point_count},[reverse(v)*(-1)^derivative_order for v in _low_boundary_coefs])
190191

191192
offside = 0
192193

193-
coefficients = fill!(Vector{T}(undef,len),0)
194+
coefficients = nothing
194195

195196

196-
DerivativeOperator{T,N,false,T,typeof(stencil_coefs),
197+
DerivativeOperator{T,Nothing,false,T,typeof(stencil_coefs),
197198
typeof(low_boundary_coefs),typeof(high_boundary_coefs),typeof(coefficients),
198-
typeof(coeff_func)}(
199-
derivative_order, approximation_order, dx, len, stencil_length,
199+
Nothing}(
200+
derivative_order, approximation_order, dx, 1, stencil_length,
200201
stencil_coefs,
201202
boundary_stencil_length,
202203
boundary_point_count,
203204
low_boundary_coefs,
204-
high_boundary_coefs,offside,coefficients,coeff_func
205+
high_boundary_coefs,offside,coefficients,nothing
205206
)
206207
end
207208

@@ -212,46 +213,45 @@ A helper function to compute the coefficients of a derivative operator including
212213
function CompleteHalfCenteredDifference(derivative_order::Int,
213214
approximation_order::Int, dx::T) where {T<:Real,N}
214215
@assert approximation_order>1 "approximation_order must be greater than 1."
215-
stencil_length = approximation_order + 2*floor(derivative_order/2) + 2*(approximation_order%2)
216-
centered_stencil_length = derivative_order + approximation_order - 1 + (derivative_order+approximation_order)%2
216+
centered_stencil_length = approximation_order + 2*Int(floor(derivative_order/2)) + (approximation_order%2)
217217
boundary_stencil_length = derivative_order + approximation_order
218-
endpoint = (stencil_length-1)/2
219-
dummy_x = -endpoint : endpoint
220-
left_boundary_x = 0:(boundary_stencil_length-1)
221-
right_boundary_x = reverse(-boundary_stencil_length+1:0)
218+
endpoint = div(centered_stencil_length, 2)
219+
dummy_x = 1-endpoint : endpoint
220+
left_boundary_x = 1:(boundary_stencil_length)
221+
right_boundary_x = reverse(-boundary_stencil_length:-1)
222222

223223
# ? Is fornberg valid when taking an x0 outside of the stencil i.e at the boundary?
224-
xoffset = convert(T, 0.5).*(-div(centered_stencil_length,2) : div(centered_stencil_length,2))
225-
224+
xoffset = 1.5:boundary_stencil_length+0.5
226225

227-
boundary_point_count = div(stencil_length,2) # -1 due to the ghost point
226+
boundary_point_count = div(centered_stencil_length,2) # -1 due to the ghost point
228227
# Because it's a N x (N+2) operator, the last stencil on the sides are the [b,0,x,x,x,x] stencils, not the [0,x,x,x,x,x] stencils, since we're never solving for the derivative at the boundary point.
229228
#deriv_spots = (-div(stencil_length,2)+1) : -1 # unused
230-
L_boundary_deriv_spots = left_boundary_x[1:div(stencil_length,2)]
229+
L_boundary_deriv_spots = xoffset[1:div(centered_stencil_length,2)]
231230

232-
stencil_coefs = convert(SVector{stencil_length, T}, (1/dx^derivative_order) * calculate_weights(derivative_order, zero(T), dummy_x))
231+
stencil_coefs = convert(SVector{centered_stencil_length, T}, (1/dx^derivative_order) * calculate_weights(derivative_order, convert(T, 0.5), dummy_x))
233232
# For each boundary point, for each tappoint in the half offset central difference stencil, we need to calculate the coefficients for the stencil.
234-
_low_boundary_coefs = [Dict([offset => convert(SVector{boundary_stencil_length, T}, (1/dx^derivative_order) * calculate_weights(derivative_order, oneunit(T)*x0+offset, left_boundary_x)) for offset in xoffset]) for x0 in L_boundary_deriv_spots]
235-
low_boundary_coefs = convert(SVector{boundary_point_count},vcat(_low_boundary_coefs))
236233

234+
235+
_low_boundary_coefs = [convert(SVector{boundary_stencil_length, T}, (1/dx^derivative_order) * calculate_weights(derivative_order, offset, left_boundary_x)) for offset in L_boundary_deriv_spots]
236+
low_boundary_coefs = convert(SVector{boundary_point_count}, _low_boundary_coefs)
237+
238+
_high_boundary_coefs = [reverse(stencil)*(-1)^derivative_order for stencil in low_boundary_coefs]
239+
high_boundary_coefs = convert(SVector{boundary_point_count}, _high_boundary_coefs)
237240
# _high_boundary_coefs = SVector{boundary_stencil_length, T}[convert(SVector{boundary_stencil_length, T}, (1/dx^derivative_order) * calculate_weights(derivative_order, oneunit(T)*x0, reverse(right_boundary_x))) for x0 in R_boundary_deriv_spots]
238-
inner_reverse(v::AbstractVector) = reverse(map.((x->reverse(x*(-1)^derivative_order),), v)) # reverse 3 ayers deep to get the high boundary cooeffs, but don't reverse in terms of the offset
239-
high_boundary_coefs = convert(SVector{boundary_point_count}, inner_reverse(_low_boundary_coefs))
240241

241242
offside = 0
242243

243-
coefficients = fill!(Vector{T}(undef,len),0)
244+
coefficients = nothing
244245

245246

246-
DerivativeOperator{T,N,false,T,typeof(stencil_coefs),
247-
typeof(low_boundary_coefs),typeof(high_boundary_coefs),typeof(coefficients),
248-
typeof(coeff_func)}(
249-
derivative_order, approximation_order, dx, len, stencil_length,
247+
DerivativeOperator{T,Nothing,false,T,typeof(stencil_coefs),
248+
typeof(low_boundary_coefs),typeof(high_boundary_coefs),typeof(coefficients),Nothing}(
249+
derivative_order, approximation_order, dx, 1, centered_stencil_length,
250250
stencil_coefs,
251251
boundary_stencil_length,
252252
boundary_point_count,
253253
low_boundary_coefs,
254-
high_boundary_coefs,offside,coefficients,coeff_func
254+
high_boundary_coefs,offside,coefficients,nothing
255255
)
256256
end
257257

0 commit comments

Comments
 (0)