def screwpinch(nx=68,
               ny=16,
               nz=128,
               xcentre=1.5,
               fname='screwpinch.fci.nc',
               a=0.2,
               npoints=421,
               show_maps=False):
    yperiod = 2 * np.pi
    field = zb.field.Screwpinch(xcentre=xcentre,
                                yperiod=yperiod,
                                shear=0 * 2e-1)
    ycoords = np.linspace(0.0, yperiod, ny, endpoint=False)
    start_r = xcentre + a / 2.
    start_z = 0.
    print("Making curvilinear poloidal grid")
    inner = zb.rzline.shaped_line(R0=xcentre,
                                  a=a / 2.,
                                  elong=0,
                                  triang=0.0,
                                  indent=0,
                                  n=npoints)
    outer = zb.rzline.shaped_line(R0=xcentre,
                                  a=a,
                                  elong=0,
                                  triang=0.0,
                                  indent=0,
                                  n=npoints)
    poloidal_grid = zb.poloidal_grid.grid_elliptic(inner, outer, nx, nz)
    grid = zb.grid.Grid(poloidal_grid, ycoords, yperiod, yperiodic=True)
    maps = zb.make_maps(grid, field)
    zb.write_maps(grid, field, maps, str(fname), metric2d=False)
    calc_curvilinear_curvature(fname, field, grid, maps)

    if show_maps:
        zb.plot.plot_forward_map(grid, maps, yslice=0)
# Generate a grid on each poloidal slice using the elliptic grid generator

nx = 20
ny = 20

pol_grids = [
    zoidberg.poloidal_grid.grid_elliptic(inner_line, outer_line, nx, ny)
    for inner_line in inner_lines
]

#############################################################################
# Create a grid, then calculate forward and backward maps

grid = zoidberg.grid.Grid(pol_grids, ycoords, yperiod, yperiodic=True)

maps = zoidberg.make_maps(grid, magnetic_field)

#############################################################################
# Write grid file

filename = "stellarator.fci.nc"

print("Writing to grid file '{0}'".format(filename))
zoidberg.write_maps(grid,
                    magnetic_field,
                    maps,
                    gridfile=filename,
                    new_names=False,
                    metric2d=True)

#############################################################################
outer_line = zoidberg.rzline.circle(R0=0.0, r=0.8)

#############################################################################
# Now have inner and outer boundaries for each poloidal grid
# Generate a grid on each poloidal slice using the elliptic grid generator

nx = 20
ny = 20

pol_grids = [ zoidberg.poloidal_grid.grid_elliptic(inner_line, outer_line, nx,ny) for inner_line in inner_lines ]

#############################################################################
# Create a grid, then calculate forward and backward maps

grid = zoidberg.grid.Grid( pol_grids, ycoords, yperiod, yperiodic=True)

maps = zoidberg.make_maps(grid, magnetic_field)

#############################################################################
# Write grid file

filename = "stellarator.fci.nc"

print("Writing to grid file '{0}'".format(filename))
zoidberg.write_maps(grid, magnetic_field, maps, gridfile=filename, new_names=False, metric2d=True)

#############################################################################
# Plot maps

