Algorithm Search⌘K

Algorithmcore

Newest vertex bisection

Refines a triangle by cutting from its newest vertex to the opposite edge, with a closure step to avoid hanging nodes, keeping angles bounded.

Time
Θ(M) average
Space
Θ(M) extra
Where
M = the number of triangles of the mesh before the call
Properties
bounded similarity classesconforming outputdeterministicnested meshes
Origin
E. G. Sewell, William F. Mitchell, Peter Binev, Wolfgang Dahmen, Ronald DeVore, 1972

Newest vertex bisection (NVB) refines a triangle mesh locally for the adaptive finite element method. Each triangle stores one of its vertices as the newest vertex, and the opposite edge is its refinement edge. A bisection cuts the triangle from the newest vertex to the midpoint of the refinement edge, and the midpoint becomes the newest vertex of both children. A closure step bisects more triangles until no vertex lies in the middle of an edge of another triangle, so the mesh stays conforming and a continuous Lagrange element space can live on it. The rule never makes thin triangles: all descendants of one initial triangle fall into at most four similarity classes, so the angles stay bounded below by a constant that depends only on the initial mesh.

In plain words

Cut a triangle in two, from one corner to the middle of the opposite side. The new point becomes the special corner of both halves, and the next cut of each half starts from there. Because the cut always starts at the newest corner, the halves never get very thin. After a few cuts, the triangles repeat the same few shapes at smaller and smaller sizes. A cut puts a new point in the middle of a side. The triangle on the other side of that side does not have this point as a corner, so it must also be cut. That cut may force another one, and so on. The closure step follows this chain until every point is a corner of all the triangles around it.

The problem

Input. A conforming triangulation T\mathcal{T} of a polygon Ω⊂R2\Omega \subset \mathbb{R}^2, where every triangle is an ordered triple [z1,z2,z3][z_1, z_2, z_3] of vertex numbers. The edge z1z2z_1z_2 is the refinement edge, and z3z_3 is the newest vertex. A set M⊂T\mathcal{M} \subset \mathcal{T} of marked triangles, usually from Dorfler marking. A list of boundary edges.

Output. The coarsest conforming triangulation T′\mathcal{T}' that we get from T\mathcal{T} by newest vertex bisections and in which every marked triangle is bisected at least once, with the refinement edges of the new triangles and the split boundary edges.

The algorithm

The code uses the form with marked edges of Funken, Praetorius and Wissgott. First it finds the set SS of edges that must be split. Then it bisects each triangle one, two or three times, according to which of its edges are in SS. This works in one pass because a bisection makes the two old edges z3z1z_3z_1 and z2z3z_2z_3 the refinement edges of the two children. So one triangle can split each of its three edges with at most three bisections, and the refinement edge always goes first.

  1. REFINE(T, M) ▷ each triangle is [z1, z2, z3]; z1z2 is its refinement edge
  2. 1S <- the refinement edges of the triangles in M; push each of them on a stack
  3. 2while the stack is not empty do pop an edge e
  4. 3for each triangle T of the mesh that contains e do
  5. 4if the refinement edge r of T is not in S then add r to S and push r
  6. 5for each edge in S do create its midpoint
  7. 6for each triangle [z1, z2, z3] with z1z2 in S do bisect it at the midpoint m of z1z2 into [z3, z1, m] and [z2, z3, m]
  8. 7look up the midpoints l of z3z1 and r of z2z3, if these edges are in S
  9. 8if z3z1 is in S then bisect [z3, z1, m] into [m, z3, l] and [z1, m, l]
  10. 9if z2z3 is in S then bisect [z2, z3, m] into [m, z2, r] and [z3, m, r]
  11. 10return the new triangles, and split every boundary edge in S into two

Triangles with no edge in SS are kept as they are. Line L4 is the closure rule. If some edge of TT will be split, then TT must be bisected, and its first bisection always cuts its refinement edge.

Initial labels. The labels of the initial mesh fix all later bisections. A labeling is compatible if every interior refinement edge is the refinement edge of both triangles that share it. Then the closure of a single triangle stops after the neighbor across the edge. On a mesh made of squares cut along one diagonal, the diagonals are a compatible choice. Every conforming triangulation in 2D has a compatible labeling (Mitchell 1989; Binev, Dahmen and DeVore 2004, Section 2). Labeling by the longest edge does not always give one.

