candidate_generation.py 15.3 KB
Newer Older
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
1
2
3
4
import numpy as np
from abc import ABC, abstractmethod
from ase.data import covalent_radii
from ase.geometry import get_distances
5
from ase import Atoms
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
6
7
from ase.visualize import view

8
9
10
from utils import check_valid_bondlengths, get_min_distances_as_fraction_of_covalent

import warnings
11

12
class OffspringOperation(ABC):
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    """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.
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
32
    """
33
34
    def __init__(self, blmin=0.7, blmax=1.4, constraints=None,
                 force_all_bonds_valid=False, *args, **kwargs):
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
35
36
        self.blmin = blmin
        self.blmax = blmax
37
        self.constraints = constraints
38
        self.force_all_bonds_valid = force_all_bonds_valid
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
39
40
        self.description = 'Unspecified'

41
    def check_bondlengths(self, a, indices=None,
42
43
44
45
                                check_too_close=True, check_isolated=True):
        """ Method to check if bondlengths are valid according to blmin
        amd blmax.
        """
46
47
48
49
50
51
52
53
        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)
54
55
            return check_valid_bondlengths(a, self.blmin, self.blmax+0.1,
                                           indices=indices,
56
57
                                           check_too_close=check_too_close,
                                           check_isolated=check_isolated)
58
59
60
61
62
63
64
65

    def get_new_candidate(self, parents=None):
        """Standardized candidate generation method for all mutation
        and crossover operations.
        """
        # Check bondlengths
        if parents is not None:
            for i, parent in enumerate(parents):
66
                self.check_all_bondlengths(parent, f'SHORT BONDS IN PARENT {i}')
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

        for _ in range(5): # Make five tries
            a = self.operation(parents)
            if a is not None:
                a = self.finalize(a)
                break
        else:
            return None
        return a

    def train(self):
        """ Method to be implemented for the operations that rely on
        a Machine-Learned model to perform more informed/guided 
        mutation and crossover operations.
        """
        pass

Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
84
    @abstractmethod
85
    def operation(self):
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
86
87
        pass

88
    def finalize(self, a, a0=None, successfull=True):
89
90
91
92
93
94
        """ Method to finalize new candidates.
        """
        # Wrap positions
        a.wrap()

        # finalize description
95
96
        if successfull:
            description = self.description
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
97
        else:
98
            description = 'failed ' + self.description
99
100

        # Save description 
101
102
103
104
        try:
            a.info['key_value_pairs']['origin'] = description
        except:
            a.info['key_value_pairs'] = {'origin': description}
105
        
106
        self.check_all_bondlengths(a, 'SHORT BONDS AFTER OPPERATION')
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
107
108
        return a

109
    def check_all_bondlengths(self, a, warn_text):
110
111
            if self.force_all_bonds_valid:
                # Check all bonds
112
                valid_bondlengths = self.check_bondlengths(a)
113
114
115
116
117
118
119
120
                assert valid_bondlengths, 'bondlengths are not valid'
            else:
                d_shortest_bond, index_shortest_bond = get_min_distances_as_fraction_of_covalent(a)
                if d_shortest_bond < self.blmin:
                    text = f"""{warn_text}:
                               Atom {index_shortest_bond} has bond with d={d_shortest_bond}d_covalent"""
                    warnings.warn(text)

121
122
123
    def set_constraints(self, constraints):
        self.constraints = constraints

124
125
126
127
128
129
130
    def check_constraints(self, indices=None):
        if self.constraints is not None:
            valid = self.constraints.check_if_valid(indices)
            return valid
        else:
            return True

131
class CandidateGenerator():
132
133
134
135
    """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.
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
136
    
137
138
139
140
141
142
143
144
145
146
    operations : list or list of lists
        Defines the operations to generate new candidates in GOFEE.
        of mutations/crossovers. Either a list of mutations, e.g. the
        RattleMutation, or alternatively a list of lists of such mutations,
        in which case consecutive operations, one drawn from each list,
        are performed. 

    probabilities : list or list of lists
        probability for each of the mutations/crossovers
        in operations. Must have the same dimensions as operations.
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
147
148
149
150
151
152
153
154
155
156
    """
    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]
157
        assert element_count_operations == element_count_probabilities, 'the two lists must have the same shape'
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
        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):
175
176
177
178
179
180
181
182
183
            for i_trial in range(5): # Do five trials
                to_use = self.__get_index__(rho_list)
                anew = op_list[to_use].get_new_candidate(parents)
                if anew is not None:
                    parents[0] = anew
                    break
            else:
                anew = parents[0]
                anew = op_list[to_use].finalize(anew, successfull=False)
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
184
185
        return anew

186
187
188
189
190
191
    def set_constraints(self, constraints):
        for op_list in self.operations:
            for op in op_list:
                op.set_constraints(constraints)
        self.constraints = constraints

192
193
194
195
    def train(self, data):
        """ Method to train all trainable operations in
        self.operations.
        """
196
197
        for op_list in self.operations:
            for operation in op_list:
198
199
                operation.train(data)

200
201
202
203
204
205
206
207
208
209
210
211

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

