File: bulk.py

package info (click to toggle)
python-ase 3.24.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 15,448 kB
  • sloc: python: 144,945; xml: 2,728; makefile: 113; javascript: 47
file content (372 lines) | stat: -rw-r--r-- 13,356 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
"""Build crystalline systems"""
from math import sqrt
from typing import Any

from ase.atoms import Atoms
from ase.data import atomic_numbers, chemical_symbols, reference_states
from ase.symbols import string2symbols
from ase.utils import plural


def incompatible_cell(*, want, have):
    return RuntimeError(f'Cannot create {want} cell for {have} structure')


def bulk(
    name: str,
    crystalstructure: str = None,
    a: float = None,
    b: float = None,
    c: float = None,
    *,
    alpha: float = None,
    covera: float = None,
    u: float = None,
    orthorhombic: bool = False,
    cubic: bool = False,
    basis=None,
) -> Atoms:
    """Creating bulk systems.

    Crystal structure and lattice constant(s) will be guessed if not
    provided.

    name: str
        Chemical symbol or symbols as in 'MgO' or 'NaCl'.
    crystalstructure: str
        Must be one of sc, fcc, bcc, tetragonal, bct, hcp, rhombohedral,
        orthorhombic, mcl, diamond, zincblende, rocksalt, cesiumchloride,
        fluorite or wurtzite.
    a: float
        Lattice constant.
    b: float
        Lattice constant.  If only a and b is given, b will be interpreted
        as c instead.
    c: float
        Lattice constant.
    alpha: float
        Angle in degrees for rhombohedral lattice.
    covera: float
        c/a ratio used for hcp.  Default is ideal ratio: sqrt(8/3).
    u: float
        Internal coordinate for Wurtzite structure.
    orthorhombic: bool
        Construct orthorhombic unit cell instead of primitive cell
        which is the default.
    cubic: bool
        Construct cubic unit cell if possible.
    """

    if c is None and b is not None:
        # If user passes (a, b) positionally, we want it as (a, c) instead:
        c, b = b, c

    if covera is not None and c is not None:
        raise ValueError("Don't specify both c and c/a!")

    xref = ''
    ref: Any = {}

    if name in chemical_symbols:  # single element
        atomic_number = atomic_numbers[name]
        ref = reference_states[atomic_number]
        if ref is None:
            ref = {}  # easier to 'get' things from empty dictionary than None
        else:
            xref = ref['symmetry']

        if crystalstructure is None:
            # `ref` requires `basis` but not given and not pre-defined
            if basis is None and 'basis' in ref and ref['basis'] is None:
                raise ValueError('This structure requires an atomic basis')
            if xref == 'cubic':
                # P and Mn are listed as 'cubic' but the lattice constants
                # are 7 and 9.  They must be something other than simple cubic
                # then. We used to just return the cubic one but that must
                # have been wrong somehow.  --askhl
                raise ValueError(
                    f'The reference structure of {name} is not implemented')

    # Mapping of name to number of atoms in primitive cell.
    structures = {'sc': 1, 'fcc': 1, 'bcc': 1,
                  'tetragonal': 1,
                  'bct': 1,
                  'hcp': 1,
                  'rhombohedral': 1,
                  'orthorhombic': 1,
                  'mcl': 1,
                  'diamond': 1,
                  'zincblende': 2, 'rocksalt': 2, 'cesiumchloride': 2,
                  'fluorite': 3, 'wurtzite': 2}

    if crystalstructure is None:
        crystalstructure = xref
        if crystalstructure not in structures:
            raise ValueError(f'No suitable reference data for bulk {name}.'
                             f'  Reference data: {ref}')

    magmom_per_atom = None
    if crystalstructure == xref:
        magmom_per_atom = ref.get('magmom_per_atom')

    if crystalstructure not in structures:
        raise ValueError(f'Unknown structure: {crystalstructure}.')

    # Check name:
    natoms = len(string2symbols(name))
    natoms0 = structures[crystalstructure]
    if natoms != natoms0:
        raise ValueError('Please specify {} for {} and not {}'
                         .format(plural(natoms0, 'atom'),
                                 crystalstructure, natoms))

    if alpha is None:
        alpha = ref.get('alpha')

    if a is None:
        if xref != crystalstructure:
            raise ValueError('You need to specify the lattice constant.')
        if 'a' in ref:
            a = ref['a']
        else:
            raise KeyError(f'No reference lattice parameter "a" for "{name}"')

    if b is None:
        bovera = ref.get('b/a')
        if bovera is not None and a is not None:
            b = bovera * a

    if crystalstructure in ['hcp', 'wurtzite']:
        if c is not None:
            covera = c / a
        elif covera is None:
            if xref == crystalstructure:
                covera = ref['c/a']
            else:
                covera = sqrt(8 / 3)

    if covera is None:
        covera = ref.get('c/a')
        if c is None and covera is not None:
            c = covera * a

    if crystalstructure == 'bct':
        from ase.lattice import BCT
        if basis is None:
            basis = ref.get('basis')
        if basis is not None:
            natoms = len(basis)
        lat = BCT(a=a, c=c)
        atoms = Atoms([name] * natoms, cell=lat.tocell(), pbc=True,
                      scaled_positions=basis)
    elif crystalstructure == 'rhombohedral':
        atoms = _build_rhl(name, a, alpha, basis)
    elif crystalstructure == 'orthorhombic':
        atoms = Atoms(name, cell=[a, b, c], pbc=True)
    elif orthorhombic:
        atoms = _orthorhombic_bulk(name, crystalstructure, a, covera, u)
    elif cubic:
        atoms = _cubic_bulk(name, crystalstructure, a)
    else:
        atoms = _primitive_bulk(name, crystalstructure, a, covera, u)

    if magmom_per_atom is not None:
        magmoms = [magmom_per_atom] * len(atoms)
        atoms.set_initial_magnetic_moments(magmoms)

    if cubic or orthorhombic:
        assert atoms.cell.orthorhombic

    return atoms


