1 | r""" |
---|
2 | This model calculates an empirical functional form for SAS data using |
---|
3 | SpericalSLD profile |
---|
4 | |
---|
5 | Similarly to the OnionExpShellModel, this model provides the form factor, |
---|
6 | P(q), for a multi-shell sphere, where the interface between the each neighboring |
---|
7 | shells can be described by one of a number of functions including error, |
---|
8 | power-law, and exponential functions. |
---|
9 | This model is to calculate the scattering intensity by building a continuous |
---|
10 | custom SLD profile against the radius of the particle. |
---|
11 | The SLD profile is composed of a flat core, a flat solvent, a number (up to 9 ) |
---|
12 | flat shells, and the interfacial layers between the adjacent flat shells |
---|
13 | (or core, and solvent) (see below). |
---|
14 | |
---|
15 | .. figure:: img/spherical_sld_profile.gif |
---|
16 | |
---|
17 | Exemplary SLD profile |
---|
18 | |
---|
19 | Unlike the <onion> model (using an analytical integration), the interfacial |
---|
20 | layers here are sub-divided and numerically integrated assuming each of the |
---|
21 | sub-layers are described by a line function. |
---|
22 | The number of the sub-layer can be given by users by setting the integer values |
---|
23 | of npts_inter. The form factor is normalized by the total volume of the sphere. |
---|
24 | |
---|
25 | Definition |
---|
26 | ---------- |
---|
27 | |
---|
28 | The form factor $P(q)$ in 1D is calculated by: |
---|
29 | |
---|
30 | .. math:: |
---|
31 | |
---|
32 | P(q) = \frac{f^2}{V_\text{particle}} \text{ where } |
---|
33 | f = f_\text{core} + \sum_{\text{inter}_i=0}^N f_{\text{inter}_i} + |
---|
34 | \sum_{\text{flat}_i=0}^N f_{\text{flat}_i} +f_\text{solvent} |
---|
35 | |
---|
36 | For a spherically symmetric particle with a particle density $\rho_x(r)$ |
---|
37 | the sld function can be defined as: |
---|
38 | |
---|
39 | .. math:: |
---|
40 | |
---|
41 | f_x = 4 \pi \int_{0}^{\infty} \rho_x(r) \frac{\sin(qr)} {qr^2} r^2 dr |
---|
42 | |
---|
43 | |
---|
44 | so that individual terms can be calcualted as follows: |
---|
45 | |
---|
46 | .. math:: |
---|
47 | f_\text{core} = 4 \pi \int_{0}^{r_\text{core}} \rho_\text{core} |
---|
48 | \frac{\sin(qr)} {qr} r^2 dr = |
---|
49 | 3 \rho_\text{core} V(r_\text{core}) |
---|
50 | \Big[ \frac{\sin(qr_\text{core}) - qr_\text{core} \cos(qr_\text{core})} |
---|
51 | {qr_\text{core}^3} \Big] |
---|
52 | |
---|
53 | f_{\text{inter}_i} = 4 \pi \int_{\Delta t_{ \text{inter}_i } } |
---|
54 | \rho_{ \text{inter}_i } \frac{\sin(qr)} {qr} r^2 dr |
---|
55 | |
---|
56 | f_{\text{shell}_i} = 4 \pi \int_{\Delta t_{ \text{inter}_i } } |
---|
57 | \rho_{ \text{flat}_i } \frac{\sin(qr)} {qr} r^2 dr = |
---|
58 | 3 \rho_{ \text{flat}_i } V ( r_{ \text{inter}_i } + |
---|
59 | \Delta t_{ \text{inter}_i } ) |
---|
60 | \Big[ \frac{\sin(qr_{\text{inter}_i} + \Delta t_{ \text{inter}_i } ) |
---|
61 | - q (r_{\text{inter}_i} + \Delta t_{ \text{inter}_i }) |
---|
62 | \cos(q( r_{\text{inter}_i} + \Delta t_{ \text{inter}_i } ) ) } |
---|
63 | {q ( r_{\text{inter}_i} + \Delta t_{ \text{inter}_i } )^3 } \Big] |
---|
64 | -3 \rho_{ \text{flat}_i } V(r_{ \text{inter}_i }) |
---|
65 | \Big[ \frac{\sin(qr_{\text{inter}_i}) - qr_{\text{flat}_i} |
---|
66 | \cos(qr_{\text{inter}_i}) } {qr_{\text{inter}_i}^3} \Big] |
---|
67 | |
---|
68 | f_\text{solvent} = 4 \pi \int_{r_N}^{\infty} \rho_\text{solvent} |
---|
69 | \frac{\sin(qr)} {qr} r^2 dr = |
---|
70 | 3 \rho_\text{solvent} V(r_N) |
---|
71 | \Big[ \frac{\sin(qr_N) - qr_N \cos(qr_N)} {qr_N^3} \Big] |
---|
72 | |
---|
73 | |
---|
74 | Here we assumed that the SLDs of the core and solvent are constant against $r$. |
---|
75 | The SLD at the interface between shells, $\rho_{\text {inter}_i}$ |
---|
76 | is calculated with a function chosen by an user, where the functions are |
---|
77 | |
---|
78 | Exp: |
---|
79 | |
---|
80 | .. math:: |
---|
81 | \rho_{{inter}_i} (r) = \begin{cases} |
---|
82 | B \exp\Big( \frac {\pm A(r - r_{\text{flat}_i})} |
---|
83 | {\Delta t_{ \text{inter}_i }} \Big) +C & \text{for} A \neq 0 \\ |
---|
84 | B \Big( \frac {(r - r_{\text{flat}_i})} |
---|
85 | {\Delta t_{ \text{inter}_i }} \Big) +C & \text{for} A = 0 \\ |
---|
86 | \end{cases} |
---|
87 | |
---|
88 | Power-Law |
---|
89 | |
---|
90 | .. math:: |
---|
91 | \rho_{{inter}_i} (r) = \begin{cases} |
---|
92 | \pm B \Big( \frac {(r - r_{\text{flat}_i} )} {\Delta t_{ \text{inter}_i }} |
---|
93 | \Big) ^A +C & \text{for} A \neq 0 \\ |
---|
94 | \rho_{\text{flat}_{i+1}} & \text{for} A = 0 \\ |
---|
95 | \end{cases} |
---|
96 | |
---|
97 | Erf: |
---|
98 | |
---|
99 | .. math:: |
---|
100 | \rho_{{inter}_i} (r) = \begin{cases} |
---|
101 | B \text{erf} \Big( \frac { A(r - r_{\text{flat}_i})} |
---|
102 | {\sqrt{2} \Delta t_{ \text{inter}_i }} \Big) +C & \text{for} A \neq 0 \\ |
---|
103 | B \Big( \frac {(r - r_{\text{flat}_i} )} {\Delta t_{ \text{inter}_i }} |
---|
104 | \Big) +C & \text{for} A = 0 \\ |
---|
105 | \end{cases} |
---|
106 | |
---|
107 | The functions are normalized so that they vary between 0 and 1, and they are |
---|
108 | constrained such that the SLD is continuous at the boundaries of the interface |
---|
109 | as well as each sub-layers. Thus B and C are determined. |
---|
110 | |
---|
111 | Once $\rho_{\text{inter}_i}$ is found at the boundary of the sub-layer of the |
---|
112 | interface, we can find its contribution to the form factor $P(q)$ |
---|
113 | |
---|
114 | .. math:: |
---|
115 | f_{\text{inter}_i} = 4 \pi \int_{\Delta t_{ \text{inter}_i } } |
---|
116 | \rho_{ \text{inter}_i } \frac{\sin(qr)} {qr} r^2 dr = |
---|
117 | 4 \pi \sum_{j=0}^{npts_{\text{inter}_i} -1 } |
---|
118 | \int_{r_j}^{r_{j+1}} \rho_{ \text{inter}_i } (r_j) |
---|
119 | \frac{\sin(qr)} {qr} r^2 dr \approx |
---|
120 | |
---|
121 | 4 \pi \sum_{j=0}^{npts_{\text{inter}_i} -1 } \Big[ |
---|
122 | 3 ( \rho_{ \text{inter}_i } ( r_{j+1} ) - \rho_{ \text{inter}_i } |
---|
123 | ( r_{j} ) V ( r_{ \text{sublayer}_j } ) |
---|
124 | \Big[ \frac {r_j^2 \beta_\text{out}^2 \sin(\beta_\text{out}) |
---|
125 | - (\beta_\text{out}^2-2) \cos(\beta_\text{out}) } |
---|
126 | {\beta_\text{out}^4 } \Big] |
---|
127 | |
---|
128 | - 3 ( \rho_{ \text{inter}_i } ( r_{j+1} ) - \rho_{ \text{inter}_i } |
---|
129 | ( r_{j} ) V ( r_{ \text{sublayer}_j-1 } ) |
---|
130 | \Big[ \frac {r_{j-1}^2 \sin(\beta_\text{in}) |
---|
131 | - (\beta_\text{in}^2-2) \cos(\beta_\text{in}) } |
---|
132 | {\beta_\text{in}^4 } \Big] |
---|
133 | |
---|
134 | + 3 \rho_{ \text{inter}_i } ( r_{j+1} ) V ( r_j ) |
---|
135 | \Big[ \frac {\sin(\beta_\text{out}) - \cos(\beta_\text{out}) } |
---|
136 | {\beta_\text{out}^4 } \Big] |
---|
137 | |
---|
138 | - 3 \rho_{ \text{inter}_i } ( r_{j} ) V ( r_j ) |
---|
139 | \Big[ \frac {\sin(\beta_\text{in}) - \cos(\beta_\text{in}) } |
---|
140 | {\beta_\text{in}^4 } \Big] |
---|
141 | \Big] |
---|
142 | |
---|
143 | where |
---|
144 | |
---|
145 | .. math:: |
---|
146 | V(a) = \frac {4\pi}{3}a^3 |
---|
147 | |
---|
148 | a_\text{in} ~ \frac{r_j}{r_{j+1} -r_j} \text{, } a_\text{out} |
---|
149 | ~ \frac{r_{j+1}}{r_{j+1} -r_j} |
---|
150 | |
---|
151 | \beta_\text{in} = qr_j \text{, } \beta_\text{out} = qr_{j+1} |
---|
152 | |
---|
153 | |
---|
154 | We assume the $\rho_{\text{inter}_i} (r)$ can be approximately linear |
---|
155 | within a sub-layer $j$ |
---|
156 | |
---|
157 | Finally form factor can be calculated by |
---|
158 | |
---|
159 | .. math:: |
---|
160 | |
---|
161 | P(q) = \frac{[f]^2} {V_\text{particle}} \text{where} V_\text{particle} |
---|
162 | = V(r_{\text{shell}_N}) |
---|
163 | |
---|
164 | For 2D data the scattering intensity is calculated in the same way as 1D, |
---|
165 | where the $q$ vector is defined as |
---|
166 | |
---|
167 | .. math:: |
---|
168 | |
---|
169 | q = \sqrt{q_x^2 + q_y^2} |
---|
170 | |
---|
171 | |
---|
172 | .. figure:: img/spherical_sld_1d.jpg |
---|
173 | |
---|
174 | 1D plot using the default values (w/400 data point). |
---|
175 | |
---|
176 | .. figure:: img/spherical_sld_default_profile.jpg |
---|
177 | |
---|
178 | SLD profile from the default values. |
---|
179 | |
---|
180 | .. note:: |
---|
181 | The outer most radius is used as the effective radius for S(Q) |
---|
182 | when $P(Q) * S(Q)$ is applied. |
---|
183 | |
---|
184 | References |
---|
185 | ---------- |
---|
186 | L A Feigin and D I Svergun, Structure Analysis by Small-Angle X-Ray |
---|
187 | and Neutron Scattering, Plenum Press, New York, (1987) |
---|
188 | |
---|
189 | """ |
---|
190 | |
---|
191 | import numpy as np |
---|
192 | from numpy import inf |
---|
193 | |
---|
194 | name = "spherical_sld" |
---|
195 | title = "Sperical SLD intensity calculation" |
---|
196 | description = """ |
---|
197 | I(q) = |
---|
198 | background = Incoherent background [1/cm] |
---|
199 | """ |
---|
200 | category = "sphere-based" |
---|
201 | |
---|
202 | # pylint: disable=bad-whitespace, line-too-long |
---|
203 | # ["name", "units", default, [lower, upper], "type", "description"], |
---|
204 | parameters = [["n_shells", "", 1, [0, 10], "volume", "number of shells"], |
---|
205 | ["npts_inter", "", 35, [0, inf], "", "number of points in each sublayer Must be odd number"], |
---|
206 | ["radius_core", "Ang", 50.0, [0, inf], "volume", "intern layer thickness"], |
---|
207 | ["sld_core", "1e-6/Ang^2", 2.07, [-inf, inf], "", "sld function flat"], |
---|
208 | ["sld_solvent", "1e-6/Ang^2", 1.0, [-inf, inf], "", "sld function solvent"], |
---|
209 | ["func_inter0", "", 0, [0, 4], "", "Erf:0, RPower:1, LPower:2, RExp:3, LExp:4"], |
---|
210 | ["thick_inter0", "Ang", 50.0, [0, inf], "volume", "intern layer thickness for core layer"], |
---|
211 | ["nu_inter0", "", 2.5, [-inf, inf], "", "steepness parameter for core layer"], |
---|
212 | ["sld_flat[n_shells]", "1e-6/Ang^2", 4.06, [-inf, inf], "", "sld function flat"], |
---|
213 | ["thick_flat[n_shells]", "Ang", 100.0, [0, inf], "volume", "flat layer_thickness"], |
---|
214 | ["func_inter[n_shells]", "", 0, [0, 4], "", "Erf:0, RPower:1, LPower:2, RExp:3, LExp:4"], |
---|
215 | ["thick_inter[n_shells]", "Ang", 50.0, [0, inf], "volume", "intern layer thickness"], |
---|
216 | ["nu_inter[n_shells]", "", 2.5, [-inf, inf], "", "steepness parameter"], |
---|
217 | ] |
---|
218 | # pylint: enable=bad-whitespace, line-too-long |
---|
219 | source = ["lib/librefl.c", "lib/sph_j1c.c", "spherical_sld.c"] |
---|
220 | single = False |
---|
221 | |
---|
222 | profile_axes = ['Radius (A)', 'SLD (1e-6/A^2)'] |
---|
223 | def profile(n_shells, radius_core, sld_core, sld_solvent, sld_flat, |
---|
224 | thick_flat, func_inter, thick_inter, nu_inter, npts_inter): |
---|
225 | """ |
---|
226 | Returns shape profile with x=radius, y=SLD. |
---|
227 | """ |
---|
228 | |
---|
229 | z = [] |
---|
230 | beta = [] |
---|
231 | z0 = 0 |
---|
232 | # two sld points for core |
---|
233 | z.append(0) |
---|
234 | beta.append(sld_core) |
---|
235 | z.append(radius_core) |
---|
236 | beta.append(sld_core) |
---|
237 | z0 += radius_core |
---|
238 | |
---|
239 | for i in range(1, n_shells+2): |
---|
240 | dz = thick_inter[i-1]/npts_inter |
---|
241 | # j=0 for interface, j=1 for flat layer |
---|
242 | for j in range(0, 2): |
---|
243 | # interation for sub-layers |
---|
244 | for n_s in range(0, npts_inter+1): |
---|
245 | if j == 1: |
---|
246 | if i == n_shells+1: |
---|
247 | break |
---|
248 | # shift half sub thickness for the first point |
---|
249 | z0 -= dz#/2.0 |
---|
250 | z.append(z0) |
---|
251 | #z0 -= dz/2.0 |
---|
252 | z0 += thick_flat[i] |
---|
253 | sld_i = sld_flat[i] |
---|
254 | beta.append(sld_flat[i]) |
---|
255 | dz = 0 |
---|
256 | else: |
---|
257 | nu = nu_inter[i-1] |
---|
258 | # decide which sld is which, sld_r or sld_l |
---|
259 | if i == 1: |
---|
260 | sld_l = sld_core |
---|
261 | else: |
---|
262 | sld_l = sld_flat[i-1] |
---|
263 | if i == n_shells+1: |
---|
264 | sld_r = sld_solvent |
---|
265 | else: |
---|
266 | sld_r = sld_flat[i] |
---|
267 | # get function type |
---|
268 | func_idx = func_inter[i-1] |
---|
269 | # calculate the sld |
---|
270 | sld_i = intersldfunc(func_idx, npts_inter, n_s, nu, |
---|
271 | sld_l, sld_r) |
---|
272 | # append to the list |
---|
273 | z.append(z0) |
---|
274 | beta.append(sld_i) |
---|
275 | z0 += dz |
---|
276 | if j == 1: |
---|
277 | break |
---|
278 | z.append(z0) |
---|
279 | beta.append(sld_solvent) |
---|
280 | z_ext = z0/5.0 |
---|
281 | z.append(z0+z_ext) |
---|
282 | beta.append(sld_solvent) |
---|
283 | # return sld profile (r, beta) |
---|
284 | return np.asarray(z), np.asarray(beta)*1e-6 |
---|
285 | |
---|
286 | def ER(n_shells, radius_core, thick_inter0, thick_inter, thick_flat): |
---|
287 | n_shells = int(n_shells) |
---|
288 | total_thickness = thick_inter0 |
---|
289 | total_thickness += np.sum(thick_inter[:n_shells], axis=0) |
---|
290 | total_thickness += np.sum(thick_flat[:n_shells], axis=0) |
---|
291 | return total_thickness + radius_core |
---|
292 | |
---|
293 | |
---|
294 | demo = { |
---|
295 | "n_shells": 4, |
---|
296 | "npts_inter": 35.0, |
---|
297 | "radius_core": 50.0, |
---|
298 | "sld_core": 2.07, |
---|
299 | "sld_solvent": 1.0, |
---|
300 | "thick_inter0": 50.0, |
---|
301 | "func_inter0": 0, |
---|
302 | "nu_inter0": 2.5, |
---|
303 | "sld_flat":[4.0,3.5,4.0,3.5], |
---|
304 | "thick_flat":[100.0,100.0,100.0,100.0], |
---|
305 | "func_inter":[0,0,0,0], |
---|
306 | "thick_inter":[50.0,50.0,50.0,50.0], |
---|
307 | "nu_inter":[2.5,2.5,2.5,2.5], |
---|
308 | } |
---|
309 | |
---|
310 | #TODO: Not working yet |
---|
311 | """ |
---|
312 | tests = [ |
---|
313 | # Accuracy tests based on content in test/utest_extra_models.py |
---|
314 | [{"n_shells":4, |
---|
315 | 'npts_inter':35, |
---|
316 | "radius_core":50.0, |
---|
317 | "sld_core":2.07, |
---|
318 | "sld_solvent": 1.0, |
---|
319 | "sld_flat":[4.0,3.5,4.0,3.5], |
---|
320 | "thick_flat":[100.0,100.0,100.0,100.0], |
---|
321 | "func_inter":[0,0,0,0], |
---|
322 | "thick_inter":[50.0,50.0,50.0,50.0], |
---|
323 | "nu_inter":[2.5,2.5,2.5,2.5] |
---|
324 | }, 0.001, 0.001], |
---|
325 | ] |
---|
326 | """ |
---|