[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) |
---|
[f1e0c1eb] | 250 | |
---|
| 251 | |
---|
[829eee9] | 252 | class TestPowerLaw(unittest.TestCase): |
---|
| 253 | """ |
---|
| 254 | Unit tests for PowerLaw function |
---|
| 255 | |
---|
| 256 | F(x) = scale* (x)^(m) + bkd |
---|
| 257 | |
---|
| 258 | The model has three parameters: |
---|
| 259 | m = power |
---|
| 260 | scale = scale factor |
---|
| 261 | bkd = incoherent background |
---|
| 262 | """ |
---|
[f629e346] | 263 | def _func(self, a, m, bgd, qval): |
---|
| 264 | return a*math.pow(qval,-m) + bgd |
---|
| 265 | |
---|
[829eee9] | 266 | |
---|
| 267 | def setUp(self): |
---|
[f629e346] | 268 | from sans.models.PowerLawModel import PowerLawModel |
---|
[829eee9] | 269 | self.model= PowerLawModel() |
---|
| 270 | |
---|
| 271 | def test1D(self): |
---|
| 272 | self.model.setParam('scale', math.exp(-6)) |
---|
| 273 | self.model.setParam('m', 4.0) |
---|
[f629e346] | 274 | self.model.setParam('background', 1.0) |
---|
[829eee9] | 275 | |
---|
[f629e346] | 276 | #self.assertEqual(self.model.run(0.0), 1.0) |
---|
[829eee9] | 277 | self.assertEqual(self.model.run(2.0), self._func(math.exp(-6), 4.0, 1.0, 2.0)) |
---|
[6b8399b] | 278 | self.assertEqual(self.model.runXY(2.0), self._func(math.exp(-6), 4.0, 1.0, 2.0)) |
---|
[f629e346] | 279 | |
---|
| 280 | def testlimit(self): |
---|
| 281 | self.model.setParam('scale', math.exp(-6)) |
---|
| 282 | self.model.setParam('m', -4.0) |
---|
| 283 | self.model.setParam('background', 1.0) |
---|
| 284 | |
---|
| 285 | self.assertEqual(self.model.run(0.0), 1.0) |
---|
[829eee9] | 286 | |
---|
| 287 | def test2D(self): |
---|
| 288 | self.model.setParam('scale', math.exp(-6)) |
---|
| 289 | self.model.setParam('m', 4.0) |
---|
[f629e346] | 290 | self.model.setParam('background', 1.0) |
---|
[829eee9] | 291 | |
---|
[a55fac1] | 292 | #value = self._func(math.exp(-6), 4.0, 1.0, 1.0)\ |
---|
| 293 | #*self._func(math.exp(-6), 4.0, 1.0, 2.0) |
---|
| 294 | value = self._func(math.exp(-6), 4.0, 1.0, math.sqrt(5.0)) |
---|
[829eee9] | 295 | |
---|
| 296 | self.assertEqual(self.model.runXY([1.0,2.0]), value) |
---|
| 297 | |
---|
| 298 | def test2Dphi(self): |
---|
| 299 | self.model.setParam('scale', math.exp(-6)) |
---|
| 300 | self.model.setParam('m', 4.0) |
---|
[f629e346] | 301 | self.model.setParam('background', 1.0) |
---|
[829eee9] | 302 | |
---|
| 303 | x = 1.0 |
---|
| 304 | y = 2.0 |
---|
| 305 | r = math.sqrt(x**2 + y**2) |
---|
| 306 | phi = math.atan2(y, x) |
---|
| 307 | |
---|
| 308 | value = self._func(math.exp(-6), 4.0, 1.0, x)\ |
---|
| 309 | *self._func(math.exp(-6), 4.0, 1.0, y) |
---|
| 310 | self.assertAlmostEquals(self.model.run([r, phi]), value,1) |
---|
| 311 | |
---|
| 312 | class TestTeubnerStrey(unittest.TestCase): |
---|
| 313 | """ |
---|
| 314 | Unit tests for PowerLaw function |
---|
| 315 | |
---|
| 316 | F(x) = 1/( scale + c1*(x)^(2)+ c2*(x)^(4)) + bkd |
---|
| 317 | |
---|
| 318 | The model has Four parameters: |
---|
| 319 | scale = scale factor |
---|
| 320 | c1 = constant |
---|
| 321 | c2 = constant |
---|
| 322 | bkd = incoherent background |
---|
| 323 | """ |
---|
[f629e346] | 324 | def _func(self, scale, c1, c2, bck, q): |
---|
| 325 | |
---|
| 326 | q2 = q*q; |
---|
| 327 | q4 = q2*q2; |
---|
| 328 | |
---|
| 329 | return 1.0/(scale + c1*q2+c2*q4) + bck |
---|
[829eee9] | 330 | |
---|
| 331 | def setUp(self): |
---|
[f629e346] | 332 | from sans.models.TeubnerStreyModel import TeubnerStreyModel |
---|
[829eee9] | 333 | self.model= TeubnerStreyModel() |
---|
| 334 | |
---|
| 335 | def test1D(self): |
---|
| 336 | |
---|
| 337 | self.model.setParam('c1', -30.0) |
---|
| 338 | self.model.setParam('c2', 5000.0) |
---|
| 339 | self.model.setParam('scale', 0.1) |
---|
[f629e346] | 340 | self.model.setParam('background', 0.1) |
---|
[829eee9] | 341 | #self.assertEqual(1/(math.sqrt(4)), math.pow(4,-1/2)) |
---|
[f629e346] | 342 | #self.assertEqual(self.model.TeubnerStreyLengths(),False ) |
---|
[829eee9] | 343 | |
---|
| 344 | self.assertEqual(self.model.run(0.0), 10.1) |
---|
| 345 | self.assertEqual(self.model.run(2.0), self._func(0.1,-30.0,5000.0,0.1,2.0)) |
---|
[6b8399b] | 346 | self.assertEqual(self.model.runXY(2.0), self._func(0.1,-30.0,5000.0,0.1,2.0)) |
---|
[829eee9] | 347 | |
---|
| 348 | def test2D(self): |
---|
| 349 | self.model.setParam('c1', -30.0) |
---|
| 350 | self.model.setParam('c2', 5000.0) |
---|
| 351 | self.model.setParam('scale', 0.1) |
---|
[f629e346] | 352 | self.model.setParam('background', 0.1) |
---|
[a55fac1] | 353 | #value = self._func(0.1,-30.0,5000.0,0.1, 1.0)\ |
---|
| 354 | #*self._func(0.1,-30.0,5000.0,0.1, 2.0) |
---|
| 355 | value = self._func(0.1,-30.0,5000.0,0.1, math.sqrt(5.0)) |
---|
[829eee9] | 356 | |
---|
| 357 | self.assertEqual(self.model.runXY([1.0,2.0]), value) |
---|
| 358 | |
---|
| 359 | def test2Dphi(self): |
---|
| 360 | self.model.setParam('c1', -30.0) |
---|
| 361 | self.model.setParam('c2', 5000.0) |
---|
| 362 | self.model.setParam('scale', 0.1) |
---|
[f629e346] | 363 | self.model.setParam('background', 0.1) |
---|
[829eee9] | 364 | |
---|
| 365 | x = 1.0 |
---|
| 366 | y = 2.0 |
---|
| 367 | r = math.sqrt(x**2 + y**2) |
---|
| 368 | phi = math.atan2(y, x) |
---|
| 369 | |
---|
[a55fac1] | 370 | #value = self._func(0.1,-30.0,5000.0,0.1, x)\ |
---|
| 371 | #*self._func(0.1,-30.0,5000.0,0.1, y) |
---|
| 372 | value = self._func(0.1,-30.0,5000.0,0.1, r) |
---|
[829eee9] | 373 | self.assertAlmostEquals(self.model.run([r, phi]), value,1) |
---|
| 374 | |
---|
| 375 | class TestBEPolyelectrolyte(unittest.TestCase): |
---|
| 376 | """ |
---|
| 377 | Unit tests for BEPolyelectrolyte function |
---|
| 378 | |
---|
| 379 | F(x) = K*1/(4*pi()*Lb*(alpha)^(2)*(q^(2)+k2)/(1+(r02)^(2))*(q^(2)+k2)\ |
---|
| 380 | *(q^(2)-(12*h*C/b^(2))) |
---|
| 381 | |
---|
| 382 | The model has Eight parameters: |
---|
| 383 | K = Constrast factor of the polymer |
---|
| 384 | Lb = Bjerrum length |
---|
| 385 | H = virial parameter |
---|
| 386 | B = monomer length |
---|
| 387 | Cs = Concentration of monovalent salt |
---|
| 388 | alpha = ionazation degree |
---|
| 389 | C = polymer molar concentration |
---|
| 390 | bkd = background |
---|
| 391 | """ |
---|
| 392 | def _func(self, K, Lb, H, B, Cs, alpha, C, bkd, r02, k2, x): |
---|
| 393 | return (K /( (4*math.pi *Lb*(alpha**2)*(x**2 +k2)) *( (1 +(r02**2)) \ |
---|
| 394 | *((x**2) + k2)*((x**2) -(12 * H * C/(B**2))) )))+ bkd |
---|
| 395 | |
---|
| 396 | def setUp(self): |
---|
[f629e346] | 397 | from sans.models.BEPolyelectrolyte import BEPolyelectrolyte |
---|
[829eee9] | 398 | self.model= BEPolyelectrolyte() |
---|
| 399 | |
---|
[f629e346] | 400 | self.K = 10.0 |
---|
| 401 | self.Lb = 6.5 |
---|
| 402 | self.h = 11 |
---|
| 403 | self.b = 13 |
---|
| 404 | self.Cs = 0.1 |
---|
| 405 | self.alpha = 0.05 |
---|
| 406 | self.C = .7 |
---|
| 407 | self.Bkd =0.01 |
---|
| 408 | |
---|
| 409 | self.model.setParam('K', self.K) |
---|
| 410 | self.model.setParam('Lb', self.Lb) |
---|
| 411 | self.model.setParam('H', self.h) |
---|
| 412 | self.model.setParam('B', self.b) |
---|
| 413 | self.model.setParam('Cs',self.Cs) |
---|
| 414 | self.model.setParam('alpha', self.alpha) |
---|
| 415 | self.model.setParam('C', self.C) |
---|
| 416 | self.model.setParam('background', self.Bkd) |
---|
| 417 | |
---|
| 418 | def _func(self, q): |
---|
| 419 | |
---|
| 420 | Ca = self.C *6.022136e-4 |
---|
| 421 | Csa = self.Cs * 6.022136e-4 |
---|
| 422 | k2= 4*math.pi*self.Lb*(2*self.Cs+self.alpha*Ca) |
---|
| 423 | |
---|
| 424 | r02 = 1./self.alpha / Ca**0.5*( self.b / (48*math.pi*self.Lb)**0.5 ) |
---|
| 425 | q2 = q**2 |
---|
| 426 | 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 |
---|
| 427 | return Sq |
---|
| 428 | |
---|
[829eee9] | 429 | def test1D(self): |
---|
| 430 | |
---|
[f629e346] | 431 | |
---|
| 432 | q = 0.001 |
---|
| 433 | |
---|
| 434 | self.assertEqual(self.model.run(q), self._func(q)) |
---|
[6b8399b] | 435 | self.assertEqual(self.model.runXY(q), self._func(q)) |
---|
[f629e346] | 436 | |
---|
[829eee9] | 437 | def test2D(self): |
---|
[a55fac1] | 438 | #self.assertAlmostEquals(self.model.runXY([1.0,2.0]), self._func(1.0)*self._func(2.0), 8) |
---|
| 439 | self.assertAlmostEquals(self.model.runXY([1.0,2.0]), self._func(math.sqrt(1.0+2.0**2)), 8) |
---|
[829eee9] | 440 | |
---|
| 441 | def test2Dphi(self): |
---|
[f629e346] | 442 | |
---|
[829eee9] | 443 | x = 1.0 |
---|
| 444 | y = 2.0 |
---|
| 445 | r = math.sqrt(x**2 + y**2) |
---|
| 446 | phi = math.atan2(y, x) |
---|
| 447 | |
---|
[a55fac1] | 448 | self.assertAlmostEquals(self.model.run([r, phi]), self._func(r), 8) |
---|
[829eee9] | 449 | |
---|
| 450 | class TestFractalModel(unittest.TestCase): |
---|
| 451 | """ |
---|
| 452 | Unit tests for Number Density Fractal function |
---|
| 453 | F(x)= P(x)*S(x) + bkd |
---|
| 454 | The model has Seven parameters: |
---|
| 455 | scale = Volume fraction |
---|
| 456 | Radius = Block radius |
---|
| 457 | Fdim = Fractal dimension |
---|
| 458 | L = correlation Length |
---|
| 459 | SDLB = SDL block |
---|
| 460 | SDLS = SDL solvent |
---|
| 461 | bkd = background |
---|
| 462 | """ |
---|
| 463 | def setUp(self): |
---|
[f629e346] | 464 | from sans.models.FractalModel import FractalModel |
---|
[829eee9] | 465 | self.model= FractalModel() |
---|
[6b8399b] | 466 | self.r0 = 5.0 |
---|
| 467 | self.sldp = 2.0e-6 |
---|
| 468 | self.sldm = 6.35e-6 |
---|
| 469 | self.phi = 0.05 |
---|
| 470 | self.Df = 2 |
---|
| 471 | self.corr = 100.0 |
---|
| 472 | self.bck = 1.0 |
---|
| 473 | |
---|
| 474 | self.model.setParam('scale', self.phi) |
---|
[ec658c85] | 475 | self.model.setParam('radius', self.r0) |
---|
[6b8399b] | 476 | self.model.setParam('fractal_dim',self.Df) |
---|
[ec658c85] | 477 | self.model.setParam('cor_length', self.corr) |
---|
| 478 | self.model.setParam('sldBlock', self.sldp) |
---|
| 479 | self.model.setParam('sldSolv', self.sldm) |
---|
[6b8399b] | 480 | self.model.setParam('background', self.bck) |
---|
| 481 | |
---|
| 482 | def _func(self, x): |
---|
| 483 | r0 = self.r0 |
---|
| 484 | sldp = self.sldp |
---|
| 485 | sldm = self.sldm |
---|
| 486 | phi = self.phi |
---|
| 487 | Df = self.Df |
---|
| 488 | corr = self.corr |
---|
| 489 | bck = self.bck |
---|
[f629e346] | 490 | |
---|
| 491 | 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); |
---|
| 492 | |
---|
| 493 | sq = Df*math.exp(gammaln(Df-1.0))*math.sin((Df-1.0)*math.atan(x*corr)); |
---|
| 494 | sq /= math.pow((x*r0),Df) * math.pow((1.0 + 1.0/(x*corr)/(x*corr)),((Df-1.0)/2.0)); |
---|
| 495 | sq += 1.0; |
---|
[829eee9] | 496 | |
---|
[a07161b] | 497 | #self.assertAlmostEqual(self.model._scatterRanDom(x), pq, 8 ) |
---|
| 498 | #self.assertEqual(self.model._Block(x),sq ) |
---|
[829eee9] | 499 | |
---|
[6b8399b] | 500 | return sq*pq+bck |
---|
| 501 | |
---|
| 502 | def test1D(self): |
---|
| 503 | x = 0.001 |
---|
| 504 | |
---|
| 505 | iq = self._func(x) |
---|
| 506 | self.assertEqual(self.model.run(x), iq) |
---|
| 507 | self.assertEqual(self.model.runXY(x), iq) |
---|
| 508 | |
---|
| 509 | def test2D(self): |
---|
| 510 | x = 1.0 |
---|
| 511 | y = 2.0 |
---|
| 512 | r = math.sqrt(x**2 + y**2) |
---|
| 513 | phi = math.atan2(y, x) |
---|
| 514 | iq_x = self._func(x) |
---|
| 515 | iq_y = self._func(y) |
---|
| 516 | |
---|
[a55fac1] | 517 | #self.assertEqual(self.model.run([r, phi]), iq_x*iq_y) |
---|
| 518 | self.assertEqual(self.model.run([r, phi]), self.model.run(r)) |
---|
| 519 | #self.assertEqual(self.model.runXY([x,y]), iq_x*iq_y) |
---|
| 520 | self.assertEqual(self.model.runXY([x,y]), self.model.run(r)) |
---|
[829eee9] | 521 | |
---|
[914caf52] | 522 | if __name__ == '__main__': |
---|
[f1e0c1eb] | 523 | unittest.main() |
---|
| 524 | |
---|