def _build_rhl(name, a, alpha, basis):
    from ase.lattice import RHL
    lat = RHL(a, alpha)
    cell = lat.tocell()
    if basis is None:
        # RHL: Given by A&M as scaled coordinates "x" of cell.sum(0):
        basis_x = reference_states[atomic_numbers[name]]['basis_x']
        basis = basis_x[:, None].repeat(3, axis=1)
    natoms = len(basis)
    return Atoms([name] * natoms, cell=cell, scaled_positions=basis, pbc=True)


def _orthorhombic_bulk(name, crystalstructure, a, covera=None, u=None):
    if crystalstructure in ('sc', 'bcc', 'cesiumchloride'):
        atoms = _cubic_bulk(name, crystalstructure, a)
    elif crystalstructure == 'fcc':
        b = a / sqrt(2)
        cell = (b, b, a)
        scaled_positions = ((0.0, 0.0, 0.0), (0.5, 0.5, 0.5))
        atoms = Atoms(2 * name, cell=cell, scaled_positions=scaled_positions)
    elif crystalstructure == 'hcp':
        cell = (a, a * sqrt(3), covera * a)
        scaled_positions = [
            (0.0, 0 / 6, 0.0),
            (0.5, 3 / 6, 0.0),
            (0.5, 1 / 6, 0.5),
            (0.0, 4 / 6, 0.5),
        ]
        atoms = Atoms(4 * name, cell=cell, scaled_positions=scaled_positions)
    elif crystalstructure == 'diamond':
        b = a / sqrt(2)
        cell = (b, b, a)
        scaled_positions = [
            (0.0, 0.0, 0.0), (0.5, 0.0, 0.25),
            (0.5, 0.5, 0.5), (0.0, 0.5, 0.75),
        ]
        atoms = Atoms(4 * name, cell=cell, scaled_positions=scaled_positions)
    elif crystalstructure == 'rocksalt':
        b = a / sqrt(2)
        cell = (b, b, a)
        scaled_positions = [
            (0.0, 0.0, 0.0), (0.5, 0.5, 0.0),
            (0.5, 0.5, 0.5), (0.0, 0.0, 0.5),
        ]
        atoms = Atoms(2 * name, cell=cell, scaled_positions=scaled_positions)
    elif crystalstructure == 'zincblende':
        symbol0, symbol1 = string2symbols(name)
        atoms = _orthorhombic_bulk(symbol0, 'diamond', a)
        atoms.symbols[[1, 3]] = symbol1
    elif crystalstructure == 'wurtzite':
        cell = (a, a * sqrt(3), covera * a)
        u = u or 0.25 + 1 / 3 / covera**2
        scaled_positions = [
            (0.0, 0 / 6, 0.0), (0.0, 2 / 6, 0.5 - u),
            (0.0, 2 / 6, 0.5), (0.0, 0 / 6, 1.0 - u),
            (0.5, 3 / 6, 0.0), (0.5, 5 / 6, 0.5 - u),
            (0.5, 5 / 6, 0.5), (0.5, 3 / 6, 1.0 - u),
        ]
        atoms = Atoms(4 * name, cell=cell, scaled_positions=scaled_positions)
    else:
        raise incompatible_cell(want='orthorhombic', have=crystalstructure)

    atoms.pbc = True

    return atoms