Example

Take the unit square with corners v0=(0,0)v_0 = (0,0), v1=(1,0)v_1 = (1,0), v2=(1,1)v_2 = (1,1), v3=(0,1)v_3 = (0,1) and the two triangles [v0,v2,v1][v_0, v_2, v_1] and [v2,v0,v3][v_2, v_0, v_3]. Both have the diagonal v0v2v_0v_2 as refinement edge, so the labeling is compatible. The reference code gives the following steps.

Step Marked triangle Edges put in SS New vertices Triangles
0 none none none 2
1 [v0,v2,v1][v_0, v_2, v_1] v0v2v_0v_2 v4=(0.5,0.5)v_4 = (0.5, 0.5) 4
2 all 4 the 4 sides of the square v5=(0.5,0)v_5 = (0.5, 0), v6=(1,0.5)v_6 = (1, 0.5), v7=(0.5,1)v_7 = (0.5, 1), v8=(0,0.5)v_8 = (0, 0.5) 8
3 [v0,v4,v5][v_0, v_4, v_5] v0v4v_0v_4 v9=(0.25,0.25)v_9 = (0.25, 0.25) 10
4 [v4,v5,v9][v_4, v_5, v_9] v4v5v_4v_5, then v4v1v_4v_1 (0.5,0.25)(0.5, 0.25), (0.75,0.25)(0.75, 0.25) 14

In step 1, line L1 puts the diagonal in SS. Line L3 visits the other triangle [v2,v0,v3][v_2, v_0, v_3], but its refinement edge is the same diagonal, so line L4 adds nothing. Both triangles are bisected once, and the result is [v1,v0,v4][v_1, v_0, v_4], [v2,v1,v4][v_2, v_1, v_4], [v3,v2,v4][v_3, v_2, v_4], [v0,v3,v4][v_0, v_3, v_4]. All four have the new vertex v4v_4 as newest vertex and a side of the square as refinement edge.

Step 4 shows the closure. The marked triangle has the refinement edge v4v5v_4v_5, which is the vertical segment from (0.5,0.5)(0.5, 0.5) to (0.5,0)(0.5, 0). Its neighbor across that edge is [v4,v1,v5][v_4, v_1, v_5], and the refinement edge of the neighbor is v4v1v_4v_1, not v4v5v_4v_5. So line L4 adds v4v1v_4v_1 to SS. The triangle on the other side of v4v1v_4v_1 is [v1,v4,v6][v_1, v_4, v_6], and its refinement edge is v1v4v_1v_4 itself, so the chain stops. Lines L6 to L9 then bisect [v4,v5,v9][v_4, v_5, v_9] once, [v1,v4,v6][v_1, v_4, v_6] once and [v4,v1,v5][v_4, v_1, v_5] twice (first at v4v1v_4v_1 and then the child that contains v4v5v_4v_5). The count is 10−3+2+2+3=1410 - 3 + 2 + 2 + 3 = 14 triangles, and no vertex hangs.

Shapes. The test file refines the scalene triangle (0,0)(0,0), (1,0)(1,0), (0.3,0.7)(0.3, 0.7) twelve times with random marks. After 12 steps the mesh is conforming and has at most four distinct triangle shapes up to similarity, for each of the three choices of the first refinement edge. Marking all triangles of this single triangle mesh eight times gives 28=2562^8 = 256 triangles with total area 0.350.35.

Correctness

The full proof is in the correctness proof. Every edge enters the stack at most once, so the closure loop stops. When the loop ends, every triangle that has some edge in SS also has its refinement edge in SS. So each triangle falls into one of four cases (no split, one, two or three bisections), and in each case the parent edges in SS appear as two halves on both sides and the other parent edges stay whole. So the output is conforming. Minimality follows by induction over the order in which edges enter SS: a conforming refinement that bisects the marked triangles must split every edge in SS, because a split edge leaves a hanging node on the neighbor until the neighbor is bisected, and a bisection always starts at the refinement edge. The test file checks the output against a second method that bisects triangles with hanging nodes until none is left, on nine random steps on the L-shaped domain.

