|
17 | 17 | import pickle
|
18 | 18 | import unittest
|
19 | 19 |
|
20 |
| -import numpy |
21 |
| - |
22 |
| -from .utils import _msg_nostructure, has_structure |
23 |
| - |
24 |
| -# Global variables to be assigned in setUp |
25 |
| -Atom = Lattice = Structure = DiffpyStructureParSet = None |
26 |
| - |
27 |
| -# ---------------------------------------------------------------------------- |
28 |
| - |
29 |
| - |
30 |
| -@unittest.skipUnless(has_structure, _msg_nostructure) |
31 |
| -class TestParameterAdapter(unittest.TestCase): |
32 |
| - |
33 |
| - def setUp(self): |
34 |
| - global Atom, Lattice, Structure, DiffpyStructureParSet |
35 |
| - from diffpy.srfit.structure.diffpyparset import DiffpyStructureParSet |
36 |
| - from diffpy.structure import Atom, Lattice, Structure |
37 |
| - |
38 |
| - return |
39 |
| - |
40 |
| - def testDiffpyStructureParSet(self): |
41 |
| - """Test the structure conversion.""" |
42 |
| - |
43 |
| - a1 = Atom("Cu", xyz=numpy.array([0.0, 0.1, 0.2]), Uisoequiv=0.003) |
44 |
| - a2 = Atom("Ag", xyz=numpy.array([0.3, 0.4, 0.5]), Uisoequiv=0.002) |
45 |
| - l = Lattice(2.5, 2.5, 2.5, 90, 90, 90) |
46 |
| - |
47 |
| - dsstru = Structure([a1, a2], l) |
48 |
| - # Structure makes copies |
49 |
| - a1 = dsstru[0] |
50 |
| - a2 = dsstru[1] |
51 |
| - |
52 |
| - s = DiffpyStructureParSet("CuAg", dsstru) |
53 |
| - |
54 |
| - self.assertEqual(s.name, "CuAg") |
55 |
| - |
56 |
| - def _testAtoms(): |
57 |
| - # Check the atoms thoroughly |
58 |
| - self.assertEqual(a1.element, s.Cu0.element) |
59 |
| - self.assertEqual(a2.element, s.Ag0.element) |
60 |
| - self.assertEqual(a1.Uisoequiv, s.Cu0.Uiso.getValue()) |
61 |
| - self.assertEqual(a2.Uisoequiv, s.Ag0.Uiso.getValue()) |
62 |
| - self.assertEqual(a1.Bisoequiv, s.Cu0.Biso.getValue()) |
63 |
| - self.assertEqual(a2.Bisoequiv, s.Ag0.Biso.getValue()) |
64 |
| - for i in range(1, 4): |
65 |
| - for j in range(i, 4): |
66 |
| - uijstru = getattr(a1, "U%i%i" % (i, j)) |
67 |
| - uij = getattr(s.Cu0, "U%i%i" % (i, j)).getValue() |
68 |
| - uji = getattr(s.Cu0, "U%i%i" % (j, i)).getValue() |
69 |
| - self.assertEqual(uijstru, uij) |
70 |
| - self.assertEqual(uijstru, uji) |
71 |
| - bijstru = getattr(a1, "B%i%i" % (i, j)) |
72 |
| - bij = getattr(s.Cu0, "B%i%i" % (i, j)).getValue() |
73 |
| - bji = getattr(s.Cu0, "B%i%i" % (j, i)).getValue() |
74 |
| - self.assertEqual(bijstru, bij) |
75 |
| - self.assertEqual(bijstru, bji) |
76 |
| - |
77 |
| - self.assertEqual(a1.xyz[0], s.Cu0.x.getValue()) |
78 |
| - self.assertEqual(a1.xyz[1], s.Cu0.y.getValue()) |
79 |
| - self.assertEqual(a1.xyz[2], s.Cu0.z.getValue()) |
80 |
| - return |
81 |
| - |
82 |
| - def _testLattice(): |
83 |
| - |
84 |
| - # Test the lattice |
85 |
| - self.assertEqual(dsstru.lattice.a, s.lattice.a.getValue()) |
86 |
| - self.assertEqual(dsstru.lattice.b, s.lattice.b.getValue()) |
87 |
| - self.assertEqual(dsstru.lattice.c, s.lattice.c.getValue()) |
88 |
| - self.assertEqual(dsstru.lattice.alpha, s.lattice.alpha.getValue()) |
89 |
| - self.assertEqual(dsstru.lattice.beta, s.lattice.beta.getValue()) |
90 |
| - self.assertEqual(dsstru.lattice.gamma, s.lattice.gamma.getValue()) |
91 |
| - |
92 |
| - _testAtoms() |
93 |
| - _testLattice() |
94 |
| - |
95 |
| - # Now change some values from the diffpy Structure |
96 |
| - a1.xyz[1] = 0.123 |
97 |
| - a1.U11 = 0.321 |
98 |
| - a1.B32 = 0.111 |
99 |
| - dsstru.lattice.setLatPar(a=3.0, gamma=121) |
100 |
| - _testAtoms() |
101 |
| - _testLattice() |
102 |
| - |
103 |
| - # Now change values from the srfit DiffpyStructureParSet |
104 |
| - s.Cu0.x.setValue(0.456) |
105 |
| - s.Cu0.U22.setValue(0.441) |
106 |
| - s.Cu0.B13.setValue(0.550) |
107 |
| - d = dsstru.lattice.dist(a1.xyz, a2.xyz) |
108 |
| - s.lattice.b.setValue(4.6) |
109 |
| - s.lattice.alpha.setValue(91.3) |
110 |
| - _testAtoms() |
111 |
| - _testLattice() |
112 |
| - # Make sure the distance changed |
113 |
| - self.assertNotEqual(d, dsstru.lattice.dist(a1.xyz, a2.xyz)) |
| 20 | +import numpy as np |
| 21 | +import pytest |
| 22 | + |
| 23 | +from diffpy.srfit.structure.diffpyparset import DiffpyStructureParSet |
| 24 | + |
| 25 | + |
| 26 | +def testDiffpyStructureParSet(diffpy_structure_available): |
| 27 | + """Test the structure conversion.""" |
| 28 | + if not diffpy_structure_available: |
| 29 | + pytest.skip("diffpy.structure package not available") |
| 30 | + from diffpy.structure import Atom, Lattice, Structure |
| 31 | + |
| 32 | + a1 = Atom("Cu", xyz=np.array([0.0, 0.1, 0.2]), Uisoequiv=0.003) |
| 33 | + a2 = Atom("Ag", xyz=np.array([0.3, 0.4, 0.5]), Uisoequiv=0.002) |
| 34 | + lattice = Lattice(2.5, 2.5, 2.5, 90, 90, 90) |
| 35 | + |
| 36 | + dsstru = Structure([a1, a2], lattice) |
| 37 | + # Structure makes copies |
| 38 | + a1 = dsstru[0] |
| 39 | + a2 = dsstru[1] |
| 40 | + |
| 41 | + s = DiffpyStructureParSet("CuAg", dsstru) |
| 42 | + |
| 43 | + assert s.name == "CuAg" |
| 44 | + |
| 45 | + def _testAtoms(): |
| 46 | + # Check the atoms thoroughly |
| 47 | + assert a1.element == s.Cu0.element |
| 48 | + assert a2.element == s.Ag0.element |
| 49 | + assert a1.Uisoequiv == s.Cu0.Uiso.getValue() |
| 50 | + assert a2.Uisoequiv == s.Ag0.Uiso.getValue() |
| 51 | + assert a1.Bisoequiv == s.Cu0.Biso.getValue() |
| 52 | + assert a2.Bisoequiv == s.Ag0.Biso.getValue() |
| 53 | + for i in range(1, 4): |
| 54 | + for j in range(i, 4): |
| 55 | + uijstru = getattr(a1, "U%i%i" % (i, j)) |
| 56 | + uij = getattr(s.Cu0, "U%i%i" % (i, j)).getValue() |
| 57 | + uji = getattr(s.Cu0, "U%i%i" % (j, i)).getValue() |
| 58 | + assert uijstru == uij |
| 59 | + assert uijstru == uji |
| 60 | + bijstru = getattr(a1, "B%i%i" % (i, j)) |
| 61 | + bij = getattr(s.Cu0, "B%i%i" % (i, j)).getValue() |
| 62 | + bji = getattr(s.Cu0, "B%i%i" % (j, i)).getValue() |
| 63 | + assert bijstru == bij |
| 64 | + assert bijstru == bji |
| 65 | + |
| 66 | + assert a1.xyz[0] == s.Cu0.x.getValue() |
| 67 | + assert a1.xyz[1] == s.Cu0.y.getValue() |
| 68 | + assert a1.xyz[2] == s.Cu0.z.getValue() |
114 | 69 | return
|
115 | 70 |
|
116 |
| - def test___repr__(self): |
117 |
| - """Test representation of DiffpyStructureParSet objects.""" |
118 |
| - lat = Lattice(3, 3, 2, 90, 90, 90) |
119 |
| - atom = Atom("C", [0, 0.2, 0.5]) |
120 |
| - stru = Structure([atom], lattice=lat) |
121 |
| - dsps = DiffpyStructureParSet("dsps", stru) |
122 |
| - self.assertEqual(repr(stru), repr(dsps)) |
123 |
| - self.assertEqual(repr(lat), repr(dsps.lattice)) |
124 |
| - self.assertEqual(repr(atom), repr(dsps.atoms[0])) |
125 |
| - return |
126 |
| - |
127 |
| - def test_pickling(self): |
128 |
| - """Test pickling of DiffpyStructureParSet.""" |
129 |
| - stru = Structure([Atom("C", [0, 0.2, 0.5])]) |
130 |
| - dsps = DiffpyStructureParSet("dsps", stru) |
131 |
| - data = pickle.dumps(dsps) |
132 |
| - dsps2 = pickle.loads(data) |
133 |
| - self.assertEqual(1, len(dsps2.atoms)) |
134 |
| - self.assertEqual(0.2, dsps2.atoms[0].y.value) |
135 |
| - return |
| 71 | + def _testLattice(): |
| 72 | + |
| 73 | + # Test the lattice |
| 74 | + assert dsstru.lattice.a == s.lattice.a.getValue() |
| 75 | + assert dsstru.lattice.b == s.lattice.b.getValue() |
| 76 | + assert dsstru.lattice.c == s.lattice.c.getValue() |
| 77 | + assert dsstru.lattice.alpha == s.lattice.alpha.getValue() |
| 78 | + assert dsstru.lattice.beta == s.lattice.beta.getValue() |
| 79 | + assert dsstru.lattice.gamma == s.lattice.gamma.getValue() |
| 80 | + |
| 81 | + _testAtoms() |
| 82 | + _testLattice() |
| 83 | + |
| 84 | + # Now change some values from the diffpy Structure |
| 85 | + a1.xyz[1] = 0.123 |
| 86 | + a1.U11 = 0.321 |
| 87 | + a1.B32 = 0.111 |
| 88 | + dsstru.lattice.setLatPar(a=3.0, gamma=121) |
| 89 | + _testAtoms() |
| 90 | + _testLattice() |
| 91 | + |
| 92 | + # Now change values from the srfit DiffpyStructureParSet |
| 93 | + s.Cu0.x.setValue(0.456) |
| 94 | + s.Cu0.U22.setValue(0.441) |
| 95 | + s.Cu0.B13.setValue(0.550) |
| 96 | + d = dsstru.lattice.dist(a1.xyz, a2.xyz) |
| 97 | + s.lattice.b.setValue(4.6) |
| 98 | + s.lattice.alpha.setValue(91.3) |
| 99 | + _testAtoms() |
| 100 | + _testLattice() |
| 101 | + # Make sure the distance changed |
| 102 | + assert d != dsstru.lattice.dist(a1.xyz, a2.xyz) |
| 103 | + return |
| 104 | + |
| 105 | + |
| 106 | +def test___repr__(diffpy_structure_available): |
| 107 | + """Test representation of DiffpyStructureParSet objects.""" |
| 108 | + if not diffpy_structure_available: |
| 109 | + pytest.skip("diffpy.structure package not available") |
| 110 | + from diffpy.structure import Atom, Lattice, Structure |
| 111 | + |
| 112 | + lat = Lattice(3, 3, 2, 90, 90, 90) |
| 113 | + atom = Atom("C", [0, 0.2, 0.5]) |
| 114 | + stru = Structure([atom], lattice=lat) |
| 115 | + dsps = DiffpyStructureParSet("dsps", stru) |
| 116 | + assert repr(stru) == repr(dsps) |
| 117 | + assert repr(lat) == repr(dsps.lattice) |
| 118 | + assert repr(atom) == repr(dsps.atoms[0]) |
| 119 | + return |
| 120 | + |
| 121 | + |
| 122 | +def test_pickling(diffpy_structure_available): |
| 123 | + """Test pickling of DiffpyStructureParSet.""" |
| 124 | + if not diffpy_structure_available: |
| 125 | + pytest.skip("diffpy.structure package not available") |
| 126 | + from diffpy.structure import Atom, Structure |
| 127 | + |
| 128 | + stru = Structure([Atom("C", [0, 0.2, 0.5])]) |
| 129 | + dsps = DiffpyStructureParSet("dsps", stru) |
| 130 | + data = pickle.dumps(dsps) |
| 131 | + dsps2 = pickle.loads(data) |
| 132 | + assert 1 == len(dsps2.atoms) |
| 133 | + assert 0.2 == dsps2.atoms[0].y.value |
| 134 | + return |
136 | 135 |
|
137 | 136 |
|
138 | 137 | # End of class TestParameterAdapter
|
|
0 commit comments