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

Commit b558da8

Browse files
author
AndiMD
committed
More tests, type restrictions
1 parent e8a2d96 commit b558da8

File tree

3 files changed

+102
-5
lines changed

3 files changed

+102
-5
lines changed

src/derivative_operators/BC_operators.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,19 @@ end
136136

137137

138138
#implement Neumann and Dirichlet as special cases of RobinBC
139-
NeumannBC::NTuple{2,T}, dx::Union{AbstractVector{T}, T}, order = 1) where T = RobinBC((zero(T), one(T), α[1]), (zero(T), one(T), α[2]), dx, order)
140-
DirichletBC(αl::T, αr::T) where T = RobinBC((one(T), zero(T), αl), (one(T), zero(T), αr), one(T), 2one(T) )
139+
NeumannBC::NTuple{2,T}, dx::Union{AbstractVector{U}, U}, order = 1) where {T<:Number,U<:Real} = RobinBC((zero(T), one(T), α[1]), (zero(T), one(T), α[2]), dx, order)
140+
DirichletBC(αl::T, αr::T) where {T<:Real} = RobinBC((one(T), zero(T), αl), (one(T), zero(T), αr), one(T), 2one(T) )
141+
DirichletBC(U::Type,αl::T, αr::T) where {T<:Number,U<:Real} = RobinBC((one(T), zero(T), αl), (one(T), zero(T), αr), one(U), 2one(U) )
141142
#specialized constructors for Neumann0 and Dirichlet0
142143
Dirichlet0BC(T::Type) = DirichletBC(zero(T), zero(T))
143-
Neumann0BC(dx::Union{AbstractVector{T}, T}, order = 1) where T = NeumannBC((zero(T), zero(T)), dx, order)
144+
Neumann0BC(dx::Union{AbstractVector{T}, T}, order = 1) where {T<:Real} = NeumannBC((zero(T), zero(T)), dx, order)
145+
Neumann0BC(U::Type,dx::Union{AbstractVector{T}, T}, order = 1) where {T<:Real,U<:Number} = NeumannBC((zero(U), zero(U)), dx, order)
144146

145147
# other acceptable argument signatures
146148
#RobinBC(al::T, bl::T, cl::T, dx_l::T, ar::T, br::T, cr::T, dx_r::T, order = 1) where T = RobinBC([al,bl, cl], [ar, br, cr], dx_l, order)
147149

148-
Base.:*(Q::AffineBC, u::AbstractVector) = BoundaryPaddedVector(Q.a_l' u[1:length(Q.a_l)] + Q.b_l, Q.a_r' u[(end-length(Q.a_r)+1):end] + Q.b_r, u)
150+
Base.:*(Q::AffineBC, u::AbstractVector) = BoundaryPaddedVector( Q.a_l' u[1:length(Q.a_l)] + Q.b_l, Q.a_r' u[(end-length(Q.a_r)+1):end] + Q.b_r, u )
151+
149152
Base.:*(Q::PeriodicBC, u::AbstractVector) = BoundaryPaddedVector(u[end], u[1], u)
150153

151154
Base.size(Q::AtomicBC) = (Inf, Inf) #Is this nessecary?

test/robin.jl

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,98 @@ ugeneralextended = G*u
104104

105105

