Changeset 829eee9 in sasview for sansmodels/src/sans/models
- Timestamp:
- Mar 24, 2008 9:05:53 AM (17 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 3db3895
- Parents:
- 15f0e94
- Location:
- sansmodels/src/sans/models
- Files:
-
- 10 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sansmodels/src/sans/models/test/utest_nonshape.py
rf03b027 r829eee9 5 5 6 6 import unittest, time, math 7 from scipy.special import erf,gamma 7 8 8 9 # Disable "missing docstring" complaint … … 13 14 # pylint: disable-msg=R0201 14 15 15 16 import scipy 16 17 class TestGuinier(unittest.TestCase): 17 18 """ … … 57 58 #self.assertEqual(self.model.run([r, phi]), value) 58 59 self.assertAlmostEquals(self.model.run([r, phi]), value,1) 60 61 59 62 class TestPorod(unittest.TestCase): 60 63 """ … … 88 91 89 92 value = self._func(2.0, 1.0)*self._func(2.0, 2.0) 90 91 #self.assertEqual(self.model.run([r, phi]), value) 92 self.assertAlmostEquals(self.model.run([r, phi]), value,1) 93 self.assertAlmostEquals(self.model.run([r, phi]), value,1) 94 95 class TestDebye(unittest.TestCase): 96 """ 97 Unit tests for Debye function 98 99 F(x) = 2( exp(-x)+x -1 )/x**2 100 101 The model has three parameters: 102 Rg = radius of gyration 103 scale = scale factor 104 bkd = Constant background 105 """ 106 def _func(self, Rg, scale, bkg, x): 107 y = (Rg * x)**2 108 return scale * (2*(math.exp(-y) + y -1)/y**2) + bkg 109 110 def setUp(self): 111 from DebyeModel import DebyeModel 112 self.model= DebyeModel() 113 self.model.setParam('Rg', 50.0) 114 self.model.setParam('scale',1.0) 115 self.model.setParam('bkd',0.001) 116 117 def test1D(self): 118 value = self._func(50.0, 1.0, 0.001, 2.0) 119 self.assertEqual(self.model.run(2.0), value) 120 121 #user enter zero as a value of x 122 self.assertEqual(self.model.run(0.0),False) 123 124 125 def test2D(self): 126 value = self._func(50.0, 1.0, 0.001, 1.0)*self._func(50.0, 1.0, 0.001, 2.0) 127 self.assertEqual(self.model.runXY([1.0,2.0]), value) 128 129 def test2Dphi(self): 130 x = 1.0 131 y = 2.0 132 r = math.sqrt(x**2 + y**2) 133 phi = math.atan2(y, x) 134 135 value = self._func(50.0, 1.0, 0.001, 1.0)*self._func(50.0, 1.0, 0.001, 2.0) 136 self.assertAlmostEquals(self.model.run([r, phi]), value,1) 137 138 139 class TestLorentz(unittest.TestCase): 140 """ 141 Unit tests for Lorentz function 142 143 F(x) = scale/( 1 + (x*L)^2 ) + bkd 144 145 The model has three parameters: 146 L = screen Length 147 scale = scale factor 148 bkd = incoherent background 149 """ 150 def _func(self,scale,L,bkd,x): 151 return scale/( 1 + (x*L)**2 ) + bkd 152 def setUp(self): 153 from LorentzModel import LorentzModel 154 self.model= LorentzModel() 155 156 def test1D(self): 157 self.model.setParam('scale', 100.0) 158 self.model.setParam('L', 50.0) 159 self.model.setParam('bkd', 1.0) 160 161 self.assertEqual(self.model.run(0.0), 101.0) 162 self.assertEqual(self.model.run(2.0), self._func(100.0, 50.0, 1.0, 2.0)) 163 164 def test2D(self): 165 self.model.setParam('scale', 100.0) 166 self.model.setParam('L', 50.0) 167 self.model.setParam('bkd', 1.0) 168 169 value = self._func(100.0, 50.0, 1.0, 1.0)*self._func(100.0, 50.0, 1.0, 2.0) 170 self.assertEqual(self.model.runXY([1.0,2.0]), value) 171 172 def test2Dphi(self): 173 self.model.setParam('scale', 100.0) 174 self.model.setParam('L', 50.0) 175 self.model.setParam('bkd', 1.0) 176 177 x = 1.0 178 y = 2.0 179 r = math.sqrt(x**2 + y**2) 180 phi = math.atan2(y, x) 181 182 value = self._func(100.0, 50.0, 1.0, x)*self._func(100.0, 50.0, 1.0, y) 183 self.assertAlmostEquals(self.model.run([r, phi]), value,1) 184 185 class TestDAB(unittest.TestCase): 186 """ 187 Unit tests for DAB function 188 189 F(x) = scale/( 1 + (x*L)^2 )^(2) + bkd 190 191 The model has three parameters: 192 L = Correlation Length 193 scale = scale factor 194 bkd = incoherent background 195 """ 196 def _func(self, a, b,c, x): 197 return a/(( 1 + ( x * b )**2 ))**2 + c 198 199 def setUp(self): 200 from DABModel import DABModel 201 self.model= DABModel() 202 203 def test1D(self): 204 self.model.setParam('scale', 10.0) 205 self.model.setParam('L', 40.0) 206 self.model.setParam('bkd', 0.0) 207 208 self.assertEqual(self.model.run(0.0), 10.0) 209 self.assertEqual(self.model.run(2.0), self._func(10.0, 40.0, 0.0, 2.0)) 210 211 def test2D(self): 212 self.model.setParam('scale', 10.0) 213 self.model.setParam('L', 40.0) 214 self.model.setParam('bkd', 0.0) 215 216 value = self._func(10.0, 40.0, 0.0, 1.0)*self._func(10.0, 40.0, 0.0, 2.0) 217 self.assertEqual(self.model.runXY([1.0,2.0]), value) 218 219 def test2Dphi(self): 220 self.model.setParam('scale', 10.0) 221 self.model.setParam('L', 40.0) 222 self.model.setParam('bkd', 0.0) 223 224 x = 1.0 225 y = 2.0 226 r = math.sqrt(x**2 + y**2) 227 phi = math.atan2(y, x) 228 229 value = self._func(10.0, 40.0, 0.0, x)*self._func(10.0, 40.0, 0.0, y) 230 self.assertAlmostEquals(self.model.run([r, phi]), value,1) 231 232 class TestPowerLaw(unittest.TestCase): 233 """ 234 Unit tests for PowerLaw function 235 236 F(x) = scale* (x)^(m) + bkd 237 238 The model has three parameters: 239 m = power 240 scale = scale factor 241 bkd = incoherent background 242 """ 243 def _func(self, a, m,c, x): 244 return a*(x )**m + c 245 246 def setUp(self): 247 from PowerLawModel import PowerLawModel 248 self.model= PowerLawModel() 249 250 def test1D(self): 251 self.model.setParam('scale', math.exp(-6)) 252 self.model.setParam('m', 4.0) 253 self.model.setParam('bkd', 1.0) 254 255 self.assertEqual(self.model.run(0.0), 1.0) 256 self.assertEqual(self.model.run(2.0), self._func(math.exp(-6), 4.0, 1.0, 2.0)) 257 258 def test2D(self): 259 self.model.setParam('scale', math.exp(-6)) 260 self.model.setParam('m', 4.0) 261 self.model.setParam('bkd', 1.0) 262 263 value = self._func(math.exp(-6), 4.0, 1.0, 1.0)\ 264 *self._func(math.exp(-6), 4.0, 1.0, 2.0) 265 266 self.assertEqual(self.model.runXY([1.0,2.0]), value) 267 268 def test2Dphi(self): 269 self.model.setParam('scale', math.exp(-6)) 270 self.model.setParam('m', 4.0) 271 self.model.setParam('bkd', 1.0) 272 273 x = 1.0 274 y = 2.0 275 r = math.sqrt(x**2 + y**2) 276 phi = math.atan2(y, x) 277 278 value = self._func(math.exp(-6), 4.0, 1.0, x)\ 279 *self._func(math.exp(-6), 4.0, 1.0, y) 280 self.assertAlmostEquals(self.model.run([r, phi]), value,1) 281 282 class TestTeubnerStrey(unittest.TestCase): 283 """ 284 Unit tests for PowerLaw function 285 286 F(x) = 1/( scale + c1*(x)^(2)+ c2*(x)^(4)) + bkd 287 288 The model has Four parameters: 289 scale = scale factor 290 c1 = constant 291 c2 = constant 292 bkd = incoherent background 293 """ 294 def _func(self, a,c1,c2,b, x): 295 return 1/( a + c1*(x)**(2)+ c2*(x)**(4)) + b 296 297 def setUp(self): 298 from TeubnerStreyModel import TeubnerStreyModel 299 self.model= TeubnerStreyModel() 300 301 def test1D(self): 302 303 self.model.setParam('c1', -30.0) 304 self.model.setParam('c2', 5000.0) 305 self.model.setParam('scale', 0.1) 306 self.model.setParam('bkd', 0.1) 307 #self.assertEqual(1/(math.sqrt(4)), math.pow(4,-1/2)) 308 self.assertEqual(self.model.TeubnerStreyLengths(),False ) 309 310 self.assertEqual(self.model.run(0.0), 10.1) 311 self.assertEqual(self.model.run(2.0), self._func(0.1,-30.0,5000.0,0.1,2.0)) 312 313 def test2D(self): 314 self.model.setParam('c1', -30.0) 315 self.model.setParam('c2', 5000.0) 316 self.model.setParam('scale', 0.1) 317 self.model.setParam('bkd', 0.1) 318 value = self._func(0.1,-30.0,5000.0,0.1, 1.0)\ 319 *self._func(0.1,-30.0,5000.0,0.1, 2.0) 320 321 self.assertEqual(self.model.runXY([1.0,2.0]), value) 322 323 def test2Dphi(self): 324 self.model.setParam('c1', -30.0) 325 self.model.setParam('c2', 5000.0) 326 self.model.setParam('scale', 0.1) 327 self.model.setParam('bkd', 0.1) 328 329 x = 1.0 330 y = 2.0 331 r = math.sqrt(x**2 + y**2) 332 phi = math.atan2(y, x) 333 334 value = self._func(0.1,-30.0,5000.0,0.1, x)\ 335 *self._func(0.1,-30.0,5000.0,0.1, y) 336 self.assertAlmostEquals(self.model.run([r, phi]), value,1) 337 338 class TestBEPolyelectrolyte(unittest.TestCase): 339 """ 340 Unit tests for BEPolyelectrolyte function 341 342 F(x) = K*1/(4*pi()*Lb*(alpha)^(2)*(q^(2)+k2)/(1+(r02)^(2))*(q^(2)+k2)\ 343 *(q^(2)-(12*h*C/b^(2))) 344 345 The model has Eight parameters: 346 K = Constrast factor of the polymer 347 Lb = Bjerrum length 348 H = virial parameter 349 B = monomer length 350 Cs = Concentration of monovalent salt 351 alpha = ionazation degree 352 C = polymer molar concentration 353 bkd = background 354 """ 355 def _func(self, K, Lb, H, B, Cs, alpha, C, bkd, r02, k2, x): 356 return (K /( (4*math.pi *Lb*(alpha**2)*(x**2 +k2)) *( (1 +(r02**2)) \ 357 *((x**2) + k2)*((x**2) -(12 * H * C/(B**2))) )))+ bkd 358 359 def setUp(self): 360 from BEPolyelectrolyte import BEPolyelectrolyte 361 self.model= BEPolyelectrolyte() 362 363 def test1D(self): 364 365 self.model.setParam('K', 10.0) 366 self.model.setParam('Lb', 7.1) 367 self.model.setParam('H', 12) 368 self.model.setParam('B', 10) 369 self.model.setParam('Cs',0.0) 370 self.model.setParam('alpha', 0.05) 371 self.model.setParam('C', 0.7) 372 self.model.setParam('bkd', 0.001) 373 K2 = 4 * math.pi * 7.1 * (2*0.0 + 0.05*0.7) 374 Ca = 0.7 * 6.022136 * math.exp(-4) 375 r02 =1/(0.05 * math.pow(Ca,0.5)*(10/math.pow((48* math.pi *7.1),0.5))) 376 377 378 self.assertEqual(self.model.run(0.0), self._func( 10.0, 7.1, 12,\ 379 10.0, 0.0,0.05,0.7,0.001, r02, K2, 0.0)) 380 self.assertEqual(self.model.run(2.0), self._func( 10.0, 7.1, 12,\ 381 10.0, 0.0,0.05,0.7,0.001, r02, K2, 2.0)) 382 383 def test2D(self): 384 self.model.setParam('K', 10.0) 385 self.model.setParam('Lb', 7.1) 386 self.model.setParam('H', 12) 387 self.model.setParam('B', 10) 388 self.model.setParam('Cs',0.0) 389 self.model.setParam('alpha', 0.05) 390 self.model.setParam('C', 0.7) 391 self.model.setParam('bkd', 0.001) 392 K2 = 4 * math.pi * 7.1 * (2*0.0 + 0.05*0.7) 393 Ca = 0.7 * 6.022136 * math.exp(-4) 394 r02 =1/(0.05 * math.pow(Ca,0.5)*(10/math.pow((48* math.pi *7.1),0.5))) 395 396 value = self._func(10.0, 7.1, 12, 10.0, 0.0,0.05,0.7,0.001, r02, K2, 1.0)\ 397 *self._func(10.0, 7.1, 12,10.0, 0.0,0.05,0.7,0.001, r02, K2, 2.0) 398 399 self.assertAlmostEquals(self.model.runXY([1.0,2.0]), value) 400 401 def test2Dphi(self): 402 self.model.setParam('K', 10.0) 403 self.model.setParam('Lb', 7.1) 404 self.model.setParam('H', 12) 405 self.model.setParam('B', 10) 406 self.model.setParam('Cs',0.0) 407 self.model.setParam('alpha', 0.05) 408 self.model.setParam('C', 0.7) 409 self.model.setParam('bkd', 0.001) 410 K2 = 4 * math.pi * 7.1 * (2*0.0 + 0.05*0.7) 411 Ca = 0.7 * 6.022136 * math.exp(-4) 412 r02 =1/(0.05 * math.pow(Ca,0.5)*(10/math.pow((48* math.pi *7.1),0.5))) 413 414 x = 1.0 415 y = 2.0 416 r = math.sqrt(x**2 + y**2) 417 phi = math.atan2(y, x) 418 419 value = self._func(10.0, 7.1, 12, 10.0, 0.0,0.05,0.7,0.001, r02, K2, x)\ 420 *self._func(10.0, 7.1, 12, 10.0, 0.0,0.05,0.7,0.001, r02, K2, y) 421 self.assertAlmostEquals(self.model.run([r, phi]), value,1) 422 423 class TestFractalModel(unittest.TestCase): 424 """ 425 Unit tests for Number Density Fractal function 426 F(x)= P(x)*S(x) + bkd 427 The model has Seven parameters: 428 scale = Volume fraction 429 Radius = Block radius 430 Fdim = Fractal dimension 431 L = correlation Length 432 SDLB = SDL block 433 SDLS = SDL solvent 434 bkd = background 435 """ 436 def _func(self,p,s, bkd, x): 437 return p*s + bkd 438 439 def setUp(self): 440 from FractalModel import FractalModel 441 self.model= FractalModel() 442 443 def test1D(self): 444 445 self.model.setParam('scale', 0.05) 446 self.model.setParam('Radius',5) 447 self.model.setParam('Fdim',2) 448 self.model.setParam('L', 100) 449 self.model.setParam('SDLB',2*math.exp(-6)) 450 self.model.setParam('SDLS', 6.35*math.exp(-6)) 451 self.model.setParam('bkd', 0.0) 452 453 s= 1 + (math.sin((2 - 1) * math.atan(2.0*100))* 2 * gamma(2-1))\ 454 /( math.pow( (2.0*5),2)*( 1 + 1/math.pow(((2.0**2)*(100**2)),(2-1)/2))) 455 v= (4/3)*math.pi *math.pow(5, 3) 456 f= ( math.sin(2.0*5)-(2.0*5)*math.cos(2.0*5) )/(3*math.pow(2.0*5, 3)) 457 p= 0.05 *(v**2)*(((2*math.exp(-6))-(6.35*math.exp(-6)))**2)*(f**2) 458 459 self.assertEqual(self.model._scatterRanDom(2.0),p ) 460 self.assertEqual(self.model._Block(2.0),s ) 461 self.assertEqual(self.model.run(2.0),self._func(p,s ,0.0,2.0)) 462 463 class TestUnifiedPowerLaw(unittest.TestCase): 464 """ 465 Unit tests for Unified PowerLaw function 466 467 F(x) = bkd + sum(G[i]*exp(-x**2 *Rg[i]**2 /3)) \ 468 + [B[i]( erf(x*Rg[i]/math.sqrt(6)))** (3*p[i])]/x**p[i] ) 469 470 """ 471 472 def setUp(self): 473 from UnifiedPowerLaw import UnifiedPowerLawModel 474 self.model= UnifiedPowerLawModel() 475 476 def _func(self,level,B,Rg,G,Pow,bkd, x): 477 return bkd + (B * math.pow(erf(x *Rg/math.sqrt(6)),3 *Pow))/math.pow(x,Pow) 478 479 def _Sum (self,level,x): 480 self.sum = 0 481 for i in xrange(level): 482 self.sum =self.sum +self.model.getParam('G'+str(i+1))*math.exp(-(x**2)*\ 483 (self.model.getParam('Rg'+str(i+1))**2)/3) 484 return self.sum 485 486 def test1D(self): 487 488 489 self.model.setParam('bkd', 0.0) 490 #setValueParam(self,level,Bvalue,Rgvalue,Gvalue,Pvalue) 491 self.model.setValueParam(1,2,1,2,5) 492 self.model.setValueParam(2,3,12,8,9) 493 self.model.setValueParam(3,0,2,3,2) 494 self.model.setValueParam(4,1,4,1,-1) 495 self.model.setValueParam(5,1,4,1,-2) 496 497 self.assertEqual(self.model.getValue('P',1),5) 498 self.assertEqual(self.model.getValue('Rg',2),12) 499 500 value1 = self._func(1,2,1,2,5,0.0,1.0)+self._Sum(5,1.0) 501 value2 = self._func(2,3,12,8,9,0.0,1.0)+self._Sum(5,1.0) 502 value3 = self._func(3,0,2,3,2,0.0,1.0)+self._Sum(5,1.0) 503 value4 = self._func(4,1,4,1,-1,0.0,1.0)+self._Sum(5,1.0) 504 value5 = self._func(5,1,4,1,-2,0.0,1.0)+self._Sum(5,1.0) 505 self.assertEqual(self.model.run(1,1.0), value1) 506 self.assertEqual(self.model.run(2,1.0), value2) 507 self.assertEqual(self.model.run(3,1.0), value3) 508 self.assertEqual(self.model.run(4,1.0), value4) 509 self.assertEqual(self.model.run(5,1.0), value5) 510 511 512 93 513 94 514 if __name__ == '__main__':
Note: See TracChangeset
for help on using the changeset viewer.