示例#1
0
    def create_paddings(self, cell, pbc, contributing_coords,
                        contributing_species_code):
        # Cast things passed through kimpy to numpy arrays
        cell = np.asarray(cell, dtype=np.double)
        pbc = np.asarray(pbc, dtype=np.intc)
        contributing_coords = np.asarray(contributing_coords, dtype=np.double)

        return neighlist.create_paddings(
            self.influence_dist,
            cell,
            pbc,
            contributing_coords,
            contributing_species_code,
        )
示例#2
0
    def _update_neigh(self, influence_distance: float):
        """
        Update neighbor list and model input.
        """

        # inquire information from conf
        cell = np.asarray(self.conf.cell, dtype=np.double)
        PBC = np.asarray(self.conf.PBC, dtype=np.intc)
        contributing_coords = np.asarray(self.conf.coords, dtype=np.double)
        contributing_species = self.conf.species
        num_contributing = self.conf.get_num_atoms()
        self.num_contributing_particles = num_contributing

        # species support and code
        unique_species = list(set(contributing_species))
        species_map = dict()
        for s in unique_species:
            if s in self.supported_species:
                species_map[s] = self.supported_species[s]
            else:
                report_error(f"species `{s}` not supported by model")
        contributing_species_code = np.array(
            [species_map[s] for s in contributing_species], dtype=np.intc)

        if any(PBC):  # need padding atoms
            out = nl.create_paddings(
                influence_distance,
                cell,
                PBC,
                contributing_coords,
                contributing_species_code,
            )
            padding_coords, padding_species_code, self.padding_image_of, error = out
            check_error(error, "nl.create_paddings")

            num_padding = padding_species_code.size
            self.num_particles = np.array([num_contributing + num_padding],
                                          dtype=np.intc)
            tmp = np.concatenate((contributing_coords, padding_coords))
            self.coords = np.asarray(tmp, dtype=np.double)
            tmp = np.concatenate(
                (contributing_species_code, padding_species_code))
            self.species_code = np.asarray(tmp, dtype=np.intc)
            self.particle_contributing = np.ones(self.num_particles[0],
                                                 dtype=np.intc)
            self.particle_contributing[num_contributing:] = 0

            # TODO check whether padding need neigh and create accordingly
            # for now, create neigh for all atoms, including paddings
            need_neigh = np.ones(self.num_particles[0], dtype=np.intc)

        else:  # do not need padding atoms
            self.padding_image_of = np.array([])
            self.num_particles = np.array([num_contributing], dtype=np.intc)
            self.coords = np.array(contributing_coords, dtype=np.double)
            self.species_code = np.array(contributing_species_code,
                                         dtype=np.intc)
            self.particle_contributing = np.ones(num_contributing,
                                                 dtype=np.intc)
            need_neigh = self.particle_contributing

        error = nl.build(
            self.neigh,
            self.coords,
            influence_distance,
            np.asarray([influence_distance], dtype=np.double),
            need_neigh,
        )
        check_error(error, "nl.build")
示例#3
0
文件: kim.py 项目: mjwen/kliff
    def _update_neigh(self, influence_distance: float):
        """
        Update neighbor list and model input.
        """

        # inquire information from conf
        cell = np.asarray(self.conf.cell, dtype=np.double)
        PBC = np.asarray(self.conf.PBC, dtype=np.intc)
        contributing_coords = np.asarray(self.conf.coords, dtype=np.double)
        contributing_species = self.conf.species
        num_contributing = self.conf.get_num_atoms()
        self.num_contributing_particles = num_contributing

        # species support and code
        unique_species = list(set(contributing_species))
        species_map = dict()
        for s in unique_species:
            if s in self.supported_species:
                species_map[s] = self.supported_species[s]
            else:
                KIMModelError(f"species `{s}` not supported by model")

        contributing_species_code = np.array(
            [species_map[s] for s in contributing_species], dtype=np.intc)

        if any(PBC):  # need padding atoms

            try:
                (
                    padding_coords,
                    padding_species_code,
                    self.padding_image_of,
                ) = nl.create_paddings(
                    influence_distance,
                    cell,
                    PBC,
                    contributing_coords,
                    contributing_species_code,
                )
            except RuntimeError:
                raise kimpy.KimPyError(
                    "Calling `nl.create_paddings()` failed.")

            num_padding = padding_species_code.size
            self.num_particles = np.array([num_contributing + num_padding],
                                          dtype=np.intc)
            tmp = np.concatenate((contributing_coords, padding_coords))
            self.coords = np.asarray(tmp, dtype=np.double)
            tmp = np.concatenate(
                (contributing_species_code, padding_species_code))
            self.species_code = np.asarray(tmp, dtype=np.intc)
            self.particle_contributing = np.ones(self.num_particles[0],
                                                 dtype=np.intc)
            self.particle_contributing[num_contributing:] = 0

            # for now, create neigh for all atoms, including paddings
            need_neigh = np.ones(self.num_particles[0], dtype=np.intc)

        else:  # do not need padding atoms
            self.padding_image_of = np.array([])
            self.num_particles = np.array([num_contributing], dtype=np.intc)
            self.coords = np.array(contributing_coords, dtype=np.double)
            self.species_code = np.array(contributing_species_code,
                                         dtype=np.intc)
            self.particle_contributing = np.ones(num_contributing,
                                                 dtype=np.intc)
            need_neigh = self.particle_contributing

        try:
            self.neigh.build(
                self.coords,
                influence_distance,
                np.asarray([influence_distance], dtype=np.double),
                need_neigh,
            )
        except RuntimeError:
            raise kimpy.KimPyError("Calling `neigh.build()` failed.")
