def run_opt(save_folder: str) -> None: """Main optimization script. This function setups the optimization and executes it. Args: save_folder: Location to save the optimization data. """ os.makedirs(save_folder) sim_space = create_sim_space("sim_fg.gds", "sim_bg.gds", box_thickness=2000, wg_thickness=220, etch_frac=0.5) obj, monitors = create_objective(sim_space) trans_list = create_transformations(obj, monitors, 50, 200, sim_space, min_feature=100) plan = optplan.OptimizationPlan(transformations=trans_list) # Save the optimization plan so we have an exact record of all the # parameters. with open(os.path.join(save_folder, "optplan.json"), "w") as fp: fp.write(optplan.dumps(plan)) # Execute the optimization and indicate that the current folder (".") is # the project folder. The project folder is the root folder for any # auxiliary files (e.g. GDS files). problem_graph.run_plan(plan, ".", save_folder=save_folder)
def loads(serialized_plan: str) -> optplan.OptimizationPlan: """Loads a serialized optimization plan. Args: serialized_plan: Serialized plan. Returns: Stored `optplan.OptimizationPlan`. """ plan = optplan.OptimizationPlan(json.loads(serialized_plan)) validate(plan) _validate_optplan_version(plan.version) # Unpack the optimization plan. # First create a dictionary mapping strings to the node. node_map = {} for node in plan.nodes: node_map[node.name] = node # Now, recursively fill any node names with the actual node. def process_field(model: models.Model, child_model: str) -> models.Model: return node_map[child_model] visited = set() if plan.transformations: for transformation in plan.transformations: _iter_optplan_fields(transformation, visited, process_field) for node in plan.nodes: _iter_optplan_fields(node, visited, process_field) validate_references(plan) return plan
def test_kerr(tmpdir): folder = os.path.join(tmpdir, 'kerr_test_results') _copyfiles(CUR_DIR, folder, ["sim_fg_kerr.gds", "sim_bg_kerr.gds"]) sim_space = kerr.create_sim_space("sim_fg_kerr.gds", "sim_bg_kerr.gds") obj, monitors = kerr.create_objective(sim_space) trans_list = kerr.create_transformations( obj, monitors, sim_space, cont_iters=20, min_feature=100) plan = optplan.OptimizationPlan(transformations=trans_list) problem_graph.run_plan(plan, folder)
def test_dumps(): plan = optplan.OptimizationPlan( nodes=[ optplan.Sum( functions=[ optplan.Constant(value=optplan.ComplexNumber(real=2)), optplan.Constant(value=optplan.ComplexNumber(real=3)), ],) ],) plan_dict = json.loads(optplan.dumps(plan))
def test_wdm2(tmpdir): folder = str(tmpdir.mkdir("wdm2")) _copyfiles(CUR_DIR, folder, ["sim_fg.gds", "sim_bg.gds"]) sim_space = wdm2.create_sim_space("sim_fg.gds", "sim_bg.gds") obj, monitors = wdm2.create_objective(sim_space) trans_list = wdm2.create_transformations(obj, monitors, sim_space, cont_iters=1, min_feature=100) plan = optplan.OptimizationPlan(transformations=trans_list) problem_graph.run_plan(plan, folder)
def test_dumps_duplicate_name_raises_value_error(): plan = optplan.OptimizationPlan( nodes=[ optplan.Sum( functions=[ optplan.Constant( name="const", value=optplan.ComplexNumber(real=2)), optplan.Constant( name="const", value=optplan.ComplexNumber(real=3)), ],) ],) with pytest.raises(ValueError, match="Nonunique name found"): optplan.dumps(plan)
def test_phc(tmpdir): folder = os.path.join(tmpdir, 'phc_gvd_test') fg = "sim_fg_rect.gds" bg = "sim_bg_rect.gds" _copyfiles(CUR_DIR, folder, [fg, bg]) sim_space = phc_rect.create_sim_space(fg, bg) obj, monitors = phc_rect.create_objective(sim_space) trans_list = phc_rect.create_transformations(obj, monitors, sim_space, cont_iters=42, min_feature=60, num_stages=6) plan = optplan.OptimizationPlan(transformations=trans_list) problem_graph.run_plan(plan, folder)
def test_custom_function_node(): @optplan.register_node_type(optplan.NodeMetaType.OPTPLAN_NODE) class CustomOp(optplan.Function): type = schema_utils.polymorphic_model_type("custom_op") int_val = types.IntType() plan = optplan.OptimizationPlan( nodes=[ optplan.Sum( functions=[ optplan.Constant(value=optplan.ComplexNumber(real=2)), CustomOp(int_val=3), ],) ],) optplan.loads(optplan.dumps(plan))
def test_phase(tmpdir): folder = os.path.join(tmpdir, 'GVD_test_wg') _copyfiles(CUR_DIR, folder, ["sim_fg_wg.gds", "sim_bg_wg.gds"]) sim_space = phase.create_sim_space("sim_fg_wg.gds", "sim_bg_wg.gds") obj, power_obj, monitors = phase.create_objective(sim_space) trans_list = phase.create_transformations(obj, monitors, sim_space, cont_iters=80, min_feature=40, num_stages=4) # power_obj=power_obj, power_iters=6, power_stages=2) plan = optplan.OptimizationPlan(transformations=trans_list) # with open("plan_dump.json", "w") as plan_dump_file: # plan_dump_file.write(optplan.dumps(plan)) problem_graph.run_plan(plan, folder)
def run_opt(save_folder: str) -> None: """Main optimization script. This function setups the optimization and executes it. Args: save_folder: Location to save the optimization data. """ # os.makedirs(save_folder) os.makedirs(save_folder, exist_ok=True) sim_space = create_sim_space("sim_fg.gds", "sim_bg.gds", wg_thickness=wg_thickness, wg_length=wg_length, wg_width=wg_width, buffer_len=buffer_len, dx=dx, num_pmls=num_pmls) obj, monitors = create_objective(sim_space, wg_thickness=wg_thickness, wg_length=wg_length, wg_width=wg_width, buffer_len=buffer_len, dx=dx, num_pmls=num_pmls) trans_list = create_transformations(obj, monitors, sim_space, 200, num_stages=5, min_feature=100) plan = optplan.OptimizationPlan(transformations=trans_list) # Save the optimization plan so we have an exact record of all the # parameters. with open(os.path.join(save_folder, "optplan.json"), "w") as fp: fp.write(optplan.dumps(plan)) # Copy over the GDS files. shutil.copyfile("sim_fg.gds", os.path.join(save_folder, "sim_fg.gds")) shutil.copyfile("sim_bg.gds", os.path.join(save_folder, "sim_bg.gds")) # Execute the optimization and indicate that the current folder (".") is # the project folder. The project folder is the root folder for any # auxiliary files (e.g. GDS files). problem_graph.run_plan(plan, ".", save_folder=save_folder)
def test_grating(tmpdir): folder = str(tmpdir.mkdir("grating")) _copyfiles(CUR_DIR, folder, ["sim_fg.gds", "sim_bg.gds"]) sim_space = grating.create_sim_space(os.path.join(folder, "sim_fg.gds"), os.path.join(folder, "sim_bg.gds"), box_thickness=2000, wg_thickness=220, etch_frac=0.5) obj, monitors = grating.create_objective(sim_space) trans_list = grating.create_transformations(obj, monitors, 1, 2, sim_space, min_feature=100) plan = optplan.OptimizationPlan(transformations=trans_list) problem_graph.run_plan(plan, folder)
def main() -> None: """Runs the optimization.""" # Create the simulation space using the GDS files. sim_space = create_sim_space("sim_fg.gds", "sim_bg.gds") # Setup the objectives and all values that should be recorded (monitors). obj, monitors = create_objective(sim_space) # Create the list of operations that should be performed during # optimization. In this case, we use a series of continuous parametrizations # that approximate a discrete structure. trans_list = create_transformations( obj, monitors, sim_space, cont_iters=100, min_feature=100) # Execute the optimization and indicate that the current folder (".") is # the project folder. The project folder is the root folder for any # auxiliary files (e.g. GDS files). By default, all log files produced # during the optimization are also saved here. This can be changed by # passing in a third optional argument. plan = optplan.OptimizationPlan(transformations=trans_list) problem_graph.run_plan(plan, ".")
def run_opt(save_folder: str, sim_width: float, wg_width: float) -> None: """Main optimization script. This function setups the optimization and executes it. Args: save_folder: Location to save the optimization data. """ os.makedirs(save_folder) sim_space = create_sim_space("sim_fg.gds", "sim_bg.gds", sim_width=sim_width, wg_width=wg_width) obj, monitors = create_objective(sim_space, sim_width=sim_width, wg_width=wg_width) # or a grating length trans_list = create_transformations(obj, monitors, 60, 200, sim_space, min_feature=100) plan = optplan.OptimizationPlan(transformations=trans_list) # Save the optimization plan so we have an exact record of all the # parameters. with open(os.path.join(save_folder, "optplan.json"), "w") as fp: fp.write(optplan.dumps(plan)) # Copy over the GDS files. shutil.copyfile("sim_fg.gds", os.path.join(save_folder, "sim_fg.gds")) shutil.copyfile("sim_bg.gds", os.path.join(save_folder, "sim_bg.gds")) # Execute the optimization and indicate that the current folder (".") is # the project folder. The project folder is the root folder for any # auxiliary files (e.g. GDS files). problem_graph.run_plan(plan, ".", save_folder=save_folder) # Generate the GDS file. gen_gds(save_folder, sim_width, wg_width)