File: soft_mutation.py

package info (click to toggle)
python-ase 3.22.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,344 kB
  • sloc: python: 126,379; xml: 946; makefile: 111; javascript: 47
file content (414 lines) | stat: -rw-r--r-- 14,324 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
"""Soft-mutation operator and associated tools"""
import inspect
import json
import numpy as np
from ase.data import covalent_radii
from ase.neighborlist import NeighborList
from ase.ga.offspring_creator import OffspringCreator
from ase.ga.utilities import atoms_too_close, gather_atoms_by_tag
from scipy.spatial.distance import cdist


class TagFilter:
    """Filter which constrains same-tag atoms to behave
    like internally rigid moieties.
    """
    def __init__(self, atoms):
        self.atoms = atoms
        gather_atoms_by_tag(self.atoms)
        self.tags = self.atoms.get_tags()
        self.unique_tags = np.unique(self.tags)
        self.n = len(self.unique_tags)

    def get_positions(self):
        all_pos = self.atoms.get_positions()
        cop_pos = np.zeros((self.n, 3))
        for i in range(self.n):
            indices = np.where(self.tags == self.unique_tags[i])
            cop_pos[i] = np.average(all_pos[indices], axis=0)
        return cop_pos

    def set_positions(self, positions, **kwargs):
        cop_pos = self.get_positions()
        all_pos = self.atoms.get_positions()
        assert np.all(np.shape(positions) == np.shape(cop_pos))
        for i in range(self.n):
            indices = np.where(self.tags == self.unique_tags[i])
            shift = positions[i] - cop_pos[i]
            all_pos[indices] += shift
        self.atoms.set_positions(all_pos, **kwargs)

    def get_forces(self, *args, **kwargs):
        f = self.atoms.get_forces()
        forces = np.zeros((self.n, 3))
        for i in range(self.n):
            indices = np.where(self.tags == self.unique_tags[i])
            forces[i] = np.sum(f[indices], axis=0)
        return forces

    def get_masses(self):
        m = self.atoms.get_masses()
        masses = np.zeros(self.n)
        for i in range(self.n):
            indices = np.where(self.tags == self.unique_tags[i])
            masses[i] = np.sum(m[indices])
        return masses

    def __len__(self):
        return self.n


class PairwiseHarmonicPotential:
    """Parent class for interatomic potentials of the type
    E(r_ij) = 0.5 * k_ij * (r_ij - r0_ij) ** 2
    """
    def __init__(self, atoms, rcut=10.):
        self.atoms = atoms
        self.pos0 = atoms.get_positions()
        self.rcut = rcut

        # build neighborlist
        nat = len(self.atoms)
        self.nl = NeighborList([self.rcut / 2.] * nat, skin=0., bothways=True,
                               self_interaction=False)
        self.nl.update(self.atoms)

        self.calculate_force_constants()

    def calculate_force_constants(self):
        msg = 'Child class needs to define a calculate_force_constants() ' \
              'method which computes the force constants and stores them ' \
              'in self.force_constants (as a list which contains, for every ' \
              'atom, a list of the atom\'s force constants with its neighbors.'
        raise NotImplementedError(msg)

    def get_forces(self, atoms):
        pos = atoms.get_positions()
        cell = atoms.get_cell()
        forces = np.zeros_like(pos)

        for i, p in enumerate(pos):
            indices, offsets = self.nl.get_neighbors(i)
            p = pos[indices] + np.dot(offsets, cell)
            r = cdist(p, [pos[i]])
            v = (p - pos[i]) / r
            p0 = self.pos0[indices] + np.dot(offsets, cell)
            r0 = cdist(p0, [self.pos0[i]])
            dr = r - r0
            forces[i] = np.dot(self.force_constants[i].T, dr * v)

        return forces


def get_number_of_valence_electrons(Z):
    """Return the number of valence electrons for the element with
    atomic number Z, simply based on its periodic table group.
    """
    groups = [[], [1, 3, 11, 19, 37, 55, 87], [2, 4, 12, 20, 38, 56, 88],
              [21, 39, 57, 89]]

    for i in range(9):
        groups.append(i + np.array([22, 40, 72, 104]))

    for i in range(6):
        groups.append(i + np.array([5, 13, 31, 49, 81, 113]))

    for i, group in enumerate(groups):
        if Z in group:
            nval = i if i < 13 else i - 10
            break
    else:
        raise ValueError('Z=%d not included in this dataset.' % Z)

    return nval


