def tri_rods(): # Import the ModeSolver defined in the mpb_tri_rods.py example from mpb_tri_rods import ms as tr_ms efields = [] # Band function to collect the efields def get_efields(tr_ms, band): efields.append(tr_ms.get_efield(band)) tr_ms.run_tm( mpb.output_at_kpoint(mp.Vector3(1 / -3, 1 / 3), mpb.fix_efield_phase, get_efields)) # Create an MPBData instance to transform the efields md = mpb.MPBData(rectify=True, resolution=32, periods=3) converted = [] for f in efields: # Get just the z component of the efields f = f[..., 0, 2] converted.append(md.convert(f)) tr_ms.run_te() eps = tr_ms.get_epsilon() plt.imshow(eps.T, interpolation='spline36', cmap='binary') plt.axis('off') plt.show() md = mpb.MPBData(rectify=True, resolution=32, periods=3) rectangular_data = md.convert(eps) plt.imshow(rectangular_data.T, interpolation='spline36', cmap='binary') plt.axis('off') plt.show() for i, f in enumerate(converted): plt.subplot(331 + i) plt.contour(rectangular_data.T, cmap='binary') plt.imshow(np.real(f).T, interpolation='spline36', cmap='RdBu', alpha=0.9) plt.axis('off') plt.show()
def diamond(): dpwr = [] def get_dpwr(ms, band): dpwr.append(ms.get_dpwr(band)) d_ms.run(mpb.output_at_kpoint(mp.Vector3(0, 0.625, 0.375), get_dpwr)) md = mpb.MPBData(rectify=True, periods=2, resolution=32) converted_dpwr = [md.convert(d) for d in dpwr]
def main(): efields = [] # Band function to collect the efields def get_efields(ms, band): efields.append(ms.get_efield(band, output=True)) ms.run_tm(mpb.output_at_kpoint(mp.Vector3(1 / -3, 1 / 3), mpb.fix_efield_phase, get_efields)) # Create an MPBData instance to transform the efields md = mpb.MPBData(ms.get_lattice(), rectify=True, resolution=32, periods=3) converted = [] for f in efields: # Get just the z component of the efields f = f[:, :, 2] converted.append(md.convert(f, ms.k_points[10])) ms.run_te() eps = ms.get_epsilon() plt.imshow(eps.T, interpolation='spline36', cmap='binary') plt.axis('off') plt.show() md = mpb.MPBData(ms.get_lattice(), rectify=True, resolution=32, periods=3) rectangular_data = md.convert(eps) plt.imshow(rectangular_data.T, interpolation='spline36', cmap='binary') plt.axis('off') plt.show() for i, f in enumerate(converted): plt.subplot(331 + i) plt.contour(rectangular_data.T, cmap='binary') plt.imshow(np.real(f).T, interpolation='spline36', cmap='RdBu', alpha=0.9) plt.axis('off') plt.show()
def diamond(): # Import the ModeSolver from the mpb_diamond.py example from mpb_diamond import ms as d_ms dpwr = [] def get_dpwr(ms, band): dpwr.append(ms.get_dpwr(band)) d_ms.run(mpb.output_at_kpoint(mp.Vector3(0, 0.625, 0.375), get_dpwr)) md = mpb.MPBData(rectify=True, periods=2, resolution=32) converted_dpwr = [md.convert(d) for d in dpwr]
def data_visualization(modal_solver: mpb.ModeSolver): """Plot lattice structure and band diagram(Dispersion relation)""" plt.figure() ax1 = plt.subplot(121) md = mpb.MPBData(rectify=True, periods=3, resolution=64) eps = modal_solver.get_epsilon() converted_eps = md.convert(eps) ax1.imshow(converted_eps.T) ax1.axis('off') ax1.set_title('Relative Permittivity') ax2 = plt.subplot(122) mu = modal_solver.get_mu() couverted_mu = md.convert(mu) ax2.imshow(couverted_mu.T) ax2.set_title('Relative Permittivity') ax2.axis('off')
def show_geometry_2d(sim_or_solver, **mpb_kwargs): if isinstance(sim_or_solver, mp.Simulation): print('Deprecation: use sim.plot2D for mp.Simulation objects') sim = sim_or_solver sim.plot2D() # if not sim._is_initialized: # sim.init_sim() # # sim.run(until=.0) # eps_data = sim.get_array(center=mp.Vector3(), size=sim.cell_size, component=mp.Dielectric) # if len(eps_data.shape) == 3: # eps_data = eps_data[:, :, int(eps_data.shape[2] / 2)] # # eps_data = eps_data[:, int(eps_data.shape[1] / 2), :] # for x-z plane elif isinstance(sim_or_solver, mpb.ModeSolver): ms = sim_or_solver if not any(p in mpb_kwargs.keys() for p in ['periods', 'x', 'y', 'z']): mpb_kwargs['periods'] = 3 md = mpb.MPBData(rectify=True, resolution=ms.resolution[1], **mpb_kwargs) eps = ms.get_epsilon() # eps_data = md.convert(eps) # make aspect ratios right eps_data = eps
# mp.Cylinder(radius = 0.001*int(sys.argv[1]), center = mp.Vector3( 0, 1/3), material=mp.Medium(epsilon=1)), # mp.Cylinder(radius = 0.001*int(sys.argv[2]), center = mp.Vector3( 0, 0), material=mp.Medium(epsilon=(0.001*int(sys.argv[4]))**2))] geometry_lattice = mp.Lattice(size=mp.Vector3(1, 1), basis1=mp.Vector3(0.5, 3**0.5 / 2), basis2=mp.Vector3(0.5, -3**0.5 / 2)) ms = mpb.ModeSolver(num_bands=num_bands, k_points=k_points, geometry_lattice=geometry_lattice, geometry=geometry, resolution=resolution, default_material=mp.Medium(epsilon=(0.001 * int(sys.argv[3]))**2)) ms.run_te() md = mpb.MPBData(rectify=True, periods=2, resolution=128) eps = ms.get_epsilon() converted_eps = md.convert(eps) plt.figure() plt.imshow(converted_eps, interpolation='spline36', cmap='binary') plt.colorbar() # try: # os.mkdir('./figures/super_cell/'+'r_air_'+str(0.001*int(sys.argv[1]))[:5]+'r_center_'+str(0.001*int(sys.argv[2]))[:5]+'n_center_'+str(0.001*int(sys.argv[4]))[:5]+'n_back_'+str(0.001*int(sys.argv[3]))[:5]) # except: # pass plt.savefig('./temp_eps' + sys.argv[7] + '.png') # plt.savefig('./figures/super_cell/'+'r_air_'+str(0.001*int(sys.argv[1]))[:5]+'r_center_'+str(0.001*int(sys.argv[2]))[:5]+'n_center_'+str(0.001*int(sys.argv[4]))[:5]+'n_back_'+str(0.001*int(sys.argv[3]))[:5]+'/eps.png')
resolution=resolution, num_bands=num_bands, ) #https://mpb.readthedocs.io/en/latest/Python_User_Interface/ ms.run_yeven(mpb.output_at_kpoint(mp.Vector3(0.5,0,0), mpb.fix_efield_phase, mpb.output_efield_z)) #This will output the electric field at the xpoint only tm_freqs = ms.all_freqs tm_gaps = ms.gap_list ms.run_yodd() te_freqs = ms.all_freqs te_gaps = ms.gap_list ###plot geom md = mpb.MPBData(rectify=True, periods=1, resolution=resolution) eps = ms.get_epsilon() converted_eps = md.convert(eps) print(np.shape(converted_eps)) np_converted_eps = np.array(converted_eps) npce = np.array(converted_eps) plot_2Dslice_epsilon(dLoc,npce,0) plot3D_epsilon(dLoc,npce) plot_bands(dLoc,te_freqs, te_gaps, tm_freqs, tm_gaps, c/a_actual*1e-12) save_bands(dLoc,te_freqs, te_gaps, tm_freqs, tm_gaps,c/a_actual*1e-12)
def main(): ms.run_te() te_freqs = ms.all_freqs te_gaps = ms.gap_list ms.run_tm() tm_freqs = ms.all_freqs tm_gaps = ms.gap_list k_points = ms.k_points ms.display_eigensolver_stats() # start plot md = mpb.MPBData(rectify=True, periods=3, resolution=32) eps = ms.get_epsilon() converted_eps = md.convert(eps) import matplotlib matplotlib.use('Agg') # this allows plotting on braid import matplotlib.pyplot as plt # plot epsilon fig, ax = plt.subplots() im = ax.imshow(converted_eps.T, interpolation='spline36', cmap='binary') plt.axis('off') plt.colorbar(im, label='$\epsilon (r)$') plt.savefig('epsilon.png') fig, ax = plt.subplots(figsize=(4, 4)) x = range(len(tm_freqs)) #kz_points = [k[0] for k in k_points] # Plot bands # Scatter plot for multiple y values, see https://stackoverflow.com/a/34280815/2261298 for xz, tmz, tez in zip(x, tm_freqs, te_freqs): ax.scatter([xz] * len(tmz), tmz, color='blue') #ax.scatter([xz]*len(tez), tez, color='red', facecolors='none') ax.plot(tm_freqs, color='blue') #ax.plot(te_freqs, color='red') #ax.set_ylim([0, 1]) ax.set_xlim([x[0], x[-1]]) #ax.set_ylim([0,0.3]) # Plot gaps for gap in tm_gaps: if gap[0] > 1: ax.fill_between(x, gap[1], gap[2], color='blue', alpha=0.2) #for gap in te_gaps: # if gap[0] > 1: # ax.fill_between(x, gap[1], gap[2], color='red', alpha=0.2) # Plot labels #ax.text(12, 0.04, 'TM bands', color='blue', size=15) #ax.text(13.05, 0.235, 'TE bands', color='red', size=15) points_in_between = (len(tm_freqs) - len(vlist)) / (len(vlist) - 1) tick_locs = [i * points_in_between + i for i in range(len(vlist))] ax.set_xticks(tick_locs) ax.set_xticklabels(tick_labs, size=16) ax.set_xlabel('Wave vector ' + '$ka/2\pi$', size=16) ax.set_ylabel('Frequency ' + '$\omega a / 2\pi c$', size=16) ax.grid(True) plt.tight_layout() plt.savefig('bands.png')
] k_points = mp.interpolate(50, k_points) ms = mpb.ModeSolver(geometry=geometry, geometry_lattice=geometry_lattice, k_points=k_points, resolution=resolution, num_bands=num_bands) ms.run_tm( mpb.output_at_kpoint(mp.Vector3(-1. / 3, 1. / 3), mpb.fix_efield_phase, mpb.output_efield_z)) tm_freqs = ms.all_freqs tm_gaps = ms.gap_list ms.run_te() te_freqs = ms.all_freqs te_gaps = ms.gap_list md = mpb.MPBData(rectify=True, periods=3, resolution=32) eps = ms.get_epsilon() converted_eps = md.convert(eps) plt.imshow(converted_eps.T, interpolation='spline36', cmap='binary') plt.axis('off') plt.show() pltd(tm_freqs, te_freqs, tm_gaps, te_gaps) plte(te_freqs, te_gaps) pltm(tm_freqs, tm_gaps)