コード例 #1
0
    def add_element(self, element):
        """
        add a element to this composition.
        
        Arguments:
            element: element's object or symbol.
            
        Returns:
            True if add a element successfully. Conversely, False.
            
        添加一个元素到这个化学式中。
        
        参数:
            element:元素对象或元素符号。
            
        返回:
            布尔值(True/False)。
        """
        from utils.check import exist
        from cache.cachedElementProvider import CachedElementProvider

        if not exist(element, self.elements, 'element'):
            if isinstance(element, basestring):  # symbol
                symbol = element
                element = CachedElementProvider().get(symbol)
            self.elements.append(element)
            element.compositions.append(self)  # update composition
            return True
        else:
            return False
コード例 #2
0
    def add_prototype(self, prototype):
        """
        add a prototype to this composition.
        
        Arguments:
            prototype: prototype's object.
            
        Returns:
            True if add a prototype successfully. Conversely, False.
            
        添加一个结构原型到这个化学式中。
        
        参数:
            prototype:结构原型对象。
            
        返回:
            布尔值(True/False)。
        """
        from utils.check import exist

        if not exist(prototype, self.prototypes, 'prototype'):
            self.prototypes.append(prototype)
            prototype.composition = self
            return True
        else:
            return False
コード例 #3
0
    def add_structure(self, structure):
        """
        add a structure to this composition.
        
        Arguments:
            structure: structure's object.
            
        Returns:
            True if add a structure successfully. Conversely, False.
            
        添加一个结构到该化学式中。
        
        参数:
            structure:结构对象。
            
        返回:
            布尔值(True/False)。
        """
        from utils.check import exist

        if not exist(structure, self.structures, 'strucutre'):
            self.structures.append(structure)
            structure.composition = self
            return True
        else:
            return False
コード例 #4
0
ファイル: species.py プロジェクト: CaptainDasheng/MMP2_learn
    def add_atom(self, atom):
        """
        add a atom to this species.
        
        Arguments:
            atom: atom's object.
        
        Returns:
            True if add a atom successfully. Conversely, False.
            
        添加一个原子到元素种类中。
        
        参数:
            atom:原子对象。
            
        返回:
            布尔值(True/False)。
        """
        from utils.check import exist

        if not exist(atom, self.atoms, 'atom'):
            self.atoms.append(atom)
            atom.species = self
            return True
        else:
            return False
コード例 #5
0
    def add_structure(self, structure):
        """
        add a structure to this prototype.
        
        Arguments:
            structure: structure's object.
            
        Returns:
            True if add a atom successfully. Conversely, False.
            
        添加一个结构到结构原型对象的属性数组(structures)中。
        
        参数:
            structure:结构对象。
            
        返回:
            布尔值(True/False)。
        """
        from utils.check import exist

        if not exist(structure, self.structures, 'structure'):
            self.structures.append(structure)
            structure.prototype = self
            return True
        else:
            return False
コード例 #6
0
    def add_element(self, element):
        """
        add a element to this composition.
        
        Arguments:
            element: element's object.
            
        Returns:
            True if add a element successfully. Conversely, False.
            
        添加一个元素到结构对象的属性数组(elements)中。
        
        参数:
            element:元素对象。
        
        返回:
            布尔值(True/False)。
        """
        from utils.check import exist

        if not exist(element, self.elements, 'element'):
            self.elements.append(element)
            element.structures.append(self)
            return True
        else:
            return False
コード例 #7
0
ファイル: element.py プロジェクト: CaptainDasheng/MMP2_learn
    def add_species(self, species):
        """
        add a species to this element.
        
        Arguments:
            species: species's object or name.
        
        Returns:
            True if add a species successfully. Conversely, False.
            
        添加一个元素种类到这个元素中。
        
        参数:
            species:元素种类对象或元素名称。
            
        返回:
            布尔值(True/False)。
        """
        from utils.check import exist
        from cache.cachedSpeciesProvider import CachedSpeciesProvider

        if not exist(species, self.species, 'species'):
            if isinstance(species, basestring):  # name
                name = species
                species = CachedSpeciesProvider().get(name)
            self.species.append(species)
            species.element = self
            return True
        else:
            return False
