Ejemplo n.º 1
0
def test_lj():
    def params_relation(params):
        sigma = params.get_value("sigma")
        epsilon = params.get_value("epsilon")
        epsilon[0] = 2 * sigma[0]
        params.set_value("epsilon", epsilon)

    model = LennardJones(params_relation_callback=params_relation)

    # set optimizing parameters
    model.set_opt_params(sigma=[[1.1, "fix"]], epsilon=[[2.1, None, 3.0]])

    model.echo_model_params()
    model.echo_opt_params()

    with warnings.catch_warnings():  # context manager to ignore warning
        warnings.simplefilter("ignore")

        # set model_params by reading from file (the same as set model_params directly)
        filename = "tmp_lj_params.txt"
        write_tmp_params(filename)
        model.read_opt_params(filename)
        delete_tmp_params(filename)

        model.echo_model_params()
        model.echo_opt_params()

    path = (Path(__file__).parents[1].joinpath(
        "configs_extxyz/MoS2/MoS2_energy_forces_stress.xyz"))
    config = Configuration.from_file(path)

    energy_forces_stress(model, config, True, False, False)
    energy_forces_stress(model, config, True, True, False)
    energy_forces_stress(model, config, True, True, True)
Ejemplo n.º 2
0
def test_desc():

    test_file_path = Path(__file__).parents[1].joinpath("configs_extxyz")
    conf = Configuration.from_file(test_file_path.joinpath("Si.xyz"))

    cut_name = "cos"
    cut_dists = {"Si-Si": 5.0}
    hyperparams = {
        "jmax": 2,
        "rfac0": 1,
        "diagonalstyle": 3,
        "rmin0": 0,
        "switch_flag": 1,
        "bzero_flag": 0,
        "weight": {
            "Si": 1.0
        },
    }

    desc = Bispectrum(cut_dists, cut_name, hyperparams)
    zeta, dzeta_dr = desc.transform(conf, grad=True)
    assert desc.get_size() == 14
    assert np.allclose(zeta, zeta_ref)
    assert np.allclose(dzeta_dr[0][:2], dzeta_dr_001)
    assert np.allclose(dzeta_dr[-1][-2:], dzeta_dr_minus_121)

    zeta, dzeta_dr = desc.transform(conf, grad=False)
    assert np.allclose(zeta, zeta_ref)
    assert dzeta_dr is None
Ejemplo n.º 3
0
def test_desc():
    test_file_path = Path(__file__).parents[1].joinpath("configs_extxyz")
    config = Configuration.from_file(test_file_path.joinpath("Si.xyz"))

    desc = get_descriptor()

    for fit_forces, fit_stress in itertools.product([False, True],
                                                    [False, True]):
        zeta, dzetadr_forces, dzetadr_stress = desc.transform(
            config, fit_forces, fit_stress)
        assert np.allclose(zeta, zeta_ref)
        if fit_forces:
            assert np.allclose(dzetadr_forces[0], dzetadr_forces_ref)
        if fit_stress:
            assert np.allclose(dzetadr_stress[0], dzetadr_stress_ref)
Ejemplo n.º 4
0
def test_descriptor():
    test_file_path = Path(__file__).parents[1].joinpath("configs_extxyz")
    fname = test_file_path.joinpath("Si.xyz")
    conf = Configuration.from_file(fname)
    conf.identifier = str(fname)
    configs = [conf, conf]

    for normalize, fit_forces, fit_stress, serial in itertools.product(
        [False, True], [False, True], [False, True], [False, True]):
        desc = ExampleDescriptor(normalize)
        desc.generate_fingerprints(configs,
                                   fit_forces,
                                   fit_stress,
                                   use_welford_method=serial)
        data = load_fingerprints("fingerprints.pkl")[0]

        if normalize:
            assert_mean_stdev(desc.mean, desc.stdev, _mean, _stdev)
            assert np.allclose(data["zeta"], _normalized_zeta)
            if fit_forces:
                assert np.allclose(data["dzetadr_forces"],
                                   _normalized_dzetadr_forces)
            if fit_stress:
                assert np.allclose(data["dzetadr_stress"],
                                   _normalized_dzetadr_stress)
        else:
            assert_mean_stdev(desc.mean, desc.stdev, None, None)
            assert np.allclose(data["zeta"], _zeta)
            if fit_forces:
                assert np.allclose(data["dzetadr_forces"], _dzetadr_forces)
            if fit_stress:
                assert np.allclose(data["dzetadr_stress"], _dzetadr_stress)

    # check when normalize is True, if mean and stdev is provided by user, it has to be
    # correct.
    for normalize, fp_path, mean_std_path in itertools.product(
        [False, True], ["fp.pkl"], [None, "ms.pkl"]):

        delete_file("fingerprints.pkl")
        delete_file("fingerprints_mean_and_stdev.pkl")
        delete_file("fp.pkl")
        delete_file("ms.pkl")

        desc = ExampleDescriptor(normalize)

        if normalize and mean_std_path is not None:
            # will raise exception because the file mean_std_path does not exist
            try:
                desc.generate_fingerprints(
                    configs,
                    fingerprints_filename=fp_path,
                    fingerprints_mean_stdev_filename=mean_std_path,
                )
            except DescriptorError:
                pass

        else:
            desc.generate_fingerprints(
                configs,
                fingerprints_filename=fp_path,
                fingerprints_mean_stdev_filename=mean_std_path,
            )

            if fp_path is None:
                fp_path = "fingerprints.pkl"
            data = load_fingerprints(fp_path)[0]

            if normalize:
                assert_mean_stdev(desc.mean, desc.stdev, _mean, _stdev)
                assert np.allclose(data["zeta"], _normalized_zeta)
            else:
                assert_mean_stdev(desc.mean, desc.stdev, None, None)
                assert np.allclose(data["zeta"], _zeta)