示例#4
0
def test_main():

    # create contributing atoms
    alat = 2.46
    d = 3.35
    cell, contrib_coords, contrib_species = create_graphite_unit_cell(alat, d)

    # create padding atoms
    cutoffs = np.array([d + 0.01, d + 0.02], dtype=np.double)
    influence_dist = cutoffs[1]
    pbc = np.array([1, 1, 1], dtype=np.intc)
    out = nl.create_paddings(influence_dist, cell, pbc,
                             contrib_coords, contrib_species)
    pad_coords, pad_species, pad_image, error = out
    check_error(error, 'nl.create_padding')

    assert pad_coords.shape == (96, 3)
    # print('pad_coords is of shape:', pad_coords.shape)

    coords = np.concatenate((contrib_coords, pad_coords))
    coords = np.asarray(coords, dtype=np.double)
    species = np.concatenate((contrib_species, pad_species))
    species = np.asarray(species, dtype=np.intc)
    fname = 'atoms.xyz'
    write_XYZ(fname, cell, species, coords)

    # flag to indicate wheter create neighbor list for an atom
    n_pad = pad_coords.shape[0]
    n_contrib = contrib_coords.shape[0]
    need_neigh = np.concatenate((np.ones(n_contrib), np.zeros(n_pad)))
    need_neigh = np.asarray(need_neigh, dtype=np.intc)

    # create neighbor list
    neigh = nl.initialize()
    error = nl.build(neigh, coords, influence_dist, cutoffs, need_neigh)
    check_error(error, 'nl.build')

    # build again (it will automatically empty previous neigh list)
    error = nl.build(neigh, coords, influence_dist, cutoffs, need_neigh)
    check_error(error, 'nl.build')

    # test get neigh function
    neigh_list_index = 0
    particle = 1
    num_neigh, neighbors, error = nl.get_neigh(
        neigh, cutoffs, neigh_list_index, particle)
    check_error(error, 'nl.get_neigh')
    assert num_neigh == 14
    # print('Atom 1 has {} neighbors:'.format(num_neigh), end=' ')
    # for i in neighbors:
    #  print(i, end=' ')

    neigh_list_index = 1
    particle = 4
    num_neigh, neighbors, error = nl.get_neigh(
        neigh, cutoffs, neigh_list_index, particle)
    check_error(error, 'nl.get_neigh')
    assert num_neigh == 0

    # expect error message from this
    # neigh_list_index = 1
    # particle = n_contrib + n_pad
    # num_neigh, neighbors, error = nl.get_neigh(neigh, cutoffs, neigh_list_index, particle)
    # assert error == 1

    # delete neighbor list
    nl.clean(neigh)

    # remove the created file
    try:
        os.remove(fname)
        os.remove('kim.log')
    except:
        pass
示例#5
0
def test_main():

    # create contributing atoms
    alat = 2.46
    d = 3.35

    cell, contrib_coords, contrib_species = create_graphite_unit_cell(alat, d)

    # create padding atoms
    cutoffs = np.array([d + 0.01, d + 0.02], dtype=np.double)

    influence_dist = cutoffs[1]

    pbc = np.array([1, 1, 1], dtype=np.intc)

    try:
        pad_coords, pad_species, _ = nl.create_paddings(
            influence_dist, cell, pbc, contrib_coords, contrib_species)
    except RuntimeError:
        msg = 'Calling "neighlist.create_paddings" failed.'
        raise KimPyError(msg)

    assert pad_coords.shape == (96, 3)

    coords = np.concatenate((contrib_coords, pad_coords))
    coords = np.asarray(coords, dtype=np.double)

    species = np.concatenate((contrib_species, pad_species))
    species = np.asarray(species, dtype=np.intc)

    fname = 'atoms.xyz'
    write_XYZ(fname, cell, species, coords)

    # flag to indicate wheter create neighbor list for an atom
    n_pad = pad_coords.shape[0]
    n_contrib = contrib_coords.shape[0]

    need_neigh = np.concatenate((np.ones(n_contrib), np.zeros(n_pad)))
    need_neigh = np.asarray(need_neigh, dtype=np.intc)

    # create neighbor list
    neigh = nl.create()

    try:
        neigh.build(coords, influence_dist, cutoffs, need_neigh)
    except RuntimeError:
        msg = 'Calling "neighlist.build" failed.'
        raise KimPyError(msg)

    # build again (it will automatically empty previous neigh list)
    try:
        neigh.build(coords, influence_dist, cutoffs, need_neigh)
    except RuntimeError:
        msg = 'Calling "neighlist.build" failed.'
        raise KimPyError(msg)

    # test get neigh function
    neigh_list_index = 0
    particle = 1

    try:
        num_neigh, _ = neigh.get_neigh(cutoffs, neigh_list_index, particle)
    except RuntimeError:
        msg = 'KIM error. Calling "neighlist.get_neigh" failed.'
        raise KimPyError(msg)

    assert num_neigh == 14

    neigh_list_index = 1
    particle = 4

    try:
        num_neigh, _ = neigh.get_neigh(cutoffs, neigh_list_index, particle)
    except RuntimeError:
        msg = 'KIM error. Calling "neighlist.get_neigh" failed.'
        raise KimPyError(msg)

    assert num_neigh == 0

    # remove the created file
    try:
        os.remove(fname)
        os.remove('kim.log')
    except:
        pass