source: sasmodels/sasmodels/models/two_power_law.py @ 7e1d090

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

Final touches on Two Power Law model

  • Property mode set to 100644
File size: 4.6 KB
Line 
1r"""
2This model calculates an empirical functional form for SAS data characterized
3by two power laws.
4
5Definition
6----------
7
8The scattering intensity $I(q)$ is calculated as
9
10.. math::
11
12    I(q) = \begin{cases}
13    A q^{-m1} + \text{background} & q <= qc \\
14    C q^{-m2} + \text{background} & q > qc
15    \end{cases}
16
17where $qc$ = the location of the crossover from one slope to the other,
18$A$ = the scaling coefficent that sets the overall intensity of the lower Q power law region,
19$m1$ = power law exponent at low Q,
20$m2$ = power law exponent at high Q
21
22The scaling of the second power law region (coefficent C) is then automatically scaled to
23match the first by following formula
24
25.. math::
26    C = \frac{A}{qc^{-m1} qc^{-m2}}
27
28.. note::
29    Be sure to enter the power law exponents as positive values!
30
31For 2D data the scattering intensity is calculated in the same way as 1D,
32where the $q$ vector is defined as
33
34.. math::
35
36    q = \sqrt{q_x^2 + q_y^2}
37
38
39.. figure:: img/two_power_law_1d.jpg
40
41    1D plot using the default values (with 500 data point).
42
43References
44----------
45
46None.
47
48"""
49
50from numpy import power
51from numpy import sqrt
52from numpy import inf
53from numpy import concatenate
54
55name = "two_power_law"
56title = "Two Power Law intensity calculation"
57description = """
58            I(q) = coef_A*pow(qval,-1.0*power1) + background for q<=qc
59            =C*pow(qval,-1.0*power2) + background for q>qc
60            where C=coef_A*pow(qc,-1.0*power1)/pow(qc,-1.0*power2).
61
62            coef_A = scaling coefficent
63            qc = crossover location [1/A]
64            power_1 (=m1) = power law exponent at low Q
65            power_2 (=m2) = power law exponent at high Q
66            background = Incoherent background [1/cm]
67        """
68category = "shape-independent"
69
70# pylint: disable=bad-whitespace, line-too-long
71#            ["name", "units", default, [lower, upper], "type", "description"],
72parameters = [["coefficent_1",  "",         1.0, [-inf, inf], "", "coefficent A in low Q region"],
73              ["crossover",     "1/Ang",    0.04,[0, inf],    "", "crossover location"],
74              ["power_1",       "",         1.0, [0, inf],    "", "power law exponent at low Q"],
75              ["power_2",       "",         4.0, [0, inf],    "", "power law exponent at high Q"],
76             ]
77# pylint: enable=bad-whitespace, line-too-long
78
79
80def Iq(q,
81       coefficent_1=1.0,
82       crossover=0.04,
83       power_1=1.0,
84       power_2=4.0,
85      ):
86
87    """
88    :param q:                   Input q-value (float or [float, float])
89    :param coefficent_1:        Scaling coefficent at low Q
90    :param crossover:           Crossover location
91    :param power_1:             Exponent of power law function at low Q
92    :param power_2:             Exponent of power law function at high Q
93    :return:                    Calculated intensity
94    """
95
96    #Two sub vectors are created to treat crossover values
97    q_lower = q[q <= crossover]
98    q_upper = q[q > crossover]
99    coefficent_2 = coefficent_1*power(crossover, -1.0*power_1)/power(crossover, -1.0*power_2)
100    intensity_lower = coefficent_1*power(q_lower, -1.0*power_1)
101    intensity_upper = coefficent_2*power(q_upper, -1.0*power_2)
102    intensity = concatenate((intensity_lower, intensity_upper), axis=0)
103
104    return intensity
105
106Iq.vectorized = True  # Iq accepts an array of q values
107
108def Iqxy(qx, qy, *args):
109    """
110    :param qx:   Input q_x-value
111    :param qy:   Input q_y-value
112    :param args: Remaining arguments
113    :return:     2D-Intensity
114    """
115
116    return Iq(sqrt(qx**2 + qy**2), *args)
117
118Iqxy.vectorized = True  # Iqxy accepts an array of qx, qy values
119
120demo = dict(scale=1, background=0.0,
121            coefficent_1=1.0,
122            crossover=0.04,
123            power_1=1.0,
124            power_2=4.0)
125
126oldname = "TwoPowerLawModel"
127oldpars = dict(coefficent_1='coef_A',
128               crossover='qc',
129               power_1='power1',
130               power_2='power2',
131               background='background')
132
133tests = [
134    # Accuracy tests based on content in test/utest_extra_models.py
135    [{'coefficent_1':     1.0,
136      'crossover':  0.04,
137      'power_1':    1.0,
138      'power_2':    4.0,
139      'background': 0.0,
140     }, 0.001, 1000],
141
142    [{'coefficent_1':     1.0,
143      'crossover':  0.04,
144      'power_1':    1.0,
145      'power_2':    4.0,
146      'background': 0.0,
147     }, 0.150141, 0.125945],
148
149    [{'coeffcent_1':    1.0,
150      'crossover':  0.04,
151      'power_1':    1.0,
152      'power_2':    4.0,
153      'background': 0.0,
154     }, 0.442528, 0.00166884],
155
156    [{'coeffcent_1':    1.0,
157      'crossover':  0.04,
158      'power_1':    1.0,
159      'power_2':    4.0,
160      'background': 0.0,
161     }, (0.442528, 0.00166884), 0.00166884],
162
163]
Note: See TracBrowser for help on using the repository browser.