Source code for freegsnke.equilibrium_update

"""
Defines the FreeGSNKE equilibrium object, extending the native base equilibrium.

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 os
import pickle

import matplotlib.pyplot as plt
import numpy as np
from scipy import interpolate

from . import critical, equilibrium, limiter_func
from .build_machine import copy_tokamak
from .copying import copy_into

logger = logging.getLogger(__name__)


[docs] class Equilibrium(equilibrium.Equilibrium): """FreeGSNKE equilibrium object, extending the native equilibrium. Adds limiter handling, a self-checking plasma-flux interpolator, in-place machine-description updates, lightweight auxiliary copies, and optional initialisation of the plasma flux from a file given by the ``EQUILIBRIUM_PATH`` environment variable. Notes ----- Code that replaces ``plasma_psi`` should use :meth:`set_plasma_psi` so the interpolator used by :meth:`psi_func` (and hence ``psiRZ``, ``Br``, ``Bz`` and related diagnostics) stays consistent with the flux array. """
[docs] def __init__(self, *args: object, **kwargs: object) -> None: """Initialise the equilibrium. Parameters ---------- *args, **kwargs Passed to ``freegsnke.equilibrium.Equilibrium.__init__``, e.g. ``tokamak``, ``Rmin``, ``Rmax``, ``Zmin``, ``Zmax``, ``nx``, ``ny``, ``boundary``, ``psi``, ``current``. Notes ----- If the ``EQUILIBRIUM_PATH`` environment variable is set, the initial plasma flux is loaded from that file via :meth:`initialize_from_equilibrium`. The equilibrium is marked as not solved (``self.solved = False``) and the limiter handler and inside/outside-limiter masks are built from ``tokamak.limiter``. """ super().__init__(*args, **kwargs) self.equilibrium_path = os.environ.get("EQUILIBRIUM_PATH", None) if self.equilibrium_path is not None: self.initialize_from_equilibrium() # Retain the checked interpolator after base-class initialisation. self._refresh_plasma_psi_interpolator() self.nxh = len(self.R) // 2 self.nyh = len(self.Z[0]) // 2 self.Rnxh = self.R[self.nxh, 0] self.Znyh = self.Z[0, self.nyh] # It's not a GS solution: self.solved = False # set up for limiter functionality self.limiter_handler = limiter_func.Limiter_handler(self, self.tokamak.limiter) self.mask_inside_limiter = 1.0 * self.limiter_handler.mask_inside_limiter # the factor 2 is needed by critical routines self.mask_outside_limiter = 2 * np.logical_not(self.mask_inside_limiter).astype( float )
def _updatePlasmaPsi(self, plasma_psi: np.ndarray) -> None: """Update plasma flux while retaining the checked FreeGSNKE interpolator. The base-class implementation stores the spline as an instance attribute ``psi_func``, which would hide this class's :meth:`psi_func` method. The spline is moved to ``psi_func_interp`` instead. Parameters ---------- plasma_psi : np.ndarray Plasma poloidal flux on the (R, Z) grid, shape ``(nx, ny)`` [Webers/2pi]. Notes ----- Modifies ``plasma_psi``, ``psi_func_interp``, ``psi_axis``, ``psi_bndry``, ``mask`` and ``mask_func`` in place. The base class recomputes the critical points and boundary flux without limiter handling. Use :meth:`set_plasma_psi` to replace the flux without touching the topology. """ super()._updatePlasmaPsi(plasma_psi) self.psi_func_interp = self.__dict__.pop("psi_func") def _refresh_plasma_psi_interpolator(self) -> None: """Rebuild the plasma-flux spline without recalculating plasma topology. Sets ``self.psi_func_interp`` to a ``RectBivariateSpline`` of the current ``self.plasma_psi``. """ self.psi_func_interp = interpolate.RectBivariateSpline( self.R[:, 0], self.Z[0, :], self.plasma_psi )
[docs] def set_plasma_psi(self, plasma_psi: np.ndarray) -> None: """Install plasma flux and synchronise its interpolation cache. Unlike ``_updatePlasmaPsi``, this lightweight update does not recalculate critical points, masks, or boundary flux. Solvers should use it when committing a flux map before updating topology through their existing profile-aware path. Parameters ---------- plasma_psi : array_like Plasma poloidal flux on the (R, Z) grid, shape ``(nx, ny)`` [Webers/2pi]. A copy is stored, so later changes to the caller's array do not affect the equilibrium. """ self.plasma_psi = np.array(plasma_psi, copy=True) self._refresh_plasma_psi_interpolator()
[docs] def update_machine_description( self, active_coils_data: dict | None = None, passive_coils_data: list | None = None, limiter_data: list | None = None, wall_data: list | None = None, magnetic_probe_data: dict | None = None, active_coils_path: str | None = None, passive_coils_path: str | None = None, limiter_path: str | None = None, wall_path: str | None = None, magnetic_probe_path: str | None = None, refine_mode: str = "G", preserve_currents: bool = True, *, strict_overlap_check: bool = False, ) -> Equilibrium: """ Update the equilibrium's tokamak directly and refresh machine-dependent caches. The computational domain, plasma current, and plasma flux are left in place. Coil Greens functions, tokamak flux, limiter masks, and probe descriptions are rebuilt for the updated machine. Parameters ---------- active_coils_data : dict, optional Dictionary containing the active coil description. passive_coils_data : list, optional List containing passive structure descriptions. If omitted, no passive structures are added. limiter_data : list, optional List of limiter boundary points. wall_data : list, optional List of wall boundary points. If omitted, the limiter is used as the wall. magnetic_probe_data : dict, optional Dictionary containing magnetic probe descriptions. active_coils_path : str, optional Path to a pickle file containing the active coil description. passive_coils_path : str, optional Path to a pickle file containing passive structure descriptions. limiter_path : str, optional Path to a pickle file containing limiter boundary points. wall_path : str, optional Path to a pickle file containing wall boundary points. magnetic_probe_path : str, optional Path to a pickle file containing magnetic probe descriptions. refine_mode : str, optional Refinement mode for extended passive structures. Defaults to ``"G"``. preserve_currents : bool, optional If True, currents for labels that are present in both the old and new machine descriptions are copied onto the updated coil objects. strict_overlap_check : bool, optional If True, reject overlaps with ValueError. By default, warn and continue. Invalid geometry always raises. Returns ------- Equilibrium This equilibrium object, updated in place. Notes ----- This method refreshes equilibrium-level data derived from the tokamak, but it does not update existing solver objects. Reinstantiate static or nonlinear solvers after changing machine geometry. """ self.tokamak.set_machine_description( active_coils_data=active_coils_data, passive_coils_data=passive_coils_data, limiter_data=limiter_data, wall_data=wall_data, magnetic_probe_data=magnetic_probe_data, active_coils_path=active_coils_path, passive_coils_path=passive_coils_path, limiter_path=limiter_path, wall_path=wall_path, magnetic_probe_path=magnetic_probe_path, refine_mode=refine_mode, preserve_currents=preserve_currents, strict_overlap_check=strict_overlap_check, ) self.refresh_machine_dependent_state() return self
[docs] def update_active_coil( self, coil_name: str, active_coil_data: dict, preserve_current: bool = True, *, strict_overlap_check: bool = False, ) -> Equilibrium: """ Update one active coil/circuit and refresh equilibrium-level coil caches. Parameters ---------- coil_name : str Existing active coil/circuit label to replace. active_coil_data : dict Machine-description entry for ``coil_name``. preserve_current : bool, optional If True, the old current on ``coil_name`` is copied onto the replacement coil/circuit. Defaults to True. strict_overlap_check : bool, optional If True, reject overlaps with ValueError. By default, warn and continue. Invalid geometry always raises. Returns ------- Equilibrium This equilibrium object, updated in place. Notes ----- This method refreshes only the Greens functions affected by the updated coil when the coil ordering is unchanged. Limiter masks are reused because the limiter geometry is not modified by an active-coil update. Existing solver objects should still be reinstantiated after a geometry change because they cache machine-dependent matrices and mode data. """ self.tokamak.update_active_coil( coil_name=coil_name, active_coil_data=active_coil_data, preserve_current=preserve_current, strict_overlap_check=strict_overlap_check, ) self.refresh_machine_dependent_state(refresh_limiter=False) return self
[docs] def add_active_coil( self, coil_name: str, active_coil_data: dict, *, strict_overlap_check: bool = False, ) -> Equilibrium: """ Add one new active coil/circuit and extend equilibrium-level coil caches. Parameters ---------- coil_name : str Label for the new active coil/circuit. active_coil_data : dict Machine-description entry for the new active coil/circuit. strict_overlap_check : bool, optional If True, reject overlaps with ValueError. By default, warn and continue. Invalid geometry always raises. Returns ------- Equilibrium This equilibrium object, updated in place. Notes ----- Only the Greens functions for the new coil are calculated; existing coil Greens functions and the limiter handler are reused. Existing solver objects should still be reinstantiated after a geometry change because they cache machine-dependent matrices and mode data, whose dimensionality will itself have changed. """ self.tokamak.add_active_coil( coil_name=coil_name, active_coil_data=active_coil_data, strict_overlap_check=strict_overlap_check, ) insert_index = self.tokamak.coil_order[coil_name] coil = self.tokamak[coil_name] self._pgreen = dict(self._pgreen) self._pgreen[coil_name] = coil.createPsiGreens(self.R, self.Z) new_vgreen_row = coil.createPsiGreensVec(self.R, self.Z)[np.newaxis, ...] self._vgreen = np.concatenate( [self._vgreen[:insert_index], new_vgreen_row, self._vgreen[insert_index:]], axis=0, ) self.tokamak_psi = self.tokamak.calcPsiFromGreens(pgreen=self._pgreen) return self
[docs] def remove_active_coil(self, coil_name: str) -> Equilibrium: """ Remove one active coil/circuit and shrink equilibrium-level coil caches. Parameters ---------- coil_name : str Existing active coil/circuit label to remove. Returns ------- Equilibrium This equilibrium object, updated in place. Raises ------ ValueError If the tokamak does not contain an active coil with that label. Notes ----- Greens functions for the remaining coils and the limiter handler are reused. Existing solver objects should still be reinstantiated after a geometry change because they cache machine-dependent matrices and mode data, whose dimensionality will itself have changed. """ if coil_name not in self.tokamak.coil_order: raise ValueError( f"Tokamak does not contain active coil label '{coil_name}'." ) index = self.tokamak.coil_order[coil_name] self.tokamak.remove_active_coil(coil_name) self._pgreen = {k: v for k, v in self._pgreen.items() if k != coil_name} self._vgreen = np.delete(self._vgreen, index, axis=0) self.tokamak_psi = self.tokamak.calcPsiFromGreens(pgreen=self._pgreen) return self
[docs] def update_passive_structure( self, name: str, passive_data: dict, preserve_current: bool = True, refine_mode: str = "G", *, strict_overlap_check: bool = False, ) -> Equilibrium: """ Update one passive structure and refresh equilibrium-level coil caches. Parameters ---------- name : str Existing passive-structure label to replace. passive_data : dict Machine-description entry for ``name``. preserve_current : bool, optional If True, the old current on ``name`` is copied onto the replacement passive structure. Defaults to True. refine_mode : str, optional Refinement mode for extended (polygonal) passive structures. Defaults to ``"G"``. strict_overlap_check : bool, optional If True, reject overlaps with ValueError. By default, warn and continue. Invalid geometry always raises. Returns ------- Equilibrium This equilibrium object, updated in place. Notes ----- This method refreshes only the Greens functions affected by the updated passive structure. Limiter masks are reused because the limiter geometry is not modified by a passive-structure update. Existing solver objects should still be reinstantiated after a geometry change because they cache machine-dependent matrices and mode data. """ self.tokamak.update_passive_structure( name=name, passive_data=passive_data, preserve_current=preserve_current, refine_mode=refine_mode, strict_overlap_check=strict_overlap_check, ) self.refresh_machine_dependent_state(refresh_limiter=False) return self
[docs] def add_passive_structure( self, passive_data: dict, name: str | None = None, refine_mode: str = "G", *, strict_overlap_check: bool = False, ) -> Equilibrium: """ Add one new passive structure and extend equilibrium-level coil caches. Parameters ---------- passive_data : dict Machine-description entry for the new passive structure. name : str, optional Label for the new passive structure. If omitted, ``passive_data["name"]`` is used, and if that is also absent a default of the form ``f"passive_{self.tokamak.n_passive_coils}"`` is used. refine_mode : str, optional Refinement mode for extended (polygonal) passive structures. Defaults to ``"G"``. strict_overlap_check : bool, optional If True, reject overlaps with ValueError. By default, warn and continue. Invalid geometry always raises. Returns ------- Equilibrium This equilibrium object, updated in place. Notes ----- Only the Greens functions for the new passive structure are calculated; existing coil Greens functions and the limiter handler are reused. Existing solver objects should still be reinstantiated after a geometry change because they cache machine-dependent matrices and mode data, whose dimensionality will itself have changed. """ self.tokamak.add_passive_structure( passive_data=passive_data, name=name, refine_mode=refine_mode, strict_overlap_check=strict_overlap_check, ) added_name = self.tokamak.coils_list[-1] coil = self.tokamak[added_name] self._pgreen = dict(self._pgreen) self._pgreen[added_name] = coil.createPsiGreens(self.R, self.Z) new_vgreen_row = coil.createPsiGreensVec(self.R, self.Z)[np.newaxis, ...] self._vgreen = np.concatenate([self._vgreen, new_vgreen_row], axis=0) self.tokamak_psi = self.tokamak.calcPsiFromGreens(pgreen=self._pgreen) return self
[docs] def remove_passive_structure(self, name: str) -> Equilibrium: """ Remove one passive structure and shrink equilibrium-level coil caches. Parameters ---------- name : str Existing passive-structure label to remove. Returns ------- Equilibrium This equilibrium object, updated in place. Raises ------ ValueError If the tokamak does not contain a passive structure with that label. Notes ----- Greens functions for the remaining coils and the limiter handler are reused. Existing solver objects should still be reinstantiated after a geometry change because they cache machine-dependent matrices and mode data, whose dimensionality will itself have changed. """ if name not in self.tokamak.coil_order: raise ValueError( f"Tokamak does not contain passive structure label '{name}'." ) index = self.tokamak.coil_order[name] self.tokamak.remove_passive_structure(name) self._pgreen = {k: v for k, v in self._pgreen.items() if k != name} self._vgreen = np.delete(self._vgreen, index, axis=0) self.tokamak_psi = self.tokamak.calcPsiFromGreens(pgreen=self._pgreen) return self
[docs] def refresh_machine_dependent_state( self, refresh_limiter: bool = True ) -> Equilibrium: """ Refresh cached data derived from the current tokamak description. Rebuilds the coil Greens functions on this equilibrium grid for changed coil labels only when possible, updates the tokamak flux from the refreshed Greens functions and current vector, and optionally recreates the limiter handler and inside/outside-limiter masks. A full Greens rebuild is used when the coil ordering or number of coils changed, or when no previous cached Greens functions are available. Parameters ---------- refresh_limiter : bool, optional If True, rebuild the limiter handler and limiter masks. Set to False for coil-only updates that leave limiter geometry unchanged. Returns ------- Equilibrium This equilibrium object, refreshed in place. """ changed_coils = getattr( self.tokamak, "_last_machine_update_changed_coils", None ) topology_changed = getattr( self.tokamak, "_last_machine_update_topology_changed", True ) can_update_partially = ( not topology_changed and changed_coils is not None and hasattr(self, "_pgreen") and hasattr(self, "_vgreen") and np.shape(self._vgreen)[0] == self.tokamak.n_coils ) if can_update_partially: self._pgreen = self._pgreen.copy() self._vgreen = np.copy(self._vgreen) for label in changed_coils: if label not in self.tokamak.coil_order: continue coil = self.tokamak[label] self._pgreen[label] = coil.createPsiGreens(self.R, self.Z) self._vgreen[self.tokamak.coil_order[label]] = coil.createPsiGreensVec( self.R, self.Z ) else: self._pgreen = self.tokamak.createPsiGreens(self.R, self.Z) self._vgreen = self.tokamak.createPsiGreensVec(self.R, self.Z) self.tokamak_psi = self.tokamak.calcPsiFromGreens(pgreen=self._pgreen) if refresh_limiter: self.limiter_handler = limiter_func.Limiter_handler( self, self.tokamak.limiter ) self.mask_inside_limiter = 1.0 * self.limiter_handler.mask_inside_limiter self.mask_outside_limiter = 2 * np.logical_not( self.mask_inside_limiter ).astype(float) return self
[docs] def create_auxiliary_equilibrium(self) -> Equilibrium: """Creates the auxiliary equilibrium object. The auxiliary object returned from this method is essentially a copy of the equilibrium object (self) however it is manually setup and so won't contain all attributes on self (especially custom attributes). It is NOT _guaranteed_ to be the same as a deepcopy, or even a shallow copy. Returns ------- Equilibrium New equilibrium object, created without calling ``__init__``. Grids, fluxes, masks, Greens functions and critical-point data are copied; the tokamak is copied with ``copy_tokamak``; the limiter handler and the plasma-flux interpolator are shared with ``self``. """ # __new__ stops __init__ being called. # This is necessary because the __init__ method does expensive # calculations which we can just copy the results of equilibrium = Equilibrium.__new__(Equilibrium) # Attributes set by the base equilibrium class. equilibrium.tokamak = copy_tokamak(self.tokamak) equilibrium.Rmin = self.Rmin equilibrium.Rmax = self.Rmax equilibrium.Zmin = self.Zmin equilibrium.Zmax = self.Zmax equilibrium.nx = self.nx equilibrium.ny = self.ny equilibrium.dR = self.dR equilibrium.dZ = self.dZ equilibrium._applyBoundary = self._applyBoundary equilibrium._current = self._current self._copy_solver_state_to(equilibrium) # attributes the FreeGSNKE sets equilibrium.solved = self.solved equilibrium.psi_func_interp = self.psi_func_interp equilibrium.nxh = self.nxh equilibrium.nyh = self.nyh equilibrium.Rnxh = self.Rnxh equilibrium.Znyh = self.Znyh equilibrium.limiter_handler = self.limiter_handler # should be safe not to copy # attributes that actually need to be copied equilibrium.R_1D = np.copy(self.R_1D) equilibrium.Z_1D = np.copy(self.Z_1D) equilibrium.R = np.copy(self.R) equilibrium.Z = np.copy(self.Z) equilibrium.tokamak_psi = np.copy(self.tokamak_psi) equilibrium.plasma_psi = np.copy(self.plasma_psi) equilibrium.psi_axis = np.copy(self.psi_axis) equilibrium.psi_bndry = np.copy(self.psi_bndry) equilibrium.mask_inside_limiter = np.copy(self.mask_inside_limiter) equilibrium.mask_outside_limiter = np.copy(self.mask_outside_limiter) equilibrium._pgreen = self._pgreen.copy() equilibrium._vgreen = self._vgreen.copy() copy_into( self, equilibrium, "flag_limiter", mutable=True, strict=False, allow_deepcopy=True, ) copy_into(self, equilibrium, "has_relevant_xpoint", strict=False) copy_into(self, equilibrium, "current_vec", mutable=True, strict=False) copy_into( self, equilibrium, "opt", mutable=True, strict=False, allow_deepcopy=True ) copy_into( self, equilibrium, "xpt", mutable=True, strict=False, allow_deepcopy=True ) # copy_into(self, equilibrium, "psi_bndry", strict=False) if hasattr(self, "_profiles"): equilibrium._profiles = self._profiles.copy() return equilibrium
[docs] def plot( self, axis: plt.Axes | None = None, xpoints: bool = True, opoints: bool = True, wall: bool = True, limiter: bool = True, legend: bool = False, show: bool = True, ) -> plt.Axes: """Plot a solved FreeGSNKE equilibrium. This overrides the base plotting wrapper so limited equilibria without a relevant X-point in the solution domain can still be plotted. In that case the LCFS is drawn from ``psi_bndry`` and no primary X-point separatrix is requested. Parameters ---------- axis : matplotlib.axes.Axes, optional Axes to draw on. If None, a new figure and axes are created. xpoints : bool, optional If True, plot the X-points (only when a relevant X-point exists). Defaults to True. opoints : bool, optional If True, plot the O-points. Defaults to True. wall : bool, optional If True, plot the wall (when the tokamak has one). Defaults to True. limiter : bool, optional If True, plot the limiter (when the tokamak has one). Defaults to True. legend : bool, optional If True, add a legend. Defaults to False. show : bool, optional If True, call ``plt.show()`` after plotting. Defaults to True. Returns ------- matplotlib.axes.Axes The axes the equilibrium was drawn on. Raises ------ RuntimeError If the equilibrium has not been solved (no ``_profiles`` with critical-point data). """ try: psi = self.psi() opt = self._profiles.opt xpt = self._profiles.xpt except AttributeError as e: raise RuntimeError( "This equilibrium has not been solved: please solve for an " "equilibrium first!" ) from e has_relevant_xpoint = getattr( self, "has_relevant_xpoint", getattr(self._profiles, "has_relevant_xpoint", len(xpt) > 0), ) if axis is None: fig = plt.figure() axis = fig.add_subplot(111) axis.set_aspect("equal") axis.set_xlabel("Major radius [m]") axis.set_ylabel("Height [m]") levels = np.linspace(np.amin(psi), np.amax(psi), 35) axis.contour(self.R, self.Z, psi, levels=levels) if has_relevant_xpoint: colour = "r" style = "solid" axis.contour( self.R, self.Z, psi, levels=[xpt[0][2]], colors=colour, linestyles=style, ) axis.plot( [], [], colour, label="Separatrix (primary X-point)", linestyle=style, ) if self._profiles.flag_limiter: colour = "k" style = "dashed" axis.contour( self.R, self.Z, psi, levels=[self.psi_bndry], colors=colour, linestyles=style, ) axis.plot([], [], colour, label="LCFS (limited plasma)", linestyle=style) if xpoints and has_relevant_xpoint: for r, z, _ in xpt: axis.plot(r, z, "rx", markersize=9) axis.plot(xpt[0][0], xpt[0][1], "rx", markersize=9, markeredgewidth=2.5) axis.plot([], [], "rx", markersize=9, label="X-points") axis.plot( [], [], "rx", markersize=9, markeredgewidth=2.5, label="X-point (primary)", ) if opoints: for r, z, _ in opt: axis.plot(r, z, "g2", markersize=9) axis.plot([], [], "g2", markersize=9, label="O-points") if wall and self.tokamak.wall and len(self.tokamak.wall.R): axis.plot( list(self.tokamak.wall.R) + [self.tokamak.wall.R[0]], list(self.tokamak.wall.Z) + [self.tokamak.wall.Z[0]], "k", ) if limiter and self.tokamak.limiter and len(self.tokamak.limiter.R): axis.plot( list(self.tokamak.limiter.R) + [self.tokamak.limiter.R[0]], list(self.tokamak.limiter.Z) + [self.tokamak.limiter.Z[0]], "k:", ) if legend: axis.legend(loc="upper right") if show: plt.show() return axis
[docs] def adjust_psi_plasma(self) -> None: """Operates an initial rescaling of the psi_plasma guess so to ensure a viable O-point and at least an X-point within the domain. Only use after appropriate coil currents have been set as desired! The guess is first scaled up (by factors of 1.5) until an O-point is found inside the limiter, then made more compact by exponentiation until an X-point appears, and finally scaled up (by factors of 1.15) to enlarge the diverted core. Notes ----- Modifies ``tokamak_psi``, ``plasma_psi``, ``psi_func_interp``, ``gmod`` and ``gexp`` in place. Failures are reported by printing, not by raising. If no O-point can be generated, or exponentiation removes the O-point, the method returns early and leaves ``plasma_psi`` at its last scaled value without refreshing the interpolator. If exponentiation completes without producing an X-point, the last exponentiated trial is still installed. """ self.tokamak_psi = self.tokamak.calcPsiFromGreens(pgreen=self._pgreen) n_up = 0 self.gmod = 0 self.gexp = 2 opoint_flag = False while (n_up < 10) and (opoint_flag == False): try: # Analyse the equilibrium, finding O- and X-points opt, xpt = critical.find_critical( self.R, self.Z, self.tokamak_psi + self.plasma_psi, self.mask_inside_limiter, None, ) opoint_flag = True except: self.plasma_psi *= 1.5 self.gmod += np.log(1.5) n_up += 1 if opoint_flag == False: logger.warning( "O-point could not be generated by simply scaling up psi_plasma. Manual initialization advised." ) return # O-point is in place xpoint_flag = len(xpt) > 0 logger.info("O-point is in place. Flag for X-point in place = %s", xpoint_flag) n_plasma_psi = self.plasma_psi.copy() n_exp = 0 if (xpoint_flag == False) and (n_exp < 10): # if it didn't work, try by making psi more compact psi_max = np.amax(self.plasma_psi) e_plasma_psi = self.plasma_psi / psi_max while (xpoint_flag == False) and (n_exp < 10): n_exp += 1 n_plasma_psi = psi_max * e_plasma_psi ** (n_exp * 1.1) try: opt, xpt = critical.find_critical( self.R, self.Z, self.tokamak_psi + n_plasma_psi, self.mask_inside_limiter, None, ) xpoint_flag = len(xpt) > 0 self.gmod *= 1.1 except: # here if exponentiation causes the o-point to disappear logger.warning( "Failed to introduce an xpoint on the domain by exponentiating psi_plasma. Manual initialization advised." ) return # assign from exponentiation if successful if xpoint_flag == False: logger.warning( "Failed to introduce an xpoint on the domain by exponentiating psi_plasma. Manual initialization advised." ) else: self.plasma_psi = n_plasma_psi.copy() n_up = 0 # try to increase the size of the diverted mask diverted_core_mask = critical.inside_mask( self.R, self.Z, self.tokamak_psi + n_plasma_psi, opt, xpt, ) limiter_size = np.sum(self.mask_inside_limiter) diverted_size = np.sum(diverted_core_mask) logger.info( "Size of the diverted core in number of domain pts = %s", diverted_size ) diverted_flag = diverted_size > 0.5 * limiter_size while diverted_flag == False and n_up < 6: # try: opt, xpt = critical.find_critical( self.R, self.Z, self.tokamak_psi + n_plasma_psi * 1.1, self.mask_inside_limiter, None, ) xpoint_flag = len(xpt) > 0 if xpoint_flag: n_plasma_psi *= 1.15 self.gmod += np.log(1.15) n_up += 1 diverted_core_mask = critical.inside_mask( self.R, self.Z, self.tokamak_psi + n_plasma_psi, opt, xpt, ) diverted_size = np.sum(diverted_core_mask) logger.debug("diverted_size: %s", diverted_size) # except: # diverted_flag = True self.set_plasma_psi(n_plasma_psi)
[docs] def psi_func( self, R: np.ndarray, Z: np.ndarray, *args: object, **kwargs: object ) -> np.ndarray: """Scipy interpolation of plasma_psi function. Replaces the original FreeGS interpolation. It now includes a check which leads to the update of the interpolation when needed. Parameters ---------- R : ndarray R coordinates where the interpolation is needed Z : ndarray Z coordinates where the interpolation is needed *args, **kwargs Passed to ``scipy.interpolate.RectBivariateSpline.__call__`` (e.g. ``dx``, ``dy``, ``grid``). Returns ------- ndarray Interpolated values of plasma_psi Notes ----- The staleness check compares the spline and ``plasma_psi`` only at the grid centre, so it can miss changes made elsewhere. Use :meth:`set_plasma_psi` to replace the flux. """ check = ( np.abs( np.max(self.psi_func_interp(self.Rnxh, self.Znyh)) - self.plasma_psi[self.nxh, self.nyh] ) > 1e-5 ) if check: logger.debug( "Discrepancy between psi_func and plasma_psi detected. psi_func has been reset." ) self._refresh_plasma_psi_interpolator() return self.psi_func_interp(R, Z, *args, **kwargs)
[docs] def initialize_from_equilibrium(self) -> None: """ This function loads a pickle file containing an initial guess for the plasma flux (and the corners of the grid points it is located on). Interpolation is carried out and mapped to the computational grid specified in the eq class. The file is read from ``self.equilibrium_path`` (set from the ``EQUILIBRIUM_PATH`` environment variable) and must contain a dict with keys ``"Rmin"``, ``"Rmax"``, ``"Zmin"``, ``"Zmax"`` and ``"psi_plasma"`` (a 2D array on a uniform grid spanning those limits). Sets ``plasma_psi`` and ``psi_func_interp`` in place. Raises ------ ValueError If the pickle data is missing any of the required keys. """ # load the data from the pickle file with open(self.equilibrium_path, "rb") as f: data = pickle.load(f) # extract the data (will fail if not in this format) try: Rmin = data["Rmin"] Rmax = data["Rmax"] Zmin = data["Zmin"] Zmax = data["Zmax"] psi_plasma = data["psi_plasma"] except: raise ValueError( "Data in EQUILIBRIUM_PATH pickle not in correct format or missing." ) # interpolate the plasma psi on the grid given in the data file plasma_psi_func = interpolate.RectBivariateSpline( np.linspace(Rmin, Rmax, psi_plasma.shape[0]), np.linspace(Zmin, Zmax, psi_plasma.shape[1]), psi_plasma, ) # extract the values on the grid given in the eq object (this is the initial guess) self.set_plasma_psi(plasma_psi_func(self.R, self.Z, grid=False)) logger.info( "Initial guess for plasma flux initialised using file provided at EQUILIBRIUM_PATH." )