forked from SciML/DiffEqOperators.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_dim_bc_test.jl
143 lines (109 loc) · 3.82 KB
/
multi_dim_bc_test.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
using LinearAlgebra, SparseArrays, DiffEqOperators, Random, Test
################################################################################
# Test 2d extension
################################################################################
Random.seed!(7373)
@testset "2d extension" begin
#Create Array
n = 8
m = 15
A = rand(n,m)
#Create atomic BC
q1 = RobinBC((1.0, 2.0, 3.0), (0.0, -1.0, 2.0), 0.1, 4.0)
q2 = PeriodicBC(Float64)
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
BCy = vcat(fill(q2, div(n,2)), fill(q1, n-div(n,2)))
Qx = MultiDimBC{1}(BCx)
Qy = MultiDimBC{2}(BCy)
Q = compose(Qx, Qy)
Ax = Qx*A
Ay = Qy*A
@test size(Ax)[1] == size(A)[1]+2
@test size(Ay)[2] == size(A)[2]+2
for j in 1:m
@test Ax[:, j] == Array(BCx[j]*A[:, j])
end
for i in 1:n
@test Ay[i,:] == Array(BCy[i]*A[i,:])
end
Q*A == compose(Ax, Ay)
end
@testset "2d extension with constructor test" begin
n = 8
m = 15
A = rand(n,m)
q1 = q2 = PeriodicBC(Float64)
Qx, Qy = PeriodicBC(Float64, (n,m))
Q = compose(Qx, Qy)
BCx = vcat(collect(fill(q1, div(m,2))), collect(fill(q2, m-div(m,2)))) #The size of BCx has to be all size components *except* for x
BCy = vcat(collect(fill(q1, div(n,2))), collect(fill(q2, n-div(n,2)))) #The size of BCx has to be all size components *except* for x
Qx1 = MultiDimBC{1}(BCx)
Qy1 = MultiDimBC{2}(BCy)
Ax = Qx1*A
Ay = Qy1*A
@test size(Ax)[1] == size(A)[1]+2
@test size(Ay)[2] == size(A)[2]+2
for j in 1:m
@test Ax[:, j] == Array(BCx[j]*A[:, j])
end
for i in 1:n
@test Ay[i,:] == Array(BCy[i]*A[i,:])
end
@test Q*A == compose(Ax,Ay)
end
################################################################################
# Test 3d extension
################################################################################
@testset "3d extension" begin
#Create Array
n = 8
m = 11
o = 12
A = rand(n,m, o)
#Create atomic BC
q1 = RobinBC((1.0, 2.0, 3.0), (0.0, -1.0, 2.0), 0.1, 4.0)
q2 = PeriodicBC(Float64)
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
BCy = vcat(fill(q1, (div(n,2), o)), fill(q2, (n-div(n,2), o)))
BCz = fill(Dirichlet0BC(Float64), (n,m))
Qx = MultiDimBC{1}(BCx)
Qy = MultiDimBC{2}(BCy)
Qz = MultiDimBC{3}(Dirichlet0BC(Float64), size(A)) #Test the other constructor
Ax = Qx*A
Ay = Qy*A
Az = Qz*A
Q = compose(Qx,Qy,Qz)
QL, Qb = Array(Q, size(A))
QLs, Qbs = sparse(Q, size(A))
A_conc = QL*reshape(A, prod(size(A))) .+ Qb
A_conc_sp = QLs*reshape(A,prod(size(A))) .+ Qbs
#test BC concretization
A_arr = Array(Q*A)
@test reshape(A_arr, prod(size(A_arr))) ≈ A_conc_sp ≈ A_conc
@test size(Ax)[1] == size(A)[1]+2
@test size(Ay)[2] == size(A)[2]+2
@test size(Az)[3] == size(A)[3]+2
for j in 1:m, k in 1:o
@test Ax[:, j, k] == Array(BCx[j, k]*A[:, j, k])
end
for i in 1:n, k in 1:o
@test Ay[i, :, k] == Array(BCy[i, k]*A[i, :, k])
end
for i in 1:n, j in 1:m
@test Az[i, j, :] == Array(BCz[i, j]*A[i, j, :])
end
end
#test compositions to higher dimension
@testset "Higher dimension test" begin
for N in 2:6
sizes = rand(4:7, N)
local A = rand(sizes...)
Q1_N = RobinBC(Tuple(rand(3)), Tuple(rand(3)), fill(0.1, N), 4.0, size(A))
local Q = compose(Q1_N...)
A1_N = Q1_N.*fill(A, N)
local A_arr = Array(Q*A)
Q_l, Q_b = sparse(Q, size(A))
@test A_arr ≈ Array(compose(A1_N...))
@test A_arr ≈ reshape(Q_l*reshape(A, length(A)) .+ Q_b, size(A_arr)) #Test concretization
end
end