zoidberg.plot.plot_forward_map(grid, maps)
def W7X(
    nx=68,
    ny=32,
    nz=256,
    fname='W7-X.fci.nc',
    vmec_file='w7-x.wout.nc',
    inner_VMEC=False,
    inner_vacuum=False,
    outer_VMEC=False,
    outer_vacuum=False,
    outer_vessel=False,
    npoints=100,
    a=2.5,
    show_maps=False,
    calc_curvature=True,
    smooth_curvature=False,
    plasma_field=False,
    configuration=0,
    vmec_url='http://svvmec1.ipp-hgw.mpg.de:8080/vmecrest/v1/w7x_ref_171/wout.nc'
):

    yperiod = 2 * np.pi / 5.
    ycoords = np.linspace(0.0, yperiod, ny, endpoint=False)
    if outer_VMEC:
        print("Aligning to outer VMEC flux surface...")
        outer_lines = get_VMEC_surfaces(phi=ycoords,
                                        s=1,
                                        npoints=nz,
                                        w7x_run=vmec_url)
    elif outer_vessel:
        print("Aligning to plasma vessel (EXPERIMENTAL) ...")
        outer_lines = get_W7X_vessel(phi=ycoords, nz=nz)
    elif (outer_vacuum or inner_vacuum):
        xmin = 4.05
        xmax = 4.05 + 2.5
        zmin = -1.35
        zmax = -zmin
        field = zb.field.W7X_vacuum(phimax=2 * np.pi / 5.,
                                    x_range=[xmin, xmax],
                                    z_range=[zmin, zmax],
                                    include_plasma_field=plasma_field)
        xcentre, zcentre = field.magnetic_axis(phi_axis=ycoords[0],
                                               configuration=configuration)
        start_r = xcentre + a / 2.
        start_z = zcentre
        if inner_vacuum:
            print("Aligning to inner vacuum flux surface...")
            inner_lines = get_lines(field,
                                    start_r,
                                    start_z,
                                    ycoords,
                                    yperiod=yperiod,
                                    npoints=npoints)
        if outer_vacuum:
            print("Aligning to outer vacuum flux surface...")
            outer_lines = get_lines(field,
                                    start_r + a,
                                    start_z,
                                    ycoords,
                                    yperiod=yperiod,
                                    npoints=npoints)

    xmin = np.min([min(outer_lines[i].R) for i in range(ny)])
    xmax = np.max([max(outer_lines[i].R) for i in range(ny)])
    zmin = np.min([min(outer_lines[i].Z) for i in range(ny)])
    zmax = np.max([max(outer_lines[i].Z) for i in range(ny)])

    if inner_VMEC:
        print("Aligning to inner VMEC flux surface...")
        inner_lines = get_VMEC_surfaces(phi=ycoords,
                                        s=0.67,
                                        npoints=nz,
                                        w7x_run=vmec_url)

    print("creating grid...")
    poloidal_grid = [
        zb.poloidal_grid.grid_elliptic(inner, outer, nx, nz, show=show_maps)
        for inner, outer in zip(inner_lines, outer_lines)
    ]

    # Create the 3D grid by putting together 2D poloidal grids
    grid = zb.grid.Grid(poloidal_grid, ycoords, yperiod, yperiodic=True)

    field = zb.field.W7X_vacuum(phimax=2 * np.pi / 5.,
                                x_range=[xmin, xmax],
                                z_range=[zmin, zmax],
                                include_plasma_field=plasma_field)

    maps = zb.make_maps(grid, field)
    zb.write_maps(grid, field, maps, str(fname), metric2d=False)

    if calc_curvature:
        print("calculating curvature...")
        calc_curvilinear_curvature(fname, field, grid)

    if (calc_curvature and smooth_curvature):
        smooth_metric(fname,
                      write_to_file=True,
                      return_values=False,
                      smooth_metric=True)

    if show_maps:
        zb.plot.plot_forward_map(grid, maps, yslice=-1)
