[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) |
---|
[ca4c150] | 19 | self.model.setParam('sldCyl', 4.e-6) |
---|
| 20 | self.model.setParam('sldSolv', 1.e-6) |
---|
[26e4a24] | 21 | self.model.setParam('background', 0.0) |
---|
| 22 | self.model.setParam('cyl_theta', 0.0) |
---|
| 23 | self.model.setParam('cyl_phi', 0.0) |
---|
| 24 | |
---|
| 25 | def test_simple(self): |
---|
| 26 | self.assertAlmostEqual(self.model.run(0.001), 450.355, 3) |
---|
| 27 | self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 452.299, 3) |
---|
| 28 | |
---|
| 29 | def test_constant(self): |
---|
| 30 | from sans.models.dispersion_models import DispersionModel |
---|
| 31 | disp = DispersionModel() |
---|
| 32 | self.model.setParam('scale', 10.0) |
---|
| 33 | self.model.set_dispersion('radius', disp) |
---|
| 34 | self.model.dispersion['radius']['width'] = 5.0 |
---|
| 35 | self.model.dispersion['radius']['npts'] = 100 |
---|
[ca4c150] | 36 | self.model.dispersion['radius']['nsigmas'] = 2.5 |
---|
[dfa8832] | 37 | print "constant",self.model.run(0.001), self.model.dispersion |
---|
| 38 | self.assertAlmostEqual(self.model.run(0.001), 1.021051*4527.47250339, 3) |
---|
| 39 | self.assertAlmostEqual(self.model.runXY([0.001, 0.001]), 1.021048*4546.997777604715, 2) |
---|
[26e4a24] | 40 | |
---|
| 41 | def test_gaussian(self): |
---|
| 42 | from sans.models.dispersion_models import GaussianDispersion |
---|
| 43 | disp = GaussianDispersion() |
---|
| 44 | self.model.set_dispersion('radius', disp) |
---|
| 45 | self.model.dispersion['radius']['width'] = 5.0 |
---|
| 46 | self.model.dispersion['radius']['npts'] = 100 |
---|
[ca4c150] | 47 | self.model.dispersion['radius']['nsigmas'] = 2.5 |
---|
[26e4a24] | 48 | self.model.setParam('scale', 10.0) |
---|
| 49 | |
---|
[dfa8832] | 50 | self.assertAlmostEqual(self.model.run(0.001), 1.1804794*4723.32213339, 3) |
---|
| 51 | self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 1.180454*4743.56, 2) |
---|
[26e4a24] | 52 | |
---|
| 53 | def test_clone(self): |
---|
| 54 | from sans.models.dispersion_models import GaussianDispersion |
---|
| 55 | disp = GaussianDispersion() |
---|
| 56 | self.model.set_dispersion('radius', disp) |
---|
| 57 | self.model.dispersion['radius']['width'] = 5.0 |
---|
| 58 | self.model.dispersion['radius']['npts'] = 100 |
---|
[ca4c150] | 59 | self.model.dispersion['radius']['nsigmas'] = 2.5 |
---|
[26e4a24] | 60 | self.model.setParam('scale', 10.0) |
---|
| 61 | |
---|
| 62 | new_model = self.model.clone() |
---|
| 63 | print "gaussian",self.model.run(0.001) |
---|
[dfa8832] | 64 | self.assertAlmostEqual(new_model.run(0.001), 1.1804794*4723.32213339, 3) |
---|
| 65 | self.assertAlmostEqual(new_model.runXY([0.001,0.001]), 1.180454*4743.56, 2) |
---|
[26e4a24] | 66 | |
---|
| 67 | def test_schulz_zero(self): |
---|
| 68 | from sans.models.dispersion_models import SchulzDispersion |
---|
| 69 | disp = SchulzDispersion() |
---|
| 70 | self.model.set_dispersion('radius', disp) |
---|
| 71 | self.model.dispersion['radius']['width'] = 5.0 |
---|
| 72 | #self.model.dispersion['radius']['width'] = 0.0 |
---|
| 73 | self.model.dispersion['radius']['npts'] = 100 |
---|
[ca4c150] | 74 | self.model.dispersion['radius']['nsigmas'] = 2.5 |
---|
[26e4a24] | 75 | #self.model.setParam('scale', 1.0) |
---|
| 76 | self.model.setParam('scale', 10.0) |
---|
| 77 | print "schulz",self.model.run(0.001), self.model.dispersion |
---|
| 78 | self.assertAlmostEqual(self.model.run(0.001), 450.355, 3) |
---|
| 79 | self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 452.299, 3) |
---|
| 80 | |
---|
| 81 | def test_lognormal_zero(self): |
---|
| 82 | from sans.models.dispersion_models import LogNormalDispersion |
---|
| 83 | disp = LogNormalDispersion() |
---|
| 84 | self.model.set_dispersion('radius', disp) |
---|
| 85 | self.model.dispersion['radius']['width'] = 5.0 |
---|
| 86 | #self.model.dispersion['radius']['width'] = 0.0 |
---|
| 87 | self.model.dispersion['radius']['npts'] = 100 |
---|
[ca4c150] | 88 | self.model.dispersion['radius']['nsigmas'] = 2.5 |
---|
[26e4a24] | 89 | #self.model.setParam('scale', 1.0) |
---|
| 90 | self.model.setParam('scale', 10.0) |
---|
| 91 | print "model dispersion",self.model.dispersion |
---|
| 92 | print "lognormal",self.model.run(0.001) |
---|
| 93 | self.assertAlmostEqual(self.model.run(0.001), 450.355, 3) |
---|
| 94 | self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 452.299, 3) |
---|
| 95 | |
---|
| 96 | def test_gaussian_zero(self): |
---|
| 97 | from sans.models.dispersion_models import GaussianDispersion |
---|
| 98 | disp = GaussianDispersion() |
---|
| 99 | self.model.set_dispersion('radius', disp) |
---|
| 100 | self.model.dispersion['radius']['width'] = 0.0 |
---|
| 101 | self.model.dispersion['radius']['npts'] = 100 |
---|
[ca4c150] | 102 | self.model.dispersion['radius']['nsigmas'] = 2.5 |
---|
[26e4a24] | 103 | self.model.setParam('scale', 1.0) |
---|
| 104 | |
---|
| 105 | self.assertAlmostEqual(self.model.run(0.001), 450.355, 3) |
---|
| 106 | self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 452.299, 3) |
---|
| 107 | |
---|
| 108 | def test_array(self): |
---|
| 109 | """ |
---|
| 110 | Perform complete rotational average and |
---|
| 111 | compare to 1D |
---|
| 112 | """ |
---|
| 113 | from sans.models.dispersion_models import ArrayDispersion |
---|
| 114 | disp_ph = ArrayDispersion() |
---|
| 115 | disp_th = ArrayDispersion() |
---|
| 116 | |
---|
| 117 | values_ph = numpy.zeros(100) |
---|
| 118 | values_th = numpy.zeros(100) |
---|
| 119 | weights = numpy.zeros(100) |
---|
| 120 | for i in range(100): |
---|
[ca4c150] | 121 | values_ph[i]=(360/99.0*i) |
---|
| 122 | values_th[i]=(180/99.0*i) |
---|
[26e4a24] | 123 | weights[i]=(1.0) |
---|
| 124 | |
---|
| 125 | disp_ph.set_weights(values_ph, weights) |
---|
| 126 | disp_th.set_weights(values_th, weights) |
---|
| 127 | |
---|
| 128 | self.model.set_dispersion('cyl_theta', disp_th) |
---|
| 129 | self.model.set_dispersion('cyl_phi', disp_ph) |
---|
| 130 | |
---|
| 131 | val_1d = self.model.run(math.sqrt(0.0002)) |
---|
| 132 | val_2d = self.model.runXY([0.01,0.01]) |
---|
| 133 | |
---|
| 134 | self.assertTrue(math.fabs(val_1d-val_2d)/val_1d < 0.02) |
---|
| 135 | |
---|
| 136 | |
---|
| 137 | if __name__ == '__main__': |
---|
| 138 | unittest.main() |
---|
| 139 | |
---|