def flux_tube_radial_profile(region, minmax_pt, surf, peak_or_pit,
        other_arr=None, mask=None):
    arr = surf.arr
    wdist = wraparound_dist_vec(*arr.shape)
    from field_trace import _level_set
    if other_arr is None:
        other_arr = arr
    seed_points = get_seed_points(region, arr, int(np.sqrt(len(region))),
            reverse=(peak_or_pit == 'peak'))
    radial_profile = []
    nx, ny = arr.shape
    for seed in seed_points:
        seed_flux = arr[seed]
        nbr_func = lambda t: _cp.neighbors6(t[0], t[1], nx, ny)
        lset = _level_set(arr, level_val=seed_flux,
                position=seed, neighbors_func=nbr_func)
        lset = lset.intersection(region)
        if not lset:
            continue
        if mask is not None:
            mask[lset.xs, lset.ys] = True
        lset_arr = other_arr[lset.xs, lset.ys]
        lset_mean = lset_arr.mean()
        lset_err = lset_arr.std() / np.sqrt(len(lset_arr))
        lset_dists = wdist(lset.xs, lset.ys, minmax_pt[0], minmax_pt[1])
        dists_mean = lset_dists.mean()
        dists_err = lset_dists.std() / np.sqrt(len(lset_dists))
        radial_profile.append(
                (seed, seed_flux, lset_mean, lset_err, dists_mean, dists_err))
    return radial_profile
def expand_region(arr, region, ntimes):
    nx, ny = arr.shape
    frontier = set()
    new_region = set(region)
    for pt in region:
        nbrs = _cp.neighbors6(pt[0], pt[1], nx, ny)
        for nbr in nbrs:
            if nbr not in region:
                frontier.add(nbr)
    new_region.update(frontier)
    for _ in range(ntimes-1):
        new_frontier = set()
        for pt in frontier:
            nbrs = _cp.neighbors6(pt[0], pt[1], nx, ny)
            for nbr in nbrs:
                if nbr not in new_region:
                    new_frontier.add(nbr)
        frontier = new_frontier
        new_region.update(frontier)
    return new_region
def expand_region_to_radius(arr, region, radius, minmax):
    nx, ny = arr.shape
    wdist = wraparound_dist_vec(nx, ny)
    frontier = set()
    new_region = set(region)
    for pt in region:
        nbrs = _cp.neighbors6(pt[0], pt[1], nx, ny)
        for nbr in nbrs:
            if nbr not in region and wdist(minmax[0], minmax[1], nbr[0], nbr[1]) <= radius:
                frontier.add(nbr)
                new_region.add(nbr)
    # new_region.update(frontier)
    while frontier:
        new_frontier = set()
        for pt in frontier:
            nbrs = _cp.neighbors6(pt[0], pt[1], nx, ny)
            for nbr in nbrs:
                if nbr not in new_region and wdist(minmax[0], minmax[1], nbr[0], nbr[1]) <= radius:
                    new_frontier.add(nbr)
        frontier = new_frontier
        new_region.update(frontier)
    return new_region
def flux_tube_radial_spokes(region, minmax_pt, arr, peak_or_pit):
    nx, ny = arr.shape
    ctr_max = nx+ny
    frontier = set()
    for pt in region:
        for nbr in _cp.neighbors6(pt[0], pt[1], nx, ny):
            if nbr not in region:
                frontier.add(pt)
    spokes = []
    for pt in frontier:
        cur = pt
        spoke = [cur]
        if peak_or_pit == 'peak': next = _cp._get_highest_neighbor
        elif peak_or_pit == 'pit': next = _cp._get_lowest_neighbor
        else: raise ValueError("peak_or_pit not in ('peak', 'pit')")
        ctr = ctr_max
        while ctr > 0:
            cur = next(arr, cur)
            spoke.append(cur)
            if cur == minmax_pt:
                spokes.append(spoke)
                break
            ctr -= 1
    return spokes