def rotating_ellipse(nx=68,
                     ny=16,
                     nz=128,
                     xcentre=5.5,
                     I_coil=0.01,
                     curvilinear=True,
                     rectangular=False,
                     fname='rotating-ellipse.fci.nc',
                     a=0.4,
                     curvilinear_inner_aligned=True,
                     curvilinear_outer_aligned=True,
                     npoints=421,
                     Btor=2.5,
                     show_maps=False,
                     calc_curvature=True,
                     smooth_curvature=False,
                     return_iota=True,
                     write_iota=False):
    yperiod = 2 * np.pi / 5.
    field = zb.field.RotatingEllipse(xcentre=xcentre,
                                     I_coil=I_coil,
                                     radius=2 * a,
                                     yperiod=yperiod,
                                     Btor=Btor)
    # Define the y locations
    ycoords = np.linspace(0.0, yperiod, ny, endpoint=False)
    start_r = xcentre + a / 2.
    start_z = 0.

    if rectangular:
        print("Making rectangular poloidal grid")
        poloidal_grid = zb.poloidal_grid.RectangularPoloidalGrid(
            nx, nz, 1.0, 1.0, Rcentre=xcentre)
    elif curvilinear:
        print("Making curvilinear poloidal grid")
        inner = zb.rzline.shaped_line(R0=xcentre,
                                      a=a / 2.,
                                      elong=0,
                                      triang=0.0,
                                      indent=0,
                                      n=npoints)
        outer = zb.rzline.shaped_line(R0=xcentre,
                                      a=a,
                                      elong=0,
                                      triang=0.0,
                                      indent=0,
                                      n=npoints)

        if curvilinear_inner_aligned:
            print("Aligning to inner flux surface...")
            inner_lines = get_lines(field,
                                    start_r,
                                    start_z,
                                    ycoords,
                                    yperiod=yperiod,
                                    npoints=npoints)
        if curvilinear_outer_aligned:
            print("Aligning to outer flux surface...")
            outer_lines = get_lines(field,
                                    xcentre + a,
                                    start_z,
                                    ycoords,
                                    yperiod=yperiod,
                                    npoints=npoints)

        print("creating grid...")
        if curvilinear_inner_aligned:
            if curvilinear_outer_aligned:
                poloidal_grid = [
                    zb.poloidal_grid.grid_elliptic(inner,
                                                   outer,
                                                   nx,
                                                   nz,
                                                   show=show_maps)
                    for inner, outer in zip(inner_lines, outer_lines)
                ]
            else:
                poloidal_grid = [
                    zb.poloidal_grid.grid_elliptic(inner,
                                                   outer,
                                                   nx,
                                                   nz,
                                                   show=show_maps)
                    for inner in inner_lines
                ]
        else:
            poloidal_grid = zb.poloidal_grid.grid_elliptic(
                inner, outer, nx, nz)

    # Create the 3D grid by putting together 2D poloidal grids
    grid = zb.grid.Grid(poloidal_grid, ycoords, yperiod, yperiodic=True)
    maps = zb.make_maps(grid, field)
    zb.write_maps(grid, field, maps, str(fname), metric2d=False)

    if (curvilinear and calc_curvature):
        print("calculating curvature...")
        calc_curvilinear_curvature(fname, field, grid, maps)

    if (calc_curvature and smooth_curvature):
        smooth_metric(fname,
                      write_to_file=True,
                      return_values=False,
                      smooth_metric=True)

    if (return_iota or write_iota):
        rindices = np.linspace(start_r, xcentre + a, nx)
        zindices = np.zeros((nx))
        iota_bar = calc_iota(field, start_r, start_z)
        if (write_iota):
            f = DataFile(str(fname), write=True)
            f.write('iota_bar', iota_bar)
            f.close()
        else:
            print("Iota_bar = ", iota_bar)
nx = 5
ny = 6
nz = 7

# Create magnetic field
magnetic_field = zoidberg.field.StraightStellarator(radius = 1.0)

# Create a rectangular grid in (x,y,z)
rectangle = zoidberg.grid.rectangular_grid(nx,ny,nz,
                                           Lx = 1.0, Lz = 1.0, Ly = 10.0,
                                           yperiodic = True)

# Here both the field and and grid are centred at (x,z) = (0,0)
# and the rectangular grid here fits entirely within the coils

maps = zoidberg.make_maps(rectangle, magnetic_field)

# Pick a poloidal slice and the next slice
yslice = 0
pol, ycoord = rectangle.getPoloidalGrid(yslice)
pol_next, ycoord_next = rectangle.getPoloidalGrid(yslice+1)

# Plot the grid points at this poloidal slice
plt.plot(pol.R, pol.Z, 'x')

# Get the coordinates which the forward map corresponds to
R_next, Z_next = pol_next.getCoordinate( maps['forward_xt_prime'][:,yslice,:], maps['forward_zt_prime'][:,yslice,:] )

plt.plot(R_next, Z_next, 'o')

plt.show()
Esempio n. 7
0
#!/usr/bin/env python

import numpy as np
import zoidberg
   
field = zoidberg.field.GEQDSK("g014220.00200") # Read magnetic field

grid = zoidberg.grid.rectangular_grid(100, 10, 100,
                                      1.5-0.1, # Range in R (max - min)
                                      2*np.pi, # Toroidal angle
                                      3., # Range in Z
                                      xcentre=(1.5+0.1)/2, # Middle of grid in R
                                      yperiodic=True) # Periodic in toroidal angle

# Create the forward and backward maps
maps = zoidberg.make_maps(grid, field)

# Save to file
zoidberg.write_maps(grid, field, maps, gridfile="tokamak.fci.nc")

#############################################################################
# Plot maps

zoidberg.plot.plot_forward_map(grid, maps)

