source: sasmodels/sasmodels/models/_spherical_sld.py @ 56b2687

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 56b2687 was 56b2687, checked in by Paul Kienzle <pkienzle@…>, 8 years ago

Merge branch 'master' into polydisp

Conflicts:

README.rst
sasmodels/core.py
sasmodels/data.py
sasmodels/generate.py
sasmodels/kernelcl.py
sasmodels/kerneldll.py
sasmodels/sasview_model.py

  • Property mode set to 100644
File size: 11.9 KB
Line 
1r"""
2This model calculates an empirical functional form for SAS data using
3SpericalSLD profile
4
5Similarly to the OnionExpShellModel, this model provides the form factor,
6P(q), for a multi-shell sphere, where the interface between the each neighboring
7shells can be described by one of a number of functions including error,
8power-law, and exponential functions.
9This model is to calculate the scattering intensity by building a continuous
10custom SLD profile against the radius of the particle.
11The SLD profile is composed of a flat core, a flat solvent, a number (up to 9 )
12flat 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
19Unlike the <onion> model (using an analytical integration), the interfacial
20layers here are sub-divided and numerically integrated assuming each of the
21sub-layers are described by a line function.
22The number of the sub-layer can be given by users by setting the integer values
23of npts_inter. The form factor is normalized by the total volume of the sphere.
24
25Definition
26----------
27
28The 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
36For a spherically symmetric particle with a particle density $\rho_x(r)$
37the 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
44so 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
74Here we assumed that the SLDs of the core and solvent are constant against $r$.
75The SLD at the interface between shells, $\rho_{\text {inter}_i}$
76is calculated with a function chosen by an user, where the functions are
77
78Exp:
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
88Power-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
97Erf:
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
107The functions are normalized so that they vary between 0 and 1, and they are
108constrained such that the SLD is continuous at the boundaries of the interface
109as well as each sub-layers. Thus B and C are determined.
110
111Once $\rho_{\text{inter}_i}$ is found at the boundary of the sub-layer of the
112interface, 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
143where
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
154We assume the $\rho_{\text{inter}_i} (r)$ can be approximately linear
155within a sub-layer $j$
156
157Finally 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
164For 2D data the scattering intensity is calculated in the same way as 1D,
165where 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
184References
185----------
186L A Feigin and D I Svergun, Structure Analysis by Small-Angle X-Ray
187and Neutron Scattering, Plenum Press, New York, (1987)
188
189"""
190
191import numpy as np
192from numpy import inf
193
194name = "spherical_sld"
195title = "Sperical SLD intensity calculation"
196description = """
197            I(q) =
198               background = Incoherent background [1/cm]
199        """
200category = "sphere-based"
201
202# pylint: disable=bad-whitespace, line-too-long
203#            ["name", "units", default, [lower, upper], "type", "description"],
204parameters = [["n_shells",          "",               1,      [0, 9],         "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
219source = ["lib/librefl.c",  "lib/sph_j1c.c", "spherical_sld.c"]
220
221profile_axes = ['Radius (A)', 'SLD (1e-6/A^2)']
222def profile(n_shells, radius_core,  sld_core,  sld_solvent, sld_flat,
223            thick_flat, func_inter, thick_inter, nu_inter, npts_inter):
224    """
225    Returns shape profile with x=radius, y=SLD.
226    """
227
228    z = []
229    beta = []
230    z0 = 0
231    # two sld points for core
232    z.append(0)
233    beta.append(sld_core)
234    z.append(radius_core)
235    beta.append(sld_core)
236    z0 += radius_core
237
238    for i in range(1, n_shells+2):
239        dz = thick_inter[i-1]/npts_inter
240        # j=0 for interface, j=1 for flat layer
241        for j in range(0, 2):
242            # interation for sub-layers
243            for n_s in range(0, npts_inter+1):
244                if j == 1:
245                    if i == n_shells+1:
246                        break
247                    # shift half sub thickness for the first point
248                    z0 -= dz#/2.0
249                    z.append(z0)
250                    #z0 -= dz/2.0
251                    z0 += thick_flat[i]
252                    sld_i = sld_flat[i]
253                    beta.append(sld_flat[i])
254                    dz = 0
255                else:
256                    nu = nu_inter[i-1]
257                    # decide which sld is which, sld_r or sld_l
258                    if i == 1:
259                        sld_l = sld_core
260                    else:
261                        sld_l = sld_flat[i-1]
262                    if i == n_shells+1:
263                        sld_r = sld_solvent
264                    else:
265                        sld_r = sld_flat[i]
266                    # get function type
267                    func_idx = func_inter[i-1]
268                    # calculate the sld
269                    sld_i = intersldfunc(func_idx, npts_inter, n_s, nu,
270                                            sld_l, sld_r)
271                # append to the list
272                z.append(z0)
273                beta.append(sld_i)
274                z0 += dz
275                if j == 1:
276                    break
277    z.append(z0)
278    beta.append(sld_solvent)
279    z_ext = z0/5.0
280    z.append(z0+z_ext)
281    beta.append(sld_solvent)
282    # return sld profile (r, beta)
283    return np.asarray(z), np.asarray(beta)*1e-6
284
285def ER(n_shells, radius_core, thick_inter0, thick_inter, thick_flat):
286    total_thickness = thick_inter0
287    total_thickness += np.sum(thick_inter[:n_shells], axis=0)
288    total_thickness += np.sum(thick_flat[:n_shells], axis=0)
289    return total_thickness + radius_core
290
291
292demo = {
293    "n_shells": 4,
294    "npts_inter": 35.0,
295    "radius_core": 50.0,
296    "sld_core": 2.07,
297    "sld_solvent": 1.0,
298    "thick_inter0": 50.0,
299    "func_inter0": 0,
300    "nu_inter0": 2.5,
301    "sld_flat":[4.0,3.5,4.0,3.5],
302    "thick_flat":[100.0,100.0,100.0,100.0],
303    "func_inter":[0,0,0,0],
304    "thick_inter":[50.0,50.0,50.0,50.0],
305    "nu_inter":[2.5,2.5,2.5,2.5],
306    }
307
308#TODO: Not working yet
309tests = [
310    # Accuracy tests based on content in test/utest_extra_models.py
311    [{"n_shells":4,
312        'npts_inter':35,
313        "radius_core":50.0,
314        "sld_core":2.07,
315        "sld_solvent": 1.0,
316        "sld_flat":[4.0,3.5,4.0,3.5],
317        "thick_flat":[100.0,100.0,100.0,100.0],
318        "func_inter":[0,0,0,0],
319        "thick_inter":[50.0,50.0,50.0,50.0],
320        "nu_inter":[2.5,2.5,2.5,2.5]
321    }, 0.001, 0.001],
322]
Note: See TracBrowser for help on using the repository browser.