Skip to content

Add an operator ​

Problem. A scheme needs a linear stencil GIDEON has not got: another window, another derivative, another bias. Every kernel is generated by scripts/gen_gideon.py and never edited by hand (each generated file starts with !< Generated by scripts/gen_gideon.py: do not edit), so a new operator is a change of the generator, followed by the gates every existing kernel passed. The generator describes the pipeline; this recipe is the order of the work.

1. Fix the meaning before the code ​

Write down, before any code: the data (point values or cell averages), the place of the result (a node, a face, the average over a cell), the window by offset in GIDEON's geometry (Conventions), the derivative, and the name gideon_<data>_<op>_<bias><order>. A new bias or operator word is a convention: it goes into Conventions first. The order is not chosen, it is measured from the row.

2. Derive the row and look at it ​

derive(data, place, kind, n, offsets) solves the moment system of the window exactly, in fractions.Fraction, and measures the row's exact degree K on the monomials beyond the window; the order is K+1−n. For example the third-order upwind-biased face value (two cells left of the face, one right), which this release does not have, and its mirror:

python
"""Derive the rows of an operator the release does not have, with the generator's own function."""

import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[3] / 'scripts'))  # the repository's scripts/
from gen_gideon import derive

# the third-order upwind-biased face value, two cells left of the face and one right; fv face: cell o spans [o-1, o]
r = derive('fv', 'face', 'point', 0, [-1, 0, 1])
print({o: str(w) for o, w in r.weights.items()}, 'exact degree', r.exact_degree, 'order', r.order)
# its mirror, one cell left and two right
r = derive('fv', 'face', 'point', 0, [0, 1, 2])
print({o: str(w) for o, w in r.weights.items()}, 'exact degree', r.exact_degree, 'order', r.order)
text
{-1: '-1/6', 0: '5/6', 1: '1/3'} exact degree 2 order 3
{0: '1/3', 1: '5/6', 2: '-1/6'} exact degree 2 order 3

(any Python 3.10+, from any directory; the generator needs no package beyond the standard library; the program and its output are checked by scripts/run_examples.sh). The samplers and targets cover the four geometries of the release (fd node, fd face, fv face, fv cell); a new geometry is a new branch of sampler and target, and of the test data (step 5).

3. Add the family to build() ​

In build(), a loop over the windows of the family that calls derive and appends a Kernel (name, family, data, operator, bias, derivative, width, order, window, the stored row and the full row by offset). Then teach the emitters the family: row_name (the constant of gideon_tables it reads), emit_kernel (the arithmetic: pair form for a centred row, one product per cell for a one-sided one, a flux difference for a cell derivative, the step divided inside), MODULES (the module it goes in), GEOM (its geometry and target for the test registry). Conventions the existing families keep, and the tests assert:

  • a left-side row is never stored: derive it independently, prove it equals the mirror of the right one (times (−1)n) and emit the left kernel as the right row read mirrored;
  • a combination that has no exact row of order ≥ 1 is skipped, never emitted as zeros (ADAM's audit item C);
  • every family gets a run-time order dispatcher, whose window bounds window_expr fits as expressions of order (order/2 + c or order + c) and checks against every kernel: a family whose windows follow no such formula stops the generator.

4. Check, then emit ​

bash
python3 scripts/gen_gideon.py --check --list    # derive, cross-check, measure the orders; write nothing
python3 scripts/gen_gideon.py                   # the same, then (re)write the generated sources

--check runs every gate on the new rows with the old ones: the exactness of each row on its own system; the cross-check against ADAM's committed tables where ADAM has the row (MISMATCH, NOT GENERATED); the order on a generic smooth function at h=2−11,2−12 in 100-digit decimal arithmetic, bound design − 0.25 (ORDER). Any of these exits 1 and writes nothing. The full run then writes gideon_tables.F90, the kernel modules, gideon_dispatch.F90, gideon_operators_3d.F90, src/tests/gideon_test_registry.F90 and docs/guide/_operators-table.md, reads the emitted rows back and compares them with the derived ones. Refresh the documentation tables after it: python3 docs/scripts/make_cookbook_tables.py.

5. The tests cover it through the registry ​

The generated registry gives every kernel an id and its metadata (kgeom, ktarget, kn, korder, klo, khi, kdegree, ksumabs, the exact leading error kerror, kzero_const, kmirror) and calls it through apply, apply_dev, apply_dispatch and apply_dispatch_bad. The tests loop over the registry, so a new kernel is tested by all of them without a new line: exactness and leading error (gideon_test_tables), orders (gideon_test_order), mirrors and symmetries (gideon_test_properties), dispatch and NaN (gideon_test_dispatch), host against device (gideon_test_device). Hand-written code is needed only for a new geometry (gideon_test_functions: the data and targets of monomials and of the smooth function) or a window wider than the registry's WMAX = 16.

bash
fobis build --mode tests-gnu      && bash scripts/run_tests.sh
fobis build --mode tests-gnu-r16  && bash scripts/run_tests.sh --exe-dir exe-r16   # every order observed
python3 scripts/mutation_test.py --mode tests-gnu                                  # do the tests see a defect?

Add the new family's mutants to MUTANTS in scripts/mutation_test.py (a coefficient of a row, the sign of a mirror, the window offset): a mutant that survives is a defect the suite cannot see (Mutation testing).

6. A new golden file, never an edited one ​

gideon_test_golden reads the golden file kernel by kernel in registry order: any new kernel makes the committed file too short or out of order, and the test stops. The rule (src/tests/gideon_test_golden.F90): a golden file is never edited; a deliberate change of results gets a new file and becomes the test's default path. For the next version, v3 (v2 is phase 2's, the FD face kernels):

  1. in gideon_test_golden.F90, set the three GOLDEN paths to gideon_golden_v3.txt, gideon_golden_r4_v3.txt, gideon_golden_r16_v3.txt, and VERSION, the version the write mode prints in the header line;

  2. write each file with the optimised gfortran build of its kind, from the repository root (the write mode refuses to overwrite an existing file):

    bash
    fobis build --mode tests-gnu     && exe/gideon_test_golden write src/tests/golden/gideon_golden_v3.txt
    fobis build --mode tests-gnu-r4  && exe-r4/gideon_test_golden write src/tests/golden/gideon_golden_r4_v3.txt
    fobis build --mode tests-gnu-r16 && exe-r16/gideon_test_golden write src/tests/golden/gideon_golden_r16_v3.txt
  3. diff v3 against v2 on the kernels that existed: they must be identical (the new file adds values, it changes none); the new families are appended in the generator's build(), so the old kernels keep their lines;

  4. record in the test's header comment what v3 changed and why, as the lines of v1 and v2 do.

A change that alters existing values (a different row, a different summation order) follows the same rule, and its reason is the commit's subject.

Reference. The generator, Golden values, Rows by their meaning.

Released under your choice of GPL-3.0-or-later, BSD-2-Clause, BSD-3-Clause or MIT.