def get_monitor_data(monitor_name, monitor_field): lumerical_data_name = "monitor_data_" + monitor_name + "_" + monitor_field extracted_data_name = lumerical_data_name + "_data" data_transfer_filename = projects_directory_location + "/data_transfer_" + monitor_name + "_" + monitor_field command_read_monitor = lumerical_data_name + " = getresult(\'" + monitor_name + "\', \'" + monitor_field + "\');" command_extract_data = extracted_data_name + " = " + lumerical_data_name + "." + monitor_field + ";" command_save_data_to_file = "matlabsave(\'" + data_transfer_filename + "\', " + extracted_data_name + ");" lumapi.evalScript(fdtd_hook.handle, command_read_monitor) lumapi.evalScript(fdtd_hook.handle, command_extract_data) # start_time = time.time() lumapi.evalScript(fdtd_hook.handle, command_save_data_to_file) monitor_data = {} load_file = h5py.File(data_transfer_filename + ".mat", 'r') monitor_data = np.array(load_file[extracted_data_name]) # end_time = time.time() # print("\nIt took " + str(end_time - start_time) + " seconds to transfer the monitor data\n") return monitor_data
def run_EME(contraDC, simulation_setup, close = True): mode = lumapi.open('mode') projectname = 'ContraDC_EME' filename = 'ContraDC_EMEscript' command ='cd("%s");'%dir_path command += 'min_wavelength=%s;'%simulation_setup.lambda_start command += 'max_wavelength=%s;'%simulation_setup.lambda_end command += 'grating_period=%s;'%contraDC.period command += 'Wa=%s;'%contraDC.w1 command += 'Wb=%s;'%contraDC.w2 command += 'dWa=%s;'%contraDC.dW1 command += 'dWb=%s;'%contraDC.dW2 command += 'gap=%s;'%contraDC.gap command += 'number_of_periods=%s;'%contraDC.N command += 'wl_ref = %s;'%simulation_setup.central_lambda if contraDC.pol == 'TE': command += "pol='TE';" else: command += "pol='TM';" command += "load('%s');"%projectname command += '%s;'%filename lumapi.evalScript(mode, command) delta_lambda_contra = lumapi.getVar(mode,"delta_lambda") lambda_contra = lumapi.getVar(mode,"lambda0") delta_lambda_self1 = lumapi.getVar(mode,"delta_lambda_self1") delta_lambda_self2 = lumapi.getVar(mode,"delta_lambda_self2") if close == True: lumapi.close(mode) return [delta_lambda_contra, delta_lambda_self1, delta_lambda_self2, lambda_contra]
def run_INTC(): intc = lumapi.open('interconnect') svg_file = "contraDC.svg" sparam_file = "ContraDC_sparams.dat" command = 'cd("%s");' % dir_path command += 'switchtodesign; new; deleteall; \n' command += 'addelement("Optical N Port S-Parameter"); createcompound; select("COMPOUND_1");\n' command += 'component = "contraDC"; set("name",component); \n' command += 'seticon(component,"%s");\n' % (svg_file) command += 'select(component+"::SPAR_1"); set("load from file", true);\n' command += 'set("s parameters filename", "%s");\n' % (sparam_file) command += 'set("load from file", false);\n' command += 'set("passivity", "enforce");\n' command += 'addport(component, "%s", "Bidirectional", "Optical Signal", "%s",%s);\n' % ( "Port 1", 'Left', 0.25) command += 'connect(component+"::RELAY_%s", "port", component+"::SPAR_1", "port %s");\n' % ( 1, 1) command += 'addport(component, "%s", "Bidirectional", "Optical Signal", "%s",%s);\n' % ( "Port 2", 'Left', 0.75) command += 'connect(component+"::RELAY_%s", "port", component+"::SPAR_1", "port %s");\n' % ( 2, 2) command += 'addport(component, "%s", "Bidirectional", "Optical Signal", "%s",%s);\n' % ( "Port 3", 'Right', 0.25) command += 'connect(component+"::RELAY_%s", "port", component+"::SPAR_1", "port %s");\n' % ( 3, 3) command += 'addport(component, "%s", "Bidirectional", "Optical Signal", "%s",%s);\n' % ( "Port 4", 'Right', 0.75) command += 'connect(component+"::RELAY_%s", "port", component+"::SPAR_1", "port %s");\n' % ( 4, 4) command += 'addtolibrary;\n' lumapi.evalScript(intc, command) return
def generate_dat( contraDC, simulation_setup, S_Matrix, close = True ): mode = lumapi.open('mode') # feed polarization into model if contraDC.pol == 'TE': lumapi.evalScript(mode,"mode_label = 'TE'; mode_ID = '1';") elif contraDC.pol == 'TM': lumapi.evalScript(mode,"mode_label = 'TM'; mode_ID = '2';") # run write sparams script lumapi.evalScript(mode,"cd('%s');"%dir_path) lumapi.evalScript(mode,'write_sparams;') if close == True: lumapi.close(mode) run_INTC() return
def generate_dat(pol='TE', terminate=True): mode = lumapi.open('mode') # feed polarization into model if pol == 'TE': lumapi.evalScript(mode, "mode_label = 'TE'; mode_ID = '1';") elif pol == 'TM': lumapi.evalScript(mode, "mode_label = 'TM'; mode_ID = '2';") # run write sparams script lumapi.evalScript(mode, "cd('%s');" % dir_path) lumapi.evalScript(mode, 'write_sparams;') if terminate == True: lumapi.close(mode) run_INTC() return
def generate_dat(contraDC, simulation_setup, S_Matrix, sfile, close=False): global mode if not (mode): mode = lumapi.open("mode") # feed polarization into model if contraDC.pol == "TE": lumapi.evalScript(mode, "mode_label = 'TE'; mode_ID = '1';") elif contraDC.pol == "TM": lumapi.evalScript(mode, "mode_label = 'TM'; mode_ID = '2';") # run write sparams script lumapi.evalScript(mode, "cd('%s'); sfile = '%s'; " % (dir_path, sfile)) lumapi.evalScript(mode, "write_sparams;") if close: lumapi.close(mode) # run_INTC() return
def get_monitor_data(monitor_name, monitor_field): lumerical_data_name = "monitor_data_" + monitor_name + "_" + monitor_field extracted_data_name = lumerical_data_name + "_data" data_transfer_filename = projects_directory_location + "/data_transfer_" + monitor_name + "_" + monitor_field command_read_monitor = lumerical_data_name + " = getresult(\'" + monitor_name + "\', \'" + monitor_field + "\');" command_extract_data = extracted_data_name + " = " + lumerical_data_name + "." + monitor_field + ";" command_save_data_to_file = "matlabsave(\'" + data_transfer_filename + "\', " + extracted_data_name + ");" lumapi.evalScript(fdtd_hook.handle, command_read_monitor) lumapi.evalScript(fdtd_hook.handle, command_extract_data) lumapi.evalScript(fdtd_hook.handle, command_save_data_to_file) monitor_data = {} load_file = h5py.File(data_transfer_filename + ".mat", 'r') monitor_data = np.array(load_file[extracted_data_name]) return monitor_data
def get_efield_interpolated(monitor_name, spatial_limits_um, new_size): field_polarizations = ['Ex', 'Ey', 'Ez'] spatial_components = ['x', 'y', 'z'] data_xfer_size_MB = 0 start = time.time() for coord_idx in range(0, len(spatial_limits_um)): command_setup_old_coord = "old_coord_space_" + str( coord_idx ) + " = getdata(\'" + monitor_name + "\', \'" + spatial_components[ coord_idx] + "\');" command_setup_new_coord = "new_coord_space_" + str( coord_idx) + " = 1e-6 * linspace( " + str( spatial_limits_um[coord_idx][0]) + ", " + str( spatial_limits_um[coord_idx][1]) + ", " + str( new_size[coord_idx]) + " );" lumapi.evalScript(fdtd_hook.handle, command_setup_old_coord) lumapi.evalScript(fdtd_hook.handle, command_setup_new_coord) total_efield = np.zeros([len(field_polarizations)] + list(new_size) + [num_design_frequency_points], dtype=np.complex) for pol_idx in range(0, len(field_polarizations)): lumerical_data_name = "monitor_data_" + monitor_name + "_E" command_make_interpolated_array = "interpolated_data = zeros( " for coord_idx in range(0, len(spatial_limits_um)): command_make_interpolated_array += str(new_size[coord_idx]) + ", " command_make_interpolated_array += str( num_design_frequency_points) + " );" lumapi.evalScript(fdtd_hook.handle, command_make_interpolated_array) command_read_monitor = lumerical_data_name + " = getdata(\'" + monitor_name + "\', \'" + field_polarizations[ pol_idx] + "\');" lumapi.evalScript(fdtd_hook.handle, command_read_monitor) for wl_idx in range(0, num_design_frequency_points): command_data_by_wavelength = "wl_data = pinch( " + lumerical_data_name + "( :, :, :, " + str( wl_idx + 1) + " ) );" command_reassemble_by_wavelength = "interpolated_data( :, :, :, " + str( wl_idx + 1) + " ) = interpolated;" command_interpolate = "interpolated = interp( wl_data, " for coord_idx in range(0, len(spatial_limits_um)): command_interpolate += "old_coord_space_" + str( coord_idx) + ", " for coord_idx in range(0, len(spatial_limits_um)): command_interpolate += "new_coord_space_" + str(coord_idx) if coord_idx < (len(spatial_limits_um) - 1): command_interpolate += ", " command_interpolate += " );" lumapi.evalScript(fdtd_hook.handle, command_data_by_wavelength) lumapi.evalScript(fdtd_hook.handle, command_interpolate) lumapi.evalScript(fdtd_hook.handle, command_reassemble_by_wavelength) Epol = fdtd_hook.getv("interpolated_data") data_xfer_size_MB += Epol.nbytes / (1024. * 1024.) total_efield[pol_idx] = Epol # Epol_0 = fdtd_hook.getdata( monitor_name, field_polarizations[ 0 ] ) # data_xfer_size_MB += Epol_0.nbytes / ( 1024. * 1024. ) # total_efield = np.zeros( [ len (field_polarizations ) ] + list( Epol_0.shape ), dtype=np.complex ) # total_efield[ 0 ] = Epol_0 # for pol_idx in range( 1, len( field_polarizations ) ): # Epol = fdtd_hook.getdata( monitor_name, field_polarizations[ pol_idx ] ) # data_xfer_size_MB += Epol.nbytes / ( 1024. * 1024. ) # total_efield[ pol_idx ] = Epol elapsed = time.time() - start date_xfer_rate_MB_sec = data_xfer_size_MB / elapsed log_file = open(projects_directory_location + "/log.txt", 'a') log_file.write("Transferred " + str(data_xfer_size_MB) + " MB\n") log_file.write("Data rate = " + str(date_xfer_rate_MB_sec) + " MB/sec\n\n") log_file.close() return total_efield
# Run the optimization # for epoch in range(start_epoch, num_epochs): bayer_filter.update_filters(epoch) for iteration in range(0, num_iterations_per_epoch): iteration_start_time = time.time() print("Working on epoch " + str(epoch) + " and iteration " + str(iteration)) fdtd_hook.load(projects_directory_location + "/optimization.fsp") job_names = {} # fdtd_hook.switchtolayout() lumapi.evalScript(fdtd_hook.handle, 'switchtolayout;') cur_permittivity = np.flip(bayer_filter.get_permittivity(), axis=2) np.save(projects_directory_location + "/cur_permittivity.npy", cur_permittivity) cur_index = np.sqrt(cur_permittivity) # bordered_replicated_index = border_and_replicate_device( cur_index, device_border_voxels, min_device_index ) fdtd_hook.select(design_import['name']) fdtd_hook.importnk2(cur_index, bayer_filter_region_x, bayer_filter_region_y, bayer_filter_region_z) num_fdtd_jobs = 0
def run_mode(contraDC, simulation_setup, close=False): global mode if not (mode): mode = lumapi.open('mode') # feed parameters into model command = "gap = %s; width_1 = %s; width_2 = %s; thick_Si = %s; period = %s;" lumapi.evalScript( mode, command % (contraDC.gap, contraDC.w1, contraDC.w2, contraDC.thick_si, contraDC.period)) command = "wavelength = %s; wavelength_stop = %s;" lumapi.evalScript( mode, "wavelength = %s; wavelength_stop = %s;" % (simulation_setup.lambda_start, simulation_setup.lambda_end)) if contraDC.pol == 'TE': lumapi.evalScript(mode, "pol = 'TE';") elif contraDC.pol == 'TM': lumapi.evalScript(mode, "pol = 'TM';") if contraDC.slab == True: lumapi.evalScript(mode, "slab = 1;") lumapi.evalScript(mode, "thick_Slab = %s;" % (contraDC.thick_slab)) else: lumapi.evalScript(mode, "slab = 0;") lumapi.evalScript(mode, "thick_Slab = 0;") # run dispersion analysis script lumapi.evalScript(mode, "cd('%s');" % dir_path) lumapi.evalScript(mode, 'dispersion_2WG;') # contra-directional coupling neff_data = lumapi.getVar(mode, 'n_eff_data') lambda_fit = lumapi.getVar(mode, 'lambda_fit') ng_contra = lumapi.getVar(mode, 'ng0') # self-coupling ng1 = lumapi.getVar(mode, 'ng0_self1') ng2 = lumapi.getVar(mode, 'ng0_self2') lambda_self1 = lumapi.getVar(mode, 'self1_lambda') lambda_self2 = lumapi.getVar(mode, 'self2_lambda') if close == True: lumapi.close(mode) return [ neff_data, lambda_fit, ng_contra, ng1, ng2, lambda_self1, lambda_self2 ]
def run_FDTD(contraDC, simulation_setup, close=False): c = 299792458 #[m/s] frequency_start = c / simulation_setup.lambda_end frequency_end = c / simulation_setup.lambda_start fdtd = lumapi.open('fdtd') #,hide=True) filename = 'contraDC_bandstructure' lumapi.evalScript( fdtd, "load('%s'); setnamed('::model','gap',%s); setnamed('::model','ax',%s); setnamed('::model','sinusoidal',%s)" % (filename, contraDC.gap, contraDC.period, contraDC.sinusoidal)) if contraDC.slab == True: lumapi.evalScript( fdtd, "setnamed('::model','slab',1); setnamed('::model','slab_thickness',%s);" % (contraDC.thick_slab)) if contraDC.sinusoidal == True: lumapi.evalScript(fdtd, "setnamed('::mode','sinusoidal',1);") lumapi.evalScript( fdtd, "setnamed('::model','bus1_width',%s); setnamed('::model','bus1_delta',%s); setnamed('::model','bus2_width',%s); setnamed('::model','bus2_delta',%s);setnamed('::model','si_thickness',%s);" % (contraDC.w1, contraDC.dW1, contraDC.w2, contraDC.dW2, contraDC.thick_si)) lumapi.evalScript( fdtd, "setnamed('::model','f1',%s); setnamed('::model','f2',%s);" % (frequency_start, frequency_end)) lumapi.evalScript(fdtd, "cd('%s');" % dir_path) lumapi.evalScript(fdtd, 'kappa_bandstructure;') delta_lambda_contra = lumapi.getVar(fdtd, "delta_lambda") lambda_contra = lumapi.getVar(fdtd, "lambda0") delta_lambda_self1 = lumapi.getVar(fdtd, "delta_lambda_self1") delta_lambda_self2 = lumapi.getVar(fdtd, "delta_lambda_self2") if close == True: lumapi.close(fdtd) return [ delta_lambda_contra, delta_lambda_self1, delta_lambda_self2, lambda_contra ]
def lumapi_set_wavelength( wl_idx ): # lumerical script is one indexed so need to adjust from python indexing cmd = "wl_idx = " + str( wl_idx + 1 ) + ";" lumapi.evalScript( fdtd_hook.handle, cmd )
get_fom = fom_color( transmission_by_wavelength ) fom_by_gsst_state.append( get_fom ) # figure_of_merit_by_iteration_by_state_by_wavelength[ iteration, gsst_state, : ] = transmission_fom # fom_by_temp.append( transmission_fom )# np.mean( transmission_fom ) ) forward_e_fields = get_complex_monitor_data( design_efield_monitor[ 'name' ], 'E' ) # if gsst_state == 0: # for i in range( 0, 2 ): # gradient_by_temp.append( np.zeros( forward_e_fields[ 0, 0 ].shape ) ) adjoint_e_fields = np.zeros( forward_e_fields.shape, dtype=np.complex ) lumapi.evalScript( fdtd_hook.handle, ''.join( lumapi_pull_results.split() ) ) for wl_idx in range( 0, num_design_frequency_points ): fdtd_hook.switchtolayout() lumapi_set_wavelength( wl_idx ) shutil.copy( projects_directory_location + "/optimization.fsp", projects_directory_location + "/optimization_gsst_state_" + str( gsst_state ) + ".fsp" ) disable_all_sources() top_adjoint_source.enabled = 1 lumapi.evalScript( fdtd_hook.handle, ''.join( lumapi_import_source.split() )
def disable_all_sources(): lumapi.evalScript(fdtd_hook.handle, 'switchtolayout;') for xy_idx in range(0, 2): fdtd_hook.select( forward_sources[xy_idx]['name'] ) fdtd_hook.set( 'enabled', 0 )
# forward_src['wavelength start'] = lambda_min_um * 1e-6 # forward_src['wavelength stop'] = lambda_max_um * 1e-6 forward_src = fdtd_hook.addimportedsource() forward_src['name'] = 'forward_src_' + xy_names[xy_idx] forward_src['direction'] = 'Backward' # forward_src['x span'] = fdtd_region_size_lateral_um * 1e-6 # forward_src['y span'] = fdtd_region_size_lateral_um * 1e-6 forward_src['x'] = 0 * 1e-6 forward_src['y'] = 0 * 1e-6 forward_src['z'] = src_maximum_vertical_um * 1e-6 forward_src['wavelength start'] = src_lambda_min_um * 1e-6 forward_src['wavelength stop'] = src_lambda_max_um * 1e-6 lumapi.evalScript( fdtd_hook.handle, 'matlabload( \'' + str(custom_source_paths[xy_idx]) + '\' );') lumapi.evalScript( fdtd_hook.handle, 'src_dataset = rectilineardataset( x, y, ' + str(src_maximum_vertical_um) + ' );') lumapi.evalScript( fdtd_hook.handle, 'src_dataset.addparameter( \'lambda\', lambda, \'f\', f );') lumapi.evalScript(fdtd_hook.handle, 'src_dataset.addattribute( \'E\', E );') lumapi.evalScript(fdtd_hook.handle, 'src_dataset.addattribute( \'H\', H );') fdtd_hook.select(forward_src['name']) lumapi.evalScript(fdtd_hook.handle, 'importdataset( src_dataset );') lumapi.evalScript(fdtd_hook.handle, 'clear;')