1 | r""" |
---|
2 | Definition |
---|
3 | ---------- |
---|
4 | |
---|
5 | The 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 | |
---|
14 | where $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 |
---|
16 | power law region, $m1$ = power law exponent at low Q, and $m2$ = power law |
---|
17 | exponent at high Q. The scaling of the second power law region (coefficent C) |
---|
18 | is 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 | |
---|
26 | For 2D data the scattering intensity is calculated in the same way as 1D, |
---|
27 | where the $q$ vector is defined as |
---|
28 | |
---|
29 | .. math:: |
---|
30 | |
---|
31 | q = \sqrt{q_x^2 + q_y^2} |
---|
32 | |
---|
33 | |
---|
34 | References |
---|
35 | ---------- |
---|
36 | |
---|
37 | None. |
---|
38 | |
---|
39 | **Author:** NIST IGOR/DANSE **on:** pre 2010 |
---|
40 | |
---|
41 | **Last Modified by:** Wojciech Wpotrzebowski **on:** February 18, 2016 |
---|
42 | |
---|
43 | **Last Reviewed by:** Paul Butler **on:** March 21, 2016 |
---|
44 | """ |
---|
45 | |
---|
46 | import numpy as np |
---|
47 | from numpy import inf, power, empty, errstate |
---|
48 | |
---|
49 | name = "two_power_law" |
---|
50 | title = "This model calculates an empirical functional form for SAS data \ |
---|
51 | characterized by two power laws." |
---|
52 | description = """ |
---|
53 | I(q) = coef_A*pow(qval,-1.0*power1) + background for q<=q_c |
---|
54 | =C*pow(qval,-1.0*power2) + background for q>q_c |
---|
55 | where C=coef_A*pow(q_c,-1.0*power1)/pow(q_c,-1.0*power2). |
---|
56 | |
---|
57 | coef_A = scaling coefficent |
---|
58 | q_c = crossover location [1/A] |
---|
59 | power_1 (=m1) = power law exponent at low Q |
---|
60 | power_2 (=m2) = power law exponent at high Q |
---|
61 | background = Incoherent background [1/cm] |
---|
62 | """ |
---|
63 | category = "shape-independent" |
---|
64 | |
---|
65 | # pylint: disable=bad-whitespace, line-too-long |
---|
66 | # ["name", "units", default, [lower, upper], "type", "description"], |
---|
67 | parameters = [ |
---|
68 | ["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 | |
---|
76 | def 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 | result = empty(q.shape, 'd') |
---|
92 | index = (q <= crossover) |
---|
93 | with errstate(divide='ignore'): |
---|
94 | coefficent_2 = coefficent_1 * power(crossover, power_2 - power_1) |
---|
95 | result[index] = coefficent_1 * power(q[index], -power_1) |
---|
96 | result[~index] = coefficent_2 * power(q[~index], -power_2) |
---|
97 | return result |
---|
98 | |
---|
99 | Iq.vectorized = True # Iq accepts an array of q values |
---|
100 | |
---|
101 | def random(): |
---|
102 | coefficient_1 = 1 |
---|
103 | crossover = 10**np.random.uniform(-3, -1) |
---|
104 | power_1 = np.random.uniform(1, 6) |
---|
105 | power_2 = np.random.uniform(1, 6) |
---|
106 | pars = dict( |
---|
107 | scale=1, #background=0, |
---|
108 | coefficient_1=coefficient_1, |
---|
109 | crossover=crossover, |
---|
110 | power_1=power_1, |
---|
111 | power_2=power_2, |
---|
112 | ) |
---|
113 | return pars |
---|
114 | |
---|
115 | demo = dict(scale=1, background=0.0, |
---|
116 | coefficent_1=1.0, |
---|
117 | crossover=0.04, |
---|
118 | power_1=1.0, |
---|
119 | power_2=4.0) |
---|
120 | |
---|
121 | tests = [ |
---|
122 | # Accuracy tests based on content in test/utest_extra_models.py |
---|
123 | [{'coefficent_1': 1.0, |
---|
124 | 'crossover': 0.04, |
---|
125 | 'power_1': 1.0, |
---|
126 | 'power_2': 4.0, |
---|
127 | 'background': 0.0, |
---|
128 | }, 0.001, 1000], |
---|
129 | |
---|
130 | [{'coefficent_1': 1.0, |
---|
131 | 'crossover': 0.04, |
---|
132 | 'power_1': 1.0, |
---|
133 | 'power_2': 4.0, |
---|
134 | 'background': 0.0, |
---|
135 | }, 0.150141, 0.125945], |
---|
136 | |
---|
137 | [{'coefficent_1': 1.0, |
---|
138 | 'crossover': 0.04, |
---|
139 | 'power_1': 1.0, |
---|
140 | 'power_2': 4.0, |
---|
141 | 'background': 0.0, |
---|
142 | }, 0.442528, 0.00166884], |
---|
143 | |
---|
144 | [{'coefficent_1': 1.0, |
---|
145 | 'crossover': 0.04, |
---|
146 | 'power_1': 1.0, |
---|
147 | 'power_2': 4.0, |
---|
148 | 'background': 0.0, |
---|
149 | }, (0.442528, 0.00166884), 0.00166884], |
---|
150 | |
---|
151 | ] |
---|