Beispiel #5
0
def mag_shear_theta_sectors(psi_arr, cur, den, thresh=100, save_basename='field-vs-r'):
    hess = hessian(psi_arr)
    ntheta = 2
    nx, ny = psi_arr.shape
    psi_grad_x, psi_grad_y = gradient(psi_arr)
    bmag = np.sqrt(psi_grad_x**2 + psi_grad_y**2)
    den_x, den_y = gradient(den)
    den_grad_mag = np.sqrt(den_x**2 + den_y**2)
    # pl.ion()
    # pl.figure()
    # pl.imshow(bmag, interpolation='nearest', cmap='hot')
    surf = _cp.TopoSurface(psi_arr)
    regions = surf.get_minmax_regions()
    pl.figure(figsize=(9,12))
    nr = 6
    nc = 2
    ctr = 0
    for (minmax, pss, type), region in regions.items():
        if len(region) < thresh:
            continue
        ctr += 1
        print ctr
        gs = gridspec.GridSpec(nr, nc, width_ratios=[1,2], hspace=0.0)
        nbr_func = lambda t: _cp.neighbors6(t[0], t[1], nx, ny)
        lset = field_trace._level_set(psi_arr, level_val=psi_arr[pss],
                position=pss, neighbors_func=nbr_func)
        bx = -psi_grad_y
        by = psi_grad_x
        br, btheta = cartesian_to_polar(bx, by, minmax[0], minmax[1])
        region = expand_region_circ(psi_arr, region, minmax, extra=2.0)
        lset = lset.intersection(region)
        # psi
        ax = pl.subplot(gs[0])
        ax.set_title("Field & separatrix", size='x-large')
        field_region_image(ax, psi_arr, region, minmax, lset, title=r'$\psi$')
        # ax = pl.subplot2grid((nr, nc), (0, 1))
        ax = pl.subplot(gs[1])
        ax.set_title(r"Field vs. $r/\rho_s$ (id={0})".format(ctr), size='x-large')
        plot_theta_sectors(ax, psi_arr, minmax, region, ntheta, title=r'$\psi$ vs. $r/\rho_s$')
        # b_theta
        ax = pl.subplot(gs[2])
        # field_region_image(ax, btheta, region, minmax, lset, title=r'$B_{\theta}$')
        field_region_image(ax, bmag, region, minmax, lset, title=r'$|B|$')
        ax = pl.subplot(gs[3])
        # btheta_sectors = plot_theta_sectors(ax, btheta, minmax, region, ntheta, title=r'$B_{\theta}$ vs. $r/\rho_s$')
        plot_theta_sectors(ax, bmag, minmax, region, ntheta, title=r'$|B|$ vs. $r/\rho_s$')
        # b_shear
        # pl.subplot(nr, nc, 5)
        # field_region_image(btheta, region, minmax, lset, title=r'$B_{\theta}$')
        # pl.subplot(nr, nc, 6)
        # plot_sect_derivs_by_r(btheta_sectors, title=r'$\partial_{r} \frac{B_{\theta}}{r}$ vs. $r/\rho_s$')
        # den
        ax = pl.subplot(gs[4])
        field_region_image(ax, den, region, minmax, lset, title=r'$n$')
        ax = pl.subplot(gs[5])
        plot_theta_sectors(ax, den, minmax, region, ntheta, title=r'$n$ vs. $r/\rho_s$')
        # den_grad
        ax = pl.subplot(gs[6])
        field_region_image(ax, den_grad_mag, region, minmax, lset, title=r'$|\nabla n|$')
        ax = pl.subplot(gs[7])
        plot_theta_sectors(ax, den_grad_mag, minmax, region, ntheta, title=r'$|\nabla n|$ vs. $r/\rho_s$')
        # cur
        ax = pl.subplot(gs[8])
        field_region_image(ax, cur, region, minmax, lset, title=r'$J$')
        ax = pl.subplot(gs[9])
        plot_theta_sectors(ax, cur, minmax, region, ntheta, title=r'$J$ vs. $r/\rho_s$')
        # hessian
        ax = pl.subplot(gs[10])
        field_region_image(ax, hess, region, minmax, lset, title=r'$H(\psi)$', xvis=True)
        ax = pl.subplot(gs[11])
        ax.set_xlabel(r'$r/\rho_s$', size='x-large')
        plot_theta_sectors(ax, hess, minmax, region, ntheta, title=r'$H(\psi)$ vs. $r/\rho_s$', xvis=True)
        # raw_input('enter to continue')
        pl.savefig('{0}_{1:04d}.pdf'.format(save_basename, ctr))
        pl.clf()
    pl.close('all')