source: sasmodels/sasmodels/models/spherical_sld.py @ 50ec515

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

spherical sld: document interface shape number→interface relationship since UI doesn't show dropdown list yet

  • Property mode set to 100644
File size: 10.0 KB
Line 
1r"""
2Similarly to the onion, this model provides the form factor, $P(q)$, for
3a multi-shell sphere, where the interface between the each neighboring
4shells can be described by the error function, power-law, or exponential
5functions.  The scattering intensity is computed by building a continuous
6custom SLD profile along the radius of the particle. The SLD profile is
7composed of a number of uniform shells with interfacial shells between them.
8
9.. figure:: img/spherical_sld_profile.png
10
11    Example SLD profile
12
13Unlike the <onion> model (using an analytical integration), the interfacial
14shells here are sub-divided and numerically integrated assuming each
15sub-shell is described by a line function, with *n_steps* sub-shells per
16interface. The form factor is normalized by the total volume of the sphere.
17
18Interface shapes are as follows::
19
20    0: erf(|nu|*z)
21    1: Rpow(z^|nu|)
22    2: Lpow(z^|nu|)
23    3: Rexp(-|nu|z)
24    4: Lexp(-|nu|z)
25
26Definition
27----------
28
29The form factor $P(q)$ in 1D is calculated by:
30
31.. math::
32
33    P(q) = \frac{f^2}{V_\text{particle}} \text{ where }
34    f = f_\text{core} + \sum_{\text{inter}_i=0}^N f_{\text{inter}_i} +
35    \sum_{\text{flat}_i=0}^N f_{\text{flat}_i} +f_\text{solvent}
36
37For a spherically symmetric particle with a particle density $\rho_x(r)$
38the sld function can be defined as:
39
40.. math::
41
42    f_x = 4 \pi \int_{0}^{\infty} \rho_x(r)  \frac{\sin(qr)} {qr^2} r^2 dr
43
44
45so that individual terms can be calculated as follows:
46
47.. math::
48    f_\text{core} = 4 \pi \int_{0}^{r_\text{core}} \rho_\text{core}
49    \frac{\sin(qr)} {qr} r^2 dr =
50    3 \rho_\text{core} V(r_\text{core})
51    \Big[ \frac{\sin(qr_\text{core}) - qr_\text{core} \cos(qr_\text{core})}
52    {qr_\text{core}^3} \Big]
53
54    f_{\text{inter}_i} = 4 \pi \int_{\Delta t_{ \text{inter}_i } }
55    \rho_{ \text{inter}_i } \frac{\sin(qr)} {qr} r^2 dr
56
57    f_{\text{shell}_i} = 4 \pi \int_{\Delta t_{ \text{inter}_i } }
58    \rho_{ \text{flat}_i } \frac{\sin(qr)} {qr} r^2 dr =
59    3 \rho_{ \text{flat}_i } V ( r_{ \text{inter}_i } +
60    \Delta t_{ \text{inter}_i } )
61    \Big[ \frac{\sin(qr_{\text{inter}_i} + \Delta t_{ \text{inter}_i } )
62    - q (r_{\text{inter}_i} + \Delta t_{ \text{inter}_i })
63    \cos(q( r_{\text{inter}_i} + \Delta t_{ \text{inter}_i } ) ) }
64    {q ( r_{\text{inter}_i} + \Delta t_{ \text{inter}_i } )^3 }  \Big]
65    -3 \rho_{ \text{flat}_i } V(r_{ \text{inter}_i })
66    \Big[ \frac{\sin(qr_{\text{inter}_i}) - qr_{\text{flat}_i}
67    \cos(qr_{\text{inter}_i}) } {qr_{\text{inter}_i}^3} \Big]
68
69    f_\text{solvent} = 4 \pi \int_{r_N}^{\infty} \rho_\text{solvent}
70    \frac{\sin(qr)} {qr} r^2 dr =
71    3 \rho_\text{solvent} V(r_N)
72    \Big[ \frac{\sin(qr_N) - qr_N \cos(qr_N)} {qr_N^3} \Big]
73
74
75Here we assumed that the SLDs of the core and solvent are constant in $r$.
76The SLD at the interface between shells, $\rho_{\text {inter}_i}$
77is calculated with a function chosen by an user, where the functions are
78
79Exp:
80
81.. math::
82    \rho_{{inter}_i} (r) = \begin{cases}
83    B \exp\Big( \frac {\pm A(r - r_{\text{flat}_i})}
84    {\Delta t_{ \text{inter}_i }} \Big) +C  & \text{for} A \neq 0 \\
85    B \Big( \frac {(r - r_{\text{flat}_i})}
86    {\Delta t_{ \text{inter}_i }} \Big) +C  & \text{for} A = 0 \\
87    \end{cases}
88
89Power-Law
90
91.. math::
92    \rho_{{inter}_i} (r) = \begin{cases}
93    \pm B \Big( \frac {(r - r_{\text{flat}_i} )} {\Delta t_{ \text{inter}_i }}
94    \Big) ^A  +C  & \text{for} A \neq 0 \\
95    \rho_{\text{flat}_{i+1}}  & \text{for} A = 0 \\
96    \end{cases}
97
98Erf:
99
100.. math::
101    \rho_{{inter}_i} (r) = \begin{cases}
102    B \text{erf} \Big( \frac { A(r - r_{\text{flat}_i})}
103    {\sqrt{2} \Delta t_{ \text{inter}_i }} \Big) +C  & \text{for} A \neq 0 \\
104    B \Big( \frac {(r - r_{\text{flat}_i} )} {\Delta t_{ \text{inter}_i }}
105    \Big)  +C  & \text{for} A = 0 \\
106    \end{cases}
107
108The functions are normalized so that they vary between 0 and 1, and they are
109constrained such that the SLD is continuous at the boundaries of the interface
110as well as each sub-shell. Thus B and C are determined.
111
112Once $\rho_{\text{inter}_i}$ is found at the boundary of the sub-shell of the
113interface, we can find its contribution to the form factor $P(q)$
114
115.. math::
116    f_{\text{inter}_i} = 4 \pi \int_{\Delta t_{ \text{inter}_i } }
117    \rho_{ \text{inter}_i } \frac{\sin(qr)} {qr} r^2 dr =
118    4 \pi \sum_{j=0}^{npts_{\text{inter}_i} -1 }
119    \int_{r_j}^{r_{j+1}} \rho_{ \text{inter}_i } (r_j)
120    \frac{\sin(qr)} {qr} r^2 dr \approx
121
122    4 \pi \sum_{j=0}^{npts_{\text{inter}_i} -1 } \Big[
123    3 ( \rho_{ \text{inter}_i } ( r_{j+1} ) - \rho_{ \text{inter}_i }
124    ( r_{j} ) V ( r_{ \text{subshell}_j } )
125    \Big[ \frac {r_j^2 \beta_\text{out}^2 \sin(\beta_\text{out})
126    - (\beta_\text{out}^2-2) \cos(\beta_\text{out}) }
127    {\beta_\text{out}^4 } \Big]
128
129    - 3 ( \rho_{ \text{inter}_i } ( r_{j+1} ) - \rho_{ \text{inter}_i }
130    ( r_{j} ) V ( r_{ \text{subshell}_j-1 } )
131    \Big[ \frac {r_{j-1}^2 \sin(\beta_\text{in})
132    - (\beta_\text{in}^2-2) \cos(\beta_\text{in}) }
133    {\beta_\text{in}^4 } \Big]
134
135    + 3 \rho_{ \text{inter}_i } ( r_{j+1} )  V ( r_j )
136    \Big[ \frac {\sin(\beta_\text{out}) - \cos(\beta_\text{out}) }
137    {\beta_\text{out}^4 } \Big]
138
139    - 3 \rho_{ \text{inter}_i } ( r_{j} )  V ( r_j )
140    \Big[ \frac {\sin(\beta_\text{in}) - \cos(\beta_\text{in}) }
141    {\beta_\text{in}^4 } \Big]
142    \Big]
143
144where
145
146.. math::
147    V(a) = \frac {4\pi}{3}a^3
148
149    a_\text{in} ~ \frac{r_j}{r_{j+1} -r_j} \text{, } a_\text{out}
150    ~ \frac{r_{j+1}}{r_{j+1} -r_j}
151
152    \beta_\text{in} = qr_j \text{, } \beta_\text{out} = qr_{j+1}
153
154
155We assume $\rho_{\text{inter}_j} (r)$ is approximately linear
156within the sub-shell $j$.
157
158Finally the form factor can be calculated by
159
160.. math::
161
162    P(q) = \frac{[f]^2} {V_\text{particle}} \text{where} V_\text{particle}
163    = V(r_{\text{shell}_N})
164
165For 2D data the scattering intensity is calculated in the same way as 1D,
166where the $q$ vector is defined as
167
168.. math::
169
170    q = \sqrt{q_x^2 + q_y^2}
171
172.. note::
173
174    The outer most radius is used as the effective radius for S(Q)
175    when $P(Q) * S(Q)$ is applied.
176
177References
178----------
179L A Feigin and D I Svergun, Structure Analysis by Small-Angle X-Ray
180and Neutron Scattering, Plenum Press, New York, (1987)
181
182"""
183
184import numpy as np
185from numpy import inf, expm1, sqrt
186from scipy.special import erf
187
188name = "spherical_sld"
189title = "Sperical SLD intensity calculation"
190description = """
191            I(q) =
192               background = Incoherent background [1/cm]
193        """
194category = "shape:sphere"
195
196SHAPES = [["erf(|nu|*z)", "Rpow(z^|nu|)", "Lpow(z^|nu|)",
197           "Rexp(-|nu|z)", "Lexp(-|nu|z)"]]
198
199# pylint: disable=bad-whitespace, line-too-long
200#            ["name", "units", default, [lower, upper], "type", "description"],
201parameters = [["n_shells",             "",           1,      [1, 10],        "volume", "number of shells"],
202              ["sld_solvent",          "1e-6/Ang^2", 1.0,    [-inf, inf],    "sld", "solvent sld"],
203              ["sld[n_shells]",        "1e-6/Ang^2", 4.06,   [-inf, inf],    "sld", "sld of the shell"],
204              ["thickness[n_shells]",  "Ang",        100.0,  [0, inf],       "volume", "thickness shell"],
205              ["interface[n_shells]",  "Ang",        50.0,   [0, inf],       "volume", "thickness of the interface"],
206              ["shape[n_shells]",      "",           0,      SHAPES,         "", "interface shape"],
207              ["nu[n_shells]",         "",           2.5,    [0, inf],       "", "interface shape exponent"],
208              ["n_steps",              "",           35,     [0, inf],       "", "number of steps in each interface (must be an odd integer)"],
209              ]
210# pylint: enable=bad-whitespace, line-too-long
211source = ["lib/polevl.c", "lib/sas_erf.c", "lib/sph_j1c.c", "spherical_sld.c"]
212single = False  # TODO: fix low q behaviour
213
214profile_axes = ['Radius (A)', 'SLD (1e-6/A^2)']
215
216SHAPE_FUNCTIONS = [
217    lambda z, nu: erf(nu/sqrt(2)*(2*z-1))/(2*erf(nu/sqrt(2))) + 0.5,  # erf
218    lambda z, nu: z**nu,                    # Rpow
219    lambda z, nu: 1 - (1-z)**nu,            # Lpow
220    lambda z, nu: expm1(-nu*z)/expm1(-nu),  # Rexp
221    lambda z, nu: expm1(nu*z)/expm1(nu),    # Lexp
222]
223
224def profile(n_shells, sld_solvent, sld, thickness,
225            interface, shape, nu, n_steps):
226    """
227    Returns shape profile with x=radius, y=SLD.
228    """
229
230    z = []
231    rho = []
232    z0 = 0
233    # two sld points for core
234    z.append(0)
235    rho.append(sld[0])
236
237    for i in range(0, int(n_shells)):
238        z0 += thickness[i]
239        z.append(z0)
240        rho.append(sld[i])
241        dz = interface[i]/n_steps
242        sld_l = sld[i]
243        sld_r = sld[i+1] if i < n_shells-1 else sld_solvent
244        fn = SHAPE_FUNCTIONS[int(np.clip(shape[i], 0, len(SHAPE_FUNCTIONS)-1))]
245        for step in range(1, n_steps+1):
246            portion = fn(float(step)/n_steps, max(abs(nu[i]), 1e-14))
247            z0 += dz
248            z.append(z0)
249            rho.append((sld_r - sld_l)*portion + sld_l)
250    z.append(z0*1.2)
251    rho.append(sld_solvent)
252    # return sld profile (r, beta)
253    return np.asarray(z), np.asarray(rho)
254
255
256def ER(n_shells, thickness, interface):
257    n_shells = int(n_shells)
258    total = (np.sum(thickness[:n_shells], axis=1)
259             + np.sum(interface[:n_shells], axis=1))
260    return total
261
262
263demo = {
264    "n_shells": 5,
265    "n_steps": 35.0,
266    "sld_solvent": 1.0,
267    "sld":[2.07,4.0,3.5,4.0,3.5],
268    "thickness":[50.0,100.0,100.0,100.0,100.0],
269    "interface":[50.0,50.0,50.0,50.0],
270    "shape": [0,0,0,0,0],
271    "nu":[2.5,2.5,2.5,2.5,2.5],
272    }
273
274#TODO: Not working yet
275"""
276tests = [
277    # Accuracy tests based on content in test/utest_extra_models.py
278    [{"n_shells": 5,
279        "n_steps": 35,
280        "sld_solvent": 1.0,
281        "sld": [2.07, 4.0, 3.5, 4.0, 3.5],
282        "thickness": [50.0, 100.0, 100.0, 100.0, 100.0],
283        "interface": [50]*5,
284        "shape": [0]*5,
285        "nu": [2.5]*5,
286    }, 0.001, 0.001],
287]
288"""
Note: See TracBrowser for help on using the repository browser.