source: sasview/sansmodels/src/sans/models/test/utest_dispersity.py @ 43ecc75f

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 43ecc75f was dfa8832, checked in by Jae Cho <jhjcho@…>, 15 years ago

Updated some values of test accordingly to the changes in finding averaged volume for dispersions

  • Property mode set to 100644
File size: 17.9 KB
RevLine 
[0f5bc9f]1"""
2    Unit tests for dispersion functionality of
3    C++ model classes
4"""
5
6import unittest, math, numpy
7
8class TestCylinder(unittest.TestCase):
9    """
10        Testing C++ Cylinder model
11    """
12    def setUp(self):
13        from sans.models.CylinderModel import CylinderModel
14        self.model= CylinderModel()
15       
16        self.model.setParam('scale', 1.0)
17        self.model.setParam('radius', 20.0)
18        self.model.setParam('length', 400.0)
19        self.model.setParam('contrast', 3.e-6)
20        self.model.setParam('background', 0.0)
21        self.model.setParam('cyl_theta', 0.0)
22        self.model.setParam('cyl_phi', 0.0)
23       
24    def test_simple(self):
25        self.assertAlmostEqual(self.model.run(0.001), 450.355, 3)
26        self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 452.299, 3)
27       
28    def test_constant(self):
29        from sans.models.dispersion_models import DispersionModel
30        disp = DispersionModel()
31        self.model.setParam('scale', 10.0)
32        self.model.set_dispersion('radius', disp)
33        self.model.dispersion['radius']['width'] = 5.0
34        self.model.dispersion['radius']['npts'] = 100
35       
[dfa8832]36        self.assertAlmostEqual(self.model.run(0.001), 1.021051*4527.47250339, 3)
37        self.assertAlmostEqual(self.model.runXY([0.001, 0.001]), 1.021048*4546.997777604715, 2)
[0f5bc9f]38       
39    def test_gaussian(self):
40        from sans.models.dispersion_models import GaussianDispersion
41        disp = GaussianDispersion()
42        self.model.set_dispersion('radius', disp)
43        self.model.dispersion['radius']['width'] = 5.0
44        self.model.dispersion['radius']['npts'] = 100
45        self.model.setParam('scale', 10.0)
46       
[dfa8832]47        self.assertAlmostEqual(self.model.run(0.001), 1.1804794*4723.32213339, 3)
48        self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 1.180454*4743.56, 2)
[0f5bc9f]49       
[8809e48]50    def test_clone(self):
51        from sans.models.dispersion_models import GaussianDispersion
52        disp = GaussianDispersion()
53        self.model.set_dispersion('radius', disp)
54        self.model.dispersion['radius']['width'] = 5.0
55        self.model.dispersion['radius']['npts'] = 100
56        self.model.setParam('scale', 10.0)
57       
58        new_model = self.model.clone()
[dfa8832]59        self.assertAlmostEqual(new_model.run(0.001), 1.1804794*4723.32213339, 3)
60        self.assertAlmostEqual(new_model.runXY([0.001,0.001]), 1.180454*4743.56, 2)
[8809e48]61       
[ae60f86]62    def test_gaussian_zero(self):
63        from sans.models.dispersion_models import GaussianDispersion
64        disp = GaussianDispersion()
65        self.model.set_dispersion('radius', disp)
66        self.model.dispersion['radius']['width'] = 0.0
67        self.model.dispersion['radius']['npts'] = 100
68        self.model.setParam('scale', 1.0)
69       
70        self.assertAlmostEqual(self.model.run(0.001), 450.355, 3)
71        self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 452.299, 3)
72       
[0f5bc9f]73    def test_array(self):
74        """
75            Perform complete rotational average and
76            compare to 1D
77        """
78        from sans.models.dispersion_models import ArrayDispersion
79        disp_ph = ArrayDispersion()
80        disp_th = ArrayDispersion()
81       
82        values_ph = numpy.zeros(100)
83        values_th = numpy.zeros(100)
84        weights   = numpy.zeros(100)
85        for i in range(100):
86            values_ph[i]=(2.0*math.pi/99.0*i)
87            values_th[i]=(math.pi/99.0*i)
88            weights[i]=(1.0)
89       
90        disp_ph.set_weights(values_ph, weights)
91        disp_th.set_weights(values_th, weights)
92       
93        self.model.set_dispersion('cyl_theta', disp_th)
94        self.model.set_dispersion('cyl_phi', disp_ph)
95       
96        val_1d = self.model.run(math.sqrt(0.0002))
97        val_2d = self.model.runXY([0.01,0.01]) 
98       
99        self.assertTrue(math.fabs(val_1d-val_2d)/val_1d < 0.02)
100       
101class TestCoreShellCylinder(unittest.TestCase):
102    """
103        Testing C++ Cylinder model
104    """
105    def setUp(self):
106        from sans.models.CoreShellCylinderModel import CoreShellCylinderModel
107        self.model= CoreShellCylinderModel()
108       
109        self.model.setParam('scale', 1.0)
110        self.model.setParam('radius', 20.0)
111        self.model.setParam('thickness', 10.0)
112        self.model.setParam('length', 400.0)
113        self.model.setParam('core_sld', 1.e-6)
114        self.model.setParam('shell_sld', 4.e-6)
115        self.model.setParam('solvent_sld', 1.e-6)
116        self.model.setParam('background', 0.0)
117        self.model.setParam('axis_theta', 0.0)
118        self.model.setParam('axis_phi', 0.0)
119       
120    def test_simple(self):
121        """
122            Test simple 1D and 2D values
123            Numbers taken from model that passed validation, before
124            the update to C++ underlying class.
125        """
126        self.assertAlmostEqual(self.model.run(0.001), 353.55013216754583, 3)
127        self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 355.25355270620543, 3)
128       
129    def test_dispersion(self):
130        """
131            Test with dispersion
132        """
133        from sans.models.DisperseModel import DisperseModel
134        disp = DisperseModel(self.model, ['radius', 'thickness', 'length'], [5, 2, 50])
135        disp.setParam('n_pts', 10)
136        self.assertAlmostEqual(disp.run(0.001), 358.44062724936009, 3)
137        self.assertAlmostEqual(disp.runXY([0.001,0.001]), 360.22673635224584, 3)
138
139    def test_new_disp(self):
140        from sans.models.dispersion_models import GaussianDispersion
141        disp_rm = GaussianDispersion()
142        self.model.set_dispersion('radius', disp_rm)
143        self.model.dispersion['radius']['width'] = 5.0
144        self.model.dispersion['radius']['npts'] = 10
145
146        disp_rr = GaussianDispersion()
147        self.model.set_dispersion('thickness', disp_rr)
148        self.model.dispersion['thickness']['width'] = 2.
149        self.model.dispersion['thickness']['npts'] = 10
150
151        disp_len = GaussianDispersion()
152        self.model.set_dispersion('length', disp_len)
153        self.model.dispersion['length']['width'] = 50.0
154        self.model.dispersion['length']['npts'] = 10
155
[dfa8832]156        self.assertAlmostEqual(self.model.run(0.001), 1.07832610*358.44062724936009, 3)
157        self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 1.07844010*360.22673635224584, 3)
[0f5bc9f]158       
159
160    def test_array(self):
161        """
162            Perform complete rotational average and
163            compare to 1D
164        """
165        from sans.models.dispersion_models import ArrayDispersion
166        disp_ph = ArrayDispersion()
167        disp_th = ArrayDispersion()
168       
169        values_ph = numpy.zeros(100)
170        values_th = numpy.zeros(100)
171        weights   = numpy.zeros(100)
172        for i in range(100):
173            values_ph[i]=(2.0*math.pi/99.0*i)
174            values_th[i]=(math.pi/99.0*i)
175            weights[i]=(1.0)
176       
177        disp_ph.set_weights(values_ph, weights)
178        disp_th.set_weights(values_th, weights)
179       
180        self.model.set_dispersion('axis_theta', disp_th)
181        self.model.set_dispersion('axis_phi', disp_ph)
182       
183        val_1d = self.model.run(math.sqrt(0.0002))
184        val_2d = self.model.runXY([0.01,0.01]) 
185       
186        self.assertTrue(math.fabs(val_1d-val_2d)/val_1d < 0.02)
187       
188       
189       
190class TestCoreShell(unittest.TestCase):
191    """
192        Testing C++ Cylinder model
193    """
194    def setUp(self):
195        from sans.models.CoreShellModel import CoreShellModel
196        self.model= CoreShellModel()
197       
198        self.model.setParam('scale', 1.0)
199        self.model.setParam('radius', 60.0)
200        self.model.setParam('thickness', 10.0)
201        self.model.setParam('core_sld', 1.e-6)
202        self.model.setParam('shell_sld', 2.e-6)
203        self.model.setParam('solvent_sld', 3.e-6)
204        self.model.setParam('background', 0.0)
205       
206    def test_simple(self):
207        """
208            Test simple 1D and 2D values
209            Numbers taken from model that passed validation, before
210            the update to C++ underlying class.
211        """
212        self.assertAlmostEqual(self.model.run(0.001), 381.27304697150055, 3)
213        self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 380.93779156218682, 3)
214       
215    def test_dispersion(self):
216        """
217            Test with dispersion
218        """
219        from sans.models.DisperseModel import DisperseModel
220        disp = DisperseModel(self.model, ['radius', 'thickness'], [10, 2])
221        disp.setParam('n_pts', 10)
222        self.assertAlmostEqual(disp.run(0.001), 407.344127907553, 3)
223   
224    def test_new_disp(self):
225        from sans.models.dispersion_models import GaussianDispersion
226        disp_rm = GaussianDispersion()
227        self.model.set_dispersion('radius', disp_rm)
228        self.model.dispersion['radius']['width'] = 10.
229        self.model.dispersion['radius']['npts'] = 10
230
231        disp_rr = GaussianDispersion()
232        self.model.set_dispersion('thickness', disp_rr)
233        self.model.dispersion['thickness']['width'] = 2.
234        self.model.dispersion['thickness']['npts'] = 10
235
236
[dfa8832]237        self.assertAlmostEqual(self.model.run(0.001), 1.16747510*407.344127907553, 3)
[0f5bc9f]238
239       
240class TestEllipsoid(unittest.TestCase):
241    """
242        Testing C++ Cylinder model
243    """
244    def setUp(self):
245        from sans.models.EllipsoidModel import EllipsoidModel
246        self.model= EllipsoidModel()
247       
248        self.model.setParam('scale', 1.0)
249        self.model.setParam('radius_a', 20.0)
250        self.model.setParam('radius_b', 400.0)
251        self.model.setParam('contrast', 3.e-6)
252        self.model.setParam('background', 0.0)
253        self.model.setParam('axis_theta', 1.57)
254        self.model.setParam('axis_phi', 0.0)
255       
256    def test_simple(self):
257        """
258            Test simple 1D and 2D values
259            Numbers taken from model that passed validation, before
260            the update to C++ underlying class.
261        """
262        self.assertAlmostEqual(self.model.run(0.001), 11808.842896863147, 3)
263        self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 11681.990374929677, 3)
264
265    def test_dispersion(self):
266        """
267            Test with dispersion
268        """
269        from sans.models.DisperseModel import DisperseModel
270        disp = DisperseModel(self.model, ['radius_a', 'radius_b'], [5, 50])
271        disp.setParam('n_pts', 10)
272        self.assertAlmostEqual(disp.run(0.001), 11948.72581312305, 3)
273        self.assertAlmostEqual(disp.runXY([0.001,0.001]), 11811.972359807551, 3)
274       
275    def test_new_disp(self):
276        from sans.models.dispersion_models import GaussianDispersion
277        disp_rm = GaussianDispersion()
278        self.model.set_dispersion('radius_a', disp_rm)
279        self.model.dispersion['radius_a']['width'] = 5.0
280        self.model.dispersion['radius_a']['npts'] = 10
281
282        disp_rr = GaussianDispersion()
283        self.model.set_dispersion('radius_b', disp_rr)
284        self.model.dispersion['radius_b']['width'] = 50.
285        self.model.dispersion['radius_b']['npts'] = 10
286
287
[dfa8832]288        self.assertAlmostEqual(self.model.run(0.001), 1.10650710*11948.72581312305, 3)
289        self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 1.105898*11811.972359807551, 2)
[0f5bc9f]290
291    def test_array(self):
292        """
293            Perform complete rotational average and
294            compare to 1D
295        """
296        from sans.models.dispersion_models import ArrayDispersion
297        disp_ph = ArrayDispersion()
298        disp_th = ArrayDispersion()
299       
300        values_ph = numpy.zeros(100)
301        values_th = numpy.zeros(100)
302        weights   = numpy.zeros(100)
303        for i in range(100):
304            values_ph[i]=(2.0*math.pi/99.0*i)
305            values_th[i]=(math.pi/99.0*i)
306            weights[i]=(1.0)
307       
308        disp_ph.set_weights(values_ph, weights)
309        disp_th.set_weights(values_th, weights)
310       
311        self.model.set_dispersion('axis_theta', disp_th)
312        self.model.set_dispersion('axis_phi', disp_ph)
313       
314        val_1d = self.model.run(math.sqrt(0.0002))
315        val_2d = self.model.runXY([0.01,0.01]) 
316       
317        self.assertTrue(math.fabs(val_1d-val_2d)/val_1d < 0.02)
318       
319       
320       
321class TestSphere(unittest.TestCase):
322    """
323        Testing C++ Cylinder model
324    """
325    def setUp(self):
326        from sans.models.SphereModel import SphereModel
327        self.model= SphereModel()
328       
329        self.model.setParam('scale', 1.0)
330        self.model.setParam('radius', 60.0)
331        self.model.setParam('contrast', 1)
332        self.model.setParam('background', 0.0)
333       
334    def test_simple(self):
335        """
336            Test simple 1D and 2D values
337            Numbers taken from model that passed validation, before
338            the update to C++ underlying class.
339        """
340        self.assertAlmostEqual(self.model.run(0.001), 90412744456148.094, 3)
[dfa8832]341        self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 90347660670656.391, 1)
[0f5bc9f]342
343    def test_dispersion(self):
344        """
345            Test with dispersion
346        """
347        from sans.models.DisperseModel import DisperseModel
348        disp = DisperseModel(self.model, ['radius'], [10])
349        disp.setParam('n_pts', 10)
[dfa8832]350        self.assertAlmostEqual(disp.run(0.001), 96795008379475.25, 1)
[0f5bc9f]351       
352    def test_new_disp(self):
353        from sans.models.dispersion_models import GaussianDispersion
354        disp_rm = GaussianDispersion()
355        self.model.set_dispersion('radius', disp_rm)
356        self.model.dispersion['radius']['width'] = 10.0
357        self.model.dispersion['radius']['npts'] = 10
358
[dfa8832]359        self.assertAlmostEqual(self.model.run(0.001), 1.19864810*96795008379475.25,3)
[0f5bc9f]360       
361       
362       
363class TestEllipticalCylinder(unittest.TestCase):
364    """
365        Testing C++ Cylinder model
366    """
367    def setUp(self):
368        from sans.models.EllipticalCylinderModel import EllipticalCylinderModel
369        self.model= EllipticalCylinderModel()
370       
371        self.model.setParam('scale', 1.0)
372        self.model.setParam('r_minor', 20.0)
373        self.model.setParam('r_ratio', 1.5)
374        self.model.setParam('length', 400.0)
375        self.model.setParam('contrast', 3.e-6)
376        self.model.setParam('background', 0.0)
377        self.model.setParam('cyl_theta', 1.57)
378        self.model.setParam('cyl_phi', 0.0)
379        self.model.setParam('cyl_psi', 0.0)
380       
381    def test_simple(self):
382        """
383            Test simple 1D and 2D values
384            Numbers taken from model that passed validation, before
385            the update to C++ underlying class.
386        """
387        self.assertAlmostEqual(self.model.run(0.001), 675.50440232504991, 3)
388        self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 669.5173937622792, 3)
389       
390    def test_dispersion(self):
391        """
392            Test with dispersion
393        """
394        from sans.models.DisperseModel import DisperseModel
395        disp = DisperseModel(self.model, ['r_minor', 'r_ratio', 'length'], [5, 0.25, 50])
396        disp.setParam('n_pts', 10)
397        self.assertAlmostEqual(disp.run(0.001), 711.18048194151925, 3)
398        self.assertAlmostEqual(disp.runXY([0.001,0.001]), 704.63525988095705, 3)
399
400    def test_new_disp(self):
401        from sans.models.dispersion_models import GaussianDispersion
402        disp_rm = GaussianDispersion()
403        self.model.set_dispersion('r_minor', disp_rm)
404        self.model.dispersion['r_minor']['width'] = 5.0
405        self.model.dispersion['r_minor']['npts'] = 10
406
407        disp_rr = GaussianDispersion()
408        self.model.set_dispersion('r_ratio', disp_rr)
409        self.model.dispersion['r_ratio']['width'] = 0.25
410        self.model.dispersion['r_ratio']['npts'] = 10
411
412        disp_len = GaussianDispersion()
413        self.model.set_dispersion('length', disp_len)
414        self.model.dispersion['length']['width'] = 50.0
415        self.model.dispersion['length']['npts'] = 10
416
[dfa8832]417        self.assertAlmostEqual(self.model.run(0.001), 1.23925910*711.18048194151925, 3)
418        self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 1.238955*704.63525988095705, 3)
[0f5bc9f]419       
420
421    def test_array(self):
422        """
423            Perform complete rotational average and
424            compare to 1D
425        """
426        from sans.models.dispersion_models import ArrayDispersion
427        disp_ph = ArrayDispersion()
428        disp_th = ArrayDispersion()
429        disp_ps = ArrayDispersion()
430       
431        values_ph = numpy.zeros(100)
432        values_th = numpy.zeros(100)
433        values_ps = numpy.zeros(100)
434        weights   = numpy.zeros(100)
435        for i in range(100):
436            values_ps[i]=(2.0*math.pi/99.0*i)
437            values_ph[i]=(2.0*math.pi/99.0*i)
438            values_th[i]=(math.pi/99.0*i)
439            weights[i]=(1.0)
440       
441        disp_ph.set_weights(values_ph, weights)
442        disp_th.set_weights(values_th, weights)
443        disp_ps.set_weights(values_ps, weights)
444       
445        self.model.set_dispersion('cyl_theta', disp_th)
446        self.model.set_dispersion('cyl_phi', disp_ph)
447        self.model.set_dispersion('cyl_psi', disp_ps)
448       
449        val_1d = self.model.run(math.sqrt(0.0002))
450        val_2d = self.model.runXY([0.01,0.01]) 
451       
452        self.assertTrue(math.fabs(val_1d-val_2d)/val_1d < 0.02)
453       
[ae60f86]454class TestDispModel(unittest.TestCase):
455    def setUp(self):
456        from sans.models.CylinderModel import CylinderModel
457        self.model = CylinderModel()
458       
459       
460    def test_disp_params(self):
461       
462        self.assertEqual(self.model.dispersion['radius']['width'], 0.0)
463        self.model.setParam('radius.width', 5.0)
464        self.assertEqual(self.model.dispersion['radius']['width'], 5.0)
465        self.assertEqual(self.model.getParam('radius.width'), 5.0)
466        self.assertEqual(self.model.dispersion['radius']['type'], 'gaussian')
[0f5bc9f]467       
468 
469if __name__ == '__main__':
470    unittest.main()
471   
Note: See TracBrowser for help on using the repository browser.