bluesky / hklpy

Diffractometer computation library with ophyd pseudopositioner support
https://blueskyproject.io/hklpy
BSD 3-Clause "New" or "Revised" License
2 stars 11 forks source link

test the `or_swap()` function #260

Closed prjemian closed 9 months ago

prjemian commented 9 months ago

Create unit tests for or_swap() that it actually swaps the two reflections or raises exceptions when the number of reflections is not exactly two.

prjemian commented 9 months ago

Such as this setup:

import hkl
from hkl.user import select_diffractometer
from hkl.user import new_sample
from hkl.user import setor
from hkl.user import calc_UB

e4cv = hkl.SimulatedE4CV("", name="e4cv")

# setup a sample
select_diffractometer(e4cv)
a0 = hkl.SI_LATTICE_PARAMETER
new_sample("silicon", a0, a0, a0, 90, 90, 90)
r400 = setor(4, 0, 0, -145.451, 0, 0, 69.0966, wavelength=1.54)
r040 = setor(0, 4, 0, -145.451, 0, 90, 69.0966, wavelength=1.54)
calc_UB(r400, r040)
show_sample()

has this output

Sample: silicon (*)

============ =========================================================
key          value                                                    
============ =========================================================
name         silicon                                                  
lattice      [5.431020511, 5.431020511, 5.431020511, 90.0, 90.0, 90.0]
reflection 1 (h=4.0, k=0.0, l=0.0)                                    
reflection 2 (h=0.0, k=4.0, l=0.0)                                    
U            [[-1.e-05 -1.e+00  0.e+00]                               
              [ 0.e+00  0.e+00  1.e+00]                               
              [-1.e+00  1.e-05  0.e+00]]                              
UB           [[-1.00000e-05 -1.15691e+00  0.00000e+00]                
              [ 0.00000e+00  0.00000e+00  1.15691e+00]                
              [-1.15691e+00  1.00000e-05  0.00000e+00]]               
============ =========================================================

With hklpy 1.0.3, these next steps show the reflections have not been swapped:

or_swap()
show_sample()
Sample: silicon (*)

============ =========================================================
key          value                                                    
============ =========================================================
name         silicon                                                  
lattice      [5.431020511, 5.431020511, 5.431020511, 90.0, 90.0, 90.0]
reflection 1 (h=4.0, k=0.0, l=0.0)                                    
reflection 2 (h=0.0, k=4.0, l=0.0)                                    
U            [[-1.e-05 -1.e+00  0.e+00]                               
              [ 0.e+00  0.e+00  1.e+00]                               
              [-1.e+00  1.e-05  0.e+00]]                              
UB           [[-1.00000e-05 -1.15691e+00  0.00000e+00]                
              [ 0.00000e+00  0.00000e+00  1.15691e+00]                
              [-1.15691e+00  1.00000e-05  0.00000e+00]]               
============ =========================================================

That's why it is a BUG.

prjemian commented 9 months ago

While we're at it, test:

calc_UB(r040, r400)
show_sample()

[[-1.41342846e-05 -1.15690694e+00  7.08409844e-17]
 [ 0.00000000e+00  0.00000000e+00  1.15690694e+00]
 [-1.15690694e+00  1.41342846e-05  7.08392534e-17]]
Sample: silicon (*)

============ =========================================================
key          value                                                    
============ =========================================================
name         silicon                                                  
lattice      [5.431020511, 5.431020511, 5.431020511, 90.0, 90.0, 90.0]
reflection 1 (h=4.0, k=0.0, l=0.0)                                    
reflection 2 (h=0.0, k=4.0, l=0.0)                                    
U            [[-1.e-05 -1.e+00  0.e+00]                               
              [ 0.e+00  0.e+00  1.e+00]                               
              [-1.e+00  1.e-05  0.e+00]]                              
UB           [[-1.00000e-05 -1.15691e+00  0.00000e+00]                
              [ 0.00000e+00  0.00000e+00  1.15691e+00]                
              [-1.15691e+00  1.00000e-05  0.00000e+00]]               
============ =========================================================
prjemian commented 9 months ago

Exceptions are tested in test_sample.py