Skip to content

Characterising a location without its electrode

What does the network do at a given station, independently of the electrode installed there?

Adding an electrode is a rank-one change to the nodal matrix, so every nodal voltage is a Möbius function of its admittance. Two solves with the electrode removed therefore determine the response for every electrode, exactly and at no further cost — measured against genuine solves from 0.05 Ω to 500 Ω and at a complex value, the closed form agrees to 3e-15 relative.

The two extremes — no electrode and an ideal one — are the endpoints of that curve, and both are exact limits rather than numerical stand-ins. Z_network, the driving-point impedance with the local electrode removed, is the site-independent number the whole analysis is built around.

See Characterising a location without its electrode.

response

Characterise a location in the network independently of the electrode there.

The idea this module implements: to describe what the network does at a given station, remove the station's own electrode and ask what is left. Whatever electrode is eventually installed there only ever appears as one shunt admittance, so the network's contribution can be stated once and the electrode varied afterwards -- including to its two extremes, an ideal electrode and none at all.

That intuition turns out to be exact rather than approximate. Adding a shunt Y_B at bus b is a rank-one change to the nodal matrix, so by the Sherman-Morrison identity every nodal voltage is a Möbius function of it:

.. math::

\underline{u}(Y_B) = \underline{u}_0
    - \frac{Y_B\, \underline{z}\, u_{0,b}}{1 + Y_B Z_\text{net}}

with three site-independent objects, all obtained without knowing the electrode:

u_0 the nodal voltages of the fault solve with the electrode removed -- the open-circuit response of the location; z the column Y_0^{-1} e_b, i.e. the voltage everywhere per ampere injected at b, source-free; Z_net = z_b the driving-point impedance of everything except the local electrode -- the parallel impedance the network offers at that point.

Two solves therefore determine the response for every electrode, exactly and at no further cost. Measured against real solves at Z_B from 0.05 Ω to 500 Ω and at a complex value, the closed form agrees to 2e-15 relative.

The extremes

Z_B → ∞ (no electrode) and Z_B → 0 (ideal electrode) are the endpoints of that curve and both are exact limits, not numerical stand-ins -- the ideal electrode in particular, which the solver itself rejects because a zero impedance is not a passive value it can invert.

The driving-point impedance is exactly Z_dp(Y_B) = 1/(Y_B + 1/Z_net). Whether its magnitude falls as the electrode improves depends on the electrode's phase. Along Y_B = t e^{j\varphi} with t growing, d|Y_B + 1/Z_net|^2/dt = 2t + 2\,Re(e^{j\varphi}\,\overline{1/Z_net}), so |Z_dp| falls monotonically from |Z_net| to zero if and only if cos(\varphi - arg(1/Z_net)) >= 0. A resistive electrode (real Y_B >= 0) always qualifies, because Re(1/Z_net) >= 0 in a passive network, and so does one in phase with 1/Z_net. A reactive electrode of the opposite sign to the network's susceptance does not: it first raises |Z_dp| above |Z_net|. The largest magnitude over all passive electrodes is reached at Y_B = -j\,Im(1/Z_net), which cancels the network's susceptance and gives |Z_dp| = 1/Re(1/Z_net) = |Z_net|/cos(arg Z_net). That is 0.08 % above |Z_net| where the network seen from the bus is nearly resistive (arg Z_net = 2.2°) but 57 % at the far end of a four-bus cable chain (arg Z_net = 50°), so it is reported next to the two endpoints rather than assumed to be negligible.

The EPR-based reduction factor

ResultReductionFactor.value is |u_F|/|u_F^{\,\text{no mutual}}| at the fault bus. When the bus being varied is the fault bus, the closed form makes it constant along the whole curve: the voltage there is u_b(Y_B) = u_{0,b} / (1 + Y_B Z_\text{net}) with and without mutual coupling alike, so the denominator cancels out of the quotient exactly. That is the same invariance the sensitivity study runs into, here derived rather than measured.

Away from the fault bus it does not cancel: the fault-bus voltage is u_F(Y_B) = u_{0,F} - Y_B z_F u_{0,b} / (1 + Y_B Z_\text{net}), a difference whose two terms scale differently, and the quotient moves with the electrode (1.147 / 1.056 / 0.973 over Z_B = 0.1 / 10 / 1000 Ω on the four-bus chain of the review). It is therefore evaluated per electrode, from a third right-hand side of the same factorisation -- the mutual-free injection vector -- rather than carried through as an invariant.

The three cases are extremes of the local quantities only -- the potential rise at the bus itself and its driving-point impedance. Quantities transferred to other buses are not bounded by them: the EPR at a remote bus and the current-based reduction factor can both exceed the ideal value for a passive, purely capacitive Z_B, because the phase of Y_B enters those through a different combination than through 1 + Y_B Z_net alone. Use :meth:BusResponse.sweep -- which costs no solve -- when the worst case of a transfer quantity is what a study needs.

BusResponse

Bases: BaseModel

