• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

materialsproject / pymatgen / 4075885785

pending completion
4075885785

push

github

Shyue Ping Ong
Merge branch 'master' of github.com:materialsproject/pymatgen

96 of 96 new or added lines in 27 files covered. (100.0%)

81013 of 102710 relevant lines covered (78.88%)

0.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.77
/pymatgen/io/atat.py
1
# Copyright (c) Pymatgen Development Team.
2
# Distributed under the terms of the MIT License.
3

4
"""
1✔
5
Classes for reading/writing mcsqs files following the rndstr.in format.
6
"""
7

8
from __future__ import annotations
1✔
9

10
import numpy as np
1✔
11

12
from pymatgen.core.lattice import Lattice
1✔
13
from pymatgen.core.periodic_table import get_el_sp
1✔
14
from pymatgen.core.structure import Structure
1✔
15

16
__author__ = "Matthew Horton"
1✔
17
__copyright__ = "Copyright 2017, The Materials Project"
1✔
18
__maintainer__ = "Matthew Horton"
1✔
19
__email__ = "mkhorton@lbl.gov"
1✔
20
__status__ = "Production"
1✔
21
__date__ = "October 2017"
1✔
22

23

24
class Mcsqs:
1✔
25
    """
26
    Handle input/output for the crystal definition format
27
    used by mcsqs and other ATAT codes.
28
    """
29

30
    def __init__(self, structure):
1✔
31
        """
32
        :param structure: input Structure
33
        """
34
        self.structure = structure
1✔
35

36
    def to_string(self):
1✔
37
        """
38
        Returns a structure in mcsqs rndstr.in format.
39
        :return (str):
40
        """
41
        # add lattice vectors
42
        m = self.structure.lattice.matrix
1✔
43
        output = [f"{l[0]:6f} {l[1]:6f} {l[2]:6f}" for l in m]
1✔
44

45
        # define coord system, use Cartesian
46
        output.append("1.0 0.0 0.0")
1✔
47
        output.append("0.0 1.0 0.0")
1✔
48
        output.append("0.0 0.0 1.0")
1✔
49

50
        # add species
51
        for site in self.structure:
1✔
52
            species_str = []
1✔
53
            for sp, occu in sorted(site.species.items()):
1✔
54
                sp = str(sp)
1✔
55
                if ("," in sp) or ("=" in sp):
1✔
56
                    # Invalid species string for AT-AT input, so modify
57
                    sp = sp.replace(",", "__").replace("=", "___")
×
58
                species_str.append(f"{sp}={occu}")
1✔
59
            species_str = ",".join(species_str)
1✔
60
            a, b, c = site.frac_coords
1✔
61
            output.append(f"{a:6f} {b:6f} {c:6f} {species_str}")
1✔
62

63
        return "\n".join(output)
1✔
64

65
    @staticmethod
1✔
66
    def structure_from_string(data):
1✔
67
        """
68
        Parses a rndstr.in, lat.in or bestsqs.out file into pymatgen's
69
        Structure format.
70

71
        :param data: contents of a rndstr.in, lat.in or bestsqs.out file
72
        :return: Structure object
73
        """
74
        data = data.splitlines()
1✔
75
        data = [x.split() for x in data if x]  # remove empty lines
1✔
76

77
        # following specification/terminology given in manual
78
        if len(data[0]) == 6:  # lattice parameters
1✔
79
            a, b, c, alpha, beta, gamma = map(float, data[0])
×
80
            coord_system = Lattice.from_parameters(a, b, c, alpha, beta, gamma).matrix
×
81
            lattice_vecs = np.array(
×
82
                [
83
                    [data[1][0], data[1][1], data[1][2]],
84
                    [data[2][0], data[2][1], data[2][2]],
85
                    [data[3][0], data[3][1], data[3][2]],
86
                ],
87
                dtype=float,
88
            )
89
            first_species_line = 4
×
90
        else:
91
            coord_system = np.array(
1✔
92
                [
93
                    [data[0][0], data[0][1], data[0][2]],
94
                    [data[1][0], data[1][1], data[1][2]],
95
                    [data[2][0], data[2][1], data[2][2]],
96
                ],
97
                dtype=float,
98
            )
99
            lattice_vecs = np.array(
1✔
100
                [
101
                    [data[3][0], data[3][1], data[3][2]],
102
                    [data[4][0], data[4][1], data[4][2]],
103
                    [data[5][0], data[5][1], data[5][2]],
104
                ],
105
                dtype=float,
106
            )
107
            first_species_line = 6
1✔
108

109
        scaled_matrix = np.matmul(lattice_vecs, coord_system)
1✔
110
        lattice = Lattice(scaled_matrix)
1✔
111

112
        all_coords = []
1✔
113
        all_species = []
1✔
114
        for l in data[first_species_line:]:
1✔
115
            coords = np.array([l[0], l[1], l[2]], dtype=float)
1✔
116
            scaled_coords = np.matmul(coords, np.linalg.inv(lattice_vecs))
1✔
117
            all_coords.append(scaled_coords)
1✔
118

119
            species_strs = "".join(l[3:])  # join multiple strings back together
1✔
120
            species_strs = species_strs.replace(" ", "")  # trim any white space
1✔
121
            species_strs = species_strs.split(",")  # comma-delimited
1✔
122

123
            species = {}
1✔
124

125
            for species_occ in species_strs:
1✔
126
                # gets a species, occupancy pair
127
                species_occ = species_occ.split("=")
1✔
128

129
                if len(species_occ) == 1:
1✔
130
                    # assume occupancy is 1.0
131
                    species_occ = [species_occ[0], 1.0]
1✔
132

133
                if "_" in species_occ[0]:
1✔
134
                    # see to_string() method in this file, since , and = are not valid
135
                    # species names in AT-AT we replace "," with "__" and "=" with "___",
136
                    # for pymatgen to parse these back correctly we have to replace them back
137
                    species_occ[0] = species_occ[0].replace("___", "=").replace("__", ",")
×
138

139
                species[get_el_sp(species_occ[0])] = float(species_occ[1])
1✔
140

141
            all_species.append(species)
1✔
142

143
        return Structure(lattice, all_species, all_coords)
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc