This commit is contained in:
2019-03-20 13:52:00 +01:00
parent 3084fe0510
commit 5db0f78aee
910 changed files with 191152 additions and 322 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,481 @@
###
# Copyright 2008-2011 Diamond Light Source Ltd.
# This file is part of Diffcalc.
#
# Diffcalc is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Diffcalc is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Diffcalc. If not, see <http://www.gnu.org/licenses/>.
###
import math
from math import pi, sin, cos
from nose.plugins.skip import SkipTest
from nose.tools import assert_almost_equal, raises, eq_ # @UnresolvedImport
from mock import Mock
try:
from numpy import matrix
except ImportError:
from numjy import matrix
from diffcalc.hkl.you.calc import YouHklCalculator, I, \
_calc_angle_between_naz_and_qaz
from test.tools import assert_array_almost_equal, \
assert_matrix_almost_equal
from diffcalc.hkl.you.geometry import YouPosition
from test.diffcalc.hkl.vlieg.test_calc import \
createMockDiffractometerGeometry, createMockHardwareMonitor, \
createMockUbcalc
from test.diffcalc.test_hardware import SimpleHardwareAdapter
from diffcalc.util import DiffcalcException
from diffcalc.hkl.you.constraints import NUNAME
TORAD = pi / 180
TODEG = 180 / pi
x = matrix('1; 0; 0')
y = matrix('0; 1; 0')
z = matrix('0; 0; 1')
def isnan(n):
# math.isnan was introduced only in python 2.6 and is not in Jython (2.5.2)
try:
return math.isnan(n)
except AttributeError:
return n != n # for Jython
class Test_anglesToVirtualAngles():
def setup_method(self):
constraints = Mock()
constraints.is_fully_constrained.return_value = True
self.calc = YouHklCalculator(createMockUbcalc(None),
createMockDiffractometerGeometry(),
createMockHardwareMonitor(),
constraints)
def check_angle(self, name, expected, mu=-99, delta=99, nu=99,
eta=99, chi=99, phi=99):
"""All in degrees"""
pos = YouPosition(mu, delta, nu, eta, chi, phi, unit='DEG')
pos.changeToRadians()
calculated = self.calc._anglesToVirtualAngles(pos, None)[name] * TODEG
assert_almost_equal(calculated, expected)
# theta
def test_theta0(self):
self.check_angle('theta', 0, delta=0, nu=0)
def test_theta1(self):
self.check_angle('theta', 1, delta=2, nu=0)
def test_theta2(self):
self.check_angle('theta', 1, delta=0, nu=2)
def test_theta3(self):
self.check_angle('theta', 1, delta=-2, nu=0)
def test_theta4(self):
self.check_angle('theta', 1, delta=0, nu=-2)
# qaz
def test_qaz0_degenerate_case(self):
self.check_angle('qaz', 0, delta=0, nu=0)
def test_qaz1(self):
self.check_angle('qaz', 90, delta=2, nu=0)
def test_qaz2(self):
self.check_angle('qaz', 90, delta=90, nu=0)
def test_qaz3(self):
self.check_angle('qaz', 0, delta=0, nu=1,)
# Can't see one by eye
# def test_qaz4(self):
# pos = YouPosition(delta=20*TORAD, nu=20*TORAD)#.inRadians()
# assert_almost_equal(
# self.calc._anglesToVirtualAngles(pos, None)['qaz']*TODEG, 45)
#alpha
def test_defaultReferenceValue(self):
# The following tests depemd on this
assert_matrix_almost_equal(self.calc._ubcalc.n_phi, matrix([[0], [0], [1]]))
def test_alpha0(self):
self.check_angle('alpha', 0, mu=0, eta=0, chi=0, phi=0)
def test_alpha1(self):
self.check_angle('alpha', 0, mu=0, eta=0, chi=0, phi=10)
def test_alpha2(self):
self.check_angle('alpha', 0, mu=0, eta=0, chi=0, phi=-10)
def test_alpha3(self):
self.check_angle('alpha', 2, mu=2, eta=0, chi=0, phi=0)
def test_alpha4(self):
self.check_angle('alpha', -2, mu=-2, eta=0, chi=0, phi=0)
def test_alpha5(self):
self.check_angle('alpha', 2, mu=0, eta=90, chi=2, phi=0)
#beta
def test_beta0(self):
self.check_angle('beta', 0, delta=0, nu=0, mu=0, eta=0, chi=0, phi=0)
def test_beta1(self):
self.check_angle('beta', 0, delta=10, nu=0, mu=0, eta=6, chi=0, phi=5)
def test_beta2(self):
self.check_angle('beta', 10, delta=0, nu=10, mu=0, eta=0, chi=0, phi=0)
def test_beta3(self):
self.check_angle('beta', -10, delta=0, nu=-10, mu=0, eta=0, chi=0,
phi=0)
def test_beta4(self):
self.check_angle('beta', 5, delta=0, nu=10, mu=5, eta=0, chi=0, phi=0)
# azimuth
def test_naz0(self):
self.check_angle('naz', 0, mu=0, eta=0, chi=0, phi=0)
def test_naz1(self):
self.check_angle('naz', 0, mu=0, eta=0, chi=0, phi=10)
def test_naz3(self):
self.check_angle('naz', 0, mu=10, eta=0, chi=0, phi=10)
def test_naz4(self):
self.check_angle('naz', 2, mu=0, eta=0, chi=2, phi=0)
def test_naz5(self):
self.check_angle('naz', -2, mu=0, eta=0, chi=-2, phi=0)
#tau
def test_tau0(self):
self.check_angle('tau', 0, mu=0, delta=0, nu=0, eta=0, chi=0, phi=0)
#self.check_angle('tau_from_dot_product', 90, mu=0, delta=0,
#nu=0, eta=0, chi=0, phi=0)
def test_tau1(self):
self.check_angle('tau', 90, mu=0, delta=20, nu=0, eta=10, chi=0, phi=0)
#self.check_angle('tau_from_dot_product', 90, mu=0, delta=20,
#nu=0, eta=10, chi=0, phi=0)
def test_tau2(self):
self.check_angle('tau', 90, mu=0, delta=20, nu=0, eta=10, chi=0, phi=3)
#self.check_angle('tau_from_dot_product', 90, mu=0, delta=20,
#nu=0, eta=10, chi=0, phi=3)
def test_tau3(self):
self.check_angle('tau', 88, mu=0, delta=20, nu=0, eta=10, chi=2, phi=0)
#self.check_angle('tau_from_dot_product', 88, mu=0, delta=20,
#nu=0, eta=10, chi=2, phi=0)
def test_tau4(self):
self.check_angle('tau', 92, mu=0, delta=20, nu=0, eta=10, chi=-2,
phi=0)
#self.check_angle('tau_from_dot_product', 92, mu=0, delta=20,
#nu=0, eta=10, chi=-2, phi=0)
def test_tau5(self):
self.check_angle('tau', 10, mu=0, delta=0, nu=20, eta=0, chi=0, phi=0)
#self.check_angle('tau_from_dot_product', 10, mu=0, delta=0,
#nu=20, eta=0, chi=0, phi=0)
#psi
def test_psi0(self):
pos = YouPosition(0, 0, 0, 0, 0, 0, 'DEG')
assert isnan(self.calc._anglesToVirtualAngles(pos, None)['psi'])
def test_psi1(self):
self.check_angle('psi', 90, mu=0, delta=11, nu=0, eta=0, chi=0, phi=0)
def test_psi2(self):
self.check_angle(
'psi', 100, mu=10, delta=.001, nu=0, eta=0, chi=0, phi=0)
def test_psi3(self):
self.check_angle(
'psi', 80, mu=-10, delta=.001, nu=0, eta=0, chi=0, phi=0)
def test_psi4(self):
self.check_angle(
'psi', 90, mu=0, delta=11, nu=0, eta=0, chi=0, phi=12.3)
def test_psi5(self):
#self.check_angle('psi', 0, mu=10, delta=.00000001,
#nu=0, eta=0, chi=90, phi=0)
pos = YouPosition(0, 0, 0, 0, 90, 0, 'DEG')
pos.changeToRadians()
assert isnan(self.calc._anglesToVirtualAngles(pos, None)['psi'])
def test_psi6(self):
self.check_angle(
'psi', 90, mu=0, delta=0.001, nu=0, eta=90, chi=0, phi=0)
def test_psi7(self):
self.check_angle(
'psi', 92, mu=0, delta=0.001, nu=0, eta=90, chi=2, phi=0)
def test_psi8(self):
self.check_angle(
'psi', 88, mu=0, delta=0.001, nu=0, eta=90, chi=-2, phi=0)
class Test_calc_theta():
def setup_method(self):
self.calc = YouHklCalculator(createMockUbcalc(I * 2 * pi),
createMockDiffractometerGeometry(),
createMockHardwareMonitor(),
Mock())
self.e = 12.398420 # 1 Angstrom
def test_100(self):
h_phi = matrix([[1], [0], [0]])
assert_almost_equal(self.calc._calc_theta(h_phi * 2 * pi, 1) * TODEG,
30)
@raises(DiffcalcException)
def test_too_short(self):
h_phi = matrix([[1], [0], [0]])
self.calc._calc_theta(h_phi * 0, 1)
@raises(DiffcalcException)
def test_too_long(self):
h_phi = matrix([[1], [0], [0]])
self.calc._calc_theta(h_phi * 2 * pi, 10)
class Test_calc_remaining_reference_angles_given_one():
# TODO: These are very incomplete due to either totally failing inutuition
# or code!
def setup_method(self):
self.calc = YouHklCalculator(createMockUbcalc(None),
createMockDiffractometerGeometry(),
createMockHardwareMonitor(),
Mock())
def check(self, name, value, theta, tau, psi_e, alpha_e, beta_e, places=7):
# all in deg
alpha, beta = self.calc._calc_remaining_reference_angles(
name, value * TORAD, theta * TORAD, tau * TORAD)
if alpha_e is not None:
assert_almost_equal(alpha * TODEG, alpha_e, places)
if beta_e is not None:
assert_almost_equal(beta * TODEG, beta_e, places)
psi_vals = list(self.calc._calc_psi(alpha, theta * TORAD, tau * TORAD))
if psi_e is not None:
assert_array_almost_equal(sorted([v * TODEG for v in psi_vals]), sorted(psi_e))
for psi in psi_vals:
print 'psi', psi * TODEG, ' alpha:', alpha * TODEG,\
' beta:', beta * TODEG
def test_psi_given0(self):
self.check('psi', 90, theta=10, tau=90, psi_e=[-90, 90],
alpha_e=0, beta_e=0)
def test_psi_given1(self):
self.check('psi', 92, theta=0.001, tau=90, psi_e=[-92, 92],
alpha_e=2, beta_e=-2)
def test_psi_given3(self):
self.check('psi', 88, theta=0.001, tau=90, psi_e=[-88, 88],
alpha_e=-2, beta_e=2)
def test_psi_given4(self):
self.check('psi', 0, theta=0.001, tau=90, psi_e=[0,],
alpha_e=-90, beta_e=90, places=2)
def test_psi_given4a(self):
self.check('psi', 180, theta=0.001, tau=90, psi_e=[-180, 180],
alpha_e=90, beta_e=-90, places=2)
def test_psi_given5(self):
self.check('psi', 180, theta=0.001, tau=80,
psi_e=[-180, 180], alpha_e=80, beta_e=-80, places=2)
def test_a_eq_b0(self):
self.check('a_eq_b', 9999, theta=0.001, tau=90,
psi_e=[-90, 90], alpha_e=0, beta_e=0)
def test_alpha_given(self):
self.check('alpha', 2, theta=0.001, tau=90,
psi_e=[-92, 92], alpha_e=2, beta_e=-2)
def test_beta_given(self):
self.check('beta', 2, theta=0.001, tau=90,
psi_e=[-88, 88], alpha_e=-2, beta_e=2)
# def test_a_eq_b1(self):
# self.check('a_eq_b', 9999, theta=20, tau=90,
# psi_e=90, alpha_e=10, beta_e=10)
# def test_psi_given0(self):
# self.check('psi', 90, theta=10, tau=45, psi_e=90,
# alpha_e=7.0530221302831952, beta_e=7.0530221302831952)
class Test_calc_detector_angles_given_one():
def setup_method(self):
self.calc = YouHklCalculator(createMockUbcalc(None),
createMockDiffractometerGeometry(),
createMockHardwareMonitor(),
Mock())
def check(self, name, value, theta, delta_e, nu_e, qaz_e):
# all in deg
delta, nu, qaz = zip(*self.calc._calc_remaining_detector_angles(
name, value * TORAD, theta * TORAD))
for delta_, nu_, qaz_ in zip(delta, nu, qaz):
print 'delta:', delta_ * TODEG, ' nu:', nu_ * TODEG, ' qaz:', qaz_ * TODEG
assert_array_almost_equal([v * TODEG for v in delta], delta_e)
assert_array_almost_equal([v * TODEG for v in nu], nu_e)
if qaz_e is not None:
assert_array_almost_equal([v * TODEG for v in qaz], qaz_e)
def test_nu_given0(self):
self.check(NUNAME, 0, theta=3, delta_e=[6, -6], nu_e=[0, 0], qaz_e=[90, -90])
def test_nu_given1(self):
self.check(NUNAME, 10, theta=7.0530221302831952,
delta_e=[10, -10], nu_e=[10, 10], qaz_e=None)
def test_nu_given2(self):
self.check(NUNAME, 6, theta=3, delta_e=[0,], nu_e=[6,], qaz_e=[0,])
def test_delta_given0(self):
self.check('delta', 0, theta=3, delta_e=[0, 0], nu_e=[6, -6], qaz_e=[0, 180])
def test_delta_given1(self):
self.check('delta', 10, theta=7.0530221302831952,
delta_e=[10, 10], nu_e=[10, -10], qaz_e=None)
def test_delta_given2(self):
self.check('delta', 6, theta=3, delta_e=[6,], nu_e=[0,], qaz_e=[90,])
def test_qaz_given0(self):
self.check('qaz', 90, theta=3, delta_e=[6, 174], nu_e=[0, -180], qaz_e=[90, 90])
def test_qaz_given2(self):
self.check('qaz', 0, theta=3, delta_e=[0, 180], nu_e=[6, -174], qaz_e=[0, 0])
class Test_calc_angle_between_naz_and_qaz():
def test1(self):
diff = _calc_angle_between_naz_and_qaz(
theta=0, alpha=0, tau=90 * TORAD)
assert_almost_equal(diff * TODEG, 90)
def test2(self):
diff = _calc_angle_between_naz_and_qaz(
theta=0 * TORAD, alpha=0, tau=80 * TORAD)
assert_almost_equal(diff * TODEG, 80)
class Test_calc_remaining_sample_angles_given_one():
#_calc_remaining_detector_angles_given_one
def setup_method(self):
self.calc = YouHklCalculator(createMockUbcalc(None),
createMockDiffractometerGeometry(),
createMockHardwareMonitor(),
Mock())
def check(self, name, value, Q_lab, n_lab, Q_phi, n_phi,
phi_e, chi_e, eta_e, mu_e):
mu, eta, chi, phi = zip(*self.calc._calc_remaining_sample_angles(
name, value * TORAD, Q_lab, n_lab, Q_phi, n_phi))
for mu_, eta_, chi_, phi_ in zip(mu, eta, chi, phi):
print 'phi', phi_ * TODEG, ' chi:', chi_ * TODEG, ' eta:', eta_ * TODEG,\
' mu:', mu_ * TODEG
if phi_e is not None:
assert_array_almost_equal([v * TODEG for v in phi], phi_e)
if chi_e is not None:
assert_array_almost_equal([v * TODEG for v in chi], chi_e)
if eta_e is not None:
assert_array_almost_equal([v * TODEG for v in eta], eta_e)
if mu_e is not None:
assert_array_almost_equal([v * TODEG for v in mu], mu_e)
def test_constrain_xx_degenerate(self):
self.check('mu', 0, Q_lab=x, n_lab=x, Q_phi=x, n_phi=x,
phi_e=[0,], chi_e=[0,], eta_e=[0,], mu_e=[0,])
def test_constrain_mu_0(self):
self.check('mu', 0, Q_lab=x, n_lab=z, Q_phi=x, n_phi=z,
phi_e=[0,], chi_e=[0,], eta_e=[0,], mu_e=[0,])
def test_constrain_mu_10(self):
self.check('mu', 10, Q_lab=x, n_lab=z, Q_phi=x, n_phi=z,
phi_e=[90, -90], chi_e=[10, -10], eta_e=[-90, 90], mu_e=[10, 10])
def test_constrain_mu_n10(self):
self.check('mu', -10, Q_lab=x, n_lab=z, Q_phi=x, n_phi=z,
phi_e=[-90, 90], chi_e=[10, -10], eta_e=[90, -90], mu_e=[-10, -10])
def test_constrain_eta_10_wasfailing(self):
# Required the choice of a different equation
self.check('eta', 10, Q_lab=x, n_lab=z, Q_phi=x, n_phi=z,
phi_e=[-10, -170], chi_e=[0, 180], eta_e=[10, 10], mu_e=[0, -180])
def test_constrain_eta_n10(self):
self.check('eta', -10, Q_lab=x, n_lab=z, Q_phi=x, n_phi=z,
phi_e=[10, 170], chi_e=[0, 180], eta_e=[-10, -10], mu_e=[0, 180])
def test_constrain_eta_20_with_theta_20(self):
theta = 20 * TORAD
Q_lab = matrix([[cos(theta)], [-sin(theta)], [0]])
self.check('eta', 20, Q_lab=Q_lab, n_lab=z, Q_phi=x, n_phi=z,
phi_e=[0, -140], chi_e=[0, 180], eta_e=[20, 20], mu_e=[0, -180])
@raises(DiffcalcException)
def test_constrain_chi_0_degenerate(self):
self.check('chi', 0, Q_lab=x, n_lab=z, Q_phi=x, n_phi=z,
phi_e=0, chi_e=0, eta_e=0, mu_e=0)
def test_constrain_chi_10(self):
self.check('chi', 10, Q_lab=x, n_lab=z, Q_phi=x, n_phi=z,
phi_e=[-90, 90], chi_e=[10, 10], eta_e=[90, -90], mu_e=[-10, 10])
def test_constrain_chi_90(self):
self.check('chi', 90, Q_lab=z * (-1), n_lab=x, Q_phi=x, n_phi=z,
phi_e=[0, 0], chi_e=[90, 90], eta_e=[0, 0], mu_e=[-180, 0])
def test_constrain_phi_0(self):
self.check('phi', 0, Q_lab=x, n_lab=z, Q_phi=x, n_phi=z,
phi_e=[0, 0], chi_e=[0, -180], eta_e=[0, 180], mu_e=[0, -180])
def test_constrain_phi_10(self):
self.check('phi', 10, Q_lab=x, n_lab=z, Q_phi=x, n_phi=z,
phi_e=[10, 10], chi_e=[0, -180], eta_e=[-10, 190], mu_e=[0, -180])
def test_constrain_phi_n10(self):
self.check('phi', -10, Q_lab=x, n_lab=z, Q_phi=x, n_phi=z,
phi_e=[-10, -10], chi_e=[0, -180], eta_e=[10, 170], mu_e=[0, -180])
def test_constrain_phi_20_with_theta_20(self):
theta = 20 * TORAD
Q_lab = matrix([[cos(theta)], [-sin(theta)], [0]])
self.check('phi', 20, Q_lab=Q_lab, n_lab=z, Q_phi=x, n_phi=z,
phi_e=[20, 20], chi_e=[0, -180], eta_e=[0, 180], mu_e=[0, -180])

View File

@@ -0,0 +1,557 @@
###
# Copyright 2008-2011 Diamond Light Source Ltd.
# This file is part of Diffcalc.
#
# Diffcalc is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Diffcalc is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Diffcalc. If not, see <http://www.gnu.org/licenses/>.
###
# TODO: class largely copied from test_calc
from math import pi
from mock import Mock
from nose.plugins.skip import SkipTest
try:
from numpy import matrix
except ImportError:
from numjy import matrix
from diffcalc.hkl.you.geometry import SixCircle
from diffcalc.hkl.willmott.calc import \
WillmottHorizontalPosition as WillPos
from diffcalc.hkl.you.geometry import YouPosition as YouPos
from diffcalc.hkl.you.calc import YouUbCalcStrategy
from test.tools import matrixeq_
from diffcalc.ub.calc import UBCalculation
from diffcalc.ub.crystal import CrystalUnderTest
from diffcalc.ub.persistence import UbCalculationNonPersister
from test.diffcalc.hkl.you.test_calc import _BaseTest
from diffcalc.hkl.you.constraints import NUNAME
TORAD = pi / 180
TODEG = 180 / pi
I = matrix('1 0 0; 0 1 0; 0 0 1')
class SkipTestSurfaceNormalVerticalCubic(_BaseTest):
def setup_method(self):
_BaseTest.setup_method(self)
self.constraints._constrained = {'a_eq_b': None, 'mu': -pi / 2,
'eta': 0}
self.wavelength = 1
self.UB = I * 2 * pi
def _configure_ub(self):
self.mock_ubcalc.UB = self.UB
def _check(self, hkl, pos, virtual_expected={}, fails=False):
if pos is not None:
self._check_angles_to_hkl('', 999, 999, hkl, pos, self.wavelength,
virtual_expected)
if fails:
self._check_hkl_to_angles_fails('', 999, 999, hkl, pos,
self.wavelength, virtual_expected)
else:
self._check_hkl_to_angles('', 999, 999, hkl, pos, self.wavelength,
virtual_expected)
def testHkl001(self):
pos = YouPos(mu=-90, delta=60, nu=0, eta=0, chi=90 + 30, phi=-90, unit='DEG')
self._check((0, 0, 1), pos, {'alpha': 30, 'beta': 30})
def testHkl011(self):
# raise SkipTest()
# skipped because we can't calculate values to check against by hand
pos = YouPos(mu=-90, delta=90, nu=0, eta=0, chi=90 + 90, phi=-90, unit='DEG')
self._check((0, 1, 1), pos, {'alpha': 45, 'beta': 45})
def testHkl010fails(self):
self._check((0, 1, 0),
None,
{'alpha': 30, 'beta': 30}, fails=True)
def testHkl100fails(self):
self._check((1, 0, 0),
None,
{'alpha': 30, 'beta': 30}, fails=True)
def testHkl111(self):
raise SkipTest()
# skipped because we can't calculate values to check against by hand
pos = YouPos(mu=-90, delta=90, nu=0, eta=0, chi=90 + 90, phi=-90, unit='DEG')
self._check((1, 1, 1), pos, {'alpha': 45, 'beta': 45})
# Primary and secondary reflections found with the help of DDIF on Diamond's
# i07 on Jan 27 2010
HKL0 = 2, 19, 32
REF0 = WillPos(delta=21.975, gamma=4.419, omegah=2, phi=326.2)
HKL1 = 0, 7, 22
REF1 = WillPos(delta=11.292, gamma=2.844, omegah=2, phi=124.1)
WAVELENGTH = 0.6358
ENERGY = 12.39842 / WAVELENGTH
# This is the version that Diffcalc comes up with ( see following test)
U_DIFFCALC = matrix([[-0.7178876, 0.6643924, -0.2078944],
[-0.6559596, -0.5455572, 0.5216170],
[0.2331402, 0.5108327, 0.8274634]])
#class WillmottHorizontalGeometry(VliegGeometry):
#
# def __init__(self):
# VliegGeometry.__init__(self,
# name='willmott_horizontal',
# supported_mode_groups=[],
# fixed_parameters={},
# gamma_location='base'
# )
#
# def physical_angles_to_internal_position(self, physicalAngles):
# assert (len(physicalAngles) == 4), "Wrong length of input list"
# return WillPos(*physicalAngles)
#
# def internal_position_to_physical_angles(self, internalPosition):
# return internalPosition.totuple()
def willmott_to_you_fixed_mu_eta(pos):
pos = YouPos(mu=-90,
delta=pos.delta,
nu=pos.gamma,
eta=0,
chi=90 + pos.omegah,
phi=-90 - pos.phi,
unit='DEG')
if pos.phi > 180:
pos.phi -= 360
elif pos.phi < -180:
pos.phi += 360
return pos
class TestUBCalculationWithWillmotStrategy_Si_5_5_12_FixedMuEta():
def setup_method(self):
hardware = Mock()
hardware.get_axes_names.return_value = ('m', 'd', 'n', 'e', 'c',
'p')
self.ubcalc = UBCalculation(hardware, SixCircle(),
UbCalculationNonPersister(),
YouUbCalcStrategy())
def testAgainstResultsFromJan_27_2010(self):
self.ubcalc.start_new('test')
self.ubcalc.set_lattice('Si_5_5_12', 7.68, 53.48, 75.63, 90, 90, 90)
self.ubcalc.add_reflection(
HKL0[0], HKL0[1], HKL0[2], willmott_to_you_fixed_mu_eta(REF0),
ENERGY, 'ref0', None)
self.ubcalc.add_reflection(
HKL1[0], HKL1[1], HKL1[2], willmott_to_you_fixed_mu_eta(REF1),
ENERGY, 'ref1', None)
self.ubcalc.calculate_UB()
print "U: ", self.ubcalc.U
print "UB: ", self.ubcalc.UB
matrixeq_(self.ubcalc.U, U_DIFFCALC)
class TestFixedMuEta(_BaseTest):
def setup_method(self):
_BaseTest.setup_method(self)
self._configure_constraints()
self.wavelength = 0.6358
B = CrystalUnderTest('xtal', 7.68, 53.48,
75.63, 90, 90, 90).B
self.UB = U_DIFFCALC * B
self._configure_limits()
def _configure_constraints(self):
self.constraints._constrained = {'alpha': 2 * TORAD, 'mu': -pi / 2,
'eta': 0}
def _configure_limits(self):
self.mock_hardware.set_lower_limit(NUNAME, None)
self.mock_hardware.set_upper_limit('delta', 90)
self.mock_hardware.set_lower_limit('mu', None)
self.mock_hardware.set_lower_limit('eta', None)
self.mock_hardware.set_lower_limit('chi', None)
def _convert_willmott_pos(self, willmott_pos):
return willmott_to_you_fixed_mu_eta(willmott_pos)
def _configure_ub(self):
self.mock_ubcalc.UB = self.UB
def _check(self, hkl, pos, virtual_expected={}, fails=False):
self._check_angles_to_hkl('', 999, 999, hkl, pos, self.wavelength,
virtual_expected)
if fails:
self._check_hkl_to_angles_fails('', 999, 999, hkl, pos,
self.wavelength, virtual_expected)
else:
self._check_hkl_to_angles('', 999, 999, hkl, pos, self.wavelength,
virtual_expected)
def testHkl_2_19_32_found_orientation_setting(self):
'''Check that the or0 reflection maps back to the assumed hkl'''
self.places = 2
self._check_angles_to_hkl('', 999, 999, HKL0,
self._convert_willmott_pos(REF0),
self.wavelength, {'alpha': 2})
def testHkl_0_7_22_found_orientation_setting(self):
'''Check that the or1 reflection maps back to the assumed hkl'''
self.places = 0
self._check_angles_to_hkl('', 999, 999, HKL1,
self._convert_willmott_pos(REF1),
self.wavelength, {'alpha': 2})
def testHkl_2_19_32_calculated_from_DDIF(self):
self.places = 3
willpos = WillPos(delta=21.974, gamma=4.419, omegah=2, phi=-33.803)
self._check((2, 19, 32),
self._convert_willmott_pos(willpos),
{'alpha': 2})
def testHkl_0_7_22_calculated_from_DDIF(self):
self.places = 3
willpos = WillPos(delta=11.241801854649, gamma=-3.038407637123,
omegah=2, phi=-86.56344250267)
self._check((0, 7, 22),
self._convert_willmott_pos(willpos),
{'alpha': 2})
def testHkl_2_m5_12_calculated_from_DDIF(self):
self.places = 3
willpos = WillPos(delta=5.224, gamma=10.415, omegah=2, phi=-1.972)
self._check((2, -5, 12),
self._convert_willmott_pos(willpos),
{'alpha': 2})
def testHkl_2_19_32_calculated_predicted_with_diffcalc_and_found(self):
willpos = WillPos(delta=21.974032376045, gamma=4.418955754003,
omegah=2, phi=-33.80254)
self._check((2, 19, 32),
self._convert_willmott_pos(willpos),
{'alpha': 2})
def testHkl_0_7_22_calculated_predicted_with_diffcalc_and_found(self):
willpos = WillPos(delta=11.241801854649, gamma=-3.038407637123,
omegah=2, phi=-86.563442502670)
self._check((0, 7, 22),
self._convert_willmott_pos(willpos),
{'alpha': 2})
def testHkl_2_m5_12_calculated_predicted_with_diffcalc_and_found(self):
willpos = WillPos(delta=5.223972025344, gamma=10.415435905622,
omegah=2, phi=-90 + 88.02751)
self._check((2, -5, 12),
self._convert_willmott_pos(willpos),
{'alpha': 2})
###############################################################################
def willmott_to_you_fixed_mu_chi(pos):
pos = YouPos(mu=-0,
delta=pos.delta,
nu=pos.gamma,
eta=pos.omegah,
chi=90,
phi=-pos.phi,
unit='DEG')
if pos.phi > 180:
pos.phi -= 360
elif pos.phi < -180:
pos.phi += 360
return pos
class TestUBCalculationWithWillmotStrategy_Si_5_5_12_FixedMuChi():
def setup_method(self):
hardware = Mock()
names = 'm', 'd', 'n', 'e', 'c', 'p'
hardware.get_axes_names.return_value = names
self.ubcalc = UBCalculation(hardware, SixCircle(),
UbCalculationNonPersister(),
YouUbCalcStrategy())
def testAgainstResultsFromJan_27_2010(self):
self.ubcalc.start_new('test')
self.ubcalc.set_lattice('Si_5_5_12', 7.68, 53.48, 75.63, 90, 90, 90)
self.ubcalc.add_reflection(
HKL0[0], HKL0[1], HKL0[2], willmott_to_you_fixed_mu_chi(REF0),
ENERGY, 'ref0', None)
self.ubcalc.add_reflection(
HKL1[0], HKL1[1], HKL1[2], willmott_to_you_fixed_mu_chi(REF1),
ENERGY, 'ref1', None)
self.ubcalc.calculate_UB()
print "U: ", self.ubcalc.U
print "UB: ", self.ubcalc.UB
matrixeq_(self.ubcalc.U, U_DIFFCALC)
class Test_Fixed_Mu_Chi(TestFixedMuEta):
def _configure_constraints(self):
self.constraints._constrained = {'alpha': 2 * TORAD, 'mu': 0,
'chi': pi / 2}
def _convert_willmott_pos(self, willmott_pos):
return willmott_to_you_fixed_mu_chi(willmott_pos)
def willmott_to_you_fixed_eta_chi(pos):
pos = YouPos(mu=pos.omegah,
delta=-pos.gamma,
nu=pos.delta,
eta=0,
chi=0,
phi=-pos.phi,
unit='DEG')
if pos.phi > 180:
pos.phi -= 360
elif pos.phi < -180:
pos.phi += 360
return pos
class Test_Fixed_Eta_Chi(TestFixedMuEta):
def _configure_constraints(self):
self.constraints._constrained = {'alpha': 2 * TORAD, 'eta': 0,
'chi': 0}
def _convert_willmott_pos(self, willmott_pos):
return willmott_to_you_fixed_eta_chi(willmott_pos)
def testHkl_2_19_32_found_orientation_setting(self):
SkipTest()
def testHkl_0_7_22_found_orientation_setting(self):
SkipTest()
def testHkl_2_19_32_calculated_from_DDIF(self):
SkipTest()
def testHkl_0_7_22_calculated_from_DDIF(self):
SkipTest()
def testHkl_2_m5_12_calculated_from_DDIF(self):
SkipTest()
def testHkl_2_19_32_calculated_predicted_with_diffcalc_and_found(self):
willpos = WillPos(delta=22.0332862, gamma=-4.0973643,
omegah=2, phi=--64.0273584)
self._check((2, 19, 32),
self._convert_willmott_pos(willpos),
{'alpha': 2})
def testHkl_0_7_22_calculated_predicted_with_diffcalc_and_found(self):
willpos = WillPos(delta=11.2572236, gamma=-2.9800571,
omegah=2, phi=-86.5634425)
self._check((0, 7, 22),
self._convert_willmott_pos(willpos),
{'alpha': 2})
def testHkl_2_m5_12_calculated_predicted_with_diffcalc_and_found(self):
willpos = WillPos(delta=5.3109941, gamma=-10.3716944,
omegah=2, phi=167.0041454)
self._check((2, -5, 12),
self._convert_willmott_pos(willpos),
{'alpha': 2})
# Primary and secondary reflections found with the help of DDIF on Diamond's
# i07 on Jan 28/29 2010
Pt531_HKL0 = -1.000, 1.000, 6.0000
Pt531_REF0 = WillPos(delta=9.3971025, gamma=16.1812303, omegah=2,
phi=-52.1392905)
Pt531_HKL1 = -2.000, -1.000, 7.0000
Pt531_REF1 = WillPos(delta=11.0126958, gamma=-11.8636128, omegah=2,
phi=40.3803393)
Pt531_REF12 = WillPos(delta=11.0126958, gamma=11.8636128, omegah=2,
phi=-121.2155975)
Pt531_HKL2 = 1, 1, 9
Pt531_REF2 = WillPos(delta=14.1881617, gamma=7.7585939, omegah=2,
phi=23.0203132)
Pt531_REF22 = WillPos(delta=14.1881617, gamma=-7.7585939, omegah=2,
phi=-183.465146)
Pt531_WAVELENGTH = 0.6358
# This is U matrix displayed by DDIF
U_FROM_DDIF = matrix([[-0.00312594, -0.00063417, 0.99999491],
[0.99999229, -0.00237817, 0.00312443],
[0.00237618, 0.99999697, 0.00064159]])
# This is the version that Diffcalc comes up with ( see following test)
Pt531_U_DIFFCALC = matrix([[-0.0023763, -0.9999970, -0.0006416],
[0.9999923, -0.0023783, 0.0031244],
[-0.0031259, -0.0006342, 0.9999949]])
class TestUBCalculationWithYouStrategy_Pt531_FixedMuChi():
def setup_method(self):
hardware = Mock()
names = 'm', 'd', 'n', 'e', 'c', 'p'
hardware.get_axes_names.return_value = names
self.ubcalc = UBCalculation(hardware, SixCircle(),
UbCalculationNonPersister(),
YouUbCalcStrategy())
def testAgainstResultsFromJan_28_2010(self):
self.ubcalc.start_new('test')
self.ubcalc.set_lattice('Pt531', 6.204, 4.806, 23.215, 90, 90, 49.8)
self.ubcalc.add_reflection(Pt531_HKL0[0], Pt531_HKL0[1], Pt531_HKL0[2],
willmott_to_you_fixed_mu_chi(Pt531_REF0),
12.39842 / Pt531_WAVELENGTH,
'ref0', None)
self.ubcalc.add_reflection(Pt531_HKL1[0], Pt531_HKL1[1], Pt531_HKL1[2],
willmott_to_you_fixed_mu_chi(Pt531_REF1),
12.39842 / Pt531_WAVELENGTH,
'ref1', None)
self.ubcalc.calculate_UB()
print "U: ", self.ubcalc.U
print "UB: ", self.ubcalc.UB
matrixeq_(self.ubcalc.U, Pt531_U_DIFFCALC)
class Test_Pt531_FixedMuChi(_BaseTest):
def setup_method(self):
_BaseTest.setup_method(self)
self._configure_constraints()
self.wavelength = Pt531_WAVELENGTH
CUT = CrystalUnderTest('Pt531', 6.204, 4.806, 23.215, 90, 90, 49.8)
B = CUT.B
self.UB = Pt531_U_DIFFCALC * B
self._configure_limits()
def _configure_constraints(self):
self.constraints._constrained = {'alpha': 2 * TORAD, 'mu': 0,
'chi': pi / 2}
def _configure_limits(self):
self.mock_hardware.set_lower_limit(NUNAME, None)
#self.mock_hardware.set_lower_limit('delta', None)
self.mock_hardware.set_upper_limit('delta', 90)
self.mock_hardware.set_lower_limit('mu', None)
self.mock_hardware.set_lower_limit('eta', None)
self.mock_hardware.set_lower_limit('chi', None)
def _convert_willmott_pos(self, willmott_pos):
return willmott_to_you_fixed_mu_chi(willmott_pos)
def _configure_ub(self):
self.mock_ubcalc.UB = self.UB
def _check(self, hkl, pos, virtual_expected={}, fails=False):
self._check_angles_to_hkl('', 999, 999, hkl, pos, self.wavelength,
virtual_expected)
if fails:
self._check_hkl_to_angles_fails('', 999, 999, hkl, pos,
self.wavelength, virtual_expected)
else:
self._check_hkl_to_angles('', 999, 999, hkl, pos, self.wavelength,
virtual_expected)
def testHkl_0_found_orientation_setting(self):
'''Check that the or0 reflection maps back to the assumed hkl'''
self.places = 1
self._check_angles_to_hkl('', 999, 999, Pt531_HKL0,
self._convert_willmott_pos(Pt531_REF0),
self.wavelength, {'alpha': 2})
def testHkl_1_found_orientation_setting(self):
'''Check that the or1 reflection maps back to the assumed hkl'''
self.places = 0
self._check_angles_to_hkl('', 999, 999, Pt531_HKL1,
self._convert_willmott_pos(Pt531_REF1),
self.wavelength, {'alpha': 2})
def testHkl_0_calculated_from_DDIF(self):
self.places = 7
pos_expected = self._convert_willmott_pos(Pt531_REF0)
self._check(Pt531_HKL0,
pos_expected,
{'alpha': 2})
def testHkl_1_calculated_from_DDIF(self):
self.places = 7
self._check(Pt531_HKL1,
self._convert_willmott_pos(Pt531_REF1),
{'alpha': 2})
def testHkl_2_calculated_from_DDIF(self):
self.places = 7
self._check(Pt531_HKL2,
self._convert_willmott_pos(Pt531_REF2),
{'alpha': 2})
def testHkl_2_m1_0_16(self):
self.places = 7
pos = WillPos(delta=25.7990976, gamma=-6.2413545, omegah=2,
phi=47.4624380)
# pos.phi -= 360
self._check((-1, 0, 16),
self._convert_willmott_pos(pos),
{'alpha': 2})
class Test_Pt531_Fixed_Mu_eta_(Test_Pt531_FixedMuChi):
def _configure_constraints(self):
self.constraints._constrained = {'alpha': 2 * TORAD, 'mu': -pi / 2,
'eta': 0}
def _convert_willmott_pos(self, willmott_pos):
return willmott_to_you_fixed_mu_eta(willmott_pos)
def testHkl_1_calculated_from_DDIF(self):
self.places = 7
self._check(Pt531_HKL1,
self._convert_willmott_pos(Pt531_REF12),
{'alpha': 2})
def testHkl_2_calculated_from_DDIF(self):
self.places = 7
self._check(Pt531_HKL2,
self._convert_willmott_pos(Pt531_REF22),
{'alpha': 2})
def testHkl_2_m1_0_16(self):
self.places = 7
pos = WillPos(delta=25.7990976, gamma=6.2413545, omegah=2,
phi=-47.4949600)
# pos.phi -= 360
self._check((-1, 0, 16),
self._convert_willmott_pos(pos),
{'alpha': 2})

