Skip to content

Sources

A source drives the network at a node (attached_to: an electrode or a junction) against remote earth (return_to=None) or against a second node (return_to). CurrentSource impresses a current; VoltageSource impresses a voltage between its two terminals, and the solve returns its current.

Mathematical and physical context

Sources are the right-hand side of the nodal system. A one-terminal source injects the phasor \(I = |I|\,e^{j\varphi_I}\) (RMS value, magnitude in A, phase_deg in degrees) at attached_to; Kirchhoff's current law closes the circuit through the soil to remote earth, the potential reference. A two-terminal source injects \(+I\) at attached_to and \(-I\) at return_to, so the loop closes through the soil and the conductor network — the set-up of a fall-of-potential measurement with its auxiliary electrode, or of a fault current that returns to a neighbouring substation. The impedance the source sees is

\[ Z_\text{in} = \frac{\varphi_a - \varphi_r}{I}, \]

FieldResult.input_impedance(), with \(\varphi_r = 0\) for a return through remote earth.

A voltage source drives its terminals with \(U = |U|\,e^{j\varphi_U}\) (magnitude in V, RMS) behind an internal series impedance \(Z_q(f) = R + j\omega L\) (internal_resistance_ohm in Ω, internal_inductance_h in H, both ≥ 0 and 0 by default — the ideal source),

\[ \varphi_a - \varphi_r + Z_q\,I = U , \]

and leaves its current \(I\) to the solve. The engines add it by modified nodal analysis: one more unknown, the source current, in Kirchhoff's current law at both terminals, and one more row with the constraint, \(Z_q\) on its diagonal (see the engine family). \(I\) has the sign convention of a current source — it flows from the source into attached_to — and the result reports it in result.sources[k].current (the impressed voltage in result.sources[k].voltage, \(R\) and \(L\) in result.sources[k].internal_resistance_ohm / internal_inductance_h, all of them in to_dataframe("sources")). input_impedance() is the impedance the source sees at its terminals, \(Z_\text{in} = (\varphi_a - \varphi_r)/I = U/I - Z_q\) — without the source's own \(Z_q\). The problem is linear: for a world with this one source the solution equals the 1 A solve of a current source at the same terminals scaled by \(I = U / (Z_q + Z_\text{in})\), to round-off, in every engine that solves voltage sources (image, image_2layer including electrodes through the layer interface, mom, mom_sommerfeld; inductive conductors and several frequencies included). With several sources the solve is the superposition of all of them — for one voltage source alone the other voltage sources are shorted (0 V behind their \(Z_q\)) and the current sources open — and each input_impedance() is an apparent one: two 230 V sources on two 3 m rods 5 m apart (100 Ω·m) each drive 7.5 % less current than one alone and report an input impedance 8.1 % higher (image). A voltage source of 0 V is a short through \(Z_q\) whose current the solve returns. The reactance \(j\omega L\) is evaluated at every solved frequency (it vanishes at \(f = 0\)). A galvanic world whose voltage source has an inductance is solved once per frequency in the image family (whose system has one row per owner), while mom and mom_sommerfeld factorise their segment system once and solve, per frequency, only the V × V system of the V source currents (exact; 1 488 segments at 20 frequencies: 1.02 times the solve of the same source without inductance, 28 times before audit W2R2-02). For \(R \gg Z_E\) (the impedance the source sees) a current source \(U/R\) is the better model: the potentials of the voltage solve and input_impedance() (\(U/I - Z_q\)) lose about \(\log_{10}(R/Z_E)\) digits, 2.7 · 10⁻⁶ relative at \(R = 10^{12}\) Ω on a 36 Ω rod (image).

World.check rejects, before any numerics run, a voltage source whose terminals are bonded into one cluster by ideal conductors (short circuit, with or without \(Z_q\); with \(Z_q\) its current \(U/Z_q\) would circulate through the bond and the source, none through the soil), negative or non-finite \(R\) / \(L\) and non-zero ones below the smallest normal float (2.2 · 10⁻³⁰⁸, which turned the solve into nan), and voltage sources without internal resistance that form a loop through ideal bonds (conductors at or below 10⁻⁶ Ω) or remote earth — two of them from one cluster to remote earth, or a ring of two-terminal ones: the loop fixes one potential difference twice. One source of the loop with internal_resistance_ohm > 0 opens it; an inductance alone does not, because its impedance vanishes at \(f = 0\). fem, which lacks the registry capability voltage_sources, raises BackendCapabilityError. Up to 0.15.0 every backend dropped voltage sources with a warning and the world solved to zero potentials (finding 10.51).

A backend whose capabilities do not include two-terminal sources returns the current through remote earth and warns with BackendCapabilityWarning (see the backend registry); the measurement physics is explained in sources and measurement.

Example

import groundfield as gf

world = gf.create_world(soil=gf.HomogeneousSoil(rho=100.0))
gf.create_electrode(world, "rod", name="earth", position=(0.0, 0.0, 0.0), length=3.0)
gf.create_electrode(world, "rod", name="aux", position=(100.0, 0.0, 0.0), length=1.0)
gf.create_source(world, attached_to="earth", return_to="aux", magnitude=1.0)

result = world.solve(gf.create_engine(frequencies=[50.0]))
Z_loop = result.input_impedance()[0]               # 126.19 Ω
Z_earth = result.cluster_impedance("earth")[0]      # 35.83 Ω
Z_aux = result.cluster_impedance("aux")[0]          # 90.35 Ω

# +1 A leaves "earth" into the soil, -1 A at "aux" (the current returns)
I = result.electrode_currents
assert abs(I["earth"][0] - 1.0) < 1e-9 and abs(I["aux"][0] + 1.0) < 1e-9
# the loop is the series connection of both earths (mutual coupling included)
assert abs(Z_loop - (Z_earth + Z_aux)) < 1e-9

A voltage source of 230 V behind a 10 Ω series resistance, modelled as before 2026.11 with a junction T and a lumped conductor to the same rod (the resistance is then part of the network, so input_impedance() includes it):

import groundfield as gf

world = gf.create_world(soil=gf.HomogeneousSoil(rho=100.0))
gf.create_electrode(world, "rod", name="earth", position=(0.0, 0.0, 0.0), length=3.0)
gf.create_junction(world, name="T", position=(0.0, 0.0, 0.0))
gf.create_conductor(
    world, name="r_i", start="T", end="earth", cross_section=50e-6,
    lumped_series_resistance_ohm=10.0,
)
gf.create_source(world, attached_to="T", kind="voltage", magnitude=230.0)

result = world.solve(gf.create_engine(frequencies=[50.0]))
I = result.sources[0].current[0]                    # 5.001 A, solved
Z_in = result.input_impedance()[0]                  # 45.99 Ω = 230 V / I
U_E = result.electrode_potentials["earth"][0]       # 179.99 V = 230 V - 10 Ω · I

# the terminal sits at the impressed voltage; the rod alone is 35.99 Ω
assert abs(result.metadata["junction_potentials"]["T"][0] - 230.0) < 1e-9
assert abs(Z_in - (10.0 + 35.9905)) < 1e-3 and abs(U_E - (Z_in - 10.0) * I) < 1e-9

