Skip to content

Commit cc55326

Browse files
authored
Merge pull request #570 from kewh5868/upd-struct2xas
Suggestion to refactor xyz2struct in struct2xas.py: Using vectorized np.max for coordina…
2 parents 48e3d53 + 996aa8e commit cc55326

File tree

1 file changed

+9
-19
lines changed

1 file changed

+9
-19
lines changed

larch/xrd/struct2xas.py

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,30 +64,20 @@ def _pprint(matrix):
6464

6565

6666
def xyz2struct(molecule):
67-
"""Convert pymatgen molecule to dummy pymatgen structure"""
68-
69-
alat, blat, clat = 1, 1, 1
70-
71-
# Set the lattice dimensions in each direction
72-
for i in range(len(molecule) - 1):
73-
if molecule.cart_coords[i][0] > molecule.cart_coords[i + 1][0]:
74-
alat = molecule.cart_coords[i][0]
75-
if molecule.cart_coords[i][1] > molecule.cart_coords[i + 1][1]:
76-
blat = molecule.cart_coords[i][1]
77-
if molecule.cart_coords[i][2] > molecule.cart_coords[i + 1][2]:
78-
clat = molecule.cart_coords[i][2]
79-
80-
# Set the lattice dimensions in each direction
67+
"""Convert pymatgen molecule to dummy pymatgen structure using vectorized calculation."""
68+
# Ensure the coordinates are in a NumPy array
69+
coords = np.array(molecule.cart_coords)
70+
71+
# Compute maximum coordinate along each axis
72+
alat, blat, clat = np.max(coords, axis=0)
73+
8174
lattice = Lattice.from_parameters(
8275
a=alat, b=blat, c=clat, alpha=90, beta=90, gamma=90
8376
)
84-
77+
8578
# Create a list of species
8679
species = [Element(sym) for sym in molecule.species]
87-
88-
# Create a list of coordinates
89-
coords = molecule.cart_coords
90-
80+
9181
# Create the Structure object
9282
struct = Structure(lattice, species, coords, coords_are_cartesian=True)
9383
return struct

0 commit comments

Comments
 (0)