212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
class OperationConstraint():
    """ Class used to enforce constraints on the positions of
    atoms in mutation and crossover operations.

    Parameters:

    box: Box in which atoms are allowed to be placed. It should
    have the form [] [p0, vspan] where 'p0' is the position of
    the box corner and 'vspan' is a matrix containing the three
    spanning vectors.

    xlim: On the form [xmin, xmax], specifying, in the x-direction, 
    the lower and upper limit of the region atoms can be moved 
    within.

    ylim, zlim: See xlim.
    """
    def __init__(self, box=None, xlim=None, ylim=None, zlim=None):
        self.box = box
        self.xlim = xlim
        self.ylim = ylim
        self.zlim = zlim

    def check_if_valid(self, positions):
        """ Returns whether positions are valid under the 
        constraints or not.
        """
239
240
241
242
        if np.ndim(positions) == 1:
            pos = positions.reshape(-1,3)
        else:
            pos = positions
243

244
        if self.box is not None:
245
246
247
248
249
250
            p0, V = self.box
            p_rel = pos - p0  # positions relative to box anchor.
            V_inv = np.linalg.inv(V)
            p_box = p_rel @ V_inv  # positions in box-vector basis.
            if (np.any(p_box < 0) or np.any(p_box > 1)):
                return False
251
252
253
        if self.xlim is not None:
            if (np.any(pos[:,0] < self.xlim[0]) or 
                    np.any(pos[:,0] > self.xlim[1])):
254
                return False
255
256
257
        if self.ylim is not None:
            if (np.any(pos[:,1] < self.ylim[0]) or 
                    np.any(pos[:,1] > self.ylim[1])):
258
                return False
259
260
261
        if self.zlim is not None:
            if (np.any(pos[:,2] < self.zlim[0]) or 
                    np.any(pos[:,2] > self.zlim[1])):
262
263
                return False

264
265
266
        return True


267
class StartGenerator(OffspringOperation):
268
    """ Class used to generate random initial candidates.
269
270
271

    Generates new candidates by iteratively adding
    one atom at a time within a user-defined box.
272
273
274

    Parameters:

275
276
277
278
    slab: Atoms object
        The atoms object describing the super cell to
        optimize within. Can be an empty cell or a cell 
        containing the atoms of a slab.
279

280
281
282
    stoichiometry: list
        A list of atomic numbers for the atoms
        that are placed on top of the slab (if one is present).
283

284
285
286
287
288
    box_to_place_in: list
        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.
289

290
291
292
    blmin: float
        The minimum allowed distance between atoms in units of
        the covalent distance between atoms, where d_cov=r_cov_i+r_cov_j.
293
    
294
295
296
297
298
299
300
301
302
    blmax: float
        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: bool
        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.
303
304
    """
    def __init__(self, slab, stoichiometry, box_to_place_in,
305
306
                 cluster=False, description='StartGenerator',
                 *args, **kwargs):
307
        OffspringOperation.__init__(self, *args, **kwargs)
308
309
310
311
312
313
        self.slab = slab
        self.stoichiometry = stoichiometry
        self.box = box_to_place_in
        self.cluster = cluster
        self.description = description

314
    def operation(self, parents=None):
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
        a = self.make_structure()
        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
334
                    not_too_close = self.check_bondlengths(a, indices=[Nslab+i],
335
336
337
338
339
340
                                                          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.
341
                            not_isolated = self.check_bondlengths(a[Nslab:], indices=[Nslab+i],
342
343
344
                                                                        check_too_close=False,
                                                                        check_isolated=True)
                        else:  # All atoms.
345
                            not_isolated = self.check_bondlengths(a, indices=[Nslab+i],
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
                                                                        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
                
    
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
364
365
366
367
368
if __name__ == '__main__':
    from ase.io import read
    from ase.visualize import view

    from candidate_operations.basic_mutations import RattleMutation, RattleMutation2, PermutationMutation
369
370

    print(0.7*2*covalent_radii[1], 1.3*2*covalent_radii[1])
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
371
    
372
    np.random.seed(7)
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
373
374
375
376
    
    #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')
377
378
379
380
381
382
383
384
385
386
    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]
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
387
    
388
389
390
391
392
    sg = StartGenerator(slab, stoichiometry, box)
    """

    a = traj[0]
    rattle = RattleMutation(n_top=len(a), Nrattle=3, rattle_range=2)
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
393
394
395
    rattle2 = RattleMutation2(n_top=16, Nrattle=0.1)
    permut = PermutationMutation(n_top=16, Npermute=2)

396
397
    candidategenerator = OperationSelector([1], [rattle])
    #candidategenerator = CandidateGenerator([0., 1., 0.], [rattle, rattle2, permut])
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
398
399
    #candidategenerator = CandidateGenerator([[1],[1]], [[rattle2], [permut]])

400
401
    """
    for a in traj:
402
        vb = rattle.check_bondlengths(a)
403
404
405
406
        print(vb)
    """

    traj_rattle = []
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
407
    for i in range(100):
408
409
410
411
412
        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]
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
413

414
    view(traj_rattle)
Malthe Kjær Bisbo's avatar
update    
Malthe Kjær Bisbo committed
415
416
417
418
    """
    a_mut = rattle.get_new_candidate([a])
    view([a,a_mut])
    """