source: sasview/calculatorview/test/utest_sld.py @ b92cbf61

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since b92cbf61 was 1e96a66, checked in by Gervaise Alina <gervyh@…>, 14 years ago

edit unittest

  • Property mode set to 100644
File size: 5.6 KB
Line 
1
2import unittest
3
4_SCALE = 1e-6
5
6# the calculator default value for wavelength is 6
7import periodictable
8from periodictable import formula
9from periodictable.xsf import xray_energy, xray_sld_from_atoms
10from periodictable.constants import avogadro_number
11from  periodictable.nsf import neutron_scattering, neutron_sld
12       
13
14def calculate_xray_sld(element, density, molecule_formula):
15    """
16    Get an element and compute the corresponding SLD for a given formula
17    :param element:  elements a string of existing atom
18    """
19    element_formula = formula(str(element))
20    if len(element_formula.atoms) != 1:
21        return 
22    element = element_formula.atoms.keys()[0] 
23    energy = xray_energy(element.K_alpha)
24    atom = molecule_formula.atoms
25    return xray_sld_from_atoms(atom, density=density, energy=energy)
26
27
28class TestH2O(unittest.TestCase):
29    """
30    Sld calculator test for H2O
31    """
32   
33    def setUp(self):
34        """Inititialze variables"""
35       
36        self.compound = "H2O"
37        self.density = 1.0
38        self.wavelength = 6.0
39        self.sld_formula = formula(self.compound, density=self.density)
40   
41    def test_neutron_sld(self):
42        """
43        test sld
44        """
45        #Compute incoherence , absorption, and incoherence
46        (sld_real,sld_im,sld_inc), (coh,abs,incoh), length = neutron_scattering(self.compound,
47                                       density=self.density, wavelength=self.wavelength) 
48        cu_real, cu_im = calculate_xray_sld(element="Cu", density=self.density,
49                                  molecule_formula=self.sld_formula)
50        mo_real, mo_im = calculate_xray_sld(element="Mo", density=self.density,
51                                  molecule_formula=self.sld_formula)
52        #test sld
53        self.assertAlmostEquals(sld_real * _SCALE, -5.6e-7, 1)
54        self.assertAlmostEquals(sld_im * _SCALE, 0)
55        #test absorption value
56        self.assertAlmostEquals(abs, 0.0741, 2)
57        self.assertAlmostEquals(incoh, 5.62, 2)
58        #Test length
59        self.assertAlmostEquals(length, 0.1755, 3)
60        #test Cu sld
61        self.assertAlmostEquals(cu_real * _SCALE, 9.46e-6, 1)
62        self.assertAlmostEquals(cu_im * _SCALE, 3.01e-8)
63        # test Mo sld
64        self.assertAlmostEquals(mo_real * _SCALE, 9.43e-6)
65        self.assertAlmostEquals(mo_im * _SCALE, 5.65e-7,1)
66   
67   
68class TestD2O(unittest.TestCase):
69    """
70    Sld calculator test for D2O
71    """
72   
73    def setUp(self):
74        """Inititialze variables"""
75       
76        self.compound = "D2O"
77        self.density = 1.1
78        self.wavelength = 6.0
79        self.sld_formula = formula(self.compound, density=self.density)
80   
81    def test_neutron_sld(self):
82        """
83        test sld
84        """
85        #Compute incoherence , absorption, and incoherence
86        (sld_real,sld_im,sld_inc), (coh,abs,incoh), length = neutron_scattering(self.compound,
87                                       density=self.density, wavelength=self.wavelength) 
88        cu_real, cu_im = calculate_xray_sld(element="Cu", density=self.density,
89                                  molecule_formula=self.sld_formula)
90        mo_real, mo_im = calculate_xray_sld(element="Mo", density=self.density,
91                                  molecule_formula=self.sld_formula)
92        #test sld
93        self.assertAlmostEquals(sld_real * _SCALE, 6.33e-6, 1)
94        self.assertAlmostEquals(sld_im * _SCALE, 0)
95        #test absorption value
96        self.assertAlmostEquals(abs, 1.35e-4, 2)
97        self.assertAlmostEquals(incoh, 0.138, 2)
98        #Test length
99        self.assertAlmostEquals(length, 1.549, 3)
100        #test Cu sld
101        self.assertAlmostEquals(cu_real * _SCALE, 9.36e-6, 1)
102        self.assertAlmostEquals(cu_im * _SCALE, 2.98e-8)
103        # test Mo sld
104        self.assertAlmostEquals(mo_real * _SCALE, 9.33e-6)
105        self.assertAlmostEquals(mo_im * _SCALE, 5.59e-9,1)
106   
107       
108class TestCd(unittest.TestCase):
109    """
110    Sld calculator test for Cd
111    """
112   
113    def setUp(self):
114        """Inititialze variables"""
115        # the calculator default value for wavelength is 6
116        self.compound = "Cd"
117        self.density = 4.0
118        self.wavelength = 6.0
119        self.sld_formula = formula(self.compound, density=self.density)
120       
121    def test_neutron_sld(self):
122        """
123        test sld
124        """
125        #Compute incoherence , absorption, and incoherence
126        (sld_real,sld_im,sld_inc), (coh,abs,incoh), length = neutron_scattering(self.compound,
127                                density=self.density, wavelength=self.wavelength) 
128        cu_real, cu_im = calculate_xray_sld(element="Cu", density=self.density,
129                                  molecule_formula=self.sld_formula)
130        mo_real, mo_im = calculate_xray_sld(element="Mo", density=self.density,
131                                  molecule_formula=self.sld_formula)
132        #test sld
133        self.assertAlmostEquals(sld_real * _SCALE, 1.04e-6, 1)
134        self.assertAlmostEquals(sld_im * _SCALE, -1.5e-7, 1)
135        #test absorption value
136        self.assertAlmostEquals(abs, 180.0,0)
137        self.assertAlmostEquals(incoh, 0.0754, 2)
138        #Test length
139        self.assertAlmostEquals(length, 0.005551, 4)
140        #test Cu sld
141        self.assertAlmostEquals(cu_real * _SCALE, 2.89e-5, 1)
142        self.assertAlmostEquals(cu_im * _SCALE, 2.81e-6)
143        # test Mo sld
144        self.assertAlmostEquals(mo_real * _SCALE, 2.84e-5, 1)
145        self.assertAlmostEquals(mo_im * _SCALE, 7.26e-7,1)
146 
147if __name__ == '__main__':
148    unittest.main()
Note: See TracBrowser for help on using the repository browser.