Skip to content

Add more bases - #21

Merged
davschneller merged 8 commits into
mainfrom
davschneller/bernstein
Oct 21, 2026
Merged

davschneller merged 8 commits into
mainfrom
davschneller/bernstein

Conversation

@davschneller

Copy link
Copy Markdown
Contributor
  • Add a (space function) basis abstraction
  • Add normalized Dubiner (identity mass matrix)
  • Add Bernstein-Bézier (advantage: many matrices become permutations)

The Dubiner basis is orthogonal, and many of the integrals that make up
the forms of dg_generator.multilinear_form vanish in exact arithmetic; the
quadrature leaves rounding of a few 1e-16 of the largest entry of the form
there, which the solves with the mass matrices spread further. json_io
writes every entry above 1e-14, so whether such an entry is written
depends on the order of the floating-point operations and on how large the
other entries of its tensor are, and with it the sparsity pattern that a
code generator reads from the file.

multilinear_form now sets every entry of at most 1e-12 times the largest
entry of its form to zero (snap_zeros). Up to order 8 the rounding stays
below 1.3e-15 of the largest entry, and the smallest integral that does not
vanish is 3.3e-9 of it. The mass matrices are diagonal then, and the
matrices computed from them have their zeros where the integrals have
theirs.

In the files that generate.py writes for orders 2 to 8 (ader with and
without a material order, dr, hom and vtk; 2145 tensors), 108758 entries of
kDivM, kDivMT and rDivM disappear, all of at most 8.1e-15 of the largest
entry of their tensor. V3mTo2nTWDivM, whose entries are no integrals but
pass through the mass matrix, rounds differently with the diagonal one: 61
of its entries just above the cutoff of 1e-14 (up to 1.5e-14) fall below
it, and 59 others rise above it (up to 1.4e-14). No entry changes by more
than 8.1e-15 of the largest entry of its tensor. Of the 1159 matrices the
generators compute for these orders (the ones above and the plasticity,
nodal boundary, face quadrature, energy, STP and collocation matrices), 601
stay bit-identical, and the largest change is 1.8e-14 of the largest entry,
in the STP matrix Z of order 6.

Tests check snap_zeros, that the mass matrices of orders 2 to 5 are
diagonal, and that kDivM and rDivM have the patterns of the stiffness
matrices and of fMrT.

AI-generated. Model: Claude Opus 5.5
A generator now evaluates all of its functions, or their derivatives, at an
array of points in one call (eval_all, eval_diff_all; shape points x
functions), and multilinear_form contracts these arrays with the quadrature
weights in a single einsum instead of integrating every index tuple of the
form on its own. A form of three factors at order 8 has 120^3 index tuples,
each of which evaluated its basis functions anew.

The matrices agree with the ones before to rounding: over the 1159 matrices
of the generators for orders 2 to 8 (mass, stiffness, kDivM, kDivMT, rT,
rDivM, fMrT, fP, the DR, plasticity, nodal boundary, face quadrature,
energy, STP and collocation matrices), the largest difference relative to
the largest entry of its matrix is 1.4e-15, and 561 are bit-identical. With
the zeros of the integrals exact, the files that generate.py writes for
these orders keep all of their entries, and no value changes by more than
1.9e-15 of the largest entry of its tensor. The test suite runs in 66 s
instead of 102 s. A new test checks that the functions evaluated at once are
the ones evaluated one by one.

AI-generated. Model: Claude Opus 5.5
basis_functions.generator(family, dim, order) returns the basis of a family
on the reference interval, triangle or tetrahedron (FAMILIES):

- dubiner: the basis as before.
- dubiner-normalized: the Dubiner functions divided by their L2 norm on the
  reference element, so that the mass matrices are identities. The norms are
  in closed form (norm_squared): 1 / (2 (2i+1) (i+j+1) (2i+2j+2k+3)) on the
  tetrahedron, 1 / (2 (2i+1) (i+j+1)) on the triangle and 1 / (2i+1) on the
  interval; they agree with the mass matrix to 1e-15.
