コード例 #1
0
# declare relaxation object
oal_relaxation = Relaxation(
    oal_initial_structure,
    all_params["relaxation"]["optimizer"],
    fmax=all_params["relaxation"]["fmax"],
    steps=all_params["relaxation"]["steps"],
    maxstep=all_params["relaxation"]["maxstep"],
)

# declare offline learner
learner = OfflineActiveLearner(
    all_params["learner"],
    [oal_initial_structure] + images,
    ml_potential,
    parent_calc,
    base_calc,
    mongo_db=mongo_db,
)

if os.path.exists("dft_calls.db"):
    os.remove("dft_calls.db")

if mongo_db is not None:
    with open("runid.txt", "a") as f:
        f.write(str(learner.mongo_wrapper.run_id) + "\n")

learner.learn()

if hasattr(parent_calc, "close"):
    parent_calc.close()
コード例 #2
0
ファイル: run_al.py プロジェクト: ulissigroup/al_mlp
def active_learning(config):
    initial_traj = Trajectory(config["links"]["traj"])
    initial_index = config["links"].get("initial_index", 0)
    initial_structure = initial_traj[initial_index]
    images = []

    if "images_path" in config["links"] and config["links"][
            "images_path"] is not None:
        with connect(config["links"]["images_path"]) as pretrain_db:
            for row in pretrain_db.select():
                image = row.toatoms(attach_calculator=False)
                image.calc.implemented_properties.append("energy")
                image.calc.implemented_properties.append("forces")
                images.append(image)

    mongo_db = None
    if "MONGOC" in os.environ:
        mongo_string = os.environ["MONGOC"]
        mongo_db = MongoClient(mongo_string)["al_db"]
    else:
        print("no recording to mongo db")

    dbname = (str(config["links"]["ml_potential"]) + "_" +
              str(initial_structure.get_chemical_formula()) + "_oal")
    oal_initial_structure = initial_structure

    # begin setting up parent calc
    parent_str = config["links"].get("parent_calc", "vasp")
    # calculate kpts
    if (parent_str == "vasp" or parent_str
            == "vasp_interactive") and "kpts" not in config["vasp"]:
        config["vasp"]["kpts"] = calculate_surface_k_points(initial_structure)
    # declare parent calc
    if parent_str == "vasp":
        parent_calc = Vasp(**config["vasp"])
    elif parent_str == "vasp_interactive":
        parent_calc = VaspInteractive(**config["vasp"])
    elif parent_str == "emt":
        parent_calc = EMT()

    # declare base calc (if path is given)
    if "ocp" in config:
        if "model_path" in config["ocp"] and "checkpoint_path" in config["ocp"]:
            base_calc = OCPCalculator(
                config_yml=config["ocp"]["model_path"],
                checkpoint=config["ocp"]["checkpoint_path"],
            )
        elif "checkpoint_path" in config["ocp"]:
            base_calc = OCPCalculator(
                checkpoint=config["ocp"]["checkpoint_path"], )

    # use given ml potential class
    potential_class = config["links"].get("ml_potential", "flare")
    if potential_class == "flare":
        # declare ml calc
        ml_potential = FlarePPCalc(config["flare"],
                                   [initial_structure] + images)
    elif potential_class == "pyflare":
        ml_potential = FlareCalc(config.get("pyflare", {}),
                                 [initial_structure] + images)
    elif potential_class == "flare_ocp_descriptor":
        ml_potential = FlareOCPDescriptorCalc(
            model_path=config["ocp"]["model_path"],
            checkpoint_path=config["ocp"]["checkpoint_path"],
            flare_params=config.get("pyflare", {}),
            initial_images=[initial_structure] + images,
        )
    elif potential_class == "ocpd_gp":
        ml_potential = OCPDGPCalc(
            model_path=config["ocp"]["model_path"],
            checkpoint_path=config["ocp"]["checkpoint_path"],
            gp_params=config.get("gp", {}),
        )
    elif potential_class == "ocpd_nn":
        ml_potential = OCPDNNCalc(
            initial_structure,
            model_path=config["ocp"]["model_path"],
            checkpoint_path=config["ocp"]["checkpoint_path"],
            nn_params=config.get("nn", {}),
        )
    elif potential_class == "ft_en":
        ml_potential = FinetunerEnsembleCalc(
            model_classes=config["ocp"]["model_class_list"],
            model_paths=config["ocp"]["model_path_list"],
            checkpoint_paths=config["ocp"]["checkpoint_path_list"],
            mlp_params=config.get("finetuner", {}),
        )
    elif potential_class == "ft_ss":
        ml_potential = FinetunerStochasticSpinconvCalc(
            model_path=config["ocp"]["model_path"],
            checkpoint_path=config["ocp"]["checkpoint_path"],
            mlp_params=config.get("finetuner", {}),
        )

    # use given learner class
    learner_class = config["links"].get("learner_class", "online")
    if learner_class == "online":
        # declare online learner
        learner = OnlineLearner(
            config["learner"],
            images,
            ml_potential,
            parent_calc,
            mongo_db=mongo_db,
            optional_config=config,
        )

        run_relaxation(
            oal_initial_structure,
            config,
            learner,
            dbname,
            mongo_db,
        )

    elif learner_class == "delta":
        # declare online learner
        learner = DeltaLearner(
            config["learner"],
            images,
            ml_potential,
            parent_calc,
            base_calc=base_calc,
            mongo_db=mongo_db,
            optional_config=config,
        )

        run_relaxation(
            oal_initial_structure,
            config,
            learner,
            dbname,
            mongo_db,
        )

    elif learner_class == "warmstart":
        # declare warmstart online learner
        learner = WarmStartLearner(
            config["learner"],
            images,
            ml_potential,
            parent_calc,
            base_calc=base_calc,
            mongo_db=mongo_db,
            optional_config=config,
        )

        run_relaxation(
            oal_initial_structure,
            config,
            learner,
            dbname,
            mongo_db,
        )

    elif learner_class == "offline":
        # set atomistic method
        config["learner"]["atomistic_method"] = {}
        config["learner"]["atomistic_method"]["initial_traj"] = config[
            "links"]["traj"]
        config["learner"]["atomistic_method"]["fmax"] = config["relaxation"][
            "fmax"]
        config["learner"]["atomistic_method"]["steps"] = config["relaxation"][
            "steps"]
        config["learner"]["atomistic_method"]["maxstep"] = config[
            "relaxation"]["maxstep"]

        # declare learner
        learner = OfflineActiveLearner(
            learner_params=config["learner"],
            training_data=images,
            ml_potential=ml_potential,
            parent_calc=parent_calc,
            base_calc=base_calc,
            mongo_db=mongo_db,
            optional_config=config,
        )

        # do boilerplate stuff
        do_between_learner_and_run(learner, mongo_db)

        # start run
        learner.learn()

    else:
        print("No valid learner class given")

    # close parent_calc (if it needs to be closed, i.e. VaspInteractive)
    if hasattr(parent_calc, "close"):
        parent_calc.close()

    return learner.info