Skip to content

Commit 51c22f4

Browse files
committed
stash
1 parent b8fd254 commit 51c22f4

File tree

11 files changed

+433
-95
lines changed

11 files changed

+433
-95
lines changed

src/MOLFiniteDifference.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ end
99
function MOLFiniteDifference(dxs, time=nothing; approx_order = 2, grid_align=CenterAlignedGrid())
1010

1111
if approx_order % 2 != 0
12-
warn("Discretization approx_order must be even, rounding up to $(approx_order+1)")
12+
@warn "Discretization approx_order must be even, rounding up to $(approx_order+1)"
1313
end
1414
return MOLFiniteDifference{typeof(grid_align)}(dxs, time, approx_order, grid_align)
1515
end

src/bcs/generate_bc_eqs.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ function BoundaryHandler!!(u0, bceqs, bcs, s::DiscreteSpace, depvar_ops, tspan,
9494
end
9595

9696
@assert boundary !== nothing "Boundary condition $bc is not on a boundary of the domain, or is not a valid boundary condition"
97-
@show bc
9897
push!(bceqs, vec(map(s.Iedge[x_][idx(boundary)]) do II
9998
rules = generate_bc_rules(II, derivweights, s, bc, u_, x_, boundary)
10099

@@ -123,7 +122,6 @@ function generate_bc_rules(II, derivweights, s::DiscreteSpace{N,M,G}, bc, u_, x_
123122
break
124123
end
125124
end
126-
@show depvarderivbcmaps
127125
fd_rules = generate_finite_difference_rules(II, s, bc, derivweights)
128126
varrules = axiesvals(s, x_, II)
129127

src/bcs/scratch.jl

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,65 @@ using DiffEqOperators
44
"""
55
Inner function for `get_half_offset_weights_and_stencil` (see below).
66
"""
7-
function _get_weights_and_stencil(D, I)
7+
function _get_weights_and_stencil(D, I, len=0)
8+
if len == 20
9+
clip = false
10+
else
11+
clip = true
12+
end
813
# k => i of itap -
914
# offset is important due to boundary proximity
1015
# The low boundary coeffs has a heirarchy of coefficients following: number of indices from boundary -> which half offset point does it correspond to -> weights
1116
if I <= (D.boundary_point_count)
1217
println("low boundary")
1318
weights = D.low_boundary_coefs[I]
19+
weight = I
1420
offset = 1 - I
1521
Itap = [I + (i+offset) for i in 0:(D.boundary_stencil_length-1)]
16-
elseif I > (20 - D.boundary_point_count)
22+
elseif I > (len - D.boundary_point_count)
1723
println("high boundary")
1824

19-
weights = D.high_boundary_coefs[20-I+1]
20-
offset = 20 - I
21-
Itap = [I + (i+offset) for i in (-D.boundary_stencil_length+1):1:0]
25+
weights = D.high_boundary_coefs[len-I + !clip]
26+
weight = -(len - I + !clip)
27+
offset = len - I
28+
Itap = [I + (i+offset+clip) for i in (-D.boundary_stencil_length+1):1:0]
2229
else
2330
println("interior")
24-
31+
weight = 0
2532
weights = D.stencil_coefs
2633
Itap = [I + i for i in (1-div(D.stencil_length,2)):(div(D.stencil_length,2))]
2734
end
2835

29-
return (weights, Itap)
36+
return (weights, Itap, weight)
3037
end
3138

3239

33-
function clip(I, n)
34-
return I>(n/2) ? I-1 : I+1
40+
function clip(I)
41+
return I-1
3542

3643
end
3744

3845
for I in [2,19, 3, 18, 10]
3946

40-
D = CompleteHalfCenteredDifference(1, 6, 1.0)
47+
D = CompleteHalfCenteredDifference(1, 2, 1.0)
4148

42-
49+
x = 1:20
4350

44-
outerweights, outerstencil = _get_weights_and_stencil(D, clip(I,20))
51+
outerweights, outerstencil, outerweight = _get_weights_and_stencil(D, clip(I), 19)
4552

4653
# Get the correct weights and stencils for this II
4754

4855
@show I
56+
@show outerweight
4957
@show outerweights
5058
@show outerstencil
5159

52-
5360
for II in outerstencil
5461
println()
5562
println("II: ", II)
5663
println()
57-
weights, stencil = _get_weights_and_stencil(D, II)
64+
weights, stencil, weight = _get_weights_and_stencil(D, II, 20)
65+
@show weight
5866
println(weights)
5967
println(stencil)
6068
end

src/bcs/scratch2.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using ModelingToolkit, MethodOfLines
2+
3+
@parameters t x
4+
@variables u(..)
5+
Dx = Differential(x)
6+
Dt = Differential(t)
7+
t_min= 0.
8+
t_max = 2.
9+
x_min = 0.
10+
x_max = 2.
11+
c = 1.0
12+
a = 1.0
13+
14+
# Equation
15+
eq = Dt(u(t,x)) ~ Dx(u(t,x) * Dx(u(t,x)))
16+
17+
terms = MethodOfLines.split_additive_terms(eq)
18+
19+
r = @rule ($(Differential(x))(u(t,x) * $(Differential(x))(u(t,x)))) => 0
20+
21+
@show r(terms[2])

src/discretization/differential_discretizer.jl

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,15 @@ Get the weights and stencil for the inner half offset centered difference for th
7171
Does not discretize so that the weights can be used in a replacement rule
7272
TODO: consider refactoring this to harmonize with centered difference
7373
"""
74-
function get_half_offset_weights_and_stencil(D, II, s, jx)
74+
function get_half_offset_weights_and_stencil(D::DerivativeOperator, II::CartesianIndex, s::DiscreteSpace, jx, len::Int = 0)
7575
j, x = jx
76+
# Check if the index is clipped because we need the outerweights
77+
if len == 0
78+
len = length(s, x)
79+
clip = false
80+
else
81+
clip = true
82+
end
7683
# unit index in direction of the derivative
7784
I1 = unitindices(nparams(s))[j]
7885
# offset is important due to boundary proximity
@@ -81,10 +88,12 @@ function get_half_offset_weights_and_stencil(D, II, s, jx)
8188
weights = D.low_boundary_coefs[II[j]]
8289
offset = 1 - II[j]
8390
Itap = [II + (i+offset)*I1 for i in 0:(D.boundary_stencil_length-1)]
84-
elseif II[j] > (length(s, x) - D.boundary_point_count)
85-
weights = D.high_boundary_coefs[length(s, x)-II[j]+1]
86-
offset = length(s, x) - II[j]
87-
Itap = [II + (i+offset)*I1 for i in (-D.boundary_stencil_length+1):1:0]
91+
elseif II[j] > (len - D.boundary_point_count)
92+
# we need !clip to shift the index, as unclipped indices start from 1 not 2
93+
weights = D.high_boundary_coefs[len-II[j] + !clip]
94+
offset = len - II[j]
95+
#use clip to shift the stencil to the right place
96+
Itap = [II + (i+offset+clip)*I1 for i in (-D.boundary_stencil_length+1):1:0]
8897
else
8998
weights = D.stencil_coefs
9099
Itap = [II + i*I1 for i in (1-div(D.stencil_length,2)):(div(D.stencil_length,2))]

src/discretization/generate_finite_difference_rules.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
Interpolate gridpoints by taking the average of the values of the discrete points, or if the offset is outside the grid, extrapolate the value with dx.
55
"""
66
@inline function interpolate_discrete_param(i, s, itap, x, bpc)
7-
return s.grid[x][i+itap]+s.dxs[x]*.5
7+
if i+itap > (length(s, x) - bpc)
8+
return s.grid[x][i+itap]-s.dxs[x]*.5
9+
10+
else
11+
return s.grid[x][i+itap]+s.dxs[x]*.5
12+
end
813
end
914

1015
"""
@@ -46,7 +51,7 @@ function cartesian_nonlinear_laplacian(expr, II, derivweights, s, x, u)
4651
inner_interpolater = derivweights.interpmap[x]
4752

4853
# Get the outer weights and stencil. clip() essentially removes a point from either end of the grid, for this reason this function is only defined on the interior, not in bcs
49-
outerweights, outerstencil = get_half_offset_weights_and_stencil(D_inner, clip(II, s, j, D_inner.boundary_point_count), s, jx)
54+
outerweights, outerstencil = get_half_offset_weights_and_stencil(D_inner, clip(II, s, j, D_inner.boundary_point_count), s, jx, length(s, x) - 1)
5055

5156
# Get the correct weights and stencils for this II
5257
inner_deriv_weights_and_stencil = [get_half_offset_weights_and_stencil(D_inner, I, s, jx) for I in outerstencil]

test/components/finite_diff_schemes.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ domains = [t ∈ Interval(t_min, t_max), x ∈ Interval(x_min, x_max)]
8080

8181
s = MethodOfLines.DiscreteSpace(domains, depvars, indvars, x̄, disc)
8282

83-
derivweights = MethodOfLines.DifferentialDiscretizer(pde, s, disc)
83+
derivweights = MethodOfLines.DifferentialDiscretizer(pde, bcs, s, disc)
8484

8585
II = s.Igrid[10]
8686

@@ -118,7 +118,7 @@ end
118118

119119
s = MethodOfLines.DiscreteSpace(domains, depvars, indvars, x̄, disc)
120120

121-
derivweights = MethodOfLines.DifferentialDiscretizer(pde, s, disc)
121+
derivweights = MethodOfLines.DifferentialDiscretizer(pde, bcs, s, disc)
122122

123123
for II in s.Igrid[2:end-1]
124124
#TODO Test Interpolation of params

0 commit comments

Comments
 (0)