def _cubic_bulk(name: str, crystalstructure: str, a: float) -> Atoms:
    cell = (a, a, a)
    if crystalstructure == 'sc':
        atoms = Atoms(name, cell=cell)
    elif crystalstructure == 'fcc':
        scaled_positions = [
            (0.0, 0.0, 0.0),
            (0.0, 0.5, 0.5),
            (0.5, 0.0, 0.5),
            (0.5, 0.5, 0.0),
        ]
        atoms = Atoms(4 * name, cell=cell, scaled_positions=scaled_positions)
    elif crystalstructure == 'bcc':
        scaled_positions = [
            (0.0, 0.0, 0.0),
            (0.5, 0.5, 0.5),
        ]
        atoms = Atoms(2 * name, cell=cell, scaled_positions=scaled_positions)
    elif crystalstructure == 'diamond':
        scaled_positions = [
            (0.0, 0.0, 0.0), (0.25, 0.25, 0.25),
            (0.0, 0.5, 0.5), (0.25, 0.75, 0.75),
            (0.5, 0.0, 0.5), (0.75, 0.25, 0.75),
            (0.5, 0.5, 0.0), (0.75, 0.75, 0.25),
        ]
        atoms = Atoms(8 * name, cell=cell, scaled_positions=scaled_positions)
    elif crystalstructure == 'cesiumchloride':
        symbol0, symbol1 = string2symbols(name)
        atoms = _cubic_bulk(symbol0, 'bcc', a)
        atoms.symbols[[1]] = symbol1
    elif crystalstructure == 'zincblende':
        symbol0, symbol1 = string2symbols(name)
        atoms = _cubic_bulk(symbol0, 'diamond', a)
        atoms.symbols[[1, 3, 5, 7]] = symbol1
    elif crystalstructure == 'rocksalt':
        scaled_positions = [
            (0.0, 0.0, 0.0), (0.5, 0.0, 0.0),
            (0.0, 0.5, 0.5), (0.5, 0.5, 0.5),
            (0.5, 0.0, 0.5), (0.0, 0.0, 0.5),
            (0.5, 0.5, 0.0), (0.0, 0.5, 0.0),
        ]
        atoms = Atoms(4 * name, cell=cell, scaled_positions=scaled_positions)
    elif crystalstructure == 'fluorite':
        scaled_positions = [
            (0.00, 0.00, 0.00), (0.25, 0.25, 0.25), (0.75, 0.75, 0.75),
            (0.00, 0.50, 0.50), (0.25, 0.75, 0.75), (0.75, 0.25, 0.25),
            (0.50, 0.00, 0.50), (0.75, 0.25, 0.75), (0.25, 0.75, 0.25),
            (0.50, 0.50, 0.00), (0.75, 0.75, 0.25), (0.25, 0.25, 0.75),
        ]
        atoms = Atoms(4 * name, cell=cell, scaled_positions=scaled_positions)
    else:
        raise incompatible_cell(want='cubic', have=crystalstructure)

    atoms.pbc = True

    return atoms


