|
| 1 | +# module MultivariateOrthogonalPolynomialsMakieExt |
| 2 | + |
| 3 | +using GLMakie |
| 4 | +using MultivariateOrthogonalPolynomials, ClassicalOrthogonalPolynomials, StaticArrays |
| 5 | +import Makie: mesh, mesh! |
| 6 | +using ContinuumArrays: plotgridvalues |
| 7 | + |
| 8 | +export contourf, contourf |
| 9 | + |
| 10 | +contourf(f::Fun; kwds...) = _mesh(meshdata(f)...; shading=false, kwds...) |
| 11 | +contourf!(s, f::Fun; kwds...) = _mesh!(s, meshdata(f)...; shading=false, kwds...) |
| 12 | + |
| 13 | + |
| 14 | +function _mesh(p, T, v; resolution=(400,400), kwds...) |
| 15 | + T_mat = Array{Int}(undef, length(T), 3) |
| 16 | + for k = 1:length(T) |
| 17 | + T_mat[k,:] .= T[k] |
| 18 | + end |
| 19 | + s = Scene(resolution=resolution) |
| 20 | + mesh!(s, [first.(p) last.(p)], T_mat; color=v, kwds...) |
| 21 | +end |
| 22 | + |
| 23 | + |
| 24 | +function _surface(p, T, v; resolution=(400,400), kwds...) |
| 25 | + T_mat = Array{Int}(undef, length(T), 3) |
| 26 | + for k = 1:length(T) |
| 27 | + T_mat[k,:] .= T[k] |
| 28 | + end |
| 29 | + # s = Scene(resolution=resolution) |
| 30 | + mesh(first.(p), last.(p), vec(v), T_mat; kwds...) |
| 31 | +end |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +function _mesh!(s, p, T, v; kwds...) |
| 36 | + T_mat = Array{Int}(undef, length(T), 3) |
| 37 | + for k = 1:length(T) |
| 38 | + T_mat[k,:] .= T[k] |
| 39 | + end |
| 40 | + mesh!(s, [first.(p) last.(p)], T_mat; color=v, kwds...) |
| 41 | +end |
| 42 | + |
| 43 | +function meshdata(f::Fun{<:PiecewiseSpace}) |
| 44 | + pTv = MultivariateTriangle.meshdata.(components(f)) |
| 45 | + p = vcat(first.(pTv)...) |
| 46 | + T = pTv[1][2] |
| 47 | + cs = length(pTv[1][1]) |
| 48 | + for k = 2:length(pTv) |
| 49 | + append!(T, (t -> (cs.+t)).(pTv[k][2])) |
| 50 | + cs += length(pTv[k][1]) |
| 51 | + end |
| 52 | + |
| 53 | + v = vcat(last.(pTv)...) |
| 54 | + |
| 55 | + p, T, v |
| 56 | +end |
| 57 | + |
| 58 | +function meshdata(f::Fun{<:TensorSpace{<:Tuple{<:Chebyshev,<:Chebyshev}}}) |
| 59 | + p = points(f) |
| 60 | + v = values(f) |
| 61 | + n = length(p) |
| 62 | + T = Vector{NTuple{3,Int}}() |
| 63 | + d_x,d_y = factors(domain(f)) |
| 64 | + a_x,b_x = endpoints(d_x) |
| 65 | + a_y,b_y = endpoints(d_y) |
| 66 | + if iseven(_padua_length(n)) |
| 67 | + l = floor(Int, (1+sqrt(1+8n))/4) |
| 68 | + |
| 69 | + push!(p, Vec(b_x,b_y)) |
| 70 | + push!(p, Vec(a_x,b_y)) |
| 71 | + |
| 72 | + push!(v, f(b_x,b_y)) |
| 73 | + push!(v, f(a_x,b_y)) |
| 74 | + |
| 75 | + for p = 0:l-2 |
| 76 | + for k = (2p*l)+1:(2p*l)+l-1 |
| 77 | + push!(T, (k+1, k, l+k+1)) |
| 78 | + end |
| 79 | + for k = (2p*l)+1:(2p*l)+l-1 |
| 80 | + push!(T, (k, l+k, l+k+1)) |
| 81 | + end |
| 82 | + for k = (2p*l)+l+1:(2p*l)+2l-1 |
| 83 | + push!(T, (k+1, k, l+k)) |
| 84 | + end |
| 85 | + for k = (2p*l)+l+2:(2p*l)+2l |
| 86 | + push!(T, (k, k+l-1, l+k)) |
| 87 | + end |
| 88 | + end |
| 89 | + for p=0:l-3 |
| 90 | + push!(T, ((2p+1)*l+1, (2p+2)*l+1, (2p+3)*l+1)) |
| 91 | + end |
| 92 | + for p =0:l-2 |
| 93 | + push!(T, ((2p+1)*l, (2p+2)*l, (2p+3)*l)) |
| 94 | + end |
| 95 | + push!(T, (1, n+1, l+1)) |
| 96 | + push!(T, (n-2l+1, n+2, n-l+1)) |
| 97 | + else |
| 98 | + l = floor(Int, (3+sqrt(1+8n))/4) |
| 99 | + |
| 100 | + push!(p, Vec(a_x,b_y)) |
| 101 | + push!(p, Vec(a_x,a_y)) |
| 102 | + |
| 103 | + push!(v, f(a_x,b_y)) |
| 104 | + push!(v, f(a_x,a_y)) |
| 105 | + |
| 106 | + for p = 0:l-2 |
| 107 | + for k = p*(2l-1)+1:p*(2l-1)+l-1 |
| 108 | + push!(T, (k+1, k, l+k)) |
| 109 | + end |
| 110 | + for k = p*(2l-1)+1:p*(2l-1)+l-2 |
| 111 | + push!(T, (k+1, l+k, l+k+1)) |
| 112 | + end |
| 113 | + end |
| 114 | + for p = 0:l-3 |
| 115 | + for k = p*(2l-1)+l+1:p*(2l-1)+2l-2 |
| 116 | + push!(T, (k+1, k, l+k)) |
| 117 | + end |
| 118 | + for k = p*(2l-1)+l+1:p*(2l-1)+2l-1 |
| 119 | + push!(T, (k, k+l-1, l+k)) |
| 120 | + end |
| 121 | + end |
| 122 | + |
| 123 | + for p=0:l-3 |
| 124 | + push!(T, (p*(2l-1) + 1, p*(2l-1) + l+1, p*(2l-1) + 2l)) |
| 125 | + end |
| 126 | + |
| 127 | + for p=0:l-3 |
| 128 | + push!(T, (p*(2l-1) + l, p*(2l-1) + 2l-1, p*(2l-1) + 3l-1)) |
| 129 | + end |
| 130 | + |
| 131 | + push!(T, (n-2l+2, n+1, n-l+2)) |
| 132 | + push!(T, (n-l+1, n+2, n)) |
| 133 | + end |
| 134 | + |
| 135 | + p, T, v |
| 136 | +end |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | +meshdata(f) = |
| 141 | + triangle_meshdata(points(f), values(f), (domain(f).a, domain(f).b, domain(f).c), |
| 142 | + f.((domain(f).a, domain(f).b, domain(f).c))) |
| 143 | + |
| 144 | +function triangle_meshdata(p, v, (a, b, c), (fa, fb, fc)) |
| 145 | + n = length(p) |
| 146 | + T = Vector{NTuple{3,Int}}() |
| 147 | + |
| 148 | + |
| 149 | + if iseven(_padua_length(n)) |
| 150 | + l = floor(Int, (1+sqrt(1+8n))/4) |
| 151 | + |
| 152 | + push!(p, b) |
| 153 | + push!(p, c) |
| 154 | + |
| 155 | + push!(v, fb) |
| 156 | + push!(v, fc) |
| 157 | + |
| 158 | + for p = 0:l-2 |
| 159 | + for k = (2p*l)+1:(2p*l)+l-1 |
| 160 | + push!(T, (k+1, k, l+k+1)) |
| 161 | + end |
| 162 | + for k = (2p*l)+1:(2p*l)+l-1 |
| 163 | + push!(T, (k, l+k, l+k+1)) |
| 164 | + end |
| 165 | + for k = (2p*l)+l+1:(2p*l)+2l-1 |
| 166 | + push!(T, (k+1, k, l+k)) |
| 167 | + end |
| 168 | + for k = (2p*l)+l+2:(2p*l)+2l |
| 169 | + push!(T, (k, k+l-1, l+k)) |
| 170 | + end |
| 171 | + end |
| 172 | + for p=0:l-3 |
| 173 | + push!(T, ((2p+1)*l+1, (2p+2)*l+1, (2p+3)*l+1)) |
| 174 | + end |
| 175 | + for p =0:l-2 |
| 176 | + push!(T, ((2p+1)*l, (2p+2)*l, (2p+3)*l)) |
| 177 | + end |
| 178 | + push!(T, (1, n+1, l+1)) |
| 179 | + push!(T, (n-2l+1, n+2, n-l+1)) |
| 180 | + else |
| 181 | + l = floor(Int, (3+sqrt(1+8n))/4) |
| 182 | + |
| 183 | + push!(p, Vec(c)) |
| 184 | + push!(p, Vec(a)) |
| 185 | + |
| 186 | + push!(v, fc) |
| 187 | + push!(v, fa) |
| 188 | + |
| 189 | + for p = 0:l-2 |
| 190 | + for k = p*(2l-1)+1:p*(2l-1)+l-1 |
| 191 | + push!(T, (k+1, k, l+k)) |
| 192 | + end |
| 193 | + for k = p*(2l-1)+1:p*(2l-1)+l-2 |
| 194 | + push!(T, (k+1, l+k, l+k+1)) |
| 195 | + end |
| 196 | + end |
| 197 | + for p = 0:l-3 |
| 198 | + for k = p*(2l-1)+l+1:p*(2l-1)+2l-2 |
| 199 | + push!(T, (k+1, k, l+k)) |
| 200 | + end |
| 201 | + for k = p*(2l-1)+l+1:p*(2l-1)+2l-1 |
| 202 | + push!(T, (k, k+l-1, l+k)) |
| 203 | + end |
| 204 | + end |
| 205 | + |
| 206 | + for p=0:l-3 |
| 207 | + push!(T, (p*(2l-1) + 1, p*(2l-1) + l+1, p*(2l-1) + 2l)) |
| 208 | + end |
| 209 | + |
| 210 | + for p=0:l-3 |
| 211 | + push!(T, (p*(2l-1) + l, p*(2l-1) + 2l-1, p*(2l-1) + 3l-1)) |
| 212 | + end |
| 213 | + |
| 214 | + push!(T, (n-2l+2, n+1, n-l+2)) |
| 215 | + push!(T, (n-l+1, n+2, n)) |
| 216 | + end |
| 217 | + |
| 218 | + p, T, v |
| 219 | +end |
| 220 | + |
| 221 | + |
| 222 | +P = JacobiTriangle() |
| 223 | +f = expand(P, splat((x,y) -> cos(x*exp(y)))) |
| 224 | +(a,b,c) = (SVector(0.,0.), SVector(0.,1.), SVector(1.,0.)) |
| 225 | +xy,F = plotgridvalues(f) |
| 226 | +x,y = first.(xy),last.(xy) |
| 227 | + |
| 228 | + |
| 229 | + |
| 230 | +triangle_meshdata(plotgridvalues(f)..., (a,b,c), getindex.(Ref(f), (a,b,c))) |
| 231 | + |
| 232 | + |
| 233 | +tricontourf(vec(x), vec(y), vec(F); levels=100) |
| 234 | + |
| 235 | +using Makie, MultivariateOrthogonalPolynomials |
| 236 | +using ContinuumArrays: ApplyQuasiVector, plotgridvalues |
| 237 | +# Makie.plottype(a::ApplyQuasiVector{<:Any, typeof(*), <:Tuple{JacobiTriangle,AbstractVector}}) = Tricontourf |
| 238 | + |
| 239 | + |
| 240 | +function Makie.tricontourf(f::ApplyQuasiVector{<:Any, typeof(*)}; kwds...) |
| 241 | + xy,F = plotgridvalues(f) |
| 242 | + x,y = first.(xy),last.(xy) |
| 243 | + tricontourf(vec(x), vec(y), vec(F); kwds...) |
| 244 | +end |
| 245 | + |
| 246 | +function Makie.tricontourf!(f::ApplyQuasiVector{<:Any, typeof(*)}; kwds...) |
| 247 | + xy,F = plotgridvalues(f) |
| 248 | + x,y = first.(xy),last.(xy) |
| 249 | + tricontourf!(vec(x), vec(y), vec(F); kwds...) |
| 250 | +end |
| 251 | + |
| 252 | + |
| 253 | +# f = expand(Weighted(JacobiTriangle(1,1,1))[:,20]) |
| 254 | +# tricontourf(f; levels=100) |
| 255 | +import Base: oneto |
| 256 | +KR = Block.(oneto(40)) |
| 257 | +P = JacobiTriangle() |
| 258 | +f = P[:,KR] * (P \ Weighted(JacobiTriangle(1,1,1)))[KR,10] |
| 259 | + |
| 260 | +p = Figure(resolution = (3200, 2400)) |
| 261 | +ax = Axis(p[1, 1]) |
| 262 | +tricontourf!(f; levels=100) |
| 263 | +save("bubble10.png", p) |
| 264 | + |
| 265 | + |
| 266 | +using ContinuumArrays: AffineMap, affine |
| 267 | +using MultivariateOrthogonalPolynomials |
| 268 | +using MultivariateOrthogonalPolynomials: Triangle |
| 269 | +using StaticArrays |
| 270 | + |
| 271 | +a = affine(Triangle(), Triangle(SVector(1,0), SVector(0,1), SVector(1,1))) |
| 272 | +a[SVector(0.1,0.2)] |
| 273 | + |
| 274 | +P = JacobiTriangle() |
| 275 | +Q = P[affine(Triangle(SVector(1,0), SVector(0,1), SVector(1,1)), axes(P,1)), :] |
| 276 | + |
| 277 | +f = Q * [[randn(20); zeros(100)]; zeros(∞)] |
| 278 | + |
| 279 | +tricontourf(f; nlevels=300) |
| 280 | + |
| 281 | + |
| 282 | +# end # modul |
0 commit comments