freegsnke.up_down_symmetry module

Prepare an approximately up-down symmetric machine for symmetric evolution.

The routines in this module retain the electrically even part of a user-provided machine description as the external current basis. They identify reflected element pairs, report odd active circuits, average accepted pairs onto an exactly reflected geometry, and provide explicit current transforms.

Copyright 2025 UKAEA, UKRI-STFC, and The Authors, as per the COPYRIGHT and README files.

This file is part of FreeGSNKE.

FreeGSNKE is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

FreeGSNKE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with FreeGSNKE. If not, see <http://www.gnu.org/licenses/>.

class freegsnke.up_down_symmetry.ElementPair(upper_name: str, lower_name: str, group: str, reflected_rms: float, matching_numeric_suffix: bool)[source]

Bases: object

Record one upper/lower pairing and its pre-symmetrisation mismatch.

group: str
lower_name: str
matching_numeric_suffix: bool
reflected_rms: float
upper_name: str
class freegsnke.up_down_symmetry.GeometryDiscrepancy(component: str, upper_name: str, lower_name: str, reflected_rms: float)[source]

Bases: object

Record one pre-symmetrisation reflected-geometry discrepancy.

component: str
lower_name: str
reflected_rms: float
upper_name: str
class freegsnke.up_down_symmetry.PreparedUpDownMachine(active_coils_data: dict[str, Any], even_active_coils_data: dict[str, Any], passive_coils_data: list[dict[str, Any]], limiter_data: list[dict[str, float]] | None, wall_data: list[dict[str, float]] | None, original_active_names: tuple[str, ...], even_active_names: tuple[str, ...], passive_names: tuple[str, ...], active_pairs: tuple[ElementPair, ...], passive_pairs: tuple[ElementPair, ...], self_symmetric_active_names: tuple[str, ...], self_symmetric_passive_names: tuple[str, ...], source_z_midplane: float, z_shift: float, midplane_fit_rms: float, midplane_fit_samples: int, geometry_discrepancies: tuple[GeometryDiscrepancy, ...], excluded_odd_active_names: tuple[str, ...], source_active_coils_data: dict[str, Any] | None = None, source_passive_coils_data: list[dict[str, Any]] | None = None, source_limiter_data: list[dict[str, float]] | None = None, source_wall_data: list[dict[str, float]] | None = None)[source]

Bases: object

Store symmetric machine descriptions, pairings, and current transforms.

active_coils_data preserves every retained original active-current label. Electrically odd source circuits are rejected by default or recorded in excluded_odd_active_names when explicitly omitted. even_active_coils_data combines each upper/lower pair into one series circuit for the existing strict symmetric evolutive-solver path. passive_coils_data preserves every original passive label.

The source_* fields keep an unmodified copy of the input descriptions (before any recentring or averaging) so that plot() can overlay them on the symmetrised geometry. summary() reports the retained and excluded circuits, the passive pairs, the midplane fit, and the pre-averaging discrepancy of every component.

active_coils_data: dict[str, Any]
property active_even_to_original: ndarray

Return the map from reduced even currents to original active currents.

property active_odd_names: tuple[str, ...]

Return labels for the active odd-current coordinates.

property active_odd_to_original: ndarray

Return the map from odd pair currents to original active currents.

property active_original_to_even: ndarray

Return the map from original active currents to pair-average currents.

The reduced even current of an upper/lower pair is (I_upper + I_lower) / 2. A self-symmetric circuit is unchanged.

property active_original_to_odd: ndarray

Return the map from original active currents to pair-difference currents.

The odd current of an upper/lower pair is (I_upper - I_lower) / 2.

active_pairs: tuple[ElementPair, ...]
property active_reflection_operator: ndarray

Return the reflection permutation in the original active basis.

check_geometry_tolerance(max_reflected_rms: float) → None[source]

Raise if any component exceeds a reflected RMS tolerance in metres.

Parameters:

max_reflected_rms (float) – Maximum permitted reflected root-mean-square mismatch [m].

Raises:

ValueError – If max_reflected_rms is negative or any component exceeds it.

combine_active_currents(even_currents: _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], odd_currents: _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None) → ndarray[source]

Reconstruct original active currents from even and odd coordinates.

Omitting odd_currents returns the exactly even projection.

even_active_coils_data: dict[str, Any]
even_active_names: tuple[str, ...]
property even_machine_reflection_operator: ndarray

Return reflection in the reduced machine basis used for even evolution.

