def nlo_coeffs(efield, indicator, ccnl): """ Returns the nonlinear coupling coeffs after integration efield is assumed to be nonbloch """ (x, y, z) = getscale(mpb) # (x, dx, y, dy, z, dz) = getscale(mpb, retstep=True) Nx = np.size(x) # Ny = np.size(y) # Nz = np.size(z) # Symbols : ((+,+), |+|), ((+,+), |-|) # compute the field quantities e2 = np.abs(efield.x)**2 + np.abs(efield.y)**2 + np.abs(efield.z)**2 e4 = e2**2 ex4 = np.abs(efield.x)**4 ey4 = np.abs(efield.y)**4 ez4 = np.abs(efield.z)**4 e2complex = efield.x**2 + efield.y**2 + efield.z**2 ex2 = np.abs(efield.x)**2 ey2 = np.abs(efield.y)**2 ez2 = np.abs(efield.z)**2 ex2complex = efield.x**2 ey2complex = efield.y**2 ez2complex = efield.z**2 for cc_type in ccnl.keys(): cc = np.zeros(Nx, dtype=complex) print('Solving for NL coefficient {0}'.format(cc_type)) if cc_type == (('+', '+'), '|+|'): integrand = indicator*(e4 + 2*(ex4 + ey4 + ez4)) elif cc_type == (('+', '+'), '|-|'): integrand = indicator*e4 elif cc_type == (('+', '-'), ('+', '-')): integrand = indicator*(ex4 + ey4 + ez4) elif cc_type == (('+', '+'), ('+', '-')): integrand = indicator*((e2complex + 2*ex2complex)*ex2 + (e2complex + 2*ey2complex)*ey2 + (e2complex + 2*ez2complex)*ez2) elif cc_type == (('+', '-'), '|-|'): integrand = indicator*((e2 + 2*ex2)*np.conjugate(ex2complex) + (e2 + 2*ey2)*np.conjugate(ey2complex) + (e2 + 2*ez2)*np.conjugate(ez2complex)) elif cc_type == (('+', '-'), '|+|'): integrand = indicator*(e2*np.conjugate(ex2complex + ey2complex + ez2complex)) elif cc_type == (('+', '+'), ('-', '+')): integrand = indicator*(np.conjugate(ex2complex)*ex2 + np.conjugate(ey2complex)*ey2 + np.conjugate(ez2complex)*ez2) elif cc_type == (('+', '-'), ('-', '+')): integrand = indicator*(np.conjugate((e2complex + 2*ex2complex)*ex2complex + (e2complex + 2*ey2complex)*ey2complex + (e2complex + 2*ez2complex)*ez2complex)) for i in range(Nx): cc[i] = spintegrate.simps(spintegrate.simps(integrand[i,:,:], z, axis=1), y, axis=0) # cc[i] = spintegrate.simps(spintegrate.simps(integrand[i,:,:], dx=dz, axis=1), dx=dy, axis=0) ccnl[cc_type] = cc # modified in place. [:] is NECESSARY print('Done!')
def plotfields(mpb, field_type, kindex=None, band=None, comp=None, mpbpostprocess=False, epsilon_contour_options={}, figsize=None, field_file=None, epsilon_file=None, plot_options={}): """ Plots fields Inputs ------ mpbpostprocess : False (default), assumes no post processing of the fields has been performed using mpb-data field_type : 'e', 'epsilon', 'epsilon-ft' plot_options : specifies extra plot options such as axes limits """ if figsize is not None: plt.figure(figsize=figsize) else: plt.figure() if epsilon_file is None: epsilon = readfield(mpb, field_type='epsilon_isotropic_trace', mpbpostprocess=mpbpostprocess) elif isinstance(epsilon_file, str): epsilon = readfield(mpb, field_type='epsilon_isotropic_trace', mpbpostprocess=mpbpostprocess, field_file=epsilon_file) if field_type == 'e': if field_file is None: E = readfield(mpb, kindex, band, field_type, mpbpostprocess=mpbpostprocess) elif isinstance(field_file, str): E = readfield(mpb, kindex, band, field_type, mpbpostprocess=mpbpostprocess, field_file=field_file) E.create_complex() if comp is None: E2 = np.abs(E.x)**2 + np.abs(E.y)**2 + np.abs(E.z)**2 if E2.ndim == 1: # (x) = getscale(mpb) # (xgrid, ygrid) = np.meshgrid(x, x) # creates a E2.ndim x E2.ndim square grid E2grid = np.tile(E2, (E2.shape[0], 1)) epsilon_grid = np.tile(epsilon.dset, (epsilon.dset.shape[0], 1)) # plt.contourf(xgrid, ygrid, E2grid) # plt.pcolormesh(E2grid) plt.imshow(E2grid) plt.colorbar() # plt.contour(xgrid, ygrid, epsilon_grid, colors='k', linewidths=lw) plt.contour(epsilon_grid, colors='w', **epsilon_contour_options) ax = plt.gca() ax.set_xticks(()) ax.set_yticks(()) plt.xlim(0, E2grid.shape[0]-1) plt.ylim(0, E2grid.shape[1]-1) elif E2.ndim == 2: # (x, y) = getscale(mpb) # (xgrid, ygrid) = np.meshgrid(x, y) # plt.contourf(xgrid, ygrid, E2) # plt.pcolormesh(E2) plt.imshow(E2) plt.colorbar() # plt.contour(xgrid, ygrid, epsilon.dset, colors='k', linewidths=lw) plt.contour(epsilon.dset, colors='w', **epsilon_contour_options) ax = plt.gca() ax.set_xticks(()) ax.set_yticks(()) # Determine which grid is on the axis. If Ny > Nx, the yindices # are placed on the x-axis if E2.shape[0] >= E2.shape[1]: plt.xlim(0, E2.shape[0]-1) plt.ylim(0, E2.shape[1]-1) else: plt.xlim(0, E2.shape[1]-1) plt.ylim(0, E2.shape[0]-1) elif E2.ndim == 3: # ASSUME SLAB GEOMETRY # xy cross section plt.imshow(E2[:, :, E2.shape[2]/2], aspect='equal') plt.colorbar() plt.contour(epsilon.dset[:, :, epsilon.dset.shape[2]/2], colors='w', **epsilon_contour_options) ax = plt.gca() ax.set_xticks(()) ax.set_yticks(()) # Determine which grid is on the axis. If Ny > Nx, the yindices # are placed on the x-axis if E2.shape[0] >= E2.shape[1]: plt.xlim(0, E2.shape[0]-1) plt.ylim(0, E2.shape[1]-1) else: plt.xlim(0, E2.shape[1]-1) plt.ylim(0, E2.shape[0]-1) elif field_type == 'epsilon': if len(epsilon.dset.shape) == 1: epsilon_grid = np.tile(epsilon.dset, (epsilon.dset.shape[0], 1)) # epsilon_grid_ft = fftshift(fft2(epsilon_grid)) (x) = getscale(mpb) (xgrid, ygrid) = np.meshgrid(x, x) # plt.pcolormesh(xgrid, ygrid, epsilon_grid, cmap='Greys') plt.imshow(epsilon_grid, cmap='Greys') plt.colorbar() ax = plt.gca() ax.set_xticks(()) ax.set_yticks(()) plt.xlim(0, epsilon_grid.shape[0]-1) plt.ylim(0, epsilon_grid.shape[1]-1) # plt.figure() # plt.imshow(np.abs(epsilon_grid_ft)**2) # plt.colorbar() elif len(epsilon.dset.shape) == 2: # Greys or gray # plt.pcolor(epsilon.dset, cmap='Greys') # filter out DC_component # epsilon_ft[np.unravel_index(np.argmax(epsilon_ft), np.shape(epsilon_ft))] = 0 plt.imshow(epsilon.dset, cmap='Greys') plt.colorbar() plt.xlim(0, epsilon.dset.shape[0]-1) plt.ylim(0, epsilon.dset.shape[1]-1) ax = plt.gca() ax.set_xticks(()) ax.set_yticks(()) elif len(epsilon.dset.shape) == 3: # ASSUME PC SLAB GEOMETRY ONLY # plot in middle of slab # xy cross section # plt.imshow(epsilon.dset[:, :, epsilon.dset.shape[2]/2], cmap='Greys', aspect='equal') # yz cross section # plt.imshow(epsilon.dset[epsilon.dset.shape[0]/2, :, :], cmap='Greys', aspect='equal') plt.imshow(epsilon.dset[0, :, :], cmap='Greys', aspect='equal') plt.colorbar() # plt.xlim(0, epsilon.dset.shape[0]-1) # plt.ylim(0, epsilon.dset.shape[1]-1) ax = plt.gca() ax.set_xticks(()) ax.set_yticks(()) elif field_type == 'epsilon-ft': if len(epsilon.dset.shape) == 1: pass elif len(epsilon.dset.shape) == 2: # zero pad epsilon_ft = fftshift(fft2(np.pad(epsilon.dset[:,:], (512, 512), 'constant'))) plt.imshow((np.abs(epsilon_ft)/np.max(np.abs(epsilon_ft)))**2, cmap='Greys', vmin=0, vmax=0.1) ax = plt.gca() ax.set_xticks(()) ax.set_yticks(()) elif len(epsilon.dset.shape) == 3: pass ax = plt.gca() ax.set_xticks(()) ax.set_yticks(()) if 'xlims' in plot_options: xa = plot_options['xlims'][0] xb = plot_options['xlims'][1] plt.xlim(xa, xb) if 'ylims' in plot_options: ya = plot_options['ylims'][0] yb = plot_options['ylims'][1] plt.ylim(ya, yb) epsilon.close()