def test_get_array_output(self): sim = self.init_simple_simulation() sim.symmetries = [] sim.geometry = [mp.Cylinder(0.2, material=mp.Medium(index=3))] sim.filename_prefix = 'test_get_array_output' sim.run(until=20) mp.output_epsilon(sim) mp.output_efield_z(sim) mp.output_tot_pwr(sim) mp.output_efield(sim) eps_arr = sim.get_epsilon() efield_z_arr = sim.get_efield_z() energy_arr = sim.get_tot_pwr() efield_arr = sim.get_efield() fname_fmt = "test_get_array_output-{}-000020.00.h5" with h5py.File(fname_fmt.format('eps'), 'r') as f: eps = f['eps'][()] with h5py.File(fname_fmt.format('ez'), 'r') as f: efield_z = f['ez'][()] with h5py.File(fname_fmt.format('energy'), 'r') as f: energy = f['energy'][()] with h5py.File(fname_fmt.format('e'), 'r') as f: ex = f['ex'][()] ey = f['ey'][()] ez = f['ez'][()] efield = np.stack([ex, ey, ez], axis=-1) np.testing.assert_allclose(eps, eps_arr) np.testing.assert_allclose(efield_z, efield_z_arr) np.testing.assert_allclose(energy, energy_arr) np.testing.assert_allclose(efield, efield_arr)
def test_get_array_output(self): sim = self.init_simple_simulation() sim.symmetries = [] sim.geometry = [mp.Cylinder(0.2, material=mp.Medium(index=3))] sim.filename_prefix = 'test_get_array_output' sim.run(until=20) mp.output_epsilon(sim) mp.output_efield_z(sim) mp.output_tot_pwr(sim) mp.output_efield(sim) eps_arr = sim.get_epsilon() efield_z_arr = sim.get_efield_z() energy_arr = sim.get_tot_pwr() efield_arr = sim.get_efield() fname_fmt = "test_get_array_output-{}-000020.00.h5" with h5py.File(fname_fmt.format('eps'), 'r') as f: eps = f['eps'].value with h5py.File(fname_fmt.format('ez'), 'r') as f: efield_z = f['ez'].value with h5py.File(fname_fmt.format('energy'), 'r') as f: energy = f['energy'].value with h5py.File(fname_fmt.format('e'), 'r') as f: ex = f['ex'].value ey = f['ey'].value ez = f['ez'].value efield = np.stack([ex, ey, ez], axis=-1) np.testing.assert_allclose(eps, eps_arr) np.testing.assert_allclose(efield_z, efield_z_arr) np.testing.assert_allclose(energy, energy_arr) np.testing.assert_allclose(efield, efield_arr)
def standard(metalens): metalens['run_date'] = int(time()) matter = mp.Medium(epsilon=metalens['epsilon']) pillar_locs = list( product( [-metalens['pillar_separation'], metalens['pillar_separation']], [-metalens['pillar_separation'], metalens['pillar_separation']])) pillar_locs.append((0, 0)) pillars = [ mp.Cylinder(radius=metalens['pillar_radius'], height=metalens['pillar_height'], center=mp.Vector3(x, y, 0), material=matter) for x, y in pillar_locs ] substrate = [ mp.Block(size=mp.Vector3(metalens['sim_cell_width'], metalens['sim_cell_width'], metalens['sim_cell_width'] / 2), center=mp.Vector3(0, 0, -metalens['sim_cell_width'] / 4), material=matter) ] geometry = substrate + pillars cell = mp.Vector3(metalens['sim_cell_width'], metalens['sim_cell_width'], metalens['sim_cell_width']) pml = [mp.PML(metalens['PML_width'])] source = [ mp.Source(src=mp.ContinuousSource(wavelength=metalens['wavelength']), component=mp.Ex, center=mp.Vector3(0, 0, -metalens['sim_cell_width'] / 4), size=mp.Vector3(0, 0, 0)) ] symmetries = [] for symmetry in metalens['symmetries']: if symmetry == 'x': symmetries.append(mp.Mirror(mp.X)) if symmetry == 'y': symmetries.append(mp.Mirror(mp.Y)) sim = mp.Simulation(cell_size=cell, boundary_layers=pml, geometry=geometry, force_complex_fields=metalens['complex_fields'], symmetries=symmetries, sources=source, resolution=metalens['resolution']) start_time = time() mp.quiet(metalens['quiet']) # mp.verbosity(3) sim.init_sim() metalens['time_for_init'] = (time() - start_time) start_time = time() #sim.run(mp.at_end(mp.output_efield(sim)),until=metalens['sim_time']) sim.run(until=metalens['sim_time']) metalens['time_for_running'] = time() - start_time start_time = time() sim.filename_prefix = 'standard_candle-%d' % metalens['run_date'] print(sim.filename_prefix) mp.output_efield(sim) mp.output_hfield(sim) # print("collecting fields...") # h5_fname = 'candle-{run_date}.h5'.format(run_date = metalens['run_date']) # if metalens['save_fields_to_h5'] and rank ==0:#(not os.path.exists(h5_fname)): # metalens['h5_fname'] = h5_fname # h5_file = h5py.File(h5_fname,'w', driver='mpio', comm=MPI.COMM_WORLD) # fields = h5_file.create_group('fields') # Ex = np.transpose(sim.get_array(component=mp.Ex),(2,1,0)) # metalens['voxels'] = Ex.shape[0]**3 # metalens['voxels'] = Ex.shape[0]**3 # metalens['above_pillar_index'] = int(Ex.shape[0]/2. + 3*metalens['pillar_height']*metalens['resolution']/2) # mp.output_efield('test') # fields.create_dataset('Ex', # data=Ex) # del Ex # fields.create_dataset('Ey', # data=np.transpose(sim.get_array(component=mp.Ey),(2,1,0))) # fields.create_dataset('Ez', # data=np.transpose(sim.get_array(component=mp.Ez),(2,1,0))) # fields.create_dataset('Hx', # data=np.transpose(sim.get_array(component=mp.Hx),(2,1,0))) # fields.create_dataset('Hy', # data=np.transpose(sim.get_array(component=mp.Hy),(2,1,0))) # fields.create_dataset('Hz', # data=np.transpose(sim.get_array(component=mp.Hz),(2,1,0))) # h5_file.close() # else: # metalens['fields'] = { # 'Ex': np.transpose(sim.get_array(component=mp.Ex),(2,1,0)), # 'Ey': np.transpose(sim.get_array(component=mp.Ey),(2,1,0)), # 'Ez': np.transpose(sim.get_array(component=mp.Ez),(2,1,0)), # 'Hx': np.transpose(sim.get_array(component=mp.Hx),(2,1,0)), # 'Hy': np.transpose(sim.get_array(component=mp.Hy),(2,1,0)), # 'Hz': np.transpose(sim.get_array(component=mp.Hz),(2,1,0)) # } # metalens['voxels'] = metalens['fields']['Ex'].shape[0]**3 # metalens['above_pillar_index'] = int(metalens['fields']['Ex'].shape[0]/2. + 3*metalens['pillar_height']*metalens['resolution']/2) metalens['voxels'] = int((8 * metalens['resolution'])**3) metalens['time_for_saving_fields'] = round(time() - start_time, 0) metalens['max_mem_usage_in_Gb'] = metalens[ 'num_cores'] * resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1E6 metalens['max_mem_usage_in_Gb'] = round(metalens['max_mem_usage_in_Gb'], 2) metalens['summary'] = '''init time = {time_for_init:.1f} s running simulation = {time_for_running:.1f} s saving fields = {time_for_saving_fields:.1f} s num voxels = {voxels} max memory usage = {max_mem_usage_in_Gb:.2f} Gb'''.format(**metalens) metalens['pkl_fname'] = 'standard-candle-%d.pkl' % metalens['run_date'] return metalens