- bernstein: the Bernstein-Bezier basis in the barycentric coordinates of the
  reference element, with the multi-indices (a_1, ..., a_dim) ordered with
  a_dim slowest and a_1 fastest (a_0 implied), so that raising one index by
  one shifts a run of consecutive positions by a fixed offset.

A generator states whether it is hierarchical, orthogonal and orthonormal, so
that a consumer need not assume what only the Dubiner basis has. A
dimension other than 1, 2 or 3 raises a ValueError from
basis_functions.generator, and points of another shape than (dim, points)
are rejected.

dg_generator and the generators of the DR, plasticity, nodal boundary, face
quadrature and energy matrices take the family, Dubiner by default; they
evaluate the basis through eval_all, which also replaces their loops over
points and functions. For the Dubiner basis the matrices are bit-identical to
the ones before, but for vInv of the plasticity, which solves with the full
mass matrix instead of dividing by its diagonal, as the Bernstein basis
needs, and differs by at most 8.7e-17 of its largest entry; V3mTo2n keeps
axes of length one (order 1, or a rule of one point) instead of dropping
them.

New tests check, for every family: that the functions span the polynomials
of the degree and their derivatives are the ones of those polynomials; the
mass matrices (the closed-form norms, the identity, symmetric positive
definite); that kDivMT gives the derivative, rT the trace and rDivM is
M^-1 rT^T; and that vInv reproduces the basis. For the Bernstein basis they
check the partition of unity, the vertex functions and the order.

AI-generated. Model: Claude Opus 5.5
bernstein_matrices states the operators of the Bernstein-Bezier basis in the
form a kernel applies them in, with exact rational entries, instead of the
dense matrices the generic generators give for it:

- derivative: d / d_x_k from degree m to m - 1,
  (D c)_b = m (c_(b + e_(k+1)) - c_(b + e_0)), two entries per row;
- elevation: from degree m - 1 to m, (E c)_a = sum_i a_i / m c_(a - e_i);
- trace: the face coefficients are the volume coefficients whose index at
  the opposite vertex is zero, a gather (trace_indices);
- face_permutation: a reparametrisation of a face permutes its coefficients;
- lift: M^-1 rT^T M_face factorises (Chan and Warburton, 2017) into
  L_0 = (2m + 3) I + m^2 E E^T on the face, the chain of transposed face
  elevations and scatters to the volume, one per index j at the opposite
  vertex, scaled by l_j = (-1)^j binom(m, j) / (j + 1)
  (lift_face_entries, lift_coefficients, lift_scatter_indices).

Which vertices of the tetrahedron a face has, and in which order, is read
off dg_matrices.volume_to_face_parametrisation (dg_matrices.face_vertices,
opposite_vertex), and how a reparametrisation moves the corners off
face_to_face_parametrisation; both parametrisations are now functions of the
module, which the methods of dg_generator call.

Tests check, for the degrees up to 7, that the derivative and the elevation
represent the same polynomials, that the gathers are rT and the permutations
M_face^-1 fP of the Bernstein dg_generator, and that the factorised lift is
M^-1 rT^T M_face to 2e-11 relative to its largest entry.

AI-generated. Model: Claude Opus 5.5
A face shared by two cells gets a frame of its own: the vertex with the
smallest global ID at (0, 0), the next one at (1, 0), the largest at (0, 1).
A cell's face has one of six orientations against it (FACE_ORIENTATIONS):
the ranks of the vertices the cell has at (0, 0), (1, 0), (0, 1) of its own
parametrisation. canonical_to_face_parametrisation maps the canonical frame
into the cell's one, and

- dg_generator.canonical_rT(face, ranks) is the trace in the face basis of
  the canonical frame, the same coefficients from both cells of the face, so
  that each trace is computed once and the jump is formed without a
  reparametrisation; canonical_rDivM is M^-1 canonical_rT^T, as rDivM;
