forked from diffpy/diffpy.srfit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_diffpyparset.py
139 lines (115 loc) · 5.07 KB
/
test_diffpyparset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python
##############################################################################
#
# diffpy.srfit by DANSE Diffraction group
# Simon J. L. Billinge
# (c) 2010 The Trustees of Columbia University
# in the City of New York. All rights reserved.
#
# File coded by: Pavol Juhas
#
# See AUTHORS.txt for a list of people who contributed.
# See LICENSE_DANSE.txt for license information.
#
##############################################################################
"""Tests for diffpy.srfit.structure package."""
import pickle
import unittest
import numpy
from utils import _msg_nostructure, has_structure
# Global variables to be assigned in setUp
Atom = Lattice = Structure = DiffpyStructureParSet = None
# ----------------------------------------------------------------------------
@unittest.skipUnless(has_structure, _msg_nostructure)
class TestParameterAdapter(unittest.TestCase):
def setUp(self):
global Atom, Lattice, Structure, DiffpyStructureParSet
from diffpy.srfit.structure.diffpyparset import DiffpyStructureParSet
from diffpy.structure import Atom, Lattice, Structure
return
def testDiffpyStructureParSet(self):
"""Test the structure conversion."""
a1 = Atom("Cu", xyz=numpy.array([0.0, 0.1, 0.2]), Uisoequiv=0.003)
a2 = Atom("Ag", xyz=numpy.array([0.3, 0.4, 0.5]), Uisoequiv=0.002)
lattice = Lattice(2.5, 2.5, 2.5, 90, 90, 90)
dsstru = Structure([a1, a2], lattice)
# Structure makes copies
a1 = dsstru[0]
a2 = dsstru[1]
s = DiffpyStructureParSet("CuAg", dsstru)
self.assertEqual(s.name, "CuAg")
def _testAtoms():
# Check the atoms thoroughly
self.assertEqual(a1.element, s.Cu0.element)
self.assertEqual(a2.element, s.Ag0.element)
self.assertEqual(a1.Uisoequiv, s.Cu0.Uiso.getValue())
self.assertEqual(a2.Uisoequiv, s.Ag0.Uiso.getValue())
self.assertEqual(a1.Bisoequiv, s.Cu0.Biso.getValue())
self.assertEqual(a2.Bisoequiv, s.Ag0.Biso.getValue())
for i in range(1, 4):
for j in range(i, 4):
uijstru = getattr(a1, "U%i%i" % (i, j))
uij = getattr(s.Cu0, "U%i%i" % (i, j)).getValue()
uji = getattr(s.Cu0, "U%i%i" % (j, i)).getValue()
self.assertEqual(uijstru, uij)
self.assertEqual(uijstru, uji)
bijstru = getattr(a1, "B%i%i" % (i, j))
bij = getattr(s.Cu0, "B%i%i" % (i, j)).getValue()
bji = getattr(s.Cu0, "B%i%i" % (j, i)).getValue()
self.assertEqual(bijstru, bij)
self.assertEqual(bijstru, bji)
self.assertEqual(a1.xyz[0], s.Cu0.x.getValue())
self.assertEqual(a1.xyz[1], s.Cu0.y.getValue())
self.assertEqual(a1.xyz[2], s.Cu0.z.getValue())
return
def _testLattice():
# Test the lattice
self.assertEqual(dsstru.lattice.a, s.lattice.a.getValue())
self.assertEqual(dsstru.lattice.b, s.lattice.b.getValue())
self.assertEqual(dsstru.lattice.c, s.lattice.c.getValue())
self.assertEqual(dsstru.lattice.alpha, s.lattice.alpha.getValue())
self.assertEqual(dsstru.lattice.beta, s.lattice.beta.getValue())
self.assertEqual(dsstru.lattice.gamma, s.lattice.gamma.getValue())
_testAtoms()
_testLattice()
# Now change some values from the diffpy Structure
a1.xyz[1] = 0.123
a1.U11 = 0.321
a1.B32 = 0.111
dsstru.lattice.setLatPar(a=3.0, gamma=121)
_testAtoms()
_testLattice()
# Now change values from the srfit DiffpyStructureParSet
s.Cu0.x.setValue(0.456)
s.Cu0.U22.setValue(0.441)
s.Cu0.B13.setValue(0.550)
d = dsstru.lattice.dist(a1.xyz, a2.xyz)
s.lattice.b.setValue(4.6)
s.lattice.alpha.setValue(91.3)
_testAtoms()
_testLattice()
# Make sure the distance changed
self.assertNotEqual(d, dsstru.lattice.dist(a1.xyz, a2.xyz))
return
def test___repr__(self):
"""Test representation of DiffpyStructureParSet objects."""
lat = Lattice(3, 3, 2, 90, 90, 90)
atom = Atom("C", [0, 0.2, 0.5])
stru = Structure([atom], lattice=lat)
dsps = DiffpyStructureParSet("dsps", stru)
self.assertEqual(repr(stru), repr(dsps))
self.assertEqual(repr(lat), repr(dsps.lattice))
self.assertEqual(repr(atom), repr(dsps.atoms[0]))
return
def test_pickling(self):
"""Test pickling of DiffpyStructureParSet."""
stru = Structure([Atom("C", [0, 0.2, 0.5])])
dsps = DiffpyStructureParSet("dsps", stru)
data = pickle.dumps(dsps)
dsps2 = pickle.loads(data)
self.assertEqual(1, len(dsps2.atoms))
self.assertEqual(0.2, dsps2.atoms[0].y.value)
return
# End of class TestParameterAdapter
if __name__ == "__main__":
unittest.main()