Skip to content

Commit 41384d6

Browse files
committed
vectorize generate_possible_hkls
1 parent 758921f commit 41384d6

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

pyxtal/XRD_indexer.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,35 @@ def generate_possible_hkls(h_max, k_max, l_max, level=2):
2020
else:
2121
base_signs = [(1, 1, 1), (1, 1, -1), (1, -1, 1), (-1, 1, 1),
2222
(1, -1, -1), (-1, 1, -1), (-1, -1, 1), (-1, -1, -1)]
23-
possible_hkls = []
24-
for h in range(0, h_max + 1):
25-
for k in range(0, k_max + 1):
26-
for l in range(0, l_max + 1):
27-
if h*h + k*k + l*l > 0: # exclude (0,0,0)
28-
# Add all permutations and sign variations
29-
base_hkls = set()
30-
for signs in base_signs:
31-
sh, sk, sl = signs[0]*h, signs[1]*k, signs[2]*l
32-
base_hkls.add((sh, sk, sl))
33-
possible_hkls.extend(list(base_hkls))
34-
return list(set(possible_hkls)) # remove duplicates
23+
# Create meshgrid for all h, k, l combinations
24+
h_vals, k_vals, l_vals = np.meshgrid(
25+
np.arange(h_max + 1),
26+
np.arange(k_max + 1),
27+
np.arange(l_max + 1),
28+
indexing='ij'
29+
)
30+
31+
# Flatten to get all combinations
32+
h_flat = h_vals.flatten()
33+
k_flat = k_vals.flatten()
34+
l_flat = l_vals.flatten()
35+
36+
# Filter out (0,0,0)
37+
non_zero_mask = (h_flat**2 + k_flat**2 + l_flat**2) > 0
38+
h_flat = h_flat[non_zero_mask]
39+
k_flat = k_flat[non_zero_mask]
40+
l_flat = l_flat[non_zero_mask]
41+
42+
# Apply all sign combinations vectorized
43+
all_hkls = []
44+
for signs in base_signs:
45+
sh = signs[0] * h_flat
46+
sk = signs[1] * k_flat
47+
sl = signs[2] * l_flat
48+
hkls_with_signs = np.column_stack([sh, sk, sl])
49+
all_hkls.append(hkls_with_signs)
50+
51+
return np.vstack(all_hkls)
3552

3653
def get_cell_params(spg, hkls, two_thetas, wave_length=1.54184):
3754
"""
@@ -56,10 +73,6 @@ def get_cell_params(spg, hkls, two_thetas, wave_length=1.54184):
5673
a_values = d_spacings * np.sqrt(h_sq_sum)
5774
cell_values = [[a] for a in a_values if 0 < a < 50]
5875

59-
#for hkl, two_theta in zip(hkls, two_thetas):
60-
# theta = np.radians(two_theta / 2)
61-
# d = wave_length / (2 * np.sin(theta))
62-
# cell_values.append([d * np.sqrt(hkl[0]**2 + hkl[1]**2 + hkl[2]**2)])
6376
elif 143 <= spg <= 194: # hexagonal, need a and c
6477
# need two hkls to determine a and c
6578
len_solutions = len(hkls) // 2
@@ -498,7 +511,7 @@ def get_cell_from_multi_hkls(spg, hkls, two_thetas, long_thetas=None, wave_lengt
498511
guesses = guesses[sorted_indices]
499512

500513
# Check the quality of each (hkl, 2theta) solutions
501-
for guess in guesses:
514+
for guess in guesses[1000:]:
502515
found = False
503516
n_peaks = len(guess)
504517

0 commit comments

Comments
 (0)