[914caf52] | 1 | """ |
---|
| 2 | Unit tests for non shape based model (Task 8.2.1) |
---|
| 3 | These tests are part of the requirements |
---|
| 4 | """ |
---|
| 5 | |
---|
| 6 | import unittest, time, math |
---|
[f629e346] | 7 | from scipy.special import erf,gammaln |
---|
[914caf52] | 8 | |
---|
| 9 | # Disable "missing docstring" complaint |
---|
| 10 | # pylint: disable-msg=C0111 |
---|
| 11 | # Disable "too many methods" complaint |
---|
| 12 | # pylint: disable-msg=R0904 |
---|
| 13 | # Disable "could be a function" complaint |
---|
| 14 | # pylint: disable-msg=R0201 |
---|
| 15 | |
---|
[829eee9] | 16 | import scipy |
---|
[914caf52] | 17 | class TestGuinier(unittest.TestCase): |
---|
| 18 | """ |
---|
| 19 | Unit tests for Guinier function |
---|
| 20 | |
---|
| 21 | F(x) = exp[ [A] + [B]*Q**2 ] |
---|
| 22 | |
---|
| 23 | The model has two parameters: A and B |
---|
| 24 | """ |
---|
| 25 | def _func(self, a, b, x): |
---|
[f629e346] | 26 | return a*math.exp(-(b*x)**2/3.0) |
---|
[914caf52] | 27 | |
---|
| 28 | def setUp(self): |
---|
[f629e346] | 29 | from sans.models.GuinierModel import GuinierModel |
---|
[914caf52] | 30 | self.model= GuinierModel() |
---|
| 31 | |
---|
| 32 | def test1D(self): |
---|
[f629e346] | 33 | self.model.setParam('scale', 2.0) |
---|
| 34 | self.model.setParam('rg', 1.0) |
---|
[914caf52] | 35 | |
---|
[f629e346] | 36 | self.assertEqual(self.model.run(0.0), 2.0) |
---|
| 37 | self.assertEqual(self.model.run(2.0), 2.0*math.exp(-(1.0*2.0)**2/3.0)) |
---|
[6b8399b] | 38 | self.assertEqual(self.model.runXY(2.0), 2.0*math.exp(-(1.0*2.0)**2/3.0)) |
---|
[914caf52] | 39 | |
---|
| 40 | def test2D(self): |
---|
[f629e346] | 41 | self.model.setParam('scale', 2.0) |
---|
| 42 | self.model.setParam('rg', 1.0) |
---|
[914caf52] | 43 | |
---|
[a55fac1] | 44 | #value = self._func(2.0, 1.0, 1.0)*self._func(2.0, 1.0, 2.0) |
---|
| 45 | value = self._func(2.0, 1.0, math.sqrt(5.0)) |
---|
| 46 | #self.assertEqual(self.model.runXY([0.0,0.0]), 2.0*2.0) |
---|
| 47 | self.assertEqual(self.model.runXY([0.0,0.0]), 2.0) |
---|
[914caf52] | 48 | self.assertEqual(self.model.runXY([1.0,2.0]), value) |
---|
| 49 | |
---|
| 50 | def test2Dphi(self): |
---|
[f629e346] | 51 | self.model.setParam('scale', 2.0) |
---|
| 52 | self.model.setParam('rg', 1.0) |
---|
[914caf52] | 53 | |
---|
| 54 | x = 1.0 |
---|
| 55 | y = 2.0 |
---|
| 56 | r = math.sqrt(x**2 + y**2) |
---|
| 57 | phi = math.atan2(y, x) |
---|
| 58 | |
---|
[a55fac1] | 59 | #value = self._func(2.0, 1.0, x)*self._func(2.0, 1.0, y) |
---|
| 60 | value = self._func(2.0, 1.0, r) |
---|
[914caf52] | 61 | |
---|
[f03b027] | 62 | #self.assertEqual(self.model.run([r, phi]), value) |
---|
| 63 | self.assertAlmostEquals(self.model.run([r, phi]), value,1) |
---|
[829eee9] | 64 | |
---|
| 65 | |
---|
[914caf52] | 66 | class TestPorod(unittest.TestCase): |
---|
| 67 | """ |
---|
| 68 | Unit tests for Porod function |
---|
| 69 | |
---|
[f629e346] | 70 | F(x) = C/Q**4 |
---|
[914caf52] | 71 | |
---|
| 72 | The model has one parameter: C |
---|
| 73 | """ |
---|
| 74 | def _func(self, c, x): |
---|
[f629e346] | 75 | return c/(x**4) |
---|
[914caf52] | 76 | |
---|
| 77 | def setUp(self): |
---|
[f629e346] | 78 | from sans.models.PorodModel import PorodModel |
---|
[914caf52] | 79 | self.model= PorodModel() |
---|
[f629e346] | 80 | self.model.setParam('scale', 2.0) |
---|
[914caf52] | 81 | |
---|
| 82 | def test1D(self): |
---|
| 83 | value = self._func(2.0, 3.0) |
---|
| 84 | self.assertEqual(self.model.run(3.0), value) |
---|
[6b8399b] | 85 | self.assertEqual(self.model.runXY(3.0), value) |
---|
[914caf52] | 86 | |
---|
| 87 | def test2D(self): |
---|
[a55fac1] | 88 | #value = self._func(2.0, 1.0)*self._func(2.0, 2.0) |
---|
| 89 | value = self._func(2.0, math.sqrt(5.0)) |
---|
[914caf52] | 90 | self.assertEqual(self.model.runXY([1.0,2.0]), value) |
---|
| 91 | |
---|
| 92 | def test2Dphi(self): |
---|
| 93 | x = 1.0 |
---|
| 94 | y = 2.0 |
---|
| 95 | r = math.sqrt(x**2 + y**2) |
---|
| 96 | phi = math.atan2(y, x) |
---|
| 97 | |
---|
[a55fac1] | 98 | #value = self._func(2.0, 1.0)*self._func(2.0, 2.0) |
---|
| 99 | value = self._func(2.0, r) |
---|
[829eee9] | 100 | self.assertAlmostEquals(self.model.run([r, phi]), value,1) |
---|
[914caf52] | 101 | |
---|
[829eee9] | 102 | class TestDebye(unittest.TestCase): |
---|
| 103 | """ |
---|
| 104 | Unit tests for Debye function |
---|
| 105 | |
---|
| 106 | F(x) = 2( exp(-x)+x -1 )/x**2 |
---|
| 107 | |
---|
| 108 | The model has three parameters: |
---|
| 109 | Rg = radius of gyration |
---|
| 110 | scale = scale factor |
---|
| 111 | bkd = Constant background |
---|
| 112 | """ |
---|
| 113 | def _func(self, Rg, scale, bkg, x): |
---|
| 114 | y = (Rg * x)**2 |
---|
| 115 | return scale * (2*(math.exp(-y) + y -1)/y**2) + bkg |
---|
| 116 | |
---|
| 117 | def setUp(self): |
---|
[f629e346] | 118 | from sans.models.DebyeModel import DebyeModel |
---|
[829eee9] | 119 | self.model= DebyeModel() |
---|
[a07161b] | 120 | self.model.setParam('rg', 50.0) |
---|
[829eee9] | 121 | self.model.setParam('scale',1.0) |
---|
[f629e346] | 122 | self.model.setParam('background',0.001) |
---|
[829eee9] | 123 | |
---|
| 124 | def test1D(self): |
---|
| 125 | value = self._func(50.0, 1.0, 0.001, 2.0) |
---|
| 126 | self.assertEqual(self.model.run(2.0), value) |
---|
[6b8399b] | 127 | self.assertEqual(self.model.runXY(2.0), value) |
---|
[a07161b] | 128 | |
---|
| 129 | # User enter zero as a value of x, y= 1 |
---|
| 130 | self.assertAlmostEqual(self.model.run(0.0), 1.00, 2) |
---|
[829eee9] | 131 | |
---|
[8809e48] | 132 | def test1D_clone(self): |
---|
| 133 | value = self._func(50.0, 1.0, 10.0, 2.0) |
---|
| 134 | self.model.setParam('background', 10.0) |
---|
| 135 | clone = self.model.clone() |
---|
| 136 | self.assertEqual(clone.run(2.0), value) |
---|
| 137 | self.assertEqual(clone.runXY(2.0), value) |
---|
| 138 | |
---|
| 139 | # User enter zero as a value of x |
---|
[a07161b] | 140 | # An exceptio is raised: No more exception |
---|
| 141 | #self.assertRaises(ZeroDivisionError, clone.run, 0.0) |
---|
[8809e48] | 142 | |
---|
[829eee9] | 143 | def test2D(self): |
---|
[a55fac1] | 144 | #value = self._func(50.0, 1.0, 0.001, 1.0)*self._func(50.0, 1.0, 0.001, 2.0) |
---|
| 145 | value = self._func(50.0, 1.0, 0.001, math.sqrt(5.0)) |
---|
[829eee9] | 146 | self.assertEqual(self.model.runXY([1.0,2.0]), value) |
---|
| 147 | |
---|
| 148 | def test2Dphi(self): |
---|
| 149 | x = 1.0 |
---|
| 150 | y = 2.0 |
---|
| 151 | r = math.sqrt(x**2 + y**2) |
---|
| 152 | phi = math.atan2(y, x) |
---|
| 153 | |
---|
| 154 | value = self._func(50.0, 1.0, 0.001, 1.0)*self._func(50.0, 1.0, 0.001, 2.0) |
---|
| 155 | self.assertAlmostEquals(self.model.run([r, phi]), value,1) |
---|
| 156 | |
---|
| 157 | |
---|
| 158 | class TestLorentz(unittest.TestCase): |
---|
| 159 | """ |
---|
| 160 | Unit tests for Lorentz function |
---|
| 161 | |
---|
| 162 | F(x) = scale/( 1 + (x*L)^2 ) + bkd |
---|
| 163 | |
---|
| 164 | The model has three parameters: |
---|
| 165 | L = screen Length |
---|
| 166 | scale = scale factor |
---|
| 167 | bkd = incoherent background |
---|
| 168 | """ |
---|
[f629e346] | 169 | def _func(self, I0 , L, bgd, qval): |
---|
| 170 | return I0/(1.0 + (qval*L)*(qval*L)) + bgd |
---|
| 171 | |
---|
[829eee9] | 172 | def setUp(self): |
---|
[f629e346] | 173 | from sans.models.LorentzModel import LorentzModel |
---|
[829eee9] | 174 | self.model= LorentzModel() |
---|
| 175 | |
---|
| 176 | def test1D(self): |
---|
| 177 | self.model.setParam('scale', 100.0) |
---|
[f629e346] | 178 | self.model.setParam('Length', 50.0) |
---|
| 179 | self.model.setParam('background', 1.0) |
---|
[829eee9] | 180 | |
---|
| 181 | self.assertEqual(self.model.run(0.0), 101.0) |
---|
| 182 | self.assertEqual(self.model.run(2.0), self._func(100.0, 50.0, 1.0, 2.0)) |
---|
[6b8399b] | 183 | self.assertEqual(self.model.runXY(2.0), self._func(100.0, 50.0, 1.0, 2.0)) |
---|
[829eee9] | 184 | |
---|
| 185 | def test2D(self): |
---|
| 186 | self.model.setParam('scale', 100.0) |
---|
[f629e346] | 187 | self.model.setParam('Length', 50.0) |
---|
| 188 | self.model.setParam('background', 1.0) |
---|
[829eee9] | 189 | |
---|
[a55fac1] | 190 | #value = self._func(100.0, 50.0, 1.0, 1.0)*self._func(100.0, 50.0, 1.0, 2.0) |
---|
| 191 | value = self._func(100.0, 50.0, 1.0, math.sqrt(5.0)) |
---|
[829eee9] | 192 | self.assertEqual(self.model.runXY([1.0,2.0]), value) |
---|
| 193 | |
---|
| 194 | def test2Dphi(self): |
---|
| 195 | self.model.setParam('scale', 100.0) |
---|
[f629e346] | 196 | self.model.setParam('Length', 50.0) |
---|
| 197 | self.model.setParam('background', 1.0) |
---|
[829eee9] | 198 | |
---|
| 199 | x = 1.0 |
---|
| 200 | y = 2.0 |
---|
| 201 | r = math.sqrt(x**2 + y**2) |
---|
| 202 | phi = math.atan2(y, x) |
---|
| 203 | |
---|
| 204 | value = self._func(100.0, 50.0, 1.0, x)*self._func(100.0, 50.0, 1.0, y) |
---|
| 205 | self.assertAlmostEquals(self.model.run([r, phi]), value,1) |
---|
| 206 | |
---|
| 207 | class TestDAB(unittest.TestCase): |
---|
| 208 | """ |
---|
| 209 | Unit tests for DAB function |
---|
| 210 | |
---|
| 211 | F(x) = scale/( 1 + (x*L)^2 )^(2) + bkd |
---|
| 212 | |
---|
| 213 | The model has three parameters: |
---|
| 214 | L = Correlation Length |
---|
| 215 | scale = scale factor |
---|
| 216 | bkd = incoherent background |
---|
| 217 | """ |
---|
[f629e346] | 218 | def _func(self, Izero, range, incoh, qval): |
---|
[a07161b] | 219 | return Izero* pow(range,3)/pow((1.0 + (qval*range)*(qval*range)),2) + incoh |
---|
[829eee9] | 220 | |
---|
| 221 | def setUp(self): |
---|
[f629e346] | 222 | from sans.models.DABModel import DABModel |
---|
[829eee9] | 223 | self.model= DABModel() |
---|
[6b8399b] | 224 | self.scale = 10.0 |
---|
| 225 | self.length = 40.0 |
---|
| 226 | self.back = 1.0 |
---|
| 227 | |
---|
| 228 | self.model.setParam('scale', self.scale) |
---|
[a07161b] | 229 | self.model.setParam('length', self.length) |
---|
[6b8399b] | 230 | self.model.setParam('background', self.back) |
---|
[829eee9] | 231 | |
---|
| 232 | def test1D(self): |
---|
[a07161b] | 233 | |
---|
[6b8399b] | 234 | self.assertEqual(self.model.run(2.0), self._func(self.scale, self.length, self.back, 2.0)) |
---|
| 235 | self.assertEqual(self.model.runXY(2.0), self._func(self.scale, self.length, self.back, 2.0)) |
---|
[829eee9] | 236 | |
---|
| 237 | def test2D(self): |
---|
[a55fac1] | 238 | #value = self._func(self.scale, self.length, self.back, 1.0)*self._func(self.scale, self.length, self.back, 2.0) |
---|
| 239 | value = self._func(self.scale, self.length, self.back, math.sqrt(5.0)) |
---|
[829eee9] | 240 | self.assertEqual(self.model.runXY([1.0,2.0]), value) |
---|
| 241 | |
---|
| 242 | def test2Dphi(self): |
---|
| 243 | x = 1.0 |
---|
| 244 | y = 2.0 |
---|
| 245 | r = math.sqrt(x**2 + y**2) |
---|
| 246 | phi = math.atan2(y, x) |
---|
| 247 | |
---|
[6b8399b] | 248 | value = self._func(self.scale, self.length, self.back, x)*self._func(self.scale, self.length, self.back, y) |
---|
[a07161b] | 249 | self.assertAlmostEquals(self.model.run([x, y]), value,1) |
---|
[914caf52] | 250 | |
---|
[829eee9] | 251 | class TestPowerLaw(unittest.TestCase): |
---|
| 252 | """ |
---|
| 253 | Unit tests for PowerLaw function |
---|
| 254 | |
---|
| 255 | F(x) = scale* (x)^(m) + bkd |
---|
| 256 | |
---|
| 257 | The model has three parameters: |
---|
| 258 | m = power |
---|
| 259 | scale = scale factor |
---|
| 260 | bkd = incoherent background |
---|
| 261 | """ |
---|
[f629e346] | 262 | def _func(self, a, m, bgd, qval): |
---|
| 263 | return a*math.pow(qval,-m) + bgd |
---|
| 264 | |
---|
[829eee9] | 265 | |
---|
| 266 | def setUp(self): |
---|
[f629e346] | 267 | from sans.models.PowerLawModel import PowerLawModel |
---|
[829eee9] | 268 | self.model= PowerLawModel() |
---|
| 269 | |
---|
| 270 | def test1D(self): |
---|
| 271 | self.model.setParam('scale', math.exp(-6)) |
---|
| 272 | self.model.setParam('m', 4.0) |
---|
[f629e346] | 273 | self.model.setParam('background', 1.0) |
---|
[829eee9] | 274 | |
---|
[f629e346] | 275 | #self.assertEqual(self.model.run(0.0), 1.0) |
---|
[829eee9] | 276 | self.assertEqual(self.model.run(2.0), self._func(math.exp(-6), 4.0, 1.0, 2.0)) |
---|
[6b8399b] | 277 | self.assertEqual(self.model.runXY(2.0), self._func(math.exp(-6), 4.0, 1.0, 2.0)) |
---|
[f629e346] | 278 | |
---|
| 279 | def testlimit(self): |
---|
| 280 | self.model.setParam('scale', math.exp(-6)) |
---|
| 281 | self.model.setParam('m', -4.0) |
---|
| 282 | self.model.setParam('background', 1.0) |
---|
| 283 | |
---|
| 284 | self.assertEqual(self.model.run(0.0), 1.0) |
---|
[829eee9] | 285 | |
---|
| 286 | def test2D(self): |
---|
| 287 | self.model.setParam('scale', math.exp(-6)) |
---|
| 288 | self.model.setParam('m', 4.0) |
---|
[f629e346] | 289 | self.model.setParam('background', 1.0) |
---|
[829eee9] | 290 | |
---|
[a55fac1] | 291 | #value = self._func(math.exp(-6), 4.0, 1.0, 1.0)\ |
---|
| 292 | #*self._func(math.exp(-6), 4.0, 1.0, 2.0) |
---|
| 293 | value = self._func(math.exp(-6), 4.0, 1.0, math.sqrt(5.0)) |
---|
[829eee9] | 294 | |
---|
| 295 | self.assertEqual(self.model.runXY([1.0,2.0]), value) |
---|
| 296 | |
---|
| 297 | def test2Dphi(self): |
---|
| 298 | self.model.setParam('scale', math.exp(-6)) |
---|
| 299 | self.model.setParam('m', 4.0) |
---|
[f629e346] | 300 | self.model.setParam('background', 1.0) |
---|
[829eee9] | 301 | |
---|
| 302 | x = 1.0 |
---|
| 303 | y = 2.0 |
---|
| 304 | r = math.sqrt(x**2 + y**2) |
---|
| 305 | phi = math.atan2(y, x) |
---|
| 306 | |
---|
| 307 | value = self._func(math.exp(-6), 4.0, 1.0, x)\ |
---|
| 308 | *self._func(math.exp(-6), 4.0, 1.0, y) |
---|
| 309 | self.assertAlmostEquals(self.model.run([r, phi]), value,1) |
---|
| 310 | |
---|
| 311 | class TestTeubnerStrey(unittest.TestCase): |
---|
| 312 | """ |
---|
| 313 | Unit tests for PowerLaw function |
---|
| 314 | |
---|
| 315 | F(x) = 1/( scale + c1*(x)^(2)+ c2*(x)^(4)) + bkd |
---|
| 316 | |
---|
| 317 | The model has Four parameters: |
---|
| 318 | scale = scale factor |
---|
| 319 | c1 = constant |
---|
| 320 | c2 = constant |
---|
| 321 | bkd = incoherent background |
---|
| 322 | """ |
---|
[f629e346] | 323 | def _func(self, scale, c1, c2, bck, q): |
---|
| 324 | |
---|
| 325 | q2 = q*q; |
---|
| 326 | q4 = q2*q2; |
---|
| 327 | |
---|
| 328 | return 1.0/(scale + c1*q2+c2*q4) + bck |
---|
[829eee9] | 329 | |
---|
| 330 | def setUp(self): |
---|
[f629e346] | 331 | from sans.models.TeubnerStreyModel import TeubnerStreyModel |
---|
[829eee9] | 332 | self.model= TeubnerStreyModel() |
---|
| 333 | |
---|
| 334 | def test1D(self): |
---|
| 335 | |
---|
| 336 | self.model.setParam('c1', -30.0) |
---|
| 337 | self.model.setParam('c2', 5000.0) |
---|
| 338 | self.model.setParam('scale', 0.1) |
---|
[f629e346] | 339 | self.model.setParam('background', 0.1) |
---|
[829eee9] | 340 | #self.assertEqual(1/(math.sqrt(4)), math.pow(4,-1/2)) |
---|
[f629e346] | 341 | #self.assertEqual(self.model.TeubnerStreyLengths(),False ) |
---|
[829eee9] | 342 | |
---|
| 343 | self.assertEqual(self.model.run(0.0), 10.1) |
---|
| 344 | self.assertEqual(self.model.run(2.0), self._func(0.1,-30.0,5000.0,0.1,2.0)) |
---|
[6b8399b] | 345 | self.assertEqual(self.model.runXY(2.0), self._func(0.1,-30.0,5000.0,0.1,2.0)) |
---|
[829eee9] | 346 | |
---|
| 347 | def test2D(self): |
---|
| 348 | self.model.setParam('c1', -30.0) |
---|
| 349 | self.model.setParam('c2', 5000.0) |
---|
| 350 | self.model.setParam('scale', 0.1) |
---|
[f629e346] | 351 | self.model.setParam('background', 0.1) |
---|
[a55fac1] | 352 | #value = self._func(0.1,-30.0,5000.0,0.1, 1.0)\ |
---|
| 353 | #*self._func(0.1,-30.0,5000.0,0.1, 2.0) |
---|
| 354 | value = self._func(0.1,-30.0,5000.0,0.1, math.sqrt(5.0)) |
---|
[829eee9] | 355 | |
---|
| 356 | self.assertEqual(self.model.runXY([1.0,2.0]), value) |
---|
| 357 | |
---|
| 358 | def test2Dphi(self): |
---|
| 359 | self.model.setParam('c1', -30.0) |
---|
| 360 | self.model.setParam('c2', 5000.0) |
---|
| 361 | self.model.setParam('scale', 0.1) |
---|
[f629e346] | 362 | self.model.setParam('background', 0.1) |
---|
[829eee9] | 363 | |
---|
| 364 | x = 1.0 |
---|
| 365 | y = 2.0 |
---|
| 366 | r = math.sqrt(x**2 + y**2) |
---|
| 367 | phi = math.atan2(y, x) |
---|
| 368 | |
---|
[a55fac1] | 369 | #value = self._func(0.1,-30.0,5000.0,0.1, x)\ |
---|
| 370 | #*self._func(0.1,-30.0,5000.0,0.1, y) |
---|
| 371 | value = self._func(0.1,-30.0,5000.0,0.1, r) |
---|
[829eee9] | 372 | self.assertAlmostEquals(self.model.run([r, phi]), value,1) |
---|
| 373 | |
---|
| 374 | class TestBEPolyelectrolyte(unittest.TestCase): |
---|
| 375 | """ |
---|
| 376 | Unit tests for BEPolyelectrolyte function |
---|
| 377 | |
---|
| 378 | F(x) = K*1/(4*pi()*Lb*(alpha)^(2)*(q^(2)+k2)/(1+(r02)^(2))*(q^(2)+k2)\ |
---|
| 379 | *(q^(2)-(12*h*C/b^(2))) |
---|
| 380 | |
---|
| 381 | The model has Eight parameters: |
---|
| 382 | K = Constrast factor of the polymer |
---|
| 383 | Lb = Bjerrum length |
---|
| 384 | H = virial parameter |
---|
| 385 | B = monomer length |
---|
| 386 | Cs = Concentration of monovalent salt |
---|
| 387 | alpha = ionazation degree |
---|
| 388 | C = polymer molar concentration |
---|
| 389 | bkd = background |
---|
| 390 | """ |
---|
| 391 | def _func(self, K, Lb, H, B, Cs, alpha, C, bkd, r02, k2, x): |
---|
| 392 | return (K /( (4*math.pi *Lb*(alpha**2)*(x**2 +k2)) *( (1 +(r02**2)) \ |
---|
| 393 | *((x**2) + k2)*((x**2) -(12 * H * C/(B**2))) )))+ bkd |
---|
| 394 | |
---|
| 395 | def setUp(self): |
---|
[f629e346] | 396 | from sans.models.BEPolyelectrolyte import BEPolyelectrolyte |
---|
[829eee9] | 397 | self.model= BEPolyelectrolyte() |
---|
| 398 | |
---|
[f629e346] | 399 | self.K = 10.0 |
---|
| 400 | self.Lb = 6.5 |
---|
| 401 | self.h = 11 |
---|
| 402 | self.b = 13 |
---|
| 403 | self.Cs = 0.1 |
---|
| 404 | self.alpha = 0.05 |
---|
| 405 | self.C = .7 |
---|
| 406 | self.Bkd =0.01 |
---|
| 407 | |
---|
| 408 | self.model.setParam('K', self.K) |
---|
| 409 | self.model.setParam('Lb', self.Lb) |
---|
| 410 | self.model.setParam('H', self.h) |
---|
| 411 | self.model.setParam('B', self.b) |
---|
| 412 | self.model.setParam('Cs',self.Cs) |
---|
| 413 | self.model.setParam('alpha', self.alpha) |
---|
| 414 | self.model.setParam('C', self.C) |
---|
| 415 | self.model.setParam('background', self.Bkd) |
---|
| 416 | |
---|
| 417 | def _func(self, q): |
---|
| 418 | |
---|
| 419 | Ca = self.C *6.022136e-4 |
---|
| 420 | Csa = self.Cs * 6.022136e-4 |
---|
| 421 | k2= 4*math.pi*self.Lb*(2*self.Cs+self.alpha*Ca) |
---|
| 422 | |
---|
| 423 | r02 = 1./self.alpha / Ca**0.5*( self.b / (48*math.pi*self.Lb)**0.5 ) |
---|
| 424 | q2 = q**2 |
---|
| 425 | Sq = self.K*1./(4*math.pi*self.Lb*self.alpha**2) * (q2 + k2) / (1+(r02**2) * (q2+k2) * (q2- (12*self.h*Ca/self.b**2)) ) + self.Bkd |
---|
| 426 | return Sq |
---|
| 427 | |
---|
[829eee9] | 428 | def test1D(self): |
---|
| 429 | |
---|
[f629e346] | 430 | |
---|
| 431 | q = 0.001 |
---|
| 432 | |
---|
| 433 | self.assertEqual(self.model.run(q), self._func(q)) |
---|
[6b8399b] | 434 | self.assertEqual(self.model.runXY(q), self._func(q)) |
---|
[f629e346] | 435 | |
---|
[829eee9] | 436 | def test2D(self): |
---|
[a55fac1] | 437 | #self.assertAlmostEquals(self.model.runXY([1.0,2.0]), self._func(1.0)*self._func(2.0), 8) |
---|
| 438 | self.assertAlmostEquals(self.model.runXY([1.0,2.0]), self._func(math.sqrt(1.0+2.0**2)), 8) |
---|
[829eee9] | 439 | |
---|
| 440 | def test2Dphi(self): |
---|
[f629e346] | 441 | |
---|
[829eee9] | 442 | x = 1.0 |
---|
| 443 | y = 2.0 |
---|
| 444 | r = math.sqrt(x**2 + y**2) |
---|
| 445 | phi = math.atan2(y, x) |
---|
| 446 | |
---|
[a55fac1] | 447 | self.assertAlmostEquals(self.model.run([r, phi]), self._func(r), 8) |
---|
[829eee9] | 448 | |
---|
| 449 | class TestFractalModel(unittest.TestCase): |
---|
| 450 | """ |
---|
| 451 | Unit tests for Number Density Fractal function |
---|
| 452 | F(x)= P(x)*S(x) + bkd |
---|
| 453 | The model has Seven parameters: |
---|
| 454 | scale = Volume fraction |
---|
| 455 | Radius = Block radius |
---|
| 456 | Fdim = Fractal dimension |
---|
| 457 | L = correlation Length |
---|
| 458 | SDLB = SDL block |
---|
| 459 | SDLS = SDL solvent |
---|
| 460 | bkd = background |
---|
| 461 | """ |
---|
| 462 | def setUp(self): |
---|
[f629e346] | 463 | from sans.models.FractalModel import FractalModel |
---|
[829eee9] | 464 | self.model= FractalModel() |
---|
[6b8399b] | 465 | self.r0 = 5.0 |
---|
| 466 | self.sldp = 2.0e-6 |
---|
| 467 | self.sldm = 6.35e-6 |
---|
| 468 | self.phi = 0.05 |
---|
| 469 | self.Df = 2 |
---|
| 470 | self.corr = 100.0 |
---|
| 471 | self.bck = 1.0 |
---|
| 472 | |
---|
| 473 | self.model.setParam('scale', self.phi) |
---|
[ec658c85] | 474 | self.model.setParam('radius', self.r0) |
---|
[6b8399b] | 475 | self.model.setParam('fractal_dim',self.Df) |
---|
[ec658c85] | 476 | self.model.setParam('cor_length', self.corr) |
---|
| 477 | self.model.setParam('sldBlock', self.sldp) |
---|
| 478 | self.model.setParam('sldSolv', self.sldm) |
---|
[6b8399b] | 479 | self.model.setParam('background', self.bck) |
---|
| 480 | |
---|
| 481 | def _func(self, x): |
---|
| 482 | r0 = self.r0 |
---|
| 483 | sldp = self.sldp |
---|
| 484 | sldm = self.sldm |
---|
| 485 | phi = self.phi |
---|
| 486 | Df = self.Df |
---|
| 487 | corr = self.corr |
---|
| 488 | bck = self.bck |
---|
[f629e346] | 489 | |
---|
| 490 | pq = 1.0e8*phi*4.0/3.0*math.pi*r0*r0*r0*(sldp-sldm)*(sldp-sldm)*math.pow((3*(math.sin(x*r0) - x*r0*math.cos(x*r0))/math.pow((x*r0),3)),2); |
---|
| 491 | |
---|
| 492 | sq = Df*math.exp(gammaln(Df-1.0))*math.sin((Df-1.0)*math.atan(x*corr)); |
---|
| 493 | sq /= math.pow((x*r0),Df) * math.pow((1.0 + 1.0/(x*corr)/(x*corr)),((Df-1.0)/2.0)); |
---|
| 494 | sq += 1.0; |
---|
[829eee9] | 495 | |
---|
[a07161b] | 496 | #self.assertAlmostEqual(self.model._scatterRanDom(x), pq, 8 ) |
---|
| 497 | #self.assertEqual(self.model._Block(x),sq ) |
---|
[829eee9] | 498 | |
---|
[6b8399b] | 499 | return sq*pq+bck |
---|
| 500 | |
---|
| 501 | def test1D(self): |
---|
| 502 | x = 0.001 |
---|
| 503 | |
---|
| 504 | iq = self._func(x) |
---|
| 505 | self.assertEqual(self.model.run(x), iq) |
---|
| 506 | self.assertEqual(self.model.runXY(x), iq) |
---|
| 507 | |
---|
| 508 | def test2D(self): |
---|
| 509 | x = 1.0 |
---|
| 510 | y = 2.0 |
---|
| 511 | r = math.sqrt(x**2 + y**2) |
---|
| 512 | phi = math.atan2(y, x) |
---|
| 513 | iq_x = self._func(x) |
---|
| 514 | iq_y = self._func(y) |
---|
| 515 | |
---|
[a55fac1] | 516 | #self.assertEqual(self.model.run([r, phi]), iq_x*iq_y) |
---|
| 517 | self.assertEqual(self.model.run([r, phi]), self.model.run(r)) |
---|
| 518 | #self.assertEqual(self.model.runXY([x,y]), iq_x*iq_y) |
---|
| 519 | self.assertEqual(self.model.runXY([x,y]), self.model.run(r)) |
---|
[829eee9] | 520 | |
---|
[914caf52] | 521 | if __name__ == '__main__': |
---|
| 522 | unittest.main() |
---|