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

Commit a680382

Browse files
Merge pull request #160 from xtalax/multidimbc
Final BC touchups
2 parents e602ab3 + 9df78b1 commit a680382

File tree

4 files changed

+39
-13
lines changed

4 files changed

+39
-13
lines changed

src/derivative_operators/BC_operators.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Robin, General, and in general Neumann, Dirichlet and Bridge BCs are all affine
88
"""
99
abstract type AffineBC{T} <: AtomicBC{T} end
1010

11+
struct NeumannBC{N} end
12+
struct Neumann0BC{N} end
13+
struct DirichletBC{N} end
14+
struct Dirichlet0BC{N} end
1115
"""
1216
q = PeriodicBC{T}()
1317
@@ -18,6 +22,7 @@ Creates a periodic boundary condition, where the lower index end of some u is ex
1822
It is not reccomended to concretize this BC type in to a BandedMatrix, since the vast majority of bands will be all 0s. SpatseMatrix concretization is reccomended.
1923
"""
2024
struct PeriodicBC{T} <: AtomicBC{T}
25+
PeriodicBC(T::Type) = new{T}()
2126
end
2227

2328
"""

src/derivative_operators/multi_dim_bc_operators.jl

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ struct ComposedMultiDimBC{T, B<:AtomicBC{T}, N,M} <: MultiDimensionalBC{T, N}
5454
BCs::Vector{Array{B, M}}
5555
end
5656

57+
5758
"""
5859
A multiple dimensional BC, supporting arbitrary BCs at each boundary point.
5960
To construct an arbitrary BC, pass an Array of BCs with dimension `N-1`, if `N` is the dimensionality of your domain `u`
@@ -82,25 +83,37 @@ For Neumann0BC, please use
8283
Qx, Qy, Qz... = Neumann0BC(T::Type, (dx::Vector, dy::Vector, dz::Vector ...), approximation_order, size(u))
8384
where T is the element type of the domain to be extended
8485
"""
85-
MultiDimBC(BC::Array{B,N}, dim::Integer) where {N, B<:AtomicBC} = MultiDimDirectionalBC{gettype(BC[1]), B, dim, N+1, N}(BC)
86+
struct MultiDimBC{N} end
87+
88+
89+
MultiDimBC{dim}(BC::Array{B,N}) where {N, B<:AtomicBC, dim} = MultiDimDirectionalBC{gettype(BC[1]), B, dim, N+1, N}(BC)
8690
#s should be size of the domain
87-
MultiDimBC(BC::B, s, dim::Integer) where {B<:AtomicBC} = MultiDimDirectionalBC{gettype(BC), B, dim, length(s), length(s)-1}(fill(BC, s[setdiff(1:length(s), dim)]))
91+
MultiDimBC{dim}(BC::B, s) where {B<:AtomicBC, dim} = MultiDimDirectionalBC{gettype(BC), B, dim, length(s), length(s)-1}(fill(BC, s[setdiff(1:length(s), dim)]))
8892

8993
#Extra constructor to make a set of BC operators that extend an atomic BC Operator to the whole domain
9094
#Only valid in the uniform grid case!
9195
MultiDimBC(BC::B, s) where {B<:AtomicBC} = Tuple([MultiDimDirectionalBC{gettype(BC), B, dim, length(s), length(s)-1}(fill(BC, s[setdiff(1:length(s), dim)])) for dim in 1:length(s)])
9296

9397
# Additional constructors for cases when the BC is the same for all boundarties
98+
PeriodicBC{dim}(T,s) where dim = MultiDimBC{dim}(PeriodicBC(T), s)
99+
PeriodicBC(T,s) = MultiDimBC(PeriodicBC(T), s)
94100

95-
PeriodicBC{T}(s) where T = MultiDimBC(PeriodicBC{T}(), s)
96-
101+
NeumannBC{dim}::NTuple{2,T}, dx, order, s) where {T,dim} = RobinBC{dim}((zero(T), one(T), α[1]), (zero(T), one(T), α[2]), dx, order, s)
97102
NeumannBC::NTuple{2,T}, dxyz, order, s) where T = RobinBC((zero(T), one(T), α[1]), (zero(T), one(T), α[2]), dxyz, order, s)
103+
104+
DirichletBC{dim}(αl::T, αr::T, s) where {T,dim} = RobinBC{dim}((one(T), zero(T), αl), (one(T), zero(T), αr), 1.0, 2.0, s)
98105
DirichletBC(αl::T, αr::T, s) where T = RobinBC((one(T), zero(T), αl), (one(T), zero(T), αr), [ones(T, si) for si in s], 2.0, s)
99106

107+
Dirichlet0BC{dim}(T::Type, s) where {dim} = DirichletBC{dim}(zero(T), zero(T), s)
100108
Dirichlet0BC(T::Type, s) = DirichletBC(zero(T), zero(T), s)
109+
110+
Neumann0BC{dim}(T::Type, dx, order, s) where {dim} = NeumannBC{dim}((zero(T), zero(T)), dx, order, s)
101111
Neumann0BC(T::Type, dxyz, order, s) = NeumannBC((zero(T), zero(T)), dxyz, order, s)
102112

