def sim(): """perform scattering simulation""" sim = meep.Simulation(cell_size=cell, boundary_layers=[pml], geometry=geometry, default_material=medium, resolution=resolution) sim.init_fields() sim.init_sim() source(sim) freq = 1 / (600 * nm) # dft = sim.add_dft_fields([meep.Ex, meep.Ey, meep.Ez], freq, freq, 1, center=meep.Vector3(0,0,0), # size=meep.Vector3(L,L,0)) vol = meep.volume(meep.vec(-L / 2, -L / 2, 0), meep.vec(L / 2, L / 2, 0)) dft = sim.fields.add_dft_fields([meep.Ex, meep.Ey, meep.Ez], vol, freq, freq, 1) sim.run(until_after_sources=meep.stop_when_fields_decayed( .5 * um, decay, pt=meep.Vector3(0, 0, box[2] / 2), decay_by=1e-5)) # for i in range(100): # sim.fields.step() Ex = sim.get_dft_array(dft, meep.Ex, 0) Ey = sim.get_dft_array(dft, meep.Ey, 0) Ez = sim.get_dft_array(dft, meep.Ez, 0) E = np.array([Ex, Ey, Ez]) return dict(E=E)
def main(args): n = 3.4 # index of waveguide w = 1.0 # width of waveguide r = 1.0 # inner radius of ring pad = 4 # padding between waveguide and edge of PML dpml = 2 # thickness of PML sxy = 2.0 * (r + w + pad + dpml) # cell size resolution = 10.0 gv = mp.voltwo(sxy, sxy, resolution) gv.center_origin() sym = mp.mirror(mp.Y, gv) # exploit the mirror symmetry in structure+source: the_structure = mp.structure(gv, dummy_eps, mp.pml(dpml), sym) # 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. objects = [] n2 = n * n dielectric = gm.Medium(epsilon_diag=gm.Vector3(n2, n2, n2)) objects.append(gm.Cylinder(r + w, material=dielectric)) objects.append(gm.Cylinder(r)) mp.set_materials_from_geometry(the_structure, objects) f = mp.fields(the_structure) # 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 TM modes (E out of the plane). fcen = 0.15 # pulse center frequency df = 0.1 src = GaussianSource(fcen, df) v = mp.volume(mp.vec(r + 0.1, 0.0), mp.vec(0.0, 0.0)) f.add_volume_source(mp.Ez, src, v) T = 300.0 stop_time = f.last_source_time() + T while f.round_time() < stop_time: f.step() # TODO: translate call to harminv # int bands = do_harminv (... Ez, vec3(r+0.1), fcen, df) # 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.) DeltaT = 1.0 / (20 * fcen) NextOutputTime = f.round_time() + DeltaT while f.round_time() < 1.0 / fcen: f.step() if f.round_time() >= NextOutputTime: f.output_hdf5(mp.Ez, f.total_volume()) NextOutputTime += DeltaT
def check_iterable(one, two, three, four): v1 = mp.vec(one) assert_one(v1) v2 = mp.vec(two) assert_two(v2) v3 = mp.vec(three) assert_three(v3) assert_raises(four, NotImplementedError)
def test_get_dft_array(self): sim = self.init() sim.init_sim() dft_fields = sim.add_dft_fields([mp.Ez], self.fcen, self.fcen, 1) fr = mp.FluxRegion(mp.Vector3(), size=mp.Vector3(self.sxy, self.sxy), direction=mp.X) dft_flux = sim.add_flux(self.fcen, 0, 1, fr) # volumes with zero thickness in x and y directions to test collapsing # of empty dimensions in DFT array and HDF5 output routines thin_x_volume = mp.volume(mp.vec(0.35 * self.sxy, -0.4 * self.sxy), mp.vec(0.35 * self.sxy, +0.4 * self.sxy)) thin_x_flux = sim.fields.add_dft_fields([mp.Ez], thin_x_volume, self.fcen, self.fcen, 1) thin_y_volume = mp.volume(mp.vec(-0.5 * self.sxy, 0.25 * self.sxy), mp.vec(+0.5 * self.sxy, 0.25 * self.sxy)) thin_y_flux = sim.fields.add_dft_flux(mp.Y, thin_y_volume, self.fcen, self.fcen, 1) sim.run(until_after_sources=100) # test proper collapsing of degenerate dimensions in HDF5 files and arrays thin_x_array = sim.get_dft_array(thin_x_flux, mp.Ez, 0) thin_y_array = sim.get_dft_array(thin_y_flux, mp.Ez, 0) np.testing.assert_equal(thin_x_array.ndim, 1) np.testing.assert_equal(thin_y_array.ndim, 1) sim.output_dft(thin_x_flux, 'thin-x-flux') sim.output_dft(thin_y_flux, 'thin-y-flux') with h5py.File('thin-x-flux.h5', 'r') as thin_x: thin_x_h5 = mp.complexarray(thin_x['ez_0.r'].value, thin_x['ez_0.i'].value) with h5py.File('thin-y-flux.h5', 'r') as thin_y: thin_y_h5 = mp.complexarray(thin_y['ez_0.r'].value, thin_y['ez_0.i'].value) np.testing.assert_allclose(thin_x_array, thin_x_h5) np.testing.assert_allclose(thin_y_array, thin_y_h5) # compare array data to HDF5 file content for fields and flux fields_arr = sim.get_dft_array(dft_fields, mp.Ez, 0) flux_arr = sim.get_dft_array(dft_flux, mp.Ez, 0) sim.output_dft(dft_fields, 'dft-fields') sim.output_dft(dft_flux, 'dft-flux') with h5py.File('dft-fields.h5', 'r') as fields, h5py.File('dft-flux.h5', 'r') as flux: exp_fields = mp.complexarray(fields['ez_0.r'].value, fields['ez_0.i'].value) exp_flux = mp.complexarray(flux['ez_0.r'].value, flux['ez_0.i'].value) np.testing.assert_allclose(exp_fields, fields_arr) np.testing.assert_allclose(exp_flux, flux_arr)
def py_v3_to_vec(dims, v3, is_cylindrical=False): if dims == 1: return mp.vec(v3.z) elif dims == 2: if is_cylindrical: return mp.veccyl(v3.x, v3.z) else: return mp.vec(v3.x, v3.y) elif dims == 3: return mp.vec(v3.x, v3.y, v3.z) else: raise ValueError("Invalid dimensions in Volume: {}".format(dims))
def test_radiating_2d(self): xmax = 8.0 # grid_volume self.gv = mp.voltwo(xmax, self.ymax, self.a) self.pnt_src_vec = mp.vec(xmax / 2 - self.dx, self.ymax / 2) self.p1 = mp.vec(xmax / 2 + 0 * self.dx, self.ymax / 2) self.p2 = mp.vec(xmax / 2 + 1 * self.dx, self.ymax / 2) self.radiating_base(True)
def setUp(self): def dummy_eps(v): return 1.0 gv = mp.voltwo(16, 16, 10) gv.center_origin() sym = mp.mirror(mp.Y, gv) the_structure = mp.structure(gv, dummy_eps, mp.pml(2), sym) objects = [] objects.append(Cylinder(1)) mp.set_materials_from_geometry(the_structure, objects) self.f = mp.fields(the_structure) self.v = mp.volume(mp.vec(1.1, 0.0), mp.vec(0.0, 0.0))
def __make_meep_vec__(self, coord): '''Convert a Coord3 object into a Meep vec object''' if (self.dim == 3): (x,y,z) = self.__calc_meep_coord__(coord) return Meep.vec(x, y, z) elif (self.dim == 2): (x,y) = self.__calc_meep_coord__(coord) return Meep.vec(x, y) elif (self.dim == 1): (x) = self.__calc_meep_coord__(coord) return Meep.vec(x) else: raise PythonSimulateException("Invalid value for self.dim : expected 1, 2 or 3. The value is : %s" %str(self.dim))
def test_get_dft_array(self): sim = self.init() sim.init_sim() dft_fields = sim.add_dft_fields([mp.Ez], self.fcen, self.fcen, 1) fr = mp.FluxRegion(mp.Vector3(), size=mp.Vector3(self.sxy, self.sxy), direction=mp.X) dft_flux = sim.add_flux(self.fcen, 0, 1, fr) # volumes with zero thickness in x and y directions to test collapsing # of empty dimensions in DFT array and HDF5 output routines thin_x_volume = mp.volume( mp.vec(0.35*self.sxy, -0.4*self.sxy), mp.vec(0.35*self.sxy, +0.4*self.sxy)); thin_x_flux = sim.fields.add_dft_fields([mp.Ez], thin_x_volume, self.fcen, self.fcen, 1) thin_y_volume = mp.volume( mp.vec(-0.5*self.sxy, 0.25*self.sxy), mp.vec(+0.5*self.sxy, 0.25*self.sxy)); thin_y_flux = sim.fields.add_dft_flux(mp.Y, thin_y_volume, self.fcen, self.fcen, 1) sim.run(until_after_sources=100) # test proper collapsing of degenerate dimensions in HDF5 files and arrays thin_x_array = sim.get_dft_array(thin_x_flux, mp.Ez, 0) thin_y_array = sim.get_dft_array(thin_y_flux, mp.Ez, 0) np.testing.assert_equal(thin_x_array.ndim, 1) np.testing.assert_equal(thin_y_array.ndim, 1) sim.output_dft(thin_x_flux, 'thin-x-flux') sim.output_dft(thin_y_flux, 'thin-y-flux') with h5py.File('thin-x-flux.h5', 'r') as thin_x: thin_x_h5 = mp.complexarray(thin_x['ez_0.r'].value, thin_x['ez_0.i'].value) with h5py.File('thin-y-flux.h5', 'r') as thin_y: thin_y_h5 = mp.complexarray(thin_y['ez_0.r'].value, thin_y['ez_0.i'].value) np.testing.assert_allclose(thin_x_array, thin_x_h5) np.testing.assert_allclose(thin_y_array, thin_y_h5) # compare array data to HDF5 file content for fields and flux fields_arr = sim.get_dft_array(dft_fields, mp.Ez, 0) flux_arr = sim.get_dft_array(dft_flux, mp.Ez, 0) sim.output_dft(dft_fields, 'dft-fields') sim.output_dft(dft_flux, 'dft-flux') with h5py.File('dft-fields.h5', 'r') as fields, h5py.File('dft-flux.h5', 'r') as flux: exp_fields = mp.complexarray(fields['ez_0.r'].value, fields['ez_0.i'].value) exp_flux = mp.complexarray(flux['ez_0.r'].value, flux['ez_0.i'].value) np.testing.assert_allclose(exp_fields, fields_arr) np.testing.assert_allclose(exp_flux, flux_arr)
def test_radiating_3d(self): xmax = 7.0 # grid_volume self.gv = mp.vol3d(xmax, self.ymax, self.ymax, self.a) self.pnt_src_vec = mp.vec(xmax / 2.0 - self.dx, self.ymax / 2.0, self.ymax / 2.0) self.p1 = mp.vec(xmax / 2.0 + 0 * self.dx, self.ymax / 2.0, self.ymax / 2.0) self.p2 = mp.vec(xmax / 2.0 + 1 * self.dx, self.ymax / 2.0, self.ymax / 2.0) self.radiating_base(False)
def test_typemap_swig_raises(self): src = mp.gaussian_src_time(0.15, 0.1) self.assertTrue(src.is_equal(src)) with self.assertRaises(TypeError) as error: src.is_equal(mp.vec()) self.assertEqual(error.exception.message, self.expected_msg)
def place_adjoint_source(self, dJ): dt = self.sim.fields.dt # the timestep size from sim.fields.dt of the forward sim self.sources = [] dJ = dJ.flatten() #TODO far_pts in 3d or cylindrical, perhaps py_v3_to_vec from simulation.py self.all_nearsrcdata = self.monitor.swigobj.near_sourcedata( mp.vec(self.far_pt.x, self.far_pt.y), dJ) for near_data in self.all_nearsrcdata: cur_comp = near_data.near_fd_comp amp_arr = np.array(near_data.amp_arr).reshape(-1, self.num_freq) scale = amp_arr * adj_src_scale(self, dt, include_resolution=False) if self.num_freq == 1: self.sources += [ mp.IndexedSource(self.time_src, near_data, scale[:, 0]) ] else: src = FilteredSource(self.time_src.frequency, self.frequencies, scale, dt) (num_basis, num_pts) = src.nodes.shape for basis_i in range(num_basis): self.sources += [ mp.IndexedSource(src.time_src_bf[basis_i], near_data, src.nodes[basis_i]) ] return self.sources
def run_simulation(self): self.sim.run(mp.at_beginning(mp.output_epsilon), mp.at_every(0.25, self.print_stuff), mp.at_end(self.print_stuff), mp.at_end(mp.output_efield_z), until=23) ref_out_field = self.ref_Ez if self.src_cmpt == mp.Ez else self.ref_Hz out_field = self.sim.fields.get_field(self.src_cmpt, mp.vec(4.13, 3.75)).real diff = abs(out_field - ref_out_field) self.assertTrue(abs(diff) <= 0.05 * abs(ref_out_field), "Field output differs")
def run_simulation(self): self.sim.run(mp.at_beginning(mp.output_epsilon), mp.at_every(0.25, self.print_stuff), mp.at_end(self.print_stuff), mp.at_end(mp.output_efield_z), until=23) ref_out_field = self.ref_Ez if self.src_cmpt == mp.Ez else self.ref_Hz out_field = self.sim.fields.get_field(self.src_cmpt, mp.vec(4.13, 3.75)).real diff = abs(out_field - ref_out_field) self.assertTrue( abs(diff) <= 0.05 * abs(ref_out_field), "Field output differs")
def k_guess(freq, band_num, w): # hard-coded dispersion relations for waveguides of given size if (equal_float(w, 1.0) and equal_float(freq, 0.15)): if (band_num >= 1): return mp.vec(0.419984, 0) if (equal_float(w, 3.0) and equal_float(freq, 0.15)): if (band_num == 1): return mp.vec(0.494499, 0) if (band_num == 2): return mp.vec(0.486657, 0) if (band_num == 3): return mp.vec(0.434539, 0) if (band_num == 4): return mp.vec(0.397068, 0) if (band_num == 5): return mp.vec(0.322812, 0) if (band_num >= 6): return mp.vec(0.211186, 0) return mp.vec(0.0, 0.0)
def test_get_center_and_size(self): v1d = mp.volume(mp.vec(-2), mp.vec(2)) center, size = mp.get_center_and_size(v1d) self.assertTrue(center.close(mp.Vector3())) self.assertTrue(size.close(mp.Vector3(z=4))) v2d = mp.volume(mp.vec(-1, -1), mp.vec(1, 1)) center, size = mp.get_center_and_size(v2d) self.assertTrue(center.close(mp.Vector3())) self.assertTrue(size.close(mp.Vector3(2, 2))) v3d = mp.volume(mp.vec(-1, -1, -1), mp.vec(1, 1, 1)) center, size = mp.get_center_and_size(v3d) self.assertTrue(center.close(mp.Vector3())) self.assertTrue(size.close(mp.Vector3(2, 2, 2)))
def assert_raises(it, err): with self.assertRaises(err): mp.vec(it)
def test_vec_constructor(self): def assert_one(v): self.assertEqual(v.z(), 1) def assert_two(v): self.assertEqual(v.x(), 1) self.assertEqual(v.y(), 2) def assert_three(v): assert_two(v) self.assertEqual(v.z(), 3) def assert_raises(it, err): with self.assertRaises(err): mp.vec(it) v1 = mp.vec(1) assert_one(v1) v2 = mp.vec(1, 2) assert_two(v2) v3 = mp.vec(1, 2, 3) assert_three(v3) mp.vec() with self.assertRaises(TypeError): mp.vec(1, 2, 3, 4) def check_iterable(one, two, three, four): v1 = mp.vec(one) assert_one(v1) v2 = mp.vec(two) assert_two(v2) v3 = mp.vec(three) assert_three(v3) assert_raises(four, NotImplementedError) check_iterable([1], [1, 2], [1, 2, 3], [1, 2, 3, 4]) check_iterable((1, ), (1, 2), (1, 2, 3), (1, 2, 3, 4)) check_iterable(np.array([1.]), np.array([1., 2.]), np.array([1., 2., 3.]), np.array([1., 2., 3., 4.])) with self.assertRaises(TypeError): mp.vec([1, 2], 3) with self.assertRaises(TypeError): mp.vec(1, [2, 3])
def __init__( self, wA=1.0, wB=3.0, # smaller, larger waveguide thickness LWaveguide=3.0, # length of each waveguide section LTaper=3.0, pTaper=0, # taper length and smoothness index eps_waveguide=11.7, # permittivity inside waveguide eps_ambient=1.0, # permittivity of medium LY=6.0, # width of computational cell DPML=0.5, # PML thickness fcen=0.15, df=0.075, # center frequency / width band_num=1, # index of eigenmode source resolution=25.0, # grid points per unit length ): #-------------------------------------------------------------------- #- user-defined epsilon function #-------------------------------------------------------------------- eps_func = lambda loc: my_eps_func(loc, LTaper, pTaper, wA, wB, eps_ambient, eps_waveguide) #-------------------------------------------------------------------- #- eigenmode source at midpoint of smaller waveguide #-------------------------------------------------------------------- LX = 2.0 * (DPML + LWaveguide) + LTaper xA = -0.5 * LX + DPML + 0.5 * LWaveguide xB = +0.5 * LX - DPML - 0.5 * LWaveguide sources = [ mp.EigenModeSource(src=mp.GaussianSource(fcen, fwidth=df), center=mp.Vector3(xA, 0.0), size=mp.Vector3(0.0, LY), eig_band=band_num) ] self.sim = mp.Simulation(cell_size=mp.Vector3(LX, LY), resolution=resolution, boundary_layers=[mp.PML(DPML)], force_complex_fields=True, epsilon_func=eps_func, sources=sources) self.sim.run(mp.at_beginning(mp.output_epsilon), until=1.0) f = self.sim.fields #-------------------------------------------------- # add DFT flux regions at midpoints of smaller and larger waveguides #-------------------------------------------------- YP = 0.5 * LY - DPML self.vA = mp.volume(mp.vec(xA, -YP), mp.vec(xA, +YP)) self.vB = mp.volume(mp.vec(xB, -YP), mp.vec(xB, +YP)) nf = 1 self.fluxA = f.add_dft_flux_plane(self.vA, fcen - 0.5 * df, fcen + 0.5 * df, nf) self.fluxB = f.add_dft_flux_plane(self.vB, fcen - 0.5 * df, fcen + 0.5 * df, nf) #-------------------------------------------------- # save some other fields in the wvg_taper class for later use #-------------------------------------------------- self.xA = xA self.xB = xB self.wA = wA self.wB = wB self.LTaper = LTaper self.pTaper = pTaper self.fcen = fcen self.df = df self.band_num = band_num
def __init__(self,gridSizeX,gridSizeY,res,f,df,n_freqs = 1000,boundary_conditions = pml(1.0,Y),periodic_directions = [],bloch = vec(0.0,0.0),symmetry_direction = None,symmetry_val = complex(1.0),gridSizeZ = None): self.gridSizeX = gridSizeX self.gridSizeY = gridSizeY self.gridSizeZ = gridSizeZ self.res = res self.boundary_conditions = boundary_conditions self.fluxes = [] self.f = f self.df = df self.n_freqs = n_freqs self.symmetry_direction = symmetry_direction self.symmetry_val = symmetry_val self.periodic_directions = periodic_directions self.bloch = bloch self.my_source = None
def make_harminv(self,probing = vec(1.0,1.0),n_points = 15): the_field = self.make_fields() return runWithHarminv(the_field,self.meep_space,self.my_source.comp,probing,self.f,self.df,15)#pHDF5OutputFile = my_file,pHDF5OutputFilePhase3 = other_file))
def add_flat_source(self,vol = volume(vec(0,1.0),vec(1.0,1.0)),comp = Ez): self.my_source = My_flat_source(vol,self.f,self.df,comp)
def bend_flux(no_bend): sx = 16.0 # size of cell in X direction sy = 32.0 # size of cell in Y direction pad = 4.0 # padding distance between waveguide and cell edge w = 1.0 # width of waveguide resolution = 10 # (set-param! resolution 10) gv = mp.voltwo(sx, sy, resolution) gv.center_origin() the_structure = mp.structure(gv, dummy_eps, mp.pml(1.0)) wvg_ycen = -0.5 * (sy - w - 2.0 * pad) # y center of horiz. wvg wvg_xcen = 0.5 * (sx - w - 2.0 * pad) # x center of vert. wvg e1 = gm.Vector3(1.0, 0.0, 0.0) e2 = gm.Vector3(0.0, 1.0, 0.0) e3 = gm.Vector3(0.0, 0.0, 1.0) dielectric = gm.Medium(epsilon_diag=gm.Vector3(12, 12, 12)) if no_bend: center = gm.Vector3(y=wvg_ycen) size = gm.Vector3(float('inf'), w, float('inf')) objects = [ gm.Block(size, e1, e2, e3, material=dielectric, center=center) ] mp.set_materials_from_geometry(the_structure, objects) else: objects = [] center = gm.Vector3(-0.5 * pad, wvg_ycen) size = gm.Vector3(sx - pad, w, float('inf')) objects.append( gm.Block(size, e1, e2, e3, material=dielectric, center=center)) center = gm.Vector3(wvg_xcen, 0.5 * pad) size = gm.Vector3(w, sy - pad, float('inf')) objects.append( gm.Block(size, e1, e2, e3, material=dielectric, center=center)) mp.set_materials_from_geometry(the_structure, objects) f = mp.fields(the_structure) fcen = 0.15 # pulse center frequency df = 0.1 src = GaussianSource(fcen, df) v = mp.volume(mp.vec(1.0 - 0.5 * sx, wvg_ycen), mp.vec(0.0, w)) f.add_volume_source(mp.Ez, src, v) f_start = fcen - 0.5 * df f_end = fcen + 0.5 * df nfreq = 100 # number of frequencies at which to compute flux if no_bend: trans_volume = mp.volume(mp.vec(0.5 * sx - 1.5, wvg_ycen), mp.vec(0.0, 2.0 * w)) else: trans_volume = mp.volume(mp.vec(wvg_xcen, 0.5 * sy - 1.5), mp.vec(2.0 * w, 0.0)) trans_vl = mp.volume_list(trans_volume, mp.Sz) trans = f.add_dft_flux(trans_vl, f_start, f_end, nfreq) refl_volume = mp.volume(mp.vec(-0.5 * sx + 1.5, wvg_ycen), mp.vec(0.0, 2.0 * w)) refl_vl = mp.volume_list(refl_volume, mp.Sz) refl = f.add_dft_flux(refl_vl, f_start, f_end, nfreq) dataname = "refl-flux" if not no_bend: refl.load_hdf5(f, dataname) refl.scale_dfts(-1.0) eval_point = mp.vec(0.5 * sx - 1.5, wvg_ycen) if no_bend else mp.vec( wvg_xcen, 0.5 * sy - 1.5) deltaT = 50.0 next_check_time = f.round_time() + deltaT tol = 1.0e-3 max_abs = 0.0 cur_max = 0.0 done = False while not done: f.step() # manually check fields-decayed condition absEz = abs(f.get_field(mp.Ez, eval_point)) cur_max = max(cur_max, absEz) if f.round_time() >= next_check_time: next_check_time += deltaT max_abs = max(max_abs, cur_max) if max_abs > 0.0 and cur_max < tol * max_abs: done = True cur_max = 0.0 # printf("%.2e %.2e %.2e %.2e\n",f.round_time(),absEz,max_abs,cur_max) if no_bend: refl.save_hdf5(f, dataname) print("{}\t\t | {}\t\t | {}".format("Time", "trans flux", "refl flux")) f0 = fcen - 0.5 * df fstep = df / (nfreq - 1) trans_flux = trans.flux() refl_flux = refl.flux() for nf in range(nfreq): print("{}\t\t | {}\t\t | {}".format(f0 + nf * fstep, trans_flux[nf], refl_flux[nf]))
def test_vec_constructor(self): def assert_one(v): self.assertEqual(v.z(), 1) def assert_two(v): self.assertEqual(v.x(), 1) self.assertEqual(v.y(), 2) def assert_three(v): assert_two(v) self.assertEqual(v.z(), 3) def assert_raises(it, err): with self.assertRaises(err): mp.vec(it) v1 = mp.vec(1) assert_one(v1) v2 = mp.vec(1, 2) assert_two(v2) v3 = mp.vec(1, 2, 3) assert_three(v3) mp.vec() with self.assertRaises(TypeError): mp.vec(1, 2, 3, 4) def check_iterable(one, two, three, four): v1 = mp.vec(one) assert_one(v1) v2 = mp.vec(two) assert_two(v2) v3 = mp.vec(three) assert_three(v3) assert_raises(four, NotImplementedError) check_iterable([1], [1, 2], [1, 2, 3], [1, 2, 3, 4]) check_iterable((1,), (1, 2), (1, 2, 3), (1, 2, 3, 4)) check_iterable(np.array([1.]), np.array([1., 2.]), np.array([1., 2., 3.]), np.array([1., 2., 3., 4.])) with self.assertRaises(TypeError): mp.vec([1, 2], 3) with self.assertRaises(TypeError): mp.vec(1, [2, 3])
def vol_cut(self): if self.dim() == 2: return self.meep_space.surroundings() if self.dim() == 3: return volume(vec(0,self.gridSizeY/2.,0),vec(self.gridSizeX,self.gridSizeY/2.,self.gridSizeZ))
def main(args): resolution = args.res w1 = 1 # width of waveguide 1 w2 = args.w2 # width of waveguide 2 Lw = 10 # length of waveguide 1 and 2 Lt = args.Lt # taper length Si = mp.Medium(epsilon=12.0) dair = 3.0 dpml = 3.0 sx = dpml + Lw + Lt + Lw + dpml sy = dpml + dair + w2 + dair + dpml cell_size = mp.Vector3(sx, sy, 0) geometry = [ mp.Block(material=Si, center=mp.Vector3(0, 0, 0), size=mp.Vector3(mp.inf, w1, mp.inf)), mp.Block(material=Si, center=mp.Vector3(0.5 * sx - 0.5 * (Lt + Lw + dpml), 0, 0), size=mp.Vector3(Lt + Lw + dpml, w2, mp.inf)) ] # form linear taper hh = w2 ww = 2 * Lt # taper angle (CCW, relative to +X axis) rot_theta = math.atan(0.5 * (w2 - w1) / Lt) pvec = mp.Vector3(-0.5 * sx + dpml + Lw, 0.5 * w1, 0) cvec = mp.Vector3(-0.5 * sx + dpml + Lw + 0.5 * ww, 0.5 * hh + 0.5 * w1, 0) rvec = cvec - pvec rrvec = rvec.rotate(mp.Vector3(0, 0, 1), rot_theta) geometry.append( mp.Block(material=mp.air, center=pvec + rrvec, size=mp.Vector3(ww, hh, mp.inf), e1=mp.Vector3(1, 0, 0).rotate(mp.Vector3(0, 0, 1), rot_theta), e2=mp.Vector3(0, 1, 0).rotate(mp.Vector3(0, 0, 1), rot_theta), e3=mp.Vector3(0, 0, 1))) pvec = mp.Vector3(-0.5 * sx + dpml + Lw, -0.5 * w1, 0) cvec = mp.Vector3(-0.5 * sx + dpml + Lw + 0.5 * ww, -(0.5 * hh + 0.5 * w1), 0) rvec = cvec - pvec rrvec = rvec.rotate(mp.Vector3(0, 0, 1), -rot_theta) geometry.append( mp.Block(material=mp.air, center=pvec + rrvec, size=mp.Vector3(ww, hh, mp.inf), e1=mp.Vector3(1, 0, 0).rotate(mp.Vector3(0, 0, 1), -rot_theta), e2=mp.Vector3(0, 1, 0).rotate(mp.Vector3(0, 0, 1), -rot_theta), e3=mp.Vector3(0, 0, 1))) boundary_layers = [mp.PML(dpml)] # mode frequency fcen = 0.15 sources = [ mp.EigenModeSource(src=mp.GaussianSource(fcen, fwidth=0.5 * fcen), component=mp.Ez, size=mp.Vector3(0, sy - 2 * dpml, 0), center=mp.Vector3(-0.5 * sx + dpml, 0, 0), eig_match_freq=True, eig_parity=mp.ODD_Z, eig_kpoint=mp.Vector3(0.4, 0, 0), eig_resolution=32) ] symmetries = [mp.Mirror(mp.Y, +1)] sim = mp.Simulation(resolution=resolution, cell_size=cell_size, boundary_layers=boundary_layers, geometry=geometry, sources=sources, symmetries=symmetries) xm = -0.5 * sx + dpml + 0.5 * Lw # x-coordinate of monitor mflux = sim.add_flux( fcen, 0, 1, mp.FluxRegion(center=mp.Vector3(xm, 0), size=mp.Vector3(0, sy - 2 * dpml))) sim.run(until_after_sources=mp.stop_when_fields_decayed( 50, mp.Ez, mp.Vector3(xm, 0), 1e-10)) bands = np.array( [1], dtype=np.int32 ) # indices of modes for which to compute expansion coefficients num_bands = 1 alpha = np.zeros( 2 * num_bands, dtype=np.complex128) # preallocate array to store coefficients vgrp = np.zeros(num_bands, dtype=np.float64) # also store mode group velocities mvol = mp.volume(mp.vec(xm, -0.5 * sy + dpml), mp.vec(xm, +0.5 * sy - dpml)) sim.get_eigenmode_coefficients(mflux, mp.X, mvol, bands, alpha, vgrp) alpha0Plus = alpha[2 * 0 + 0] # coefficient of forward-traveling fundamental mode alpha0Minus = alpha[2 * 0 + 1] # coefficient of backward-traveling fundamental mode print("refl:, {}, {:.8f}".format(Lt, abs(alpha0Minus)**2))