Example #1
0
    def add_random(self, random_probability=0.3):
        """
        Add one random structure to the population
        """
        entry_id = None
        structure = Structure()
        if self.composition is None:
            raise ValueError('No composition associated to this population')
        factor = np.random.randint(self.min_comp_mult, self.max_comp_mult + 1)
        comp = self.composition.composition.copy()
        #print("Initial composition: %s" % comp)
        #print(Composition(comp))
        #print(Composition(comp).symbols)
        for i in comp:
            comp[i] *= factor
        new_comp = Composition(comp)

        while True:
            rnd = random.random()
            condition = {
                'structure.nspecies': new_comp.nspecies,
                'structure.natom': new_comp.natom
            }
            if self.pcdb_source is None:
                rnd = 0
            elif len(self.sources[factor]) == 0:
                rnd = 0
            if self.pcdb_source is None or rnd < random_probability:
                pcm_log.debug('Random Structure')
                structure = Structure.random_cell(new_comp,
                                                  method='stretching',
                                                  stabilization_number=5,
                                                  nparal=5,
                                                  periodic=True)
                break
            else:
                pcm_log.debug('From source')
                entry_id = self.sources[factor][np.random.randint(
                    0, len(self.sources[factor]))]
                structure = self.pcdb_source.get_structure(entry_id)
                print("chosen structure from database =", structure)
                sym = CrystalSymmetry(structure)

                scale_factor = float(
                    np.max(covalent_radius(new_comp.species)) /
                    np.max(covalent_radius(structure.species)))
                reduce_scale = scale_factor**(1. / 3)  # WIH
                msg = 'Mult: %d natom: %d From source: %s Spacegroup: %d Scaling: %7.3f'
                print(msg % (factor, structure.natom, structure.formula,
                             sym.number(), scale_factor))
                # structure.set_cell(np.dot(scale_factor * np.eye(3), structure.cell)) # WIH
                structure.set_cell(
                    np.dot(reduce_scale * np.eye(3), structure.cell))  # WIH
                print("symbols before change = ", structure.symbols)
                structure.symbols = new_comp.symbols
                print("symbols after change = ", structure.symbols)
                self.sources[factor].remove(entry_id)
                break

        return self.new_entry(structure), entry_id