Closed-form response of the network to the electrode at one bus.

Built by :func:bus_response. Evaluating it costs no solve.

Attributes:

Name Type Description
fault str

Fault the response was built for.

bus str

Bus whose electrode is the free parameter.

fault_bus str

Bus the fault sits on. Used to anchor which group of buses is named as feeding the soil when the earth-return current is split.

frequencies list of float

Frequencies covered.

bus_names list of str

The active buses of the network, in the order of the nodal system; the keys of the voltage dictionaries.

z_network dict of float to complex

Driving-point impedance at bus with the local electrode removed -- the parallel impedance the rest of the network offers at that location. Independent of anything installed at bus.

u_open dict of float to dict of str to complex

Nodal voltages with the local electrode removed.

u_open_no_mutual dict of float to dict of str to complex

The same, for the injection vector without the mutual Norton equivalents -- the third right-hand side of the same factorisation. It is what makes the EPR-based reduction factor evaluable per electrode instead of assumed invariant. Empty when the solve carried no mutual-free vector, in which case :meth:evaluate falls back to :attr:r_epr.

other_impedance dict of float to dict of str to complex

Per frequency, the electrode impedance of every bus that carries one, the response bus included; what splits the earth-return current between the electrodes.

z_column dict of float to dict of str to complex

Voltage at every bus per ampere injected at bus, source-free.

r_epr dict of float to float or None

The EPR-based reduction factor of the solved fault, i.e. for the electrode that was installed at bus when :func:bus_response ran. It is constant along the curve only for bus == fault_bus; elsewhere use the r_epr column of :meth:evaluate, which is evaluated for the electrode in hand.

i_fault dict of float to complex

Fault current, the negated sum of source injections. Constant.

z_fault_open dict of float to complex

Driving-point impedance of the earthing network at the fault bus with the electrode at bus removed (Thevenin loop closures excluded) -- Z0_FF.

z_fault_transfer dict of float to complex

Transfer impedance between bus and the fault bus in that network -- Z0_Fb.

z_bus_open dict of float to complex

Driving-point impedance at bus in that network -- Z0_bb; equal to :attr:z_network when the network has no voltage source.

z_fault_ideal dict of float to complex

Where the electrode at bus is the only one of the fault side -- no other electrode is connected to the fault bus through shields -- the three open-network values above do not exist (that network floats). All of the fault current then leaves through the electrode at bus, and Z_E = Z_B + z_fault_ideal with z_fault_ideal the driving point at the fault bus for an ideal electrode at bus. Present only at such frequencies.

Examples:

>>> import groundinsight as gi
>>> from groundinsight.api.examples import build_minimal
>>> net = build_minimal()
>>> _ = gi.run_fault(net, "fault")
>>> response = gi.bus_response(net, fault="fault")
>>> response.bus, round(abs(response.z_network[50.0]), 4)
('bus3', 1.0965)
>>> frame = response.sweep([0.5, 1.0, 2.0])
>>> [round(v, 2) for v in frame["EPR_V"]]
[11.73, 18.03, 24.28]

admittance

admittance(z_bus: ElectrodeSpec) -> complex

Translate an electrode spelling into a shunt admittance.

None and an infinite impedance both mean no electrode (Y_B = 0); a zero impedance means an ideal one and is returned as inf, which :meth:voltages handles as a limit rather than a division.

Parameters:

Name Type Description Default
z_bus (complex, float or None)

The electrode impedance in ohms; None for no electrode.

required

Returns:

Type Description
complex

The shunt admittance Y_B = 1 / z_bus in siemens: 0 without an electrode, inf for an ideal one.

Notes

The electrode is a value, not a frequency-dependent model: the frequency dependence lives in :attr:z_network and in whatever the caller passes per frequency. The method therefore takes no frequency: a parameter it never read would promise an influence it does not have.

Source code in src/groundinsight/analysis/response.py
def admittance(self, z_bus: ElectrodeSpec) -> complex:
    """
    Translate an electrode spelling into a shunt admittance.

    ``None`` and an infinite impedance both mean *no electrode*
    (``Y_B = 0``); a zero impedance means an *ideal* one and is returned as
    ``inf``, which :meth:`voltages` handles as a limit rather than a
    division.

    Parameters
    ----------
    z_bus : complex, float or None
        The electrode impedance in ohms; ``None`` for no electrode.

    Returns
    -------
    complex
        The shunt admittance ``Y_B = 1 / z_bus`` in siemens: ``0`` without
        an electrode, ``inf`` for an ideal one.

    Notes
    -----
    The electrode is a value, not a frequency-dependent model: the
    frequency dependence lives in :attr:`z_network` and in whatever the
    caller passes per frequency. The method therefore takes no frequency:
    a parameter it never read would promise an influence it does not
    have.
    """
    if z_bus is None:
        return 0.0 + 0.0j
    z = complex(z_bus)
    if np.isinf(z.real) or np.isinf(z.imag):
        return 0.0 + 0.0j
    if z == 0:
        return complex(np.inf, 0.0)
    return 1.0 / z

