"""
Defines functionality needed for building passive conductive structures.
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/>.
"""
from __future__ import annotations
import logging
import numpy as np
from matplotlib.path import Path
from scipy.stats.qmc import LatinHypercube
logger = logging.getLogger(__name__)
LH_SEED = 42
def _new_engine() -> LatinHypercube:
"""
Fresh, fixed-seed Latin hypercube sampling engine.
A new engine is created per call (rather than sharing one persistent,
stateful module-level engine) so that sampling is reproducible: a shared
engine advances its internal state with every draw, so a build would
get different quasi-random samples depending on how many other
structures/builds had already consumed samples from it earlier in the
same process - i.e. rebuilding the exact same machine description twice
in one process would not give the same resistance/inductance matrices.
Returns
-------
scipy.stats.qmc.LatinHypercube
Two-dimensional sampling engine seeded with ``LH_SEED``.
"""
return LatinHypercube(d=2, seed=LH_SEED)
[docs]
def generate_refinement(
R: np.ndarray, Z: np.ndarray, n_refine: int, refine_mode: str
) -> tuple[np.ndarray, float] | None:
"""
Generate a refined set of points in (R, Z) space using a selected strategy.
The refinement strategy is selected via `refine_mode` and delegates to
the corresponding refinement routine.
Parameters
----------
R : np.ndarray
Radial coordinates of the polygon vertices in m.
Z : np.ndarray
Vertical coordinates of the polygon vertices in m.
n_refine : int
Number of refinement points to generate.
refine_mode : str
Refinement strategy selector:
- "G" : regular grid refinement
- "LH" : Latin Hypercube refinement
Returns
-------
points : np.ndarray
Refined (R, Z) point set, shape (N, 2), produced by the selected method.
area : float
Estimated area of the polygon.
None
Returned instead (after printing a message) if ``refine_mode`` is
not recognised.
"""
if refine_mode == "G":
return generate_refinement_G(R, Z, n_refine)
elif refine_mode == "LH":
return generate_refinement_LH(R, Z, n_refine)
else:
logger.error(
"refine_mode '%s' not recognised!, please use 'G' or 'LH'.", refine_mode
)
raise ValueError(
f"refine_mode '{refine_mode}' not recognised!, please use 'G' or 'LH'."
)
[docs]
def generate_refinement_LH(
R: np.ndarray, Z: np.ndarray, n_refine: int
) -> tuple[np.ndarray, float]:
"""
Generate refinement points inside a polygon using Latin Hypercube sampling.
This method samples candidate points using a Latin Hypercube strategy,
maps them into the bounding box of the polygon defined by (R, Z), and
retains only points lying inside the polygon.
Sampling is repeated until at least `n_refine` valid interior points
are obtained (or a maximum of 100 iterations is reached).
Parameters
----------
R : np.ndarray
R coordinates of the polygon vertices in m.
Z : np.ndarray
Z coordinates of the polygon vertices in m.
n_refine : int
Number of interior refinement points to generate.
Returns
-------
points : np.ndarray, shape (n_refine, 2)
Generated refinement points inside the polygon.
area : float
Estimated area of the polygon region returned by `find_area`.
"""
area, path, vmin, vmax, dv, meanR, meanZ = find_area(R, Z, n_refine)
Len = np.linalg.norm(dv)
engine = _new_engine()
rand_fil = np.zeros((0, 2))
it = 0
while len(rand_fil) < n_refine and it < 100:
vals = engine.random(n=n_refine)
vals = vmin + (vmax - vmin) * vals
rand_fil = np.concatenate((rand_fil, vals[path.contains_points(vals)]), axis=0)
it += 1
return rand_fil[:n_refine], area
[docs]
def generate_refinement_G(
R: np.ndarray, Z: np.ndarray, n_refine: int
) -> tuple[np.ndarray, float]:
"""
Generate a structured grid refinement inside a polygon.
This method constructs a regular Cartesian grid over the bounding box
of the polygon defined by (R, Z), and retains only the points that lie
inside the polygon. The grid resolution is increased iteratively until
at least `n_refine` interior points are obtained.
Parameters
----------
R : np.ndarray
R coordinates of the polygon vertices in m.
Z : np.ndarray
Z coordinates of the polygon vertices in m.
n_refine : int
Target number of refinement points.
Returns
-------
points : np.ndarray, shape (N, 2)
Refinement points inside the polygon (N ≥ n_refine).
area : float
Estimated area of the polygon region returned by `find_area`.
"""
area, path, vmin, vmax, dv, meanR, meanZ = find_area(R, Z, n_refine)
dl = (area / n_refine) ** 0.5
nx = int(dv[0] // dl)
ny = int(dv[1] // dl)
grid_fil = []
while len(grid_fil) < n_refine:
if nx > 1:
x = np.linspace(vmin[0] * 1.00001, vmax[0] * 0.99999, nx)
else:
x = np.mean(R)
if ny > 1:
y = np.linspace(vmin[1] * 1.00001, vmax[1] * 0.99999, ny)
else:
y = np.mean(R)
xv, yv = np.meshgrid(x, y)
grid_fil = np.concatenate((xv.reshape(-1, 1), yv.reshape(-1, 1)), axis=1)
grid_fil = grid_fil[path.contains_points(grid_fil)]
if nx < ny:
nx += 1
else:
ny += 1
return grid_fil, area
[docs]
def find_area(
R: np.ndarray, Z: np.ndarray, n_refine: int | None
) -> tuple[float, Path, np.ndarray, np.ndarray, np.ndarray, float, float]:
"""
Estimate polygon area and construct a point-in-polygon test path.
This function computes a bounding-box-based Monte Carlo estimate of the
area enclosed by a polygon defined by vertices (R, Z). It also constructs
a `matplotlib.path.Path` object for point-in-polygon queries and estimates
a centroid-like mean of accepted sample points.
Parameters
----------
R : np.ndarray
R coordinates of the polygon vertices in m.
Z : np.ndarray
Z coordinates of the polygon vertices in m.
n_refine : int
Target number of refinement points used to control Monte Carlo
sampling. If None, 100 is used.
Returns
-------
area : float
Estimated area of the polygon.
path : matplotlib.path.Path
Path object for point-in-polygon evaluation.
vmin : np.ndarray
Minimum bounds of the vertex coordinates (R_min, Z_min).
vmax : np.ndarray
Maximum bounds of the vertex coordinates (R_max, Z_max).
dv : np.ndarray
Bounding box size (vmax - vmin).
meanR : float
Mean R coordinate of accepted Monte Carlo samples.
meanZ : float
Mean Z coordinate of accepted Monte Carlo samples.
"""
if n_refine is None:
n_refine = 100
verts = np.concatenate(
(
np.array(R)[:, np.newaxis],
np.array(Z)[:, np.newaxis],
),
axis=-1,
)
path = Path(verts)
vmin = np.min(verts, axis=0)
vmax = np.max(verts, axis=0)
dv = vmax - vmin
area = dv[0] * dv[1]
engine = _new_engine()
accepted = 0
mult = 10
while accepted < 10 * n_refine and mult < 1e6:
mult *= 10
vals = engine.random(n=int(mult * n_refine))
vals = vmin + (vmax - vmin) * vals
mask = path.contains_points(vals)
accepted = np.sum(mask)
area *= accepted / (mult * n_refine)
meanR, meanZ = np.mean(vals[mask], axis=0)
return area, path, vmin, vmax, dv, meanR, meanZ