Proof: The marked-edge form of newest vertex bisection gives the coarsest conforming refinementfull

Setting. T\mathcal{T} is a conforming triangulation of a polygon: two triangles meet in a common vertex, a common edge or not at all. Each triangle is stored as an ordered triple [z1,z2,z3][z_1, z_2, z_3]. The edge z1z2z_1z_2 is its refinement edge, and z3z_3 is its newest vertex. One bisection of [z1,z2,z3][z_1, z_2, z_3] adds the midpoint mm of z1z2z_1z_2 and gives the two children [z3,z1,m][z_3, z_1, m] and [z2,z3,m][z_2, z_3, m]. So the new vertex mm is the newest vertex of both children, and the two old edges z3z1z_3z_1 and z2z3z_2z_3 become their refinement edges. A refinement of T\mathcal{T} by newest vertex bisection is any triangulation that we get from T\mathcal{T} by a finite number of such bisections. The input is a set M\mathcal{M} of marked triangles. REFINE is the procedure of the main page, lines L1 to L10.

We prove four claims.

  1. (Termination.) REFINE stops after at most ∣E∣|\mathcal{E}| passes of the loop at line L2, where E\mathcal{E} is the set of edges of T\mathcal{T}.
  2. (Closure.) At line L5, for every triangle TT, if any edge of TT is in the set SS of split edges, then the refinement edge of TT is in SS.
  3. (Conformity.) The output is a conforming triangulation of the same polygon, every marked triangle is bisected at least once, and the output is a refinement of T\mathcal{T} by newest vertex bisection.
  4. (Minimality.) Every conforming refinement of T\mathcal{T} by newest vertex bisection in which every marked triangle is bisected is equal to the output of REFINE or is a refinement of it.

Proof of 1. An edge enters the stack only when it enters SS (lines L1 and L4), and it never leaves SS. So every edge enters the stack at most once, and each pass of line L2 removes one edge from the stack. The loop at line L3 visits the at most two triangles that contain the edge. So the number of passes is at most ∣E∣|\mathcal{E}|, and the total work of lines L2 to L4 is O(∣E∣)O(|\mathcal{E}|).

Proof of 2. Let e∈Se \in S be an edge of TT. The edge ee entered the stack when it entered SS, and the loop at line L2 runs until the stack is empty, so ee was popped once. At that pass, line L3 visited every triangle that contains ee, and TT is one of them. Line L4 then put the refinement edge of TT into SS, or it was there already.

Proof of 3. By claim 2, each triangle T=[z1,z2,z3]T = [z_1, z_2, z_3] falls into one of four cases at line L6: no edge of TT is in SS; only z1z2z_1z_2 is in SS; z1z2z_1z_2 and one other edge are in SS; all three edges are in SS. In the first case TT is kept. In the second case line L6 bisects TT once. If the left edge z3z1z_3z_1 is in SS, line L8 bisects the child [z3,z1,m][z_3, z_1, m], whose refinement edge is z3z1z_3z_1. If the right edge z2z3z_2z_3 is in SS, line L9 bisects the child [z2,z3,m][z_2, z_3, m], whose refinement edge is z2z3z_2z_3. So in every case the children of TT come from bisections by the rule of the setting, and the output is a refinement by newest vertex bisection. A marked triangle has its refinement edge in SS by line L1, so it is bisected at least once.

Now look at the edges of the children inside TT. A direct check of the four cases shows:

  • an edge of TT that is not in SS is a whole edge of exactly one child;
  • an edge of TT that is in SS, with midpoint pp from line L5, is the union of two edges of children, which meet at pp;
  • every other edge of a child lies inside TT (apart from its end points) and is shared by exactly two children of TT.

For example, in the case with all three edges split, the children are [m,z3,l][m, z_3, l], [z1,m,l][z_1, m, l], [m,z2,r][m, z_2, r] and [z3,m,r][z_3, m, r], where ll and rr are the midpoints of z3z1z_3z_1 and z2z3z_2z_3. The edge z1z2z_1z_2 becomes z1mz_1m and mz2mz_2, the edge z3z1z_3z_1 becomes z3lz_3l and lz1lz_1, the edge z2z3z_2z_3 becomes z2rz_2r and rz3rz_3, and the inner edges mz3mz_3, mlml and mrmr each lie in two children.

