source: sasmodels/sasmodels/models/two_power_law.py @ b297ba9

core_shell_microgelsmagnetic_modelticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since b297ba9 was b297ba9, checked in by Paul Kienzle <pkienzle@…>, 7 months ago

lint

  • Property mode set to 100644
File size: 4.4 KB
Line 
1r"""
2Definition
3----------
4
5The scattering intensity $I(q)$ is calculated as
6
7.. math::
8
9    I(q) = \begin{cases}
10    A q^{-m1} + \text{background} & q <= q_c \\
11    C q^{-m2} + \text{background} & q > q_c
12    \end{cases}
13
14where $q_c$ = the location of the crossover from one slope to the other,
15$A$ = the scaling coefficent that sets the overall intensity of the lower Q
16power law region, $m1$ = power law exponent at low Q, and $m2$ = power law
17exponent at high Q.  The scaling of the second power law region (coefficent C)
18is then automatically scaled to match the first by following formula:
19
20.. math::
21    C = \frac{A q_c^{m2}}{q_c^{m1}}
22
23.. note::
24    Be sure to enter the power law exponents as positive values!
25
26For 2D data the scattering intensity is calculated in the same way as 1D,
27where the $q$ vector is defined as
28
29.. math::
30
31    q = \sqrt{q_x^2 + q_y^2}
32
33
34References
35----------
36
37None.
38
39* **Author:** NIST IGOR/DANSE **Date:** pre 2010
40* **Last Modified by:** Wojciech Wpotrzebowski **Date:** February 18, 2016
41* **Last Reviewed by:** Paul Butler **Date:** March 21, 2016
42"""
43
44import numpy as np
45from numpy import inf, power, empty, errstate
46
47name = "two_power_law"
48title = "This model calculates an empirical functional form for SAS data \
49characterized by two power laws."
50description = """
51            I(q) = coef_A*pow(qval,-1.0*power1) + background for q<=q_c
52            =C*pow(qval,-1.0*power2) + background for q>q_c
53            where C=coef_A*pow(q_c,-1.0*power1)/pow(q_c,-1.0*power2).
54
55            coef_A = scaling coefficent
56            q_c = crossover location [1/A]
57            power_1 (=m1) = power law exponent at low Q
58            power_2 (=m2) = power law exponent at high Q
59            background = Incoherent background [1/cm]
60        """
61category = "shape-independent"
62
63# pylint: disable=bad-whitespace, line-too-long
64#   ["name", "units", default, [lower, upper], "type", "description"],
65parameters = [
66    ["coefficent_1", "",       1.0, [-inf, inf], "", "coefficent A in low Q region"],
67    ["crossover",    "1/Ang",  0.04,[0, inf],    "", "crossover location"],
68    ["power_1",      "",       1.0, [0, inf],    "", "power law exponent at low Q"],
69    ["power_2",      "",       4.0, [0, inf],    "", "power law exponent at high Q"],
70    ]
71# pylint: enable=bad-whitespace, line-too-long
72
73
74def Iq(q,
75       coefficent_1=1.0,
76       crossover=0.04,
77       power_1=1.0,
78       power_2=4.0,
79      ):
80    """
81    :param q:                   Input q-value (float or [float, float])
82    :param coefficent_1:        Scaling coefficent at low Q
83    :param crossover:           Crossover location
84    :param power_1:             Exponent of power law function at low Q
85    :param power_2:             Exponent of power law function at high Q
86    :return:                    Calculated intensity
87    """
88    result = empty(q.shape, 'd')
89    index = (q <= crossover)
90    with errstate(divide='ignore'):
91        coefficent_2 = coefficent_1 * power(crossover, power_2 - power_1)
92        result[index] = coefficent_1 * power(q[index], -power_1)
93        result[~index] = coefficent_2 * power(q[~index], -power_2)
94    return result
95
96Iq.vectorized = True  # Iq accepts an array of q values
97
98def random():
99    """Return a random parameter set for the model."""
100    coefficient_1 = 1
101    crossover = 10**np.random.uniform(-3, -1)
102    power_1 = np.random.uniform(1, 6)
103    power_2 = np.random.uniform(1, 6)
104    pars = dict(
105        scale=1, #background=0,
106        coefficient_1=coefficient_1,
107        crossover=crossover,
108        power_1=power_1,
109        power_2=power_2,
110    )
111    return pars
112
113demo = dict(scale=1, background=0.0,
114            coefficent_1=1.0,
115            crossover=0.04,
116            power_1=1.0,
117            power_2=4.0)
118
119tests = [
120    # Accuracy tests based on content in test/utest_extra_models.py
121    [{'coefficent_1':     1.0,
122      'crossover':  0.04,
123      'power_1':    1.0,
124      'power_2':    4.0,
125      'background': 0.0,
126     }, 0.001, 1000],
127
128    [{'coefficent_1':     1.0,
129      'crossover':  0.04,
130      'power_1':    1.0,
131      'power_2':    4.0,
132      'background': 0.0,
133     }, 0.150141, 0.125945],
134
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.442528, 0.00166884],
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.442528, 0.00166884), 0.00166884],
148
149]
Note: See TracBrowser for help on using the repository browser.