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

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since e481a39 was aa2edb2, checked in by gonzalezm, 8 years ago

Removing hardcoded figures to be replaced by autogenerated ones

  • 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
27References
28----------
29
30None.
31
32"""
33
34from math import sqrt
35from numpy import inf, power
36
37name = "two_lorentzian"
38title = "Two Lorentzian type peak"
39description = """I(q) = scale_1/(1.0 + pow((q*length_1),exponent_1))
40             + scale_2/(1.0 + pow((q*length_2),exponent_2) )+ background
41
42             scale_1    = Lorentzian term scaling #1
43             length_1   = Lorentzian screening length #1 [A]
44             exponent_1 = Lorentzian exponent #1
45             scale_2    = Lorentzian term scaling #2
46             length_2   = Lorentzian screening length #2 [A]
47             exponent_2 = Lorentzian exponent #2
48             background = Incoherent background
49        """
50category = "shape-independent"
51
52# pylint: disable=bad-whitespace, line-too-long
53#            ["name", "units", default, [lower, upper], "type", "description"],
54parameters = [["lorentz_scale_1",  "",     10.0, [-inf, inf], "", "First power law scale factor"],
55              ["lorentz_length_1", "Ang", 100.0, [-inf, inf], "", "First Lorentzian screening length"],
56              ["lorentz_exp_1",    "",      3.0, [-inf, inf], "", "First exponent of power law"],
57              ["lorentz_scale_2",  "",      1.0, [-inf, inf], "", "Second scale factor for broad Lorentzian peak"],
58              ["lorentz_length_2", "Ang",  10.0, [-inf, inf], "", "Second Lorentzian screening length"],
59              ["lorentz_exp_2",    "",      2.0, [-inf, inf], "", "Second exponent of power law"],
60             ]
61# pylint: enable=bad-whitespace, line-too-long
62
63
64def Iq(q,
65       lorentz_scale_1=10.0,
66       lorentz_length_1=100.0,
67       lorentz_exp_1=3.0,
68       lorentz_scale_2=1.0,
69       lorentz_length_2=10.0,
70       lorentz_exp_2=2.0):
71
72    """
73    :param q:                   Input q-value (float or [float, float])
74    :param lorentz_scale_1:     Second scale factor for broad Lorentzian peak
75    :param lorentz_length_1:    First Lorentzian screening length
76    :param lorentz_exp_1:       Exponent of the second Lorentz function
77    :param lorentz_scale_2:     Second scale factor for broad Lorentzian peak
78    :param lorentz_length_2:    Second Lorentzian screening length
79    :param lorentz_exp_2:       Exponent of the second Lorentz function
80    :return:                    Calculated intensity
81    """
82# pylint: disable=bad-whitespace
83    intensity  = lorentz_scale_1/(1.0 +
84                                  power(q*lorentz_length_1, lorentz_exp_1))
85    intensity += lorentz_scale_2/(1.0 +
86                                  power(q*lorentz_length_2, lorentz_exp_2))
87# pylint: enable=bad-whitespace
88
89    return intensity
90
91Iq.vectorized = True  # Iq accepts an array of q values
92
93
94def Iqxy(qx, qy, *args):
95    """
96    :param qx:   Input q_x-value
97    :param qy:   Input q_y-value
98    :param args: Remaining arguments
99    :return:     2D-Intensity
100    """
101
102    return Iq(sqrt(qx**2 + qy**2), *args)
103
104Iqxy.vectorized = True  # Iqxy accepts an array of qx, qy values
105
106
107demo = dict(scale=1, background=0.1,
108            lorentz_scale_1=10,
109            lorentz_length_1=100.0,
110            lorentz_exp_1=3.0,
111            lorentz_scale_2=1,
112            lorentz_length_2=10,
113            lorentz_exp_2=2.0)
114
115oldname = "TwoLorentzianModel"
116oldpars = dict(background='background',
117               lorentz_scale_1='scale_1',
118               lorentz_scale_2='scale_2',
119               lorentz_length_1='length_1',
120               lorentz_length_2='length_2',
121               lorentz_exp_1='exponent_1',
122               lorentz_exp_2='exponent_2')
123
124tests = [
125
126    # Accuracy tests based on content in test/utest_extra_models.py
127    [{'lorentz_scale_1':   10.0,
128      'lorentz_length_1': 100.0,
129      'lorentz_exp_1':      3.0,
130      'lorentz_scale_2':    1.0,
131      'lorentz_length_2':  10.0,
132      'lorentz_exp_2':      2.0,
133      'background':         0.1,
134     }, 0.001, 11.08991],
135
136    [{'lorentz_scale_1':   10.0,
137      'lorentz_length_1': 100.0,
138      'lorentz_exp_1':      3.0,
139      'lorentz_scale_2':    1.0,
140      'lorentz_length_2':  10.0,
141      'lorentz_exp_2':      2.0,
142      'background':         0.1,
143     }, 0.150141, 0.410245],
144
145    [{'lorentz_scale_1':   10.0,
146      'lorentz_length_1': 100.0,
147      'lorentz_exp_1':      3.0,
148      'lorentz_scale_2':    1.0,
149      'lorentz_length_2':  10.0,
150      'lorentz_exp_2':      2.0,
151      'background':         0.1,
152     }, 0.442528, 0.148699],
153
154    # Additional tests with larger range of parameters
155    [{'lorentz_scale_1':   10.0,
156      'lorentz_length_1': 100.0,
157      'lorentz_exp_1':      3.0,
158      'lorentz_scale_2':    1.0,
159      'lorentz_length_2':  10.0,
160      'lorentz_exp_2':      2.0,
161     }, 0.000332070182643, 10.9996228107],
162
163    [{'lorentz_scale_1':  0.0,
164      'lorentz_length_1': 0.0,
165      'lorentz_exp_1':    0.0,
166      'lorentz_scale_2':  0.0,
167      'lorentz_length_2': 0.0,
168      'lorentz_exp_2':    0.0,
169      'background':     100.0
170     }, 5.0, 100.0],
171
172    [{'lorentz_scale_1': 200.0,
173      'lorentz_length_1': 10.0,
174      'lorentz_exp_1':     0.1,
175      'lorentz_scale_2':   0.1,
176      'lorentz_length_2':  5.0,
177      'lorentz_exp_2':     2.0
178     }, 20000., 45.5659201896],
179    ]
Note: See TracBrowser for help on using the repository browser.