The children of TT tile TT, because a bisection cuts a triangle into two triangles along a segment. So the output tiles the polygon. For conformity, take an edge ee of T\mathcal{T} that lies in two triangles TT and T′T'. The decision whether ee is in SS does not depend on the side, and line L5 gives ee one midpoint. If e∉Se \notin S, then ee is a whole edge of one child on each side. If e∈Se \in S, then both sides have the two halves of ee as edges, with the same midpoint. In both cases no vertex lies inside an edge of a child on the other side. Inner edges of TT meet only children of TT. So no hanging node appears, and the output is conforming. The boundary loop after line L9 splits a boundary edge exactly when it is in SS, so the boundary list matches the boundary of the new mesh.

Proof of 4. Let T∗\mathcal{T}^* be a conforming refinement by newest vertex bisection in which every marked triangle is bisected. Say that T∗\mathcal{T}^* splits an edge ee of T\mathcal{T} if the midpoint of ee is a vertex of T∗\mathcal{T}^*. We show by induction over the order in which edges enter SS that T∗\mathcal{T}^* splits every edge in SS.

An edge that enters at line L1 is the refinement edge of a marked triangle TT. The first bisection of TT always cuts its refinement edge, and TT is bisected in T∗\mathcal{T}^*. So T∗\mathcal{T}^* splits it.

An edge rr that enters at line L4 is the refinement edge of a triangle TT that contains an edge ee which entered SS before rr. By the induction hypothesis, the midpoint pp of ee is a vertex of T∗\mathcal{T}^*. Because T∗\mathcal{T}^* is conforming, pp cannot lie inside an edge of a triangle of T∗\mathcal{T}^*. If TT were not bisected in T∗\mathcal{T}^*, then TT would be a triangle of T∗\mathcal{T}^* with pp inside its edge ee, which is a hanging node. So TT is bisected, and its first bisection cuts rr. So T∗\mathcal{T}^* splits rr.

So T∗\mathcal{T}^* splits every edge of SS. Fix a triangle TT of T\mathcal{T}. The descendants of TT in T∗\mathcal{T}^* form a refinement of TT by bisection that contains the midpoint of every edge of TT in SS. The bisection of a triangle is fixed by its labels, so the refinements of TT form a binary tree of bisections. The children that REFINE gives to TT are the smallest subtree that splits exactly the edges of TT in SS: one bisection for the refinement edge, and one more bisection of a child for each of the other two edges, as no other bisection adds the midpoint of that edge. So the descendants of TT in T∗\mathcal{T}^* are the REFINE children of TT or refinements of them. This holds for every TT, which proves claim 4. □\square

What the proof does not cover. The claims above hold for every labeling of the initial triangles, and one call of REFINE always stops. Two further facts are cited, not proved here. First, all descendants of one initial triangle fall into at most four similarity classes (Sewell 1972; Mitchell 1989), so the minimum angle stays bounded below by a constant that depends only on T\mathcal{T}. The test file checks this on a scalene triangle. Second, the closure estimate of Binev, Dahmen and DeVore (2004), #Tk−#T0≤C∑j<k#Mj\#\mathcal{T}_k - \#\mathcal{T}_0 \le C\sum_{j<k}\#\mathcal{M}_j, needs a compatible labeling of T0\mathcal{T}_0 in their proof. Karkulik, Pavlicek and Praetorius (2013) proved it in 2D for every labeling.

Complexity

With a hash map from edges to triangles, one call costs Θ(M)\Theta(M) expected time in the real-RAM model for a mesh of MM triangles: building the map, the closure (each edge is pushed at most once and has at most two triangles) and the bisection are all linear. The memory is Θ(M)\Theta(M). The output has at most 4M4M triangles.

One call can bisect triangles far from the marked ones, because a closure chain can be long. So the size of one step is not bounded by a constant times #M\#\mathcal{M}. The bound holds on average over the whole adaptive loop. Binev, Dahmen and DeVore proved that, for a compatible initial labeling, #Tk−#T0≤C∑j=0k−1#Mj\#\mathcal{T}_k - \#\mathcal{T}_0 \le C\sum_{j=0}^{k-1}\#\mathcal{M}_j with CC that depends only on T0\mathcal{T}_0. Karkulik, Pavlicek and Praetorius proved the same estimate in 2D with no condition on the initial labeling. This closure estimate is one of the three parts of the AFEM optimality theorem.