class BondElectroNegativityModel(PairwiseHarmonicPotential):
    """Pairwise harmonic potential where the force constants are
    determined using the "bond electronegativity" model, see:

    * `Lyakhov, Oganov, Valle, Comp. Phys. Comm. 181 (2010) 1623-1632`__

      __ https://dx.doi.org/10.1016/j.cpc.2010.06.007

    * `Lyakhov, Oganov, Phys. Rev. B 84 (2011) 092103`__

      __ https://dx.doi.org/10.1103/PhysRevB.84.092103
    """
    def calculate_force_constants(self):
        cell = self.atoms.get_cell()
        pos = self.atoms.get_positions()
        num = self.atoms.get_atomic_numbers()
        nat = len(self.atoms)
        nl = self.nl

        # computing the force constants
        s_norms = []
        valence_states = []
        r_cov = []
        for i in range(nat):
            indices, offsets = nl.get_neighbors(i)
            p = pos[indices] + np.dot(offsets, cell)
            r = cdist(p, [pos[i]])
            r_ci = covalent_radii[num[i]]
            s = 0.
            for j, index in enumerate(indices):
                d = r[j] - r_ci - covalent_radii[num[index]]
                s += np.exp(-d / 0.37)
            s_norms.append(s)
            valence_states.append(get_number_of_valence_electrons(num[i]))
            r_cov.append(r_ci)

        self.force_constants = []
        for i in range(nat):
            indices, offsets = nl.get_neighbors(i)
            p = pos[indices] + np.dot(offsets, cell)
            r = cdist(p, [pos[i]])[:, 0]
            fc = []
            for j, ii in enumerate(indices):
                d = r[j] - r_cov[i] - r_cov[ii]
                chi_ik = 0.481 * valence_states[i] / (r_cov[i] + 0.5 * d)
                chi_jk = 0.481 * valence_states[ii] / (r_cov[ii] + 0.5 * d)
                cn_ik = s_norms[i] / np.exp(-d / 0.37)
                cn_jk = s_norms[ii] / np.exp(-d / 0.37)
                fc.append(np.sqrt(chi_ik * chi_jk / (cn_ik * cn_jk)))
            self.force_constants.append(np.array(fc))


