{'fraction_non_alphanumeric': 0.04084307840905171, 'fraction_numerical': 0.014798716754631068, 'mean_word_length': 4.703324808184143, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 2, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Gingred, a general grid generator for 2D edge plasma modeling

The current work reports the development of a new general grid generator called Gingred for arbitrary (e.g., number of X-points) 2D magnetic equilibria and plate geometries. A standardization of the construction of a grid is explained, the main command lines are detailed, and examples of snowflake grids are shown. The main advantages of Gingred are the tracking of the step-by-step progresses (from"skeleton"grids) of grid generations by non-experts and new users, the ability to use an arbitrary magnetic configuration, and the more accurate numerical computation by bi-cubic interpolation of the magnetic flux rather than discrete cosine transform.


Introduction
Understanding and predicting the physics at the edge of tokamaks is one of the keys for enhancing the efficiency of future fusion reactors. Transport fluid codes are used to simulate profiles that depend on the boundary conditions (core, shape of divertor plates and wall) in order to find good geometric configurations that reduce particle and heat fluxes on divertor plates. Possible solutions of the first wall protection against large particle and heat fluxes need to be assessed by simulating particle and power exhausts in the area in front of the divertor plates. To date, one of the most promising solutions to protect the first wall is to use an advanced divertor configuration.
The original divertor (called standard divertor [1]) includes one X-point of the poloidal magnetic field where the primary separatrix is the magnetic flux surface which separates the closed (core) and the open (scrape-off layer, SOL) magnetic flux surfaces that hit the first walls at the divertor plates. The standard divertor has been extended to include additional secondary X-points (advanced divertor) close to the primary X-point. The presence of an additional secondary X-point can also reduce fluxes on divertor plates thanks to the parallel connection lengths, the flux expansions or the impurity radiations. These advanced divertor configurations have been called in the literature X [2], super-X [3], snowflake-minus, snowflake-plus [4] or cloverleaf [5] (with two secondary X-points). These configurations are related to significant different physics phenomena but from the grid generation point of view, all of them are based on the unified idea of adding additional Xpoints. The secondary X-point can be relatively far away or close to the primary X-point in order to create a large area where the poloidal magnetic field is reduced (allowing different radiation and transport regimes in that area). Another possible effect is an increase of the poloidal flux expansion before the divertor plates (spreading the particle and power fluxes). Indeed, by focusing on the geometry we remark that the advanced divertor configurations include a secondary X-point that can be located far or close to the primary separatrix (i.e., X versus snowflake), inside or outside the SOL (i.e., snowflake-minus versus snowflake-plus), and before or after the plates (i.e., super-X versus snowflake). All these cases can be investigated if one is able to construct arbitrary grids. In this paper, the focus is on the advanced divertor geometry with only one additional X-point, called here by simplification the snowflake divertor (regardless of the leg length, the position of the secondary X-point with respect to the plate or the primary separatrix). It is straightforward to include more X-points following our bottom-up approach used in the code. The first experiment with a snowflake divertor has been successfully performed on TCV [6], followed by NSTX [7] and DIII-D [8]. It turns out that there is a large number of free parameters (e.g., position of the secondary X-point, geometry of the plates, distance between the X-points, distance between the X-points and the plates). Therefore, we must run many numerical simulations of the divertor plasmas in order to find the best configurations that reduce the fluxes at the plates without reducing the core performances. Continuous or particle based kinetic numerical codes (e.g., COGENT [9], XGC1 [10]) are under development for simulating divertor plasmas and have a large CPU-consumption, but fluid codes with smaller CPU-consumption have been developed and validated against experiments. Currently, the stateof-the-art transport fluid codes are SOLPS-Iter [11] (recent choice of Iter Organisation, not benchmarked yet), EMC3 [12] (3D but no charge-state resolved impurity model) and UEDGE [13] (2D with a possible charge-state resolved impurity model). All fluid codes require a grid, i.e., the mesh on which the fluid equations are computed. Moreover, other fluid codes that focus on turbulence (e.g., BOUT++ [14]) use very similar grids. Even the next generation of fluid codes that will include non-Maxwellian kinetic effects [15,16] would be able to use those grids. It is then required to be able to manage the creation of arbitrary grids from arbitrary magnetic configuration for arbitrary codes. We notice that UEDGE has the capability to internally generate the grid which but that has been initially developed for single-and double-null magnetic geometries. The spatial indexing scheme that allows the various plasma edge configurations to be represented on a logically rectangular mesh was developed previously by many groups [17,18,19] for single-and double-null magnetic configurations. According to our knowledge, the first snowflake logical maps for some snowflake configurations have been developed by Rensink [20] and is illustrated in Appendix A. 1 However, the UEDGE internal grid generator is not adapted for the unified step-bystep bottom-up development of the grid that is crucial for debugging. There exist other grid generators: BOUT++ uses Hypnotoad [14] and SOLPS-Iter uses CARRE [21] which both have a graphical user interface (IDL widgets for Hypnotoad and the DG code for CARRE), but to date Hypnotoad and CARRE do not include the step-by-step construction of the grids and do not manage snowflake grids. For all of these reasons, our interest is to develop a new universal code toward the development of arbitrary grids, independent of all fluid code, and easily modifiable by new users following a robust and simple (i.e., small number of command lines) step-by-step workflow. We develop here the IDL-based Gingred code for grid generation that is robust, universal, sequential and easily manageable by new users with respect to the main existing grid generators thanks to new algorithms and the bottom-up approach. Indeed, Gingred works with a few simple command lines, such as splitting a cell of a mesh in 2, which allows to construct arbitrary grids (i.e., regardless of the leg length or the flux expansion). The name Gingred has been chosen by inverting the letters "i" and "e" from the common "GenGrid" or "GridGen" names. The execution of Gingred from the integrated framework OMFIT [22,23] is in progress. The work on Gingred was initiated in 2012 to simplify and create an interactive grid generator for the construction of snowflake grids for UEDGE simulations [24,25]. One of the advantages of Gingred is the capability to see each step of the grid construction from automatic plots. This feature helps with the creation of arbitrary grids by new users and with the debugging and development of additional capabilities of the code by developers. Gingred is accessible via the Version Control with Subversion [26] (called svn). Svn is similar to Github [27] for sharing projects between developers and managing different versions of a code. This article is organized as follow. Sec. 2 contains the description of several magnetic configurations that require a different logical grid topology. The latest version of the code manages the following grids: no X-point, a singlenull, a double-null, two snowflake-minus, and two snowflake-plus magnetic configurations. However, the extension to new magnetic geometries would be straightforward following the existing scripts and the details shown here. Sec. 3 provides a description of the code and the main command lines, and shows examples of snowflake grid generation. Sec. 5 deals with possible future developments of Gingred.

Available grids
This section summarizes the different grids and notations used for different magnetic equilibria. It turns out that the different magnetic geometries related to the grids shown in Appendix A can be universalized by using a minimal set of parameters. We show values of this minimal set of parameters for the main magnetic equilibria. Initially developed in the literature [21,13,20], each grid configuration is associated with a 2D map of indices of cells (called the 2D logic map). Here we chose to standardize these 2D logic maps by using parameters in order to easily switch between various grid configurations. The position of the X-point(s) and the plates are determined by the minimal set of parameters such as the arrays ixpt1/ixpt2 for the inner/outer index position of X-point(s), and ixlb/ixrb for the index of left/right boundary (plates) positions. Only for the left boundary ixlb the indices correspond with the cell of the poloidal position of the left plates in the 2D logic map, but for all the other arrays the indices correspond with the position of the previous poloidal cell. This choice of indices follows the code UEDGE because Gingred was initially developed for the grid generation of advanced divertor configurations for UEDGE simulations. In Appendix A, we show the correspondence between the main magnetic geometries and their 2D logic map of cells (also called minimal grid of patches). Once we understand how to switch between each magnetic geometries with different number of plates and different X-point(s) and plates positions, it becomes trivial to add more X-points, but the number of different 2D logic map of cells quickly increases. Indeed, we have 1 main configuration with one X-point, 4 main configurations with two X-points, and 16 main configurations with three X-points (without taking into account special cases where two X-points are on the same magnetic flux surface). We explicitly show the minimal 2D logic map of cells in Appendix A for the main grid configurations. Each map is different due to the relative position of plates and X-point(s). The bottom-up approach of Gingred starts by creating these minimal 2D logic maps (i.e., the "skeleton" grids), then the refinement of the grid configuration can be done independently of the 2D logic map under consideration. Moreover, it is straightforward to expand the 2D logic map of cells to include more X-points such as one upper X-point with a lower snowflake geometry or the cloverleaf (3 X-points) magnetic geometry [5]. The generalization of the code with an arbitrary number of X-points and plates is under development.

Gingred grid generator
This section reports some details of the Gingred grid generator. The code, written in IDL, manages the construction of all magnetic configurations detailed in Sec. 2 as well as the case with no X-point relevant to the edge of reversed field configurations (FRC).
In order to use Gringred, one can import the code using Subversion svn command line. All other IDL functions are included in the SVN package.

List of available commands
The main commands necessary to run Gingred and to construct a grid (a snowflake-minus SF45 grid is taken as example, see Appendix A) are listed by order of appearance for the grid generation. The options are shown with brackets [option=value] or [/option] for a flag.
read efit[, /plot] This command line imports the magnetic equilibrium from the geqdsk files (note that geqdsk can be a link to a file).
mapping[, importfile="gp.SF45", gridparams=gp, conf="SF45", /manual, /ascii] This command line setup the boundaries of the grid. The user interface is used by the manual flag /manual for the magnetic configuration set by conf="SF45" (the available geometries are FRC, SN, DN, SF15, SF45, SF75 or SF95), whereas the automatic mode can be used if one set importfile= "gp.SF45"[, /ascii] where gp.SF45 is an idl.save file (or an ascii file if the flag /ascii is used) file. Similarly, the automatic mode can be used by importing gridparams=gp where gp is a local IDL structured variable defining the grid parameters. Examples of those IDL and ascii files are given and they can be created after the execution of the interactive mode. For the interactive mode (with the flag /manual), the user has to select consecutively point positions close to: the magnetic field center, the primary X-point, and the secondary X-point (for non single-null grids). Then the user has to choose the flux surface boundaries (represented by different colors in the 2D logic maps) of: the primary SOL (yellow), the core (cyan), the primary private flux region (red), and the following additional choices for non single-null grids: the secondary private flux region (purple) and the secondary SOL (grey). It is also possible to fix the boundary fluxes, and the plate boundaries from their values instead of using the user interface with the command grid params, gp, name='psi core', value=Get PsiN Inv(0.9), /set var, where the function Get PsiN Inv returns the non-normalized poloidal flux from the normalized value given as input. For example, the most used variables one can set are psi core, psi sol1, psi sol2, psi pf1 and psi pf2 flux surfaces, and magx, core, xpt1, xpt2, istrike1, istrike2, ostrike1 and ostrike2 points, at least for a snowflake magnetic configuration with two X-points. For the type of the variables: fluxes are double variables, and points are structures with 5 quantities (r and z the position coordinates, psi the magnetic flux, s a length coordinate internally used by the code, and type a logical variable equal to 1 if the point is a X-point).
grid params, gp[, importfile="gp.SF45", exportfile="gp.SF45.new", /reset, /ascii] This command line creates a local copy of the variable gp accessible by the user that contains some parameters for the grid such as the position of the magnetic axis (i.e., gp.magx), the position of a point delimiting the core boundary (i.e., gp.core), or the position of the X-point(s) (i.e., gp.xpt1 and gp.xpt2). The list of available parameters can be shown with the command help, gp. These parameters can be imported from or exported to a IDL (respectively ascii) file by using the options such as importfile or exportfile (respectively by using the function read gp and write gp when the flag /ascii is used). -ptN={r:0d0,z:0d0,psi:0d0,type:0,s:0d0} The users need to set list of points to define each plate. This structure contains the radial r and vertical z coordinates, the poloidal flux psi, the logical type equal to 1 if the point is a X-point, and a curved integrated length s.
This is an example to modify the point p1a. The function Get psi(r,z) is defined to return the value of the magnetic flux surface at the coordinate (r, z). Note that the functions Get psiN(psi) and Get psiN Inv(psiN) also exist and return respectively the normalized poloidal flux (psiN) and its nonnormalized value (psi).
To setup 4 plates with only 2 points each (make sure that each plate intersects the domain). We can have as many segments as wanted to define each plate because Gingred detects which segment of the plate intersects a specific magnetic flux surface during the non-orthogonal grid modification by plate constraints. By default the code keeps the ratio of the poloidal lengths from the orthogonal grid. This option is ideal when the original orthogonal boundary curve is close to the plate geometry. However, for more complex plate geometry, the option /equal can be used to have equal poloidal lengths of the cells in the divertor legs. Even if the later option is not often used, we remark that it induces larger angles between the edges of adjacent cells (see Figs. 1(c) and 2(c)) that can facilitate numerical convergence due to the area around the primary X-point (see BOUT++ grids generated by Hypnotoad).
plates add, g1, g2[, /equal, /fastrun, ixortho=ixortho, wplt=[1,0,0,0]] This command line reconstructs non-orthogonal grids for each legs with /equal or proportional (by default) poloidal lengths, and /fastrun is an option that can be used when the longest poloidal lengths of the legs are less than 1 meter. The output grid is g2. The script uses the parameters saved in the gp variable such as the poloidal position of X-points and plates in the 2D logic map of indices. The option given by the array wplt specify which boundary is going to be constrained by the plate geometry given by the list pltarr. By default wplt is an array of ones in order to constrain each plate geometry. The matrix ixortho of dimension (nplt, ny) 2 is setup by default to the indices ixpt1 and ixpt2 of the poloidal positions of the X-point(s) in order to constrain all cells in each leg. With this option, users can constraint only a part of the private flux region, and more cells in the SOL above the X-point(s) positions.
plates plot To plot the plates defined by the list of arrays pltarr generate header, g1, hd1 The output variable hd1 is the header needed for the creation of the "gridue" file.
export grid, g1, h=hd1, /save It exports the grid g1 into the file "gridue". The guard cells are constructed before to export the grid. All of these command lines can be used for the generation of grids with different sizes and refinements. Examples of grid generation with plate geometry constraints are shown in Secs. 3.3 and 3.4.

Algorithms used in Gingred
The approach used in this work is to produce a minimal grid of patches called the "skeleton" grids (as described in Sec. 2 and shown in Appendix A), consistent with a given magnetic equilibrium configuration. Then, each patch can be divided poloidally and radially until a desired grid is built. Moreover, there are other advanced algorithms used by Gingred. In comparison to the literature where the discrete cosine transform is used for the interpolation of the poloidal magnetic flux, Gingred has the capability to use a bi-cubic interpolation. We believe that the bi-cubic interpolation is more robust, especially for snowflake magnetic configuration where a large area of small poloidal magnetic field is present as well as a low refinement of the magnetic equilibrium geqdsk input file (usually on a 64x64 mesh). Based on this interpolated magnetic field, the code can use ODE integrators such as LSODE and Runge-Kutta (RK4) for the computation of radial and poloidal lines. The integrator LSODE is more robust than RK4, when we use the default error tolerance parameters. Radial lines are the curves fol-lowing perpendicular directions of the flux surfaces defined by iso-contours of the poloidal magnetic flux, and poloidal lines are the curves following the iso-contours of the poloidal magnetic flux. From these capabilities, the code manages the construction of the orthogonal grid and arbitrary refinements of the grid decided by the user. For the non-orthogonal capability of the code, the length of the poloidal curves following iso-contours of the flux surfaces are used to determine the position of the cells. Finally, for the refinement of the number of radial cells, the code uses the default IDL root finding in order to find a value of the flux surface along a radial line. All of these algorithm are robust and there is no bottleneck that significantly reduces the computation time.

Example of a SF45 grid generation
Here is a list of command lines used for the generation of the SF45 grid for a simulated magnetic equilibrium on NSTX-U [28]. The link of the geqdsk magnetic equilibrium file (where $DIR refers to /your dir /gingred/data/SF45) is setup by: > ln -s $DIR/geqdsk neqdsk Then, from the initial 13 × 5 (with guard cells used for the plates) grid we started by doubling three times the number of radial cells (doubler) and one time the number of poloidal cells (doublep) and we obtain a 22 × 26 grid (with guard cells). Then, the poloidal refinement of the grid is done by executing the refgridp command where i0 ∈ {20, 21,19,19,19,18,14,13,13,13,10,11,12,13,14,15 We remark that the arrays gp.ixpt1, gp.ixpt2, gp.ixlb and gp.ixrb are defining the leg indices where the non-orthogonal grid is computed and the flag /equal can be added in the command line plates add in order to use equal poloidal cell lengths. The flag /fastrun can be used to avoid additional search of the smallest poloidal distance in case the first direction gives a distance smaller than 1 m (i.e., one do not should use this option when legs are too close to a poloidal magnetic O-point). The function plates plot shows the plates defined by pltarr. Fig. 1 shows (a) the grid gf4f29f without plate constraints, (b) the grid gf4f29fp with plate constraints and (c) the grid gf4f29fp2 with plate constraints and the flag /equal. SF45 nonortho equal (c) Nonorthogonal SF45 with "equal" plate constraints.

Example of a SF75 grid generation
Here is a list of command used for the generation of the SF75 grid for a simulated magnetic equilibrium on NSTX-U [28]. The link of the geqdsk magnetic equilibrium file (where $DIR refers to /your dir /gingred/data/SF75) are setup by: > ln -s $DIR/neqdsk neqdsk Then, from the initial 13 × 5 grid we started by doubling three times the number of radial cells and one time the number of poloidal cells and we obtain a 22 × 26 grid. Then, the poloidal refinement of the grid is done by executing the refgridp command where i0 ∈ {20, 19,19,18,19,14,13,13,12,11,10,11,12,13,14, 3, 3, 3, 3, 3, 2, 3, 1}. (2) Finally, the radial refinement of the grid is done by executing the refgridr command where j0 ∈ {17, 17, 16, 17, 18, 8}. The constraints of plate geometries are possible, similar to the previous example. Fig. 2 shows (a) the grid gf4f29f without plate constraints, (b) the grid gf4f29fp with plate constraints and (c) the grid gf4f29fp2 with plate constraints and the flag /equal.
In addition to these examples, the code contains more detailed documentation as well as examples of automatic generation of grids from a given input option file. The input option file, an ascii or an IDL structure, can be used in by the mapping function (without the flag /manual) in order to automatically build the initial grid of patches. To date, there is no support for the full automatic generation of the input option file (find X-point, detection of magnetic configuration, standard position of plates and boundary flux surfaces). Instead, they are created by a interactive run of the function mapping with the flag /manual.

Comparison with state of the art grid generators
The new grid generator Gingred is unique by comparing existing grid generators. First, according to our knowledge there is no existing grid generator that is capable of constructing a grid for snowflake magnetic configurations except the internal grid generator of the code UEDGE [20]. The reason is mainly due to the unique extension, for the first time in Ref. [20], of the logic maps from single-null and double-null to the new classes of logic maps SF75 nonortho equal (c) Nonorthogonal SF75 with "equal" plate constraints. for snowflake configurations (see Appendix A). However, the internal grid generator of UEDGE is not accessible without UEDGE and it has not been tested yet with new users. Gingred has been successfully tested [25] by new users [30] for reversed field configurations, and different grids have been created for multiple machines (e.g., NSTX, NSTX-U, DIII-D and ADX) with complex plate geometries such as the Small Angle Slot divertor installed at DIII-D [29]. In other words, Gingred is the first numerical application of the new logic maps for snowflake configurations accessible for multiple codes. However, there are fundamental differences between the internal grid generator of UEDGE and Gingred with respect to the snowflake grid generation. As examples, (i) Gingred is focused on a bottom-up approach at the level of the user interaction that ease significantly the progresses of the grid generation even for non-experienced users in Gingred (see Ref. [30]) , (ii) the code includes multiple options for debugging, plotting, or changing the accuracy at each step of the workflow, (iii) robust automatic scripts have been developed and are included in the code as practical examples, (iv) Gingred can be used to generate grids for arbitrary kinetic or fluid codes, and (v) the new bi-cubic interpolation capability can be used instead of the usual linear interpolation of the magnetic equilibrium that is present in the major grid generators. This last point represents a significant improvement of the accuracy of the magnetic equilibrium as well as the geometry of the cells around the X-points.

Future extensions of Gingred
The first public release of the code is now available. However, the development of existing or new capabilities are still in process. (i) As examples, the automatic generation of a grid, without the user interactivity, is developed by using an IDL or ascii input file fixing all required boundary conditions and refinement options, but the automatic detection of the magnetic configuration (single-null, double-null or snowflake) from the magnetic equilibrium input file is under investigation. Globally the graphical user interface can be enhanced allowing multiple choices for the user. (ii) Another improvement under development is the inclusion of Gingred in a grid generator module of the python integrated modeling framework OMFIT [22,23] that allows interactions between multiple codes. All figures shown here are obtained with python scripts in OMFIT. (iii) Moreover, the algorithm which tracks radial and poloidal lines across and along the poloidal magnetic flux surfaces could be enhanced by using an external ODE integrator. This improvement would reduce the time computation during the refinement and the plate geometry constraints, even if it is not significantly large (within a few minutes for high resolution grids). (iv) In the same topic, other options managing the distribution of poloidal lengths of the cells in the legs during the plate geometry constraint can be developed. The two available options, namely the ratio and the equal poloidal lengths, seem nevertheless robust enough. A successful test has been done for the grid construction of a small-angle-slot (SAS) divertor similar to the one recently installed at DIII-D, and UEDGE convergences have already been obtained. (v) Finally, the generalization of the code is straightforward for an arbitrary number of X-points, even without X-point in the case of field reversed configurations (already developed but not shown here), or with 3 X-points if one wants to consider a cloverleaf divertor, or associate a lower snowflake with a upper single-null configuration.  For the double-null, the initial grids are 12×5 and the position of the X-points and the plates are given by ixpt1= [1,7], ixpt2= [3,9], ixlb=[0,6] and ixrb= [4,10].
exhaust by radiative losses before the first material in the SOL, the divertor plates. It is indispensable to allow UEDGE simulations of snowflake divertors. The starting point is to be able creating snowflake grids. There are two type of snowflake geometries, the snowflake-minus (when the secondary X-point is in the SOL) and the snowflake-plus (when the secondary X-point is in the private flux region of the primary X-point). The two main possible (13 × 5) grids of snowflake-minus (respectively snowflake-plus) are shown in   All indices and notations used in Gingred are given in the caption of figures. All snowflake grids contains 13 × 5 cells, including the guard cells. The difference between the 2 snowflake-minus and the 2 snowflake-plus are the poloidal position of the secondary X-point. We can see that the projection of the secondary X-point crossing perpendicularly the flux surfaces in the direction of the primary X-point can be closer to the inner plate P1 (at ix=0) or the outer plate P2 (at ix=7 or 8). Indeed, for the snowflake-minus grids, the projection of the secondary X-point goes toward the core in cyan (SF15) or primary private flux region in red (SF45). Similarly for the snowflake-plus grids, the projection of the secondary X-point goes toward the low field side SOL (SF75) or the high field side SOL (SF95). The initial grids are 13 × 5 and the position of the X-points and the plates are ixpt1= [1,9] and ixpt2= [5,10], ixlb= [0,8] and ixrb= [6,11] for the SF15, and ixpt1= [1,6], ixpt2= [5,10], ixlb= [0,9] and ixrb= [7,11] for the SF45. For main snowflake-plus maps: The initial grids are 13 × 5 and the position of the X-points and the plates are ixpt1= [1,6], ixpt2= [5,10] for the SF75 and ixpt1= [1,6], ixpt2= [2,10] for the SF95, and ixlb=[0,9] and ixrb= [7,11] for both.