Properties

  • Conformity. The output has no hanging nodes, so P1 functions on it are continuous, and the P1 space of T\mathcal{T} is a subspace of the P1 space of T′\mathcal{T}'.
  • Shape regularity. All descendants of one initial triangle are similar to at most four triangles (Sewell 1972; Mitchell 1989). The minimum angle and the ratio of diameter to inradius stay bounded by constants that depend only on T0\mathcal{T}_0.
  • Area halving. Each bisection halves the area. A triangle of generation gg has area 2−g2^{-g} times the area of its initial ancestor.
  • Uniform refinement. With a compatible labeling, two rounds of marking every triangle cut each triangle into four triangles of area one quarter. All three edges of the parent are split at their midpoints.
  • Binary tree. The refinements of one initial triangle form a binary tree of bisections, so the set of all NVB meshes is a lattice: two NVB refinements of T0\mathcal{T}_0 have a coarsest common refinement. The optimality proofs use this overlay.
  • Orientation. The children keep the orientation of the parent, as the test file checks with signed areas.

Variants and improvements

  • Recursive form. Mitchell refines a marked triangle by first refining its neighbor across the refinement edge if the two edges do not agree, recursively, and then bisecting both together. The result is the same mesh as the form with marked edges.
  • More bisections per marked triangle. Some codes split all three edges of each marked triangle, which gives four children in one step. The convergence proof of Morin, Nochetto and Siebert (2000) needed several bisections, so that each marked triangle gets a new vertex inside it. Cascon, Kreuzer, Nochetto and Siebert (2008) showed that one bisection is enough.
  • Longest edge bisection. Rivara cuts each triangle at its longest edge. The angles also stay bounded, but the closure is longer and the theory of optimal rates is less complete.
  • Red-green refinement. Bank, Sherman and Weiser cut a marked triangle into four similar children (red) and remove hanging nodes with temporary bisections (green). The green triangles must be removed before the next step.
  • Higher dimensions. Bansch, Kossaczky, Maubach and Traxler gave bisection rules for tetrahedra. Stevenson extended the closure estimate to any dimension under a condition on the initial mesh.
  • Coarsening. The binary tree makes coarsening simple: two children with the same parent can be merged back if no neighbor needs them. The 2004 algorithm of Binev, Dahmen and DeVore used coarsening, and later proofs removed it.

When to use it

Use NVB for adaptive finite elements on triangles, especially when the code must follow the theory of convergence rates: the closure estimate, the bounded shapes and the nested spaces are exactly what the proofs need. It is simple to store, because each triangle only needs the order of its vertices. For meshes that must stretch in one direction, for example along boundary layers, NVB is the wrong tool, because it keeps the shapes of the initial mesh; use anisotropic mesh adaptation. For structured grids with block refinement in time dependent problems, use block-structured AMR.

Pitfalls

  • Vertex order. The refinement edge must be stored as z1z2z_1z_2 in every triangle. A mesh generator that outputs counterclockwise triples with no thought about labels gives a valid labeling, but it can be far from compatible, and then the closure chains get long.
  • Missed boundary split. If the list of boundary edges is not split with the triangles, the midpoints of Dirichlet edges become free nodes, and the solve imposes a wrong boundary condition with no error message.
  • Hash keys. The edge z1z2z_1z_2 and the edge z2z1z_2z_1 must give the same key. Store each edge as the pair (smaller number, larger number).
  • Mixed refinement rules. Mixing NVB with other cuts, such as red refinement on some triangles, breaks the bound on similarity classes and the closure estimate.
  • Cost of one step. Do not expect one call to bisect only a few triangles beyond the marked ones. The bound on the number of extra triangles holds only summed over all steps.
  • Floating point midpoints. Computing midpoints again from coordinates for each triangle can give two different nodes for one edge after many levels. Create each midpoint once per edge, as line L5 does.

Implementations

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