driving_point

driving_point(
    z_bus: ElectrodeSpec, frequency: float
) -> complex

Z_dp = 1 / (Y_B + 1/Z_net) -- the impedance seen at bus.

Parameters:

Name Type Description Default
z_bus (complex, float or None)

The electrode at bus in Ohm: None or an infinite value for no electrode, 0 for an ideal one (see :meth:admittance).

required
frequency float

One of the frequencies of the response, in Hz. The former keyword freq is accepted as a deprecated alias.

required

Returns:

Type Description
complex

The driving-point impedance in Ohm.

Source code in src/groundinsight/analysis/response.py
@renamed_parameters(freq="frequency")
def driving_point(self, z_bus: ElectrodeSpec, frequency: float) -> complex:
    """
    ``Z_dp = 1 / (Y_B + 1/Z_net)`` -- the impedance seen at ``bus``.

    Parameters
    ----------
    z_bus : complex, float or None
        The electrode at ``bus`` in Ohm: ``None`` or an infinite value
        for no electrode, ``0`` for an ideal one (see :meth:`admittance`).
    frequency : float
        One of the frequencies of the response, in Hz. The former
        keyword ``freq`` is accepted as a deprecated alias.

    Returns
    -------
    complex
        The driving-point impedance in Ohm.
    """
    y = self.admittance(z_bus)
    if np.isinf(y.real):
        return 0.0 + 0.0j
    z_net = self.z_network[frequency]
    if z_net == 0:
        return 0.0 + 0.0j
    total = y + 1.0 / z_net
    if total == 0:
        return complex(np.inf, 0.0)
    return 1.0 / total

evaluate

evaluate(
    z_bus: ElectrodeSpec, *, label: Optional[str] = None
) -> pl.DataFrame

Everything the location does for one electrode, as one frame.

Parameters:

Name Type Description Default
z_bus (complex, float or None)

The electrode. None or an infinite value means none installed, 0 means ideal. Both are evaluated as exact limits.

required
label str

Value of the case column. Defaults to a readable rendering of z_bus.

None

Returns:

Type Description
DataFrame

One row per frequency: case, bus, fault, frequency_Hz, Z_bus_Ohm, Z_network_Ohm, Z_driving_point_Ohm, EPR_V, EPR_deg, I_electrode_A, r_epr, r_current, Z_G_Ohm, plus EPR_<bus>_V for every other bus.

Notes

r_epr is evaluated for this electrode at the fault bus (see the module docstring); it is constant across electrodes only when bus is the fault bus. Z_G_Ohm is the EN 50522 earthing impedance |Z_E|: the driving-point impedance of the earthing network at the fault bus for this electrode (:meth:fault_driving_point), the same definition :func:~groundinsight.network_operations.run_fault reports. For bus == fault_bus it equals Z_driving_point_Ohm when the network has no voltage source.

Source code in src/groundinsight/analysis/response.py
def evaluate(
    self, z_bus: ElectrodeSpec, *, label: Optional[str] = None
) -> pl.DataFrame:
    """
    Everything the location does for one electrode, as one frame.

    Parameters
    ----------
    z_bus : complex, float or None
        The electrode. ``None`` or an infinite value means *none installed*,
        ``0`` means *ideal*. Both are evaluated as exact limits.
    label : str, optional
        Value of the ``case`` column. Defaults to a readable rendering of
        ``z_bus``.

    Returns
    -------
    pl.DataFrame
        One row per frequency: ``case``, ``bus``, ``fault``,
        ``frequency_Hz``, ``Z_bus_Ohm``, ``Z_network_Ohm``,
        ``Z_driving_point_Ohm``, ``EPR_V``, ``EPR_deg``, ``I_electrode_A``,
        ``r_epr``, ``r_current``, ``Z_G_Ohm``, plus ``EPR_<bus>_V`` for
        every other bus.

    Notes
    -----
    ``r_epr`` is evaluated **for this electrode** at the fault bus (see the
    module docstring); it is constant across electrodes only when ``bus`` is
    the fault bus. ``Z_G_Ohm`` is the EN 50522 earthing impedance
    ``|Z_E|``: the driving-point impedance of the earthing network at the
    **fault** bus for this electrode (:meth:`fault_driving_point`), the same
    definition :func:`~groundinsight.network_operations.run_fault` reports.
    For ``bus == fault_bus`` it equals
    ``Z_driving_point_Ohm`` when the network has no voltage source.
    """
    return _frame(self._rows(z_bus, label, self.frequencies))

extremes

extremes() -> pl.DataFrame

The bracket: no electrode, ideal electrode, and the passive worst case.

Three rows per frequency, labelled "open", "ideal" and "worst_passive". The first two are the endpoints of the curve; the third is the reactive electrode that maximises the driving-point magnitude (see :meth:worst_case_electrode).

Returns:

Type Description
DataFrame