def W7X(nx=68,
        ny=16,
        nz=256,
        fname='W7-X.fci.nc',
        VMEC=True,
        vmec_file='w7-x.wout.nc',
        inner_VMEC=True,
        inner_vacuum=False,
        outer_VMEC=True,
        outer_vacuum=False,
        outer_vessel=False,
        npoints=100,
        a=0.5,
        show_maps=False,
        calc_curvature=True,
        smooth_curvature=False,
        configuration=0):
    field = zb.field.W7X(phimax=2 * np.pi / 5.)
    yperiod = 2 * np.pi / 5.
    ycoords = np.linspace(0.0, yperiod, ny, endpoint=False)
    xcentre, zcentre = field.magnetic_axis(phi_axis=ycoords[0],
                                           configuration=configuration)
    start_r = xcentre + a / 2.
    start_z = zcentre

    print("Making curvilinear poloidal grid")
    inner = zb.rzline.shaped_line(R0=xcentre,
                                  a=a / 2.,
                                  elong=0,
                                  triang=0.0,
                                  indent=0,
                                  n=npoints)
    outer = zb.rzline.shaped_line(R0=xcentre,
                                  a=a,
                                  elong=0,
                                  triang=0.0,
                                  indent=0,
                                  n=npoints)

    if inner_vacuum:
        print("Aligning to inner vacuum flux surface...")
        inner_lines = get_lines(field,
                                start_r,
                                start_z,
                                ycoords,
                                yperiod=yperiod,
                                npoints=npoints)
    elif inner_VMEC:
        print("Aligning to inner VMEC flux surface...")
        inner_lines = get_VMEC_surfaces(phi=ycoords, s=0.5, npoints=nz)
    if outer_vacuum:
        print("Aligning to outer vacuum flux surface...")
        outer_lines = get_lines(field,
                                start_r + a,
                                start_z,
                                ycoords,
                                yperiod=yperiod,
                                npoints=npoints)
    elif outer_VMEC:
        print("Aligning to outer VMEC flux surface...")
        outer_lines = get_VMEC_surfaces(phi=ycoords, s=1, npoints=nz)
    elif outer_vessel:
        print("Aligning to plasma vessel (EXPERIMENTAL) ...")
        outer_lines = get_W7X_vessel(phi=ycoords, nz=nz)

    print("creating grid...")
    poloidal_grid = [
        zb.poloidal_grid.grid_elliptic(inner, outer, nx, nz, show=show_maps)
        for inner, outer in zip(inner_lines, outer_lines)
    ]

    # Create the 3D grid by putting together 2D poloidal grids
    grid = zb.grid.Grid(poloidal_grid, ycoords, yperiod, yperiodic=True)
    maps = zb.make_maps(grid, field)
    zb.write_maps(grid, field, maps, str(fname), metric2d=False)

    if calc_curvature:
        print("calculating curvature...")
        calc_curvilinear_curvature(fname, field, grid)

    if (calc_curvature and smooth_curvature):
        smooth_metric(fname,
                      write_to_file=True,
                      return_values=False,
                      smooth_metric=True)
nx = 5
ny = 6
nz = 7

# Create magnetic field
magnetic_field = zoidberg.field.StraightStellarator(radius = 1.0)

# Create a rectangular grid in (x,y,z)
rectangle = zoidberg.grid.rectangular_grid(nx,ny,nz,
                                           Lx = 1.0, Lz = 1.0, Ly = 10.0,
                                           yperiodic = True)

# Here both the field and and grid are centred at (x,z) = (0,0)
# and the rectangular grid here fits entirely within the coils

maps = zoidberg.make_maps(rectangle, magnetic_field)

# Pick a poloidal slice and the next slice
yslice = 0
pol, ycoord = rectangle.getPoloidalGrid(yslice)
pol_next, ycoord_next = rectangle.getPoloidalGrid(yslice+1)

# Plot the grid points at this poloidal slice
plt.plot(pol.R, pol.Z, 'x')

# Get the coordinates which the forward map corresponds to
R_next, Z_next = pol_next.getCoordinate( maps['forward_xt_prime'][:,yslice,:], maps['forward_zt_prime'][:,yslice,:] )

plt.plot(R_next, Z_next, 'o')

plt.show()
Esempio n. 10
0
#!/usr/bin/env python

import numpy as np
import zoidberg

field = zoidberg.field.GEQDSK("g014220.00200")  # Read magnetic field

grid = zoidberg.grid.rectangular_grid(
    100,
    10,
    100,
    1.5 - 0.1,  # Range in R (max - min)
    2 * np.pi,  # Toroidal angle
    3.,  # Range in Z
    xcentre=(1.5 + 0.1) / 2,  # Middle of grid in R
    yperiodic=True)  # Periodic in toroidal angle

# Create the forward and backward maps
maps = zoidberg.make_maps(grid, field)

# Save to file
zoidberg.write_maps(grid, field, maps, gridfile="tokamak.fci.nc")

#############################################################################
# Plot maps

zoidberg.plot.plot_forward_map(grid, maps)