newest_vertex_bisection.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/newest-vertex-bisection/. The authors are listed in prov/GGH2BB.jsonl.

"""Newest vertex bisection (NVB) of a conforming triangulation in 2D.

A mesh is (coords, elements, boundary). coords is a list of (x, y) points.
An element [z1, z2, z3] has the refinement edge z1 z2 and the newest vertex
z3, the vertex opposite the refinement edge. boundary is a list of boundary
edges [a, b]; they are split together with the elements, so that boundary
conditions can follow the refinement.

refine marks the refinement edges of the marked elements, closes the set of
marked edges (a marked edge of an element forces its refinement edge), and
then bisects each element one, two or three times. The result is the
smallest conforming NVB refinement in which every marked element is
bisected at least once. Cost: O(M) for M elements with a hash map
(real-RAM, expected time of the hash map).
"""


def _edge(a, b):
    return (a, b) if a < b else (b, a)


def refine(coords, elements, boundary, marked):
    """Return the refined mesh (coords, elements, boundary)."""
    coords = list(coords)
    touching = {}
    for t, z in enumerate(elements):
        for r in range(3):
            touching.setdefault(_edge(z[r], z[(r + 1) % 3]), []).append(t)
    split = {}
    stack = []
    for t in marked:  # @line L1
        e = _edge(elements[t][0], elements[t][1])
        if e not in split:
            split[e] = None
            stack.append(e)
    while stack:  # closure over a work list  # @line L2
        e = stack.pop()
        for t in touching[e]:  # @line L3
            z = elements[t]
            r = _edge(z[0], z[1])
            if r not in split:  # @line L4
                split[r] = None
                stack.append(r)
    for e in split:  # @line L5
        (xa, ya), (xb, yb) = coords[e[0]], coords[e[1]]
        split[e] = len(coords)
        coords.append(((xa + xb) / 2, (ya + yb) / 2))
    new = []
    for z1, z2, z3 in elements:  # @line L6
        m = split.get(_edge(z1, z2))
        if m is None:
            new.append([z1, z2, z3])
            continue
        left = split.get(_edge(z3, z1))  # @line L7
        right = split.get(_edge(z2, z3))
        if left is None:
            new.append([z3, z1, m])
        else:
            new.extend([[m, z3, left], [z1, m, left]])  # @line L8
        if right is None:
            new.append([z2, z3, m])
        else:
            new.extend([[m, z2, right], [z3, m, right]])  # @line L9
    bnew = []
    for a, b in boundary:
        m = split.get(_edge(a, b))
        if m is None:
            bnew.append([a, b])
        else:
            bnew.extend([[a, m], [m, b]])
    return coords, new, bnew  # @line L10


def refine_by_hanging_nodes(coords, elements, boundary, marked):
    """Second method, used as an oracle: bisect the marked elements once, then
    bisect any element that has a hanging node on an edge until none is left.
    Cost O(M^2) in the worst case.
    """
    coords = list(coords)
    where = {p: i for i, p in enumerate(coords)}

    def midpoint(a, b):
        (xa, ya), (xb, yb) = coords[a], coords[b]
        p = ((xa + xb) / 2, (ya + yb) / 2)
        if p not in where:
            where[p] = len(coords)
            coords.append(p)
        return where[p]

    def bisect(z):
        m = midpoint(z[0], z[1])
        return [[z[2], z[0], m], [z[1], z[2], m]]

    marked = set(marked)
    elements = [c for t, z in enumerate(elements) for c in (bisect(z) if t in marked else [z])]
    while True:
        out, changed = [], False
        for z in elements:
            hanging = False
            for r in range(3):
                (xa, ya), (xb, yb) = coords[z[r]], coords[z[(r + 1) % 3]]
                if ((xa + xb) / 2, (ya + yb) / 2) in where:
                    hanging = True
            if hanging:
                out.extend(bisect(z))
                changed = True
            else:
                out.append(z)
        elements = out
        if not changed:
            break
    bnew = []
    work = [list(e) for e in boundary]
    while work:
        a, b = work.pop()
        (xa, ya), (xb, yb) = coords[a], coords[b]
        p = ((xa + xb) / 2, (ya + yb) / 2)
        if p in where:
            work.extend([[a, where[p]], [where[p], b]])
        else:
            bnew.append([a, b])
    return coords, elements, bnew