Every reduced active circuit is self-symmetric, while passive elements retain their original labels and are exchanged with their partners.

excluded_odd_active_names: tuple[str, ...]
geometry_discrepancies: tuple[GeometryDiscrepancy, ...]
largest_geometry_discrepancies(count: int | None = None) → tuple[GeometryDiscrepancy, ...][source]

Return pre-symmetrisation discrepancies in descending RMS order.

Parameters:

count (int or None, optional) – Maximum number of discrepancy records to return. When None, all recorded discrepancies are returned.

Returns:

Discrepancy records sorted from largest to smallest reflected_rms.

Return type:

tuple of GeometryDiscrepancy

limiter_data: list[dict[str, float]] | None
property maximum_geometry_discrepancy: GeometryDiscrepancy | None

Return the largest pre-symmetrisation discrepancy, or None.

midplane_fit_rms: float
midplane_fit_samples: int
original_active_names: tuple[str, ...]
passive_coils_data: list[dict[str, Any]]
passive_names: tuple[str, ...]
passive_pairs: tuple[ElementPair, ...]
property passive_reflection_operator: ndarray

Return the reflection permutation in the original passive basis.

plot(axis: Axes | None = None, show: bool = True) → Axes[source]

Plot the original and symmetrised machine geometries overlaid.

Parameters:
  • axis (matplotlib.axes.Axes, optional) – Existing axes to draw onto. A new figure and axes are created when omitted.

  • show (bool, default=True) – If True, call plt.show() after drawing.

Returns:

Axes containing the comparison plot.

Return type:

matplotlib.axes.Axes

project_metal_currents_even(currents: _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) → ndarray[source]

Project all original-basis metal currents onto the even subspace.

property reflection_operator: ndarray

Return the block reflection permutation for all metal currents.

self_symmetric_active_names: tuple[str, ...]
self_symmetric_passive_names: tuple[str, ...]
source_active_coils_data: dict[str, Any] | None = None
source_limiter_data: list[dict[str, float]] | None = None
source_passive_coils_data: list[dict[str, Any]] | None = None
source_wall_data: list[dict[str, float]] | None = None
source_z_midplane: float
split_active_currents(currents: _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) → tuple[ndarray, ndarray][source]

Split original active currents into even and odd coordinates.

Parameters:

currents (array-like) – Active currents in original_active_names order.

Returns:

even_currents, odd_currents – Pair-average and pair-difference currents. Both retain ampere units.

Return type:

ndarray

summary(print_summary: bool = True) → str[source]

Format and optionally print a summary of the symmetrisation.

Parameters:

print_summary (bool, default=True) – If True, print the summary text to standard output.

Returns:

Formatted summary string.

Return type:

str

symmetrise_even_machine_greens(greens: _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], z_axis: int = -1) → ndarray[source]

Reflection-average Green functions in the reduced machine basis.

Operates on a vectorised Green-function array (such as eq._vgreen) used by the static and evolutive solvers, rather than the named dictionary cache eq._pgreen.

Parameters:
  • greens (ndarray) – Green functions with the reduced even-machine conductor index on axis zero.

  • z_axis (int, default=-1) – Axis corresponding to an exactly symmetric Z grid.

Returns:

Reflection-averaged Green-function array with the same shape as greens.

Return type:

ndarray

symmetrise_even_machine_resistances(resistances: _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) → ndarray[source]

Reflection-average resistances in the reduced even-machine basis.

symmetrise_even_machine_square_operator(operator: _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) → ndarray[source]

Reflection-average an operator in the reduced even-machine basis.

symmetrise_greens(greens: _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], z_axis: int = -1) → ndarray[source]

Average coil Green functions with their reflected partners.

Operates on a vectorised Green-function array (such as eq._vgreen) rather than the named dictionary cache eq._pgreen.

Parameters:
  • greens (ndarray) – Green functions with the original metal-current index on axis zero.

  • z_axis (int, default=-1) – Axis corresponding to an exactly symmetric Z grid.

Returns:

Reflection-averaged Green-function array with the same shape as greens.

Return type:

ndarray

symmetrise_square_operator(operator: _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) → ndarray[source]

Average a full metal-current operator with its reflected counterpart.

This is useful for auditing operators generated before the geometric description itself was made exactly symmetric.

