-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcreate_matrices.py
214 lines (203 loc) · 7.53 KB
/
create_matrices.py
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
"""
## Script for creating the density matrices and species matrices representations
## Uses MPI4PY to parallelise
## Provide the name of the data folder that contains the properties and structures, as created using query_matrpoj.py
## Example:
## -- python3 create_matrices.py --name heusler
--------------------------------------------------
## Author: Callum J. Court.
## Email: [email protected]
## Version: 1.0.0
--------------------------------------------------
## License: MIT
## Copyright: Copyright Callum Court & Batuhan Yildirim 2020, ICSG3D
-------------------------------------------------
"""
import argparse
import os
import sys
import numpy as np
from func_timeout import FunctionTimedOut
from mpi4py import MPI
from utils import (
coordinate_grid,
create_crystal,
density_matrix,
get_sites,
random_rotation_3d,
)
from viz import plot_points_3d
if __name__ == "__main__":
# MPI variables
size = MPI.COMM_WORLD.Get_size()
rank = MPI.COMM_WORLD.Get_rank()
name = MPI.Get_processor_name()
# Arguments
parser = argparse.ArgumentParser()
parser.add_argument("--name", metavar="name", type=str, help="Name of data folder")
parser.add_argument(
"--d", metavar="d", type=int, help="Dimensionality of the matrices", default=32
)
parser.add_argument(
"--nrot",
metavar="nrot",
type=int,
help="Number of rotations to apply",
default=10,
)
parser.add_argument(
"--label_frac",
metavar="label_frac",
type=float,
help="Fraction of ionic radius to label in species matrices",
default=1.0,
)
parser.add_argument(
"--sigma_frac",
metavar="sigma_frac",
type=float,
help="Fraction of ionic radius to use as Gaussian width",
default=1.0,
)
parser.add_argument(
"--eps_frac",
metavar="eps_frac",
type=float,
help="Fraction of unit cell to add along each dimension",
default=0.25,
)
parser.add_argument(
"--max_sites",
metavar="max_sites",
type=int,
help="Maximum number of sites in the unit cell",
default=40,
)
namespace = parser.parse_args()
mode = namespace.name
d = namespace.d
dims = (d, d, d)
n_rot = namespace.nrot
label_frac = namespace.label_frac
eps_frac = namespace.eps_frac
sigma_frac = namespace.sigma_frac
max_sites = namespace.max_sites
data_path = os.path.join("data", mode, "cifs")
csv_path = os.path.join("data", mode, mode + ".csv")
sdir = os.path.join("data", mode, "matrices")
if rank == 0:
os.makedirs(sdir, exist_ok=True)
os.makedirs(os.path.join(sdir, "density_matrices"), exist_ok=True)
os.makedirs(os.path.join(sdir, "species_matrices"), exist_ok=True)
os.makedirs(os.path.join(sdir, "lattice_vectors"), exist_ok=True)
os.makedirs(os.path.join(sdir, "coordinate_grids"), exist_ok=True)
MPI.COMM_WORLD.Barrier()
for root, dirs, files in os.walk(data_path):
for i, fname in enumerate(files):
if i % size == rank:
if not fname.endswith(".cif"):
continue
try:
path = os.path.join(root, fname)
try:
# Create a pymatgen crystal from CIF
crystal = create_crystal(path, primitive=False)
except FunctionTimedOut:
continue
# coordinates, atomic numbers, atomic radii
N, z, r = get_sites(crystal)
if len(N) > max_sites:
continue
Nm = (N <= 1.0) & (N >= 0)
if not np.all(Nm):
print(fname, N)
sys.stdout.flush()
sys.exit()
break
# Lattice vectors
(a, b, c) = crystal.lattice.abc
lattice_vector = np.array(
[
a,
b,
c,
crystal.lattice.alpha,
crystal.lattice.beta,
crystal.lattice.gamma,
]
)
# Convert sites to cartesian
N = np.multiply(N, lattice_vector[:3])
# Create the grid for coordinate convolutions
p = coordinate_grid(lattice_vector, eps_frac=eps_frac, dim=d)
if any(np.isnan(r)):
continue
try:
# Create M and S matrices -- timeout as some massive crystals take forever
M, S = density_matrix(
N,
z,
l=[a, b, c],
sigma=sigma_frac * r,
dims=dims,
label_frac=label_frac,
eps_frac=eps_frac,
)
except FunctionTimedOut:
print(i, " timeout")
continue
# Save matrices
np.save(
os.path.join(sdir, "density_matrices", fname.strip(".cif")), M
)
np.save(
os.path.join(sdir, "species_matrices", fname.strip(".cif")), S
)
np.save(
os.path.join(sdir, "lattice_vectors", fname.strip(".cif")),
lattice_vector,
)
np.save(
os.path.join(sdir, "coordinate_grids", fname.strip(".cif")), p
)
# Apply k random rotations to each
for k in range(n_rot):
m_rot_k, s_rot_k, p_rot_k = random_rotation_3d(M, S, p)
np.save(
os.path.join(
sdir,
"density_matrices",
fname.strip(".cif") + "_rot_" + str(k),
),
m_rot_k,
)
np.save(
os.path.join(
sdir,
"species_matrices",
fname.strip(".cif") + "_rot_" + str(k),
),
s_rot_k,
)
np.save(
os.path.join(
sdir,
"lattice_vectors",
fname.strip(".cif") + "_rot_" + str(k),
),
lattice_vector,
)
np.save(
os.path.join(
sdir,
"coordinate_grids",
fname.strip(".cif") + "_rot_" + str(k),
),
p_rot_k,
)
print(rank, i)
sys.stdout.flush()
except Exception as e:
print(e)
sys.stdout.flush()
break