Factories¶
The create_* functions are the notebook-friendly way to build a
model: each one constructs an object, registers it in the World and
returns it. They are thin wrappers around the model classes
(gf.RodElectrode(...) plus world.add_electrode(...) is equivalent),
with two additions: the parameters of every electrode kind are typed
(IDE completion, type-checker errors for a misspelled kind or keyword),
and unknown names raise with the closest valid name ("did you mean
…?").
| Factory | Creates | Physical meaning |
|---|---|---|
create_world(soil=...) |
World |
container of the physical model; the soil is the conducting half-space |
create_electrode(world, kind, ...) |
electrode (rod, ring, strip, polyline, star, mesh, grid_mesh) |
metal in contact with the soil; leaks current |
create_conductor(world, start=, end=, ...) |
Conductor |
connection between two nodes: ideal, lumped resistance, or a distributed line with resistance, inductance and optional leakage |
create_junction(world, position=) |
Junction |
node without soil contact (cable joint, instrument terminal) |
create_source(world, attached_to=, ...) |
CurrentSource |
impressed current between a node and remote earth or a second node (return_to) |
create_engine(...) |
Engine |
the numerics: backend, frequencies, segment length, kernel, earth-return model |
run_simulation(world, engine) |
FieldResult |
the same as world.solve(engine) and engine.solve(world) |
Mathematical and physical context¶
The factories assemble the boundary-value problem that every backend
solves: Laplace's equation \(\nabla\cdot(\sigma\nabla\varphi) = 0\) in the
layered soil (soil), the electrode surfaces as equipotential
boundaries per galvanic cluster (create_electrode), Kirchhoff's laws
for the conductor network between the nodes (create_conductor,
create_junction), and the impressed currents as the right-hand side
(create_source). The Engine decides how the problem is discretised
and solved; the World holds only physics and can be solved by several
engines for a cross-check.
Coordinates are \((x, y, z)\) in metres with \(z\) the depth below the soil surface (positive downwards); every other quantity is in SI units (see Units and conventions).
Example¶
import groundfield as gf
world = gf.create_world(soil=gf.HomogeneousSoil(rho=100.0))
rod = gf.create_electrode(world, "rod", name="g1", position=(0.0, 0.0, 0.0), length=1.5)
ring = gf.create_electrode(world, "ring", name="g2", center=(10.0, 0.0, 0.8), radius=2.0)
joint = gf.create_junction(world, name="J1", position=(5.0, 0.0, 0.7)) # cable joint
gf.create_conductor(world, name="l1", start=rod, end=joint, cross_section=50e-6)
gf.create_conductor(world, name="l2", start=joint, end=ring, cross_section=50e-6)
gf.create_source(world, name="s1", attached_to="g1", magnitude=10.0)
engine = gf.create_engine(backend="image", frequencies=[50.0, 150.0])
result = gf.run_simulation(world, engine)
assert type(ring).__name__ == "RingElectrode"
assert len(result.input_impedance("s1")) == 2 # one value per frequency
create_electrode(world, "rnig", ...) raises
ValueError: Unknown electrode kind 'rnig'. Did you mean 'ring'?;
a keyword that the chosen kind does not take raises TypeError with the
list of valid keywords. A junction that no conductor connects to an
electrode has no defined potential and is rejected when the world is
solved.
API reference¶
api ¶
Top-level factory functions.
This module provides the convenient, notebook-friendly API of
groundfield. Instead of instantiating each class manually, the
caller writes:
import groundfield as gf world = gf.create_world(soil=gf.HomogeneousSoil(rho=100.0)) rod = gf.create_electrode(world, "rod", name="g1", ... position=(0.0, 0.0, 0.0), length=1.5) ring = gf.create_electrode(world, "ring", name="g2", ... center=(10.0, 0.0, 0.8), radius=2.0) conn = gf.create_conductor(world, name="l1", start=rod, end=ring, ... conductor_type="bare_copper") src = gf.create_source(world, name="s1", attached_to="g1", ... magnitude=10.0) j = gf.create_junction(world, name="J1", position=(10.0, 0.0, 0.5)) eng = gf.create_engine(backend="image", frequencies=[50.0]) result = gf.run_simulation(world, eng) # or world.solve(eng)
Conventions
- Every
create_*function registers the new object in theWorldand returns it. start/endof a conductor may be electrodes, junctions, their names, or coordinate tuples.- Names are auto-generated when omitted (
electrode_0,conductor_0,source_0,junction_0). - Coordinates are in metres with
zpointing downwards (z > 0is in the soil).
Static typing
create_electrode() has one @overload per electrode kind, so an
IDE shows the parameters of the chosen kind and a type checker reports a
misspelled kind or keyword and infers the concrete return type
(create_electrode(w, "ring", ...) is a RingElectrode). The
string options are Literal types (ElectrodeKind,
SourceKind, KernelKind, and
Backend,
EarthInductiveModel,
ConductorType,
SoilCoupling,
InductanceModel where they are
defined). A kind chosen at run time (a plain str) needs a
typing.cast to ElectrodeKind — or build the class directly
(gf.RodElectrode(...) plus world.add_electrode). At run time,
an unknown kind, keyword or electrode name raises with the closest
valid name ("did you mean …?").
ElectrodeKind
module-attribute
¶
Electrode kinds accepted by create_electrode().
"rod", "ring", "strip", "polyline", "star", "mesh"
and "grid_mesh" create a RodElectrode, RingElectrode,
StripElectrode, PolylineElectrode, StarElectrode,
MeshElectrode and GridMeshElectrode.
SourceKind
module-attribute
¶
Source kinds accepted by create_source().
"current" creates a CurrentSource (impressed current),
"voltage" a VoltageSource (impressed voltage, ideal or behind
an internal impedance).
KernelKind
module-attribute
¶
Source kernels accepted by create_engine().
The kernel is used for the reaction matrix and the field evaluation:
"point" (one point source per segment) or "line" (the exact
uniform line source per segment). kernel=None leaves the choice to
the backend.
PointLike
module-attribute
¶
A point (x, y, z) in m: a tuple, a list or a numpy array.
ConductorEnd
module-attribute
¶
One end of a conductor in create_conductor().
An electrode or junction, the name of a registered electrode or
junction, or a point (x, y, z) in m.
create_world ¶
create_world(*, name: str = 'world', soil: SoilModel | None = None, boundary: BoundaryConditions | None = None) -> World
Create an empty World.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Human-readable name (used in logs and result metadata). |
'world'
|
soil
|
SoilModel | None
|
Optional pre-built soil model. Can also be set later via
|
None
|
boundary
|
BoundaryConditions | None
|
Optional pre-built boundary configuration. No backend reads it
(see |
None
|
Returns:
| Type | Description |
|---|---|
World
|
The new, empty world. |
Source code in src/groundfield/api.py
create_electrode ¶
create_electrode(world: World, kind: Literal['rod'] = ..., *, name: str | None = ..., position: PointLike, length: float, wire_radius: float = ..., concrete_shell_coefficient_ohm_m: float = ...) -> RodElectrode
create_electrode(world: World, kind: Literal['ring'], *, name: str | None = ..., center: PointLike, radius: float, wire_radius: float = ..., concrete_shell_coefficient_ohm_m: float = ...) -> RingElectrode
create_electrode(world: World, kind: Literal['strip'], *, name: str | None = ..., start: PointLike, end: PointLike, wire_radius: float = ..., concrete_shell_coefficient_ohm_m: float = ...) -> StripElectrode
create_electrode(world: World, kind: Literal['polyline'], *, name: str | None = ..., vertices: Sequence[PointLike] | NDArray[Any], closed: bool = ..., wire_radius: float = ..., concrete_shell_coefficient_ohm_m: float = ...) -> PolylineElectrode
create_electrode(world: World, kind: Literal['star'], *, name: str | None = ..., center: PointLike, n_arms: int, arm_length: float, orientation_deg: float = ..., wire_radius: float = ..., concrete_shell_coefficient_ohm_m: float = ...) -> StarElectrode
create_electrode(world: World, kind: str = 'rod', *, name: str | None = None, **params: Any) -> _ElectrodeBase
Create an electrode and register it with world.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
world
|
World
|
Target world that will own the new electrode. |
required |
kind
|
str
|
Geometry type (
See |
'rod'
|
name
|
str | None
|
Optional unique name. Auto-generated ( |
None
|
**params
|
Any
|
The parameters of |
{}
|
Returns:
| Type | Description |
|---|---|
Electrode
|
The registered electrode, of the class that belongs to |
Raises:
| Type | Description |
|---|---|
ValueError
|
Unknown |
TypeError
|
A keyword that |
Source code in src/groundfield/api.py
create_conductor ¶
create_conductor(world: World, *, start: ConductorEnd, end: ConductorEnd, name: str | None = None, conductor_type: ConductorType = 'generic', wire_radius: float = 0.005, resistivity: float = 1.68e-08, cross_section: float | Literal['from_radius'] | None = None, discretize_segment_length: float | None = None, coupling_to_soil: SoilCoupling = 'isolated', inductance_model: InductanceModel | None = None, lumped_series_resistance_ohm: float | None = None, resistance_model: ResistanceModel = 'dc') -> Conductor
Create a conductor and register it with world.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
world
|
World
|
Target world. |
required |
start
|
ConductorEnd
|
Start and end point. Each may be an electrode or a junction
( |
required |
end
|
ConductorEnd
|
Start and end point. Each may be an electrode or a junction
( |
required |
name
|
str | None
|
Optional unique name. Auto-generated ( |
None
|
conductor_type
|
ConductorType
|
Conductor type ( |
'generic'
|
wire_radius
|
float
|
Wire radius in m. |
0.005
|
resistivity
|
float
|
Material resistivity of the conductor in Ω·m (default: copper, 1.68e-8 Ω·m). |
1.68e-08
|
cross_section
|
float | Literal['from_radius'] | None
|
Conductor cross section in \(\mathrm{m}^2\). |
None
|
discretize_segment_length
|
float | None
|
Maximum sub-segment length in m for the distributed-conductor
model (see ADR-0003). |
None
|
coupling_to_soil
|
SoilCoupling
|
|
'isolated'
|
inductance_model
|
InductanceModel | None
|
|
None
|
lumped_series_resistance_ohm
|
float | None
|
Optional additional lumped series resistance in Ω. |
None
|
resistance_model
|
ResistanceModel
|
|
'dc'
|
Returns:
| Type | Description |
|---|---|
Conductor
|
The registered conductor. |
Raises:
| Type | Description |
|---|---|
KeyError
|
|
Source code in src/groundfield/api.py
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 | |
create_junction ¶
Create a leakage-free junction node and register it with world.
A junction is a node of the conductor network without soil contact:
conductors can start or end there (create_conductor()) and
sources can attach to it (create_source()), but it injects no
current into the soil. Its potential follows from Kirchhoff's
current law and Ohm's law along the conductors; solved results
report it in result.metadata["junction_potentials"][name] (V per
frequency). Use it for cable joints and for the terminals of a
measuring instrument (see Junction).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
world
|
World
|
Target world. |
required |
position
|
PointLike
|
Location |
required |
name
|
str | None
|
Optional unique node name (electrodes and junctions share one
namespace). Auto-generated ( |
None
|
Returns:
| Type | Description |
|---|---|
Junction
|
The registered junction. |
Raises:
| Type | Description |
|---|---|
ValueError
|
The name is already taken by an electrode or junction, or the position is not finite. |
Notes
A junction must be connected through conductors to an electrode or
to a galvanically coupled conductor; a subnetwork without soil
contact has an undefined potential and is rejected when solved.
Only backends that declare the junctions capability (the image
family) accept worlds with junctions.
Source code in src/groundfield/api.py
create_source ¶
create_source(world: World, *, attached_to: str | Electrode | Junction, name: str | None = ..., kind: Literal['current'] = ..., magnitude: float = ..., phase_deg: float = ..., return_to: str | Electrode | Junction | None = ...) -> CurrentSource
create_source(world: World, *, attached_to: str | Electrode | Junction, name: str | None = ..., kind: Literal['voltage'], magnitude: float = ..., phase_deg: float = ..., return_to: str | Electrode | Junction | None = ..., internal_resistance_ohm: float | None = ..., internal_inductance_h: float | None = ...) -> VoltageSource
create_source(world: World, *, attached_to: str | Electrode | Junction, name: str | None = None, kind: SourceKind = 'current', magnitude: float = 1.0, phase_deg: float = 0.0, return_to: str | Electrode | Junction | None = None, internal_resistance_ohm: float | None = None, internal_inductance_h: float | None = None) -> Source
Create a source and register it with world.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
world
|
World
|
Target world. |
required |
attached_to
|
str | Electrode | Junction
|
Node the source feeds: an electrode or a junction
( |
required |
name
|
str | None
|
Optional unique name. Auto-generated ( |
None
|
kind
|
SourceKind
|
|
'current'
|
magnitude
|
float
|
Amplitude in A (current source) or V (voltage source), RMS; finite. A current source of magnitude 0 is rejected when the world is solved; a voltage source of 0 V is an ideal short. |
1.0
|
phase_deg
|
float
|
Phase angle in degrees; finite. |
0.0
|
return_to
|
str | Electrode | Junction | None
|
Optional second terminal (electrode or junction, or its name):
the source injects \(+I\) at |
None
|
internal_resistance_ohm
|
float | None
|
Internal series impedance \(Z_q = R + j\omega L\) of a voltage
source, in Ω and H (finite, ≥ 0; |
None
|
internal_inductance_h
|
float | None
|
Internal series impedance \(Z_q = R + j\omega L\) of a voltage
source, in Ω and H (finite, ≥ 0; |
None
|
Returns:
| Type | Description |
|---|---|
CurrentSource or VoltageSource
|
The registered source. |
Raises:
| Type | Description |
|---|---|
ValueError
|
Unknown |
KeyError
|
|
Source code in src/groundfield/api.py
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 | |
create_engine ¶
create_engine(*, backend: Backend = 'image', frequencies: Sequence[float] | NDArray[Any] | None = None, segment_length: float | None = None, tolerance: float | None = None, max_iterations: int | None = None, earth_inductive_model: EarthInductiveModel | None = None, image_max_terms: int | None = None, image_series_tol: float | None = None, kernel: KernelKind | None = None, image_far_field: Literal['quadrupole', 'point'] | None = None, options: Mapping[str, Any] | None = None, sommerfeld_assembly: Literal['exact', 'tabulated'] | None = None, max_segments: int | None = None, warn_segments: int | None = None) -> Engine
Create a configured Engine.
Only the arguments you pass are forwarded; every argument left at
None takes the Engine default, so the engine
is the single source of its defaults and option-level warnings of the
engine concern only options you actually set. Backend-specific
settings, such as the accuracy controls of mom_sommerfeld
(epsabs, epsrel, lambda_max_factor, max_osc_panels) or
the mesh of fem, go into options.
The Engine performs an automatic backend dispatch inside
Engine.solve(): when backend="image" (the default) is
combined with a TwoLayerSoil the call is forwarded to
"image_2layer", and with a MultiLayerSoil to
"image_nlayer". Notebooks written for the homogeneous case
therefore keep working when the soil model is replaced by a
layered one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
Backend
|
Numerical method ( |
'image'
|
frequencies
|
Sequence[float] | NDArray[Any] | None
|
Frequencies in Hz (order preserved). Default: |
None
|
segment_length
|
float | None
|
Maximum segment length used to discretise the geometry, in m. Default: the engine default (0.5 m). |
None
|
tolerance
|
float | None
|
Convergence parameters for iterative solvers. No backend reads them; they are forwarded only when given. |
None
|
max_iterations
|
float | None
|
Convergence parameters for iterative solvers. No backend reads them; they are forwarded only when given. |
None
|
earth_inductive_model
|
EarthInductiveModel | None
|
Earth model for the inductive coupling between distributed
conductor segments, effective only when a conductor sets
|
None
|
image_max_terms
|
int | None
|
Maximum number of image-charge series terms of the layered
backends; a truncated series emits a
|
None
|
image_series_tol
|
float | None
|
Series stop tolerance: iteration ends when the geometric tail
bound |
None
|
kernel
|
KernelKind | None
|
Field / reaction-matrix kernel ( |
None
|
image_far_field
|
Literal['quadrupole', 'point'] | None
|
Form of the far images of the line kernel in two-layer soil,
|
None
|
options
|
Mapping[str, Any] | None
|
Backend-specific settings, keyed by backend name, e.g.
|
None
|
sommerfeld_assembly
|
Literal['exact', 'tabulated'] | None
|
|
None
|
max_segments
|
int | None
|
Resource limit of the engine: the largest number of leakage
segments and of nodal unknowns it solves ( |
None
|
warn_segments
|
int | None
|
Size of the soft notice of the engine: at or above this number of
leakage segments or nodal unknowns the solve warns that it may take
minutes ( |
None
|
Returns:
| Type | Description |
|---|---|
Engine
|
The configured engine. |
Raises:
| Type | Description |
|---|---|
TypeError
|
An unknown keyword argument. |
ValueError
|
A value the engine rejects. |
Source code in src/groundfield/api.py
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 | |
run_simulation ¶
Solve world with engine; identical to world.solve(engine).
The call goes through World.solve() and therefore has exactly
its semantics, including the source snapshot: the world's sources
are restored after the solve even if a backend changes them in
flight, and the handles returned by create_source() stay
attached to the world.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
world
|
World
|
The world to solve. |
required |
engine
|
Engine
|
The engine configuration. |
required |
Returns:
| Type | Description |
|---|---|
FieldResult
|
The solution. |