View File

@@ -0,0 +1,575 @@
###
# Copyright 2008-2011 Diamond Light Source Ltd.
# This file is part of Diffcalc.
#
# Diffcalc is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Diffcalc is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Diffcalc. If not, see <http://www.gnu.org/licenses/>.
###
from nose.tools import eq_ # @UnresolvedImport
from mock import Mock
from diffcalc.hkl.you.constraints import YouConstraintManager
from diffcalc.util import DiffcalcException
from nose.tools import raises
from nose.tools import assert_raises # @UnresolvedImport
from diffcalc.hkl.you.constraints import NUNAME
import diffcalc.util
def joined(d1, d2):
d1.update(d2)
return d1
class TestConstraintManager:
def setup_method(self):
diffcalc.util.COLOURISE_TERMINAL_OUTPUT = False
self.hardware_monitor = Mock()
self.hardware_monitor.get_position.return_value = (1.,) * 6
self.hardware_monitor.get_axes_names.return_value = [
'mu', 'delta', NUNAME, 'eta', 'chi', 'phi']
self.cm = YouConstraintManager(self.hardware_monitor)
def test_init(self):
eq_(self.cm.all, {})
eq_(self.cm.detector, {})
eq_(self.cm.reference, {})
eq_(self.cm.sample, {})
eq_(self.cm.naz, {})
def test_build_display_table(self):
self.cm.constrain('qaz')
self.cm.constrain('alpha')
self.cm.constrain('eta')
self.cm.set_constraint('qaz', 1.234)
self.cm.set_constraint('eta', 99.)
print '\n'.join(self.cm.build_display_table_lines())
eq_(self.cm.build_display_table_lines(),
[' DET REF SAMP',
' ------ ------ ------',
' delta a_eq_b mu',
' %s o-> alpha --> eta' % NUNAME.ljust(6),
'--> qaz beta chi',
' naz psi phi',
' mu_is_%s' % NUNAME])
#"""
# DET REF SAMP Available:
# ====== ====== ======
# delta a_eq_b mu 3x samp: 80 of 80
# nu o-> alpha --> eta 2x samp and ref: chi & phi
#--> qaz beta chi mu & eta
# naz psi phi chi=90 & mu=0
# mu_is_nu 2x samp and det: 0 of 6
# 3x samp: 0 of 4
#"""[1:-1]
def test_unconstrain_okay(self):
eq_(self.cm.all, {})
self.cm.constrain('delta')
self.cm.constrain('mu')
eq_(self.cm.all, {'delta': None, 'mu': None})
eq_(self.cm.unconstrain('delta'), None)
eq_(self.cm.all, {'mu': None})
def test_clear_constraints(self):
self.cm.constrain('delta')
self.cm.constrain('mu')
self.cm.clear_constraints()
eq_(self.cm.all, {})
def test_unconstrain_bad(self):
eq_(self.cm.all, {})
eq_(self.cm.unconstrain('delta'), "Delta was not already constrained.")
def test_constrain_det(self, pre={}):
eq_(self.cm.all, pre)
eq_(self.cm.constrain('delta'), None)
eq_(self.cm.all, joined({'delta': None}, pre))
eq_(self.cm.constrain('delta'), 'Delta is already constrained.')
eq_(self.cm.all, joined({'delta': None}, pre))
eq_(self.cm.constrain('naz'), 'Delta constraint replaced.')
eq_(self.cm.all, joined({'naz': None}, pre))
eq_(self.cm.constrain('delta'), 'Naz constraint replaced.')
eq_(self.cm.all, joined({'delta': None}, pre))
def test_constrain_det_one_preexisting_ref(self):
self.cm.constrain('alpha')
self.test_constrain_det({'alpha': None})
def test_constrain_det_one_preexisting_samp(self):
self.cm.constrain('phi')
self.test_constrain_det({'phi': None})
def test_constrain_det_one_preexisting_samp_and_ref(self):
self.cm.constrain('alpha')
self.cm.constrain('phi')
self.test_constrain_det({'alpha': None, 'phi': None})
def test_constrain_det_two_preexisting_samp(self):
self.cm.constrain('chi')
self.cm.constrain('phi')
self.test_constrain_det({'chi': None, 'phi': None})
def test_constrain_det_three_preexisting_other(self):
self.cm.constrain('alpha')
self.cm.constrain('phi')
self.cm.constrain('chi')
try:
self.cm.constrain('delta')
assert False
except DiffcalcException, e:
eq_(e.args[0], (
"Delta could not be constrained. First un-constrain one of the"
"\nangles alpha, chi or phi (with 'uncon')"))
def test_constrain_det_three_preexisting_samp(self):
self.cm.constrain('phi')
self.cm.constrain('chi')
self.cm.constrain('eta')
try:
self.cm.constrain('delta')
assert False
except DiffcalcException, e:
eq_(e.args[0],
"Delta could not be constrained. First un-constrain one of the"
"\nangles eta, chi or phi (with 'uncon')")
def test_constrain_ref(self, pre={}):
eq_(self.cm.all, pre)
eq_(self.cm.constrain('alpha'), None)
eq_(self.cm.all, joined({'alpha': None}, pre))
eq_(self.cm.constrain('alpha'), 'Alpha is already constrained.')
eq_(self.cm.all, joined({'alpha': None}, pre))
eq_(self.cm.constrain('beta'), 'Alpha constraint replaced.')
eq_(self.cm.all, joined({'beta': None}, pre))
def test_constrain_ref_one_preexisting_det(self):
self.cm.constrain('delta')
self.test_constrain_ref({'delta': None})
def test_constrain_ref_one_preexisting_samp(self):
self.cm.constrain('phi')
self.test_constrain_ref({'phi': None})
def test_constrain_ref_one_preexisting_samp_and_det(self):
self.cm.constrain('delta')
self.cm.constrain('phi')
self.test_constrain_ref({'delta': None, 'phi': None})
def test_constrain_ref_two_preexisting_samp(self):
self.cm.constrain('chi')
self.cm.constrain('phi')
self.test_constrain_ref({'chi': None, 'phi': None})
def test_constrain_ref_three_preexisting_other(self):
self.cm.constrain('delta')
self.cm.constrain('phi')
self.cm.constrain('chi')
try:
self.cm.constrain('alpha'),
assert False
except DiffcalcException, e:
eq_(e.args[0],
"Alpha could not be constrained. First un-constrain one of the"
"\nangles delta, chi or phi (with 'uncon')")
def test_constrain_ref_three_preexisting_samp(self):
self.cm.constrain('phi')
self.cm.constrain('chi')
self.cm.constrain('eta')
try:
self.cm.constrain('delta')
assert False
except DiffcalcException, e:
eq_(e.args[0],
"Delta could not be constrained. First un-constrain one of the"
"\nangles eta, chi or phi (with 'uncon')")
def test_constrain_samp_when_one_free(self, pre={}):
eq_(self.cm.all, pre)
eq_(self.cm.constrain('phi'), None)
eq_(self.cm.all, joined({'phi': None}, pre))
eq_(self.cm.constrain('phi'), 'Phi is already constrained.')
eq_(self.cm.all, joined({'phi': None}, pre))
def test_constrain_samp_one_preexisting_samp(self):
self.cm.constrain('chi')
self.test_constrain_samp_when_one_free({'chi': None})
def test_constrain_samp_two_preexisting_samp(self):
self.cm.constrain('chi')
self.cm.constrain('eta')
self.test_constrain_samp_when_one_free({'chi': None, 'eta': None})
def test_constrain_samp_two_preexisting_other(self):
self.cm.constrain('delta')
self.cm.constrain('alpha')
self.test_constrain_samp_when_one_free({'delta': None, 'alpha': None})
def test_constrain_samp_two_preexisting_one_det(self):
self.cm.constrain('delta')
self.cm.constrain('eta')
self.test_constrain_samp_when_one_free({'delta': None, 'eta': None})
def test_constrain_samp_two_preexisting_one_ref(self):
self.cm.constrain('alpha')
self.cm.constrain('eta')
self.test_constrain_samp_when_one_free({'alpha': None, 'eta': None})
def test_constrain_samp_three_preexisting_only_one_samp(self):
self.cm.constrain('delta')
self.cm.constrain('alpha')
self.cm.constrain('eta')
eq_(self.cm.constrain('phi'), 'Eta constraint replaced.')
eq_(self.cm.all, {'delta': None, 'alpha': None, 'phi': None})
eq_(self.cm.constrain('phi'), 'Phi is already constrained.')
eq_(self.cm.all, {'delta': None, 'alpha': None, 'phi': None})
def test_constrain_samp_three_preexisting_two_samp_one_det(self):
self.cm.constrain('delta')
self.cm.constrain('eta')
self.cm.constrain('chi')
try:
self.cm.constrain('phi')
assert False
except DiffcalcException, e:
eq_(e.args[0],
"Phi could not be constrained. First un-constrain one of the"
"\nangles delta, eta or chi (with 'uncon')")
def test_constrain_samp_three_preexisting_two_samp_one_ref(self):
self.cm.constrain('alpha')
self.cm.constrain('eta')
self.cm.constrain('chi')
try:
self.cm.constrain('phi')
assert False
except DiffcalcException, e:
eq_(e.args[0],
"Phi could not be constrained. First un-constrain one of the"
"\nangles alpha, eta or chi (with 'uncon')")
def test_constrain_samp_three_preexisting_samp(self):
self.cm.constrain('mu')
self.cm.constrain('eta')
self.cm.constrain('chi')
try:
self.cm.constrain('phi')
assert False
except DiffcalcException, e:
eq_(e.args[0],
"Phi could not be constrained. First un-constrain one of the"
"\nangles mu, eta or chi (with 'uncon')")
def test_report_constraints_none(self):
eq_(self.cm.report_constraints_lines(),
['! 3 more constraints required'])
def test_report_constraints_one_with_value(self):
self.cm.constrain(NUNAME)
self.cm.set_constraint(NUNAME, 9.12343)
eq_(self.cm.report_constraints_lines(),
['! 2 more constraints required',
' %s : 9.1234' % NUNAME])
def test_report_constraints_one_with_novalue(self):
self.cm.constrain(NUNAME)
print '! 2 more constraints required\n! %s: ---' % NUNAME
eq_(self.cm.report_constraints_lines(),
['! 2 more constraints required',
'! %s : ---' % NUNAME])
def test_report_constraints_one_with_valueless(self):
self.cm.constrain('a_eq_b')
eq_(self.cm.report_constraints_lines(),
['! 2 more constraints required',
' a_eq_b'])
def test_report_constraints_one_with_two(self):
self.cm.constrain('naz')
self.cm.set_constraint('naz', 9.12343)
self.cm.constrain('a_eq_b')
eq_(self.cm.report_constraints_lines(),
['! 1 more constraint required',
' naz : 9.1234',
' a_eq_b'])
def test_report_constraints_one_with_three(self):
self.cm.constrain('naz')
self.cm.set_constraint('naz', 9.12343)
self.cm.constrain('a_eq_b')
self.cm.constrain('mu')
self.cm.set_constraint('mu', 9.12343)
eq_(self.cm.report_constraints_lines(),
[' naz : 9.1234',
' a_eq_b',
' mu : 9.1234'])
def _constrain(self, *args):
for con in args:
self.cm.constrain(con)
@raises(ValueError)
def test_is_implemented_invalid(self):
self._constrain('naz')
self.cm.is_current_mode_implemented()
# 1 samp
def test_is_implemented_1_samp_naz(self):
self._constrain('naz', 'alpha', 'mu')
eq_(self.cm.is_current_mode_implemented(), True)
def test_is_implemented_1_samp_det(self):
self._constrain('qaz', 'alpha', 'mu')
eq_(self.cm.is_current_mode_implemented(), True)
# 2 samp + ref
def test_is_implemented_2_samp_ref_mu_chi(self):
self._constrain('beta', 'mu', 'chi')
eq_(self.cm.is_current_mode_implemented(), True)
def test_is_implemented_2_samp_ref_mu90_chi0(self):
self._constrain('beta', 'mu', 'chi')
self.cm.set_constraint('mu', 0)
self.cm.set_constraint('chi', 90)
eq_(self.cm.is_current_mode_implemented(), True)
def test_is_implemented_2_samp_ref_mu_eta(self):
self._constrain('beta', 'mu', 'eta')
eq_(self.cm.is_current_mode_implemented(), True)
def test_is_implemented_2_samp_ref_mu_phi(self):
self._constrain('beta', 'mu', 'phi')
eq_(self.cm.is_current_mode_implemented(), False)
def test_is_implemented_2_samp_ref_eta_chi(self):
self._constrain('beta', 'eta', 'chi')
eq_(self.cm.is_current_mode_implemented(), True)
def test_is_implemented_2_samp_ref_eta_phi(self):
self._constrain('beta', 'eta', 'phi')
eq_(self.cm.is_current_mode_implemented(), False)
def test_is_implemented_2_samp_ref_chi_phi(self):
self._constrain('beta', 'chi', 'phi')
eq_(self.cm.is_current_mode_implemented(), True)
# 2 samp + det
def test_is_implemented_2_samp_det_mu_chi(self):
self._constrain('qaz', 'mu', 'chi')
eq_(self.cm.is_current_mode_implemented(), False)
def test_is_implemented_2_samp_det_mu_eta(self):
self._constrain('qaz', 'mu', 'eta')
eq_(self.cm.is_current_mode_implemented(), True)
def test_is_implemented_2_samp_det_mu_phi(self):
self._constrain('qaz', 'mu', 'phi')
eq_(self.cm.is_current_mode_implemented(), True)
def test_is_implemented_2_samp_det_eta_chi(self):
self._constrain('qaz', 'eta', 'chi')
eq_(self.cm.is_current_mode_implemented(), False)
def test_is_implemented_2_samp_det_eta_phi(self):
self._constrain('qaz', 'eta', 'phi')
eq_(self.cm.is_current_mode_implemented(), False)
def test_is_implemented_2_samp_det_chi_phi(self):
self._constrain('qaz', 'chi', 'phi')
eq_(self.cm.is_current_mode_implemented(), True)
# 3 samp
def test_is_implemented_3_samp_no_mu(self):
self._constrain('eta', 'chi', 'phi')
eq_(self.cm.is_current_mode_implemented(), True)
def test_is_implemented_3_samp_no_eta(self):
self._constrain('mu', 'chi', 'phi')
eq_(self.cm.is_current_mode_implemented(), False)
def test_is_implemented_3_samp_no_chi(self):
self._constrain('mu', 'eta', 'phi')
eq_(self.cm.is_current_mode_implemented(), False)
def test_is_implemented_3_samp_no_phi(self):
self._constrain('mu', 'eta', 'chi')
eq_(self.cm.is_current_mode_implemented(), False)
def test_set_fails(self):
try:
self.cm.set_constraint('not_a_constraint', object())
assert False
except DiffcalcException, e:
eq_(e.args[0], "Could not set not_a_constraint. This is not an "
"available constraint.")
try:
self.cm.set_constraint('delta', object())
assert False
except DiffcalcException, e:
eq_(e.args[0],
"Could not set delta. This is not currently constrained.")
self.cm.constrain('a_eq_b')
try:
self.cm.set_constraint('a_eq_b', object())
assert False
except DiffcalcException, e:
eq_(e.args[0],
"Could not set a_eq_b. This constraint takes no value.")
# self.cm.constrain('delta')
# self.cm.track('delta')
# try:
# self.cm.set('delta', object())
# assert False
# except DiffcalcException, e:
# eq_(e.args[0], ("Could not set delta as this constraint is "
# "configured to track its associated\n"
# "physical angle. First remove this tracking "
# "(use 'untrack delta')."))
def test_set(self):
#"%s: %s --> %f %s"
self.cm.constrain('alpha')
eq_(self.cm.set_constraint('alpha', 1.), 'alpha : --- --> 1.0')
eq_(self.cm.set_constraint('alpha', 2.), 'alpha : 1.0 --> 2.0')
# def test_track_fails(self):
# try:
# self.cm.track('not_a_constraint')
# assert False
# except DiffcalcException, e:
# eq_(e.args[0], "Could not track not_a_constraint as this is not "
# "an available constraint.")
#
# try:
# self.cm.track('delta')
# assert False
# except DiffcalcException, e:
# eq_(e.args[0],
# "Could not track delta as this is not currently constrained.")
#
# self.cm.constrain('a_eq_b')
# try:
# self.cm.track('a_eq_b')
# assert False
# except DiffcalcException, e:
# eq_(e.args[0],
# "Could not track a_eq_b as this constraint takes no value.")
#
# self.cm.constrain('alpha')
# try:
# self.cm.track('alpha')
# assert False
# except DiffcalcException, e:
# eq_(e.args[0], ("Could not configure alpha to track as this "
# "constraint is not associated with a\n"
# "physical angle."))
#
# def test_track(self):
# #"%s: %s --> %f %s"
# self.cm.constrain('delta')
# eq_(self.cm.track('delta'), 'delta : --- ~~> 1.0 (tracking)')
# eq_(self.cm.track('delta'), 'Delta was already configured to track.')
# self.cm.untrack('delta')
# self.cm.set_constraint('delta', 2.)
# eq_(self.cm.track('delta'), 'delta : 2.0 ~~> 1.0 (tracking)')
class TestConstraintManagerWithFourCircles:
def setup_method(self):
self.cm = YouConstraintManager(None, {NUNAME: 0, 'mu': 0})
def test_init(self):
eq_(self.cm.all, {NUNAME: 0, 'mu': 0})
eq_(self.cm.detector, {NUNAME: 0})
eq_(self.cm.reference, {})
eq_(self.cm.sample, {'mu': 0})
eq_(self.cm.naz, {})
def test_build_initial_display_table_with_fixed_detector(self):
self.cm = YouConstraintManager(None, {NUNAME: 0})
print self.cm.build_display_table_lines()
eq_(self.cm.build_display_table_lines(),
[' REF SAMP',
' ------ ------',
' a_eq_b mu',
' alpha eta',
' beta chi',
' psi phi'])
def test_build_initial_display_table_with_fixed_sample(self):
self.cm = YouConstraintManager(None, {'mu': 0})
print self.cm.build_display_table_lines()
eq_(self.cm.build_display_table_lines(),
[' DET REF SAMP',
' ------ ------ ------',
' delta a_eq_b eta',
' %s alpha chi' % NUNAME.ljust(6),
' qaz beta phi',
' naz psi'])
def test_build_initial_display_table_for_four_circle(self):
self.cm = YouConstraintManager(None, {'mu': 0, NUNAME: 0})
print self.cm.build_display_table_lines()
eq_(self.cm.build_display_table_lines(),
[' REF SAMP',
' ------ ------',
' a_eq_b eta',
' alpha chi',
' beta phi',
' psi'])
def test_constrain_fixed_detector_angle(self):
assert_raises(DiffcalcException, self.cm.constrain, 'delta')
assert_raises(DiffcalcException, self.cm.constrain, NUNAME)
assert_raises(DiffcalcException, self.cm.constrain, 'naz')
assert_raises(DiffcalcException, self.cm.constrain, 'qaz')
def test_unconstrain_fixed_detector_angle(self):
assert_raises(DiffcalcException, self.cm.unconstrain, 'delta')
assert_raises(DiffcalcException, self.cm.unconstrain, NUNAME)
assert_raises(DiffcalcException, self.cm.unconstrain, 'naz')
assert_raises(DiffcalcException, self.cm.unconstrain, 'qaz')
def test_set_constrain_fixed_detector_angle(self):
assert_raises(DiffcalcException, self.cm.set_constraint, 'delta', 0)
assert_raises(DiffcalcException, self.cm.set_constraint, NUNAME, 0)
assert_raises(DiffcalcException, self.cm.set_constraint, 'naz', 0)
assert_raises(DiffcalcException, self.cm.set_constraint, 'qaz', 0)
@raises(DiffcalcException)
def test_constrain_fixed_sample_angle(self):
self.cm.constrain('mu')
@raises(DiffcalcException)
def test_unconstrain_fixed_sample_angle(self):
self.cm.unconstrain('mu')
@raises(DiffcalcException)
def test_set_constrain_fixed_sample_angle(self):
self.cm.set_constraint('mu', 0)

