diff --git a/src/bases/bcspace.jl b/src/bases/bcspace.jl index 5e2d68061..98a46ad2b 100644 --- a/src/bases/bcspace.jl +++ b/src/bases/bcspace.jl @@ -87,6 +87,48 @@ isclosed(a, pred) = length(a)>2 && pred(a[end], a[1]) Construct the set of Buffa-Christiansen functions subject to mesh Γ and only enforcing zero normal components on ∂Γ ∖ γ. """ +# The vertex of `fine` that the refinement inserted on the coarse edge with chart `ch` and the +# global vertex indices `edge`, found among the corners of the fine cells `F` around `edge[1]`. +# For the barycentric refinement it is the image of the parametric midpoint of the edge, which +# for a curved edge does not lie on the segment between its end points: it is identified as the +# corner closest to that midpoint. The line of sight refinement places it elsewhere on the +# (straight) edge, and the corner on that segment is searched for instead. +function _edgevertex(Γ, fine, F, edge, ch, ibscaled) + T = coordtype(fine) + vert1 = vertices(Γ)[edge[1]] + vert2 = vertices(Γ)[edge[2]] + if !ibscaled + edge_ctr = cartesian(center(ch)) + p, dmin = 0, typemax(T) + for f in F, q in CompScienceMeshes.indices(fine, f) + d = norm(vertices(fine)[q] - edge_ctr) + d < dmin && ((p, dmin) = (q, d)) + end + @assert dmin ≤ 1e-8 * norm(vert2 - vert1) + return p + end + ln = volume(ch) + supp = fine[F] + bnd = boundary(supp) + bnd_nodes = skeleton(bnd,0) + p = 0 + for bn in bnd_nodes + node = CompScienceMeshes.indices(bnd_nodes, bn) + vert = vertices(fine)[node[1]] + dist = norm(vert-vert1) + node[1] == edge[1] && continue + if dot(vert-vert1,vert2-vert1) ≈ dist*ln + p = node[1] + break + end + end + @assert p != 0 + edge_ctr = cartesian(center(ch)) + @assert all(carttobary(ch, edge_ctr) .≥ 0) + @assert norm(cross(edge_ctr - ch.vertices[1], vertices(fine)[p] - ch.vertices[1])) ≤ 1e-8 + return p +end + function buffachristiansen(Γ, γ=mesh(coordtype(Γ),1,3); ibscaled=false, sort=:spacefillingcurve, edges=:all) @assert CompScienceMeshes.isoriented(Γ) @@ -133,40 +175,17 @@ function buffachristiansen(Γ, γ=mesh(coordtype(Γ),1,3); ibscaled=false, sort= edge = CompScienceMeshes.indices(edges,e) ch = chart(edges, e) - ln = volume(ch) # !in_interior(edge) && !on_junction(ch) && continue # index of edge center in fine's vertexbuffer v = edge[1] n = vton[v] F = vtof[v,1:n] - supp = fine[F] - bnd = boundary(supp) - bnd_nodes = skeleton(bnd,0) - p = 0 - for bn in bnd_nodes - node = CompScienceMeshes.indices(bnd_nodes, bn) - vert1 = vertices(Γ)[edge[1]] - vert2 = vertices(Γ)[edge[2]] - vert = vertices(fine)[node[1]] - dist = norm(vert-vert1) - node[1] == edge[1] && continue - if dot(vert-vert1,vert2-vert1) ≈ dist*ln - p = node[1] - break - end - end - @assert p != 0 + p = _edgevertex(Γ, fine, F, edge, ch, ibscaled) # p = numvertices(edges) + i pos[k] = vertices(fine)[p] - # sanity check - edge_ctr = cartesian(center(ch)) - fine_vtx = vertices(fine)[p] - @assert all(carttobary(ch, edge_ctr) .≥ 0) - @assert norm(cross(edge_ctr - ch.vertices[1], fine_vtx - ch.vertices[1])) ≤ 1e-8 - v = edge[1] n = vton[v] F = vec(vtof[v,1:n]) @@ -200,7 +219,7 @@ function buildhalfbc(fine, S, v, p, onjunction, ibscaled) @assert length(S) >= 2 @assert mod(length(S), 2) == 0 @assert all(0 .< S .<= numcells(fine)) - @assert all([v in cells(fine)[s].indices for s in S]) + @assert all([v in CompScienceMeshes.indices(fine, s) for s in S]) bf = Shape{T}[] @@ -209,12 +228,12 @@ function buildhalfbc(fine, S, v, p, onjunction, ibscaled) modn(i) = mod1(i,n) # sort the fine faces making up the domain - share_edge(i,j) = length(intersect(cells(fine)[i].indices, cells(fine)[j].indices)) == 2 + share_edge(i,j) = length(intersect(CompScienceMeshes.indices(fine, i), CompScienceMeshes.indices(fine, j))) == 2 S = sortneighbors(S, share_edge) @assert all(0 .< S .<= numcells(fine)) - port_faces = findall(i -> p in cells(fine)[i].indices, S) - port_edges = [something(findfirst(isequal(v), cells(fine)[S[i]].indices),0) for i in port_faces] + port_faces = findall(i -> p in CompScienceMeshes.indices(fine, i), S) + port_edges = [something(findfirst(isequal(v), CompScienceMeshes.indices(fine, S[i])),0) for i in port_faces] c_on_boundary = !share_edge(S[end], S[1]) || length(S) == 2 e_on_boundary = length(port_faces) == 1 @@ -233,7 +252,7 @@ function buildhalfbc(fine, S, v, p, onjunction, ibscaled) end port_lengths = [begin - face = cells(fine)[S[j]] + face = CompScienceMeshes.indices(fine, S[j]) v1 = vertices(fine)[face[mod1(i+1,3)]] v2 = vertices(fine)[face[mod1(i+2,3)]] norm(v2-v1) @@ -253,10 +272,10 @@ function buildhalfbc(fine, S, v, p, onjunction, ibscaled) bnd_edges = Int[] if c_on_boundary bnd_faces = [1,n] - p1 = x -> x in cells(fine)[S[2]].indices && x != v - p2 = x -> x in cells(fine)[S[n-1]].indices && x != v - e1 = findfirst(p1, cells(fine)[S[1]].indices) - e2 = findfirst(p2, cells(fine)[S[n]].indices) + p1 = x -> x in CompScienceMeshes.indices(fine, S[2]) && x != v + p2 = x -> x in CompScienceMeshes.indices(fine, S[n-1]) && x != v + e1 = findfirst(p1, CompScienceMeshes.indices(fine, S[1])) + e2 = findfirst(p2, CompScienceMeshes.indices(fine, S[n])) bnd_edges = [e1, e2] end @@ -266,8 +285,8 @@ function buildhalfbc(fine, S, v, p, onjunction, ibscaled) if c_on_boundary for i in 1:length(bnd_faces) f, e = bnd_faces[i], bnd_edges[i] - a = vertices(fine)[cells(fine)[S[f]].indices[mod1(e+1,3)]] - b = vertices(fine)[cells(fine)[S[f]].indices[mod1(e+2,3)]] + a = vertices(fine)[CompScienceMeshes.indices(fine, S[f])[mod1(e+1,3)]] + b = vertices(fine)[CompScienceMeshes.indices(fine, S[f])[mod1(e+2,3)]] seg = simplex(a,b) if onjunction(seg) push!(jct_idxes, i) @@ -324,7 +343,7 @@ function buildhalfbc(fine, S, v, p, onjunction, ibscaled) # get the indices of the faces in fine.faces f0, f1 = S[j0], S[j1] # what are the local indices of the common edge? - e0, e1 = getcommonedge(cells(fine)[f0].indices, cells(fine)[f1].indices) + e0, e1 = getcommonedge(CompScienceMeshes.indices(fine, f0), CompScienceMeshes.indices(fine, f1)) # add the two half Raviart-Thomas shape functions add!(bf, S[j0], abs(e0), +charges[j0]) add!(bf, S[j1], abs(e1), -charges[j0]) diff --git a/src/bases/local/laglocal.jl b/src/bases/local/laglocal.jl index eca8e2240..d9718ac64 100644 --- a/src/bases/local/laglocal.jl +++ b/src/bases/local/laglocal.jl @@ -279,14 +279,14 @@ Returns a vector of indices into the vector of local shape functions that corres global degrees of freedom supported on sub-entity `i`, where the type of entity (nodes, edge, face) is encoded in the type of 'dof'. """ -function localindices(dof::_LagrangeGlobalNodesDoFs, chart::CompScienceMeshes.Simplex{<:Any, 2}, +function localindices(dof::_LagrangeGlobalNodesDoFs, chart::CompScienceMeshes.AbstractSimplex{<:Any, 2}, localspace, i) d = dof.order [1, d+1, div((d+1)*(d+2),2)][[i]] end -function localindices(dof::_LagrangeGlobalEdgeDoFs, chart::CompScienceMeshes.Simplex{<:Any, 2}, +function localindices(dof::_LagrangeGlobalEdgeDoFs, chart::CompScienceMeshes.AbstractSimplex{<:Any, 2}, localspace, i) d = dof.order @@ -324,7 +324,7 @@ function localindices(dof::_LagrangeGlobalEdgeDoFs, chart::CompScienceMeshes.Sim return lids end -function localindices(dof::_LagrangeGlobalFaceDoFs, chart::CompScienceMeshes.Simplex{<:Any, 2}, +function localindices(dof::_LagrangeGlobalFaceDoFs, chart::CompScienceMeshes.AbstractSimplex{<:Any, 2}, localspace, i) d = dof.order diff --git a/src/bases/local/rtlocal.jl b/src/bases/local/rtlocal.jl index a6ecf89a4..c37a7edef 100644 --- a/src/bases/local/rtlocal.jl +++ b/src/bases/local/rtlocal.jl @@ -85,12 +85,12 @@ function interpolate(fields, interpolant::RTRefSpace{T}, chart) where {T} u_face = T(1//2) p_face = neighborhood(face, (u_face,)) t_face = -tangents(p_face, 1) - t1 = tangents(chart, 1)[:,1] - t2 = tangents(chart, 2)[:,2] u_chart = cartesian(inj, u_face) p_chart = neighborhood(chart, u_chart) - n_chart = normalize(t1× t2) + # from the tangents at the point, which follow the vertex order, and not normal(p_chart): + # a flat chart from `intersection` carries a normal flipped against its vertex order + n_chart = normalize(tangents(p_chart, 1) × tangents(p_chart, 2)) m_face = cross(t_face, n_chart) # p = center(face) diff --git a/src/identityop.jl b/src/identityop.jl index ee6f8cc1b..c370cb77a 100644 --- a/src/identityop.jl +++ b/src/identityop.jl @@ -195,8 +195,9 @@ defaultquadstrat(::LocalOperator, _::Space, _::Space) = SingleNumQStrat(6) # return qd, A # end -function quaddata(op::LocalOperator, g, f, - tels::Vector{<:CompScienceMeshes.Simplex{U,2}}, bels::Vector{<:CompScienceMeshes.Simplex{U,2}}, +# also for curvilinear triangles: the rule is one on the reference triangle +function quaddata(op::LocalOperator, g, f, + tels::Vector{<:CompScienceMeshes.AbstractSimplex{U,2}}, bels::Vector{<:CompScienceMeshes.AbstractSimplex{U,2}}, qs::SingleNumQStrat) where {U} u, w = trgauss(qs.quad_rule) diff --git a/src/localop.jl b/src/localop.jl index 5178b5ab3..c2d655d88 100644 --- a/src/localop.jl +++ b/src/localop.jl @@ -84,6 +84,10 @@ function assemble!(biop::LocalOperator, tfs::Space, bfs::Space, store, return assemble_local_refines!(biop, tfs, bfs, store; quadstrat, verbose) end + if CompScienceMeshes.refines(geometry(bfs), geometry(tfs)) + return assemble_local_refinedby!(biop, tfs, bfs, store; quadstrat, verbose) + end + return assemble_local_mixed!(biop, tfs, bfs, store; quadstrat, verbose) end @@ -148,6 +152,10 @@ function assemble!(biop::LocalOperator, tfs::Space, bfs::Space, store, return assemble_local_refines!(biop, tfs, bfs, store; quadstrat, verbose) end + if CompScienceMeshes.refines(geometry(bfs), geometry(tfs)) + return assemble_local_refinedby!(biop, tfs, bfs, store; quadstrat, verbose) + end + return assemble_local_mixed!(biop, tfs, bfs, store; quadstrat, verbose) end @@ -195,6 +203,93 @@ function assemble_local_matched!(biop::LocalOperator, tfs::Space, bfs::Space, st end +# The interaction of the test functions on `tcell`, cell `P` of the refinement `tgeo`, with the +# trial functions on its parent `bcell`, over their intersection. +function _assemble_testchild_interaction!(biop, trefs, brefs, tcell, bcell, tgeo, P, + tad, bad, p, q, qd, quadstrat, store) + + @assert overlap(tcell, bcell) + + isct = intersection(tcell, bcell) + for cell in isct + _assemble_restricted_interaction!( + biop, + trefs, + brefs, + tcell, + bcell, + cell, + tad, + bad, + p, + q, + qd, + quadstrat, + store, + nothing, + ) + end + return nothing +end + +# A curved child cannot be located in its parent geometrically, but its parametric domain is +# mapped into the parent's affinely, which yields the restriction of the trial functions exactly. +# That of the test functions is the identity. +function _assemble_testchild_interaction!(biop, trefs, brefs, + tcell::CompScienceMeshes.CurvilinearSimplex, bcell, tgeo, P, + tad, bad, p, q, qd, quadstrat, store) + + tcellinbcell = CompScienceMeshes.chartinparent(tgeo, P) + trialrestriction = restrict(brefs, bcell, tcell, tcellinbcell) + qr = quadrule(biop, trefs, brefs, tcell, qd, quadstrat) + zlocal = cellinteractions(biop, trefs, brefs, tcell, qr) + _scatterlocal!(store, zlocal * trialrestriction', tad, bad, p, q) + return nothing +end + +# The interaction of the test functions on `tcell` with the trial functions on its child `bcell`, +# cell `Q` of the refinement `bgeo`, over their intersection. +function _assemble_trialchild_interaction!(biop, trefs, brefs, tcell, bcell, bgeo, Q, + tad, bad, p, q, qd, quadstrat, store) + + @assert overlap(tcell, bcell) + + isct = intersection(tcell, bcell) + for cell in isct + _assemble_restricted_interaction!( + biop, + trefs, + brefs, + tcell, + bcell, + cell, + tad, + bad, + p, + q, + qd, + quadstrat, + store, + nothing, + ) + end + return nothing +end + +# As for a curved test child: the parametric domain of the child yields the restriction of the +# test functions exactly, and that of the trial functions is the identity. +function _assemble_trialchild_interaction!(biop, trefs, brefs, + tcell, bcell::CompScienceMeshes.CurvilinearSimplex, bgeo, Q, + tad, bad, p, q, qd, quadstrat, store) + + bcellintcell = CompScienceMeshes.chartinparent(bgeo, Q) + testrestriction = restrict(trefs, tcell, bcell, bcellintcell) + qr = quadrule(biop, trefs, brefs, bcell, qd, quadstrat) + zlocal = cellinteractions(biop, trefs, brefs, bcell, qr) + _scatterlocal!(store, testrestriction * zlocal, tad, bad, p, q) + return nothing +end + function assemble_local_refines!(biop::LocalOperator, tfs::Space, bfs::Space, store; quadstrat=defaultquadstrat(biop, tfs, bfs), verbose=true) @@ -231,28 +326,8 @@ function assemble_local_refines!(biop::LocalOperator, tfs::Space, bfs::Space, st q == 0 && continue bcell = bels[q] - @assert overlap(tcell, bcell) - - isct = intersection(tcell, bcell) - for cell in isct - _assemble_restricted_interaction!( - biop, - trefs, - brefs, - tcell, - bcell, - cell, - tad, - bad, - p, - q, - qd, - quadstrat, - store, - nothing, - ) - - end # next cell in intersection + _assemble_testchild_interaction!(biop, trefs, brefs, tcell, bcell, tgeo, P, + tad, bad, p, q, qd, quadstrat, store) done += 1 new_pctg = round(Int, done / todo * 100) @@ -266,6 +341,51 @@ function assemble_local_refines!(biop::LocalOperator, tfs::Space, bfs::Space, st end +# The mirror image of assemble_local_refines!: the trial geometry refines the test geometry, and +# every cell of the trial geometry lies in its parent in the test geometry. +function assemble_local_refinedby!(biop::LocalOperator, tfs::Space, bfs::Space, store; + quadstrat=defaultquadstrat(biop, tfs, bfs), verbose=true) + + tgeo = geometry(tfs) + bgeo = geometry(bfs) + @assert CompScienceMeshes.refines(bgeo, tgeo) + + trefs = refspace(tfs) + brefs = refspace(bfs) + + tels, tad, ta2g = assemblydata(tfs) + bels, bad, ba2g = assemblydata(bfs) + + tg2a = zeros(Int, length(tgeo)) + for (i,j) in enumerate(ta2g) tg2a[j] = i end + + qd = quaddata(biop, trefs, brefs, tels, bels, quadstrat) + + verbose && print("dots out of 10: ") + todo, done, pctg = length(bels), 0, 0 + for (q,bcell) in enumerate(bels) + + Q = ba2g[q] + P = CompScienceMeshes.parent(bgeo, Q) + p = tg2a[P] + p == 0 && continue + + tcell = tels[p] + _assemble_trialchild_interaction!(biop, trefs, brefs, tcell, bcell, bgeo, Q, + tad, bad, p, q, qd, quadstrat, store) + + done += 1 + new_pctg = round(Int, done / todo * 100) + if verbose && new_pctg > pctg + 9 + print(".") + pctg = new_pctg + end + end # next cell in the trial geometry + + verbose && println("") + +end + function assemble_local_matched!(biop::LocalOperator, tfs::subdBasis, bfs::subdBasis, store; quadstrat=defaultquadstrat(biop, tfs, bfs), verbose=true) @@ -286,9 +406,10 @@ function assemble_local_matched!(biop::LocalOperator, tfs::subdBasis, bfs::subdB end +# The cells in an octree of balls around the mean of their vertices, which for curvilinear cells +# are all their interpolation nodes. function elementstree(elements, expansion_ratio=1.1) - nverts = dimension(eltype(elements)) + 1 ncells = length(elements) @assert !isempty(elements) @@ -307,8 +428,8 @@ function elementstree(elements, expansion_ratio=1.1) bary += verts[j] end - points[i] = bary / nverts - for j in 1 : nverts + points[i] = bary / length(verts) + for j in eachindex(verts) radii[i] = max(radii[i], norm(verts[j]-points[i])) end end diff --git a/test/runtests.jl b/test/runtests.jl index df3979233..e2c6f80d2 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -29,6 +29,8 @@ include("test_nd2.jl") include("test_dvg.jl") include("test_bcspace.jl") +include("test_curvilinear_spaces.jl") +include("test_curvilinear_localops.jl") include("test_trace.jl") include("test_ttrace.jl") include("test_timebasis.jl") diff --git a/test/test_curvilinear_localops.jl b/test/test_curvilinear_localops.jl new file mode 100644 index 000000000..1eecc3eb9 --- /dev/null +++ b/test/test_curvilinear_localops.jl @@ -0,0 +1,61 @@ +using Test +using BEAST +using CompScienceMeshes +using LinearAlgebra + +# Local operators between the spaces on a curvilinear mesh and those on its barycentric +# refinement, in both directions: the test geometry refining the trial geometry and conversely. +# Identity is symmetric and NCross antisymmetric, so the two directions are each other's +# transposes. A curvilinear mesh of order 1 has the geometry of the flat mesh it is made of, so +# its matrices are those of the flat mesh. +@testset "local operators on curvilinear refinements" begin + + CSM = CompScienceMeshes + asm(op, X, Y) = Matrix(assemble(op, X, Y; verbose=false)) + pairs(Γ) = ((raviartthomas(Γ), buffachristiansen(Γ)), (lagrangecxd0(Γ), duallagrangec0d1(Γ))) + ops = ((BEAST.Identity(), 1), (BEAST.NCross(), -1)) + + flat = meshsphere(1.0, 0.6) + lin = CSM.CurvilinearMesh(vertices(flat), [CSM.HOSimplexGraph{3,1}(c.indices...) for c in cells(flat)]) + + for ((Xf, Yf), (Xl, Yl)) in zip(pairs(flat), pairs(lin)), (op, _) in ops + op isa BEAST.NCross && !(Xf isa BEAST.RTBasis) && continue + A, B = asm(op, Xl, Yl), asm(op, Xf, Yf) + @test norm(A - B) ≤ 1e-12 * norm(B) + end + + for O in (2, 3) + Γ = gmshsphere(1.0, 0.6; order=O) + for (X, Y) in pairs(Γ), (op, s) in ops + op isa BEAST.NCross && !(X isa BEAST.RTBasis) && continue + A, B = asm(op, X, Y), asm(op, Y, X) + @test norm(A - s * transpose(B)) ≤ 1e-12 * norm(A) + end + end +end + +# The same matrices by the mixed assembly, which intersects the cells of the test and the trial +# geometry: the spaces on the refinement are built on a copy of the mesh, which the refinement +# does not refine. +@testset "local operators on curvilinear meshes, mixed assembly" begin + + CSM = CompScienceMeshes + asm(op, X, Y) = Matrix(assemble(op, X, Y; verbose=false)) + ops = (BEAST.Identity(), BEAST.NCross()) + + for O in (2, 3) + Γ = gmshsphere(1.0, 0.6; order=O) + Γ2 = deepcopy(Γ) + for (coarse, fine) in ((raviartthomas, buffachristiansen), (lagrangecxd0, duallagrangec0d1)) + X, Y, Y2 = coarse(Γ), fine(Γ), fine(Γ2) + @test !CSM.refines(geometry(Y2), geometry(X)) + for op in ops + op isa BEAST.NCross && coarse !== raviartthomas && continue + A, B = asm(op, X, Y2), asm(op, X, Y) + @test norm(A - B) ≤ 1e-12 * norm(B) + A, B = asm(op, Y2, X), asm(op, Y, X) + @test norm(A - B) ≤ 1e-12 * norm(B) + end + end + end +end diff --git a/test/test_curvilinear_spaces.jl b/test/test_curvilinear_spaces.jl new file mode 100644 index 000000000..91417bfaf --- /dev/null +++ b/test/test_curvilinear_spaces.jl @@ -0,0 +1,48 @@ +using Test +using BEAST +using CompScienceMeshes +using StaticArrays +using LinearAlgebra + +# The spaces built on the barycentric refinement (and the continuous Lagrange space) on meshes +# of curvilinear triangles. A curvilinear mesh of order 1 has the geometry of the flat mesh it is +# made of, so its spaces have the same coefficients; for higher orders the number of functions is +# that of the flat mesh, and the dual constant Lagrange functions keep unit integral on the +# curved refinement. +@testset "spaces on curvilinear meshes" begin + + CSM = CompScienceMeshes + flat = meshsphere(1.0, 0.6) + lin = CSM.CurvilinearMesh(vertices(flat), [CSM.HOSimplexGraph{3,1}(c.indices...) for c in cells(flat)]) + + shapes(X) = [[(s.cellid, s.refid, s.coeff) for s in fn] for fn in X.fns] + spaces = (raviartthomas, buffachristiansen, lagrangec0d1, lagrangecxd0, duallagrangec0d1, duallagrangecxd0) + + for f in spaces + A, B = f(flat), f(lin) + @test numfunctions(A) == numfunctions(B) + @test all(length.(A.fns) .== length.(B.fns)) + @test all(((a[1], a[2]) == (b[1], b[2]) && isapprox(a[3], b[3]; rtol=1e-12)) + for (fa, fb) in zip(shapes(A), shapes(B)) for (a, b) in zip(fa, fb)) + @test all(isapprox.(A.pos, B.pos; atol=1e-12)) + end + + for O in (2, 3) + Γ = gmshsphere(1.0, 0.6; order=O) + @test Γ isa CSM.CurvilinearMesh + for f in spaces + @test numfunctions(f(Γ)) == numfunctions(f(flat)) + end + + # the Buffa-Christiansen functions are supported on the curved refinement, and positioned + # at the (curved) midpoints of the edges, which lie on the sphere + Y = buffachristiansen(Γ) + @test Y.geo isa CSM.BarycentricRefinement + @test Y.geo.mesh isa CSM.CurvilinearMesh + @test all(p -> isapprox(norm(p), 1.0; atol=1e-2), Y.pos) + + X = duallagrangecxd0(Γ) + fine = X.geo + @test all(fn -> isapprox(sum(s.coeff * volume(chart(fine, s.cellid)) for s in fn), 1; atol=1e-12), X.fns) + end +end