wall_data: list[dict[str, float]] | None
z_shift: float
freegsnke.up_down_symmetry.prepare_up_down_symmetric_machine(active_coils_data: dict[str, Any], passive_coils_data: Sequence[dict[str, Any]] | None = None, limiter_data: Sequence[dict[str, float]] | None = None, wall_data: Sequence[dict[str, float]] | None = None, z_midplane: float | str = 0.0, active_pairs: Sequence[tuple[str, str]] | None = None, passive_pairs: Sequence[tuple[str, str]] | None = None, max_pair_mismatch: float | None = None, exclude_odd_active: bool = False, verbose: bool = True) → PreparedUpDownMachine[source]

Identify pairs and construct an exactly up-down symmetric description.

Pairing is performed independently within structural groups. Names and element metadata are used only to define candidate groups; reflected geometry determines the final one-to-one assignment. Optional explicit pair lists can be supplied for descriptions whose metadata are ambiguous.

Parameters:
  • active_coils_data (dict) – Standard FreeGSNKE active-coil description.

  • passive_coils_data (list, optional) – Standard FreeGSNKE passive-structure description. Outputs carry private refinement-pair metadata so machine construction uses reflected filament samples rather than independent meshes.

  • limiter_data (list, optional) – Standard FreeGSNKE boundary descriptions. Each outline is intersected with its reflection, emitting a UserWarning if its footprint changes (use PreparedUpDownMachine.plot() for a visual comparison).

  • wall_data (list, optional) – Standard FreeGSNKE boundary descriptions. Each outline is intersected with its reflection, emitting a UserWarning if its footprint changes (use PreparedUpDownMachine.plot() for a visual comparison).

  • z_midplane (float or "auto", default=0) – Symmetry-plane coordinate. "auto" fits the common vertical offset from matched machine points, shifts the complete description so that the fitted plane is at Z=0, and then performs exact pair averaging.

  • active_pairs (sequence of pairs, optional) – Explicit (upper_name, lower_name) pairings. When omitted, pairings are inferred from names/metadata and reflected geometry.

  • passive_pairs (sequence of pairs, optional) – Explicit (upper_name, lower_name) pairings. When omitted, pairings are inferred from names/metadata and reflected geometry.

  • max_pair_mismatch (float, optional) – Maximum permitted reflected RMS point mismatch in metres before averaging. The limit applies to active and passive pairs, internally symmetric elements, limiter, and wall. For boundary outlines this is the RMS distance of source vertices to the reflected boundary, without pairing.

  • exclude_odd_active (bool, default=False) – If True, omit active circuits whose reflected winding polarity is odd from the prepared machine and record them in excluded_odd_active_names. By default their presence raises with a complete list, requiring explicit user consent before exclusion.

  • verbose (bool, default=True) – If True, print PreparedUpDownMachine.summary() (retained and excluded active circuits, passive pairs, midplane fit, and per-component discrepancies) before any boundary-change warnings are emitted.

Returns:

Symmetric full and even-active descriptions, reflection maps, current transforms, and auditable pair diagnostics.

Return type:

PreparedUpDownMachine

freegsnke.up_down_symmetry.symmetrise_boundary(boundary_data: Sequence[dict[str, float]], z_midplane: float = 0.0) → list[dict[str, float]][source]

Keep the intersection of a boundary and its reflection about the midplane.

This is the common strategy for limiter and wall outlines. Retain existing segments and introduce vertices only at edge crossings, without pairing or resampling. Emit a UserWarning whenever the footprint changes.

Parameters:
  • boundary_data (sequence of dict) – Ordered closed-polygon vertices with R and Z entries.

  • z_midplane (float, default=0) – Coordinate of the symmetry plane.

Returns:

Symmetric inner boundary, preserving winding and explicit closure.

Return type:

list of dict

Raises:

ValueError – If the input is invalid or the intersection is empty, disconnected, or contains holes.

freegsnke.up_down_symmetry.symmetrise_limiter(limiter_data: Sequence[dict[str, float]], z_midplane: float = 0.0) → list[dict[str, float]][source]

Keep the intersection of a limiter and its reflection about the midplane.

Retain existing boundary segments and add vertices only at edge crossings. Unequal upper/lower vertex counts require no pairing or resampling. An already symmetric polygon is returned unchanged. Otherwise, emit a UserWarning quantifying the removed area and vertex-count change.

Parameters:
  • limiter_data (sequence of dict) – Ordered polygon vertices with R and Z entries.

  • z_midplane (float, default=0) – Coordinate of the symmetry plane.

Returns:

Symmetric inner boundary, preserving winding and explicit closure.

Return type:

list of dict

Raises:

ValueError – If the input is invalid or the intersection is empty, disconnected, or contains holes.