The rows of :meth:evaluate for the three cases, stacked.

Source code in src/groundinsight/analysis/response.py
def extremes(self) -> pl.DataFrame:
    """
    The bracket: no electrode, ideal electrode, and the passive worst case.

    Three rows per frequency, labelled ``"open"``, ``"ideal"`` and
    ``"worst_passive"``. The first two are the endpoints of the curve; the
    third is the reactive electrode that maximises the driving-point
    magnitude (see :meth:`worst_case_electrode`).

    Returns
    -------
    pl.DataFrame
        The rows of :meth:`evaluate` for the three cases, stacked.
    """
    frames = [
        self.evaluate(None, label="open"),
        self.evaluate(0.0, label="ideal"),
    ]
    worst_rows: List[Dict[str, object]] = []
    for freq in self.frequencies:
        worst = self.worst_case_electrode(freq)
        worst_rows.extend(self._rows(worst["z_bus"], "worst_passive", [freq]))
    frames.append(_frame(worst_rows))
    return pl.concat(frames, how="diagonal")

fault_driving_point

fault_driving_point(
    z_bus: ElectrodeSpec, frequency: float
) -> Optional[complex]

Z_E, the driving-point impedance at the fault bus, for one electrode.

The electrode sits at bus; the impedance is the one seen at the fault bus.

The shunt Y_B at bus is a rank-one change of the earthing network, so the fault-bus driving point is Möbius in it as well:

Z_FF(Y_B) = Z0_FF - Y_B * Z0_Fb**2 / (1 + Y_B * Z0_bb)

with both endpoints as exact limits. It is the quantity run_fault reports as ResultReductionFactor.z_earthing and as the grounding impedance; for bus == fault_bus it reduces to :meth:driving_point of a network without voltage source.

Where the electrode at bus is the only one of the fault side (:attr:z_fault_ideal), every ampere leaves through it and Z_FF = Z_B + z_fault_ideal: finite for every electrode, undefined without one.

Parameters:

Name Type Description Default
z_bus (complex, float or None)

The electrode at bus, as for :meth:evaluate.

required
frequency float

Frequency in Hz.

required

Returns:

Type Description
complex or None

None where Z_E does not exist: the fault side reaches remote earth only through the loop closure of a voltage source -- whatever the electrode at bus, or because z_bus is None and the electrode at bus was its only path.

Source code in src/groundinsight/analysis/response.py
def fault_driving_point(
    self, z_bus: ElectrodeSpec, frequency: float
) -> Optional[complex]:
    """
    ``Z_E``, the driving-point impedance at the fault bus, for one electrode.

    The electrode sits at ``bus``; the impedance is the one seen at the
    **fault** bus.

    The shunt ``Y_B`` at ``bus`` is a rank-one change of the earthing
    network, so the fault-bus driving point is Möbius in it as well:

    ``Z_FF(Y_B) = Z0_FF - Y_B * Z0_Fb**2 / (1 + Y_B * Z0_bb)``

    with both endpoints as exact limits. It is the quantity
    ``run_fault`` reports as ``ResultReductionFactor.z_earthing`` and as
    the grounding impedance; for ``bus == fault_bus`` it
    reduces to :meth:`driving_point` of a network without voltage source.

    Where the electrode at ``bus`` is the only one of the fault side
    (:attr:`z_fault_ideal`), every ampere leaves through it and
    ``Z_FF = Z_B + z_fault_ideal``: finite for every electrode, undefined
    without one.

    Parameters
    ----------
    z_bus : complex, float or None
        The electrode at ``bus``, as for :meth:`evaluate`.
    frequency : float
        Frequency in Hz.

    Returns
    -------
    complex or None
        ``None`` where ``Z_E`` does not exist: the fault side reaches
        remote earth only through the loop closure of a voltage source --
        whatever the electrode at ``bus``, or because ``z_bus`` is ``None``
        and the electrode at ``bus`` was its only path.
    """
    z_ff = self.z_fault_open.get(frequency)
    z_fb = self.z_fault_transfer.get(frequency)
    z_bb = self.z_bus_open.get(frequency)
    if z_ff is None or z_fb is None or z_bb is None:
        ideal = self.z_fault_ideal.get(frequency)
        if ideal is None:
            return None
        y = self.admittance(z_bus)
        if y == 0:
            return None
        if np.isinf(y.real):
            return ideal
        return ideal + 1.0 / y
    y = self.admittance(z_bus)
    if np.isinf(y.real):
        if z_bb == 0:
            return z_ff
        return z_ff - z_fb * z_fb / z_bb
    if y == 0:
        return z_ff
    denominator = 1.0 + y * z_bb
    if denominator == 0:
        return None
    return z_ff - y * z_fb * z_fb / denominator

sweep

sweep(
    z_values: Sequence[ElectrodeSpec],
    *,
    labels: Optional[Sequence[str]] = None,
) -> pl.DataFrame

Evaluate many electrodes at once. No solve, whatever the length.

Parameters:

Name Type Description Default
z_values sequence

Electrodes, in the spellings :meth:evaluate accepts.

required
labels sequence of str

One label per value. Defaults to a rendering of each value.

None

Returns:

Type Description
DataFrame

The rows of :meth:evaluate for every electrode, stacked in the order of z_values.

Raises:

Type Description
ValueError

If z_values is empty, or if labels has a different length. An empty sweep would otherwise reach pl.concat([]) and come back as a polars error about an empty iterable, which names neither the argument nor the caller's mistake.

Source code in src/groundinsight/analysis/response.py
def sweep(
    self,
    z_values: Sequence[ElectrodeSpec],
    *,
    labels: Optional[Sequence[str]] = None,
) -> pl.DataFrame:
    """
    Evaluate many electrodes at once. No solve, whatever the length.

    Parameters
    ----------
    z_values : sequence
        Electrodes, in the spellings :meth:`evaluate` accepts.
    labels : sequence of str, optional
        One label per value. Defaults to a rendering of each value.

    Returns
    -------
    pl.DataFrame
        The rows of :meth:`evaluate` for every electrode, stacked in the
        order of ``z_values``.

    Raises
    ------
    ValueError
        If ``z_values`` is empty, or if ``labels`` has a different length.
        An empty sweep would otherwise reach ``pl.concat([])`` and come
        back as a polars error about an empty iterable, which names
        neither the argument nor the caller's mistake.
    """
    if not len(z_values):
        raise ValueError(
            "sweep() needs at least one electrode; an empty z_values has "
            "no frame to build. Pass the electrodes to evaluate, e.g. "
            "sweep([1.0, 10.0, None])."
        )
    if labels is not None and len(labels) != len(z_values):
        raise ValueError(
            f"{len(z_values)} electrode(s) were given but {len(labels)} "
            f"label(s); they have to correspond one to one."
        )
    frames = [
        self.evaluate(z, label=None if labels is None else labels[i])
        for i, z in enumerate(z_values)
    ]
    return pl.concat(frames, how="diagonal")

voltages

voltages(
    z_bus: ElectrodeSpec, frequency: float
) -> Dict[str, complex]

Nodal voltages for one electrode at one frequency.

Both endpoints are evaluated as limits, so an ideal electrode gives exactly zero at bus instead of a very small number.

Parameters:

Name Type Description Default
z_bus (complex, float or None)

The electrode at bus in Ohm: None or an infinite value for no electrode, 0 for an ideal one (see :meth:admittance).

required
frequency float

One of the frequencies of the response, in Hz. The former keyword freq is accepted as a deprecated alias.

required

Returns:

Type Description
dict of str to complex

The EPR phasor per bus.

Source code in src/groundinsight/analysis/response.py
@renamed_parameters(freq="frequency")
def voltages(
    self, z_bus: ElectrodeSpec, frequency: float
) -> Dict[str, complex]:
    """
    Nodal voltages for one electrode at one frequency.

    Both endpoints are evaluated as limits, so an ideal electrode gives
    exactly zero at ``bus`` instead of a very small number.

    Parameters
    ----------
    z_bus : complex, float or None
        The electrode at ``bus`` in Ohm: ``None`` or an infinite value
        for no electrode, ``0`` for an ideal one (see :meth:`admittance`).
    frequency : float
        One of the frequencies of the response, in Hz. The former
        keyword ``freq`` is accepted as a deprecated alias.

    Returns
    -------
    dict of str to complex
        The EPR phasor per bus.
    """
    return self._with_electrode(self.u_open[frequency], z_bus, frequency)

voltages_no_mutual

voltages_no_mutual(
    z_bus: ElectrodeSpec, frequency: float
) -> Optional[Dict[str, complex]]

The same, for the injection vector without the mutual Norton sources.

This is the denominator of the EPR-based reduction factor. None when the response was built without that right-hand side.

Parameters:

Name Type Description Default
z_bus (complex, float or None)

The electrode at bus in Ohm: None or an infinite value for no electrode, 0 for an ideal one (see :meth:admittance).

required
frequency float

One of the frequencies of the response, in Hz. The former keyword freq is accepted as a deprecated alias.

required

Returns:

Type Description
dict of str to complex or None

The EPR phasor per bus without mutual coupling.

Source code in src/groundinsight/analysis/response.py
@renamed_parameters(freq="frequency")
def voltages_no_mutual(
    self, z_bus: ElectrodeSpec, frequency: float
) -> Optional[Dict[str, complex]]:
    """
    The same, for the injection vector without the mutual Norton sources.

    This is the denominator of the EPR-based reduction factor. ``None`` when
    the response was built without that right-hand side.

    Parameters
    ----------
    z_bus : complex, float or None
        The electrode at ``bus`` in Ohm: ``None`` or an infinite value
        for no electrode, ``0`` for an ideal one (see :meth:`admittance`).
    frequency : float
        One of the frequencies of the response, in Hz. The former
        keyword ``freq`` is accepted as a deprecated alias.

    Returns
    -------
    dict of str to complex or None
        The EPR phasor per bus without mutual coupling.
    """
    u0 = self.u_open_no_mutual.get(frequency)
    if u0 is None:
        return None
    return self._with_electrode(u0, z_bus, frequency)