def hanging_nodes(coords, elements):
    """Return the list of (element, edge) pairs whose edge midpoint is a mesh vertex."""
    used = {coords[v] for z in elements for v in z}
    bad = []
    for t, z in enumerate(elements):
        for r in range(3):
            (xa, ya), (xb, yb) = coords[z[r]], coords[z[(r + 1) % 3]]
            if ((xa + xb) / 2, (ya + yb) / 2) in used:
                bad.append((t, r))
    return bad


def is_conforming(coords, elements, boundary):
    """Check that every edge lies in two elements or is a boundary edge, and that no node hangs."""
    count = {}
    for z in elements:
        for r in range(3):
            e = _edge(z[r], z[(r + 1) % 3])
            count[e] = count.get(e, 0) + 1
    bset = {_edge(a, b) for a, b in boundary}
    for e, c in count.items():
        if c > 2 or (c == 1 and e not in bset):
            return False
    return all(count.get(e) == 1 for e in bset) and not hanging_nodes(coords, elements)


def area(coords, z):
    """Return the signed area of the element z."""
    (x0, y0), (x1, y1), (x2, y2) = coords[z[0]], coords[z[1]], coords[z[2]]
    return ((x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0)) / 2


def shape_key(coords, z, digits=9):
    """Return a key that is equal for similar triangles: sorted squared side ratios."""
    p = [coords[v] for v in z]
    s = sorted((p[r][0] - p[(r + 1) % 3][0]) ** 2 + (p[r][1] - p[(r + 1) % 3][1]) ** 2 for r in range(3))
    return (round(s[0] / s[2], digits), round(s[1] / s[2], digits))
test_newest_vertex_bisection.py 98 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/newest-vertex-bisection/. The authors are listed in prov/GGH2BB.jsonl.

import random

from newest_vertex_bisection import (area, hanging_nodes, is_conforming, refine,
                                     refine_by_hanging_nodes, shape_key)


def l_shape():
    coords = [(-1.0, -1.0), (0.0, -1.0), (-1.0, 0.0), (0.0, 0.0),
              (1.0, 0.0), (-1.0, 1.0), (0.0, 1.0), (1.0, 1.0)]
    elements = [[0, 3, 1], [3, 0, 2], [3, 5, 2], [5, 3, 6], [3, 7, 4], [7, 3, 6]]
    boundary = [[0, 1], [1, 3], [3, 4], [4, 7], [7, 6], [6, 5], [5, 2], [2, 0]]
    return coords, elements, boundary


def canon(coords, elements):
    return sorted(tuple(sorted(coords[v] for v in z)) for z in elements)


def total_area(coords, elements):
    return sum(area(coords, z) for z in elements)


def test_single_triangle_hand_example():
    c, e, b = refine([(0.0, 0.0), (1.0, 0.0), (0.0, 1.0)], [[0, 1, 2]],
                     [[0, 1], [1, 2], [2, 0]], [0])
    assert c[3] == (0.5, 0.0)
    assert e == [[2, 0, 3], [1, 2, 3]]
    assert sorted(tuple(x) for x in b) == [(0, 3), (1, 2), (2, 0), (3, 1)]
    assert all(z[2] == 3 for z in e)


def test_closure_on_two_triangles():
    coords = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)]
    elements = [[0, 2, 1], [2, 0, 3]]
    boundary = [[0, 1], [1, 2], [2, 3], [3, 0]]
    c, e, b = refine(coords, elements, boundary, [0])
    assert len(e) == 4
    assert is_conforming(c, e, b)
    c2, e2, b2 = refine(c, e, b, [0])
    assert is_conforming(c2, e2, b2)
    assert len(e2) == 5  # the refinement edge of element 0 is on the boundary
    c3, e3, b3 = refine(c2, e2, b2, [t for t in range(len(e2)) if 4 in e2[t][:2]])
    assert is_conforming(c3, e3, b3)
    assert abs(sum(abs(area(c3, z)) for z in e3) - 1.0) < 1e-12