106106
#TODO: Implement tests for BC's that are contingent on the sign of the coefficient on the operator near the boundary
107+
108+
109+
110+
111+
# Test complex Robin BC, uniform grid
112+
113+
# Generate random parameters
114+
al = rand(ComplexF64,5)
115+
bl = rand(ComplexF64,5)
116+
cl = rand(ComplexF64,5)
117+
dx = rand(Float64,5)
118+
ar = rand(ComplexF64,5)
119+
br = rand(ComplexF64,5)
120+
cr = rand(ComplexF64,5)
121+
122+
# Construct 5 arbitrary RobinBC operators for each parameter set
123+
for i in 1:5
124+
125+
Q = RobinBC((al[i], bl[i], cl[i]), (ar[i], br[i], cr[i]), dx[i])
126+
127+
Q_L, Q_b = Array(Q,5i)
128+
129+
#Check that Q_L is is correctly computed
130+
@test Q_L[2:5i+1,1:5i] Array(I, 5i, 5i)
131+
@test Q_L[1,:] [1 / (1-al[i]*dx[i]/bl[i]); zeros(5i-1)]
132+
@test Q_L[5i+2,:] [zeros(5i-1); 1 / (1+ar[i]*dx[i]/br[i])]
133+
134+
#Check that Q_b is computed correctly
135+
@test Q_b [cl[i]/(al[i]-bl[i]/dx[i]); zeros(5i); cr[i]/(ar[i]+br[i]/dx[i])]
136+
137+
# Construct the extended operator and check that it correctly extends u to a (5i+2)
138+
# vector, along with encoding boundary condition information.
139+
u = rand(ComplexF64,5i)
140+
141+
Qextended = Q*u
142+
CorrectQextended = [(cl[i]-(bl[i]/dx[i])*u[1])/(al[i]-bl[i]/dx[i]); u; (cr[i]+ (br[i]/dx[i])*u[5i])/(ar[i]+br[i]/dx[i])]
143+
@test length(Qextended) 5i+2
144+
145+
# Check concretization
146+
@test Array(Qextended) CorrectQextended # Q.a_l ⋅ u[1:length(Q.a_l)] + Q.b_l, Q.a_r ⋅ u[(end-length(Q.a_r)+1):end] + Q.b_r
147+
148+
# Check that Q_L and Q_b correctly compute BoundaryPaddedVector
149+
@test Q_L*u + Q_b CorrectQextended
150+
151+
@test [Qextended[1]; Qextended.u; Qextended[5i+2]] CorrectQextended
152+
153+
end
154+
155+
# Construct 5 arbitrary RobinBC operators w/non-uniform grid
156+
al = rand(ComplexF64,5)
157+
bl = rand(ComplexF64,5)
158+
cl = rand(ComplexF64,5)
159+
dx = rand(Float64,5)
160+
ar = rand(ComplexF64,5)
161+
br = rand(ComplexF64,5)
162+
cr = rand(ComplexF64,5)
163+
for j in 1:2
164+
for i in 1:5
165+
if j == 1
166+
Q = RobinBC((al[i], bl[i], cl[i]), (ar[i], br[i], cr[i]),
167+
dx[i] .* ones(5 * i))
168+
else
169+
Q = RobinBC([al[i], bl[i], cl[i]], [ar[i], br[i], cr[i]],
170+
dx[i] .* ones(5 * i))
171+
end
172+
Q_L, Q_b = Array(Q,5i)
173+
174+
#Check that Q_L is is correctly computed
175+
@test Q_L[2:5i+1,1:5i] Array(I, 5i, 5i)
176+
@test Q_L[1,:] [1 / (1-al[i]*dx[i]/bl[i]); zeros(5i-1)]
177+
@test Q_L[5i+2,:] [zeros(5i-1); 1 / (1+ar[i]*dx[i]/br[i])]
178+
179+
#Check that Q_b is computed correctly
180+
@test Q_b [cl[i]/(al[i]-bl[i]/dx[i]); zeros(5i); cr[i]/(ar[i]+br[i]/dx[i])]
181+
182+
# Construct the extended operator and check that it correctly extends u to a (5i+2)
183+
# vector, along with encoding boundary condition information.
184+
u = rand(ComplexF64,5i)
185+
186+
Qextended = Q*u
187+
CorrectQextended = [(cl[i]-(bl[i]/dx[i])*u[1])/(al[i]-bl[i]/dx[i]); u; (cr[i]+ (br[i]/dx[i])*u[5i])/(ar[i]+br[i]/dx[i])]
188+
@test length(Qextended) 5i+2
189+
190+
# Check concretization
191+
@test Array(Qextended) CorrectQextended
192+
193+
# Check that Q_L and Q_b correctly compute BoundaryPaddedVector
194+
@test Q_L*u + Q_b CorrectQextended
195+
196+
@test [Qextended[1]; Qextended.u; Qextended[5i+2]] CorrectQextended
197+
198+
end
199+
end
200+
201+

test/runtests.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ if GROUP == "All" || GROUP == "Interface"
1212
@time @safetestset "Poisson example" begin include("../examples/poisson.jl") end
1313
@time @safetestset "Heat equation example" begin include("../examples/heat_equation.jl") end
1414
@time @safetestset "Robin Boundary Condition Operators" begin include("robin.jl") end
15-
@time @safetestset "Robin Boundary Condition Operators, Complex" begin include("robin_complex.jl") end
1615
@time @safetestset "JacVec Operators Interface" begin include("jacvec_operators.jl") end
1716
@time @safetestset "Composite Operators Interface" begin include("composite_operators_interface.jl") end
1817
@time @safetestset "BC and Coefficient Compositions" begin include("bc_coeff_compositions.jl") end

0 commit comments

Comments
 (0)