source: sasmodels/sasmodels/models/two_power_law.py @ 8115d82

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

Two Power Law code converted from old model and ready for testing

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