source: sasmodels/sasmodels/models/two_power_law.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: 4.5 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
39References
40----------
41
42None.
43
44"""
45
46from numpy import power
47from numpy import sqrt
48from numpy import inf
49from numpy import concatenate
50
51name = "two_power_law"
52title = "Two Power Law intensity calculation"
53description = """
54            I(q) = coef_A*pow(qval,-1.0*power1) + background for q<=qc
55            =C*pow(qval,-1.0*power2) + background for q>qc
56            where C=coef_A*pow(qc,-1.0*power1)/pow(qc,-1.0*power2).
57
58            coef_A = scaling coefficent
59            qc = crossover location [1/A]
60            power_1 (=m1) = power law exponent at low Q
61            power_2 (=m2) = power law exponent at high Q
62            background = Incoherent background [1/cm]
63        """
64category = "shape-independent"
65
66# pylint: disable=bad-whitespace, line-too-long
67#            ["name", "units", default, [lower, upper], "type", "description"],
68parameters = [["coefficent_1",  "",         1.0, [-inf, inf], "", "coefficent A in low Q region"],
69              ["crossover",     "1/Ang",    0.04,[0, inf],    "", "crossover location"],
70              ["power_1",       "",         1.0, [0, inf],    "", "power law exponent at low Q"],
71              ["power_2",       "",         4.0, [0, inf],    "", "power law exponent at high Q"],
72             ]
73# pylint: enable=bad-whitespace, line-too-long
74
75
76def Iq(q,
77       coefficent_1=1.0,
78       crossover=0.04,
79       power_1=1.0,
80       power_2=4.0,
81      ):
82
83    """
84    :param q:                   Input q-value (float or [float, float])
85    :param coefficent_1:        Scaling coefficent at low Q
86    :param crossover:           Crossover location
87    :param power_1:             Exponent of power law function at low Q
88    :param power_2:             Exponent of power law function at high Q
89    :return:                    Calculated intensity
90    """
91
92    #Two sub vectors are created to treat crossover values
93    q_lower = q[q <= crossover]
94    q_upper = q[q > crossover]
95    coefficent_2 = coefficent_1*power(crossover, -1.0*power_1)/power(crossover, -1.0*power_2)
96    intensity_lower = coefficent_1*power(q_lower, -1.0*power_1)
97    intensity_upper = coefficent_2*power(q_upper, -1.0*power_2)
98    intensity = concatenate((intensity_lower, intensity_upper), axis=0)
99
100    return intensity
101
102Iq.vectorized = True  # Iq accepts an array of q values
103
104def Iqxy(qx, qy, *args):
105    """
106    :param qx:   Input q_x-value
107    :param qy:   Input q_y-value
108    :param args: Remaining arguments
109    :return:     2D-Intensity
110    """
111
112    return Iq(sqrt(qx**2 + qy**2), *args)
113
114Iqxy.vectorized = True  # Iqxy accepts an array of qx, qy values
115
116demo = dict(scale=1, background=0.0,
117            coefficent_1=1.0,
118            crossover=0.04,
119            power_1=1.0,
120            power_2=4.0)
121
122oldname = "TwoPowerLawModel"
123oldpars = dict(coefficent_1='coef_A',
124               crossover='qc',
125               power_1='power1',
126               power_2='power2',
127               background='background')
128
129tests = [
130    # Accuracy tests based on content in test/utest_extra_models.py
131    [{'coefficent_1':     1.0,
132      'crossover':  0.04,
133      'power_1':    1.0,
134      'power_2':    4.0,
135      'background': 0.0,
136     }, 0.001, 1000],
137
138    [{'coefficent_1':     1.0,
139      'crossover':  0.04,
140      'power_1':    1.0,
141      'power_2':    4.0,
142      'background': 0.0,
143     }, 0.150141, 0.125945],
144
145    [{'coeffcent_1':    1.0,
146      'crossover':  0.04,
147      'power_1':    1.0,
148      'power_2':    4.0,
149      'background': 0.0,
150     }, 0.442528, 0.00166884],
151
152    [{'coeffcent_1':    1.0,
153      'crossover':  0.04,
154      'power_1':    1.0,
155      'power_2':    4.0,
156      'background': 0.0,
157     }, (0.442528, 0.00166884), 0.00166884],
158
159]
Note: See TracBrowser for help on using the repository browser.