class SoftMutation(OffspringCreator):
    """Mutates the structure by displacing it along the lowest
    (nonzero) frequency modes found by vibrational analysis, as in:

    `Lyakhov, Oganov, Valle, Comp. Phys. Comm. 181 (2010) 1623-1632`__

    __ https://dx.doi.org/10.1016/j.cpc.2010.06.007

    As in the reference above, the next-lowest mode is used if the
    structure has already been softmutated along the current-lowest
    mode. This mutation hence acts in a deterministic way, in contrast
    to most other genetic operators.

    If you find this implementation useful in your work,
    please consider citing:

    `Van den Bossche, Gronbeck, Hammer, J. Chem. Theory Comput. 14 (2018)`__

    __ https://dx.doi.org/10.1021/acs.jctc.8b00039

    in addition to the paper mentioned above.

    Parameters:

    blmin: dict
        The closest allowed interatomic distances on the form:
        {(Z, Z*): dist, ...}, where Z and Z* are atomic numbers.

    bounds: list
        Lower and upper limits (in Angstrom) for the largest
        atomic displacement in the structure. For a given mode,
        the algorithm starts at zero amplitude and increases
        it until either blmin is violated or the largest
        displacement exceeds the provided upper bound).
        If the largest displacement in the resulting structure
        is lower than the provided lower bound, the mutant is
        considered too similar to the parent and None is
        returned.

    calculator: ASE calculator object
        The calculator to be used in the vibrational
        analysis. The default is to use a calculator
        based on pairwise harmonic potentials with force
        constants from the "bond electronegativity"
        model described in the reference above.
        Any calculator with a working :func:`get_forces()`
        method will work.

    rcut: float
        Cutoff radius in Angstrom for the pairwise harmonic
        potentials.

    used_modes_file: str or None
        Name of json dump file where previously used
        modes will be stored (and read). If None,
        no such file will be used. Default is to use
        the filename 'used_modes.json'.

    use_tags: boolean
        Whether to use the atomic tags to preserve molecular identity.
    """
    def __init__(self, blmin, bounds=[0.5, 2.0],
                 calculator=BondElectroNegativityModel, rcut=10.,
                 used_modes_file='used_modes.json', use_tags=False,
                 verbose=False):
        OffspringCreator.__init__(self, verbose)
        self.blmin = blmin
        self.bounds = bounds
        self.calc = calculator
        self.rcut = rcut
        self.used_modes_file = used_modes_file
        self.use_tags = use_tags
        self.descriptor = 'SoftMutation'

        self.used_modes = {}
        if self.used_modes_file is not None:
            try:
                self.read_used_modes(self.used_modes_file)
            except IOError:
                # file doesn't exist (yet)
                pass

    def _get_hessian(self, atoms, dx):
        """Returns the Hessian matrix d2E/dxi/dxj using a first-order
        central difference scheme with displacements dx.
        """
        N = len(atoms)
        pos = atoms.get_positions()
        hessian = np.zeros((3 * N, 3 * N))

        for i in range(3 * N):
            row = np.zeros(3 * N)
            for direction in [-1, 1]:
                disp = np.zeros(3)
                disp[i % 3] = direction * dx
                pos_disp = np.copy(pos)
                pos_disp[i // 3] += disp
                atoms.set_positions(pos_disp)
                f = atoms.get_forces()
                row += -1 * direction * f.flatten()

            row /= (2. * dx)
            hessian[i] = row

        hessian += np.copy(hessian).T
        hessian *= 0.5
        atoms.set_positions(pos)

        return hessian

    def _calculate_normal_modes(self, atoms, dx=0.02, massweighing=False):
        """Performs the vibrational analysis."""
        hessian = self._get_hessian(atoms, dx)
        if massweighing:
            m = np.array([np.repeat(atoms.get_masses()**-0.5, 3)])
            hessian *= (m * m.T)

        eigvals, eigvecs = np.linalg.eigh(hessian)
        modes = {eigval: eigvecs[:, i] for i, eigval in enumerate(eigvals)}
        return modes

    def animate_mode(self, atoms, mode, nim=30, amplitude=1.0):
        """Returns an Atoms object showing an animation of the mode."""
        pos = atoms.get_positions()
        mode = mode.reshape(np.shape(pos))
        animation = []
        for i in range(nim):
            newpos = pos + amplitude * mode * np.sin(i * 2 * np.pi / nim)
            image = atoms.copy()
            image.positions = newpos
            animation.append(image)
        return animation

    def read_used_modes(self, filename):
        """Read used modes from json file."""
        with open(filename, 'r') as fd:
            modes = json.load(fd)
            self.used_modes = {int(k): modes[k] for k in modes}
        return

    def write_used_modes(self, filename):
        """Dump used modes to json file."""
        with open(filename, 'w') as fd:
            json.dump(self.used_modes, fd)
        return

    def get_new_individual(self, parents):
        f = parents[0]

        indi = self.mutate(f)
        if indi is None:
            return indi, 'mutation: soft'

        indi = self.initialize_individual(f, indi)
        indi.info['data']['parents'] = [f.info['confid']]

        return self.finalize_individual(indi), 'mutation: soft'

    def mutate(self, atoms):
        """Does the actual mutation."""
        a = atoms.copy()

        if inspect.isclass(self.calc):
            assert issubclass(self.calc, PairwiseHarmonicPotential)
            calc = self.calc(atoms, rcut=self.rcut)
        else:
            calc = self.calc
        a.calc = calc

        if self.use_tags:
            a = TagFilter(a)

        pos = a.get_positions()
        modes = self._calculate_normal_modes(a)

        # Select the mode along which we want to move the atoms;
        # The first 3 translational modes as well as previously
        # applied modes are discarded.

        keys = np.array(sorted(modes))
        index = 3
        confid = atoms.info['confid']
        if confid in self.used_modes:
            while index in self.used_modes[confid]:
                index += 1
            self.used_modes[confid].append(index)
        else:
            self.used_modes[confid] = [index]

        if self.used_modes_file is not None:
            self.write_used_modes(self.used_modes_file)

        key = keys[index]
        mode = modes[key].reshape(np.shape(pos))

        # Find a suitable amplitude for translation along the mode;
        # at every trial amplitude both positive and negative
        # directions are tried.

        mutant = atoms.copy()
        amplitude = 0.
        increment = 0.1
        direction = 1
        largest_norm = np.max(np.apply_along_axis(np.linalg.norm, 1, mode))

        def expand(atoms, positions):
            if isinstance(atoms, TagFilter):
                a.set_positions(positions)
                return a.atoms.get_positions()
            else:
                return positions

        while amplitude * largest_norm < self.bounds[1]:
            pos_new = pos + direction * amplitude * mode
            pos_new = expand(a, pos_new)
            mutant.set_positions(pos_new)
            mutant.wrap()
            too_close = atoms_too_close(mutant, self.blmin,
                                        use_tags=self.use_tags)
            if too_close:
                amplitude -= increment
                pos_new = pos + direction * amplitude * mode
                pos_new = expand(a, pos_new)
                mutant.set_positions(pos_new)
                mutant.wrap()
                break

            if direction == 1:
                direction = -1
            else:
                direction = 1
                amplitude += increment

        if amplitude * largest_norm < self.bounds[0]:
            mutant = None

        return mutant