def test_matches_hanging_node_oracle_and_conforms():
    rng = random.Random(7)
    coords, elements, boundary = l_shape()
    signed = total_area(coords, elements)
    for _ in range(9):
        marked = [t for t in range(len(elements)) if rng.random() < 0.15] or [0]
        parents = [tuple(sorted(coords[v] for v in elements[t])) for t in marked]
        c1, e1, b1 = refine(coords, elements, boundary, marked)
        c2, e2, _ = refine_by_hanging_nodes(coords, elements, boundary, marked)
        assert canon(c1, e1) == canon(c2, e2)
        assert is_conforming(c1, e1, b1)
        assert not hanging_nodes(c1, e1)
        assert abs(sum(abs(area(c1, z)) for z in e1) - 3.0) < 1e-12
        assert abs(total_area(c1, e1) - signed) < 1e-12  # bisection keeps the orientation
        assert all(abs(area(c1, z)) > 0 for z in e1)
        children = set(canon(c1, e1))
        assert all(p not in children for p in parents)
        coords, elements, boundary = c1, e1, b1


def test_uniform_bisection_of_one_triangle_doubles():
    coords = [(0.0, 0.0), (1.0, 0.0), (0.3, 0.7)]
    elements = [[0, 1, 2]]
    boundary = [[0, 1], [1, 2], [2, 0]]
    for k in range(1, 9):
        coords, elements, boundary = refine(coords, elements, boundary, list(range(len(elements))))
        assert len(elements) == 2 ** k
        assert abs(total_area(coords, elements) - 0.35) < 1e-12
        assert is_conforming(coords, elements, boundary)


def test_at_most_four_similarity_classes():
    rng = random.Random(3)
    base = [(0.0, 0.0), (1.0, 0.0), (0.3, 0.7)]
    for rot in range(3):
        coords = list(base)
        elements = [[rot % 3, (rot + 1) % 3, (rot + 2) % 3]]
        boundary = [[0, 1], [1, 2], [2, 0]]
        for _ in range(12):
            marked = [t for t in range(len(elements)) if rng.random() < 0.3] or [0]
            coords, elements, boundary = refine(coords, elements, boundary, marked)
        assert is_conforming(coords, elements, boundary)
        assert len({shape_key(coords, z) for z in elements}) <= 4


def test_unmarked_mesh_is_unchanged():
    coords, elements, boundary = l_shape()
    c, e, b = refine(coords, elements, boundary, [])
    assert c == coords and e == elements and b == boundary

History

E. G. Sewell introduced the newest vertex rule in his 1972 PhD thesis at Purdue University, and showed that the descendants of a triangle fall into a few similarity classes. William Mitchell gave the recursive refinement and compared it with other adaptive refinement methods in 1989. Eberhard Bansch, Igor Kossaczky, Joseph Maubach and Christoph Traxler extended bisection to tetrahedra in the 1990s. Peter Binev, Wolfgang Dahmen and Ronald DeVore proved in 2004 that the closure does not spoil the complexity of the adaptive loop, which made optimal rates for adaptive finite elements possible. Rob Stevenson extended this to higher dimensions, and Michael Karkulik, David Pavlicek and Dirk Praetorius removed the condition on the initial labeling in 2D in 2013.

References

  1. William F. Mitchell, A comparison of adaptive refinement techniques for elliptic problems, ACM Transactions on Mathematical Software 15(4) (1989) 326-347, newest node bisection, its recursive form and the bound on the similarity classes
  2. Peter Binev, Wolfgang Dahmen and Ronald DeVore, Adaptive finite element methods with convergence rates, Numerische Mathematik 97(2) (2004) 219-268, Section 2 (newest vertex bisection, compatible initial labeling and the closure estimate)
  3. Michael Karkulik, David Pavlicek and Dirk Praetorius, On 2D newest vertex bisection, optimality of mesh-closure and H1-stability of L2-projection, Constructive Approximation 38(2) (2013) 213-234, the closure estimate in 2D for an arbitrary initial labeling
  4. Stefan Funken, Dirk Praetorius and Philipp Wissgott, Efficient implementation of adaptive P1-FEM in Matlab, Computational Methods in Applied Mathematics 11(4) (2011) 460-490, the refinement by marked edges, the closure loop and the vectorized bisection

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.average
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example