Skip to content

Validation (cross-engine)

compare_engines(world, engines={...}) solves the same world with two or more engines and reports, in one tolerance table, how far their results are apart. Engines that differ in method (image series vs point-matching MoM vs Sommerfeld quadrature), in kernel or only in the discretisation make it the standard check that a number does not depend on a numerical choice (ADR-0001: two engines side by side, validating each other). The validation of the engines against closed-form references and against each other is collected on the validation page.

What is compared

Every compared quantity is complex and is compared at every frequency, real and imaginary part separately:

  • the cluster impedance of every cluster (Ω),
  • the input impedance of every current or voltage source (Ω; a voltage source since 2026.11, the impedance seen by the source without its internal impedance),
  • optionally the potential at sample_points (V).

Each (quantity, frequency, part) is one ComparisonRow with the deviation

\[ \mathrm{dev} = \frac{\max_k c_k - \min_k c_k}{\max\bigl(\min_k \lvert c_k\rvert,\ \epsilon\,\min_k \lvert Z_k\rvert\bigr)}, \]

the worst pairwise spread of the part \(c_k\) over the engines, relative to the smallest value; the floor \(\epsilon\) (component_floor, default 10⁻³ of the smallest magnitude) keeps a part that is nearly zero for every engine (the reactance at 50 Hz) from dominating. A row passes when \(\mathrm{dev} \le\) rel_tolerance. A value that is non-finite or zero for some engines only fails its row.

is_consistent is True only if at least one quantity was compared, every row passed and no engine returned a stub result: a comparison in which nothing could be evaluated (no source, a source on an unknown node) is reported as inconsistent rather than as a silent pass. The engines must use the same frequency list.

Example

import groundfield as gf

world = gf.create_world(soil=gf.TwoLayerSoil(rho_1=100.0, rho_2=500.0, h_1=2.0))
gf.create_electrode(world, "ring", name="g1", center=(0.0, 0.0, 0.8), radius=5.0)
gf.create_source(world, attached_to="g1", magnitude=1.0)

report = gf.compare_engines(
    world,
    engines={
        "image_2layer": gf.create_engine(segment_length=0.5),
        "mom": gf.create_engine(backend="mom", segment_length=0.5),
        "refined": gf.create_engine(segment_length=0.25),
    },
    rel_tolerance=0.02,
)
print(report.summary())

assert report.is_consistent
assert len(report.rows) == 4    # Z_cluster[g1] and Z_in[source_0], Re and Im, at 50 Hz

A single engine raises ValueError: one engine cannot validate itself. The deprecated names cim and bem run image and mom and add no independent information.

API reference

validation

Cross-engine comparison for self-validation.

This module provides compare_engines() — a small convenience helper that runs the same World through several Engine configurations and checks the consistency of the results. It implements ADR-0001 (docs/adr/0001-two-layer-method.md): two engines side by side, validating each other.

Usage

import groundfield as gf world = gf.create_world(soil=gf.HomogeneousSoil(resistivity=100.0)) gf.create_electrode(world, "rod", name="g1", ... position=(0, 0, 0.0), length=1.5) gf.create_source(world, attached_to="g1", magnitude=1.0) report = gf.compare_engines( ... world, ... engines={ ... "image": gf.create_engine(backend="image", segment_length=0.05), ... "mom": gf.create_engine(backend="mom", segment_length=0.05), ... }, ... rel_tolerance=0.05, ... ) report.is_consistent True

At least two engines are required — a single-entry mapping raises ValueError, because one engine cannot validate itself.

What is compared (review pass 10, 10.71)

Every compared quantity is complex and is compared at every frequency, real and imaginary part separately:

Each (quantity, frequency, component) is one row of the report's tolerance table (EngineComparison.rows). Up to 0.15.0 only the real part of the cluster impedance at the first frequency was compared, so an earth-return model that doubled the reactance at 1 kHz passed, engines with different frequency lists were compared at different frequencies, and a non-finite result was silently dropped.

ComparisonRow dataclass

