gbspy package

GBS Python Toolbox

This package contains a set of routines to load, visualize and post-process data from GBS simulations.

class gbspy.Sim(path=None)

Bases: object

The Sim class provides an interface for opening GBS results.

Attributes of the simulation (mostly input parameters such as nu, dt, etc.) are loaded in the sim.attribute dictionnary.

Parameters

path (str, Path or path-like, optional) – Path to be opened as a GBS simulation. Can either be a single results file, or an entire directory containing many results files. If not provided, defaults to the current working directory.

Examples

This loads a simulation by visiting the folder where it is saved:

>>> %cd /path/containing/the/simulation
>>> import gbspy
>>> sim = gbspy.Sim()
>>> sim.attribute['dt']  # show the timestep
FIELD_LIST = {'SnD2_nD2cxD2': ('$n_{D}\nu_{cx,D_2^+-D}$', ('neutral_density', 'thetaD2', 'vo_cx_D_D2p'), <function Sim.<lambda>>), 'SnD2_nD2cxD2Dp': ('$n_{D_2}\nu_{cx,D_2-D^+}$', ('thetaD', 'neutralD2_density', 'vo_cx_D2Dp'), <function Sim.<lambda>>), 'SnD2_nD2pdiD2p': ('$n_{D_2^+}\nu_{di,D_2^+}$', ('theta', 'thetaD2', 'vo_di_D2p'), <function Sim.<lambda>>), 'Sne_iztot': ('$S_{iz,tot}$', ('ionsource', 'Sne_nnD2izD2', 'Sne_nD2pdiizD2p'), <function Sim.<lambda>>), 'Sne_nD2diizD2': ('$n_{D_2}\nu_{di-iz,D_2}$', ('theta', 'neutralD2_density', 'vo_di_iz_D2'), <function Sim.<lambda>>), 'Sne_nD2pdiizD2p': ('$n_{D_2^+}\nu_{di-iz,D_2^+}$', ('theta', 'thetaD2', 'vo_di_iz_D2p'), <function Sim.<lambda>>), 'Sne_nD2pdirecD2p': ('$n_{D_2^+}\nu_{di-rec,D_2^+}$', ('theta', 'thetaD2', 'vo_di_rec_D2p'), <function Sim.<lambda>>), 'Sne_nD2precD2p': ('$n_{D_2^+}\nu_{rec,D_2^+}$', ('theta', 'thetaD2', 'vo_rec_D2'), <function Sim.<lambda>>), 'Sne_nDprecDp': ('$n_{D^+}\nu_{rec,D^+}$', ('theta', 'thetaD', 'vo_rec'), <function Sim.<lambda>>), 'Sne_nnD2izD2': ('$n_{D_2}\nu_{iz,D_2}$', ('theta', 'neutralD2_density', 'vo_iz_D2'), <function Sim.<lambda>>), 'Sne_rectot': ('$S_{rec,tot}$', ('Sne_nDprecDp', 'Sne_nD2pdirecD2p'), <function Sim.<lambda>>), 'TD2': ('$ T_{D_2^+}$', ('tempD2',), <function Sim.<lambda>>), 'Te': ('$T_e$', ('temperature',), <function Sim.<lambda>>), 'Ti': ('$T_i$', ('temperaturi',), <function Sim.<lambda>>), 'gammaD2': ('$ \\Gamma_{D_2^+}$', ('thetaD2', 'vparD2'), <function Sim.<lambda>>), 'gammae': ('$ \\Gamma_{e}$', ('theta', 'vpare'), <function Sim.<lambda>>), 'gammai': ('$ \\Gamma_{i}$', ('theta', 'vpari'), <function Sim.<lambda>>), 'ionsource': ('$S_{iz}$', ('theta', 'neutral_density', 'vo_iz'), <function Sim.<lambda>>), 'n': ('$n$', ('theta',), <function Sim.<lambda>>), 'nD': ('$ n_{D^+}$', ('thetaD',), <function Sim.<lambda>>), 'nD2': ('$ n_{D_2^+}$', ('thetaD2',), <function Sim.<lambda>>), 'neutralD2_density': ('$n_{D_2}$', None, None), 'neutralD2_flux_R': ('$\\Gamma_{D_2,R}$', None, None), 'neutralD2_flux_Z': ('$\\Gamma_{D_2,Z}$', None, None), 'neutralD2_temperature': ('$T_{D_2}$', None, None), 'neutralD2_velocity': ('$v_{\\parallel D_2}$', None, None), 'neutral_density': ('$n_n$', None, None), 'neutral_flux_R': ('$\\Gamma_{n,R}$', None, None), 'neutral_flux_Z': ('$\\Gamma_{n,Z}$', None, None), 'neutral_flux_phi': ('$\\Gamma_{n,\\phi}$', None, None), 'neutral_temperature': ('$T_n$', None, None), 'neutral_velocity': ('$v_{\\parallel n}$', None, None), 'omega': ('$\\omega$', None, None), 'pD2': ('$p_{D_2^+}$', ('thetaD2', 'tempD2'), <function Sim.<lambda>>), 'pe': ('$p_e$', ('theta', 'temperature'), <function Sim.<lambda>>), 'pi': ('$p_i$', ('theta', 'temperaturi'), <function Sim.<lambda>>), 'pressurD2': ('$log(p_{D_2^+})$', ('thetaD2', 'tempD2'), <function Sim.<lambda>>), 'psi': ('$\\psi$', None, None), 'strmf': ('$\\phi$', None, None), 'tempD2': ('$\\log(T_{D_2^+})$', None, None), 'temperature': ('$\\log(T_e)$', None, None), 'temperaturi': ('$\\log(T_i)$', None, None), 'theta': ('$\\log(n)$', None, None), 'thetaD': ('$\\log(n_{D^+})$', None, None), 'thetaD2': ('$\\log(n_{D_2^+})$', None, None), 'vo_cx': ('$vo_{cx}$', None, None), 'vo_cx_D2': ('$vo_{cx,D_2}$', None, None), 'vo_cx_D2_Dp': ('$vo_{cx,D_2-D^+}$', None, None), 'vo_cx_D_D2p': ('$vo_{cx,D-D_2^+}$', None, None), 'vo_di_D2': ('$vo_{diss,D_2}$', None, None), 'vo_di_D2p': ('$vo_{diss,D_2^+}$', None, None), 'vo_di_iz_D2': ('$vo_{diss-iz,D_2}$', None, None), 'vo_di_iz_D2p': ('$vo_{diss-iz,D_2^+}$', None, None), 'vo_di_rec_D2p': ('$vo_{diss-rec,D_2^+}$', None, None), 'vo_en': ('$vo_{en}$', None, None), 'vo_en_D2': ('$vo_{en,D_2}$', None, None), 'vo_iz': ('$vo_{iz}$', None, None), 'vo_iz_D2': ('$vo_{iz,D_2}$', None, None), 'vo_rec': ('$vo_{rec}$', None, None), 'vo_rec_D2': ('$vo_{rec,D_2^+}$', None, None), 'vparD2': ('$v_{\\parallel D_2}$', None, None), 'vpare': ('$v_{\\parallel e}$', None, None), 'vpari': ('$v_{\\parallel i}$', None, None)}
static add_field_id(name, latex='', loaded=None, func=None, overwrite=False)

