import numpy as np
from abc import ABC, abstractmethod
from ase.data import covalent_radii
from ase.geometry import get_distances
from ase import Atoms
from ase.visualize import view
from utils import check_valid_bondlengths
class CandidateGenerator(ABC):
"""Baseclass for mutation and crossover operations as well
as the startgenerator.
Parameters:
blmin: The minimum allowed distance between atoms in units of
the covalent distance between atoms, where d_cov=r_cov_i+r_cov_j.
blmax: The maximum allowed distance, in units of the covalent
distance, from a single isolated atom to the closest atom. If
blmax=None, no constraint is enforced on isolated atoms.
force_all_bonds_valid: If True all bondlengths are forced to
be valid according to blmin/blmax. If False, only bondlengths
of atoms specified in bondlength checks during operations are
tested. The specified atoms are typically the ones changed
during operations. Default is False, as True might cause
problems with GOFEE, as GPR-relaxations and dual-steps might
result in structures that does not obey blmin/blmax.
"""
def __init__(self, blmin=0.7, blmax=1.4, force_all_bonds_valid=False):
self.blmin = blmin
self.blmax = blmax
self.force_all_bonds_valid = force_all_bonds_valid
self.description = 'Unspecified'
def check_valid_bondlengths(self, a, indices=None, check_too_close=True, check_isolated=True):
if self.force_all_bonds_valid:
# Check all bonds (mainly for testing)
return check_valid_bondlengths(a, self.blmin, self.blmax+0.1,
check_too_close=check_too_close,
check_isolated=check_isolated)
else:
# Check only specified ones
# (typically only for the atoms changed during operation)
return check_valid_bondlengths(a, self.blmin, self.blmax+0.1, indices=indices,
check_too_close=check_too_close,
check_isolated=check_isolated)
@abstractmethod
def get_new_candidate(self):
pass
def finalize(self, a, a0=None, successfull=True):
if successfull:
description = self.description
else:
description = 'failed ' + self.description
try:
a.info['key_value_pairs']['origin'] = description
except:
a.info['key_value_pairs'] = {'origin': description}
if self.force_all_bonds_valid:
# Check all bonds
valid_bondlengths = self.check_valid_bondlengths(a)
assert valid_bondlengths, 'bondlengths are not valid'
return a
class OperationSelector():
"""Class to produce new candidates by applying one of the
candidate generation operations which is supplied in the
'operations'-list. The operations are drawn randomly according
to the 'probabilities'-list.
operations : "list" or "list of lists" of mutations/crossovers.
probabilities : probability for each of the mutations/crossovers
in operations. Must have the same dimensions as operations.
"""
def __init__(self, probabilities, operations):
cond1 = isinstance(operations[0], list)
cond2 = isinstance(probabilities[0], list)
if not cond1 and not cond2:
operations = [operations]
probabilities = [probabilities]
element_count_operations = [len(op_list) for op_list in operations]
element_count_probabilities = [len(prob_list)
for prob_list in probabilities]
assert element_count_operations == element_count_probabilities, 'the two lists must have the same shape'
self.operations = operations
self.rho = [np.cumsum(prob_list) for prob_list in probabilities]
def __get_index__(self, rho):
"""Draw from the cumulative probalility distribution, rho,
to return the index of which operation to use"""
v = np.random.random() * rho[-1]
for i in range(len(rho)):
if rho[i] > v:
return i
def get_new_candidate(self, parents):
"""Generate new candidate by applying a randomly drawn
operation on the structures. This is done successively for
each list of operations, if multiple are present.
"""
for op_list, rho_list in zip(self.operations, self.rho):
to_use = self.__get_index__(rho_list)
anew = op_list[to_use].get_new_candidate(parents)
parents[0] = anew
return anew
def random_pos(box):
""" Returns a random position within the box
described by the input box. """
p0 = box[0].astype(float)
vspan = box[1]
r = np.random.random((1, len(vspan)))
pos = p0.copy()
for i in range(len(vspan)):
pos += vspan[i] * r[0, i]
return pos
class StartGenerator(CandidateGenerator):
""" Class used to generate random initial candidates.
The candidates are generated by iteratively adding in
one atom at a time within the box described.
Parameters:
slab: The atoms object describing the super cell to
optimize within. Can be an empty cell or a cell
containing the atoms of a slab.
stoichiometry: A list of atomic numbers for the atoms
that are placed on top of the slab (if one is present).
box_to_place_in: The box within which atoms are placed. The box
should be on the form [p0, vspan] where 'p0' is the position of
the box corner and 'vspan' is a matrix containing the three
spanning vectors.
blmin: The minimum allowed distance between atoms in units of
the covalent distance between atoms, where d_cov=r_cov_i+r_cov_j.
blmax: The maximum allowed distance, in units of the covalent
distance, from a single isolated atom to the closest atom. If
blmax=None, no constraint is enforced on isolated atoms.
cluster: If True atoms are required to be placed within
blmin*d_cov of one of the other atoms to be placed. If
False the atoms in the slab are also included.
"""
def __init__(self, slab, stoichiometry, box_to_place_in,
blmin=0.7, blmax=1.4, cluster=False, description='StartGenerator'):
CandidateGenerator.__init__(self, blmin=blmin, blmax=blmax)
self.slab = slab
self.stoichiometry = stoichiometry
self.box = box_to_place_in
self.cluster = cluster
self.description = description
def get_new_candidate(self, parents=None):
a = self.make_structure()
a = self.finalize(a)
return a
def make_structure(self):
""" Generates a new random structure """
Nslab = len(self.slab)
Ntop = len(self.stoichiometry)
num = np.random.permutation(self.stoichiometry)
for i_trials in range(1000):
a = self.slab.copy()
for i in range(Ntop):
pos_found = False
for _ in range(300):
# Place new atom
posi = random_pos(self.box)
a += Atoms([num[i]], posi.reshape(1,3))
# Check if position of new atom is valid
not_too_close = self.check_valid_bondlengths(a, indices=[Nslab+i],
check_too_close=True,
check_isolated=False)
if len(a) == 1: # The first atom
not_isolated = True
else:
if self.cluster: # Check isolation excluding slab atoms.
not_isolated = self.check_valid_bondlengths(a[Nslab:], indices=[Nslab+i],
check_too_close=False,
check_isolated=True)
else: # All atoms.
not_isolated = self.check_valid_bondlengths(a, indices=[Nslab+i],
check_too_close=False,
check_isolated=True)
valid_bondlengths = not_too_close and not_isolated
if not valid_bondlengths:
del a[-1]
else:
pos_found = True
break
if not pos_found:
break
if pos_found:
break
if i_trials == 999 and not pos_found:
raise RuntimeError('StartGenerator: No valid structure was produced in 1000 trials.')
else:
return a
if __name__ == '__main__':
from ase.io import read
from ase.visualize import view
from candidate_operations.basic_mutations import RattleMutation, RattleMutation2, PermutationMutation
print(0.7*2*covalent_radii[1], 1.3*2*covalent_radii[1])
np.random.seed(7)
#a = read('/home/mkb/DFT/gpLEA/Si3x3/ref/gm_unrelaxed_done.traj', index='0')
#a = read('si3x3.traj', index='0')
#a = read('c6h6.traj', index='0')
traj = read('c6h6_init.traj', index=':')
#a = read('sn2o3.traj', index='0')
#slab = read('slab_sn2o3.traj', index='0')
"""
stoichiometry = 6*[50] + 10*[8]
c = slab.get_cell()
c[2,2] = 3.3
p0 = np.array([0,0,14])
box = [p0, c]
sg = StartGenerator(slab, stoichiometry, box)
"""
a = traj[0]
rattle = RattleMutation(n_top=len(a), Nrattle=3, rattle_range=2)
rattle2 = RattleMutation2(n_top=16, Nrattle=0.1)
permut = PermutationMutation(n_top=16, Npermute=2)
candidategenerator = OperationSelector([1], [rattle])
#candidategenerator = CandidateGenerator([0., 1., 0.], [rattle, rattle2, permut])
#candidategenerator = CandidateGenerator([[1],[1]], [[rattle2], [permut]])
"""
for a in traj:
vb = rattle.check_valid_bondlengths(a)
print(vb)
"""
traj_rattle = []
for i in range(100):
for j, a in enumerate(traj[13:14]):
print('i =', i, 'j =', j)
a0 = a.copy()
anew = candidategenerator.get_new_candidate([a0,a0])
traj_rattle += [a0, anew]
view(traj_rattle)
"""
a_mut = rattle.get_new_candidate([a])
view([a,a_mut])
"""