Skip to content

Commit 0534fa8

Browse files
fix tth_to_d and d_to_tth
1 parent b733b94 commit 0534fa8

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

src/diffpy/utils/scattering_objects/diffraction_objects.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -427,12 +427,12 @@ def tth_to_d(self):
427427
The array of :math:`d` values in the inverse of the units
428428
of ``wavelength``
429429
"""
430-
epsilon = 1e-10
431430
two_theta = np.asarray(np.deg2rad(self.on_tth[0]))
432431
wavelength = float(self.wavelength)
433432
sin_two_theta = np.sin(two_theta / 2)
434-
sin_two_theta = np.where(np.abs(sin_two_theta) < epsilon, sin_two_theta + epsilon, sin_two_theta)
435-
return wavelength / (2 * sin_two_theta)
433+
d = np.where(sin_two_theta != 0, wavelength / (2 * sin_two_theta), np.inf)
434+
d = np.minimum(d, DMAX)
435+
return d[::-1]
436436

437437
def d_to_tth(self):
438438
r"""
@@ -454,11 +454,10 @@ def d_to_tth(self):
454454
two_theta : array
455455
The array of :math:`2\theta` values in radians
456456
"""
457-
epsilon = 1e-10
458457
d = np.asarray(self.on_d[0])
459-
d = np.where(np.abs(d) < epsilon, d + epsilon, d)
460458
wavelength = float(self.wavelength)
461-
return np.rad2deg(2.0 * np.arcsin(wavelength / (2 * d)))
459+
tth = np.where(d != 0, np.rad2deg(2.0 * np.arcsin(wavelength / (2 * d))), 180)
460+
return tth[::-1]
462461

463462
def set_all_arrays(self):
464463
master_array, xtype = self._get_original_array()

tests/test_diffraction_objects.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -268,18 +268,21 @@ def test_d_to_q():
268268

269269

270270
def test_tth_to_d():
271-
actual = Diffraction_object(wavelength=0.71)
272-
setattr(actual, "on_tth", [[0, 30], [1, 1]])
271+
actual = Diffraction_object(wavelength=2)
272+
setattr(actual, "on_tth", [[0, 30, 60, 90, 120, 180], [1, 2, 3, 4, 5, 6]])
273273
actual_d = actual.tth_to_d()
274-
expected_d = [3550000000, 1.37161]
274+
# expected d values are DMAX=100, 1/sin15, 1/sin30, 1/sin45, 1/sin60, 1/sin90, in reverse order
275+
expected_d = [1, 1.1547, 1.41421, 2, 3.8637, 100]
275276
assert np.allclose(actual_d, expected_d)
276277

277278

278279
def test_d_to_tth():
279-
actual = Diffraction_object(wavelength=0.71)
280-
setattr(actual, "on_d", [[1e10, 1.37161], [1, 1]])
280+
actual = Diffraction_object(wavelength=2)
281+
setattr(actual, "on_d", [[0, 2, 4, 6, 8, 100], [1, 2, 3, 4, 5, 6]])
281282
actual_tth = actual.d_to_tth()
282-
expected_tth = [0, 30]
283+
# expected tth values are 2*arcsin(1/d), in reverse order
284+
# when d is really small we have tth to be 180
285+
expected_tth = [1.14593, 14.36151, 19.18814, 28.95502, 60, 180]
283286
assert np.allclose(actual_tth, expected_tth)
284287

285288

0 commit comments

Comments
 (0)