Defines a loadable field from GBS.

Parameters
  • name (str) – Keyword for the new field. Should be unique and not already defined unless overwrite=True.

  • latex (str, optional) – LaTeX representation of the new field.

  • loaded (tuple of str, optional) – Tuple containing the GBS field names (datasets in the HDF5 results) required to compute the quantity.

  • func (callable, optional) – Function processing the loaded quantities.

  • overwrite (bool, optional) – If True, existing field names will be overwritten.

Examples

The following line could be used to add the electron pressure to the field list (it is already defined in the toolbox)

>>> Sim.add_field_id('pe', '$p_e$', ('theta', 'temperature',),
...                  lambda n, T: np.exp(n*T))
find_ind(val, quant)

Find the index of a value in quant

get_field(which_field, x1=None, x2=None, y1=None, y2=None, z1=None, z2=None, t1=None, t2=None)

Returns the requested simulation field as 4D array

Parameters
  • which_field (str) – String identifying the field to load

  • x1 (float, optional) – The range of values to select in the radial direction. By default, all available points are loaded.

  • x2 (float, optional) – The range of values to select in the radial direction. By default, all available points are loaded.

  • y1 (float, optional) – The range of values to select in the vertical direction. By default, all available points are loaded.

  • y2 (float, optional) – The range of values to select in the vertical direction. By default, all available points are loaded.

  • z1 (float, optional) – The range of values to select in the toroidal direction. By default, all available points are loaded.

  • z2 (float, optional) – The range of values to select in the toroidal direction. By default, all available points are loaded.

static get_field_id(key, what='latex')

Fetches information about a certain field name in GBS.

For example, calling Sim.get_field_id('n', 'latex') returns the string "$n$".

Parameters
  • key (str) – String identifying the GBS field.

  • what (str, optional) – Specify what should be returned. Should be 'latex', 'loaded', 'func' or 'all'.

load_equilibrium()

Loads the simulation’s magnetic equilibrium

Read the magnetic equilibrium from the result files into the simulation object. Note the most recent magnetic configuration is read. This method will not handle equilibria that vary from one restart to another. This function will replace gbspy.utils.load_mag_field().

gbspy.build_grid(s, rmin, rmax, Nchi=200)

Returns the variables Xpsi, Ypsi that contain the flux-aligned grid

Parameters
  • s (gbspy.pp.Sim) – Simulation object

  • rmin (float) – Initial normalized radius of the grid

  • rmax (float) – Final normalized radius of the grid

  • Nchi (int, optional) – Number of points along chi

Returns

  • Xpsi (np.ndarray) – X-coordinate of the new grid as a 2D array

  • Ypsi (np.ndarray) – Y-coordinate of the new grif as a 2D array

  • rpsi (np.ndarray) – Radial position of the flux surfaces as a 1D array

  • chi (np.ndarray) – Coordinate along a flux surface as a 2D array

Examples

Interpolate the density defined on the meshgrid X, Y into a new grid Xnew, Ynew generated by build_grid:

>>> import numpy as np
>>> from scipy.interpolate import griddata
>>> import gbspy as g
>>> s = g.Sim()
>>> n = s.get_field('n')
>>> Xnew, Ynew, _, _ = g.build_grid(s, 0.4, 1)
>>> X, Y = np.meshgrid(s.x, s.y)
>>> nnew = griddata((X.ravel(), Y.ravel()),
...                 n[:,:,0,0].ravel(), (Xnew, Ynew))
class gbspy.set_options(**kwargs)

Bases: object

Set options for gbspy, including in contexts.

Parameters
  • cmap (str or matplotlib.colors.Colormap, default: "viridis") – Colormap to use on two-dimensional plots. If string, must be recognized as a valid colormap by matplotlib.

  • num_contours (int, default: 50) – The number of contours to draw on contour plots.

  • pcolor_shading (str, default: "nearest") – Shading option used for pcolormesh() plots.

Examples

Change the default colormap only in a few plots,

>>> sim = gbspy.Sim("/some/path/to/a/simulation")
>>> with gbspy.options.set_options(cmap="hot"):
...     gbspy.plot2d(sim, "theta")

Subpackages

Submodules