Skip to content

Commit 82afce2

Browse files
committed
Fast Laplacian experiment
1 parent b4addae commit 82afce2

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed

test/TriangleTest.jl

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,164 @@ let P = (n,k,a,b,c,x,y) -> x == 1.0 ? ((1-x))^k*jacobip(n-k,2k+b+c+1,a,1.0)*jaco
4848
end
4949
end
5050

51+
N = 2000
52+
@time f = Fun((x,y) -> cos(1000x*y), Triangle(), sum(1:N));
53+
D_x = Derivative(space(f), [1,0])
54+
@time M_x = D_x[Block.(1:N), Block.(1:N)]
55+
@time (M_x*f.coefficients)
56+
57+
D_x
58+
59+
@time Fun(rangespace(D_x), (M_x*f.coefficients))(0.1,0.2)
60+
(M_x*f.coefficients)
61+
Fun(rangespace(D_x), (M_x*f.coefficients))(0.1,0.2) - (-1000*0.2*sin(1000*0.1*0.2))
62+
63+
struct FastLap
64+
D_x::BandedBlockBandedMatrix{Float64}
65+
D̃_x::BandedBlockBandedMatrix{Float64}
66+
R_x::BandedBlockBandedMatrix{Float64}
67+
R̃_x::BandedBlockBandedMatrix{Float64}
68+
69+
D_y::BandedBlockBandedMatrix{Float64}
70+
D̃_y::BandedBlockBandedMatrix{Float64}
71+
R_y::BandedBlockBandedMatrix{Float64}
72+
R̃_y::BandedBlockBandedMatrix{Float64}
73+
end
74+
75+
76+
function FastLap(N)
77+
S = KoornwinderTriangle(0.,0.,0.)
78+
D_x = Derivative(S, [1,0])
79+
M_x = D_x[Block.(1:N), Block.(1:N)]
80+
D̃_x = Derivative(rangespace(D_x), [1,0])
81+
M̃_x = D̃_x[Block.(1:N), Block.(1:N)]
82+
C_x = Conversion(rangespace(D̃_x) , KoornwinderTriangle(2.0, 1.0, 2.0))
83+
R_x = C_x[Block.(1:N), Block.(1:N)]
84+
C̃_x = Conversion( rangespace(C_x) , KoornwinderTriangle(2.0, 2.0, 2.0))
85+
R̃_x = C̃_x[Block.(1:N), Block.(1:N)]
86+
87+
D_y = Derivative(S, [0,1])
88+
M_y = D_y[Block.(1:N), Block.(1:N)]
89+
D̃_y = Derivative(rangespace(D_y), [0,1])
90+
M̃_y = D̃_y[Block.(1:N), Block.(1:N)]
91+
C_y = Conversion(rangespace(D̃_y) , KoornwinderTriangle(1.0, 2.0, 2.0))
92+
R_y = C_y[Block.(1:N), Block.(1:N)]
93+
C̃_y = Conversion( rangespace(C_y) , KoornwinderTriangle(2.0, 2.0, 2.0))
94+
R̃_y = C̃_y[Block.(1:N), Block.(1:N)]
95+
96+
FastLap(M_x, M̃_x, R_x, R̃_x, M_y, M̃_y, R_y, R̃_y)
97+
end
98+
99+
function Base.:*(L::FastLap, f)
100+
Fun(KoornwinderTriangle(2.,2.,2.), L.R̃_x*(L.R_x*(L.D̃_x*(L.D_x*f.coefficients))) + L.R̃_y*(L.R_y*(L.D̃_y*(L.D_y*f.coefficients))))
101+
end
102+
103+
104+
N = 50
105+
n = sum(1:N)
106+
ω = n/40
107+
@time f = Fun((x,y) -> cos*x*y), Triangle(), n);
108+
plot(abs.(f.coefficients) .+ eps(); yscale=:log10)
109+
@time L = FastLap(N)
110+
@time L*f
111+
112+
x,y = 0.1,0.2
113+
(-ω^2*(x^2+y^2)*cos*x*y) - (L*f)(0.1,0.2))/(L*f)(0.1,0.2)
114+
115+
err = Dict{Int,Float64}()
116+
tim_tran = Dict{Int,Float64}()
117+
tim_lapset = Dict{Int,Float64}()
118+
tim_lap = Dict{Int,Float64}()
119+
120+
121+
Ns = 100:100:2000
122+
for N in Ns
123+
@show N
124+
n = sum(1:N)
125+
ω = N/10
126+
tim_tran[N] = @elapsed(f = Fun((x,y) -> cos*x*y), Triangle(), n))
127+
tim_lapset[N] = @elapsed(L = FastLap(N))
128+
tim_lap[N] = @elapsed(v = L*f)
129+
err[N] = abs((-ω^2*(x^2+y^2)*cos*x*y) - v(x,y))/(-ω^2*(x^2+y^2)*cos*x*y)))
130+
end
131+
132+
133+
plot([sum(1:N) for N in Ns], [err[N] for N in Ns]; legend=false, title="Relative error of laplacian evaluated at (0.1,0.2)",
134+
linewidth=2.0, xscale=:log10, yscale=:log10)
135+
savefig("trianglelaplacianerror.pdf")
136+
137+
plot([sum(1:N) for N in Ns], [tim_tran[N] for N in Ns]; legend=:bottomright, title = "Time to calculate Laplacian coefficients",
138+
label="transform",
139+
linewidth=2.0, xscale=:log10, yscale=:log10)
140+
plot!([sum(1:N) for N in Ns], [tim_lapset[N] for N in Ns]; label="setup",
141+
linewidth=2.0, xscale=:log10, yscale=:log10)
142+
plot!([sum(1:N) for N in Ns], [tim_lap[N] for N in Ns]; label="apply",
143+
linewidth=2.0, xscale=:log10, yscale=:log10)
144+
savefig("trianglelaplaciantimes.pdf")
145+
146+
147+
sum(1:2000)
148+
149+
v(0.1,0.2)
150+
plot(abs.(v.coefficients).+eps(); yscale=:log10)
151+
152+
153+
154+
sum(1:2000)
155+
156+
157+
158+
159+
160+
161+
f.coefficients
162+
163+
164+
D_x = Derivative(space(f), [1,0])
165+
M_x = D_x[Block.(1:N), Block.(1:N)]
166+
D̃_x = Derivative(rangespace(D_x), [1,0])
167+
M̃_x = D̃_x[Block.(1:N), Block.(1:N)]
168+
C_x = Conversion(rangespace(D̃_x) , KoornwinderTriangle(2.0, 1.0, 2.0))
169+
R_x = C_x[Block.(1:N), Block.(1:N)]
170+
C̃_x = Conversion( rangespace(C_x) , KoornwinderTriangle(2.0, 2.0, 2.0))
171+
R̃_x = C̃_x[Block.(1:N), Block.(1:N)]
172+
@time R̃_x*(R_x*(M̃_x*(M_x*f.coefficients)))
173+
174+
typeof(M_x)
175+
176+
rangespace(D̃_x)
177+
178+
179+
L = Laplacian(space(f))
180+
181+
@time L.op.ops[1][Block.(1:100), Block.(1:100)]
182+
183+
184+
@time points(Triangle(), 4_000_000);
185+
186+
@time p = points(Chebyshev()^2, 1_000_000);
187+
S = Chebyshev()^2
188+
N = 1_000_000
189+
T = Float64
190+
d = domain(S)
191+
@time pts = ApproxFun.squarepoints(T, N)
192+
@time for k=1:length(pts)
193+
pts[k] = fromcanonical(d,pts[k])
194+
end
195+
196+
@which fromcanonical(S,pts[1])
197+
198+
@time points(S, 1_000_000)
199+
200+
201+
(1,2) .+ Vec(1,2)
202+
d.domains
203+
fromcanonical.(d.domains, Vec(0.1,0.2))
204+
205+
@time pts .= iduffy.(pts)
206+
# fromcanonical.(Ref(S.domain), )
207+
208+
51209
P = (n,k,a,b,c,x,y) -> x == 1.0 ? ((1-x))^k*jacobip(n-k,2k+b+c+1,a,1.0)*jacobip(k,c,b,-1.0) :
52210
((1-x))^k*jacobip(n-k,2k+b+c+1,a,2x-1)*jacobip(k,c,b,2y/(1-x)-1)
53211

0 commit comments

Comments
 (0)