ComparisonRow(quantity: str, frequency: float, component: str, values: dict[str, float], reference: float, deviation: float, tolerance: float, passed: bool)

One row of the tolerance table of EngineComparison.

Attributes:

Name Type Description
quantity str

"Z_cluster[<cluster root>]" (Ω), "Z_in[<source>]" (Ω) or "phi[<sample point index>]" (V; the worst sample point).

frequency float

Frequency in Hz.

component str

"Re" or "Im".

values dict[str, float]

engine_label -> value of the component (Ω or V).

reference float

Denominator of the relative deviation (same unit as values): max(min_k |c_k|, component_floor · min_k |Z_k|).

deviation float

Pairwise spread (max c - min c) / reference; inf when an engine returned a non-finite value while another did not.

tolerance float

The relative tolerance the row was checked against.

passed bool

deviation <= tolerance.

EngineComparison dataclass

EngineComparison(results: dict[str, 'FieldResult'], rel_tolerance: float, cluster_impedance_table: dict[str, dict[str, float]] = dict(), deviations: dict[str, float] = dict(), is_consistent: bool = False, notes: list[str] = list(), frequencies: list[float] = list(), rows: list[ComparisonRow] = list(), impedances: dict[str, dict[str, list[complex]]] = dict(), input_deviations: dict[str, float] = dict())

Outcome of a cross-engine comparison.

Attributes:

Name Type Description
results dict[str, 'FieldResult']

Mapping engine_label -> FieldResult.

rel_tolerance float

Relative tolerance the results were checked against.

cluster_impedance_table dict[str, dict[str, float]]

{cluster_root: {engine_label: Re Z(f0)}} with f0 the first frequency — a compact summary kept for compatibility; the comparison itself covers rows.

deviations dict[str, float]

{cluster_root: worst deviation} of the cluster impedance over all frequencies and both components. Clusters whose impedance is undefined for every engine (purely passive observers) do not appear here.

is_consistent bool

True if and only if at least one quantity was actually evaluated, every row passed (no deviation above rel_tolerance, no non-finite value next to a finite one), and no engine returned a stub result. A comparison in which nothing could be evaluated is reported as not consistent — see compare_engines().

notes list[str]

Diagnostic strings (stub results, skipped or non-finite quantities, the sample-point summary).

frequencies list[float]

The common frequency list in Hz.

rows list[ComparisonRow]

The explicit tolerance table: one ComparisonRow per compared quantity, frequency and component (real / imaginary part).

impedances dict[str, dict[str, list[complex]]]

{"Z_cluster[<root>]" | "Z_in[<source>]": {engine_label: [Z(f) per frequency]}} — the complex values the rows were built from, in Ω.

input_deviations dict[str, float]

{source_name: worst deviation} of the input impedance, same convention as deviations.

Notes

The fields of 0.15.0 keep their positional order; the fields added in review pass 10 (10.71) follow them.

summary

summary() -> str

Return a line-oriented textual summary with the tolerance table.

Source code in src/groundfield/validation.py
def summary(self) -> str:
    """Return a line-oriented textual summary with the tolerance table."""
    labels = list(self.results.keys())
    lines = [
        f"Cross-engine comparison, tolerance {self.rel_tolerance * 100:.1f} %",
        f"Engines: {', '.join(labels)}",
        f"Frequencies: {', '.join(f'{f:g} Hz' for f in self.frequencies)}",
        "",
        "Quantity             f [Hz]  part  "
        + "  ".join(f"{lbl:>14s}" for lbl in labels)
        + "   dev [%]  ok",
    ]
    for row in self.rows:
        vals = "  ".join(
            f"{row.values.get(lbl, float('nan')):14.6g}" for lbl in labels
        )
        lines.append(
            f"{row.quantity:20s} {row.frequency:7g}  {row.component:4s}  "
            f"{vals}  {row.deviation * 100:8.3f}  "
            f"{'yes' if row.passed else 'NO'}"
        )
    lines.append("")
    lines.append(
        "Consistent." if self.is_consistent else "INCONSISTENT — see notes."
    )
    if self.notes:
        lines.append("")
        lines.append("Notes:")
        lines.extend(f"  - {n}" for n in self.notes)
    return "\n".join(lines)

