[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() |
---|
| 120 | self.model.setParam('Rg', 50.0) |
---|
| 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) |
---|
[829eee9] | 128 | |
---|
[f629e346] | 129 | # User enter zero as a value of x |
---|
| 130 | # An exceptio is raised |
---|
| 131 | self.assertRaises(ZeroDivisionError, self.model.run, 0.0) |
---|
[829eee9] | 132 | |
---|
| 133 | def test2D(self): |
---|
[a55fac1] | 134 | #value = self._func(50.0, 1.0, 0.001, 1.0)*self._func(50.0, 1.0, 0.001, 2.0) |
---|
| 135 | value = self._func(50.0, 1.0, 0.001, math.sqrt(5.0)) |
---|
[829eee9] | 136 | self.assertEqual(self.model.runXY([1.0,2.0]), value) |
---|
| 137 | |
---|
| 138 | def test2Dphi(self): |
---|
| 139 | x = 1.0 |
---|
| 140 | y = 2.0 |
---|
| 141 | r = math.sqrt(x**2 + y**2) |
---|
| 142 | phi = math.atan2(y, x) |
---|
| 143 | |
---|
| 144 | value = self._func(50.0, 1.0, 0.001, 1.0)*self._func(50.0, 1.0, 0.001, 2.0) |
---|
| 145 | self.assertAlmostEquals(self.model.run([r, phi]), value,1) |
---|
| 146 | |
---|
| 147 | |
---|
| 148 | class TestLorentz(unittest.TestCase): |
---|
| 149 | """ |
---|
| 150 | Unit tests for Lorentz function |
---|
| 151 | |
---|
| 152 | F(x) = scale/( 1 + (x*L)^2 ) + bkd |
---|
| 153 | |
---|
| 154 | The model has three parameters: |
---|
| 155 | L = screen Length |
---|
| 156 | scale = scale factor |
---|
| 157 | bkd = incoherent background |
---|
| 158 | """ |
---|
[f629e346] | 159 | def _func(self, I0 , L, bgd, qval): |
---|
| 160 | return I0/(1.0 + (qval*L)*(qval*L)) + bgd |
---|
| 161 | |
---|
[829eee9] | 162 | def setUp(self): |
---|
[f629e346] | 163 | from sans.models.LorentzModel import LorentzModel |
---|
[829eee9] | 164 | self.model= LorentzModel() |
---|
| 165 | |
---|
| 166 | def test1D(self): |
---|
| 167 | self.model.setParam('scale', 100.0) |
---|
[f629e346] | 168 | self.model.setParam('Length', 50.0) |
---|
| 169 | self.model.setParam('background', 1.0) |
---|
[829eee9] | 170 | |
---|
| 171 | self.assertEqual(self.model.run(0.0), 101.0) |
---|
| 172 | self.assertEqual(self.model.run(2.0), self._func(100.0, 50.0, 1.0, 2.0)) |
---|
[6b8399b] | 173 | self.assertEqual(self.model.runXY(2.0), self._func(100.0, 50.0, 1.0, 2.0)) |
---|
[829eee9] | 174 | |
---|
| 175 | def test2D(self): |
---|
| 176 | self.model.setParam('scale', 100.0) |
---|
[f629e346] | 177 | self.model.setParam('Length', 50.0) |
---|
| 178 | self.model.setParam('background', 1.0) |
---|
[829eee9] | 179 | |
---|
[a55fac1] | 180 | #value = self._func(100.0, 50.0, 1.0, 1.0)*self._func(100.0, 50.0, 1.0, 2.0) |
---|
| 181 | value = self._func(100.0, 50.0, 1.0, math.sqrt(5.0)) |
---|
[829eee9] | 182 | self.assertEqual(self.model.runXY([1.0,2.0]), value) |
---|
| 183 | |
---|
| 184 | def test2Dphi(self): |
---|
| 185 | self.model.setParam('scale', 100.0) |
---|
[f629e346] | 186 | self.model.setParam('Length', 50.0) |
---|
| 187 | self.model.setParam('background', 1.0) |
---|
[829eee9] | 188 | |
---|
| 189 | x = 1.0 |
---|
| 190 | y = 2.0 |
---|
| 191 | r = math.sqrt(x**2 + y**2) |
---|
| 192 | phi = math.atan2(y, x) |
---|
| 193 | |
---|
| 194 | value = self._func(100.0, 50.0, 1.0, x)*self._func(100.0, 50.0, 1.0, y) |
---|
| 195 | self.assertAlmostEquals(self.model.run([r, phi]), value,1) |
---|
| 196 | |
---|
| 197 | class TestDAB(unittest.TestCase): |
---|
| 198 | """ |
---|
| 199 | Unit tests for DAB function |
---|
| 200 | |
---|
| 201 | F(x) = scale/( 1 + (x*L)^2 )^(2) + bkd |
---|
| 202 | |
---|
| 203 | The model has three parameters: |
---|
| 204 | L = Correlation Length |
---|
| 205 | scale = scale factor |
---|
| 206 | bkd = incoherent background |
---|
| 207 | """ |
---|
[f629e346] | 208 | def _func(self, Izero, range, incoh, qval): |
---|
| 209 | return Izero/pow((1.0 + (qval*range)*(qval*range)),2) + incoh |
---|
[829eee9] | 210 | |
---|
| 211 | def setUp(self): |
---|
[f629e346] | 212 | from sans.models.DABModel import DABModel |
---|
[829eee9] | 213 | self.model= DABModel() |
---|
[6b8399b] | 214 | self.scale = 10.0 |
---|
| 215 | self.length = 40.0 |
---|
| 216 | self.back = 1.0 |
---|
| 217 | |
---|
| 218 | self.model.setParam('scale', self.scale) |
---|
| 219 | self.model.setParam('Length', self.length) |
---|
| 220 | self.model.setParam('background', self.back) |
---|
[829eee9] | 221 | |
---|
| 222 | def test1D(self): |
---|
| 223 | |
---|
[6b8399b] | 224 | self.assertEqual(self.model.run(0.0), self.scale+self.back) |
---|
| 225 | self.assertEqual(self.model.run(2.0), self._func(self.scale, self.length, self.back, 2.0)) |
---|
| 226 | self.assertEqual(self.model.runXY(2.0), self._func(self.scale, self.length, self.back, 2.0)) |
---|
[829eee9] | 227 | |
---|
| 228 | def test2D(self): |
---|
[a55fac1] | 229 | #value = self._func(self.scale, self.length, self.back, 1.0)*self._func(self.scale, self.length, self.back, 2.0) |
---|
| 230 | value = self._func(self.scale, self.length, self.back, math.sqrt(5.0)) |
---|
[829eee9] | 231 | self.assertEqual(self.model.runXY([1.0,2.0]), value) |
---|
| 232 | |
---|
| 233 | def test2Dphi(self): |
---|
| 234 | x = 1.0 |
---|
| 235 | y = 2.0 |
---|
| 236 | r = math.sqrt(x**2 + y**2) |
---|
| 237 | phi = math.atan2(y, x) |
---|
| 238 | |
---|
[6b8399b] | 239 | value = self._func(self.scale, self.length, self.back, x)*self._func(self.scale, self.length, self.back, y) |
---|
[f03b027] | 240 | self.assertAlmostEquals(self.model.run([r, phi]), value,1) |
---|
[914caf52] | 241 | |
---|
[829eee9] | 242 | class TestPowerLaw(unittest.TestCase): |
---|
| 243 | """ |
---|
| 244 | Unit tests for PowerLaw function |
---|
| 245 | |
---|
| 246 | F(x) = scale* (x)^(m) + bkd |
---|
| 247 | |
---|
| 248 | The model has three parameters: |
---|
| 249 | m = power |
---|
| 250 | scale = scale factor |
---|
| 251 | bkd = incoherent background |
---|
| 252 | """ |
---|
[f629e346] | 253 | def _func(self, a, m, bgd, qval): |
---|
| 254 | return a*math.pow(qval,-m) + bgd |
---|
| 255 | |
---|
[829eee9] | 256 | |
---|
| 257 | def setUp(self): |
---|
[f629e346] | 258 | from sans.models.PowerLawModel import PowerLawModel |
---|
[829eee9] | 259 | self.model= PowerLawModel() |
---|
| 260 | |
---|
| 261 | def test1D(self): |
---|
| 262 | self.model.setParam('scale', math.exp(-6)) |
---|
| 263 | self.model.setParam('m', 4.0) |
---|
[f629e346] | 264 | self.model.setParam('background', 1.0) |
---|
[829eee9] | 265 | |
---|
[f629e346] | 266 | #self.assertEqual(self.model.run(0.0), 1.0) |
---|
[829eee9] | 267 | self.assertEqual(self.model.run(2.0), self._func(math.exp(-6), 4.0, 1.0, 2.0)) |
---|
[6b8399b] | 268 | self.assertEqual(self.model.runXY(2.0), self._func(math.exp(-6), 4.0, 1.0, 2.0)) |
---|
[f629e346] | 269 | |
---|
| 270 | def testlimit(self): |
---|
| 271 | self.model.setParam('scale', math.exp(-6)) |
---|
| 272 | self.model.setParam('m', -4.0) |
---|
| 273 | self.model.setParam('background', 1.0) |
---|
| 274 | |
---|
| 275 | self.assertEqual(self.model.run(0.0), 1.0) |
---|
[829eee9] | 276 | |
---|
| 277 | def test2D(self): |
---|
| 278 | self.model.setParam('scale', math.exp(-6)) |
---|
| 279 | self.model.setParam('m', 4.0) |
---|
[f629e346] | 280 | self.model.setParam('background', 1.0) |
---|
[829eee9] | 281 | |
---|
[a55fac1] | 282 | #value = self._func(math.exp(-6), 4.0, 1.0, 1.0)\ |
---|
| 283 | #*self._func(math.exp(-6), 4.0, 1.0, 2.0) |
---|
| 284 | value = self._func(math.exp(-6), 4.0, 1.0, math.sqrt(5.0)) |
---|
[829eee9] | 285 | |
---|
| 286 | self.assertEqual(self.model.runXY([1.0,2.0]), value) |
---|
| 287 | |
---|
| 288 | def test2Dphi(self): |
---|
| 289 | self.model.setParam('scale', math.exp(-6)) |
---|
| 290 | self.model.setParam('m', 4.0) |
---|
[f629e346] | 291 | self.model.setParam('background', 1.0) |
---|
[829eee9] | 292 | |
---|
| 293 | x = 1.0 |
---|
| 294 | y = 2.0 |
---|
| 295 | r = math.sqrt(x**2 + y**2) |
---|
| 296 | phi = math.atan2(y, x) |
---|
| 297 | |
---|
| 298 | value = self._func(math.exp(-6), 4.0, 1.0, x)\ |
---|
| 299 | *self._func(math.exp(-6), 4.0, 1.0, y) |
---|
| 300 | self.assertAlmostEquals(self.model.run([r, phi]), value,1) |
---|
| 301 | |
---|
| 302 | class TestTeubnerStrey(unittest.TestCase): |
---|
| 303 | """ |
---|
| 304 | Unit tests for PowerLaw function |
---|
| 305 | |
---|
| 306 | F(x) = 1/( scale + c1*(x)^(2)+ c2*(x)^(4)) + bkd |
---|
| 307 | |
---|
| 308 | The model has Four parameters: |
---|
| 309 | scale = scale factor |
---|
| 310 | c1 = constant |
---|
| 311 | c2 = constant |
---|
| 312 | bkd = incoherent background |
---|
| 313 | """ |
---|
[f629e346] | 314 | def _func(self, scale, c1, c2, bck, q): |
---|
| 315 | |
---|
| 316 | q2 = q*q; |
---|
| 317 | q4 = q2*q2; |
---|
| 318 | |
---|
| 319 | return 1.0/(scale + c1*q2+c2*q4) + bck |
---|
[829eee9] | 320 | |
---|
| 321 | def setUp(self): |
---|
[f629e346] | 322 | from sans.models.TeubnerStreyModel import TeubnerStreyModel |
---|
[829eee9] | 323 | self.model= TeubnerStreyModel() |
---|
| 324 | |
---|
| 325 | def test1D(self): |
---|
| 326 | |
---|
| 327 | self.model.setParam('c1', -30.0) |
---|
| 328 | self.model.setParam('c2', 5000.0) |
---|
| 329 | self.model.setParam('scale', 0.1) |
---|
[f629e346] | 330 | self.model.setParam('background', 0.1) |
---|
[829eee9] | 331 | #self.assertEqual(1/(math.sqrt(4)), math.pow(4,-1/2)) |
---|
[f629e346] | 332 | #self.assertEqual(self.model.TeubnerStreyLengths(),False ) |
---|
[829eee9] | 333 | |
---|
| 334 | self.assertEqual(self.model.run(0.0), 10.1) |
---|
| 335 | self.assertEqual(self.model.run(2.0), self._func(0.1,-30.0,5000.0,0.1,2.0)) |
---|
[6b8399b] | 336 | self.assertEqual(self.model.runXY(2.0), self._func(0.1,-30.0,5000.0,0.1,2.0)) |
---|
[829eee9] | 337 | |
---|
| 338 | def test2D(self): |
---|
| 339 | self.model.setParam('c1', -30.0) |
---|
| 340 | self.model.setParam('c2', 5000.0) |
---|
| 341 | self.model.setParam('scale', 0.1) |
---|
[f629e346] | 342 | self.model.setParam('background', 0.1) |
---|
[a55fac1] | 343 | #value = self._func(0.1,-30.0,5000.0,0.1, 1.0)\ |
---|
| 344 | #*self._func(0.1,-30.0,5000.0,0.1, 2.0) |
---|
| 345 | value = self._func(0.1,-30.0,5000.0,0.1, math.sqrt(5.0)) |
---|
[829eee9] | 346 | |
---|
| 347 | self.assertEqual(self.model.runXY([1.0,2.0]), value) |
---|
| 348 | |
---|
| 349 | def test2Dphi(self): |
---|
| 350 | self.model.setParam('c1', -30.0) |
---|
| 351 | self.model.setParam('c2', 5000.0) |
---|
| 352 | self.model.setParam('scale', 0.1) |
---|
[f629e346] | 353 | self.model.setParam('background', 0.1) |
---|
[829eee9] | 354 | |
---|
| 355 | x = 1.0 |
---|
| 356 | y = 2.0 |
---|
| 357 | r = math.sqrt(x**2 + y**2) |
---|
| 358 | phi = math.atan2(y, x) |
---|
| 359 | |
---|
[a55fac1] | 360 | #value = self._func(0.1,-30.0,5000.0,0.1, x)\ |
---|
| 361 | #*self._func(0.1,-30.0,5000.0,0.1, y) |
---|
| 362 | value = self._func(0.1,-30.0,5000.0,0.1, r) |
---|
[829eee9] | 363 | self.assertAlmostEquals(self.model.run([r, phi]), value,1) |
---|
| 364 | |
---|
| 365 | class TestBEPolyelectrolyte(unittest.TestCase): |
---|
| 366 | """ |
---|
| 367 | Unit tests for BEPolyelectrolyte function |
---|
| 368 | |
---|
| 369 | F(x) = K*1/(4*pi()*Lb*(alpha)^(2)*(q^(2)+k2)/(1+(r02)^(2))*(q^(2)+k2)\ |
---|
| 370 | *(q^(2)-(12*h*C/b^(2))) |
---|
| 371 | |
---|
| 372 | The model has Eight parameters: |
---|
| 373 | K = Constrast factor of the polymer |
---|
| 374 | Lb = Bjerrum length |
---|
| 375 | H = virial parameter |
---|
| 376 | B = monomer length |
---|
| 377 | Cs = Concentration of monovalent salt |
---|
| 378 | alpha = ionazation degree |
---|
| 379 | C = polymer molar concentration |
---|
| 380 | bkd = background |
---|
| 381 | """ |
---|
| 382 | def _func(self, K, Lb, H, B, Cs, alpha, C, bkd, r02, k2, x): |
---|
| 383 | return (K /( (4*math.pi *Lb*(alpha**2)*(x**2 +k2)) *( (1 +(r02**2)) \ |
---|
| 384 | *((x**2) + k2)*((x**2) -(12 * H * C/(B**2))) )))+ bkd |
---|
| 385 | |
---|
| 386 | def setUp(self): |
---|
[f629e346] | 387 | from sans.models.BEPolyelectrolyte import BEPolyelectrolyte |
---|
[829eee9] | 388 | self.model= BEPolyelectrolyte() |
---|
| 389 | |
---|
[f629e346] | 390 | self.K = 10.0 |
---|
| 391 | self.Lb = 6.5 |
---|
| 392 | self.h = 11 |
---|
| 393 | self.b = 13 |
---|
| 394 | self.Cs = 0.1 |
---|
| 395 | self.alpha = 0.05 |
---|
| 396 | self.C = .7 |
---|
| 397 | self.Bkd =0.01 |
---|
| 398 | |
---|
| 399 | self.model.setParam('K', self.K) |
---|
| 400 | self.model.setParam('Lb', self.Lb) |
---|
| 401 | self.model.setParam('H', self.h) |
---|
| 402 | self.model.setParam('B', self.b) |
---|
| 403 | self.model.setParam('Cs',self.Cs) |
---|
| 404 | self.model.setParam('alpha', self.alpha) |
---|
| 405 | self.model.setParam('C', self.C) |
---|
| 406 | self.model.setParam('background', self.Bkd) |
---|
| 407 | |
---|
| 408 | def _func(self, q): |
---|
| 409 | |
---|
| 410 | Ca = self.C *6.022136e-4 |
---|
| 411 | Csa = self.Cs * 6.022136e-4 |
---|
| 412 | k2= 4*math.pi*self.Lb*(2*self.Cs+self.alpha*Ca) |
---|
| 413 | |
---|
| 414 | r02 = 1./self.alpha / Ca**0.5*( self.b / (48*math.pi*self.Lb)**0.5 ) |
---|
| 415 | q2 = q**2 |
---|
| 416 | 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 |
---|
| 417 | return Sq |
---|
| 418 | |
---|
[829eee9] | 419 | def test1D(self): |
---|
| 420 | |
---|
[f629e346] | 421 | |
---|
| 422 | q = 0.001 |
---|
| 423 | |
---|
| 424 | self.assertEqual(self.model.run(q), self._func(q)) |
---|
[6b8399b] | 425 | self.assertEqual(self.model.runXY(q), self._func(q)) |
---|
[f629e346] | 426 | |
---|
[829eee9] | 427 | def test2D(self): |
---|
[a55fac1] | 428 | #self.assertAlmostEquals(self.model.runXY([1.0,2.0]), self._func(1.0)*self._func(2.0), 8) |
---|
| 429 | self.assertAlmostEquals(self.model.runXY([1.0,2.0]), self._func(math.sqrt(1.0+2.0**2)), 8) |
---|
[829eee9] | 430 | |
---|
| 431 | def test2Dphi(self): |
---|
[f629e346] | 432 | |
---|
[829eee9] | 433 | x = 1.0 |
---|
| 434 | y = 2.0 |
---|
| 435 | r = math.sqrt(x**2 + y**2) |
---|
| 436 | phi = math.atan2(y, x) |
---|
| 437 | |
---|
[a55fac1] | 438 | self.assertAlmostEquals(self.model.run([r, phi]), self._func(r), 8) |
---|
[829eee9] | 439 | |
---|
| 440 | class TestFractalModel(unittest.TestCase): |
---|
| 441 | """ |
---|
| 442 | Unit tests for Number Density Fractal function |
---|
| 443 | F(x)= P(x)*S(x) + bkd |
---|
| 444 | The model has Seven parameters: |
---|
| 445 | scale = Volume fraction |
---|
| 446 | Radius = Block radius |
---|
| 447 | Fdim = Fractal dimension |
---|
| 448 | L = correlation Length |
---|
| 449 | SDLB = SDL block |
---|
| 450 | SDLS = SDL solvent |
---|
| 451 | bkd = background |
---|
| 452 | """ |
---|
| 453 | def setUp(self): |
---|
[f629e346] | 454 | from sans.models.FractalModel import FractalModel |
---|
[829eee9] | 455 | self.model= FractalModel() |
---|
[6b8399b] | 456 | self.r0 = 5.0 |
---|
| 457 | self.sldp = 2.0e-6 |
---|
| 458 | self.sldm = 6.35e-6 |
---|
| 459 | self.phi = 0.05 |
---|
| 460 | self.Df = 2 |
---|
| 461 | self.corr = 100.0 |
---|
| 462 | self.bck = 1.0 |
---|
| 463 | |
---|
| 464 | self.model.setParam('scale', self.phi) |
---|
| 465 | self.model.setParam('Radius', self.r0) |
---|
| 466 | self.model.setParam('fractal_dim',self.Df) |
---|
| 467 | self.model.setParam('corr_length', self.corr) |
---|
| 468 | self.model.setParam('block_sld', self.sldp) |
---|
| 469 | self.model.setParam('solvent_sld', self.sldm) |
---|
| 470 | self.model.setParam('background', self.bck) |
---|
| 471 | |
---|
| 472 | def _func(self, x): |
---|
| 473 | r0 = self.r0 |
---|
| 474 | sldp = self.sldp |
---|
| 475 | sldm = self.sldm |
---|
| 476 | phi = self.phi |
---|
| 477 | Df = self.Df |
---|
| 478 | corr = self.corr |
---|
| 479 | bck = self.bck |
---|
[f629e346] | 480 | |
---|
| 481 | 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); |
---|
| 482 | |
---|
| 483 | sq = Df*math.exp(gammaln(Df-1.0))*math.sin((Df-1.0)*math.atan(x*corr)); |
---|
| 484 | sq /= math.pow((x*r0),Df) * math.pow((1.0 + 1.0/(x*corr)/(x*corr)),((Df-1.0)/2.0)); |
---|
| 485 | sq += 1.0; |
---|
[829eee9] | 486 | |
---|
[6b8399b] | 487 | self.assertAlmostEqual(self.model._scatterRanDom(x), pq, 8 ) |
---|
[f629e346] | 488 | self.assertEqual(self.model._Block(x),sq ) |
---|
[829eee9] | 489 | |
---|
[6b8399b] | 490 | return sq*pq+bck |
---|
| 491 | |
---|
| 492 | def test1D(self): |
---|
| 493 | x = 0.001 |
---|
| 494 | |
---|
| 495 | iq = self._func(x) |
---|
| 496 | self.assertEqual(self.model.run(x), iq) |
---|
| 497 | self.assertEqual(self.model.runXY(x), iq) |
---|
| 498 | |
---|
| 499 | def test2D(self): |
---|
| 500 | x = 1.0 |
---|
| 501 | y = 2.0 |
---|
| 502 | r = math.sqrt(x**2 + y**2) |
---|
| 503 | phi = math.atan2(y, x) |
---|
| 504 | iq_x = self._func(x) |
---|
| 505 | iq_y = self._func(y) |
---|
| 506 | |
---|
[a55fac1] | 507 | #self.assertEqual(self.model.run([r, phi]), iq_x*iq_y) |
---|
| 508 | self.assertEqual(self.model.run([r, phi]), self.model.run(r)) |
---|
| 509 | #self.assertEqual(self.model.runXY([x,y]), iq_x*iq_y) |
---|
| 510 | self.assertEqual(self.model.runXY([x,y]), self.model.run(r)) |
---|
[829eee9] | 511 | |
---|
[914caf52] | 512 | if __name__ == '__main__': |
---|
| 513 | unittest.main() |
---|