コード例 #8
0
ファイル: element.py プロジェクト: CaptainDasheng/MMP2_learn
    def add_composition(self, composition):
        """
        add a composition to this element.
        
        Arguments:
            composition: composition's object or formula.
            
        Returns:
            True if add a composition successfully. Conversely, False.
            
        添加一个化学式到元素中。
        
        参数:
            composition:化学式对象或化学式字符串。
            
        返回:
            布尔值(True/False)。
        """
        from utils.check import exist
        from cache.cachedCompositionProvider import CachedCompositionProvider

        if not exist(composition, self.compositions, 'composition'):
            if isinstance(composition, basestring):  # formula
                formula = composition
                composition = CachedCompositionProvider().get(formula)
                if composition is None:
                    composition = CachedCompositionProvider().set(formula)
            self.compositions.append(composition)
            composition.elements.append(self)
            return True
        else:
            return False
コード例 #9
0
    def add_wyckoffSite(self, wyckoffSite):
        """
        add a wyckoffSite to this spacegroup.
        
        Arguments:
            wyckoffSite: wyckoffSite's object.
            
        Returns:
            True if add a operation successfully. Conversely, False.
            
        添加一个wyckoff位置到这个空间群中。
        
        参数:
            wyckoffSite:wyckoff位置对象。
            
        返回:
            布尔值(True/False)。
        """
        from utils.check import exist

        if not exist(wyckoffSite, self.wyckoffSites, 'wyckoffSite'):
            self.wyckoffSites.append(wyckoffSite)
            wyckoffSite.spacegroup = self
            return True
        else:
            return False
コード例 #10
0
    def add_operation(self, operation):
        """
        add operation to this spacegroup.
        
        Arguments:
            operation: operation's object.
            
        Returns:
            True if add a operation successfully. Conversely, False.
            
        添加一个操作到这个空间群中。
        
        参数:
            operation:操作对象。
            
        返回:
            布尔值(True/False)。
        """
        from utils.check import exist

        if not exist(operation, self.operations, 'operation'):
            self.operations.append(operation)
            operation.spacegroups.append(self)
            return True
        else:
            return False
コード例 #11
0
    def add_site(self, site):
        """
        add site to this wyckoffSite.
        
        Arguments:
            site: site's object.
            
        Returns:
            True if add a site successfully. Conversely, False.
        
        添加一个不等价位置到wyckoffSite中。
        
        参数:
            site:不等价位置对象。
            
        返回:
            布尔值(True/False)。
        """
        from utils.check import exist

        if not exist(site, self.sites, 'site'):
            self.sites.append(site)
            site.wyckoffSite = self
            return True
        else:
            return False
コード例 #12
0
    def add_spacegroup(self, spacegroup):
        """
        add a spacegroup to this operation.
        
        Arguments:
            spacegroup: spacegroup's object.
            
        Returns:
            True if add a operation successfully. Conversely, False.
            
        添加一个空间群到操作中。
        
        参数:
            spacegroup:空间群对象。
            
        返回:
            布尔值(True/False)。
        """
        from utils.check import exist

        if not exist(spacegroup, self.spacegroups, 'spacegroup'):
            self.spacegroups.append(spacegroup)
            spacegroup.operations.append(self)
            return True
        else:
            return False
