Skip to content

Commit

Permalink
ENH #78 forwardSolutionsTable()
Browse files Browse the repository at this point in the history
  • Loading branch information
prjemian committed Apr 17, 2021
1 parent 9106f09 commit de73384
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
36 changes: 32 additions & 4 deletions hkl/diffract.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Diffractometer(PseudoPositioner):
~engine
~forward
~inverse
~forwardSolutionsTable
~pa
~wh
~_energy_changed
Expand Down Expand Up @@ -382,17 +383,44 @@ def check_value(self, pos):
pos = [pos.get(p.attr_name, p.position) for p in self.pseudo_positioners]
super().check_value(pos)

def forwardSolutionsTable(self, reflections, full=False):
"""
Return table of computed solutions for each (hkl) in the supplied reflections list.
The solutions are calculated using the current UB matrix & constraints
"""
_table = pyRestTable.Table()
motors = self.real_positioners._fields
_table.labels = "(hkl) solution".split() + list(motors)
for reflection in reflections:
try:
solutions = self.calc.forward(reflection)
except ValueError as exc:
solutions = exc
if isinstance(solutions, ValueError):
row = [reflection, "none"]
row += ["" for m in motors]
_table.addRow(row)
else:
for i, s in enumerate(solutions):
row = [reflection, i]
row += [f"{getattr(s, m):.5f}" for m in motors]
_table.addRow(row)
if not full:
break # only show the first (default) solution
return _table

def pa(self, all_samples=False, printing=True):
"""
report the diffractometer settings
Report the diffractometer settings.
EXAMPLE::
In [1]: from apstools import diffractometer as APS_diffractometer
In [1]: from hkl.geometries import SimulatedK4CV
In [2]: sim4c = APS_diffractometer.SoftE4CV('', name='sim4c')
In [2]: k4cv = SimulatedK4CV('', name='k4cv')
In [3]: sim4c.pa()
In [3]: k4cv.pa() # FIXME lines are too long to include in source code
===================== ====================================================================
term value
===================== ====================================================================
Expand Down
34 changes: 34 additions & 0 deletions hkl/tests/test_diffract.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,40 @@ def test_names(fourc):
assert fourc.class_name.get() == "Fourc"


def test_forwardSolutionsTable(fourc):
fourc.calc.wavelength = 1.54

# (100) has chi ~ 0 which poses occasional roundoff errors
# (sometimes -0.00000, sometimes 0.00000)
sol = fourc.forward(1, 0, 0)
assert pytest.approx(sol.omega, 1e-5) == -30
assert pytest.approx(sol.chi, 1e-5) == 0
assert pytest.approx(sol.phi, 1e-5) == -90
assert pytest.approx(sol.tth, 1e-5) == -60

tbl = fourc.forwardSolutionsTable(
# fmt: off
[
[1, 1, 0],
[1, 1, 1],
[100, 1, 1], # no solutions
]
# fmt: on
)
received = str(tbl).splitlines()
expected = [
"=========== ======== ======== ======== ======== =========",
"(hkl) solution omega chi phi tth ",
"=========== ======== ======== ======== ======== =========",
"[1, 1, 0] 0 45.00000 45.00000 90.00000 90.00000 ",
"[1, 1, 1] 0 60.00000 35.26439 45.00000 120.00000",
"[100, 1, 1] none ",
"=========== ======== ======== ======== ======== =========",
]
for r, e in zip(received, expected):
assert r == e


def test_pa(fourc, capsys):
tbl = fourc.pa()
assert isinstance(tbl, pyRestTable.Table)
Expand Down

0 comments on commit de73384

Please sign in to comment.