source: sasmodels/sasmodels/models/two_lorentzian.py @ d507c3a

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since d507c3a was 168052c, checked in by piotr, 8 years ago

pylint prettification

  • Property mode set to 100644
File size: 5.7 KB
Line 
1r"""
2This model calculates an empirical functional form for SAS data characterized
3by two Lorentzian-type functions.
4
5Definition
6----------
7
8The scattering intensity $I(q)$ is calculated as
9
10.. math::
11
12    I(q) = \frac{A}{1 +(Q\xi_1)^n} + \frac{C}{1 +(Q\xi_2)^m} + \text{B}
13
14where $A$ = Lorentzian scale factor #1, $C$ = Lorentzian scale #2,
15$\xi_1$ and $\xi_2$ are the corresponding correlation lengths, and $n$ and
16$m$ are the respective power law exponents (set $n = m = 2$ for
17Ornstein-Zernicke behaviour).
18
19For 2D data the scattering intensity is calculated in the same way as 1D,
20where the $q$ vector is defined as
21
22.. math::
23
24    q = \sqrt{q_x^2 + q_y^2}
25
26
27.. figure:: img/two_lorentzian.jpg
28
29    1D plot using the default values (w/500 data point).
30
31References
32----------
33
34None.
35
36"""
37
38from math import sqrt
39from numpy import inf, power
40
41name = "two_lorentzian"
42title = "Two Lorentzian type peak"
43description = """I(q) = scale_1/(1.0 + pow((q*length_1),exponent_1))
44             + scale_2/(1.0 + pow((q*length_2),exponent_2) )+ background
45
46             scale_1    = Lorentzian term scaling #1
47             length_1   = Lorentzian screening length #1 [A]
48             exponent_1 = Lorentzian exponent #1
49             scale_2    = Lorentzian term scaling #2
50             length_2   = Lorentzian screening length #2 [A]
51             exponent_2 = Lorentzian exponent #2
52             background = Incoherent background
53        """
54category = "shape-independent"
55
56# pylint: disable=bad-whitespace, line-too-long
57#            ["name", "units", default, [lower, upper], "type", "description"],
58parameters = [["lorentz_scale_1",  "",     10.0, [-inf, inf], "", "First power law scale factor"],
59              ["lorentz_length_1", "Ang", 100.0, [-inf, inf], "", "First Lorentzian screening length"],
60              ["lorentz_exp_1",    "",      3.0, [-inf, inf], "", "First exponent of power law"],
61              ["lorentz_scale_2",  "",      1.0, [-inf, inf], "", "Second scale factor for broad Lorentzian peak"],
62              ["lorentz_length_2", "Ang",  10.0, [-inf, inf], "", "Second Lorentzian screening length"],
63              ["lorentz_exp_2",    "",      2.0, [-inf, inf], "", "Second exponent of power law"],
64             ]
65# pylint: enable=bad-whitespace, line-too-long
66
67
68def Iq(q,
69       lorentz_scale_1=10.0,
70       lorentz_length_1=100.0,
71       lorentz_exp_1=3.0,
72       lorentz_scale_2=1.0,
73       lorentz_length_2=10.0,
74       lorentz_exp_2=2.0):
75
76    """
77    :param q:                   Input q-value (float or [float, float])
78    :param lorentz_scale_1:     Second scale factor for broad Lorentzian peak
79    :param lorentz_length_1:    First Lorentzian screening length
80    :param lorentz_exp_1:       Exponent of the second Lorentz function
81    :param lorentz_scale_2:     Second scale factor for broad Lorentzian peak
82    :param lorentz_length_2:    Second Lorentzian screening length
83    :param lorentz_exp_2:       Exponent of the second Lorentz function
84    :return:                    Calculated intensity
85    """
86# pylint: disable=bad-whitespace
87    intensity  = lorentz_scale_1/(1.0 +
88                                  power(q*lorentz_length_1, lorentz_exp_1))
89    intensity += lorentz_scale_2/(1.0 +
90                                  power(q*lorentz_length_2, lorentz_exp_2))
91# pylint: enable=bad-whitespace
92
93    return intensity
94
95Iq.vectorized = True  # Iq accepts an array of q values
96
97
98def Iqxy(qx, qy, *args):
99    """
100    :param qx:   Input q_x-value
101    :param qy:   Input q_y-value
102    :param args: Remaining arguments
103    :return:     2D-Intensity
104    """
105
106    return Iq(sqrt(qx**2 + qy**2), *args)
107
108Iqxy.vectorized = True  # Iqxy accepts an array of qx, qy values
109
110
111demo = dict(scale=1, background=0.1,
112            lorentz_scale_1=10,
113            lorentz_length_1=100.0,
114            lorentz_exp_1=3.0,
115            lorentz_scale_2=1,
116            lorentz_length_2=10,
117            lorentz_exp_2=2.0)
118
119oldname = "TwoLorentzianModel"
120oldpars = dict(background='background',
121               lorentz_scale_1='scale_1',
122               lorentz_scale_2='scale_2',
123               lorentz_length_1='length_1',
124               lorentz_length_2='length_2',
125               lorentz_exp_1='exponent_1',
126               lorentz_exp_2='exponent_2')
127
128tests = [
129
130    # Accuracy tests based on content in test/utest_extra_models.py
131    [{'lorentz_scale_1':   10.0,
132      'lorentz_length_1': 100.0,
133      'lorentz_exp_1':      3.0,
134      'lorentz_scale_2':    1.0,
135      'lorentz_length_2':  10.0,
136      'lorentz_exp_2':      2.0,
137      'background':         0.1,
138     }, 0.001, 11.08991],
139
140    [{'lorentz_scale_1':   10.0,
141      'lorentz_length_1': 100.0,
142      'lorentz_exp_1':      3.0,
143      'lorentz_scale_2':    1.0,
144      'lorentz_length_2':  10.0,
145      'lorentz_exp_2':      2.0,
146      'background':         0.1,
147     }, 0.150141, 0.410245],
148
149    [{'lorentz_scale_1':   10.0,
150      'lorentz_length_1': 100.0,
151      'lorentz_exp_1':      3.0,
152      'lorentz_scale_2':    1.0,
153      'lorentz_length_2':  10.0,
154      'lorentz_exp_2':      2.0,
155      'background':         0.1,
156     }, 0.442528, 0.148699],
157
158    # Additional tests with larger range of parameters
159    [{'lorentz_scale_1':   10.0,
160      'lorentz_length_1': 100.0,
161      'lorentz_exp_1':      3.0,
162      'lorentz_scale_2':    1.0,
163      'lorentz_length_2':  10.0,
164      'lorentz_exp_2':      2.0,
165     }, 0.000332070182643, 10.9996228107],
166
167    [{'lorentz_scale_1':  0.0,
168      'lorentz_length_1': 0.0,
169      'lorentz_exp_1':    0.0,
170      'lorentz_scale_2':  0.0,
171      'lorentz_length_2': 0.0,
172      'lorentz_exp_2':    0.0,
173      'background':     100.0
174     }, 5.0, 100.0],
175
176    [{'lorentz_scale_1': 200.0,
177      'lorentz_length_1': 10.0,
178      'lorentz_exp_1':     0.1,
179      'lorentz_scale_2':   0.1,
180      'lorentz_length_2':  5.0,
181      'lorentz_exp_2':     2.0
182     }, 20000., 45.5659201896],
183    ]
Note: See TracBrowser for help on using the repository browser.