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) |
---|
19 | self.model.setParam('contrast', 3.e-6) |
---|
20 | self.model.setParam('background', 0.0) |
---|
21 | self.model.setParam('cyl_theta', 0.0) |
---|
22 | self.model.setParam('cyl_phi', 0.0) |
---|
23 | |
---|
24 | def test_simple(self): |
---|
25 | self.assertAlmostEqual(self.model.run(0.001), 450.355, 3) |
---|
26 | self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 452.299, 3) |
---|
27 | |
---|
28 | def test_constant(self): |
---|
29 | from sans.models.dispersion_models import DispersionModel |
---|
30 | disp = DispersionModel() |
---|
31 | self.model.setParam('scale', 10.0) |
---|
32 | self.model.set_dispersion('radius', disp) |
---|
33 | self.model.dispersion['radius']['width'] = 5.0 |
---|
34 | self.model.dispersion['radius']['npts'] = 100 |
---|
35 | |
---|
36 | self.assertAlmostEqual(self.model.run(0.001), 4527.47250339, 3) |
---|
37 | self.assertAlmostEqual(self.model.runXY([0.001, 0.001]), 4546.997777604715, 3) |
---|
38 | |
---|
39 | def test_gaussian(self): |
---|
40 | from sans.models.dispersion_models import GaussianDispersion |
---|
41 | disp = GaussianDispersion() |
---|
42 | self.model.set_dispersion('radius', disp) |
---|
43 | self.model.dispersion['radius']['width'] = 5.0 |
---|
44 | self.model.dispersion['radius']['npts'] = 100 |
---|
45 | self.model.setParam('scale', 10.0) |
---|
46 | |
---|
47 | self.assertAlmostEqual(self.model.run(0.001), 4723.32213339, 3) |
---|
48 | self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 4743.56, 2) |
---|
49 | |
---|
50 | def test_array(self): |
---|
51 | """ |
---|
52 | Perform complete rotational average and |
---|
53 | compare to 1D |
---|
54 | """ |
---|
55 | from sans.models.dispersion_models import ArrayDispersion |
---|
56 | disp_ph = ArrayDispersion() |
---|
57 | disp_th = ArrayDispersion() |
---|
58 | |
---|
59 | values_ph = numpy.zeros(100) |
---|
60 | values_th = numpy.zeros(100) |
---|
61 | weights = numpy.zeros(100) |
---|
62 | for i in range(100): |
---|
63 | values_ph[i]=(2.0*math.pi/99.0*i) |
---|
64 | values_th[i]=(math.pi/99.0*i) |
---|
65 | weights[i]=(1.0) |
---|
66 | |
---|
67 | disp_ph.set_weights(values_ph, weights) |
---|
68 | disp_th.set_weights(values_th, weights) |
---|
69 | |
---|
70 | self.model.set_dispersion('cyl_theta', disp_th) |
---|
71 | self.model.set_dispersion('cyl_phi', disp_ph) |
---|
72 | |
---|
73 | val_1d = self.model.run(math.sqrt(0.0002)) |
---|
74 | val_2d = self.model.runXY([0.01,0.01]) |
---|
75 | |
---|
76 | self.assertTrue(math.fabs(val_1d-val_2d)/val_1d < 0.02) |
---|
77 | |
---|
78 | class TestCoreShellCylinder(unittest.TestCase): |
---|
79 | """ |
---|
80 | Testing C++ Cylinder model |
---|
81 | """ |
---|
82 | def setUp(self): |
---|
83 | from sans.models.CoreShellCylinderModel import CoreShellCylinderModel |
---|
84 | self.model= CoreShellCylinderModel() |
---|
85 | |
---|
86 | self.model.setParam('scale', 1.0) |
---|
87 | self.model.setParam('radius', 20.0) |
---|
88 | self.model.setParam('thickness', 10.0) |
---|
89 | self.model.setParam('length', 400.0) |
---|
90 | self.model.setParam('core_sld', 1.e-6) |
---|
91 | self.model.setParam('shell_sld', 4.e-6) |
---|
92 | self.model.setParam('solvent_sld', 1.e-6) |
---|
93 | self.model.setParam('background', 0.0) |
---|
94 | self.model.setParam('axis_theta', 0.0) |
---|
95 | self.model.setParam('axis_phi', 0.0) |
---|
96 | |
---|
97 | def test_simple(self): |
---|
98 | """ |
---|
99 | Test simple 1D and 2D values |
---|
100 | Numbers taken from model that passed validation, before |
---|
101 | the update to C++ underlying class. |
---|
102 | """ |
---|
103 | self.assertAlmostEqual(self.model.run(0.001), 353.55013216754583, 3) |
---|
104 | self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 355.25355270620543, 3) |
---|
105 | |
---|
106 | def test_dispersion(self): |
---|
107 | """ |
---|
108 | Test with dispersion |
---|
109 | """ |
---|
110 | from sans.models.DisperseModel import DisperseModel |
---|
111 | disp = DisperseModel(self.model, ['radius', 'thickness', 'length'], [5, 2, 50]) |
---|
112 | disp.setParam('n_pts', 10) |
---|
113 | self.assertAlmostEqual(disp.run(0.001), 358.44062724936009, 3) |
---|
114 | self.assertAlmostEqual(disp.runXY([0.001,0.001]), 360.22673635224584, 3) |
---|
115 | |
---|
116 | def test_new_disp(self): |
---|
117 | from sans.models.dispersion_models import GaussianDispersion |
---|
118 | disp_rm = GaussianDispersion() |
---|
119 | self.model.set_dispersion('radius', disp_rm) |
---|
120 | self.model.dispersion['radius']['width'] = 5.0 |
---|
121 | self.model.dispersion['radius']['npts'] = 10 |
---|
122 | |
---|
123 | disp_rr = GaussianDispersion() |
---|
124 | self.model.set_dispersion('thickness', disp_rr) |
---|
125 | self.model.dispersion['thickness']['width'] = 2. |
---|
126 | self.model.dispersion['thickness']['npts'] = 10 |
---|
127 | |
---|
128 | disp_len = GaussianDispersion() |
---|
129 | self.model.set_dispersion('length', disp_len) |
---|
130 | self.model.dispersion['length']['width'] = 50.0 |
---|
131 | self.model.dispersion['length']['npts'] = 10 |
---|
132 | |
---|
133 | self.assertAlmostEqual(self.model.run(0.001), 358.44062724936009, 3) |
---|
134 | self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 360.22673635224584, 3) |
---|
135 | |
---|
136 | |
---|
137 | def test_array(self): |
---|
138 | """ |
---|
139 | Perform complete rotational average and |
---|
140 | compare to 1D |
---|
141 | """ |
---|
142 | from sans.models.dispersion_models import ArrayDispersion |
---|
143 | disp_ph = ArrayDispersion() |
---|
144 | disp_th = ArrayDispersion() |
---|
145 | |
---|
146 | values_ph = numpy.zeros(100) |
---|
147 | values_th = numpy.zeros(100) |
---|
148 | weights = numpy.zeros(100) |
---|
149 | for i in range(100): |
---|
150 | values_ph[i]=(2.0*math.pi/99.0*i) |
---|
151 | values_th[i]=(math.pi/99.0*i) |
---|
152 | weights[i]=(1.0) |
---|
153 | |
---|
154 | disp_ph.set_weights(values_ph, weights) |
---|
155 | disp_th.set_weights(values_th, weights) |
---|
156 | |
---|
157 | self.model.set_dispersion('axis_theta', disp_th) |
---|
158 | self.model.set_dispersion('axis_phi', disp_ph) |
---|
159 | |
---|
160 | val_1d = self.model.run(math.sqrt(0.0002)) |
---|
161 | val_2d = self.model.runXY([0.01,0.01]) |
---|
162 | |
---|
163 | self.assertTrue(math.fabs(val_1d-val_2d)/val_1d < 0.02) |
---|
164 | |
---|
165 | |
---|
166 | |
---|
167 | class TestCoreShell(unittest.TestCase): |
---|
168 | """ |
---|
169 | Testing C++ Cylinder model |
---|
170 | """ |
---|
171 | def setUp(self): |
---|
172 | from sans.models.CoreShellModel import CoreShellModel |
---|
173 | self.model= CoreShellModel() |
---|
174 | |
---|
175 | self.model.setParam('scale', 1.0) |
---|
176 | self.model.setParam('radius', 60.0) |
---|
177 | self.model.setParam('thickness', 10.0) |
---|
178 | self.model.setParam('core_sld', 1.e-6) |
---|
179 | self.model.setParam('shell_sld', 2.e-6) |
---|
180 | self.model.setParam('solvent_sld', 3.e-6) |
---|
181 | self.model.setParam('background', 0.0) |
---|
182 | |
---|
183 | def test_simple(self): |
---|
184 | """ |
---|
185 | Test simple 1D and 2D values |
---|
186 | Numbers taken from model that passed validation, before |
---|
187 | the update to C++ underlying class. |
---|
188 | """ |
---|
189 | self.assertAlmostEqual(self.model.run(0.001), 381.27304697150055, 3) |
---|
190 | self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 380.93779156218682, 3) |
---|
191 | |
---|
192 | def test_dispersion(self): |
---|
193 | """ |
---|
194 | Test with dispersion |
---|
195 | """ |
---|
196 | from sans.models.DisperseModel import DisperseModel |
---|
197 | disp = DisperseModel(self.model, ['radius', 'thickness'], [10, 2]) |
---|
198 | disp.setParam('n_pts', 10) |
---|
199 | self.assertAlmostEqual(disp.run(0.001), 407.344127907553, 3) |
---|
200 | |
---|
201 | def test_new_disp(self): |
---|
202 | from sans.models.dispersion_models import GaussianDispersion |
---|
203 | disp_rm = GaussianDispersion() |
---|
204 | self.model.set_dispersion('radius', disp_rm) |
---|
205 | self.model.dispersion['radius']['width'] = 10. |
---|
206 | self.model.dispersion['radius']['npts'] = 10 |
---|
207 | |
---|
208 | disp_rr = GaussianDispersion() |
---|
209 | self.model.set_dispersion('thickness', disp_rr) |
---|
210 | self.model.dispersion['thickness']['width'] = 2. |
---|
211 | self.model.dispersion['thickness']['npts'] = 10 |
---|
212 | |
---|
213 | |
---|
214 | self.assertAlmostEqual(self.model.run(0.001), 407.344127907553, 3) |
---|
215 | |
---|
216 | |
---|
217 | class TestEllipsoid(unittest.TestCase): |
---|
218 | """ |
---|
219 | Testing C++ Cylinder model |
---|
220 | """ |
---|
221 | def setUp(self): |
---|
222 | from sans.models.EllipsoidModel import EllipsoidModel |
---|
223 | self.model= EllipsoidModel() |
---|
224 | |
---|
225 | self.model.setParam('scale', 1.0) |
---|
226 | self.model.setParam('radius_a', 20.0) |
---|
227 | self.model.setParam('radius_b', 400.0) |
---|
228 | self.model.setParam('contrast', 3.e-6) |
---|
229 | self.model.setParam('background', 0.0) |
---|
230 | self.model.setParam('axis_theta', 1.57) |
---|
231 | self.model.setParam('axis_phi', 0.0) |
---|
232 | |
---|
233 | def test_simple(self): |
---|
234 | """ |
---|
235 | Test simple 1D and 2D values |
---|
236 | Numbers taken from model that passed validation, before |
---|
237 | the update to C++ underlying class. |
---|
238 | """ |
---|
239 | self.assertAlmostEqual(self.model.run(0.001), 11808.842896863147, 3) |
---|
240 | self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 11681.990374929677, 3) |
---|
241 | |
---|
242 | def test_dispersion(self): |
---|
243 | """ |
---|
244 | Test with dispersion |
---|
245 | """ |
---|
246 | from sans.models.DisperseModel import DisperseModel |
---|
247 | disp = DisperseModel(self.model, ['radius_a', 'radius_b'], [5, 50]) |
---|
248 | disp.setParam('n_pts', 10) |
---|
249 | self.assertAlmostEqual(disp.run(0.001), 11948.72581312305, 3) |
---|
250 | self.assertAlmostEqual(disp.runXY([0.001,0.001]), 11811.972359807551, 3) |
---|
251 | |
---|
252 | def test_new_disp(self): |
---|
253 | from sans.models.dispersion_models import GaussianDispersion |
---|
254 | disp_rm = GaussianDispersion() |
---|
255 | self.model.set_dispersion('radius_a', disp_rm) |
---|
256 | self.model.dispersion['radius_a']['width'] = 5.0 |
---|
257 | self.model.dispersion['radius_a']['npts'] = 10 |
---|
258 | |
---|
259 | disp_rr = GaussianDispersion() |
---|
260 | self.model.set_dispersion('radius_b', disp_rr) |
---|
261 | self.model.dispersion['radius_b']['width'] = 50. |
---|
262 | self.model.dispersion['radius_b']['npts'] = 10 |
---|
263 | |
---|
264 | |
---|
265 | self.assertAlmostEqual(self.model.run(0.001), 11948.72581312305, 3) |
---|
266 | self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 11811.972359807551, 3) |
---|
267 | |
---|
268 | def test_array(self): |
---|
269 | """ |
---|
270 | Perform complete rotational average and |
---|
271 | compare to 1D |
---|
272 | """ |
---|
273 | from sans.models.dispersion_models import ArrayDispersion |
---|
274 | disp_ph = ArrayDispersion() |
---|
275 | disp_th = ArrayDispersion() |
---|
276 | |
---|
277 | values_ph = numpy.zeros(100) |
---|
278 | values_th = numpy.zeros(100) |
---|
279 | weights = numpy.zeros(100) |
---|
280 | for i in range(100): |
---|
281 | values_ph[i]=(2.0*math.pi/99.0*i) |
---|
282 | values_th[i]=(math.pi/99.0*i) |
---|
283 | weights[i]=(1.0) |
---|
284 | |
---|
285 | disp_ph.set_weights(values_ph, weights) |
---|
286 | disp_th.set_weights(values_th, weights) |
---|
287 | |
---|
288 | self.model.set_dispersion('axis_theta', disp_th) |
---|
289 | self.model.set_dispersion('axis_phi', disp_ph) |
---|
290 | |
---|
291 | val_1d = self.model.run(math.sqrt(0.0002)) |
---|
292 | val_2d = self.model.runXY([0.01,0.01]) |
---|
293 | |
---|
294 | self.assertTrue(math.fabs(val_1d-val_2d)/val_1d < 0.02) |
---|
295 | |
---|
296 | |
---|
297 | |
---|
298 | class TestSphere(unittest.TestCase): |
---|
299 | """ |
---|
300 | Testing C++ Cylinder model |
---|
301 | """ |
---|
302 | def setUp(self): |
---|
303 | from sans.models.SphereModel import SphereModel |
---|
304 | self.model= SphereModel() |
---|
305 | |
---|
306 | self.model.setParam('scale', 1.0) |
---|
307 | self.model.setParam('radius', 60.0) |
---|
308 | self.model.setParam('contrast', 1) |
---|
309 | self.model.setParam('background', 0.0) |
---|
310 | |
---|
311 | def test_simple(self): |
---|
312 | """ |
---|
313 | Test simple 1D and 2D values |
---|
314 | Numbers taken from model that passed validation, before |
---|
315 | the update to C++ underlying class. |
---|
316 | """ |
---|
317 | self.assertAlmostEqual(self.model.run(0.001), 90412744456148.094, 3) |
---|
318 | self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 90347660670656.391, 3) |
---|
319 | |
---|
320 | def test_dispersion(self): |
---|
321 | """ |
---|
322 | Test with dispersion |
---|
323 | """ |
---|
324 | from sans.models.DisperseModel import DisperseModel |
---|
325 | disp = DisperseModel(self.model, ['radius'], [10]) |
---|
326 | disp.setParam('n_pts', 10) |
---|
327 | self.assertAlmostEqual(disp.run(0.001), 96795008379475.25, 3) |
---|
328 | |
---|
329 | def test_new_disp(self): |
---|
330 | from sans.models.dispersion_models import GaussianDispersion |
---|
331 | disp_rm = GaussianDispersion() |
---|
332 | self.model.set_dispersion('radius', disp_rm) |
---|
333 | self.model.dispersion['radius']['width'] = 10.0 |
---|
334 | self.model.dispersion['radius']['npts'] = 10 |
---|
335 | |
---|
336 | self.assertAlmostEqual(self.model.run(0.001), 96795008379475.25, 3) |
---|
337 | |
---|
338 | |
---|
339 | |
---|
340 | class TestEllipticalCylinder(unittest.TestCase): |
---|
341 | """ |
---|
342 | Testing C++ Cylinder model |
---|
343 | """ |
---|
344 | def setUp(self): |
---|
345 | from sans.models.EllipticalCylinderModel import EllipticalCylinderModel |
---|
346 | self.model= EllipticalCylinderModel() |
---|
347 | |
---|
348 | self.model.setParam('scale', 1.0) |
---|
349 | self.model.setParam('r_minor', 20.0) |
---|
350 | self.model.setParam('r_ratio', 1.5) |
---|
351 | self.model.setParam('length', 400.0) |
---|
352 | self.model.setParam('contrast', 3.e-6) |
---|
353 | self.model.setParam('background', 0.0) |
---|
354 | self.model.setParam('cyl_theta', 1.57) |
---|
355 | self.model.setParam('cyl_phi', 0.0) |
---|
356 | self.model.setParam('cyl_psi', 0.0) |
---|
357 | |
---|
358 | def test_simple(self): |
---|
359 | """ |
---|
360 | Test simple 1D and 2D values |
---|
361 | Numbers taken from model that passed validation, before |
---|
362 | the update to C++ underlying class. |
---|
363 | """ |
---|
364 | self.assertAlmostEqual(self.model.run(0.001), 675.50440232504991, 3) |
---|
365 | self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 669.5173937622792, 3) |
---|
366 | |
---|
367 | def test_dispersion(self): |
---|
368 | """ |
---|
369 | Test with dispersion |
---|
370 | """ |
---|
371 | from sans.models.DisperseModel import DisperseModel |
---|
372 | disp = DisperseModel(self.model, ['r_minor', 'r_ratio', 'length'], [5, 0.25, 50]) |
---|
373 | disp.setParam('n_pts', 10) |
---|
374 | self.assertAlmostEqual(disp.run(0.001), 711.18048194151925, 3) |
---|
375 | self.assertAlmostEqual(disp.runXY([0.001,0.001]), 704.63525988095705, 3) |
---|
376 | |
---|
377 | def test_new_disp(self): |
---|
378 | from sans.models.dispersion_models import GaussianDispersion |
---|
379 | disp_rm = GaussianDispersion() |
---|
380 | self.model.set_dispersion('r_minor', disp_rm) |
---|
381 | self.model.dispersion['r_minor']['width'] = 5.0 |
---|
382 | self.model.dispersion['r_minor']['npts'] = 10 |
---|
383 | |
---|
384 | disp_rr = GaussianDispersion() |
---|
385 | self.model.set_dispersion('r_ratio', disp_rr) |
---|
386 | self.model.dispersion['r_ratio']['width'] = 0.25 |
---|
387 | self.model.dispersion['r_ratio']['npts'] = 10 |
---|
388 | |
---|
389 | disp_len = GaussianDispersion() |
---|
390 | self.model.set_dispersion('length', disp_len) |
---|
391 | self.model.dispersion['length']['width'] = 50.0 |
---|
392 | self.model.dispersion['length']['npts'] = 10 |
---|
393 | |
---|
394 | self.assertAlmostEqual(self.model.run(0.001), 711.18048194151925, 3) |
---|
395 | self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 704.63525988095705, 3) |
---|
396 | |
---|
397 | |
---|
398 | def test_array(self): |
---|
399 | """ |
---|
400 | Perform complete rotational average and |
---|
401 | compare to 1D |
---|
402 | """ |
---|
403 | from sans.models.dispersion_models import ArrayDispersion |
---|
404 | disp_ph = ArrayDispersion() |
---|
405 | disp_th = ArrayDispersion() |
---|
406 | disp_ps = ArrayDispersion() |
---|
407 | |
---|
408 | values_ph = numpy.zeros(100) |
---|
409 | values_th = numpy.zeros(100) |
---|
410 | values_ps = numpy.zeros(100) |
---|
411 | weights = numpy.zeros(100) |
---|
412 | for i in range(100): |
---|
413 | values_ps[i]=(2.0*math.pi/99.0*i) |
---|
414 | values_ph[i]=(2.0*math.pi/99.0*i) |
---|
415 | values_th[i]=(math.pi/99.0*i) |
---|
416 | weights[i]=(1.0) |
---|
417 | |
---|
418 | disp_ph.set_weights(values_ph, weights) |
---|
419 | disp_th.set_weights(values_th, weights) |
---|
420 | disp_ps.set_weights(values_ps, weights) |
---|
421 | |
---|
422 | self.model.set_dispersion('cyl_theta', disp_th) |
---|
423 | self.model.set_dispersion('cyl_phi', disp_ph) |
---|
424 | self.model.set_dispersion('cyl_psi', disp_ps) |
---|
425 | |
---|
426 | val_1d = self.model.run(math.sqrt(0.0002)) |
---|
427 | val_2d = self.model.runXY([0.01,0.01]) |
---|
428 | |
---|
429 | self.assertTrue(math.fabs(val_1d-val_2d)/val_1d < 0.02) |
---|
430 | |
---|
431 | |
---|
432 | |
---|
433 | if __name__ == '__main__': |
---|
434 | unittest.main() |
---|
435 | |
---|