def test_makeAuxReflection(): lattice = Cell(12.32, 3.32, 9.8, 92, 91, 120) B = lattice.calculateBMatrix() E = 22 ki = energyToK(E) h, k, ll = 1, 0, -1 qm = np.linalg.norm(np.dot(B, [h, k, ll])) r1 = tasReflection(ki=ki, kf=ki, qh=h, qk=k, ql=ll, qm=qm, monochromator_two_theta=74.2, a3=12.2, sgu=0.0, sgl=0.0, analyzer_two_theta=74.2) tt = calcTwoTheta(B, r1, -1) r1.angles.sample_two_theta = tt r2 = makeAuxReflection(B, r1, -1.0, [3, 1, -1]) assert (np.isclose(r2.a3, 35.875022341)) assert (np.isclose(r2.sample_two_theta, -64.129182823)) try: r2 = makeAuxReflection(B, r1, -1.0, [30, 1, -1]) assert False except RuntimeError: assert True
def test_Cell_init(): cell = Cell() # Default cell object for length in ['a', 'b', 'c']: assert (getattr(cell, length) == 1.0) for length in ['alpha', 'beta', 'gamma']: assert (getattr(cell, length) == 90.0) nonStandard = { 'a': 10, 'b': 11, 'c': 0.2, 'alpha': 60, 'beta': 90.1, 'gamma': 120 } nonStandardCell = Cell(**nonStandard) for key, value in nonStandard.items(): assert (getattr(nonStandardCell, key) == value) cellString = str(cell) wantedString = "cell.Cell(a=1.0, b=1.0, c=1.0, " \ "alpha=90.0, beta=90.0, gamma=90.0)" assert (cellString == wantedString)
def test_tasAngleBetweenReflections(): lattice = Cell(6.11, 6.11, 11.35, 90.0, 90.0, 120.0) B = lattice.calculateBMatrix() r1 = tasReflection(qh=1.0, qk=-2.0, ql=0.0) r2 = tasReflection(qh=-1.5, qk=1.1, ql=1.0) angle = tasAngleBetweenReflections(B, r1, r2) assert (np.isclose(angle, 131.993879212))
def test_tasReflectionToQC(): r = tasQEPosition(ki=1.5, kf=2.5, qh=1, qk=2, ql=3, qm=2.0) lattice = Cell(6.11, 6.11, 11.35, 90.0, 90.0, 120.0) B = lattice.calculateBMatrix() Q = tasReflectionToQC(r, B) assert (np.all( np.isclose( Q, np.array([+0.377970716, +0.327332242, +0.264317181]) * 2 * np.pi)))
def test_calcTwoTheta(): lattice = Cell(6.11, 6.11, 11.35, 90.0, 90.0, 120.0) B = lattice.calculateBMatrix() hkl = [[0, 0, 0], [1, 0, 0], [0, 1, 0], [-1, 0, 0], [0, -1, 0], [0, 0, 1], [0, 0, -1], [2, -1, 3]] result = [ 0.0, 44.93975435373514, 44.93975435373514, 44.93975435373514, 44.93975435373514, 20.52777682289506, 20.52777682289506, 116.61092637820377 ] for (h, k, ll), res in zip(hkl, result): qm = np.linalg.norm(np.dot(B, [h, k, ll])) r1 = tasReflection(ki=1.553424, kf=1.553424, qh=h, qk=k, ql=ll, qm=qm, monochromator_two_theta=74.2, a3=0.0, sample_two_theta=-200, sgu=0.0, sgl=0.0, analyzer_two_theta=74.2) tt = calcTwoTheta(B, r1, 1) if not np.isclose(tt, res): print('Two theta for ({},{},{})={} ' 'but expected {}'.format(h, k, ll, tt, res)) assert False try: h, k, ll = 10, -10, 10 qm = np.linalg.norm(np.dot(B, [h, k, ll])) # HKL is far out of reach r1 = tasReflection(ki=1.553424, kf=1.553424, qh=h, qk=k, ql=ll, qm=qm, monochromator_two_theta=74.2, a3=0.0, sample_two_theta=-200, sgu=0.0, sgl=0.0, analyzer_two_theta=74.2) tt = calcTwoTheta(B, r1, 1) assert False except RuntimeError: assert True
def test_tasReflectionToHC(): lattice = Cell(12.32, 3.32, 9.8, 93, 45, 120) B = lattice.calculateBMatrix() energy = 5.00 K = energyToK(energy) reflection = tasReflection(ki=K, kf=K, qh=2, qk=0, ql=-1) hc = tasReflectionToHC(reflection, B) result = np.array([+0.434561180, -0.005347733, -0.102040816]) * 2 * np.pi assert (np.all(np.isclose(hc, result)))
def test_calcPlaneNormal(): lattice = Cell(6.11, 6.11, 11.35, 90.0, 90.0, 120.0) B = lattice.calculateBMatrix() HKL1 = [[1, 0, 0], [1, 1, 0], [1, 0, 0]] HKL2 = [[0, 1, 0], [1, -1, 0], [0, 0, 1]] result = [[0, 0, 1], [0, 0, 1], [0, 0, 1]] for hkl1, hkl2, res in zip(HKL1, HKL2, result): qm = np.linalg.norm(np.dot(B, hkl1)) r1 = tasReflection(ki=1.553424, kf=1.553424, qh=hkl1[0], qk=hkl1[1], ql=hkl1[2], qm=qm, monochromator_two_theta=74.2, a3=0.0, sample_two_theta=-200, sgu=0.0, sgl=0.0, analyzer_two_theta=74.2) tt = calcTwoTheta(B, r1, 1) r1.angles.sample_two_theta = tt r1.angles.a3 = calcTheta(r1.ki, r1.kf, tt) print('r1.a3: ', r1.a3) qm = np.linalg.norm(np.dot(B, hkl2)) r2 = tasReflection(ki=1.553424, kf=1.553424, qh=hkl2[0], qk=hkl2[1], ql=hkl2[2], qm=qm, monochromator_two_theta=74.2, a3=0.0, sample_two_theta=-200, sgu=0.0, sgl=0.0, analyzer_two_theta=74.2) tt = calcTwoTheta(B, r2, 1) r2.angles.sample_two_theta = tt r2.angles.a3 = calcTheta(r2.ki, r2.kf, tt) +\ tasAngleBetweenReflections(B, r1, r2) print('r2.a3: ', r2.a3) planeNormal = calcPlaneNormal(r1, r2) if not np.all(np.isclose(planeNormal, res)): print('Plane normal for {} and {} = ' '{} but expected {}'.format(hkl1, hkl2, planeNormal, res)) assert False
def test_Cell_BMatrix(): # B matrix for 6.11, 6.11, 11.35, 90.0, 90.0, 120.0 as calculated by six BFromSix = np.array([[+0.188985358, +0.094492679, +0.000000000], [+0.000000000, +0.163666121, +0.000000000], [+0.000000000, +0.000000000, +0.088105727] ]) * 2 * np.pi lattice = Cell(6.11, 6.11, 11.35, 90.0, 90.0, 120.0) B = lattice.calculateBMatrix() print(B) print(BFromSix) assert (np.all(np.isclose(B, BFromSix, atol=1e-9)))
def test_angle_between_reflections(): cell = Cell(9.663, 9.663, 9.663, 81.496, 81.496, 81.496) r1 = {'h': 1., 'k': 0., 'l': 0.} r2 = {'h': 0., 'k': 1., 'l': 0.} B = calculateBMatrix(cell) angle = angleBetweenReflections(B, r1, r2) assert(abs(np.rad2deg(angle) - 97.40) < .01)
def test_Cell_error(): cell = Cell(-10, -0.1, -0.2, -0., -0., -90) try: import warnings with warnings.catch_warnings(): warnings.simplefilter("ignore") # Ignore the warnings temporary reciprocal = directToReciprocalLattice(cell) reciprocal.a = 26. # Only to satisfy pylint, flake8 assert False # pragma: no cover except AttributeError as e: str(e) assert True
def test_Cell_CellFromUBMatrix(): latticeCostants = 20 * np.random.rand(3) + 1.0 angles = 90 * np.random.rand(3) # To ensure possitive volumen a = Cosd(angles[0]) b = Cosd(angles[1]) lowerLimit = a * b - np.sqrt((a**2 - 1) * (b**2 - 1)) upperLimit = np.sqrt((a**2 - 1) * (b**2 - 1)) + a * b angles[2] = Acosd(0.5 * (lowerLimit + upperLimit)) arguments = list(latticeCostants) + list(angles) lattice = Cell(*arguments) B = lattice.calculateBMatrix() lattice2 = Cell(UB=B) print(lattice) print(lattice2) assert (lattice == lattice2)
def test_ub_nb_calc(): cell = Cell(9.663, 9.663, 9.663, 81.496, 81.496, 81.496) r1 = {'h': 1., 'k': -2., 'l': 1, 'gamma': 13.60, 'om': -102.52, 'nu': 12.40} r2 = {'h': 1., 'k': 1., 'l': 1., 'gamma': 10.62179, 'om': -14.005692, 'nu': 0.84147} ub_expected = np.array([[0.0211, 0.0773564, 0.04842], [-.1007840, 0.043792, 0.00344], [-.0225, -.0568516, .09368]], dtype='float64') ub = calcNBUBFromCellAndReflections(cell, r1, r2) for calc, exp in zip(np.nditer(ub), np.nditer(ub_expected)): assert(abs(calc - exp) < .001)
def test_ub_calc(): cell = Cell(5.402, 5.402, 12.3228) r1 = {'h': 2., 'k': 2., 'l': 0, 'stt': 35.8, 'om': 17.90, 'chi': 180.642, 'phi': 86.229} r2 = {'h': 0., 'k': 0., 'l': 3., 'stt': 16.498, 'om': 8.249, 'chi': 268.331, 'phi': 333.714} ub_expected = np.array([[.1215666, -.138694, -0.0021278], [-.1386887, -.121654, .0010515], [-.0049867, .0020612, -.081156]], dtype='float64') ub = calcUBFromCellAndReflections(cell, r1, r2) for calc, exp in zip(np.nditer(ub), np.nditer(ub_expected)): assert(abs(calc - exp) < .001)
def test_Cell_reciprocal(): # The default cell is equal to its reciprocal with # lattice vectors multiplied with 1/(2*pi) defaultCell = Cell() reciprocal = directToReciprocalLattice(defaultCell) for length in ['a', 'b', 'c']: assert (np.all(np.isclose(getattr(reciprocal, length), 2 * np.pi))) for length in ['alpha', 'beta', 'gamma']: assert (np.all(np.isclose(getattr(reciprocal, length), 90.0))) # Back and forth is the same defaultCell2 = reciprocal.reciprocalToDirectLattice() assert defaultCell == defaultCell2
def test_calcTasQAngles(): # SeCuO3 latticeSeCuo = Cell(7.725, 8.241, 8.502, 90.0, 99.16, 90.0) Ei = 5.0000076 ki = energyToK(Ei) Ef = 4.9221945 kf = energyToK(Ef) r1 = tasReflection(ki=ki, kf=kf, qh=2, qk=1, ql=0, a3=47.841908, sample_two_theta=-71.840689064, sgl=0, sgu=0) r2 = tasReflection(ki=ki, kf=kf, qh=2, qk=-1, ql=0, a3=-1.819579944281713, sample_two_theta=-71.840689064, sgl=0, sgu=0) UB = calcTasUBFromTwoReflections(latticeSeCuo, r1, r2) planeNormal = calcPlaneNormal(r1, r2) ss = -1 a3Off = 0.0 for (h, k, l), a3, a4 in zip([[r1.qh, r1.qk, r1.ql], [r2.qh, r2.qk, r2.ql]], [r1.a3, r2.a3], [r1.sample_two_theta, r2.sample_two_theta]): R = tasReflection(ki=ki, kf=kf, qh=h, qk=k, ql=l) qm = np.linalg.norm(np.dot(UB, [h, k, l])) qe = tasQEPosition(ki, kf, R.qh, R.qk, R.ql, qm=qm) angles = calcTasQAngles(UB, planeNormal, ss, a3Off, qe) assert (np.isclose(a3, angles.a3)) assert (np.isclose(a4, angles.sample_two_theta))
def directToReciprocalLattice(cell): """ caclulate the reciprocal lattice from the direct one """ reciprocal = Cell() alfa = np.deg2rad(cell.alpha) beta = np.deg2rad(cell.beta) gamma = np.deg2rad(cell.gamma) cos_alfa = cos(alfa) cos_beta = cos(beta) cos_gamma = cos(gamma) sin_alfa = sin(alfa) sin_beta = sin(beta) sin_gamma = sin(gamma) reciprocal.alpha = \ acos((cos_beta * cos_gamma - cos_alfa) / sin_beta / sin_gamma) reciprocal.beta = \ acos((cos_alfa * cos_gamma - cos_beta) / sin_alfa / sin_gamma) reciprocal.gamma =\ acos((cos_alfa * cos_beta - cos_gamma) / sin_alfa / sin_beta) ad = cell.a bd = cell.b cd = cell.c arg = 1 + 2 * cos_alfa * cos_beta * cos_gamma - cos_alfa * \ cos_alfa - cos_beta * cos_beta - cos_gamma * cos_gamma if arg < 0.0: return None vol = ad * bd * cd * sqrt(arg) reciprocal.a = bd * cd * sin_alfa / vol reciprocal.b = ad * cd * sin_beta / vol reciprocal.c = bd * ad * sin_gamma / vol return reciprocal
def getCell(self): return Cell(self.a, self.b, self.c, self.alpha, self.beta, self.gamma)
def test_calcTasUBFromTwoReflections(): lattices = [] r1s = [] r2s = [] UBs = [] # YMnO3, Eu latticeYMnO3 = Cell(6.11, 6.11, 11.35, 90.0, 90.0, 120.0) Ei = 5.0 ki = energyToK(Ei) Ef = 4.96 kf = energyToK(Ef) r1 = tasReflection(ki=ki, kf=kf, qh=0, qk=-1, ql=0, a3=-60, sample_two_theta=-45.23, sgl=0, sgu=0) r2 = tasReflection(ki=ki, kf=kf, qh=1, qk=0, ql=0, a3=0, sample_two_theta=-45.23, sgl=0, sgu=0) UB = np.array([[0.023387708, -0.150714153, 0.0], [-0.187532612, -0.114020655, -0.0], [0.0, -0.0, -0.088105727]]) lattices.append(latticeYMnO3) r1s.append(r1) r2s.append(r2) UBs.append(UB) # PbTi latticePbTi = Cell(9.556, 9.556, 7.014, 90.0, 90.0, 90.0) Ei = 5.0000076 ki = energyToK(Ei) Ef = 4.924756 kf = energyToK(Ef) r1 = tasReflection(ki=ki, kf=kf, qh=0, qk=0, ql=2, a3=74.2, sample_two_theta=-71.1594, sgl=0, sgu=0) r2 = tasReflection(ki=ki, kf=kf, qh=1, qk=1, ql=2, a3=41.33997, sample_two_theta=-81.6363, sgl=0, sgu=0) UB = np.array([[0.06949673, 0.0694967, -0.048957292], [-0.025409263, -0.025409255, -0.13390279], [-0.07399609, 0.07399613, -4.2151984E-9]]) lattices.append(latticePbTi) r1s.append(r1) r2s.append(r2) UBs.append(UB) # SecuO3 latticeSeCuo = Cell(7.725, 8.241, 8.502, 90.0, 99.16, 90.0) Ei = 5.0000076 ki = energyToK(Ei) Ef = 4.9221945 kf = energyToK(Ef) r1 = tasReflection(ki=ki, kf=kf, qh=2, qk=1, ql=0, a3=47.841908, sample_two_theta=-72.0, sgl=0, sgu=0) r2 = tasReflection(ki=ki, kf=kf, qh=2, qk=-1, ql=0, a3=-1.8000551, sample_two_theta=-72.0, sgl=0, sgu=0) UB = np.array([[0.066903256, -0.10436039, 0.009677114], [-0.11276933, -0.061914437, -0.016311338], [-0.0, 0.0, -0.11761938]]) lattices.append(latticeSeCuo) r1s.append(r1) r2s.append(r2) UBs.append(UB) # SICS cellSX = Cell(9.297644, 3.032419, 10.769930, 90., 90., 90.) ki = energyToK(5.) r1 = tasReflection(ki=ki, kf=ki, qh=2, qk=0, ql=0, a3=-21.92, sample_two_theta=-51.57, sgu=.057, sgl=-.844) r2 = tasReflection(ki=ki, kf=ki, qh=0, qk=1, ql=0, a3=52.03, sample_two_theta=-83.659, sgu=.377, sgl=.025) UB = np.array([[0.6741724, -.13984819, 0.00833411], [-0.04554178, -2.06723436, -0.0044177], [0.00997698, 0.01365775, -0.5833245]]) UB /= 2. * np.pi lattices.append(cellSX) r1s.append(r1) r2s.append(r2) UBs.append(UB) for lat, r1, r2, UBSix in zip(lattices, r1s, r2s, UBs): UB = calcTasUBFromTwoReflections(lat, r1, r2) assert (np.all(np.isclose(UB, UBSix * 2 * np.pi, atol=1e-6)))