compare_engines

compare_engines(world: 'World', engines: dict[str, 'Engine'], *, rel_tolerance: float = 0.05, sample_points: ndarray | None = None, component_floor: float = DEFAULT_COMPONENT_FLOOR) -> EngineComparison

Run world through every engine and compare the results.

Parameters:

Name Type Description Default
world 'World'

World to evaluate. Not modified by this function.

required
engines dict[str, 'Engine']

Mapping label -> Engine. Must contain at least two entries, all with the same frequency list.

required
rel_tolerance float

Maximum allowed pairwise relative deviation (default 5 %) of every row of the tolerance table, i.e. per compared quantity, frequency and component (real / imaginary part):

\[ \frac{\max_k c_k - \min_k c_k} {\max(\min_k |c_k|,\; \eta\, \min_k |Z_k|)} \;\le\; \texttt{rel\_tolerance} \]

over the engines \(k\), where \(c_k\) is the real or imaginary part of the complex value \(Z_k\) and \(\eta\) is component_floor. Two engines differing by 5 % of the smaller value sit exactly on the threshold.

0.05
sample_points ndarray | None

Optional. Array of shape (M, 3) in m: additional field points whose complex potentials are compared at every frequency (same metric, per point; the worst point is reported).

None
component_floor float

Dimensionless floor \(\eta\) of the component reference, as a fraction of the smallest compared magnitude (default 1e-3). A real or imaginary part that is smaller than \(\eta |Z|\) for every engine is compared on that absolute scale instead of relative to itself, so a reactance that is numerically zero (galvanic worlds) or negligible against the resistance does not turn round-off into a large relative deviation.

DEFAULT_COMPONENT_FLOOR

Returns:

Type Description
EngineComparison

Structured report with the explicit tolerance table (EngineComparison.rows).

Raises:

Type Description
ValueError

If fewer than two engines are given, or if the engines (or their results) do not share one frequency list.

Notes

What is compared (changed in review pass 10, 10.71). The cluster impedance of every cluster and the input impedance of every current source, as complex numbers at every frequency, real and imaginary part separately; optionally the complex potential at sample_points. Up to 0.15.0 only \(\mathrm{Re}\,Z\) of the clusters at the first frequency was compared, so a reactance that differed by a factor two at 1 kHz passed. A quantity that is undefined for every engine (a purely passive cluster with \(\sum I = 0\)) is skipped with a note; a quantity that is non-finite for some engines only is a failure (up to 0.15.0 the non-finite engine was dropped and the rest compared); so is a value that is exactly zero for some engines only (audit round 1, F03). Engines with different frequency lists raise instead of being compared at different frequencies.

Metric definition (changed in 0.15.0). The deviation is the full spread of the compared values normalised by the smallest magnitude among them, which is the worst pairwise relative deviation max_ij |c_i - c_j| / min |c| for same-sign values. Up to 0.14.x the deviation was measured against the ensemble mean, which for two engines reports half the true disagreement.

Aliases (review pass 10, 10.76). Engines that ran the same computation — the same backend after alias resolution (metadata['dispatched_to'] / ['effective_backend']) with identical settings, e.g. the deprecated cim next to image_2layer — are named in a note: their agreement is not an independent cross-check. The verdict itself is unchanged.

Vacuous comparisons (changed in 0.15.0). A report is is_consistent=True only if at least one row was evaluated. A world without sources (or with a misspelled Source.attached_to) has no defined impedance anywhere; such a run yields is_consistent=False plus a "comparison vacuous" note.

