Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/CompScienceMeshes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")

Expand Down
59 changes: 42 additions & 17 deletions src/charts/curvilinear_chart.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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])

Expand Down Expand Up @@ -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})
Expand Down Expand Up @@ -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}()
Expand Down Expand Up @@ -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
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
96 changes: 96 additions & 0 deletions src/charts/curvilinear_intersect.jl
Original file line number Diff line number Diff line change
@@ -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
99 changes: 99 additions & 0 deletions src/meshes/curvilinear_baryref.jl
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion src/meshes/curvilinear_mesh.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
12 changes: 11 additions & 1 deletion src/meshes/curvilinear_neighborhood.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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
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)
26 changes: 25 additions & 1 deletion src/meshes/curvilinear_silvester.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
@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
Loading
Loading