worst_case_electrode

worst_case_electrode(
    frequency: float,
) -> Dict[str, complex]

The passive electrode that maximises |Z_dp|, and the value it gives.

Over the closed right half-plane of Y_B the magnitude of 1/(Y_B + Y_net) is largest where the imaginary parts cancel and the real part is as small as it can be, i.e. at Y_B = -j*Im(Y_net). The result exceeds |Z_net| only slightly in a cable network, but it is the true bound rather than an assumed one.

Parameters:

Name Type Description Default
frequency float

One of the frequencies of the response, in Hz. The former keyword freq is accepted as a deprecated alias.

required

Returns:

Type Description
dict

z_bus (the electrode, purely reactive) and z_driving_point.

Raises:

Type Description
ValueError

If Re(1/Z_net) <= 0. The bound 1/Re(Y_net) is only the maximum while the network's conductance is positive; a non-positive one turns the same expression into a negative or infinite "worst case" that reads like an answer.

Source code in src/groundinsight/analysis/response.py
@renamed_parameters(freq="frequency")
def worst_case_electrode(self, frequency: float) -> Dict[str, complex]:
    """
    The passive electrode that maximises ``|Z_dp|``, and the value it gives.

    Over the closed right half-plane of ``Y_B`` the magnitude of
    ``1/(Y_B + Y_net)`` is largest where the imaginary parts cancel and the
    real part is as small as it can be, i.e. at ``Y_B = -j*Im(Y_net)``. The
    result exceeds ``|Z_net|`` only slightly in a cable network, but it is
    the true bound rather than an assumed one.

    Parameters
    ----------
    frequency : float
        One of the frequencies of the response, in Hz. The former
        keyword ``freq`` is accepted as a deprecated alias.

    Returns
    -------
    dict
        ``z_bus`` (the electrode, purely reactive) and ``z_driving_point``.

    Raises
    ------
    ValueError
        If ``Re(1/Z_net) <= 0``. The bound ``1/Re(Y_net)`` is only the
        maximum while the network's conductance is positive; a
        non-positive one turns the same expression into a negative or
        infinite "worst case" that reads like an answer.
    """
    z_net = self.z_network[frequency]
    if z_net == 0 or not np.isfinite(z_net):
        return {"z_bus": complex(np.inf, 0.0), "z_driving_point": z_net}
    y_net = 1.0 / z_net
    if y_net.real <= 0:
        raise ValueError(
            f"The network at bus '{self.bus}' has a non-positive "
            f"conductance at {frequency} Hz (Re(1/Z_net) = {y_net.real:g}), so "
            f"the passive worst case 1/Re(Y_net) is not a maximum -- it "
            f"would come back negative or infinite. A passive network "
            f"cannot do this: check the grounding impedances of network "
            f"'{self.fault}' behind this bus, and whether the assembled "
            f"system still matches them (see run_fault)."
        )
    y_b = complex(0.0, -y_net.imag)
    if y_b == 0:
        return {"z_bus": complex(np.inf, 0.0), "z_driving_point": z_net}
    return {"z_bus": 1.0 / y_b, "z_driving_point": 1.0 / y_net.real}

bus_response

bus_response(
    network: Network,
    *,
    fault: str,
    bus: Optional[str] = None,
) -> BusResponse

Build the closed-form response of the network to the electrode at one bus.

Requires a solved fault: the assembled nodal system is reused, the bus's own shunt is taken back out of it, and three right-hand sides are solved against the one factorisation -- the fault with the electrode removed, a unit injection at the bus, and the same fault without the mutual Norton equivalents. None of them depends on the electrode, which is why the result covers every electrode.

Parameters:

Name Type Description Default
network Network

A network with network.results[fault] present, i.e. after :func:~groundinsight.network_operations.run_fault.

required
fault str

The solved fault.

required
bus str

Bus whose electrode is the free parameter. Defaults to the fault bus.

None

Returns:

Type Description
BusResponse

The response of the network to the electrode at bus; evaluating it for any electrode costs no further solve.

Raises:

Type Description
ValueError

If the fault has not been solved, if the bus is unknown or inactive, or if the network without that electrode has no path to reference earth -- in which case there is nothing to characterise, because the location's behaviour is its own electrode.

Examples:

The fault bus of the minimal network, with its own electrode as the free parameter -- every electrode is evaluated without another solve:

>>> import groundinsight as gi
>>> from groundinsight.api.examples import build_minimal
>>> net = build_minimal()
>>> _ = gi.run_fault(net, "fault")
>>> response = gi.bus_response(net, fault="fault", bus="bus3")
>>> row = response.evaluate(1.0)
>>> round(row["EPR_V"].item(), 2)
18.03
Source code in src/groundinsight/analysis/response.py
def bus_response(
    network: Network,
    *,
    fault: str,
    bus: Optional[str] = None,
) -> BusResponse:
    """
    Build the closed-form response of the network to the electrode at one bus.

    Requires a solved fault: the assembled nodal system is reused, the bus's own
    shunt is taken back out of it, and three right-hand sides are solved against
    the one factorisation -- the fault with the electrode removed, a unit
    injection at the bus, and the same fault without the mutual Norton
    equivalents. None of them depends on the electrode, which is why the result
    covers every electrode.

    Parameters
    ----------
    network : Network
        A network with ``network.results[fault]`` present, i.e. after
        :func:`~groundinsight.network_operations.run_fault`.
    fault : str
        The solved fault.
    bus : str, optional
        Bus whose electrode is the free parameter. Defaults to the fault bus.

    Returns
    -------
    BusResponse
        The response of the network to the electrode at ``bus``; evaluating
        it for any electrode costs no further solve.

    Raises
    ------
    ValueError
        If the fault has not been solved, if the bus is unknown or inactive, or
        if the network without that electrode has no path to reference earth --
        in which case there is nothing to characterise, because the location's
        behaviour *is* its own electrode.

    Examples
    --------
    The fault bus of the minimal network, with its own electrode as the free
    parameter -- every electrode is evaluated without another solve:

    >>> import groundinsight as gi
    >>> from groundinsight.api.examples import build_minimal
    >>> net = build_minimal()
    >>> _ = gi.run_fault(net, "fault")
    >>> response = gi.bus_response(net, fault="fault", bus="bus3")
    >>> row = response.evaluate(1.0)
    >>> round(row["EPR_V"].item(), 2)
    18.03
    """
    if fault not in network.results:
        raise ValueError(
            f"Fault '{fault}' has no result on network '{network.name}'. The "
            f"response is read off the assembled nodal system, so call "
            f"run_fault first."
        )
    electrical = network.electrical_network
    if electrical is None or not getattr(electrical, "Y_sparse", None):
        raise ValueError(
            f"Network '{network.name}' carries no assembled electrical network. "
            f"Call run_fault before asking for a bus response."
        )
    # The response is read off the assembled matrices, and those are a
    # snapshot: an impedance changed after ``run_fault`` -- by hand, or by a
    # study that scaled rho and put it back -- does not reach them. Reading a
    # stale Y next to the current impedances does not fail loudly; it produced
    # a *negative* driving-point impedance in the review case, because the
    # shunt taken out below was never the shunt that went in. So refuse rather
    # than answer.
    if not electrical.matches_network_state():
        raise ValueError(
            f"The assembled nodal system of network '{network.name}' was built "
            f"from different model values than the ones the network carries "
            f"now (impedances, active flags or the active fault changed since "
            f"the last run_fault). A bus response read off it would mix the two "
            f"states and can come out non-physical -- a negative driving-point "
            f"impedance, for instance. Call run_fault again for fault "
            f"'{fault}' before asking for the response."
        )

    target = bus if bus is not None else network.faults[fault].bus
    if target not in electrical.bus_indices:
        known = sorted(electrical.bus_indices)
        raise ValueError(
            f"Bus '{target}' is not part of the solved system of network "
            f"'{network.name}'. Active buses: {known}."
        )

    index = electrical.bus_indices
    order = sorted(index, key=lambda name: index[name])
    k = index[target]
    frequencies = [float(f) for f in network.frequencies]

    result = network.results[fault]
    reduction = result.reduction_factor
    r_epr = dict(reduction.value) if reduction is not None else {}

    z_network: Dict[float, complex] = {}
    u_open: Dict[float, Dict[str, complex]] = {}
    u_open_no_mutual: Dict[float, Dict[str, complex]] = {}
    z_column: Dict[float, Dict[str, complex]] = {}
    other_impedance: Dict[float, Dict[str, complex]] = {}
    i_fault: Dict[float, complex] = {}
    z_fault_open: Dict[float, complex] = {}
    z_fault_transfer: Dict[float, complex] = {}
    z_bus_open: Dict[float, complex] = {}
    z_fault_ideal: Dict[float, complex] = {}
    fault_bus = network.faults[fault].bus

    for freq in frequencies:
        Y = electrical.Y_sparse.get(freq)
        i_vector = electrical.i_vectors.get(freq)
        if Y is None or i_vector is None:
            continue

        # The resolved grounding impedance of every bus, and the admittance the
        # assembly stamped for it, in one call. The same values answer three
        # questions below -- the shunt to remove, whether any other bus
        # reaches earth, and the electrode currents.
        impedances, admittances = electrical.grounding_shunts(freq)
        electrode = np.isfinite(impedances) & (impedances != 0)

        # Take the bus's own shunt back out of a copy of the sparse matrix.
        # The same rules as the assembly: a missing or infinite impedance
        # never contributed one in the first place. The value subtracted is
        # the one that was added, so an electrode that was the bus's only
        # connection leaves an exact zero behind.
        Y0 = Y.copy()
        if electrode[k]:
            Y0 = _subtract_from_diagonal(Y0, k, admittances[k])

        # The check is structural, not a tolerance: at least one *other* bus of
        # the target's group -- the buses the solved matrix couples it to,
        # through shields or a voltage source's loop closure -- has to carry a
        # finite, non-zero grounding impedance once the target's shunt is
        # gone. Leaving it to the linear solver is not enough -- measured on
        # the exactly singular case, numpy returns a finite but meaningless
        # -2.25e15 instead of raising, the same inconsistency the DC work
        # found in scipy's splu -- and an earthed bus elsewhere in the
        # network does not help a group that is not connected to it.
        others = electrode.copy()
        others[k] = False
        labels = component_labels(Y0)
        if not np.any(others & (labels == labels[k])):
            raise ValueError(
                f"With the electrode at bus '{target}' removed, no other bus of "
                f"network '{network.name}' connected to it has a finite "
                f"grounding impedance at {freq} Hz, so there is no path to "
                f"reference earth. There is nothing to characterise "
                f"independently of that electrode: at this location the "
                f"network's behaviour *is* the electrode. Pick a bus that has "
                f"at least one other earthed bus behind it."
            )

        unit = np.zeros(len(order), dtype=complex)
        unit[k] = 1.0
        # Three right-hand sides, one factorisation: the fault injection, the
        # unit injection at the bus, and -- when the solve carried it -- the
        # same fault injection without the mutual Norton equivalents. The third
        # is what makes ``r_epr`` evaluable per electrode (it is the denominator
        # of the reduction factor). The factorisation is a sparse LU of the
        # assembled matrix: a dense solve on a copy of a dense ``Y`` is cubic in
        # the number of buses -- 5.6 s on a 2000-bus ring at seven frequencies,
        # the dense earthing columns included.
        i_no_mutual = electrical.i_vectors_no_mutual.get(freq)
        rhs = [np.asarray(i_vector, dtype=complex), unit]
        if i_no_mutual is not None:
            rhs.append(np.asarray(i_no_mutual, dtype=complex))
        try:
            factor = splu(Y0)
            solved = factor.solve(np.column_stack(rhs))
            u0 = solved[:, 0]
            z_col = solved[:, 1]
            u0_nm = solved[:, 2] if i_no_mutual is not None else None
        except (np.linalg.LinAlgError, RuntimeError) as exc:
            raise ValueError(
                f"The nodal system of network '{network.name}' is singular at "
                f"{freq} Hz once the electrode at bus '{target}' is removed, "
                f"even though other buses are earthed. Check for a subnetwork "
                f"that reaches earth only through '{target}'."
            ) from exc
        if not (np.all(np.isfinite(u0)) and np.all(np.isfinite(z_col))):
            raise ValueError(
                f"The response of bus '{target}' at {freq} Hz came out "
                f"non-finite. That points at an infinite or NaN impedance "
                f"somewhere in network '{network.name}' rather than at the bus "
                f"itself."
            )

        z_network[freq] = complex(z_col[k])
        u_open[freq] = dict(zip(order, u0.tolist(), strict=True))
        z_column[freq] = dict(zip(order, z_col.tolist(), strict=True))
        if u0_nm is not None:
            u_open_no_mutual[freq] = dict(zip(order, u0_nm.tolist(), strict=True))

        other_impedance[freq] = {
            order[i]: complex(impedances[i]) for i in np.flatnonzero(electrode)
        }

        # The earthing network without the electrode at ``target`` and without
        # the Thevenin loop closures: three numbers make the fault-bus driving
        # point Z_E available for every electrode (fault_driving_point, F4).
        earth_columns = _earthing_columns(
            electrical, Y0, factor, k, index[fault_bus], freq
        )
        if earth_columns is not None:
            kind, values = earth_columns
            if kind == "open":
                z_fault_open[freq], z_fault_transfer[freq], z_bus_open[freq] = values
            else:
                z_fault_ideal[freq] = values

        total_source = electrical.total_source_currents.get(freq)
        if total_source is not None:
            i_fault[freq] = -complex(total_source)

    if not z_network:
        raise ValueError(
            f"No frequency of fault '{fault}' carried an assembled system, so "
            f"there is no response to build."
        )

    return BusResponse(
        fault=fault,
        bus=target,
        fault_bus=network.faults[fault].bus,
        frequencies=[f for f in frequencies if f in z_network],
        bus_names=order,
        z_network=z_network,
        u_open=u_open,
        u_open_no_mutual=u_open_no_mutual,
        z_column=z_column,
        other_impedance=other_impedance,
        r_epr=r_epr,
        i_fault=i_fault,
        z_fault_open=z_fault_open,
        z_fault_transfer=z_fault_transfer,
        z_bus_open=z_bus_open,
        z_fault_ideal=z_fault_ideal,
    )