- bernstein_matrices.trace and lift take the orientation as well: in the
  Bernstein basis the canonical trace is still a gather, and only the
  scatters of the lift change, since L_0 and the elevations commute with the
  permutations of a face.

For a cell whose local vertices are ordered by global ID
(sorted_vertex_ranks), faces 1 and 3 are in the canonical frame already and
faces 0 and 2 have their two last vertices swapped, the reparametrisation
fP(0).

The fault reads the field the same way: with its points in the canonical
frame, V2mTo2Quad reads the canonical trace of either side there, and the
flux of the fault goes back through V2QuadTo2m and the canonical lift. Since
the time derivatives of the predictor have lower degrees and a Bernstein
basis is not hierarchical, V2mTo2Quad of dr_generator and
generate_V2mToFaceQuad of the face quadrature take a degree.

Tests check: two cells with a shared face, numbered at random, give the same
canonical trace of a polynomial defined on both, in every basis and for the
orders 1, 3 and 5 (ignoring the orientation instead gives differences of
about 0.1); the sorted numbering gives rT for faces 1 and 3 and M_face^-1
fP(0) rT for faces 0 and 2; for all six orientations the Bernstein gathers
are canonical_rT and the factorised lift is M^-1 canonical_rT^T M_face;
V3mTo2n(a, b) is V2mTo2Quad P_b rT(a) and V3mTo2nTWDivM(a, b) is
rDivM(a) P_b^T M_face V2QuadTo2m in every basis, so that the fault on the
trace is the fault as before; with the fault's points in the canonical
frame, V2mTo2Quad canonical_rT(f, ranks) is the volume basis at those
points in the cell, and canonical_rDivM M_face V2QuadTo2m takes the flux
back as V3mTo2nTWDivM does, for every face and orientation; and the
matrices of a lower degree are the leading columns in the hierarchical
bases.

AI-generated. Model: Claude Opus 5.5
generate.py takes the basis (--basis, one of basis_functions.FAMILIES) and an
output directory (--output-dir; by default the working directory for the
Dubiner basis and a directory named after any other family), and writes
basis.json there with what the matrices of the family may be relied on for
(basis_functions.describe): whether the basis is hierarchical, orthogonal and
orthonormal, the structure of its mass matrices (diagonal for the Dubiner
basis, the identity for the normalised one, dense for the Bernstein basis),
and the order of its functions. A build can choose the memory layouts of
M2/M3 and the other matrices from it instead of assuming the Dubiner basis.

The modes vtk, ader, dr and hom write the files they wrote before: for the
Dubiner basis the 2145 tensors of ader, ader with a material order, dr, vtk
and hom of orders 2 to 8 are bit-identical and in the same order. New modes
write what another basis needs on top:

- basis: the integrals of the basis functions (basisIntegrals,
  faceBasisIntegrals; dg_generator.basis_integrals), which give the mean of
  a cell where the first coefficient alone does not, and the changes to and
  from the Dubiner basis (dg_matrices.basis_change), for checkpoints and for
  comparing runs;
- faces: the orientations against the canonical frame of a face, the
  canonical traces and lifts for every face and orientation, and the face
  basis of every degree at the points of the symmetric face rule;
- degrees: the basis of every degree at the plasticity and material points
  (ip, sq, sq3) and the projection back
  (PlasticityGenerator.generate_Vandermonde(_inv) with a degree), for the
  time derivatives of the predictor, which in a basis that is not
  hierarchical are not the leading entries of the full one;
- bernstein: the sparse operators of the Bernstein basis, with the lift as
  liftFace and the scaled scatters liftScatter(face,orientation,level).

dr also writes the face basis of every lower degree at the points of the
fault for a basis that is not hierarchical. json_io.write_tensors writes all
tensors of a file at once. A mode that cannot run with its arguments (hom
without a material order, bernstein for another basis) stops with a usage
error before anything is written.

