Skip to content

Commit 290d25b

Browse files
committed
improve the cell solver for mono
1 parent 30d729d commit 290d25b

File tree

1 file changed

+39
-23
lines changed

1 file changed

+39
-23
lines changed

pyxtal/XRD_indexer.py

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -154,27 +154,35 @@ def get_cell_params(spg, hkls, two_thetas, wave_length=1.54184):
154154
for i in range(len_solutions):
155155
hkls_sub = hkls[N*i:N*i+N]
156156
thetas_sub = np.radians(two_thetas[N*i:N*i+N]/2)
157-
d_sub = (1/wave_length / (2 * np.sin(thetas_sub)))**2
158-
159-
# Non-linear system; use numerical methods
160-
from scipy.optimize import minimize
161-
def objective(params):
162-
a, b, c, beta = params
163-
sin_beta2 = np.sin(beta)**2
164-
cos_beta = np.cos(beta)
165-
h, k, l = hkls_sub[:, 0], hkls_sub[:, 1], hkls_sub[:, 2]
166-
d_inv_sq = (h**2 / (a**2 * sin_beta2)) + (k**2 / b**2) + \
167-
(l**2 / (c**2 * sin_beta2)) - \
168-
(2 * h * l * cos_beta / (a * c * sin_beta2))
169-
return np.sum((d_sub - d_inv_sq)**2)
170-
171-
initial_guess = [2.0, 2.0, 2.0, np.pi/2]
172-
bounds = [(1.8, 50), (1.8, 50), (1.8, 50), (np.pi/4, np.pi*3/4)]
173-
result = minimize(objective, initial_guess, bounds=bounds)
174-
if result.success and result.fun < 1e-5:
175-
a, b, c, beta = result.x
176-
cell_values.append((a, b, c, np.degrees(beta)))
177-
#print(result.x, result.fun, hkls_sub, thetas_sub)
157+
d_sub = (2 * np.sin(thetas_sub) / wave_length)**2
158+
h1, k1, l1 = hkls_sub[0]
159+
h2, k2, l2 = hkls_sub[1]
160+
h3, k3, l3 = hkls_sub[2]
161+
h4, k4, l4 = hkls_sub[3]
162+
d1, d2, d3, d4 = d_sub[0], d_sub[1], d_sub[2], d_sub[3]
163+
A = np.array([[h1**2, k1**2, l1**2, h1*l1],
164+
[h2**2, k2**2, l2**2, h2*l2],
165+
[h3**2, k3**2, l3**2, h3*l3],
166+
[h4**2, k4**2, l4**2, h4*l4]])
167+
b = np.array([d1, d2, d3, d4])
168+
#print(A, b)
169+
try:
170+
x = np.linalg.solve(A, b)#; print(A, x)
171+
a1, a2, a3, a4 = x
172+
if a1 <= 0 or a2 <= 0 or a3 <= 0: continue
173+
b = np.sqrt(1/a2)#; print('b value', b); import sys; sys.exit()
174+
cos_beta = - a4 / (2 * np.sqrt(a1) * np.sqrt(a3))
175+
if cos_beta < -1 or cos_beta > 1: continue
176+
betar = np.arccos(cos_beta)
177+
sin_beta = np.sin(betar)
178+
if sin_beta <= np.sin(np.pi/6): continue
179+
a = np.sqrt(1/a1) / sin_beta
180+
c = np.sqrt(1/a3) / sin_beta
181+
if a > 50 or b > 50 or c > 50: continue
182+
cell_values.append((a, b, c, np.degrees(betar)))
183+
#print(h1, k1, l1, cell_values[-1])
184+
except np.linalg.LinAlgError:
185+
continue
178186
else:
179187
msg = "Only cubic, tetragonal, hexagonal, and orthorhombic systems are supported."
180188
raise NotImplementedError(msg)
@@ -441,8 +449,11 @@ def get_cell_from_multi_hkls(spg, hkls, two_thetas, long_thetas=None, wave_lengt
441449
if __name__ == "__main__":
442450
from pyxtal import pyxtal
443451
from itertools import combinations
452+
np.set_printoptions(precision=4, suppress=True)
453+
444454
xtal = pyxtal()
445-
xtal.from_seed('pyxtal/database/cifs/JVASP-62168.cif')
455+
#xtal.from_seed('pyxtal/database/cifs/JVASP-62168.cif')
456+
xtal.from_seed('pyxtal/database/cifs/JVASP-98225.cif')
446457
xrd = xtal.get_XRD(thetas=[0, 120], SCALED_INTENSITY_TOL=0.5)
447458
cell_ref = np.sort(np.array(xtal.lattice.encode()))
448459
long_thetas = xrd.pxrd[:15, 0]
@@ -451,12 +462,17 @@ def get_cell_from_multi_hkls(spg, hkls, two_thetas, long_thetas=None, wave_lengt
451462
print(xrd.by_hkl(N_max=10))
452463

453464
# Get the a list of hkl guesses and sort them by d^2
454-
guesses = xtal.group.generate_hkl_guesses(2, 3, 5, max_square=29, total_square=40, verbose=True)
465+
if spg >= 16:
466+
guesses = xtal.group.generate_hkl_guesses(2, 3, 5, max_square=29, total_square=40, verbose=True)
467+
else:
468+
guesses = xtal.group.generate_hkl_guesses(2, 2, 2, max_square=20, total_square=30, verbose=True)
455469
guesses = np.array(guesses)
456470
print("Total guesses:", len(guesses))
457471
sum_squares = np.sum(guesses**2, axis=(1,2))
458472
sorted_indices = np.argsort(sum_squares)
459473
guesses = guesses[sorted_indices]
474+
#guesses = np.array([[[2, 0, 0], [1, 1, 0], [0, 1, 1], [0, 0, 2]]])
475+
#guesses = np.array([[[2, 0, 0], [1, 1, 0], [0, 0, 2], [2, 0, -2]]])
460476

461477
# Check the quality of each (hkl, 2theta) solutions
462478
for guess in guesses[:]:

0 commit comments

Comments
 (0)