コード例 #13
0
ファイル: fetch.py プロジェクト: CaptainDasheng/MMP2_learn
def get_index_from_collection(index_or_entity, collection, entity_type,
                              **kwargs):
    """
    get the index of entity from the collection.
        
    Arguments:
        index_or_entity: entity's index or object.
        collection: collection of entity.
        entity_type: type of entity.
        
        kwargs:
            for position type:
                lattice: lattice parameter of structure. i.e.
                    [[x1,y1,z1],
                     [x2,y2,z2],
                     [x3,y3,z3]]
                precision (default=1e-3): used to determine whether the two atoms are overlapped. Note that, 
                        to determine whether this atom is in collection by comparing its distance 
                        from other atoms.
    Return:
        index if exist. Conversely, False.
    """
    from utils.check import exist, check_formated_position_only_direct, check_formated_position_only_cartesian
    from utils.convert import cartesian2direct
    from materials.atom import Atom

    index = None
    if isinstance(index_or_entity, int):
        if index_or_entity < 0 or index_or_entity > len(collection):
            raise ValueError('index out of range')
        index = index_or_entity
    elif entity_type == 'atom' and isinstance(index_or_entity, Atom) and exist(
            index_or_entity, collection, entity_type):
        index = collection.index(index_or_entity)
    elif entity_type == 'position':

        precision = 1e-3
        if 'precision' in kwargs:
            precision = kwargs['precision']

        position = index_or_entity
        if check_formated_position_only_cartesian(position):
            if not 'lattice' is kwargs:
                raise ValueError('lattice must be given')
            lattice = kwargs['lattice']
            position = cartesian2direct(lattice, position)
        if check_formated_position_only_direct(position):
            collection = np.array(collection)
            for i in xrange(0, collection.shape[0]):
                distance = collection[i] - np.array(position)
                if np.linalg.norm(distance) <= precision:
                    if index is None:
                        index = i
                    else:
                        raise ValueError('exist reduplicative positon')

    return index
コード例 #14
0
    def add_atom(self, atom, isUpdatedInfo=False, isPersist=False):
        """
        add a atom to this structure.
        Arguments:
            atom: atom's object or formated string. you must specify the type. The valid formation: ['Na', 2.3, 1.4, 0.0, 'Cartesian']
            isUpdatedInfo (default=False): whether to update the composition. 
            isPersist (default=False): whether to save to the database.

        Returns:
            True if add a atom successfully. Conversely, False.
            
        添加一个原子到结构对象的属性数组(atoms)中。
        
        参数:
            atom:参数的值可以是原子的格式化数组或原子对象。注意:必须指定原子坐标类型(‘Cartesian’),其合法的格式为: ['Na', 5.234, 0.0, 0.0, 'Cartesian']。
            isUpdatedInfo (default=False): 是否更新结构的其他关联数据信息(如,化学式、不等价位置,等)。
            isPersist (default=False):是否持久化,即将结构保存到数据库中。
        
        返回:
            布尔值(True/False)。
        """
        import warnings
        from utils.check import exist, check_formated_atom_only_cartesian
        from cache.cachedMolCompositionProvider import CachedMolCompositionProvider
        from cache.cachedMolElementProvider import CachedMolElementProvider

        if not exist(atom, self.atoms, 'atom'):
            if isinstance(atom, MolAtom):
                #if not atom.element in self.elements:
                if self.get_element(atom.element.symbol) is None:
                    self.add_element(atom.element)
                    if isPersist:
                        self.element_set.add(atom.element)
                elif not atom.element in self.elements:
                    warnings.warn(
                        "element's object of given atom isn't in this structure, but have the same element's symbol"
                    )
                    return False
                self.atoms.append(atom)
                atom.structure = self
            elif check_formated_atom_only_cartesian(atom):
                formated_atom = atom
                if len(formated_atom) == 5 and formated_atom[-1].strip().lower(
                ).startswith('c'):  # Cartesian
                    formated_atom = formated_atom[:4]

                symbol_of_element = formated_atom[0]
                element = self.get_element(symbol_of_element)
                position = formated_atom[1:]

                if element is None:
                    element = CachedMolElementProvider().get(symbol_of_element)
                    atom = MolAtom().create(element,
                                            position,
                                            isPersist=False,
                                            structure=self)
                    self.ntypes = len(self.elements)
                else:
                    atom = MolAtom().create(element,
                                            position,
                                            isPersist=False,
                                            structure=self)
            # atom
            self.natoms = len(self.atoms)

        if isPersist:
            self.update(isPersist=isPersist)
        elif isUpdatedInfo and not isPersist:
            self.update(isPersist=False)

            return True
        else:
            return False