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 **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 | |
---|
44 | import numpy as np |
---|
45 | from numpy import inf, power, empty, errstate |
---|
46 | |
---|
47 | name = "two_power_law" |
---|
48 | title = "This model calculates an empirical functional form for SAS data \ |
---|
49 | characterized by two power laws." |
---|
50 | description = """ |
---|
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 | """ |
---|
61 | category = "shape-independent" |
---|
62 | |
---|
63 | # pylint: disable=bad-whitespace, line-too-long |
---|
64 | # ["name", "units", default, [lower, upper], "type", "description"], |
---|
65 | parameters = [ |
---|
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 | |
---|
74 | def Iq(q, |
---|
75 | coefficent_1=1.0, |
---|
76 | crossover=0.04, |
---|
77 | power_1=1.0, |
---|
78 | power_2=4.0, |
---|
79 | ): |
---|
80 | |
---|
81 | """ |
---|
82 | :param q: Input q-value (float or [float, float]) |
---|
83 | :param coefficent_1: Scaling coefficent at low Q |
---|
84 | :param crossover: Crossover location |
---|
85 | :param power_1: Exponent of power law function at low Q |
---|
86 | :param power_2: Exponent of power law function at high Q |
---|
87 | :return: Calculated intensity |
---|
88 | """ |
---|
89 | result = empty(q.shape, 'd') |
---|
90 | index = (q <= crossover) |
---|
91 | with errstate(divide='ignore'): |
---|
92 | coefficent_2 = coefficent_1 * power(crossover, power_2 - power_1) |
---|
93 | result[index] = coefficent_1 * power(q[index], -power_1) |
---|
94 | result[~index] = coefficent_2 * power(q[~index], -power_2) |
---|
95 | return result |
---|
96 | |
---|
97 | Iq.vectorized = True # Iq accepts an array of q values |
---|
98 | |
---|
99 | def random(): |
---|
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 | |
---|
113 | demo = 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 | |
---|
119 | tests = [ |
---|
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 | ] |
---|