Tests check that such a mode writes nothing, that the projection of every
degree reproduces its basis and is
the leading part of the full one in the hierarchical bases, the basis
integrals of the three families, that the basis changes are inverse to each
other and represent the same polynomials, the description of the families,
and, for orders 2 to 4, that the files of the new modes hold what they say:
the lift assembled from liftFace, the face elevations and liftScatter is the
lift of bernstein_matrices, and the canonical traces and orientations are
the ones of dg_matrices.

AI-generated. Model: Claude Opus 5.5
tools/basis_benchmark generates the blocks of an ADER-DG time step of
SeisSol with yateto, in the Dubiner and in the Bernstein basis, checks them
against numpy and times them per cell, outside of SeisSol (blocks.py):

- ck and taylor: the time derivatives of the predictor, each of its own
  degree, and from them the time integral and the value at the end of the
  step (the strong form without a volume kernel);
- trace, lift and nodal: the traces of the time integral in the canonical
  frame of each face, and per face the jump of both cells' traces, the flux
  and its lift, with the flux on the face basis or at the points of the face
  quadrature;
- dr-volume and dr-face: dynamic rupture as SeisSol reads the fault today,
  through V3mTo2n per time point, and through the traces of the time
  derivatives, evaluated at the fault's points once per face.

The Bernstein basis applies the operators of bernstein_matrices: the
derivative as a difference stencil with entries +-1 (the degree moves into
the Taylor coefficients), sums of derivatives of different degree by
Horner's scheme with the elevation, the trace as a gather and the lift
factorised. The operators of a basis are kept dense (PSpaMM), as CSC, or
written into the generated code (yateto's IMMEDIATE addressing); matrices
that evaluate at points stay dense in every variant. The data holds one
simulation or fused simulations in its leading dimension, and the kernels
are built for AVX2 (dhsw) and AVX-512 (dskx), for the orientations of the
faces of cells with vertices sorted by global ID or for all six. An
operator written into the code has a layout over its whole shape: a GEMM
widens its rows to the vector width as soon as an operand's stride is
aligned, which a bounding box that starts at an unaligned row can have as
well, and the widened rows have to lie inside the layout.

Before a build is timed, its driver runs every kernel once on known input,
and check.py compares the result with what numpy computes from the dense
matrices of seissol_matrices (canonical_rT and M^-1 rT^T for traces and
lifts, kDivMT or the Bernstein derivative and the elevation for the
predictor, the basis at the fault's points for dynamic rupture); a build
that differs by more than 1e-9, relative to the largest expected entry
where that exceeds one, is not timed. benchmark.py writes
the time per cell (per fault face for dynamic rupture) of each block, with
a working set inside the caches and one beyond the last level cache, and
yateto's non-zero and hardware flops, to results.csv and summary.md.

AI-generated. Model: Claude Opus 5.5
rDivM(side, matorder) integrates the cell basis, the face basis and the
material basis over a face. The material basis, a field of the cell, was
evaluated without the parametrisation of the face, which puts every point
of the face at the vertex (0, 0, 0): the lift took the material there for
the whole face. A constant material (order 1) gives the same matrix either
way; for a material of order 2 at order 3, the entries differed from the
integrals over the face by as much as the entries themselves.

The material basis is now read on the face like the cell basis; of the
files generate.py writes for orders 2 to 8, the 28 tensors rDivM of ader
with a material order change, and all others stay bit-identical. Tests
compare rDivM with a material of order 3 with the integrals by a quadrature
of their own, and check that a material that vanishes on a face (the
barycentric coordinate of the vertex opposite to it) gives nothing through
that face but does through another one; both tests fail without the change.

AI-generated. Model: Claude Opus 5.5
@davschneller
davschneller merged commit 7608ce4 into main Oct 21, 2026
1 check failed
@davschneller
davschneller deleted the davschneller/bernstein branch October 21, 2026 04:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant