ADR-0008: groundinsight bridge — BusType export from a rho-f fit¶
| Status | Accepted — amended 2026.11 (review pass 10: safe formula parser, input impedance, passivity, the effective resistivity as the rho of layered soil, with weight presets per electrode class); see the amendment at the end of this document |
| Date | 2026-05-08 |
| Deciders | Project maintainers |
| Scope | groundfield.io.groundinsight; bridge to the sister project groundinsight |
Context¶
groundfield is the field-grade reference. groundinsight is the
reduced equivalent network model that runs the actual planning and
fault-current studies. The reduction pipeline
requires a precise, regression-tested handover between
groundfield and groundinsight. The rho-f fit is now in place
in two flavours:
groundfield.postprocess.vector_fitting— a Gustavsen/Semlyen rational fit \(Z(s)\) in the Laplace variable \(s = j\,2\pi f\) at fixed soil (output of one parametric run).groundfield.postprocess.rho_f_standard— the physically-motivated 5-coefficient form \(Z(\rho, f) = k_1\rho + (k_2 + j k_3)f + (k_4 + j k_5)f\rho\) fitted across a soil-resistivity sweep.
Both produce a SymPy expression. groundinsight.BusType consumes
exactly such an expression as its impedance_formula. What is
missing is the actual transport: a documented file format, a
Python helper that builds a BusType and (optionally) writes it
to the groundinsight SQLite store, and a regression test that
proves the formula round-trips through groundinsight's own
parser and produces the same numbers.
Decision¶
Symbol convention¶
groundinsight.BusType.impedance_formula is parsed with two free
symbols that the compute_impedance evaluator binds at run time:
f— frequency in Hzrho—Bus.specific_earth_resistance, in \(\Omega\,\mathrm{m}\) (for layered soil the effective resistivity, see item 5 of the amendment)
and the imaginary unit j (mapped to sympy.I by the validator).
Therefore:
- A
RhoFStandardFitexports natively: its expression is already in(rho, f). - A
VectorFitResultexports via the substitution \(s \to j\,2\pi f\) insidefit_to_sympy(...). The exported formula then depends onfonly and is independent ofrho. This is correct: a vector fit is bound to one specific soil configuration, and itsBusTypeis meant to be paired with aBuswhosespecific_earth_resistancematches that soil. Discrepancies are flagged at export time but not enforced.
Transport: JSON + Python API, both first-class¶
The user has two equally supported paths:
-
to_bustype_dict(fit, ...)returns a JSON-ready Pythondict;save_bustype_json(fit, path, ...)writes the neutral schema below to disk. This is the canonical, persistent exchange. The schema is versioned (schema_version), andgroundfieldwill honour older versions through dedicated loaders. The JSON file is independent of thegroundinsightPython package — anyone can produce or consume it. -
to_bustype(fit, ...)returns a livegroundinsight.BusTypePydantic instance via a lazy import (no top-level dependency ongroundinsight). For users running in a notebook with both packages installed this is the one-liner that closes the family pipeline. Ifgroundinsightis not installed the function raises a clearImportErrorpointing at the JSON path or at the optional installpip install groundfield[groundinsight].
JSON schema (v1)¶
Single document, parseable in seconds, no MIME-type tricks. Layout
is intentionally close to the groundinsight.BusType Pydantic
schema so a programme that only knows the groundinsight shape
can still read the file by ignoring the metadata block.
{
"schema": "groundfield.bustype",
"schema_version": 1,
"name": "TN_house_connection_5EFH",
"description": "...",
"system_type": "LV",
"voltage_level": 0.4,
"impedance_formula": "0.123*rho + 0.000456*f + 0.0*j*f + 1.2e-7*f*rho + 3.4e-8*j*f*rho",
"samples": {
"frequency_Hz": [0.1, 1.0, 10.0, 50.0, 100.0, 1000.0],
"rho_Ohm_m": [100.0, 100.0, 100.0, 100.0, 100.0, 100.0],
"Z_real_Ohm": [...],
"Z_imag_Ohm": [...]
},
"metadata": {
"fit_method": "rho_f_standard",
"fit_quality": {"rms_error_Ohm": 0.012, "rms_relative": 0.004},
"coefficients": {"k1": 0.123, "k2": 4.56e-4, "k3": 0.0,
"k4": 1.2e-7, "k5": 3.4e-8},
"source": "groundfield 0.1.0",
"created_at_utc": "2026-05-08T14:32:11Z",
"electrode_name": "trafo_ring",
"soil_summary": "TwoLayerSoil(rho_1 sweep [50, 100, 300, 1000], rho_2=20, h_1=2.0)",
"rho_definition": "effective_resistivity" // amendment 2026.11, item 5
}
}
A number that is not finite (an unknown rms_error_Ohm of a
hand-built fit, a non-finite sample) is written as null; the file is
strict JSON (allow_nan=False, amendment 2026.11, finding 10.19).
For vector_fit the metadata block carries n_poles, the pole
list and the residue list; coefficients is replaced by
{"R_inf": ..., "L_inf": ...}. For rho_f_standard the
samples.rho_Ohm_m array spans all sampled rhos; for
vector_fit it is constant at the soil under which the fit was
done.
Coupling¶
groundinsight becomes an optional dependency declared as the
extras group [groundinsight] in pyproject.toml. The library
imports groundinsight lazily inside the few functions that
return BusType instances or talk to the database, so
pip install groundfieldworks as before;pip install groundfield[groundinsight]enables the Python-API path;- the JSON path needs neither extra.
Validation programme¶
- Symbol round-trip — every formula produced by
to_bustype_*is fed back throughgroundinsight.utils.validations.validate_impedance_formula_valueand throughgroundinsight.utils.impedance_calculator.compute_impedance, and the resulting per-frequency complex values must matchfit.evaluate(...)to \(10^{-9}\) relative. - JSON round-trip —
save_bustype_jsonfollowed byload_bustype_jsonreproduces every numerical field bit-exact and the formula string verbatim. - End-to-end — a transformer-station notebook
(the internal groundinsight-export harness) sweeps
\(\rho_1 \in \{50, 100, 300, 1000\}\,\Omega\,\mathrm{m}\) on a
2-layer soil, fits the standard form, exports a
BusType, loads it back into agroundinsight.Network, and reproduces the per-frequency \(|Z|\) and \(\arg Z\) within the fit RMS. - Optional-dependency hygiene — a test that monkey-patches
groundinsightto be unimportable and verifies theto_bustype/save_bustype_to_dbfunctions raise a clearImportError, whileto_bustype_dict/save_bustype_jsonkeep working.
Consequences¶
- The
rho-ffamily is now closed-loop:groundfieldproduces a fit, exports it, andgroundinsightconsumes it without any manual stringification. - The neutral JSON makes both packages forward-compatible: tabular
ingestion on the
groundinsightside will read thesamplesblock; the formula stays the canonical artifact in v1. BranchTypeis out of scope for this ADR. The mutual coupling between two grounding clusters is not yet arho-fproduct ofgroundfield; it is a follow-up oncecompute_mutual_impedanceproduces a calibrated formula in the same(f, rho, l)symbol set thatBranchTypeexpects.
References¶
- ADR-0001, ADR-0007 — engine family and cross-layer support that produced the underlying physics.
- Vector fitting: Gustavsen & Semlyen, IEEE T-PWRD 14(3), 1052–1061 (1999).
- Standard
rho-fform: defined ingroundfield.postprocess.rho_f_standard. groundinsight.models.core_models.BusType— consumer of the exported formula.
Amendment 2026.11 — formulas are parsed, exports are checked (review pass 10)¶
Findings 10.01, 10.19, 10.27, 10.40, 10.73 and 10.84.
- No code execution. A BusType formula is data.
evaluate_spec,BusTypeSpec.from_dictandload_bustype_jsonparse it with an allow-list parser (groundfield.io._formula):ast.parse(which executes nothing), then the SymPy expression is built node by node from numbers,f,rho,j/I,pi,E,+ - * /,**with a bounded literal exponent andsqrt/exp/log, within size, depth, magnitude and work budgets.sympy.sympify— which evaluates its input as Python — is no longer called on formula text (10.01). A rejected formula raisesEvaluateSpecErrornaming the element. The same corpus of accepted and rejected formulas (tests/data/formula_corpus.json) is shared with groundinsight's own guard. - Which impedance is exported. The rho-f exporters fit the input
impedance of the feeding source by default (
quantity="input"); the BusType metadata records the fittedquantity, all coefficientsk0…k5,weightsandfit_quality.max_relative(10.19, 10.27). The standard form of this ADR gains the constantk0. - Readable formulas. Coefficients are rounded once to
decimalssignificant digits;nsimplify(rationals with up to 254 digits) and the merging of partial fractions are gone (10.73). - Passivity. Every export evaluates the formula it writes from 0 Hz
to twice the highest fitted frequency (over the fitted ρ range for the
standard form) with the shared
check_passivity;Re Z < 0warns (NonPassiveFitWarning) or, withrequire_passive=True, raisesNonPassiveFitErrorbefore anything is written (10.40).metadata["passivity"]records the verdict. rho_at_fitand therhoof layered soil. A vector fit belongs to one soil; its export requiresrho_at_fit, and the error message says what to pass (10.84). For layered soil therhoof every BusType —rho_at_fitof a vector fit, therhosamples a standard form is fitted over — is the effective resistivity of the soil it was fitted in, \(\rho_\text{eff} = \rho_1^{\,1-w}\rho_2^{\,w}\) with \(w = w_0 (h_1/10\ \mathrm{m})^{-\beta}\) limited to \([0, 1]\) (groundfield.effective_resistivity; weights \((w_0, \beta)\) of the electrode class, by default the AP1 calibration \(w_0 = 0.13\), \(\beta = 0.8\); \(\rho\) itself for homogeneous soil). This is the value the AP2 network model gives aBusasspecific_earth_resistance(the author's decision for the 2026.11 release), so a BusType pairs with the bus of its soil. It replaces the upper-layer resistivity \(\rho_1\) of an earlier draft of this amendment, which describes a soil only together with a fixed lower layer and thickness. Every document records the convention asmetadata["rho_definition"] = "effective_resistivity"; documents of 0.15.0 have no such entry. The exporter cannot check the number it is given. \(\rho_\text{eff}\) is an empirical calibration on the LV networks of the AP1 catalogue (\(h_1\) = 5 … 30 m, \(\rho_1\) = 30 … 1000 Ω·m, \(\rho_2\) = 30 … 200 Ω·m); check the fit quality (max_relative) of a BusType fitted over it.
Weight presets (user decision Q36). The weights depend on the extent
of the earthing system: a compact electrode in the upper layer sees
less of the lower layer than an LV network. effective_resistivity(soil,
preset=...) takes \((w_0, \beta)\) from the read-only mapping
groundfield.soil.EFFECTIVE_RESISTIVITY_PRESETS: "lv_network"
(0.13, 0.8, the default and the global calibration), "station"
(0.06, 1.3, the station electrode alone) and "lv_group_<g>_p<pp>"
for the four LV network groups (perimeter equivalent \(n_U\),
thresholds 20 / 100 / 250) and the penetrations 5, 10, 20, 40 % of
foundation electrodes. The values are those of the author's AP2
hand-over (generated 2026-10-20 from the AP1 catalogue computed with
groundfield 0.14.0; per group the median network, q50); they belong
to that catalogue and are re-derived when it is recomputed. An
explicit w0 or beta replaces that weight only, so every call made
before the presets keeps its value. A BusType and its bus must use the
same weights. rho_definition stays the token
"effective_resistivity"; the preset is recorded next to it (item 7). For the AP1 station in the 39 AP1 soils,
\(R = c\,\rho_\text{eff}\) (\(c\) from homogeneous soil) misses the field
solution by at most 15.7 % with "station" (median over the 39
soils 1.8 %; the hand-over: 1.7 %, 15.7 %) against 35.9 % with
"lv_network" and 41.9 % over \(\rho_1\).
6. Validation programme. The round trip through groundinsight is
checked at \(10^{-9}\), as this ADR specified (the test had used
\(10^{-3}\)), and evaluate_spec against groundinsight's
compute_impedance at \(10^{-9}\).
7. The preset in the document (user decision Q39). The exporters take
rho_preset=..., a key of EFFECTIVE_RESISTIVITY_PRESETS (any other
value raises, naming the closest presets), and record it as
metadata["rho_preset"] next to rho_definition; None when no preset
is named (homogeneous soil, explicit weights, or not stated).
BusTypeSpec.rho_preset reads it; documents of earlier versions have no
entry and load with None, a malformed entry is refused on load. The
number rho_at_fit is still not checked against the preset. The paths
that serve a bus know its preset: evaluate_spec(spec, f, rho,
rho_preset=...) and bustype_from_spec(spec, rho_preset=...) (the live
groundinsight.BusType of a loaded document) emit
RhoPresetMismatchWarning when the document records another preset; a
preset holds for its electrode class only (the AP1 station with
"lv_group_4_p40": 39.9 % rms). The live BusType carries no metadata,
so the check stays on the groundfield side. Round trip through
groundinsight 0.3.0 (PyPI) at \(10^{-9}\). Since audit W3B-06 the live
BusType of to_bustype, save_bustype_to_db and bustype_from_spec
names the preset in the last line of its description, groundfield
rho_preset: <name> (before, it was validated and dropped, so a BusType
read back from the database could not be checked); groundinsight's
BusType and its database keep the description (bus_types.description
of groundinsight 0.3.0), and bustype_rho_preset reads the line back
(database round trip tested). On load
the preset must be a name of EFFECTIVE_RESISTIVITY_PRESETS or null
(audit W3R2B-06), and only the exact preset line is removed or replaced
in a description.