def mag_shear_rad_scatter(psi_arr): nx, ny = psi_arr.shape # mshear = mag_shear(psi_arr) psi_grad_x, psi_grad_y = gradient(psi_arr) bmag = np.sqrt(psi_grad_x**2 + psi_grad_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() for (minmax, pss, type), region in regions.items(): br, btheta = cartesian_to_polar(-psi_grad_y, psi_grad_x, minmax[0], minmax[1]) if len(region) < 100: continue # region = expand_region(psi_arr, region, ntimes=2) region = expand_region_circ(psi_arr, region, minmax, extra=10.0) # dists, shears = \ # flux_tube_radial_scatter(region, minmax, mshear) dists, bthetas = \ flux_tube_radial_scatter(region, minmax, btheta) dists, bmags = \ flux_tube_radial_scatter(region, minmax, bmag) # dists, fluxes = \ # flux_tube_radial_scatter(region, minmax, psi_arr) pl.subplot(221) pl.scatter(dists, bthetas, c='b', marker='s') pl.grid() pl.title(r'$B_{\theta}$ vs. $r$') pl.subplot(222) pl.scatter(dists, bthetas/dists, c='g', marker='d') pl.grid() pl.title(r'$B_{\theta}/r$ vs. $r$') # nonlin_shear = (shears - bmags / dists) / dists # pl.scatter(dists, nonlin_shear, c='b', marker='s', label=r'$\nabla_{\perp}B$') pl.subplot(224) # psi_cpy = np.zeros_like(psi_arr) btheta_cpy = np.zeros_like(btheta) xs, ys = zip(*region) btheta_cpy[xs, ys] = btheta[xs, ys] pl.imshow(btheta_cpy, interpolation='nearest', cmap='hot') pl.title(r'$B_{\theta}$') pl.subplot(223) bmag_cpy = np.zeros_like(bmag) bmag_cpy[xs, ys] = bmag[xs, ys] pl.imshow(bmag_cpy, interpolation='nearest', cmap='hot') pl.title(r'$|B|$') raw_input('enter to continue') pl.clf() pl.close('all')
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')
def mag_shear_rad_spokes(psi_arr): nx, ny = psi_arr.shape wdist = wraparound_dist_vec(nx, ny) # mshear = mag_shear(psi_arr) psi_grad_x, psi_grad_y = gradient(psi_arr) bmag = np.sqrt(psi_grad_x**2 + psi_grad_y**2) surf = _cp.TopoSurface(psi_arr) regions = surf.get_minmax_regions() pl.ion() pl.figure() for (minmax, pss, type), region in regions.items(): br, btheta = cartesian_to_polar(-psi_grad_y, psi_grad_x, minmax[0], minmax[1]) if len(region) < 100: continue region = expand_region_circ(psi_arr, region, minmax, extra=10.0) spokes = flux_tube_radial_spokes(region, minmax, psi_arr, type) pl.clf() # max_2_bmags = [] max_2_btheta = [] for spoke in spokes: sp_arr = np.array(spoke) xs, ys = sp_arr[:,0], sp_arr[:,1] # bmags = bmag[xs, ys] bthetas = btheta[xs, ys] max_2_btheta.append((bthetas.max(), bthetas, spoke)) max_2_btheta.sort(key=lambda x: x[0], reverse=True) spokes = [sp for (m, bthetas, sp) in max_2_btheta] btheta_cpy = np.zeros_like(btheta) bmag_cpy = np.zeros_like(bmag) br_cpy = np.zeros_like(br) xs, ys = zip(*region) btheta_cpy[xs, ys] = btheta[xs, ys] bmag_cpy[xs, ys] = bmag[xs, ys] br_cpy[xs, ys] = br[xs, ys] for spoke in spokes: sp_arr = np.array(spoke) xs, ys = sp_arr[:,0], sp_arr[:,1] dists = wdist(minmax[0], minmax[1], xs, ys) bthetas = btheta[xs, ys] # mshears = mshear[xs, ys] pl.subplot(221) pl.plot(dists, bthetas, 'o-') # pl.subplot(222) # pl.plot(dists, mshears, 's-') pl.subplot(222) pl.plot(dists[1:], bthetas[1:]/dists[1:], 'd-') pl.subplot(221) pl.grid() pl.title(r'$B_{\theta}$ vs. $r$') # pl.subplot(222) # pl.grid() # pl.title(r'$\nabla_{\perp} B$ vs. $r$') pl.subplot(222) pl.grid() pl.title(r'$B_{\theta}/r$ vs. $r$') pl.subplot(234) pl.imshow(btheta_cpy, interpolation='nearest', cmap='hot') pl.title(r'$B_{\theta}$') pl.subplot(235) pl.imshow(br_cpy, interpolation='nearest', cmap='hot') pl.title(r'$B_{r}$') pl.subplot(236) pl.imshow(bmag_cpy, interpolation='nearest', cmap='hot') pl.title(r'$|B|$') raw_input('enter to continue') pl.clf() pl.close('all')