Validation (cross-engine)¶
compare_engines(world, engines={...}) solves the same world with two
or more engines and reports, in one tolerance table, how far their
results are apart. Engines that differ in method (image series vs
point-matching MoM vs Sommerfeld quadrature), in kernel or only in the
discretisation make it the standard check that a number does not
depend on a numerical choice (ADR-0001: two engines side by side,
validating each other). The validation of the engines against
closed-form references and against each other is collected on the
validation page.
What is compared¶
Every compared quantity is complex and is compared at every frequency, real and imaginary part separately:
- the cluster impedance of every cluster (Ω),
- the input impedance of every current or voltage source (Ω; a voltage source since 2026.11, the impedance seen by the source without its internal impedance),
- optionally the potential at
sample_points(V).
Each (quantity, frequency, part) is one ComparisonRow with the
deviation
the worst pairwise spread of the part \(c_k\) over the engines, relative
to the smallest value; the floor \(\epsilon\) (component_floor,
default 10⁻³ of the smallest magnitude) keeps a part that is nearly zero
for every engine (the reactance at 50 Hz) from dominating. A row passes
when \(\mathrm{dev} \le\) rel_tolerance. A value that is non-finite or
zero for some engines only fails its row.
is_consistent is True only if at least one quantity was compared,
every row passed and no engine returned a stub result: a comparison in
which nothing could be evaluated (no source, a source on an unknown
node) is reported as inconsistent rather than as a silent pass. The
engines must use the same frequency list.
Example¶
import groundfield as gf
world = gf.create_world(soil=gf.TwoLayerSoil(rho_1=100.0, rho_2=500.0, h_1=2.0))
gf.create_electrode(world, "ring", name="g1", center=(0.0, 0.0, 0.8), radius=5.0)
gf.create_source(world, attached_to="g1", magnitude=1.0)
report = gf.compare_engines(
world,
engines={
"image_2layer": gf.create_engine(segment_length=0.5),
"mom": gf.create_engine(backend="mom", segment_length=0.5),
"refined": gf.create_engine(segment_length=0.25),
},
rel_tolerance=0.02,
)
print(report.summary())
assert report.is_consistent
assert len(report.rows) == 4 # Z_cluster[g1] and Z_in[source_0], Re and Im, at 50 Hz
A single engine raises ValueError: one engine cannot validate itself.
The deprecated names cim and bem run image and mom and add no
independent information.
API reference¶
validation ¶
Cross-engine comparison for self-validation.
This module provides compare_engines() — a small convenience
helper that runs the same World through several
Engine configurations and checks the consistency of the
results. It implements ADR-0001 (docs/adr/0001-two-layer-method.md):
two engines side by side, validating each other.
Usage
import groundfield as gf world = gf.create_world(soil=gf.HomogeneousSoil(resistivity=100.0)) gf.create_electrode(world, "rod", name="g1", ... position=(0, 0, 0.0), length=1.5) gf.create_source(world, attached_to="g1", magnitude=1.0) report = gf.compare_engines( ... world, ... engines={ ... "image": gf.create_engine(backend="image", segment_length=0.05), ... "mom": gf.create_engine(backend="mom", segment_length=0.05), ... }, ... rel_tolerance=0.05, ... ) report.is_consistent True
At least two engines are required — a single-entry mapping raises
ValueError, because one engine cannot validate itself.
What is compared (review pass 10, 10.71)
Every compared quantity is complex and is compared at every frequency, real and imaginary part separately:
- the cluster impedance of every cluster
(
FieldResult.cluster_impedance(), Ω), - the input impedance of every current source
(
FieldResult.input_impedance(), Ω), - optionally the potential at
sample_points(V).
Each (quantity, frequency, component) is one row of the report's
tolerance table (EngineComparison.rows). Up to 0.15.0 only the
real part of the cluster impedance at the first frequency was
compared, so an earth-return model that doubled the reactance at
1 kHz passed, engines with different frequency lists were compared at
different frequencies, and a non-finite result was silently dropped.
ComparisonRow
dataclass
¶
ComparisonRow(quantity: str, frequency: float, component: str, values: dict[str, float], reference: float, deviation: float, tolerance: float, passed: bool)
One row of the tolerance table of EngineComparison.
Attributes:
| Name | Type | Description |
|---|---|---|
quantity |
str
|
|
frequency |
float
|
Frequency in Hz. |
component |
str
|
|
values |
dict[str, float]
|
|
reference |
float
|
Denominator of the relative deviation (same unit as
|
deviation |
float
|
Pairwise spread |
tolerance |
float
|
The relative tolerance the row was checked against. |
passed |
bool
|
|
EngineComparison
dataclass
¶
EngineComparison(results: dict[str, 'FieldResult'], rel_tolerance: float, cluster_impedance_table: dict[str, dict[str, float]] = dict(), deviations: dict[str, float] = dict(), is_consistent: bool = False, notes: list[str] = list(), frequencies: list[float] = list(), rows: list[ComparisonRow] = list(), impedances: dict[str, dict[str, list[complex]]] = dict(), input_deviations: dict[str, float] = dict())
Outcome of a cross-engine comparison.
Attributes:
| Name | Type | Description |
|---|---|---|
results |
dict[str, 'FieldResult']
|
Mapping |
rel_tolerance |
float
|
Relative tolerance the results were checked against. |
cluster_impedance_table |
dict[str, dict[str, float]]
|
|
deviations |
dict[str, float]
|
|
is_consistent |
bool
|
|
notes |
list[str]
|
Diagnostic strings (stub results, skipped or non-finite quantities, the sample-point summary). |
frequencies |
list[float]
|
The common frequency list in Hz. |
rows |
list[ComparisonRow]
|
The explicit tolerance table: one |
impedances |
dict[str, dict[str, list[complex]]]
|
|
input_deviations |
dict[str, float]
|
|
Notes
The fields of 0.15.0 keep their positional order; the fields added in review pass 10 (10.71) follow them.
summary ¶
Return a line-oriented textual summary with the tolerance table.
Source code in src/groundfield/validation.py
compare_engines ¶
compare_engines(world: 'World', engines: dict[str, 'Engine'], *, rel_tolerance: float = 0.05, sample_points: ndarray | None = None, component_floor: float = DEFAULT_COMPONENT_FLOOR) -> EngineComparison
Run world through every engine and compare the results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
world
|
'World'
|
World to evaluate. Not modified by this function. |
required |
engines
|
dict[str, 'Engine']
|
Mapping |
required |
rel_tolerance
|
float
|
Maximum allowed pairwise relative deviation (default 5 %) of every row of the tolerance table, i.e. per compared quantity, frequency and component (real / imaginary part): \[
\frac{\max_k c_k - \min_k c_k}
{\max(\min_k |c_k|,\;
\eta\, \min_k |Z_k|)}
\;\le\; \texttt{rel\_tolerance}
\]
over the engines \(k\), where \(c_k\) is the real or
imaginary part of the complex value \(Z_k\) and
\(\eta\) is |
0.05
|
sample_points
|
ndarray | None
|
Optional. Array of shape |
None
|
component_floor
|
float
|
Dimensionless floor \(\eta\) of the component reference, as a fraction of the smallest compared magnitude (default 1e-3). A real or imaginary part that is smaller than \(\eta |Z|\) for every engine is compared on that absolute scale instead of relative to itself, so a reactance that is numerically zero (galvanic worlds) or negligible against the resistance does not turn round-off into a large relative deviation. |
DEFAULT_COMPONENT_FLOOR
|
Returns:
| Type | Description |
|---|---|
EngineComparison
|
Structured report with the explicit tolerance table
( |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than two engines are given, or if the engines (or their results) do not share one frequency list. |
Notes
What is compared (changed in review pass 10, 10.71). The
cluster impedance of every cluster and the input impedance of every
current source, as complex numbers at every frequency, real and
imaginary part separately; optionally the complex potential at
sample_points. Up to 0.15.0 only \(\mathrm{Re}\,Z\) of the
clusters at the first frequency was compared, so a reactance that
differed by a factor two at 1 kHz passed. A quantity that is
undefined for every engine (a purely passive cluster with
\(\sum I = 0\)) is skipped with a note; a quantity that is
non-finite for some engines only is a failure (up to 0.15.0 the
non-finite engine was dropped and the rest compared); so is a value
that is exactly zero for some engines only (audit round 1, F03).
Engines with
different frequency lists raise instead of being compared at
different frequencies.
Metric definition (changed in 0.15.0). The deviation is the
full spread of the compared values normalised by the smallest
magnitude among them, which is the worst pairwise relative deviation
max_ij |c_i - c_j| / min |c| for same-sign values. Up to 0.14.x
the deviation was measured against the ensemble mean, which for
two engines reports half the true disagreement.
Aliases (review pass 10, 10.76). Engines that ran the same
computation — the same backend after alias resolution
(metadata['dispatched_to'] / ['effective_backend']) with
identical settings, e.g. the deprecated cim next to
image_2layer — are named in a note: their agreement is not an
independent cross-check. The verdict itself is unchanged.
Vacuous comparisons (changed in 0.15.0). A report is
is_consistent=True only if at least one row was evaluated. A
world without sources (or with a misspelled Source.attached_to)
has no defined impedance anywhere; such a run yields
is_consistent=False plus a "comparison vacuous" note.
Source code in src/groundfield/validation.py
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 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 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 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 | |