def setUp(self): resolution = 40 cell_size = mp.Vector3(z=10) absorber_layers = [mp.Absorber(1, direction=mp.Z)] sources = [mp.Source(src=mp.GaussianSource(1 / 0.803, fwidth=0.1), center=mp.Vector3(), component=mp.Ex)] self.sim = mp.Simulation(cell_size=cell_size, resolution=resolution, dimensions=1, default_material=Al, boundary_layers=absorber_layers, sources=sources)
def test_set_materials(self): def change_geom(sim): t = sim.meep_time() fn = t * 0.02 geom = [ mp.Cylinder(radius=3, material=mp.Medium(index=3.5), center=mp.Vector3(fn, fn)), mp.Ellipsoid(size=mp.Vector3(1, 2, mp.inf), center=mp.Vector3(fn, fn)) ] sim.set_materials(geometry=geom) c = mp.Cylinder(radius=3, material=mp.Medium(index=3.5)) e = mp.Ellipsoid(size=mp.Vector3(1, 2, mp.inf)) sources = mp.Source(src=mp.GaussianSource(1, fwidth=0.1), component=mp.Hz, center=mp.Vector3()) symmetries = [mp.Mirror(mp.X, -1), mp.Mirror(mp.Y, -1)] sim = mp.Simulation(cell_size=mp.Vector3(10, 10), geometry=[c, e], boundary_layers=[mp.PML(1.0)], sources=[sources], symmetries=symmetries, resolution=16) eps = {'arr1': None, 'arr2': None} def get_arr1(sim): eps['arr1'] = sim.get_array(mp.Volume(mp.Vector3(), mp.Vector3(10, 10)), component=mp.Dielectric) def get_arr2(sim): eps['arr2'] = sim.get_array(mp.Volume(mp.Vector3(), mp.Vector3(10, 10)), component=mp.Dielectric) sim.run(mp.at_time(50, get_arr1), mp.at_time(100, change_geom), mp.at_end(get_arr2), until=200) self.assertFalse(np.array_equal(eps['arr1'], eps['arr2']))
def free_space_radiation(self): sxy = 4 dpml = 1 cell_size = mp.Vector3(sxy + 2 * dpml, sxy + 2 * dpml) pml_layers = [mp.PML(dpml)] fcen = 1 / self.wvl sources = [ mp.Source(src=mp.GaussianSource(fcen, fwidth=0.2 * fcen), center=mp.Vector3(), component=self.src_cmpt) ] if self.src_cmpt == mp.Hz: symmetries = [mp.Mirror(mp.X, phase=-1), mp.Mirror(mp.Y, phase=-1)] elif self.src_cmpt == mp.Ez: symmetries = [mp.Mirror(mp.X, phase=+1), mp.Mirror(mp.Y, phase=+1)] else: symmetries = [] sim = mp.Simulation(cell_size=cell_size, resolution=self.resolution, sources=sources, symmetries=symmetries, boundary_layers=pml_layers, default_material=mp.Medium(index=self.n)) nearfield_box = sim.add_near2far( fcen, 0, 1, mp.Near2FarRegion(center=mp.Vector3(0, +0.5 * sxy), size=mp.Vector3(sxy, 0)), mp.Near2FarRegion(center=mp.Vector3(0, -0.5 * sxy), size=mp.Vector3(sxy, 0), weight=-1), mp.Near2FarRegion(center=mp.Vector3(+0.5 * sxy, 0), size=mp.Vector3(0, sxy)), mp.Near2FarRegion(center=mp.Vector3(-0.5 * sxy, 0), size=mp.Vector3(0, sxy), weight=-1)) sim.run(until_after_sources=mp.stop_when_dft_decayed()) Pr = self.radial_flux(sim, nearfield_box, self.r) return Pr
def test_load_dump_structure(self): from meep.materials import Al resolution = 50 cell = mp.Vector3(5, 5) sources = mp.Source(src=mp.GaussianSource(1, fwidth=0.2), center=mp.Vector3(), component=mp.Ez) one_by_one = mp.Vector3(1, 1, mp.inf) geometry = [mp.Block(material=Al, center=mp.Vector3(-1.5, -1.5), size=one_by_one), mp.Block(material=mp.Medium(epsilon=13), center=mp.Vector3(1.5, 1.5), size=one_by_one)] pml_layers = [mp.PML(0.5)] sim = mp.Simulation(resolution=resolution, cell_size=cell, boundary_layers=pml_layers, geometry=geometry, sources=[sources]) sample_point = mp.Vector3(0.12, -0.29) ref_field_points = [] def get_ref_field_point(sim): p = sim.get_field_point(mp.Ez, sample_point) ref_field_points.append(p.real) sim.run(mp.at_every(5, get_ref_field_point), until=50) dump_fn = 'test_load_dump_structure.h5' sim.dump_structure(dump_fn) sim = mp.Simulation(resolution=resolution, cell_size=cell, boundary_layers=pml_layers, sources=[sources], load_structure=dump_fn) field_points = [] def get_field_point(sim): p = sim.get_field_point(mp.Ez, sample_point) field_points.append(p.real) sim.run(mp.at_every(5, get_field_point), until=50) for ref_pt, pt in zip(ref_field_points, field_points): self.assertAlmostEqual(ref_pt, pt) mp.all_wait() if mp.am_master(): os.remove(dump_fn)
def test_custom_source(self): n = 3.4 w = 1 r = 1 pad = 4 dpml = 2 sxy = 2 * (r + w + pad + dpml) cell = mp.Vector3(sxy, sxy) geometry = [ mp.Cylinder(r + w, material=mp.Medium(index=n)), mp.Cylinder(r, material=mp.air) ] boundary_layers = [mp.PML(dpml)] resolution = 10 fcen = 0.15 df = 0.1 # Bump function def my_src_func(t): if t > 0 and t < 2: return math.exp(-1 / (1 - ((t - 1)**2))) return 0j sources = [ mp.Source(src=mp.CustomSource(src_func=my_src_func, end_time=100), component=mp.Ez, center=mp.Vector3(r + 0.1)) ] symmetries = [mp.Mirror(mp.Y)] sim = mp.Simulation(cell_size=cell, resolution=resolution, geometry=geometry, boundary_layers=boundary_layers, sources=sources, symmetries=symmetries) h = mp.Harminv(mp.Ez, mp.Vector3(r + 0.1), fcen, df) sim.run(mp.after_sources(h), until_after_sources=200) fp = sim.get_field_point(mp.Ez, mp.Vector3(1)) self.assertAlmostEqual(fp, -0.021997617628500023 + 0j)
def gaussian_beam(sim, src_time, width, polarization, amplitude=1.0): # assume first boundary layer is the full PML pml_thickness = sim.boundary_layers[0].thickness cell = sim.cell_size def amp_func(pos): return gaussian_amp(pos, width) source = meep.Source(src_time, component=polarization, center=meep.Vector3(0, 0, -cell[2] / 2 + pml_thickness), size=meep.Vector3(cell[0], cell[1], 0), amplitude=amplitude, amp_func=amp_func) sim.add_source(source)
def setUp(self): resolution = 20 cell = mp.Vector3(10, 10) pml_layers = mp.PML(1.0) fcen = 1.0 df = 1.0 sources = mp.Source(src=mp.GaussianSource(fcen, fwidth=df), center=mp.Vector3(), component=mp.Ez) self.sim = mp.Simulation(resolution=resolution, cell_size=cell, boundary_layers=[pml_layers], sources=[sources]) fr = mp.ForceRegion(mp.Vector3(y=1.27), mp.Y, size=mp.Vector3(4.38)) self.myforce = self.sim.add_force(fcen, 0, 1, fr)
def setUp(self): r = 0.36 d = 1.4 sy = 6 pad = 2 dpml = 1 sx = (2 * (pad + dpml + 3)) + d - 1 cell = mp.Vector3(sx, sy, 0) blk = mp.Block(size=mp.Vector3(1e20, 1.2, 1e20), material=mp.Medium(epsilon=13)) geometry = [blk] for i in range(3): geometry.append(mp.Cylinder(r, center=mp.Vector3(d / 2 + i))) for i in range(3): geometry.append(mp.Cylinder(r, center=mp.Vector3(d / -2 - i))) sources = [ mp.Source(mp.GaussianSource(0.25, fwidth=0.2), mp.Hz, mp.Vector3()) ] self.sim = mp.Simulation(cell_size=cell, geometry=geometry, sources=sources, boundary_layers=[mp.PML(dpml)], resolution=20) self.x_min = -0.25 * sx self.x_max = +0.25 * sx self.y_min = -0.15 * sy self.y_max = +0.15 * sy self.size_1d = mp.Vector3(self.x_max - self.x_min) self.center_1d = mp.Vector3((self.x_min + self.x_max) / 2) self.size_2d = mp.Vector3(self.x_max - self.x_min, self.y_max - self.y_min) self.center_2d = mp.Vector3((self.x_min + self.x_max) / 2, (self.y_min + self.y_max) / 2)
def main(args): sx = 4.0 #spatial extent along x including pmls (μm) sy = 4 sz = 0 dpml = 1.0 cell = mp.Vector3(sx, sy, sz) pml_layers = [mp.PML(dpml)] geometry = [ mp.Block(mp.Vector3(mp.inf, 0.050, 0), center=mp.Vector3(0, 0.025, 0), material=Ag) ] resolution = args.res wvlmax = 1.0 fmin = 1 / wvlmax wvlmin = 0.100 fmax = 1 / wvlmin wvl = args.wvl fcen = 1 / wvl df = fmax - fmin nfreq = 1 print("wavelength =", wvl, "μm") print("center frequency =", fcen, "1/μm") source = [ mp.Source(mp.GaussianSource(fcen, 0, nfreq), component=mp.Ex, center=mp.Vector3(0, -0.050, 0)) ] symmetries = [mp.Mirror(mp.X), mp.Mirror(mp.Z)] sim = mp.Simulation(cell_size=cell, geometry=geometry, sources=source, resolution=resolution, boundary_layers=pml_layers) pt = mp.Vector3(0, -0.050, 0) sim.run(mp.dft_ldos(fcen, 0, nfreq), until_after_sources=mp.stop_when_fields_decayed( 20, mp.Ex, pt, 1e-3)) eps_data = sim.get_array(center=mp.Vector3(), size=cell, component=mp.Dielectric) ey_data = sim.get_array(center=mp.Vector3(), size=cell, component=mp.Ey)
def test_divide_parallel_processes(self): resolution = 20 sxy = 4 dpml = 1 cell = mp.Vector3(sxy+2*dpml,sxy+2*dpml) pml_layers = [mp.PML(dpml)] n = mp.divide_parallel_processes(2) fcen = 1.0/(n+1) sources = [mp.Source(src=mp.GaussianSource(fcen,fwidth=0.2*fcen), center=mp.Vector3(), component=mp.Ez)] symmetries = [mp.Mirror(mp.X), mp.Mirror(mp.Y)] self.sim = mp.Simulation(cell_size=cell, resolution=resolution, sources=sources, symmetries=symmetries, boundary_layers=pml_layers) flux_box = self.sim.add_flux(fcen, 0, 1, mp.FluxRegion(mp.Vector3(y=0.5*sxy), size=mp.Vector3(sxy)), mp.FluxRegion(mp.Vector3(y=-0.5*sxy), size=mp.Vector3(sxy), weight=-1), mp.FluxRegion(mp.Vector3(0.5*sxy), size=mp.Vector3(y=sxy)), mp.FluxRegion(mp.Vector3(-0.5*sxy), size=mp.Vector3(y=sxy), weight=-1)) self.sim.run(until_after_sources=30) tot_flux = mp.get_fluxes(flux_box)[0] tot_fluxes = mp.merge_subgroup_data(tot_flux) fcens = mp.merge_subgroup_data(fcen) self.assertEqual(fcens[0],1) self.assertEqual(fcens[1],0.5) self.assertAlmostEqual(tot_fluxes[0], 9.8628728533) self.assertAlmostEqual(tot_fluxes[1], 19.6537275387)
def test_material_dispersion_with_user_material(self): susceptibilities = [ mp.LorentzianSusceptibility(frequency=1.1, gamma=1e-5, sigma=0.5), mp.LorentzianSusceptibility(frequency=0.5, gamma=0.1, sigma=2e-5) ] def mat_func(p): return mp.Medium(epsilon=2.25, E_susceptibilities=susceptibilities) fcen = 1.0 df = 2.0 sources = mp.Source(mp.GaussianSource(fcen, fwidth=df), component=mp.Ez, center=mp.Vector3()) kmin = 0.3 kmax = 2.2 k_interp = 5 kpts = mp.interpolate(k_interp, [mp.Vector3(kmin), mp.Vector3(kmax)]) self.sim = mp.Simulation(cell_size=mp.Vector3(), geometry=[], sources=[sources], material_function=mat_func, default_material=mp.air, resolution=20) all_freqs = self.sim.run_k_points(200, kpts) res = [f.real for fs in all_freqs for f in fs] expected = [ 0.1999342026399106, 0.41053963810375294, 0.6202409070451909, 0.8285737385146619, 1.0350739448523063, 1.2392775309110078, 1.4407208712852109, ] np.testing.assert_allclose(expected, res)
def setUp(cls): cls.resolution = 20 # pixels/μm cls.dpml = 0.5 # thickness of PML cls.L = 6.0 # length of non-PML region cls.n = 2.4 # refractive index of surrounding medium cls.wvl = 1.0 # wavelength (in vacuum) cls.fcen = 1 / cls.wvl cls.sources = [ mp.Source(src=mp.GaussianSource(cls.fcen, fwidth=0.2 * cls.fcen), component=mp.Ex, center=mp.Vector3()) ] cls.symmetries = [ mp.Mirror(direction=mp.X, phase=-1), mp.Mirror(direction=mp.Y), mp.Mirror(direction=mp.Z) ]
def test_absorber_2d(self): source = mp.Source( src=mp.GaussianSource(frequency=0.1, fwidth=0.1), component=mp.Hz, center=mp.Vector3() ) sim = mp.Simulation( cell_size=mp.Vector3(20, 20, 0), resolution=10, sources=[source], boundary_layers=[mp.Absorber(5)] ) sim.run(until_after_sources=1000) v = mp.Vector3(4.13, 3.75, 0) p = sim.get_field_point(mp.Hz, v) self.assertAlmostEqual(-4.058476603571745e-11, p.real)
def main(): n = 3.4 # index of waveguide w = 1 # width of waveguide r = 1 # inner radius of ring pad = 4 # padding between waveguide and edge of PML dpml = 2 # thickness of PML sxy = 2 * (r + w + pad + dpml) # cell size # Create a ring waveguide by two overlapping cylinders - later objects # take precedence over earlier objects, so we put the outer cylinder first. # and the inner (air) cylinder second. c1 = mp.Cylinder(radius=r + w, material=mp.Medium(index=n)) c2 = mp.Cylinder(radius=r) # If we don't want to excite a specific mode symmetry, we can just # put a single point source at some arbitrary place, pointing in some # arbitrary direction. We will only look for Ez-polarized modes. fcen = 0.15 # pulse center frequency df = 0.1 # pulse width (in frequency) src = mp.Source(mp.GaussianSource(fcen, fwidth=df), mp.Hz, mp.Vector3(r + 0.1)) sim = mp.Simulation(cell_size=mp.Vector3(sxy, sxy), geometry=[c1, c2], sources=[src], resolution=10, symmetries=[mp.Mirror(mp.Y)], boundary_layers=[mp.PML(dpml)]) h = mp.Harminv(mp.Ex, mp.Vector3(r + 0.1), fcen, df) sim.run(mp.after_sources(h), until_after_sources=200) print(f'Harminv found {len(h.modes)} resonant modes(s).') for mode in h.modes: print(f'The resonant mode with f={mode.freq} has Q={mode.Q}') sim.plot2D(fields=mp.Ex, field_parameters={'alpha':0.8, 'cmap':'RdBu', 'interpolation':'none'}, boundary_parameters={'hatch':'o', 'linewidth':1.5, 'facecolor':'y', 'edgecolor':'b', 'alpha':0.3}) plt.show()
def place_adjoint_sources(self, qweights): # extract temporal envelope of forward sources from existing simulation envelope = self.sim.sources[0].src freq = envelope.frequency omega = 2.0*np.pi*freq factor = 2.0j*omega if callable(getattr(envelope, "fourier_transform", None)): factor /= envelope.fourier_transform(freq) ################################################## # loop over all objective quantities, adding # appropriately-weighted adjoint sources for each # quantity ################################################## self.sim.reset_meep() self.sim.change_sources([]) nf=0 for qr, qw in zip(self.obj_func.qrules, qweights): if qw==0.0: continue code, mode, cell=qr.code, qr.mode, self.dft_cells[qr.ncell] EH = cell.get_EH_slices(nf=nf, label='forward') if mode==0 \ else cell.get_eigenfield_slices(mode=mode, nf=0) components = cell.components x, y, z, w = cell.xyzw[0], cell.xyzw[1], cell.xyzw[2], cell.xyzw[3] shape = [np.shape(q)[0] for q in [x,y,z]] if code in 'PM': sign = 1.0 if code=='P' else -1.0 signs=[+1.0,-1.0,+1.0*sign,-1.0*sign] self.sim.sources+=[mp.Source(envelope, cell.components[3-nc], cell.center, cell.size, amplitude=signs[nc]*factor*qw, amp_data=np.reshape(np.conj(EH[nc]),shape) ) for nc in range(len(components)) ] self.sim.force_complex_fields=True
def main(): # Some parameters to describe the geometry: eps = 13 # dielectric constant of waveguide w = 1.2 # width of waveguide r = 0.36 # radius of holes # The cell dimensions sy = 12 # size of cell in y direction (perpendicular to wvg.) dpml = 1 # PML thickness (y direction only!) cell = mp.Vector3(1, sy) b = mp.Block(size=mp.Vector3(mp.inf, w, mp.inf), material=mp.Medium(epsilon=eps)) c = mp.Cylinder(radius=r) fcen = 0.25 # pulse center frequency df = 1.5 # pulse freq. width: large df = short impulse s = mp.Source(src=mp.GaussianSource(fcen, fwidth=df), component=mp.Hz, center=mp.Vector3(0.1234)) sym = mp.Mirror(direction=mp.Y, phase=-1) sim = mp.Simulation(cell_size=cell, geometry=[b, c], sources=[s], symmetries=[sym], boundary_layers=[mp.PML(dpml, direction=mp.Y)], resolution=20) kx = False # if true, do run at specified kx and get fields k_interp = 19 # # k-points to interpolate, otherwise if kx: sim.k_point = mp.Vector3(kx) sim.run( mp.at_beginning(mp.output_epsilon), mp.after_sources(mp.Harminv(mp.Hz, mp.Vector3(0.1234), fcen, df)), until_after_sources=300 ) sim.run(mp.at_every(1 / fcen / 20, mp.output_hfield_z), until=1 / fcen) else: sim.run_k_points(300, mp.interpolate(k_interp, [mp.Vector3(), mp.Vector3(0.5)]))
def setUp(self): resolution = 50 sxy = 4 dpml = 1 cell = mp.Vector3(sxy + 2 * dpml, sxy + 2 * dpml, 0) pml_layers = mp.PML(dpml) fcen = 1.0 df = 0.4 self.src_cmpt = mp.Ez sources = mp.Source( src=mp.GaussianSource(fcen, fwidth=df), center=mp.Vector3(), component=self.src_cmpt ) if self.src_cmpt == mp.Ex: symmetries = [mp.Mirror(mp.Y)] elif self.src_cmpt == mp.Ey: symmetries = [mp.Mirror(mp.X)] elif self.src_cmpt == mp.Ez: symmetries = [mp.Mirror(mp.X), mp.Mirror(mp.Y)] self.sim = mp.Simulation( cell_size=cell, resolution=resolution, sources=[sources], symmetries=symmetries, boundary_layers=[pml_layers] ) self.nearfield = self.sim.add_near2far( fcen, 0, 1, mp.Near2FarRegion(mp.Vector3(0, 0.5 * sxy), size=mp.Vector3(sxy)), mp.Near2FarRegion(mp.Vector3(0, -0.5 * sxy), size=mp.Vector3(sxy), weight=-1.0), mp.Near2FarRegion(mp.Vector3(0.5 * sxy), size=mp.Vector3(0, sxy)), mp.Near2FarRegion(mp.Vector3(-0.5 * sxy), size=mp.Vector3(0, sxy), weight=-1.0) )
def setUp(self): resolution = 20 cell = mp.Vector3(10, 10, 0) pml_layers = mp.PML(1.0) self.fcen = 1.0 df = 1.0 sources = mp.Source(src=mp.GaussianSource(self.fcen, fwidth=df), center=mp.Vector3(), component=mp.Ez) symmetries = [mp.Mirror(mp.X), mp.Mirror(mp.Y)] self.sim = mp.Simulation(resolution=resolution, cell_size=cell, boundary_layers=[pml_layers], sources=[sources], symmetries=symmetries)
def main(args): sx = 4.0 #spatial extent along x including pmls (μm) sy = 4.0 sz = 4.0 dpml = 1.0 cell = mp.Vector3(sx, sy, sz) pml_layers = [mp.PML(dpml)] geometry = [ mp.Sphere(radius=0.7, center=mp.Vector3(0, 0, 0), material=Graph), mp.Sphere(radius=0.5, center=mp.Vector3(0, 0, 0), material=mp.Medium(epsilon=12.25)) ] resolution = args.res wvl = args.wvl / 1000 #convert args.wvl from nm to μm fcen = 1 / wvl df = 0.1 * fcen nfreq = 1 print("wavelength =", wvl, "μm") print("center frequency =", fcen, "1/μm") source = [ mp.Source(mp.GaussianSource(fcen, df, nfreq), component=mp.Ex, center=mp.Vector3(0, 0.705, 0)) ] symmetries = [mp.Mirror(mp.X), mp.Mirror(mp.Z)] sim = mp.Simulation(cell_size=cell, geometry=geometry, sources=source, resolution=resolution, boundary_layers=pml_layers) pt = mp.Vector3(0, 0.75, 0) sim.run(mp.dft_ldos(fcen, df, nfreq), until_after_sources=mp.stop_when_fields_decayed( 20, mp.Ex, pt, 1e-9))
def main(): n = 3.4 # index of waveguide w = 1 # width of waveguide r = 1 # inner radius of ring pad = 4 # padding between waveguide and edge of PML dpml = 2 # thickness of PML sxy = 2 * (r + w + pad + dpml) # cell size # Create a ring waveguide by two overlapping cylinders - later objects # take precedence over earlier objects, so we put the outer cylinder first. # and the inner (air) cylinder second. c1 = mp.Cylinder(radius=r + w, material=mp.Medium(index=n)) c2 = mp.Cylinder(radius=r) # If we don't want to excite a specific mode symmetry, we can just # put a single point source at some arbitrary place, pointing in some # arbitrary direction. We will only look for Ez-polarized modes. fcen = 0.15 # pulse center frequency df = 0.1 # pulse width (in frequency) src = mp.Source(mp.GaussianSource(fcen, fwidth=df), mp.Ez, mp.Vector3(r + 0.1)) sim = mp.Simulation(cell_size=mp.Vector3(sxy, sxy), geometry=[c1, c2], sources=[src], resolution=10, symmetries=[mp.Mirror(mp.Y)], boundary_layers=[mp.PML(dpml)]) sim.run( mp.at_beginning(mp.output_epsilon), mp.after_sources(mp.Harminv(mp.Ez, mp.Vector3(r + 0.1), fcen, df)), until_after_sources=300 ) # Output fields for one period at the end. (If we output # at a single time, we might accidentally catch the Ez field when it is # almost zero and get a distorted view.) sim.run(mp.at_every((1 / fcen / 20), mp.output_efield_z), until=(1 / fcen))
def setUp(self): n = 3.4 w = 1 self.r = 1 pad = 4 dpml = 2 sr = self.r + w + pad + dpml dimensions = mp.CYLINDRICAL cell = mp.Vector3(sr, 0, 0) m = 3 geometry = [ mp.Block( center=mp.Vector3(self.r + (w / 2)), size=mp.Vector3(w, mp.inf, mp.inf), material=mp.Medium(index=n) ) ] pml_layers = [mp.PML(dpml)] resolution = 10 self.fcen = 0.15 self.df = 0.1 sources = [ mp.Source( src=mp.GaussianSource(self.fcen, fwidth=self.df), component=mp.Ez, center=mp.Vector3(self.r + 0.1) ) ] self.sim = mp.Simulation( cell_size=cell, geometry=geometry, boundary_layers=pml_layers, resolution=resolution, sources=sources, dimensions=dimensions, m=m )
def test_eigfreq(self): w = 1.2 # width of waveguide r = 0.36 # radius of holes d = 1.4 # defect spacing (ordinary spacing = 1) N = 3 # number of holes on either side of defect sy = 6 # size of cell in y direction (perpendicular to wvg.) pad = 2 # padding between last hole and PML edge dpml = 1 # PML thickness sx = 2 * (pad + dpml + N) + d - 1 # size of cell in x direction geometry = [ mp.Block(size=mp.Vector3(mp.inf, w, mp.inf), material=mp.Medium(epsilon=13)) ] for i in range(N): geometry.append(mp.Cylinder(r, center=mp.Vector3(d / 2 + i))) geometry.append(mp.Cylinder(r, center=mp.Vector3(-(d / 2 + i)))) fcen = 0.25 df = 0.2 src = [ mp.Source(mp.GaussianSource(fcen, fwidth=df), component=mp.Hz, center=mp.Vector3(0), size=mp.Vector3(0, 0)) ] sim = mp.Simulation( cell_size=mp.Vector3(sx, sy), force_complex_fields=True, geometry=geometry, boundary_layers=[mp.PML(1.0)], sources=src, symmetries=[mp.Mirror(mp.X, phase=-1), mp.Mirror(mp.Y, phase=-1)], resolution=20) sim.init_sim() eigfreq = sim.solve_eigfreq(tol=1e-6) self.assertAlmostEqual(eigfreq.real, 0.23445413142440263, places=5) self.assertAlmostEqual(eigfreq.imag, -0.0003147775697388, places=5)
def init_simple_simulation(self, **kwargs): resolution = 20 cell = mp.Vector3(10, 10) pml_layers = mp.PML(1.0) fcen = 1.0 df = 1.0 sources = mp.Source(src=mp.GaussianSource(fcen, fwidth=df), center=mp.Vector3(), component=mp.Ez) symmetries = [mp.Mirror(mp.X), mp.Mirror(mp.Y)] return mp.Simulation(resolution=resolution, cell_size=cell, boundary_layers=[pml_layers], sources=[sources], symmetries=symmetries, **kwargs)
def setUp(self): cell = mp.Vector3(1, 12) b = mp.Block(size=mp.Vector3(1e20, 1.2, 1e20), material=mp.Medium(epsilon=13)) c = mp.Cylinder(0.36) self.fcen = 0.25 self.df = 1.5 s = mp.Source(src=mp.GaussianSource(self.fcen, fwidth=self.df), component=mp.Hz, center=mp.Vector3(0.1234)) sym = mp.Mirror(direction=mp.Y, phase=-1) self.sim = mp.Simulation(cell_size=cell, geometry=[b, c], sources=[s], symmetries=[sym], boundary_layers=[mp.PML(1, direction=mp.Y)], resolution=20)
def createDiffractionSlit(): cell = mp.Vector3(globalVariables.waveguideXSize, globalVariables.waveguideYSize, 0) geometry = [mp.Block(mp.Vector3(1e20, 1, 1e20), center=mp.Vector3(0, 0), material=mp.Medium(epsilon=globalVariables.epsilonOfMaterial)), mp.Block(mp.Vector3(globalVariables.blockXSize,globalVariables.blockYSize,0), center=mp.Vector3(0,(globalVariables.blockYSize-globalVariables.waveguideYSize)/2.0,0), material=mp.Medium(epsilon=globalVariables.epsilonOfBoundary)), mp.Block(mp.Vector3(globalVariables.blockXSize,globalVariables.blockYSize,0), center=mp.Vector3(0,(globalVariables.waveguideYSize-globalVariables.blockYSize)/2.0,0), material=mp.Medium(epsilon=globalVariables.epsilonOfBoundary))] sources = [mp.Source(mp.ContinuousSource(frequency=0.15), component=mp.Ez, center=mp.Vector3(-globalVariables.waveguideXSize/2+1,0,0))] pml_layers = [mp.PML(1.0)] return cell,geometry,sources,pml_layers
def test_physical(self): a = 10.0 ymax = 3.0 xmax = 8.0 dx = 2.0 w = 0.30 cell_size = mp.Vector3(xmax, ymax) pml_layers = [mp.PML(ymax / 3.0)] sources = [ mp.Source(src=mp.ContinuousSource(w), component=mp.Ez, center=mp.Vector3(-dx), size=mp.Vector3()) ] sim = mp.Simulation(cell_size=cell_size, resolution=a, boundary_layers=pml_layers, sources=sources, force_complex_fields=True) sim.init_sim() sim.solve_cw(tol=1e-5 if mp.is_single_precision() else 1e-6) p1 = mp.Vector3() p2 = mp.Vector3(dx) amp1 = sim.get_field_point(mp.Ez, p1) amp2 = sim.get_field_point(mp.Ez, p2) ratio = abs(amp1) / abs(amp2) ratio = ratio**2 # in 2d, decay is ~1/sqrt(r), so square to get 1/r fail_fmt = "Failed: amp1 = ({}, {}), amp2 = ({}, {})\nabs(amp1/amp2){} = {}, too far from 2.0" fail_msg = fail_fmt.format(amp1.real, amp1, amp2.real, amp2, "^2", ratio) self.assertTrue(ratio <= 2.12 and ratio >= 1.88, fail_msg)
def main(args): resolution = 20 # 20 pixels per unit 1 um eps = 80 # epsilon of cylinder medium Mat1 = mp.Medium(epsilon=eps) # definition of the material dimensions = mp.CYLINDRICAL r = args.r # the value of cylinder radius rl = args.rl # the r/l ration is given, to obtain the l value l=r/rl x = args.x dpml = args.dpml dair = args.dair m=args.m w=1*x h = r/rl # the height of the cylinder sr = r + dair + dpml sz = h + 2*dair + 2*dpml w_max=1.8 w_min=0.2 dw=w_max-w_min boundary_layers = [mp.PML(dpml)] Cyl = mp.Cylinder(material=Mat1, radius=r, height=h, center=mp.Vector3(0,0,0)) # make a cylinder with given parameters geometry = [Cyl] cell_size = mp.Vector3(sr,0,sz) #sources = [ mp.Source(mp.ContinuousSource(frequency = w), component=mp.Hz, center=mp.Vector3(r+dair,0,0)) ] sources = [mp.Source(mp.GaussianSource(w, fwidth=dw), component=mp.Hz, center=mp.Vector3(r+dair,0,0))] sim = mp.Simulation(resolution=resolution, cell_size=cell_size, boundary_layers=boundary_layers, geometry=geometry, sources=sources, dimensions=dimensions, m=m) sim.run(mp.in_volume(mp.Volume(center=mp.Vector3(), size=mp.Vector3(sr,0,sz)), mp.at_end(mp.output_epsilon, mp.output_efield_z)), mp.after_sources(mp.Harminv(mp.Hz, mp.Vector3(), w, dw)), until_after_sources=100)
def test_integrated_source(self): sources = [ mp.Source(mp.ContinuousSource(1, is_integrated=True), center=mp.Vector3(-2), size=mp.Vector3(y=6), component=mp.Ez) ] sim = mp.Simulation(resolution=20, cell_size=(6, 6), boundary_layers=[mp.PML(thickness=1)], sources=sources, k_point=mp.Vector3()) sim.run(until=30) # field in mid-plane should be nearly constant, # so compute its normalized std. dev. and check << 1 ez = sim.get_array(mp.Ez, center=mp.Vector3(2), size=mp.Vector3(y=6)) std = np.std(ez) / np.sqrt(np.mean(ez**2)) print("std = ", std) self.assertAlmostEqual(std, 0.0, places=4 if mp.is_single_precision() else 8)
def test_geometry_center(self): resolution = 20 cell_size = mp.Vector3(10, 10) pml = [mp.PML(1)] center = mp.Vector3(2, -1) result = [] fcen = 0.15 df = 0.1 sources = [mp.Source(src=mp.GaussianSource(fcen, fwidth=df), component=mp.Ez, center=mp.Vector3())] geometry = [mp.Block(center=mp.Vector3(), size=mp.Vector3(mp.inf, 3, mp.inf), material=mp.Medium(epsilon=12))] def print_field(sim): result.append(sim.get_field_point(mp.Ez, mp.Vector3(2, -1))) sim = mp.Simulation(resolution=resolution, cell_size=cell_size, boundary_layers=pml, sources=sources, geometry=geometry, geometry_center=center) sim.run(mp.at_end(print_field), until=50) self.assertAlmostEqual(result[0], -0.0599602798684155)
def test_load_dump_structure(self): resolution = 10 cell = mp.Vector3(10, 10) pml_layers = mp.PML(1.0) fcen = 1.0 df = 1.0 sources = mp.Source(src=mp.GaussianSource(fcen, fwidth=df), center=mp.Vector3(), component=mp.Hz) geometry = mp.Cylinder(0.2, material=mp.Medium(index=3)) sim = mp.Simulation(resolution=resolution, cell_size=cell, default_material=mp.Medium(index=1), geometry=[geometry], boundary_layers=[pml_layers], sources=[sources]) sim.run(until=200) ref_field = sim.get_field_point(mp.Hz, mp.Vector3(z=2)) dump_fn = 'test_load_dump_structure.h5' sim.dump_structure(dump_fn) sim = mp.Simulation(resolution=resolution, cell_size=cell, default_material=mp.Medium(index=1), geometry=[], boundary_layers=[pml_layers], sources=[sources], load_structure=dump_fn) sim.run(until=200) field = sim.get_field_point(mp.Hz, mp.Vector3(z=2)) self.assertAlmostEqual(ref_field, field) mp.all_wait() if mp.am_master(): os.remove(dump_fn)