Example #2
0
    def add_random(self, random_probability=0.3):
        """
        Add one random structure to the population
        """
        structure = Structure()
        if self.composition is None:
            raise ValueError('No composition associated to this population')
        comp = self.composition.composition.copy()
        rnd = random.random()
        natom_limit = self.max_comp_mult * self.composition.natom / self.composition.gcd
        condition = {
            'structure.nspecies': self.composition.nspecies,
            'structure.natom': {
                '$lte': natom_limit
            }
        }

        if self.pcdb_source is None or self.pcdb_source.entries.find(
                condition).count() <= len(self.source_blacklist):
            rnd = 0
        origin = None

        if self.pcdb_source is None or rnd < random_probability or self.composition.nspecies > 1:
            pcm_log.debug('Random Structure')
            factor = np.random.randint(self.min_comp_mult,
                                       self.max_comp_mult + 1)
            for i in comp:
                comp[i] *= factor
            structure = Structure.random_cell(comp,
                                              method='stretching',
                                              stabilization_number=5,
                                              nparal=5,
                                              periodic=True)
        else:
            pcm_log.debug('From source')
            while True:

                entry = None
                condition['properties.spacegroup'] = random.randint(1, 230)
                print('Trying', condition['properties.spacegroup'])
                for ientry in self.pcdb_source.entries.find(condition):
                    if ientry['_id'] not in self.source_blacklist:
                        entry = ientry
                        break
                if entry is not None:
                    origin = entry['_id']
                    structure = self.pcdb_source.get_structure(entry['_id'])
                    factor = covalent_radius(
                        self.composition.species[0]) / covalent_radius(
                            structure.species[0])
                    print('From source: %s Spacegroup: %d Scaling: %7.3f' %
                          (structure.formula,
                           entry['properties']['spacegroup'], factor))
                    structure.set_cell(
                        np.dot(factor * np.eye(3), structure.cell))
                    structure.symbols = structure.natom * self.composition.species
                    self.source_blacklist.append(entry['_id'])
                    break

        return self.new_entry(structure), origin
    def add_random(self, random_probability=0.3):
        """
        Add one random structure to the population
        """
        entry_id = None
        structure = Structure()
        if self.composition is None:
            raise ValueError('No composition associated to this population')
        factor = np.random.randint(self.min_comp_mult, self.max_comp_mult + 1)
        comp = self.composition.composition.copy()
        # print("Initial composition: %s" % comp)
        # print(Composition(comp))
        # print(Composition(comp).symbols)
        for i in comp:
            comp[i] *= factor
        new_comp = Composition(comp)

        while True:
            rnd = random.random()
            condition = {'structure.nspecies': new_comp.nspecies,
                         'structure.natom': new_comp.natom}
            if self.pcdb_source is None:
                rnd = 0
            elif len(self.sources[factor]) == 0:
                rnd = 0
            if self.pcdb_source is None or rnd < random_probability:
                pcm_log.debug('Random Structure')
                structure = Structure.random_cell(new_comp, method='stretching', stabilization_number=5, nparal=5,
                                                  periodic=True)
                break
            else:
                pcm_log.debug('From source')
                entry_id = self.sources[factor][np.random.randint(0, len(self.sources[factor]))]
                structure = self.pcdb_source.get_structure(entry_id)
                print("chosen structure from database =", structure)
                sym = CrystalSymmetry(structure)

                scale_factor = float(np.max(covalent_radius(new_comp.species)) /
                                     np.max(covalent_radius(structure.species)))
                reduce_scale = scale_factor ** (1. / 3)    # WIH
                msg = 'Mult: %d natom: %d From source: %s Spacegroup: %d Scaling: %7.3f'
                print(msg % (factor, structure.natom, structure.formula, sym.number(), scale_factor))
                # structure.set_cell(np.dot(scale_factor * np.eye(3), structure.cell)) # WIH
                structure.set_cell(np.dot(reduce_scale * np.eye(3), structure.cell))  # WIH
                print("symbols before change = ", structure.symbols)
                structure.symbols = new_comp.symbols
                print("symbols after change = ", structure.symbols)
                self.sources[factor].remove(entry_id)
                break

        return self.new_entry(structure), entry_id
Example #4
0
    def add_random(self, random_probability=0.3):
        """
        Add one random structure to the population
        """
        structure = Structure()
        if self.composition is None:
            raise ValueError('No composition associated to this population')
        comp = self.composition.composition.copy()
        rnd = random.random()
        natom_limit = self.max_comp_mult * self.composition.natom / self.composition.gcd
        condition = {'structure.nspecies': self.composition.nspecies,
                     'structure.natom': {'$lte': natom_limit}}

        if self.pcdb_source is None or self.pcdb_source.entries.find(condition).count() <= len(self.source_blacklist):
            rnd = 0
        origin = None

        if self.pcdb_source is None or rnd < random_probability or self.composition.nspecies > 1:
            pcm_log.debug('Random Structure')
            factor = np.random.randint(self.min_comp_mult, self.max_comp_mult + 1)
            for i in comp:
                comp[i] *= factor
            structure = Structure.random_cell(comp, method='stretching', stabilization_number=5, nparal=5,
                                              periodic=True)
        else:
            pcm_log.debug('From source')
            while True:

                entry = None
                condition['properties.spacegroup'] = random.randint(1, 230)
                print('Trying', condition['properties.spacegroup'])
                for ientry in self.pcdb_source.entries.find(condition):
                    if ientry['_id'] not in self.source_blacklist:
                        entry = ientry
                        break
                if entry is not None:
                    origin = entry['_id']
                    structure = self.pcdb_source.get_structure(entry['_id'])
                    factor = covalent_radius(self.composition.species[0]) / covalent_radius(structure.species[0])
                    print('From source: %s Spacegroup: %d Scaling: %7.3f' % (structure.formula,
                                                                             entry['properties']['spacegroup'],
                                                                             factor))
                    structure.set_cell(np.dot(factor * np.eye(3), structure.cell))
                    structure.symbols = structure.natom * self.composition.species
                    self.source_blacklist.append(entry['_id'])
                    break

        return self.new_entry(structure), origin