예제 #1
0
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))
예제 #2
0
파일: solver.py 프로젝트: kwadwo00/spins-b
def main() -> None:
    """Executes an optimization plan from command line."""
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument("--json_file", type=str)
    args = parser.parse_args()

    # Read in json.
    with open(args.json_file, "r") as fp:
        plan = optplan.loads(fp.read())

    run_plan(plan, os.path.dirname(args.json_file))
예제 #3
0
파일: grating.py 프로젝트: shanham/spins-b
def resume_opt(save_folder: str) -> None:
    """Resumes a stopped optimization.

    This restarts an optimization that was stopped prematurely. Note that
    resuming an optimization will not lead the exact same results as if the
    optimization were finished the first time around.

    Args:
        save_folder: Location where log files are saved. It is assumed that
            the optimization plan is also saved there.
    """
    # Load the optimization plan.
    with open(os.path.join(save_folder, "optplan.json")) as fp:
        plan = optplan.loads(fp.read())

    # Run the plan with the `resume` flag to restart.
    problem_graph.run_plan(plan, ".", save_folder=save_folder, resume=True)
예제 #4
0
def gen_gds(save_folder: str, sim_width: float) -> None:
    """Generates a GDS file of the grating.

    Args:
        save_folder: Location where log files are saved. It is assumed that
            the optimization plan is also saved there.
        sim width: width of the simulation
    """
    # Load the optimization plan.
    with open(os.path.join(save_folder, "optplan.json")) as fp:
        plan = optplan.loads(fp.read())
    dx = plan.transformations[-1].parametrization.simulation_space.mesh.dx

    # Load the data from the latest log file.
    with open(workspace.get_latest_log_file(save_folder), "rb") as fp:
        log_data = pickle.load(fp)
        if log_data["transformation"] != plan.transformations[-1].name:
            raise ValueError("Optimization did not run until completion.")

        coords = log_data["parametrization"]["vector"] * dx


#        if plan.transformations[-1].parametrization.inverted:
#            coords = np.insert(coords, 0, 0, axis=0)
#            coords = np.insert(coords, -1, sim_width, axis=0)

# TODO Not sure about this part below creating rectangles
# Change the variables and names here

# `coords` now contains the location of the grating edges. Now draw a
# series of rectangles to represent the grating.
    grating_poly = []
    for i in range(0, len(coords), 2):
        grating_poly.append(
            ((coords[i], -sim_width / 2), (coords[i], sim_width / 2),
             (coords[i - 1], sim_width / 2), (coords[i - 1], -sim_width / 2)))

    # Save the grating to `annulus.gds`.
    grating = gdspy.Cell("ANNULUS", exclude_from_current=True)
    grating.add(gdspy.PolygonSet(grating_poly, 100))
    gdspy.write_gds(os.path.join(save_folder, "annulus.gds"), [grating],
                    unit=1.0e-9,
                    precision=1.0e-9)
예제 #5
0
def test_dump_and_load():
    plan = generate_wdm_2d()
    serialized_plan = optplan.dumps(plan)
    deserialized_plan = optplan.loads(serialized_plan)