[26e4a24] | 1 | """ |
---|
| 2 | Unit tests for dispersion functionality of |
---|
| 3 | C++ model classes |
---|
| 4 | """ |
---|
| 5 | |
---|
| 6 | import unittest, math, numpy |
---|
| 7 | |
---|
| 8 | class 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 |
---|
[dfa8832] | 35 | print "constant",self.model.run(0.001), self.model.dispersion |
---|
| 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) |
---|
[26e4a24] | 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) |
---|
[26e4a24] | 49 | |
---|
| 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() |
---|
| 59 | print "gaussian",self.model.run(0.001) |
---|
[dfa8832] | 60 | self.assertAlmostEqual(new_model.run(0.001), 1.1804794*4723.32213339, 3) |
---|
| 61 | self.assertAlmostEqual(new_model.runXY([0.001,0.001]), 1.180454*4743.56, 2) |
---|
[26e4a24] | 62 | |
---|
| 63 | def test_schulz_zero(self): |
---|
| 64 | from sans.models.dispersion_models import SchulzDispersion |
---|
| 65 | disp = SchulzDispersion() |
---|
| 66 | self.model.set_dispersion('radius', disp) |
---|
| 67 | self.model.dispersion['radius']['width'] = 5.0 |
---|
| 68 | #self.model.dispersion['radius']['width'] = 0.0 |
---|
| 69 | self.model.dispersion['radius']['npts'] = 100 |
---|
| 70 | #self.model.setParam('scale', 1.0) |
---|
| 71 | self.model.setParam('scale', 10.0) |
---|
| 72 | print "schulz",self.model.run(0.001), self.model.dispersion |
---|
| 73 | self.assertAlmostEqual(self.model.run(0.001), 450.355, 3) |
---|
| 74 | self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 452.299, 3) |
---|
| 75 | |
---|
| 76 | def test_lognormal_zero(self): |
---|
| 77 | from sans.models.dispersion_models import LogNormalDispersion |
---|
| 78 | disp = LogNormalDispersion() |
---|
| 79 | self.model.set_dispersion('radius', disp) |
---|
| 80 | self.model.dispersion['radius']['width'] = 5.0 |
---|
| 81 | #self.model.dispersion['radius']['width'] = 0.0 |
---|
| 82 | self.model.dispersion['radius']['npts'] = 100 |
---|
| 83 | #self.model.setParam('scale', 1.0) |
---|
| 84 | self.model.setParam('scale', 10.0) |
---|
| 85 | print "model dispersion",self.model.dispersion |
---|
| 86 | print "lognormal",self.model.run(0.001) |
---|
| 87 | self.assertAlmostEqual(self.model.run(0.001), 450.355, 3) |
---|
| 88 | self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 452.299, 3) |
---|
| 89 | |
---|
| 90 | def test_gaussian_zero(self): |
---|
| 91 | from sans.models.dispersion_models import GaussianDispersion |
---|
| 92 | disp = GaussianDispersion() |
---|
| 93 | self.model.set_dispersion('radius', disp) |
---|
| 94 | self.model.dispersion['radius']['width'] = 0.0 |
---|
| 95 | self.model.dispersion['radius']['npts'] = 100 |
---|
| 96 | self.model.setParam('scale', 1.0) |
---|
| 97 | |
---|
| 98 | self.assertAlmostEqual(self.model.run(0.001), 450.355, 3) |
---|
| 99 | self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 452.299, 3) |
---|
| 100 | |
---|
| 101 | def test_array(self): |
---|
| 102 | """ |
---|
| 103 | Perform complete rotational average and |
---|
| 104 | compare to 1D |
---|
| 105 | """ |
---|
| 106 | from sans.models.dispersion_models import ArrayDispersion |
---|
| 107 | disp_ph = ArrayDispersion() |
---|
| 108 | disp_th = ArrayDispersion() |
---|
| 109 | |
---|
| 110 | values_ph = numpy.zeros(100) |
---|
| 111 | values_th = numpy.zeros(100) |
---|
| 112 | weights = numpy.zeros(100) |
---|
| 113 | for i in range(100): |
---|
| 114 | values_ph[i]=(2.0*math.pi/99.0*i) |
---|
| 115 | values_th[i]=(math.pi/99.0*i) |
---|
| 116 | weights[i]=(1.0) |
---|
| 117 | |
---|
| 118 | disp_ph.set_weights(values_ph, weights) |
---|
| 119 | disp_th.set_weights(values_th, weights) |
---|
| 120 | |
---|
| 121 | self.model.set_dispersion('cyl_theta', disp_th) |
---|
| 122 | self.model.set_dispersion('cyl_phi', disp_ph) |
---|
| 123 | |
---|
| 124 | val_1d = self.model.run(math.sqrt(0.0002)) |
---|
| 125 | val_2d = self.model.runXY([0.01,0.01]) |
---|
| 126 | |
---|
| 127 | self.assertTrue(math.fabs(val_1d-val_2d)/val_1d < 0.02) |
---|
| 128 | |
---|
| 129 | |
---|
| 130 | if __name__ == '__main__': |
---|
| 131 | unittest.main() |
---|
| 132 | |
---|