113+
RobinBC{dim}(l::NTuple{3,T}, r::NTuple{3,T}, dx, order, s) where {T,dim} = MultiDimBC{dim}(RobinBC(l, r, dx, order), s)
103114
RobinBC(l::NTuple{3,T}, r::NTuple{3,T}, dxyz, order, s) where {T} = Tuple([MultiDimDirectionalBC{T, RobinBC{T}, dim, length(s), length(s)-1}(fill(RobinBC(l, r, dxyz[dim], order), perpindex(s,dim))) for dim in 1:length(s)])
115+
116+
GeneralBC{dim}(αl::AbstractVector{T}, αr::AbstractVector{T}, dx, order, s) where {T,dim} = MultiDimBC{dim}(GeneralBC(αl, αr, dx, order), s)
104117
GeneralBC(αl::AbstractVector{T}, αr::AbstractVector{T}, dxyz, order, s) where {T} = Tuple([MultiDimDirectionalBC{T, GeneralBC{T}, dim, length(s), length(s)-1}(fill(GeneralBC(αl, αr, dxyz[dim], order),perpindex(s,dim))) for dim in 1:length(s)])
105118

106119

@@ -118,7 +131,7 @@ Creates a ComposedMultiDimBC operator, Q, that extends every boundary when appli
118131
119132
Qx Qy and Qz can be passed in any order, as long as there is exactly one BC operator that extends each dimension.
120133
"""
121-
function compose(BCs...)
134+
function compose(BCs::MultiDimDirectionalBC...)
122135
T = gettype(BCs[1])
123136
N = ndims(BCs[1])
124137
Ds = getaxis.(BCs)
@@ -131,6 +144,8 @@ function compose(BCs...)
131144
ComposedMultiDimBC{T, Union{eltype.(BC.BCs for BC in BCs)...}, N,N-1}([condition.BCs for condition in BCs])
132145
end
133146

147+
Base.:+(BCs::MultiDimDirectionalBC...) = compose(BCs...)
148+
134149
"""
135150
Qx, Qy,... = decompose(Q::ComposedMultiDimBC{T,N,M})
136151

test/2D_3D_fast_multiplication.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ end
313313
# Test a single axis, multiple operators: (Lyy+Lyyyy)*M, dx = 1.0
314314
A = Ly2+Ly4
315315
mul!(M_temp, A, M)
316-
@test M_temp ((Ly2*M) + (Ly4*M))
316+
@test isapprox(M_temp, ((Ly2*M) + (Ly4*M)), atol = 1e-7)
317317

318318
# Test a single axis, multiple operators: (Lyy++Lyyy+Lyyyy)*M, dx = 1.0
319319
A += Ly3

test/MultiDimBC_test.jl

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ A = rand(n,m)
1010

1111
#Create atomic BC
1212
q1 = RobinBC((1.0, 2.0, 3.0), (0.0, -1.0, 2.0), 0.1, 4.0)
13-
q2 = PeriodicBC{Float64}()
13+
q2 = PeriodicBC(Float64)
1414

1515
BCx = vcat(fill(q1, div(m,2)), fill(q2, m-div(m,2))) #The size of BCx has to be all size components *except* for x
1616
BCy = vcat(fill(q1, div(n,2)), fill(q2, n-div(n,2)))
1717

1818

19-
Qx = MultiDimBC(BCx, 1)
20-
Qy = MultiDimBC(BCy, 2)
19+
Qx = MultiDimBC{1}(BCx)
20+
Qy = MultiDimBC{2}(BCy)
2121

2222
Ax = Qx*A
2323
Ay = Qy*A
@@ -45,16 +45,22 @@ A = rand(n,m, o)
4545

4646
#Create atomic BC
4747
q1 = RobinBC((1.0, 2.0, 3.0), (0.0, -1.0, 2.0), 0.1, 4.0)
48-
q2 = PeriodicBC{Float64}()
48+
q2 = PeriodicBC(Float64)
4949

5050
BCx = vcat(fill(q1, (div(m,2), o)), fill(q2, (m-div(m,2), o))) #The size of BCx has to be all size components *except* for x
5151
BCy = vcat(fill(q1, (div(n,2), o)), fill(q2, (n-div(n,2), o)))
5252
BCz = fill(Dirichlet0BC(Float64), (n,m))
5353

54-
Qx = MultiDimBC(BCx, 1)
55-
Qy = MultiDimBC(BCy, 2)
56-
Qz = MultiDimBC(Dirichlet0BC(Float64), size(A), 3) #Test the other constructor
54+
Qx = MultiDimBC{1}(BCx)
55+
Qy = MultiDimBC{2}(BCy)
56+
Qz = Dirichlet0BC{3}(Float64, size(A)) #Test the other constructor
5757

58+
Q1 = (Qx+Qy+Qz)
59+
Q2 = compose(Qx,Qy,Qz) #test addition combinations
60+
@test_broken Q1 == Q2 #This fails
61+
@test all(Q1.BCs .== Q2.BCs) # but this passes so it does actually work
62+
@test_broken Qtmp = Qx + Qz
63+
@test_skip Qz+Qx+Qy == Qy+Qtmp
5864
Ax = Qx*A
5965
Ay = Qy*A
6066
Az = Qz*A

0 commit comments

Comments
 (0)