Ejemplo n.º 1
0
    def testCheckTypes(self):
        """ Test that the types checking works. """
        # This is a valid types list.
        types = ['A','a', """A""", "ABC"]
        size = 4
        checked_types = checkTypes(types, size)
        self.assertEqual(checked_types, types)

        # Wrong size.
        size = 3
        self.assertRaises(Error, lambda: checkTypes(types,size))

        # Mixed types.
        types = ['A','a', """A""", 2]
        self.assertRaises(Error, lambda: checkTypes(types,size))

        # Not a list.
        types = "ABCDEfgH"
        self.assertRaises(Error, lambda: checkTypes(types,8))
Ejemplo n.º 2
0
    def testCheckTypes(self):
        """ Test that the types checking works. """
        # This is a valid types list.
        types = ['A', 'a', """A""", "ABC"]
        size = 4
        checked_types = checkTypes(types, size)
        self.assertEqual(checked_types, types)

        # Wrong size.
        size = 3
        self.assertRaises(Error, lambda: checkTypes(types, size))

        # Mixed types.
        types = ['A', 'a', """A""", 2]
        self.assertRaises(Error, lambda: checkTypes(types, size))

        # Not a list.
        types = "ABCDEfgH"
        self.assertRaises(Error, lambda: checkTypes(types, 8))
Ejemplo n.º 3
0
    def __checkAndSetShortTypes(self, types, default_type, possible_types):
        """ """
        """
        Private helper to check the types input for the 'short' format.
        """
        # Check the default types.
        if default_type is not None:
            raise Error("A default type can only be used in combination with the long types format\nwhen constructing a KMCConfiguration object.")

        # Check the types.
        return checkTypes(types,self.__n_lattice_sites)
Ejemplo n.º 4
0
    def __init__(self,
                 coordinates=None,
                 types=None,
                 center=None):
        """
        Constructor for the KMCLocalConfiguration.

        :param coordinates: The coordinates of the configuration given as
                            a 3xN sequence of floating point numbers, where
                            N is the number of local sites.

        :param types: The lattice site types at the specified coordinates given
                      as a sequence of strings of length N.

        :param center: The coordinate in the list to treat as the central site
                       indexed from zero. If not given it will default to the
                       first coordinate (i.e. center == 0).
        :type center:  int
        """
        # Check the coordinates.
        coordinates = checkCoordinateList(coordinates)

        # Set the center coordinate if not given.
        if center is None:
            center = 0

        # Check the bounds of the center coordinate.
        center = checkIndexWithinBounds(center,
                                        coordinates,
                                        msg="The 'center' index paramter must be one in the coordinate list.")

        # Center the coordinates.
        self.__coordinates = centerCoordinates(coordinates, center)

        # Check the tyeps.
        self.__types = checkTypes(types, len(coordinates))

        # Set the backend to None, to generate it at first query.
        self.__backend = None
Ejemplo n.º 5
0
    def __init__(self,
                 coordinates=None,
                 elements_before=None,
                 elements_after=None,
                 move_vectors=None,
                 basis_sites=None,
                 rate_constant=None):
        """
        Constructor for the KMCProcess.

        :param coordinates: The local coordinates, corresponding to the lattice
                            positions of the surrounding of the center where
                            the process will be preformed.

        :param elements_before: The elements, as a list of strings,
                                before the process is preformed.
                                This list of elements will be used to match the
                                local surroundings of each center in the
                                simulation, to determine at which sites the
                                process can be performed.

        :param elements_after: The elements, as a list of strings,
                               after the process is preformed.
                               This list of elements will be used to update the
                               local surrounding of the site where the process
                               is performed.

        :param move_vectors: A set of vectors in the local coordinates that define
                             which elements are moved to which place in the move.
                             The vectors are given as a list of tuples, where the first
                             element in each tuple indicates which index is moved and
                             the second element in the tuple is a vector in local coordinates
                             indicating where the move is to.

        :param basis_sites: The basis sites in the lattice at which the process
                            can possibly be applied. Only if the length of this
                            list is 1 can implicit wildcards be used for the
                            matching.

        :param rate_constant: The rate constant associated with this process.
        :type rate_constant: float

        """
        # Check the coordinates.
        coordinates = checkCoordinateList(coordinates)

        # Center the coordinates on the first entry.
        center = 0
        self.__coordinates = centerCoordinates(coordinates, center)

        # Check the types.
        elements_before = checkTypes(elements_before, len(coordinates))
        elements_after  = checkTypes(elements_after,  len(coordinates))

        # Check that the elements represents a valid move.
        self.__checkValidMoveElements(elements_before, elements_after)

        # All types checking done.
        self.__elements_before = elements_before
        self.__elements_after  = elements_after

        # Check that the move vectors are compatible with the elements.
        self.__move_vectors = self.__checkValidMoveVectors(move_vectors)

        # Sort the coordinates and co-sort the elements and move vectors.
        self.__sortCoordinatesElementsAndMoveVectors()

        # Check the list of basis sites.
        basis_sites = checkSequenceOfPositiveIntegers(basis_sites,
                                                      msg="The basis_sites must be given as a list of positive integers.")

        if len(basis_sites) == 0:
            msg = "The list of available basis sites for a process may not be empty."

        # Passed the  tests.
        self.__basis_sites = basis_sites

        # Check the rate constant.
        self.__rate_constant = checkPositiveFloat(rate_constant,
                                                  default_parameter=None,
                                                  parameter_name="rate_constant")
        # Setup the local configurations.
        c1 = KMCLocalConfiguration(self.__coordinates, self.__elements_before, center)
        c2 = KMCLocalConfiguration(self.__coordinates, self.__elements_after,  center)
        self.__local_configurations = (c1, c2)
