diff --git a/src/CompScienceMeshes.jl b/src/CompScienceMeshes.jl index e931a9d..fc27fb2 100644 --- a/src/CompScienceMeshes.jl +++ b/src/CompScienceMeshes.jl @@ -122,6 +122,7 @@ include("subd_chart.jl") include("sphere.jl") include("overlap.jl") include("intersect.jl") +include("charts/curvilinear_intersect.jl") include("isinside.jl") include("findchart.jl") include("neighborhood.jl") @@ -151,6 +152,7 @@ include("fileio/gid.jl") include("primitives/primitives.jl") #include("../examples/waveguide_with_post.jl") include("baryref.jl") +include("meshes/curvilinear_baryref.jl") include("subdivision.jl") include("weld.jl") diff --git a/src/charts/curvilinear_chart.jl b/src/charts/curvilinear_chart.jl index 870d00e..b3fb014 100644 --- a/src/charts/curvilinear_chart.jl +++ b/src/charts/curvilinear_chart.jl @@ -78,11 +78,11 @@ Return the static vector type used to represent vertex coordinates. """ vertextype(::CurvilinearSimplex{U,D,C,N,T}) where {U,D,C,N,T} = SVector{U,T} -# type-form -coordtype(::Type{CurvilinearSimplex{U,D,C,N,T}}) where {U,D,C,N,T} = T -dimension(::Type{CurvilinearSimplex{U,D,C,N,T}}) where {U,D,C,N,T} = D -universedimension(::Type{CurvilinearSimplex{U,D,C,N,T}}) where {U,D,C,N,T} = U -vertextype(::Type{CurvilinearSimplex{U,D,C,N,T}}) where {U,D,C,N,T} = SVector{U,T} +# type-form; `<:` since the concrete type also carries the order O +coordtype(::Type{<:CurvilinearSimplex{U,D,C,N,T}}) where {U,D,C,N,T} = T +dimension(::Type{<:CurvilinearSimplex{U,D,C,N,T}}) where {U,D,C,N,T} = D +universedimension(::Type{<:CurvilinearSimplex{U,D,C,N,T}}) where {U,D,C,N,T} = U +vertextype(::Type{<:CurvilinearSimplex{U,D,C,N,T}}) where {U,D,C,N,T} = SVector{U,T} """ neighborhood(ch::CurvilinearSimplex, bary) @@ -102,7 +102,8 @@ function neighborhood(p::C, bary) where {C<:CurvilinearSimplex} D = dimension(p) T = coordtype(p) P = SVector{D,T} - cart = barytocart(p, T.(bary)) + # the parameters may come as a tuple, as for flat simplices + cart = barytocart(p, P(T.(bary))) Q = typeof(cart) U = length(cart) MeshPointNM{T,C,D,U}(p, P(bary), cart) @@ -119,8 +120,10 @@ function barytocart(ch::CurvilinearSimplex{U,1,C,N,T,O}, bary::AbstractVector) w u = bary[1] s = zero(SVector{U,T}) + Ru = silvester(Val(O), u) + Rv = silvester(Val(O), 1 - u) @inbounds for (r, (i, j)) in enumerate(gmsh_line_index_to_tuple[O]) - s += ch.vertices[r] * silvester(O, u)[i + 1] * silvester(O, 1 - u)[j + 1] + s += ch.vertices[r] * Ru[i + 1] * Rv[j + 1] end return s @@ -148,11 +151,14 @@ function barytocart(ch::CurvilinearSimplex{U,2,C,N,T,O}, bary) where {U,C,N,T,O} v = bary[2] s = zero(SVector{U,T}) + Ru = silvester(Val(O), u) + Rv = silvester(Val(O), v) + Rw = silvester(Val(O), 1 - u - v) @inbounds for (r, (i, j, k)) in enumerate(gmsh_triangle_index_to_tuple[O]) s += ch.vertices[r] * - silvester(O, u)[i + 1] * - silvester(O, v)[j + 1] * - silvester(O, 1 - u - v)[k + 1] + Ru[i + 1] * + Rv[j + 1] * + Rw[k + 1] end return s @@ -218,8 +224,8 @@ function unitarybase(ch::CurvilinearSimplex{U,1,C,N,T,O}, bary) where {U,C,N,T,O s = zero(SVector{U,T}) - Ru, dRu = fast_silvester(O, u) - Rv, dRv = fast_silvester(O, 1 - u) + Ru, dRu = fast_silvester(Val(O), u) + Rv, dRv = fast_silvester(Val(O), 1 - u) @inbounds for (r, (i, j)) in enumerate(gmsh_line_index_to_tuple[O]) @@ -256,9 +262,9 @@ function unitarybase(ch::CurvilinearSimplex{U,2,C,N,T,O}, bary) where {U,C,N,T,O u = bary[1] v = bary[2] - Ru, dRu = fast_silvester(O, u) - Rv, dRv = fast_silvester(O, v) - Rw, dRw = fast_silvester(O, 1 - u - v) + Ru, dRu = fast_silvester(Val(O), u) + Rv, dRv = fast_silvester(Val(O), v) + Rw, dRw = fast_silvester(Val(O), 1 - u - v) 𝓵¹ = zero(SVector{U,T}) 𝓵² = zero(SVector{U,T}) @@ -372,7 +378,8 @@ evaluating the mapping at `ζ = 0.5`. """ function center(ch::CurvilinearSimplex{U,0}) where {U} ch.vertices[1] end function center(ch::CurvilinearSimplex{U,1}) where {U} neighborhood(ch, SVector(0.5)) end -function center(ch::CurvilinearSimplex{U,2}) where {U} neighborhood(ch, SVector(0.5, 0.5)) end +# the centroid of the reference triangle; (1/2, 1/2) would be the midpoint of an edge +function center(ch::CurvilinearSimplex{U,2,C,N,T}) where {U,C,N,T} neighborhood(ch, SVector(one(T) / 3, one(T) / 3)) end domain(::CurvilinearSimplex{U,1,C,N,T}) where {U,C,N,T} = ReferenceSimplex{1,T,2}() domain(::CurvilinearSimplex{U,2,C,N,T}) where {U,C,N,T} = ReferenceSimplex{2,T,3}() @@ -431,4 +438,22 @@ function faces(ch::CurvilinearSimplex{U,2,C,N,T,O}, ::Type{Val{1}}) where {U,C,N E(F(I[3], I[1], I[3+2*(O-1)+1:3+3*(O-1)]...)), E(F(I[1], I[2], I[3+0*(O-1)+1:3+1*(O-1)]...)), ) -end \ No newline at end of file +end +# the edges of a triangle, as `faces(::Simplex{3,2})` does for flat ones: edge j is opposite to corner j +faces(ch::CurvilinearSimplex{U,2}) where {U} = faces(ch, Val{1}) + +# the edges of a triangle with their injections into its parametric domain, as +# `subcharts(::Simplex{U,2}, Val{1})` does for flat ones +function subcharts(c::CurvilinearSimplex{U,2,C,N,T}, ::Type{Val{1}}) where {U,C,N,T} + d = ( + point(T, 1, 0), + point(T, 0, 1), + point(T, 0, 0), + ) + e = faces(c, Val{1}) + return ( + (e[1], simplex(d[2], d[3])), + (e[2], simplex(d[3], d[1])), + (e[3], simplex(d[1], d[2])), + ) +end diff --git a/src/charts/curvilinear_intersect.jl b/src/charts/curvilinear_intersect.jl new file mode 100644 index 0000000..f125c7e --- /dev/null +++ b/src/charts/curvilinear_intersect.jl @@ -0,0 +1,96 @@ +# Locating points in curvilinear triangles, and the overlap and intersection of two of them. + +""" + carttobary(ch::CurvilinearSimplex{U,2}, x; maxiter=20) + +The parameters `u` of the point of `ch` closest to `x`: the solution of +`barytocart(ch, u) ≈ x` in the least squares sense by Gauss-Newton iteration, starting from the +parameters of `x` in the flat triangle through the corners of `ch`. For `x` on `ch` this is the +inverse of `barytocart`. The map of `ch` is a polynomial, defined beyond the reference triangle: +`u` lies outside of it when `x` lies on the continuation of `ch`. +""" +function carttobary(ch::CurvilinearSimplex{U,2,C,N,T}, x; maxiter=20) where {U,C,N,T} + u = carttobary(simplex(nodes(ch)...), x) + for _ in 1:maxiter + t1, t2 = unitarybase(ch, u) + r = barytocart(ch, u) - x + G = SMatrix{2,2,T}(dot(t1, t1), dot(t2, t1), dot(t1, t2), dot(t2, t2)) + du = G \ SVector(dot(t1, r), dot(t2, r)) + u -= du + norm(du) ≤ 4 * eps(T) && break + end + return u +end + +# The parameters in `p` of the corners of `q` if `q` is an affine reparametrization of the map of +# `p` (continued beyond its reference triangle), as are a cell and its children in the barycentric +# refinement, or two children of the same cell; `nothing` otherwise. The corners determine the +# affine map, and the maps of `q` and of `p` composed with it are polynomials of the order of `q` +# that are equal if they agree at the interpolation nodes of `q`. +function _cornersinparams(p::CurvilinearSimplex{U,2,C,N,T}, q::CurvilinearSimplex{U,2,C,M,T,O}) where {U,C,N,T,M,O} + us = map(c -> carttobary(p, c), nodes(q)) + scale = zero(T) + for a in nodes(p), b in nodes(q) + scale = max(scale, norm(a - b)) + end + tol = sqrt(eps(T)) * scale + for (r, t) in enumerate(gmsh_triangle_index_to_tuple[O]) + u = (t[1] * us[1] + t[2] * us[2] + t[3] * us[3]) / O + norm(barytocart(p, u) - q.vertices[r]) ≤ tol || return nothing + end + return us +end + +# The part of `p` over the triangle `(a, b, c)` of its parametric domain, as a chart of the same +# order, with the interpolation nodes of `p` mapped onto `(a, b, c)` affinely. +function _subchart(p::CurvilinearSimplex{U,2,C,N,T,O}, a, b, c) where {U,C,N,T,O} + X = map(gmsh_triangle_index_to_tuple[O]) do t + barytocart(p, (t[1] * a + t[2] * b + t[3] * c) / O) + end + return CurvilinearSimplex{U,2,C,N,T,O}(SVector{N}(X)) +end + +# The intersection of `q` with `p` as a polygon in the parametric domain of `p`, if `q` is an +# affine reparametrization of `p` (see `_cornersinparams`); empty otherwise. +function _intersectionparams(p::CurvilinearSimplex{U,2,C,N,T}, q::CurvilinearSimplex{U,2}) where {U,C,N,T} + us = _cornersinparams(p, q) + us === nothing && return SVector{2,T}[] + reference = [SVector{2,T}(1, 0), SVector{2,T}(0, 1), SVector{2,T}(0, 0)] + return sutherlandhodgman2d(collect(us), reference) +end + +""" + overlap(p::CurvilinearSimplex{U,2}, q::CurvilinearSimplex{U,2}) + +Whether the curvilinear triangles `p` and `q` share a part of positive area. This is decided +exactly when `q` is an affine reparametrization of the map of `p`, as for the cells of a mesh and +of its refinements; otherwise they are taken not to overlap, as non coplanar flat triangles. +""" +function overlap(p::CurvilinearSimplex{U,2,C,N,T}, q::CurvilinearSimplex{U,2}) where {U,C,N,T} + poly = _intersectionparams(p, q) + area = zero(T) + for i in 3:length(poly) + area += cross(poly[i-1] - poly[1], poly[i] - poly[1]) / 2 + end + return abs(area) > 1e3 * eps(T) +end + +""" + intersection(p::CurvilinearSimplex{U,2}, q::CurvilinearSimplex{U,2}) + +The intersection of the curvilinear triangles `p` and `q` as curvilinear triangles of the order +of `p`, with its orientation, under the assumption of `overlap`: empty unless `q` is an affine +reparametrization of the map of `p`. +""" +function intersection(p::CurvilinearSimplex{U,2,C,N,T}, q::CurvilinearSimplex{U,2}) where {U,C,N,T} + poly = _intersectionparams(p, q) + pieces = typeof(p)[] + for i in 3:length(poly) + a, b, c = poly[1], poly[i-1], poly[i] + area = cross(b - a, c - a) / 2 + abs(area) ≤ 1e3 * eps(T) && continue + area < 0 && ((b, c) = (c, b)) + push!(pieces, _subchart(p, a, b, c)) + end + return pieces +end diff --git a/src/meshes/curvilinear_baryref.jl b/src/meshes/curvilinear_baryref.jl new file mode 100644 index 0000000..434f982 --- /dev/null +++ b/src/meshes/curvilinear_baryref.jl @@ -0,0 +1,99 @@ +""" + barycentric_refinement(mesh::CurvilinearMesh{U,3}; sort=:spacefillingcurve) + +Barycentric refinement of a mesh of curvilinear triangles of order `O`: every triangle is split +into the six triangles spanned by one of its corners, the midpoint of an adjacent edge, and its +centroid, all taken in the reference (barycentric) coordinates of the parent. + +The children are curvilinear triangles of the same order `O`, whose interpolation nodes are the +chart of the parent evaluated at the images of the reference nodes. Since the reference triangle +of a child is an affine image of a part of the parent's, the parent's map of degree `O` +restricted to it is again of degree `O`: the children reproduce the curved surface exactly, and +not only at their vertices, as a flat refinement would. + +Nodes shared by several children, of the same or of neighbouring parents, are merged, so that the +refined mesh is conforming. They are identified by their exact (rational) barycentric position on +the parent vertex, edge, or cell they lie on, and the parent's own nodes are reused where they +coincide (for `O ≥ 2`, e.g., the midpoints of the edges). The cells are numbered as in the +refinement of flat meshes: the children of the parent cell `p` are `6(p-1)+1:6p`, the pair +`6(p-1)+2j-1`, `6(p-1)+2j` being adjacent to the local edge `j`, opposite to the corner `j`, and +they have the orientation of their parent. The keyword `sort` is accepted for compatibility +with the method for flat meshes and has no effect. +""" +function barycentric_refinement(mesh::CurvilinearMesh{U,3,N,T,O}; sort=:spacefillingcurve) where {U,N,T,O} + + tuples = gmsh_triangle_index_to_tuple[O] + verts = copy(vertices(mesh)) + + # the key of the point with the exact barycentric coordinates λ in the parent cell F, whose + # corners have the global indices c, on the parent entity it lies on: a corner, an edge + # (oriented by its global vertex indices, so that neighbouring cells agree), or the cell + nodekey(F, c, λ) = + if count(!iszero, λ) == 1 + (:vertex, c[findfirst(!iszero, λ)]) + elseif count(!iszero, λ) == 2 + i, j = findall(!iszero, λ) + c[i] < c[j] ? (:edge, c[i], c[j], λ[i]) : (:edge, c[j], c[i], λ[j]) + else + (:cell, F, λ[1], λ[2]) + end + + # the nodes of the parent mesh, at their reference positions (i, j, k)/O + nodeindex = Dict{Any,Int}() + for (F, cell) in enumerate(cells(mesh)) + c = cell.indices + for (r, t) in enumerate(tuples) + get!(nodeindex, nodekey(F, c, t .// O), c[r]) + end + end + + corners = (SVector(1 // 1, 0 // 1, 0 // 1), SVector(0 // 1, 1 // 1, 0 // 1), SVector(0 // 1, 0 // 1, 1 // 1)) + centroid = SVector(1 // 3, 1 // 3, 1 // 3) + + fcs = Vector{HOSimplexGraph{3,O,N}}(undef, 6 * numcells(mesh)) + for (F, cell) in enumerate(cells(mesh)) + c = cell.indices + ch = chart(mesh, F) + for j in 1:3 + a = corners[mod1(j + 1, 3)] + b = corners[mod1(j + 2, 3)] + m = (a + b) / 2 # the midpoint of the local edge j + # the two children adjacent to the edge j, with the orientation of the parent (as + # (a, m, c) and (b, c, m) in the refinement of flat meshes) + for (s, Λ) in enumerate(((a, m, centroid), (b, centroid, m))) + idcs = map(tuples) do t + λ = (t[1] * Λ[1] + t[2] * Λ[2] + t[3] * Λ[3]) / O + get!(nodeindex, nodekey(F, c, λ)) do + push!(verts, barytocart(ch, SVector(T(λ[1]), T(λ[2])))) + length(verts) + end + end + fcs[6 * (F - 1) + 2 * (j - 1) + s] = HOSimplexGraph{3,O,N}(SVector{N,Int}(idcs)) + end + end + end + + refmesh = CurvilinearMesh(verts, fcs) + return BarycentricRefinement{U,3,T,typeof(refmesh),typeof(mesh)}(refmesh, mesh) +end + +""" + chartinparent(refinement::BarycentricRefinement{U,3}, cell) + +The cell `cell` of the barycentric refinement of a triangle mesh as a flat simplex in the +parametric domain of its parent, `parent(refinement, cell)`: its vertices are the parameters +`(u, v)` of its corners in the parent. Since the children are affine images of parts of the +parent's reference triangle, the parameters of a point of the child are mapped to those in the +parent by `cartesian(chartinparent(refinement, cell), u)`, also for curvilinear cells, whose +corners cannot be located in the parent from their coordinates alone. The children are +numbered as in `barycentric_refinement`. +""" +function chartinparent(refinement::BarycentricRefinement{U,3,T}, cell) where {U,T} + k = cell - 6 * (parent(refinement, cell) - 1) + j, s = divrem(k - 1, 2) .+ 1 + corners = (SVector{2,T}(1, 0), SVector{2,T}(0, 1), SVector{2,T}(0, 0)) + a, b = corners[mod1(j + 1, 3)], corners[mod1(j + 2, 3)] + m = (a + b) / 2 + c = SVector{2,T}(1, 1) / 3 + return s == 1 ? simplex(a, m, c) : simplex(b, c, m) +end diff --git a/src/meshes/curvilinear_mesh.jl b/src/meshes/curvilinear_mesh.jl index 770d176..ff9c164 100644 --- a/src/meshes/curvilinear_mesh.jl +++ b/src/meshes/curvilinear_mesh.jl @@ -105,7 +105,8 @@ function celltype(m::CurvilinearMesh{U,D1,N,T,O}, ::Type{Val{M}}) where {U,D1,N, HOSimplexGraph{M+1,O,binomial(M+O,O)} end -function indextype(m::CurvilinearMesh{U,D1,N,T,O}) where {U,D1,N,T,O} SVector{N-O+1,Int} end +# the indices of the corners only, as for flat meshes (N-O+1 was correct for lines alone) +function indextype(m::CurvilinearMesh{U,D1,N,T,O}) where {U,D1,N,T,O} SVector{D1,Int} end function indextype(m::CurvilinearMesh{U,D1,N,T,O}, ::Type{Val{M}}) where {U,D1,N,T,O,M} SVector{M+1,Int} end function indices(m::CurvilinearMesh{U,1,N,T,O}, cellptr) where {U,N,T,O} diff --git a/src/meshes/curvilinear_neighborhood.jl b/src/meshes/curvilinear_neighborhood.jl index 3ea0a8e..34fb532 100644 --- a/src/meshes/curvilinear_neighborhood.jl +++ b/src/meshes/curvilinear_neighborhood.jl @@ -7,6 +7,12 @@ function tangents(mp::MeshPointNM{T,C,D,U}, i) where {T,C<:CurvilinearSimplex,D, return unitarybase(mp.patch, mp.bary)[i] end +# all tangents as the columns of a U×D matrix, as for flat simplices +function tangents(mp::MeshPointNM{T,C,D,U}) where {T,C<:CurvilinearSimplex,D,U} + t = unitarybase(mp.patch, mp.bary) + return hcat(t...) +end + function normal(mp::MeshPointNM{T,C,1,U}) where {T,C<:CurvilinearSimplex,U} t = tangents(mp, 1) nt = SVector(-t[2], t[1]) @@ -25,4 +31,8 @@ end function jacobian(mp::MeshPointNM{T,C,D,U}) where {T,C<:CurvilinearSimplex,D,U} return jacobian(mp.patch, mp.bary) -end \ No newline at end of file +end +# The lazy neighborhood is meant for flat simplices, whose tangents and normal do not depend on +# the point and are taken from the chart. The tangents of a curved simplex do, and are computed +# from its unitary base at the point: as for quadrilaterals, the full neighborhood is returned. +neighborhood_lazy(ch::CurvilinearSimplex, u) = neighborhood(ch, u) diff --git a/src/meshes/curvilinear_silvester.jl b/src/meshes/curvilinear_silvester.jl index b47e3de..5f7c72d 100644 --- a/src/meshes/curvilinear_silvester.jl +++ b/src/meshes/curvilinear_silvester.jl @@ -220,4 +220,28 @@ end @inline dsilv(::Val{10}, ::Val{7}, ξ::T) where {T} = (70000000ξ^6 - 42000000ξ^5 + 12600000ξ^4 - 2352000ξ^3 + 277200ξ^2 - 17640ξ + 140) / 5040 @inline dsilv(::Val{10}, ::Val{8}, ξ::T) where {T} = (800000000ξ^7 - 560000000ξ^6 + 201600000ξ^5 - 47040000ξ^4 + 7257600ξ^3 - 725760ξ^2 + 40320ξ - 160) / 40320 @inline dsilv(::Val{10}, ::Val{9}, ξ::T) where {T} = (9000000000ξ^8 - 7200000000ξ^7 + 2835000000ξ^6 - 729000000ξ^5 + 135000000ξ^4 - 17280000ξ^3 + 1417500ξ^2 - 63000ξ + 180) / 362880 -@inline dsilv(::Val{10}, ::Val{10}, ξ::T) where {T} = (100000000000ξ^9 - 90000000000ξ^8 + 36000000000ξ^7 - 9450000000ξ^6 + 1800000000ξ^5 - 250000000ξ^4 + 24000000ξ^3 - 1440000ξ^2 + 48000ξ - 200) / 3628800 \ No newline at end of file +@inline dsilv(::Val{10}, ::Val{10}, ξ::T) where {T} = (100000000000ξ^9 - 90000000000ξ^8 + 36000000000ξ^7 - 9450000000ξ^6 + 1800000000ξ^5 - 250000000ξ^4 + 24000000ξ^3 - 1440000ξ^2 + 48000ξ - 200) / 3628800 + +# The same polynomials for an order known at compile time, as static vectors: no allocations, +# and the same arithmetic, hence the same values, as the methods above. The charts evaluate them +# at every point, e.g. at every quadrature point of every integral. +@inline function silvester(::Val{P}, x::T) where {P,T} + R = MVector{P + 1,T}(undef) + R[1] = T(1) + for i = 0:(P-1) + R[i + 1 + 1] = (P*x - i)/(i + 1)*R[i + 1] + end + return SVector(R) +end + +@inline function fast_silvester(::Val{P}, x::T) where {P,T} + R = MVector{P + 1,T}(undef) + dR = MVector{P + 1,T}(undef) + R[1] = T(1) + dR[1] = T(0) + for i = 0:(P - 1) + R[i + 1 + 1] = (P*x - i)/(i + 1)*R[i + 1] + dR[i + 1 + 1] = P/(i + 1) * R[i + 1] + (P*x - i)/(i + 1) * dR[i + 1] + end + return SVector(R), SVector(dR) +end diff --git a/test/runtests.jl b/test/runtests.jl index 65a7c62..dbab231 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -57,6 +57,8 @@ include("primitives/surfacemeshes/test_sphere.jl") include("primitives/volumemeshes/test_cuboid.jl") include("test_curvilinear_elements.jl") +include("test_curvilinear_baryref.jl") +include("test_curvilinear_intersect.jl") using TestItemRunner @run_package_tests diff --git a/test/test_curvilinear_baryref.jl b/test/test_curvilinear_baryref.jl new file mode 100644 index 0000000..075bd09 --- /dev/null +++ b/test/test_curvilinear_baryref.jl @@ -0,0 +1,110 @@ +using Test +using CompScienceMeshes +using StaticArrays +using LinearAlgebra +using SparseArrays + +const CSM = CompScienceMeshes + +@testset "barycentric refinement of curvilinear triangles, order $O" for O in 1:3 + + # gmsh returns a flat Mesh for order 1: the curvilinear mesh of order 1 is built from it + Γ = if O == 1 + Γflat = gmshsphere(1.0, 0.6) + CSM.CurvilinearMesh(vertices(Γflat), [CSM.HOSimplexGraph{3,1}(c.indices...) for c in cells(Γflat)]) + else + gmshsphere(1.0, 0.6; order=O) + end + @test Γ isa CSM.CurvilinearMesh + @test CSM.indextype(Γ) == SVector{3,Int} + + # the center of a curved triangle is the image of the reference centroid + for p in 1:numcells(Γ) + ch = chart(Γ, p) + @test parametric(center(ch)) ≈ SVector(1 / 3, 1 / 3) + @test cartesian(center(ch)) ≈ barytocart(ch, SVector(1 / 3, 1 / 3)) + end + O == 1 && @test all(cartesian(center(chart(Γ, p))) ≈ cartesian(center(chart(Γflat, p))) for p in 1:numcells(Γ)) + + # the edges of a curved triangle and their injections into its parametric domain agree + for p in 1:numcells(Γ) + ch = chart(Γ, p) + for (edge, inj) in CSM.subcharts(ch, Val{1}), s in (0.1, 0.5, 0.8) + @test barytocart(edge, SVector(s)) ≈ barytocart(ch, cartesian(inj, SVector(s))) + end + end + + R = barycentric_refinement(Γ) + F = R.mesh + + @test R isa CSM.BarycentricRefinement + @test CSM.refines(R, Γ) + @test F isa CSM.CurvilinearMesh + @test CSM.meshorder(F) == O + @test numcells(R) == 6 * numcells(Γ) + + # the children reproduce their parent exactly: a child's reference point μ is the parent's + # point Σ μ_i Λ_i, with Λ_i the parent-barycentric coordinates of the child's corners + corners = (SVector(1.0, 0, 0), SVector(0, 1.0, 0), SVector(0, 0, 1.0)) + ctr = SVector(1 / 3, 1 / 3, 1 / 3) + μs = [SVector(0.2, 0.3), SVector(0.6, 0.1), SVector(0.05, 0.9), SVector(1 / 3, 1 / 3)] + maxdev = 0.0 + for p in 1:numcells(Γ) + chp = chart(Γ, p) + for j in 1:3 + a, b = corners[mod1(j + 1, 3)], corners[mod1(j + 2, 3)] + m = (a + b) / 2 + for (s, Λ) in enumerate(((a, m, ctr), (b, ctr, m))) + chc = chart(F, 6 * (p - 1) + 2 * (j - 1) + s) + for μ in μs + λ = μ[1] * Λ[1] + μ[2] * Λ[2] + (1 - μ[1] - μ[2]) * Λ[3] + maxdev = max(maxdev, norm(barytocart(chc, μ) - barytocart(chp, SVector(λ[1], λ[2])))) + end + end + end + end + @test maxdev < 1e-13 + + # chartinparent maps the parameters of a child to those of its parent + maxdev = 0.0 + for q in 1:numcells(R) + p = CSM.parent(R, q) + m = CSM.chartinparent(R, q) + for μ in μs + maxdev = max(maxdev, norm(barytocart(chart(F, q), μ) - barytocart(chart(Γ, p), cartesian(m, μ)))) + end + end + @test maxdev < 1e-13 + + # conforming: every edge of the refined (closed) surface is shared by exactly two cells, with + # opposite orientations, and no node appears twice + edges = skeleton(F, 1) + D = connectivity(edges, F) + @test all(i -> length(nzrange(D, i)) == 2, 1:size(D, 2)) + @test all(i -> sum(nonzeros(D)[nzrange(D, i)] .|> sign) == 0, 1:size(D, 2)) + used = unique(reduce(vcat, [collect(c.indices) for c in cells(F)])) + @test length(unique(map(v -> round.(v, digits=10), vertices(F)[used]))) == length(used) + + # the Euler characteristic of the sphere, from the corners of the refined cells + nv = length(unique(reduce(vcat, [collect(CSM.indices(c)) for c in cells(F)]))) + @test nv - numcells(edges) + numcells(F) == 2 + + # the children keep the orientation of their parent: their normals agree at a common point + for p in 1:numcells(Γ), k in 1:6 + np = normal(neighborhood(chart(Γ, p), SVector(1 / 3, 1 / 3))) + chc = chart(F, 6 * (p - 1) + k) + nc = normal(neighborhood(chc, SVector(1 / 3, 1 / 3))) + @test dot(np, nc) > 0 + end + + # a flat parent (order 1): the refinement coincides with the one of the flat mesh + if O == 1 + Rflat = barycentric_refinement(Γflat) + @test numcells(Rflat) == numcells(R) + for q in 1:numcells(R) + chf = chart(Rflat, q) + chc = chart(R, q) + @test sort(collect(CSM.vertices(chf)), by=Tuple) ≈ sort(collect(CSM.nodes(chc)), by=Tuple) + end + end +end diff --git a/test/test_curvilinear_elements.jl b/test/test_curvilinear_elements.jl index aa6c5ae..daf6f00 100644 --- a/test/test_curvilinear_elements.jl +++ b/test/test_curvilinear_elements.jl @@ -187,6 +187,12 @@ m2 = CompScienceMeshes.CurvilinearMesh(verts2, faces2, 2, 2) ch1 = chart(m1, 1) ch2 = chart(m2, 1) +# the type-form queries agree with those on the chart +@test CSM.dimension(typeof(ch2)) == CSM.dimension(ch2) == 2 +@test CSM.universedimension(typeof(ch2)) == CSM.universedimension(ch2) == 3 +@test CSM.coordtype(typeof(ch2)) == CSM.coordtype(ch2) == Float64 +@test CSM.vertextype(typeof(ch2)) == CSM.vertextype(ch2) == SVector{3,Float64} + ## Order 7 v1 = SVector(1.0, 0.0, 0.0) diff --git a/test/test_curvilinear_intersect.jl b/test/test_curvilinear_intersect.jl new file mode 100644 index 0000000..fdd50d0 --- /dev/null +++ b/test/test_curvilinear_intersect.jl @@ -0,0 +1,47 @@ +using Test +using CompScienceMeshes +using StaticArrays +using LinearAlgebra + +# Locating points in curvilinear triangles, and the overlap and intersection of a cell of a mesh +# with the cells of its barycentric refinement: exact for a cell and its children, empty for the +# children of its neighbours, which share at most an edge with it. +@testset "intersection of curvilinear triangles, order $O" for O in 2:3 + + CSM = CompScienceMeshes + Γ = gmshsphere(1.0, 0.6; order=O) + R = barycentric_refinement(Γ) + + for c in 1:numcells(Γ) + P = chart(Γ, c) + + # carttobary inverts barytocart, also on the continuation beyond the reference triangle + for u in (SVector(0.2, 0.3), SVector(0.7, 0.1), SVector(-0.3, 0.6), SVector(1.2, 0.4)) + @test carttobary(P, barytocart(P, u)) ≈ u atol=1e-12 + end + + for k in 6(c-1)+1:6c + Q = chart(R, k) + @test overlap(P, Q) + @test overlap(Q, P) + for (A, B) in ((P, Q), (Q, P)) + pieces = intersection(A, B) + @test sum(volume, pieces) ≈ volume(Q) rtol=1e-12 + # the pieces have the orientation of the first operand + @test all(dot(normal(center(s)), normal(center(A))) > 0 for s in pieces) + end + for kk in 6(c-1)+1:6c + kk == k || @test !overlap(Q, chart(R, kk)) + end + end + + for d in 1:numcells(Γ) + d == c && continue + isempty(intersect(CSM.indices(Γ, c), CSM.indices(Γ, d))) && continue + for k in 6(d-1)+1:6d + @test !overlap(P, chart(R, k)) + @test isempty(intersection(P, chart(R, k))) + end + end + end +end