Source code in src/groundfield/validation.py
def compare_engines(
    world: "World",
    engines: dict[str, "Engine"],
    *,
    rel_tolerance: float = 0.05,
    sample_points: np.ndarray | None = None,
    component_floor: float = DEFAULT_COMPONENT_FLOOR,
) -> EngineComparison:
    """Run ``world`` through every engine and compare the results.

    Parameters
    ----------
    world
        World to evaluate. **Not modified** by this function.
    engines
        Mapping ``label -> Engine``. Must contain at least two entries,
        all with the same frequency list.
    rel_tolerance
        Maximum allowed **pairwise** relative deviation (default 5 %)
        of every row of the tolerance table, i.e. per compared
        quantity, frequency and component (real / imaginary part):

        .. math::

            \\frac{\\max_k c_k - \\min_k c_k}
                  {\\max(\\min_k |c_k|,\\;
                         \\eta\\, \\min_k |Z_k|)}
            \\;\\le\\; \\texttt{rel\\_tolerance}

        over the engines :math:`k`, where :math:`c_k` is the real or
        imaginary part of the complex value :math:`Z_k` and
        :math:`\\eta` is ``component_floor``. Two engines differing by
        5 % of the smaller value sit exactly on the threshold.
    sample_points
        Optional. Array of shape ``(M, 3)`` in m: additional field
        points whose complex potentials are compared at every frequency
        (same metric, per point; the worst point is reported).
    component_floor
        Dimensionless floor :math:`\\eta` of the component reference,
        as a fraction of the smallest compared magnitude (default
        1e-3). A real or imaginary part that is smaller than
        :math:`\\eta |Z|` for every engine is compared on that absolute
        scale instead of relative to itself, so a reactance that is
        numerically zero (galvanic worlds) or negligible against the
        resistance does not turn round-off into a large relative
        deviation.

    Returns
    -------
    EngineComparison
        Structured report with the explicit tolerance table
        (:attr:`EngineComparison.rows`).

    Raises
    ------
    ValueError
        If fewer than two engines are given, or if the engines (or
        their results) do not share one frequency list.

    Notes
    -----
    **What is compared (changed in review pass 10, 10.71).** The
    cluster impedance of every cluster and the input impedance of every
    current source, as complex numbers at **every** frequency, real and
    imaginary part separately; optionally the complex potential at
    ``sample_points``. Up to 0.15.0 only :math:`\\mathrm{Re}\\,Z` of the
    clusters at the first frequency was compared, so a reactance that
    differed by a factor two at 1 kHz passed. A quantity that is
    undefined for *every* engine (a purely passive cluster with
    :math:`\\sum I = 0`) is skipped with a note; a quantity that is
    non-finite for *some* engines only is a failure (up to 0.15.0 the
    non-finite engine was dropped and the rest compared); so is a value
    that is exactly zero for some engines only (audit round 1, F03).
    Engines with
    different frequency lists raise instead of being compared at
    different frequencies.

    **Metric definition (changed in 0.15.0).** The deviation is the
    full spread of the compared values normalised by the *smallest*
    magnitude among them, which is the worst pairwise relative deviation
    ``max_ij |c_i - c_j| / min |c|`` for same-sign values. Up to 0.14.x
    the deviation was measured against the ensemble *mean*, which for
    two engines reports half the true disagreement.

    **Aliases (review pass 10, 10.76).** Engines that ran the same
    computation — the same backend after alias resolution
    (``metadata['dispatched_to']`` / ``['effective_backend']``) with
    identical settings, e.g. the deprecated ``cim`` next to
    ``image_2layer`` — are named in a note: their agreement is not an
    independent cross-check. The verdict itself is unchanged.

    **Vacuous comparisons (changed in 0.15.0).** A report is
    ``is_consistent=True`` only if at least one row was evaluated. A
    world without sources (or with a misspelled ``Source.attached_to``)
    has no defined impedance anywhere; such a run yields
    ``is_consistent=False`` plus a "comparison vacuous" note.
    """
    if len(engines) < 2:
        raise ValueError("compare_engines requires at least 2 engines.")

    freq_lists = {
        label: [float(f) for f in getattr(eng, "frequencies", [])]
        for label, eng in engines.items()
    }
    if len({tuple(f) for f in freq_lists.values()}) > 1:
        detail = "; ".join(f"{lbl}: {f}" for lbl, f in freq_lists.items())
        raise ValueError(
            "compare_engines needs one frequency list for all engines, got "
            f"{detail}. Compare at common frequencies (Engine.frequencies)."
        )

    results: dict[str, FieldResult] = {
        label: engine.solve(world) for label, engine in engines.items()
    }
    freqs = [float(f) for f in next(iter(results.values())).frequencies]
    for label, res in results.items():
        if [float(f) for f in res.frequencies] != freqs:
            raise ValueError(
                f"Engine '{label}' returned the frequencies {res.frequencies}, "
                f"not {freqs}."
            )

    cmp = EngineComparison(
        results=results, rel_tolerance=rel_tolerance, frequencies=freqs
    )

    # Review pass 10, 10.76: a deprecated alias (``cim``, ``bem``,
    # ``image_nlayer``) runs another backend's computation; agreement with
    # that backend at the same settings checks nothing.
    groups: dict[tuple[str, str], list[str]] = {}
    for label, res in results.items():
        groups.setdefault(_computation_key(engines[label], res), []).append(label)
    for (ran, _settings), labels in groups.items():
        if len(labels) > 1:
            names = ", ".join(f"'{lbl}'" for lbl in labels)
            cmp.notes.append(
                f"Engines {names} ran the same computation (backend '{ran}' "
                "with identical settings); their agreement is not an "
                "independent cross-check."
            )

    # Detect stub backends
    for label, res in results.items():
        if res.metadata.get("stub"):
            cmp.notes.append(
                f"Engine '{label}' returned a stub result "
                f"(metadata['stub']=True). Comparison not meaningful."
            )

    # Cluster sets must be identical
    cluster_keys_first = {
        tuple(sorted(v)) for v in next(iter(results.values())).clusters.values()
    }
    for label, res in results.items():
        keys = {tuple(sorted(v)) for v in res.clusters.values()}
        if keys != cluster_keys_first:
            cmp.notes.append(
                f"Engine '{label}' reports a different cluster structure. "
                "Cluster comparison skipped."
            )
            cmp.is_consistent = False
            return cmp

    # Quantities: cluster impedances (one representative per cluster)
    # and the input impedance of every current or voltage source (a
    # voltage source since 2026.11, audit W2A-02).
    roots = sorted(
        {sorted(m)[0] for res in results.values() for m in res.clusters.values()}
    )
    quantities: list[tuple[str, str, dict[str, list[complex] | None]]] = []
    for root in roots:
        quantities.append(
            (
                f"Z_cluster[{root}]",
                root,
                {
                    lbl: _safe(res.cluster_impedance, root)
                    for lbl, res in results.items()
                },
            )
        )
    source_names = [
        s.name
        for s in next(iter(results.values())).sources
        if getattr(s, "kind", None) in ("current", "voltage")
    ]
    for name in source_names:
        quantities.append(
            (
                f"Z_in[{name}]",
                name,
                {lbl: _safe(res.input_impedance, name) for lbl, res in results.items()},
            )
        )

    n_compared = 0
    for quantity, key, per_engine in quantities:
        n_freq = len(freqs)
        values = {
            lbl: (
                v if v is not None and len(v) == n_freq else [complex("nan")] * n_freq
            )
            for lbl, v in per_engine.items()
        }
        cmp.impedances[quantity] = values
        worst = None
        for k, f in enumerate(freqs):
            rows = _component_rows(
                quantity,
                f,
                {lbl: v[k] for lbl, v in values.items()},
                rel_tolerance=rel_tolerance,
                component_floor=component_floor,
            )
            if rows is None:
                continue
            cmp.rows.extend(rows)
            n_compared += len(rows)
            dev = max(r.deviation for r in rows)
            worst = dev if worst is None else max(worst, dev)
            if not np.isfinite(dev):
                bad = sorted(lbl for lbl, v in values.items() if not np.isfinite(v[k]))
                if bad:
                    cmp.notes.append(
                        f"{quantity} at {f:g} Hz: non-finite for engine(s) "
                        f"{', '.join(bad)} while finite for the others — counted "
                        "as a failure."
                    )
                else:
                    zero = sorted(lbl for lbl, v in values.items() if v[k] == 0)
                    cmp.notes.append(
                        f"{quantity} at {f:g} Hz: zero for engine(s) "
                        f"{', '.join(zero)} while non-zero for the others — "
                        "counted as a failure."
                    )
        if worst is None:
            cmp.notes.append(
                f"{quantity}: undefined for every engine (Σ I = 0 or Z = 0) — skipped."
            )
            continue
        if quantity.startswith("Z_cluster["):
            cmp.deviations[key] = worst
            re0 = {
                lbl: float(v[0].real)
                for lbl, v in values.items()
                if np.isfinite(v[0].real)
            }
            if re0:
                cmp.cluster_impedance_table[key] = re0
        else:
            cmp.input_deviations[key] = worst

    # Optional point-sample for the potential, complex, every frequency.
    if sample_points is not None:
        try:
            pts = np.asarray(sample_points, dtype=float)
            n_pts = len(pts)
            sample_max = 0.0
            n_undefined = 0
            for k, f in enumerate(freqs):
                phis = np.stack(
                    [
                        np.asarray(res.potential(pts, frequency_index=k), dtype=complex)
                        for res in results.values()
                    ]
                )
                scale = np.min(np.abs(phis), axis=0)
                usable = np.isfinite(phis).all(axis=0) & (scale > 0.0)
                n_undefined = max(n_undefined, int(np.count_nonzero(~usable)))
                if not usable.any():
                    continue
                for name, part in _COMPONENTS:
                    c = part(phis)[:, usable]
                    ref = np.maximum(
                        np.min(np.abs(c), axis=0), component_floor * scale[usable]
                    )
                    dev = (c.max(axis=0) - c.min(axis=0)) / ref
                    j = int(np.argmax(dev))
                    point = int(np.nonzero(usable)[0][j])
                    row = ComparisonRow(
                        quantity=f"phi[{point}]",
                        frequency=f,
                        component=name,
                        values={
                            lbl: float(v)
                            for lbl, v in zip(results, c[:, j], strict=True)
                        },
                        reference=float(ref[j]),
                        deviation=float(dev[j]),
                        tolerance=rel_tolerance,
                        passed=bool(dev[j] <= rel_tolerance),
                    )
                    cmp.rows.append(row)
                    n_compared += int(np.count_nonzero(usable))
                    sample_max = max(sample_max, row.deviation)
            note = (
                f"Potential point-sample at {n_pts} points and {len(freqs)} "
                f"frequencies: max relative deviation = {sample_max * 100:.2f} % "
                "(max pairwise, referenced to min |phi|, Re and Im)."
            )
            if n_undefined:
                note += (
                    f" {n_undefined} point(s) excluded: phi = 0 or non-finite "
                    "for at least one engine, relative deviation undefined."
                )
            cmp.notes.append(note)
        except (RuntimeError, NotImplementedError) as e:
            cmp.notes.append(f"Potential sample not evaluated: {e}")

    failed = [r for r in cmp.rows if not r.passed]
    if failed:
        worst_row = max(failed, key=lambda r: r.deviation)
        cmp.notes.append(
            f"{len(failed)} of {len(cmp.rows)} rows exceed the tolerance; worst: "
            f"{worst_row.quantity} {worst_row.component} at "
            f"{worst_row.frequency:g} Hz, deviation "
            f"{worst_row.deviation * 100:.3g} % > {rel_tolerance * 100:.3g} %."
        )

    # A comparison that evaluated *nothing* has validated nothing.
    if n_compared == 0:
        cmp.notes.append(
            "No comparable quantity (every impedance undefined, no usable "
            "sample point) — comparison vacuous, nothing was validated."
        )
    cmp.is_consistent = (
        n_compared > 0
        and all(r.passed for r in cmp.rows)
        and not any(n.startswith("Engine '") and "stub result" in n for n in cmp.notes)
    )
    return cmp