View File

@@ -0,0 +1,96 @@
###
# Copyright 2008-2011 Diamond Light Source Ltd.
# This file is part of Diffcalc.
#
# Diffcalc is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Diffcalc is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Diffcalc. If not, see <http://www.gnu.org/licenses/>.
###
from mock import Mock, call
from diffcalc import settings
import diffcalc
from test.diffcalc.test_hardware import SimpleHardwareAdapter
diffcalc.util.DEBUG = True
hkl = None
def setup_module():
global hkl
settings.hardware=SimpleHardwareAdapter([])
settings.geometry=Mock()
settings.angles_to_hkl_function=Mock()
settings.ubcalc_strategy=Mock()
settings.geometry.fixed_constraints = {}
from diffcalc.hkl.you import hkl
reload(hkl)
hkl.hklcalc = Mock()
hkl.hklcalc.constraints.report_constraints_lines.return_value = ['report1', 'report2']
def test_con_with_1_constraint():
hkl.con('cona')
hkl.hklcalc.constraints.constrain.assert_called_with('cona')
def test_con_with_1_constraint_with_value():
hkl.con('cona', 123)
hkl.hklcalc.constraints.constrain.assert_called_with('cona')
hkl.hklcalc.constraints.set_constraint.assert_called_with('cona', 123)
def test_con_with_3_constraints():
hkl.con('cona', 'conb', 'conc')
hkl.hklcalc.constraints.clear_constraints.assert_called()
calls = [call('cona'), call('conb'), call('conc')]
hkl.hklcalc.constraints.constrain.assert_has_calls(calls)
def test_con_with_3_constraints_first_val():
hkl.con('cona', 1, 'conb', 'conc')
hkl.hklcalc.constraints.clear_constraints.assert_called()
calls = [call('cona'), call('conb'), call('conc')]
hkl.hklcalc.constraints.constrain.assert_has_calls(calls)
hkl.hklcalc.constraints.set_constraint.assert_called_with('cona', 1)
def test_con_with_3_constraints_second_val():
hkl.con('cona', 'conb', 2, 'conc')
hkl.hklcalc.constraints.clear_constraints.assert_called()
calls = [call('cona'), call('conb'), call('conc')]
hkl.hklcalc.constraints.constrain.assert_has_calls(calls)
hkl.hklcalc.constraints.set_constraint.assert_called_with('conb', 2)
def test_con_with_3_constraints_third_val():
hkl.con('cona', 'conb', 'conc', 3)
hkl.hklcalc.constraints.clear_constraints.assert_called()
calls = [call('cona'), call('conb'), call('conc')]
hkl.hklcalc.constraints.constrain.assert_has_calls(calls)
hkl.hklcalc.constraints.set_constraint.assert_called_with('conc', 3)
def test_con_with_3_constraints_all_vals():
hkl.con('cona', 1, 'conb', 2, 'conc', 3)
hkl.hklcalc.constraints.clear_constraints.assert_called()
calls = [call('cona'), call('conb'), call('conc')]
hkl.hklcalc.constraints.constrain.assert_has_calls(calls)
calls = [call('cona', 1), call('conb', 2), call('conc', 3)]
hkl.hklcalc.constraints.set_constraint.assert_has_calls(calls)
def test_con_messages_and_help_visually():
hkl.con()
print "**"
print hkl.con.__doc__
def test_con_message_display_whenn_selecting_an_unimplmented_mode():
hkl.hklcalc.constraints.is_fully_constrained.return_value = True
hkl.hklcalc.constraints.is_current_mode_implemented.return_value = False
hkl.con('phi', 'chi', 'eta')