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(center=mp.Vector3(0.35 * self.sxy), size=mp.Vector3(y=0.8 * self.sxy)) thin_x_flux = sim.add_dft_fields([mp.Ez], self.fcen, self.fcen, 1, where=thin_x_volume) thin_y_volume = mp.Volume(center=mp.Vector3(y=0.25 * self.sxy), size=mp.Vector3(x=self.sxy)) thin_y_flux = sim.add_flux(self.fcen, self.fcen, 1, mp.FluxRegion(volume=thin_y_volume)) 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'][()], thin_x['ez_0.i'][()]) with h5py.File('thin-y-flux.h5', 'r') as thin_y: thin_y_h5 = mp.complexarray(thin_y['ez_0.r'][()], thin_y['ez_0.i'][()]) 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'][()], fields['ez_0.i'][()]) exp_flux = mp.complexarray(flux['ez_0.r'][()], flux['ez_0.i'][()]) np.testing.assert_allclose(exp_fields, fields_arr) np.testing.assert_allclose(exp_flux, flux_arr)
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 verify_output_and_slice(self, material, frequency): # Since the slice routines average the diagonals, we need to do that too: chi1 = material.epsilon(frequency).astype(np.complex128) chi1inv = np.linalg.inv(chi1) chi1inv = np.diag(chi1inv) N = chi1inv.size n = np.sqrt(N / np.sum(chi1inv)) sim = mp.Simulation(cell_size=mp.Vector3(2, 2, 2), default_material=material, resolution=20, eps_averaging=False) sim.use_output_directory(self.temp_dir) sim.init_sim() # Check to make sure the get_slice routine is working with frequency n_slice = np.sqrt(np.max(sim.get_epsilon(frequency))) self.assertAlmostEqual(n, n_slice, places=4) # Check to make sure h5 output is working with frequency filename = os.path.join(self.temp_dir, 'dispersive_eigenmode-eps-000000.00.h5') mp.output_epsilon(sim, frequency=frequency) n_h5 = 0 mp.all_wait() with h5py.File(filename, 'r') as f: n_h5 = np.sqrt( np.max(mp.complexarray(f['eps.r'][()], f['eps.i'][()]))) self.assertAlmostEqual(n, n_h5, places=4)
def run_test(self, nfreqs): eps = 13 w = 1.2 r = 0.36 d = 1.4 N = 3 sy = 6 pad = 2 dpml = 1 sx = 2 * (pad + dpml + N) + d - 1 cell = mp.Vector3(sx, sy, 0) geometry = [mp.Block(center=mp.Vector3(), size=mp.Vector3(mp.inf, w, mp.inf), material=mp.Medium(epsilon=eps))] 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))) pml_layers = mp.PML(dpml) resolution = 10 fcen = 0.25 df = 0.2 sources = mp.Source(src=mp.GaussianSource(fcen, fwidth=df), component=mp.Hz, center=mp.Vector3()) symmetries = [mp.Mirror(mp.Y, phase=-1), mp.Mirror(mp.X, phase=-1)] d1 = 0.2 sim = mp.Simulation(cell_size=cell, geometry=geometry, sources=[sources], symmetries=symmetries, boundary_layers=[pml_layers], resolution=resolution) nearfield = sim.add_near2far( fcen, 0.1, nfreqs, mp.Near2FarRegion(mp.Vector3(0, 0.5 * w + d1), size=mp.Vector3(2 * dpml - sx)), mp.Near2FarRegion(mp.Vector3(-0.5 * sx + dpml, 0.5 * w + 0.5 * d1), size=mp.Vector3(0, d1), weight=-1.0), mp.Near2FarRegion(mp.Vector3(0.5 * sx - dpml, 0.5 * w + 0.5 * d1), size=mp.Vector3(0, d1)) ) sim.run(until=200) d2 = 20 h = 4 vol = mp.Volume(mp.Vector3(0, (0.5 * w) + d2 + (0.5 * h)), size=mp.Vector3(sx - 2 * dpml, h)) result = sim.get_farfields(nearfield, resolution, where=vol) fname = 'cavity-farfield.h5' if nfreqs == 1 else 'cavity-farfield-4-freqs.h5' ref_file = os.path.join(self.data_dir, fname) with h5py.File(ref_file, 'r') as f: # Get reference data into memory ref_ex = mp.complexarray(f['ex.r'][()], f['ex.i'][()]) ref_ey = mp.complexarray(f['ey.r'][()], f['ey.i'][()]) ref_ez = mp.complexarray(f['ez.r'][()], f['ez.i'][()]) ref_hx = mp.complexarray(f['hx.r'][()], f['hx.i'][()]) ref_hy = mp.complexarray(f['hy.r'][()], f['hy.i'][()]) ref_hz = mp.complexarray(f['hz.r'][()], f['hz.i'][()]) np.testing.assert_allclose(ref_ex, result['Ex']) np.testing.assert_allclose(ref_ey, result['Ey']) np.testing.assert_allclose(ref_ez, result['Ez']) np.testing.assert_allclose(ref_hx, result['Hx']) np.testing.assert_allclose(ref_hy, result['Hy']) np.testing.assert_allclose(ref_hz, result['Hz'])