def _primitive_bulk(name, crystalstructure, a, covera=None, u=None):
    if crystalstructure == 'sc':
        atoms = Atoms(name, cell=(a, a, a))
    elif crystalstructure == 'fcc':
        b = 0.5 * a
        cell = ((0, b, b), (b, 0, b), (b, b, 0))
        atoms = Atoms(name, cell=cell)
    elif crystalstructure == 'bcc':
        b = 0.5 * a
        cell = ((-b, b, b), (b, -b, b), (b, b, -b))
        atoms = Atoms(name, cell=cell)
    elif crystalstructure == 'hcp':
        c = covera * a
        cell = ((a, 0, 0), (-0.5 * a, 0.5 * sqrt(3) * a, 0), (0, 0, c))
        scaled_positions = [
            (0 / 3, 0 / 3, 0.0),
            (1 / 3, 2 / 3, 0.5),
        ]
        atoms = Atoms(2 * name, cell=cell, scaled_positions=scaled_positions)
    elif crystalstructure == 'diamond':
        atoms = \
            _primitive_bulk(name, 'fcc', a) + \
            _primitive_bulk(name, 'fcc', a)
        atoms.positions[1, :] += 0.25 * a
    elif crystalstructure == 'rocksalt':
        symbol0, symbol1 = string2symbols(name)
        atoms = \
            _primitive_bulk(symbol0, 'fcc', a) + \
            _primitive_bulk(symbol1, 'fcc', a)
        atoms.positions[1, 0] += 0.5 * a
    elif crystalstructure == 'cesiumchloride':
        symbol0, symbol1 = string2symbols(name)
        atoms = \
            _primitive_bulk(symbol0, 'sc', a) + \
            _primitive_bulk(symbol1, 'sc', a)
        atoms.positions[1, :] += 0.5 * a
    elif crystalstructure == 'zincblende':
        symbol0, symbol1 = string2symbols(name)
        atoms = \
            _primitive_bulk(symbol0, 'fcc', a) + \
            _primitive_bulk(symbol1, 'fcc', a)
        atoms.positions[1, :] += 0.25 * a
    elif crystalstructure == 'fluorite':
        symbol0, symbol1, symbol2 = string2symbols(name)
        atoms = \
            _primitive_bulk(symbol0, 'fcc', a) + \
            _primitive_bulk(symbol1, 'fcc', a) + \
            _primitive_bulk(symbol2, 'fcc', a)
        atoms.positions[1, :] += 0.25 * a
        atoms.positions[2, :] += 0.75 * a
    elif crystalstructure == 'wurtzite':
        c = covera * a
        cell = ((a, 0, 0), (-0.5 * a, 0.5 * sqrt(3) * a, 0), (0, 0, c))
        u = u or 0.25 + 1 / 3 / covera**2
        scaled_positions = [
            (0 / 3, 0 / 3, 0.0), (1 / 3, 2 / 3, 0.5 - u),
            (1 / 3, 2 / 3, 0.5), (0 / 3, 0 / 3, 1.0 - u),
        ]
        atoms = Atoms(2 * name, cell=cell, scaled_positions=scaled_positions)
    else:
        raise incompatible_cell(want='primitive', have=crystalstructure)

    atoms.pbc = True

    return atoms