The same source with its internal impedance on the source itself, here \(Z_q = 10\ \Omega + j\omega \cdot 5\ \text{mH}\) at 0, 50 and 1000 Hz (the current is \(U / (Z_q + Z_E)\), the input impedance the rod's \(Z_E\) alone):

import groundfield as gf

world = gf.create_world(soil=gf.HomogeneousSoil(rho=100.0))
gf.create_electrode(world, "rod", name="earth", position=(0.0, 0.0, 0.0), length=3.0)
source = gf.create_source(
    world, attached_to="earth", kind="voltage", magnitude=230.0,
    internal_resistance_ohm=10.0, internal_inductance_h=5e-3,
)

result = world.solve(gf.create_engine(frequencies=[0.0, 50.0, 1000.0]))
I = result.sources[0].current                       # 5.001, 4.998, 4.130 A
Z_E = result.input_impedance()                      # 35.99 Ω at every frequency
U_E = result.electrode_potentials["earth"]          # 179.99, 179.88, 148.62 V

for k, f in enumerate(result.frequencies):
    Z_q = source.internal_impedance(f)              # 10 Ω + j·2π·f·5 mH
    assert abs(I[k] - 230.0 / (Z_q + Z_E[k])) < 1e-12 * abs(I[k])
    assert abs(U_E[k] - (230.0 - Z_q * I[k])) < 1e-9   # the terminal voltage
assert abs(Z_E[1] - 35.9905) < 1e-3 and abs(abs(I[2]) - 4.1295) < 1e-4

Source is a discriminated union over kind ("current", "voltage"); SourceAdapter validates a stand-alone source dictionary, for example from a JSON file. Assignments are validated, so a source edited between two solves (src.magnitude = 20.0) cannot take a non-finite value. A VoltageSource or a SourceRecord pickled before 2026.11 loads with \(R = L = 0\), the ideal source it was, as its JSON does.

API reference

sources

Source models: impressed currents and impressed voltages.

Grounding studies are dominated by current sources: ground-fault currents, test currents in loop measurements, lightning partial currents. A source feeds a node of the network — an electrode or a leakage-free junction (Junction) — and, optionally, takes its current back at a second node. A voltage source impresses a potential difference between the same two terminals instead and the solve returns its current.

One-terminal and two-terminal sources

With return_to=None the impressed current \(I\) enters the soil at attached_to and returns through remote earth (the classical grounding-impedance set-up). With return_to set, the source is a two-terminal source: it injects \(+I\) at attached_to and \(-I\) at return_to, so Kirchhoff's current law closes the loop through the soil and the conductor network (a fall-of-potential measurement with its auxiliary electrode, a fault current returning to a neighbouring substation). The driving-point impedance the source sees is \((\varphi_a - \varphi_r)/I\) (input_impedance()). Every backend whose capabilities declare two_terminal_sources honours return_to; the others warn and return the current through remote earth.

Voltage sources

A VoltageSource is a voltage \(U = |U|\,e^{j\varphi}\) (RMS phasor, magnitude in V) behind an internal series impedance \(Z_q(f) = R + j\omega L\) (internal_resistance_ohm, internal_inductance_h; both 0 by default, the ideal source), between attached_to and return_to, or between attached_to and remote earth (the potential reference) for return_to=None:

\[ \varphi_a - \varphi_r + Z_q\,I = U . \]

Its current \(I\) is an unknown of the solve (modified nodal analysis: one constraint row and one current unknown per voltage source in the nodal system of electrodes, junctions and conductor branches; \(Z_q\) is the diagonal entry of the row) and follows the sign convention of a current source: it flows from the source into attached_to and back out of return_to. The result reports it in current, the impressed voltage in voltage and \(R\), \(L\) in the record's two fields of the same names; input_impedance() is the impedance the source sees at its terminals, \(Z_\text{in} = (\varphi_a - \varphi_r) / I = U / I - Z_q\) (\(Z_q\) excluded). The problem is linear: a world with one source, a voltage source, gives the solution of a 1 A current source at the same terminals scaled by \(I = U / (Z_q + Z_\text{in})\), with conductors, inductive coupling and layered soil (to round-off, tests/test_voltage_sources.py, tests/test_voltage_source_impedance.py).

  • Several voltage and current sources superpose: for one voltage source alone the other voltage sources are shorted (0 V, their \(Z_q\) kept), the current sources open. The solve is then the superposition of all of them, not the scaled 1 A solve of one source, and the input impedance of each is an apparent one: two 230 V sources on two 3 m rods 5 m apart (100 Ω·m) each drive 7.5 % less current than one of them alone (\(U / Z_\text{in}\) of the 1 A solve), and input_impedance() reports 8.1 % more (image; mom: 7.5 % and 8.2 %).
  • A voltage source of 0 V is a short through \(Z_q\) between its terminals (or to remote earth); the solve returns the current through it. With \(Z_q \ne 0\) it is a passive impedance.
  • \(Z_q\) is evaluated at every solved frequency; \(j\omega L\) makes the constraint rows complex. A galvanic world whose sources carry an inductance is solved once per frequency in the image family (its owner-level system is small), while mom and mom_sommerfeld factorise their segment system once and solve one V × V system per frequency for the V source currents (exact; audit W2R2-02: ×1.02 the R-only solve at 20 frequencies on 1 488 segments, ×28 before). Before 2026.11 a series impedance was modelled as a junction at the source terminal and a finite conductor to the electrode (lumped_series_resistance_ohm); the two give the same solution to round-off.
  • For \(R \gg Z_E\) (the impedance the source sees) a current source \(U/R\) is the better model: the potentials of the voltage solve and input_impedance() (\(U/I - Z_q\)) lose about \(\log_{10}(R/Z_E)\) digits (2.7e-6 relative at \(R = 10^{12}\) Ω on a 36 Ω rod, image; audit W2R2-10). \(R\) and \(L\) are 0 or at least the smallest normal float, 2.2e-308: a subnormal value is rejected (it turned the solve into nan).
  • Rejected before any numerics run (World.check): a voltage source whose terminals are bonded into one cluster by ideal conductors (short circuit, with or without \(Z_q\); with \(Z_q\) its current \(U/Z_q\) would circulate through the bond and the source, none through the soil), and voltage sources without internal resistance that form a loop through ideal bonds or remote earth (two of them from one cluster to remote earth, a ring of two-terminal ones) — the loop fixes one potential difference twice. A source with internal_resistance_ohm > 0 opens such a loop; an inductance alone does not, because \(Z_q\) vanishes at \(f = 0\).
  • Backends without the registry capability voltage_sources raise BackendCapabilityError (every built-in backend has it since 2026.11; fem lacked it up to its conforming model, user decision Q37).

Up to 0.15.0 every backend dropped voltage sources with a warning, so a world driven by one solved to zero potentials (review finding 10.51).

Notes

Source is a discriminated union — Pydantic uses the kind field to pick the correct sub-class on validation and JSON round-trip. This means error messages for malformed source dicts point at the specific sub-class (CurrentSource / VoltageSource) selected by kind, instead of dumping the entire union's validator chain.

Assignments are validated (validate_assignment), so a source handle edited between two solves (src.magnitude = 20.0) cannot take a non-finite value.

A VoltageSource (and a SourceRecord) pickled before 2026.11 unpickles with internal_resistance_ohm = internal_inductance_h = 0, the ideal source it was, as its JSON loads (audit W2R2-01).

Source module-attribute

Source = Annotated[Union[CurrentSource, VoltageSource], Discriminator('kind')]

Union of the source classes, discriminated by kind.

CurrentSource ("current") and VoltageSource ("voltage"). The kind field selects the validator at validation time, which keeps Pydantic error messages on the actual sub-class instead of the union.

SourceAdapter module-attribute

SourceAdapter: TypeAdapter[Source] = TypeAdapter(Source)

Pydantic TypeAdapter that validates one stand-alone source.

Used by code paths that need to validate a source given as a dict (e.g. CLI imports, JSON round-trips). World.sources uses the annotation directly via Pydantic field generation.

CurrentSource

Bases: _SourceBase

Impressed current source.

Attributes:

Name Type Description
magnitude float

Current amplitude in A (RMS in the frequency domain); finite. A world is solved only if at least one source has a non-zero magnitude, and a source of magnitude 0 is rejected when solved (it injects nothing; remove it instead).

phase_deg float

Phase angle in degrees; finite. Default 0.

VoltageSource

Bases: _SourceBase

Impressed voltage source, ideal or behind an internal impedance.

Drives attached_to against return_to, or against remote earth for return_to=None, with the voltage \(U = |U|\,e^{j\varphi}\) behind the internal series impedance \(Z_q(f) = R + j\omega L\) (internal_resistance_ohm, internal_inductance_h; both 0 by default, the ideal source):

\[ \varphi_a - \varphi_r = U - Z_q\,I . \]

The source current \(I\) (from the source into attached_to) is solved for and reported in current; input_impedance() is the impedance the source sees at its terminals, \((\varphi_a - \varphi_r) / I = U / I - Z_q\), without \(Z_q\). See the module docstring for the semantics, the checks and the backends that solve it.

Attributes:

Name Type Description
magnitude float

Voltage amplitude in V (RMS in the frequency domain); finite. 0 V is a short through \(Z_q\) (its current is still solved for); a world needs at least one source of non-zero magnitude.

phase_deg float

Phase angle in degrees; finite. Default 0.

internal_resistance_ohm float

Internal series resistance \(R\) in Ω; finite, ≥ 0, and either 0 or at least the smallest normal float (sys.float_info.min = numpy.finfo(float).tiny, 2.2e-308; a subnormal \(R\) turned the solve into nan, audit W2R2-10). Default 0. For \(R \gg Z_E\) (the impedance the source sees) a current source \(U/R\) is the better model: the potentials of the voltage solve and input_impedance() lose about \(\log_{10}(R/Z_E)\) digits (2.7e-6 relative at \(R = 10^{12}\) Ω on a 36 Ω rod, image).

internal_inductance_h float

Internal series inductance \(L\) in H; finite, ≥ 0, 0 or normal like \(R\). Default 0. It enters as \(j\omega L\) at each solved frequency and vanishes at \(f = 0\).

Notes

A source pickled before the two fields existed (2026.11, user decision D1) unpickles with \(R = L = 0\), the ideal source it was, as its JSON loads (audit W2R2-01).

internal_impedance

internal_impedance(frequency: float) -> complex

Internal impedance \(Z_q = R + j\omega L\) at frequency.

Parameters:

Name Type Description Default
frequency float

Frequency in Hz.

required

Returns:

Type Description
complex

\(Z_q\) in Ω, with \(\omega = 2\pi f\); \(R\) at \(f = 0\) and 0j for an ideal source.

Source code in src/groundfield/sources.py
def internal_impedance(self, frequency: float) -> complex:
    """Internal impedance $Z_q = R + j\\omega L$ at ``frequency``.

    Parameters
    ----------
    frequency
        Frequency in Hz.

    Returns
    -------
    complex
        $Z_q$ in Ω, with $\\omega = 2\\pi f$; $R$ at $f = 0$ and
        ``0j`` for an ideal source.
    """
    return _internal_impedance(
        self.internal_resistance_ohm,
        self.internal_inductance_h,
        2.0 * math.pi * float(frequency),
    )