Ejemplo n.º 6
0
    def __checkAndSetTypes(self, types, default_type, possible_types):
        """ """
        """
        Private helper routine to check and set the types input.
        """
        # Check that the types is a list.
        if not isinstance(types, list):
            raise Error(
                "The 'types' given to the KMCConfiguration constructor must be \na list of type strings, e.g. ['a','b','c'] or tuples e.g. [(0,0,1,0,'a'), (0,0,2,0,'b'), ...]."
            )

        # Check the first element to get the assumed format.
        use_long_format = not isinstance(types[0], str)

        # Set the use of default type flag.
        use_default_type = (default_type is not None)

        # We can not have both a short format and default type.
        if use_default_type and not use_long_format:
            raise Error(
                "A default type can only be used in combination with the long types format\nwhen constructing a KMCConfiguration object."
            )

        # If we use the long format, check that each entry is of the form (int,int,int,int,string)
        if use_long_format:

            # We must have a default type for the long format.
            if not use_default_type:
                raise Error(
                    "A default type must be specified when using the long types format."
                )

            # Check and distribute the default type.
            if not isinstance(default_type, str):
                raise Error(
                    "The default type given to the KMCConfiguration constructor must be a string."
                )
            types_to_set = [default_type] * self.__n_lattice_sites

            # Check each element in the list.
            for t in types:
                # Check that it is ia tuple.
                if not isinstance(t, tuple) or len(t) != 5:
                    raise Error(
                        "All elements in the types list must be of type (int,int,int,int,string) when\nusing the long type format."
                    )

                # Check that the elements in the tuple have the correct type.
                if not all([
                        isinstance(tt, (int, int, int, int, str)[i])
                        for i, tt in enumerate(t)
                ]):
                    raise Error(
                        "All elements in the types list must be of type (int,int,int,int,string) when\nusing the long type format."
                    )

                # Check the bounds of the given indices.
                (nI, nJ, nK) = self.__lattice.repetitions()
                nB = len(self.__lattice.basis())

                if t[0] < 0 or t[0] >= nI:
                    raise Error(
                        "The first index in the type tuple must be within the limits of the repetitions in the 'a' direction, indexed from 0."
                    )
                if t[1] < 0 or t[1] >= nJ:
                    raise Error(
                        "The second index in the type tuple must be within the limits of the repetitions in the 'b' direction, indexed from 0."
                    )
                if t[2] < 0 or t[2] >= nK:
                    raise Error(
                        "The third index in the type tuple must be within the limits of the repetitions in the 'c' direction, indexed from 0."
                    )
                if t[3] < 0 or t[3] >= nB:
                    raise Error(
                        "The fourth index in the type tuple must be withing the limits of the basis points in the original unit cell, indexed from 0."
                    )

                # Set the type.
                index = self.__lattice._globalIndex(t[0], t[1], t[2], t[3])
                types_to_set[index] = t[4]

        else:
            # Otherwise, check the types the normal way.
            types_to_set = checkTypes(types, self.__n_lattice_sites)

        # Setup the list of possible types.
        all_types_present = list(set(types_to_set))
        if possible_types is None:
            possible_types = all_types_present
        else:
            # Check that the possible types is a list of strings.
            if not isinstance(possible_types, list) or not all(
                [isinstance(pt, str) for pt in possible_types]):
                raise Error(
                    "The possible types must be given as a list of strings.")

            # Check that each present type is in the list of possible types.
            possible_set = set(possible_types)
            present_set = set(all_types_present)
            if not present_set.issubset(possible_set):
                raise Error(
                    "There are types specified which are not in the given list of possible types."
                )

        # Every thing is checked - store the data on the class.
        self.__types = types_to_set

        # Check that the wildcard has not made it into the possible types allready.
        if "*" in possible_types:
            raise Error("The wildcard caracter '*' is not a valid type.")

        # Setup the possible types as a dict.
        possible_types = ["*"] + possible_types
        self.__possible_types = dict(
            zip(possible_types, range(len(possible_types))))
