Exemple #1
0
def test_desc():

    fname = "./configs_extxyz/Si.xyz"
    conf = Configuration(format="extxyz")
    conf.read(fname)

    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
Exemple #2
0
def test_neigh():
    conf = Configuration()
    fname = "configs_extxyz/bilayer_graphene/bilayer_sep3.36_i0_j0.xyz"
    conf.read(fname)
    conf.species[0] = "O"

    neigh = NeighborList(conf, infl_dist=2, padding_need_neigh=False)
    fname = "tmp_test_neighbor.xyz"
    cell = conf.get_cell()
    PBC = conf.get_PBC()
    coords = neigh.get_coords()
    species = neigh.get_species()
    write_config(fname, cell, PBC, species, coords, fmt="extxyz")

    assert np.allclose(coords, target_coords)
    assert np.array_equal(species, target_species)

    # contributing
    for i in range(conf.get_number_of_atoms()):
        nei_indices, nei_coords, nei_species = neigh.get_neigh(i)
        assert np.allclose(nei_indices, all_indices[i])

    # padding
    for i in range(conf.get_number_of_atoms(), len(coords)):
        nei_indices, nei_coords, nei_species = neigh.get_neigh(i)
        assert nei_indices.size == 0
        assert nei_coords.size == 0
        assert nei_species.size == 0

    numneigh, neighlist = neigh.get_numneigh_and_neighlist_1D(
        request_padding=False)
    np.array_equal(numneigh, all_numneigh)
    np.array_equal(neighlist, np.concatenate(all_indices))
Exemple #3
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)
def test_desc():

    config = Configuration(format="extxyz")
    config.read("./configs_extxyz/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)
Exemple #5
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
Exemple #6
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)
Exemple #7
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)
Exemple #8
0
def test_descriptor():
    fname = "./configs_extxyz/Si.xyz"
    conf = Configuration(format="extxyz", identifier=fname)
    conf.read(fname)
    configs = [conf, conf]

    # reuse should be the last and `True` should be after `False` so as to test reuse for
    # each case of normalize, fit_forces, and fit_stress
    for normalize, fit_forces, fit_stress, serial, reuse in itertools.product(
        [False, True], [False, True], [False, True], [False, True],
        [False, True]):
        desc = ExampleDescriptor(normalize)
        desc.generate_fingerprints(configs,
                                   fit_forces,
                                   fit_stress,
                                   reuse=reuse,
                                   serial=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], [None, "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_path=fp_path,
                    fingerprints_mean_and_stdev_path=mean_std_path,
                )
            except DescriptorError:
                pass

        else:
            desc.generate_fingerprints(
                configs,
                fingerprints_path=fp_path,
                fingerprints_mean_and_stdev_path=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)
Exemple #9
0
def test_configuration(e=True, f=False, s=False, order=False):
    fname = "./configs_extxyz/MoS2/MoS2"
    if e:
        fname += "_energy"
    if f:
        fname += "_forces"
    if s:
        fname += "_stress"
    fname += ".xyz"

    config = Configuration(format="extxyz", order_by_species=order)
    config.read(fname)

    natoms = config.get_number_of_atoms()
    cell = config.get_cell()
    PBC = config.get_PBC()
    species = config.get_species()
    coords = config.get_coordinates()
    if e:
        energy = config.get_energy()
    if f:
        forces = config.get_forces()
    if s:
        stress = config.get_stress()

    assert natoms == 288
    assert np.allclose(
        cell,
        [
            [33.337151, 0.035285, 0.03087],
            [0.027151, 25.621674, -0.000664],
            [0.027093, 9e-05, 30.262626],
        ],
    )
    assert np.allclose(PBC, [1, 1, 1])

    if e:
        assert energy == -5.302666

    if s:
        assert np.allclose(stress, [1.1, 2.2, 3.3, 4.4, 5.5, 6.6])

    if order:
        ref_species = ["Mo", "Mo", "Mo", "Mo", "Mo", "Mo"]
        ref_coords = [
            [0.051823, -0.017150, 16.736001],
            [2.819495, 1.607633, 16.734550],
            [5.601008, 0.015314, 16.738646],
        ]
        ref_forces = [
            [-0.425324, 0.295866, -0.065479],
            [0.245043, -0.061658, 0.264104],
            [-0.164382, -0.103679, -0.050262],
        ]
    else:
        ref_species = ["Mo", "Mo", "S", "S", "S", "S"]
        ref_coords = [
            [0.051823, -0.017150, 16.736001],
            [2.819495, 1.607633, 16.734550],
            [1.901722, -0.004881, 18.317009],
        ]
        ref_forces = [
            [-0.425324, 0.295866, -0.065479],
            [0.245043, -0.061658, 0.264104],
            [0.010127, 0.041539, 0.301571],
        ]

    assert_1d_array(species[:6], ref_species)
    assert np.allclose(coords[:3], ref_coords)
    if f:
        assert np.allclose(forces[:3], ref_forces)

    natoms_by_species = config.count_atoms_by_species()
    assert natoms_by_species["Mo"] == 96
    assert natoms_by_species["S"] == 192

    if not e:
        energy = None
    if not f:
        forces = None
    if not s:
        stress = None
    outname = fname.replace("./configs_extxyz/MoS2/MoS2", "tmp_Mos2")
    write_extxyz(outname, cell, PBC, species, coords, energy, forces, stress)