Ejemplo n.º 7
0
    def __checkAndSetTypes(self, types, default_type, possible_types):
        """ """
        """
        Private helper routine to check and set the types input.
        """
        # Check that the types is a list.
        if not isinstance(types, list):
            raise Error("The 'types' given to the KMCConfiguration constructor must be \na list of type strings, e.g. ['a','b','c'] or tuples e.g. [(0,0,1,0,'a'), (0,0,2,0,'b'), ...].")

        # Check the first element to get the assumed format.
        use_long_format = not isinstance(types[0],str)

        # Set the use of default type flag.
        use_default_type = (default_type is not None)

        # We can not have both a short format and default type.
        if use_default_type and not use_long_format:
            raise Error("A default type can only be used in combination with the long types format\nwhen constructing a KMCConfiguration object.")

        # If we use the long format, check that each entry is of the form (int,int,int,int,string)
        if use_long_format:

            # We must have a default type for the long format.
            if not use_default_type:
                raise Error("A default type must be specified when using the long types format.")

            # Check and distribute the default type.
            if not isinstance(default_type, str):
                raise Error("The default type given to the KMCConfiguration constructor must be a string.")
            types_to_set = [default_type]*self.__n_lattice_sites

            # Check each element in the list.
            for t in types:
                # Check that it is ia tuple.
                if not isinstance(t,tuple) or len(t) != 5:
                    raise Error("All elements in the types list must be of type (int,int,int,int,string) when\nusing the long type format.")

                # Check that the elements in the tuple have the correct type.
                if not all([isinstance(tt,(int,int,int,int,str)[i]) for i,tt in enumerate(t)]):
                    raise Error("All elements in the types list must be of type (int,int,int,int,string) when\nusing the long type format.")

                # Check the bounds of the given indices.
                (nI, nJ, nK) = self.__lattice.repetitions()
                nB = len(self.__lattice.basis())

                if t[0] < 0 or t[0] >= nI:
                    raise Error("The first index in the type tuple must be within the limits of the repetitions in the 'a' direction, indexed from 0.")
                if t[1] < 0 or t[1] >= nJ:
                    raise Error("The second index in the type tuple must be within the limits of the repetitions in the 'b' direction, indexed from 0.")
                if t[2] < 0 or t[2] >= nK:
                    raise Error("The third index in the type tuple must be within the limits of the repetitions in the 'c' direction, indexed from 0.")
                if t[3] < 0 or t[3] >= nB:
                    raise Error("The fourth index in the type tuple must be withing the limits of the basis points in the original unit cell, indexed from 0.")

                # Set the type.
                index = self.__lattice._globalIndex(t[0],t[1],t[2],t[3])
                types_to_set[index] = t[4]

        else:
            # Otherwise, check the types the normal way.
            types_to_set = checkTypes(types,self.__n_lattice_sites)

        # Setup the list of possible types.
        all_types_present = list(set(types_to_set))
        if possible_types is None:
            possible_types = all_types_present
        else:
            # Check that the possible types is a list of strings.
            if not isinstance(possible_types, list) or not all([isinstance(pt,str) for pt in possible_types]):
                raise Error("The possible types must be given as a list of strings.")

            # Check that each present type is in the list of possible types.
            possible_set = set(possible_types)
            present_set = set(all_types_present)
            if not present_set.issubset(possible_set):
                raise Error("There are types specified which are not in the given list of possible types.")

        # Every thing is checked - store the data on the class.
        self.__types = types_to_set

        # Check that the wildcard has not made it into the possible types allready.
        if "*" in possible_types:
            raise Error("The wildcard caracter '*' is not a valid type.")

        # Setup the possible types as a dict.
        possible_types = ["*"] + possible_types
        self.__possible_types = dict(zip(possible_types, range(len(possible_types))))