source: sasmodels/sasmodels/models/_onion.py @ fdb1487

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

add non-working onion model, named _onion so it is not in the model list

  • Property mode set to 100644
File size: 11.5 KB
Line 
1r"""
2This model provides the form factor, $P(q)$, for a multi-shell sphere where
3the scattering length density (SLD) of the each shell is described by an
4exponential, linear, or constant function. The form factor is normalized by
5the volume of the sphere where the SLD is not identical to the SLD of the
6solvent. We currently provide up to 9 shells with this model.
7
8NB: *radius* represents the core radius $r_0$ and
9*thickness[k]* represents the thickness of the shell, $r_{k+1} - r_k$.
10
11Definition
12----------
13
14The 1D scattering intensity is calculated in the following way
15
16.. math::
17
18    P(q) &= [f]^2 / V_\text{particle}
19
20where
21
22.. math::
23
24    f    &= f_\text{core}
25            + \left(\sum_{\text{shell}=1}^N f_\text{shell}\right)
26            + f_\text{solvent}
27
28
29The shells are spherically symmetric with particle density $\rho(r)$ and
30constant SLD within the core and solvent, so
31
32.. math::
33
34    f_\text{core}
35        &= 4\pi\int_0^{r_\text{core}} \rho_\text{core}
36            \frac{\sin(qr)}{qr}\, r^2\,\mathrm{d}r
37        &= 3\rho_\text{core} V(r_\text{core})
38            \frac{j_1(qr_\text{core})}{qr_\text{core}} \\
39    f_\text{shell}
40        &= 4\pi\int_{r_{\text{shell}-1}}^{r_\text{shell}}
41            \rho_\text{shell}(r)\frac{\sin(qr)}{qr}\,r^2\,\mathrm{d}r \\
42    f_\text{solvent}
43        &= 4\pi\int_{r_N}^\infty
44            \rho_\text{solvent}\frac{\sin(qr)}{qr}\,r^2\,\mathrm{d}r
45        &= -3\rho_\text{solvent}V(r_N)\frac{j_1(q r_N)}{q r_N}
46
47where the spherical bessel function $j_1$ is
48
49.. math::
50
51    j_1(x) = \frac{\sin(x)}{x^2} - \frac{\cos(x)}{x}
52
53and the volume is $V(r) = \frac{4\pi}{3}r^3$. The volume of the particle
54is determined by the radius of the outer shell, so $V_\text{particle} = V(r_N)$.
55
56Now lets consider the SLD of a shell defined by
57
58.. math::
59
60    \rho_\text{shell}(r) = \begin{cases}
61        B\exp\left(A(r-r_{\text{shell}-1})/\Delta t_\text{shell}\right)
62            + C & \mbox{for } A \neq 0 \\
63        \rho_\text{in} = \text{constant} & \mbox{for } A = 0
64    \end{cases}
65
66An example of a possible SLD profile is shown below where
67$\rho_\text{in}$ and $\Delta t_\text{shell}$ stand for the
68SLD of the inner side of the $k^\text{th}$ shell and the
69thickness of the $k^\text{th}$ shell in the equation above, respectively.
70
71For $A \gt 0$,
72
73.. math::
74
75    f_\text{shell} &= 4 \pi \int_{r_{\text{shell}-1}}^{r_\text{shell}}
76        \left[ B\exp
77            \left(A (r - r_{\text{shell}-1}) / \Delta t_\text{shell} \right) + C
78        \right] \frac{\sin(qr)}{qr}\,r^2\,\mathrm{d}r \\
79    &= 3BV(r_\text{shell}) e^A h(\alpha_\text{out},\beta_\text{out})
80        - 3BV(r_{\text{shell}-1}) h(\alpha_\text{in},\beta_\text{in})
81        + 3CV(r_{\text{shell}}) \frac{j_1(\beta_\text{out})}{\beta_\text{out}}
82        - 3CV(r_{\text{shell}-1}) \frac{j_1(\beta_\text{in})}{\beta_\text{in}}
83
84for
85
86.. math::
87    :nowrap:
88
89    \begin{align*}
90    B&=\frac{\rho_\text{out} - \rho_\text{in}}{e^A-1}
91         &C &= \frac{\rho_\text{in}e^A - \rho_\text{out}}{e^A-1} \\
92    \alpha_\text{in} &= A\frac{r_{\text{shell}-1}}{\Delta t_\text{shell}}
93         &\alpha_\text{out} &= A\frac{r_\text{shell}}{\Delta t_\text{shell}} \\
94    \beta_\text{in} &= qr_{\text{shell}-1}
95        &\beta_\text{out} &= qr_\text{shell}
96    \end{align*}
97
98where $h$ is
99
100 .. math::
101
102    h(x,y) = \frac{x \sin(y) - y\cos(y)}{(x^2+y^2)y}
103               - \frac{(x^2-y^2)\sin(y) - 2xy\cos(y)}{(x^2+y^2)^2y}
104
105
106For $A \sim 0$, e.g., $A = -0.0001$, this function converges to that of the
107linear SLD profile with
108$\rho_\text{shell}(r) \approx A(r-r_{\text{shell}-1})/\Delta t_\text{shell})+B$,
109so this case is equivalent to
110
111.. math::
112
113
114    f_\text{shell}
115    &=
116      3 V(r_\text{shell}) \frac{\Delta\rho_\text{shell}}{\Delta t_\text{shell}}
117        \left[\frac{
118                2 \cos(qr_\text{out})
119                    + qr_\text{out} \sin(qr_\text{out})
120            }{
121                (qr_\text{out})^4
122            }\right] \\
123     &{}
124      -3 V(r_\text{shell}) \frac{\Delta\rho_\text{shell}}{\Delta t_\text{shell}}
125        \left[\frac{
126                    2\cos(qr_\text{in})
127                +qr_\text{in}\sin(qr_\text{in})
128            }{
129                (qr_\text{in})^4
130            }\right] \\
131    &{}
132      +3\rho_\text{out}V(r_\text{shell}) \frac{j_1(qr_\text{out})}{qr_\text{out}}
133      -3\rho_\text{in}V(r_{\text{shell}-1}) \frac{j_1(qr_\text{in})}{qr_\text{in}}
134
135For $A = 0$, the exponential function has no dependence on the radius (so that
136$\rho_\text{out}$ is ignored this case) and becomes flat. We set the constant
137to $\rho_\text{in}$ for convenience, and thus the form factor contributed by
138the shells is
139
140.. math::
141
142    f_\text{shell} =
143        3\rho_\text{in}V(r_\text{shell})
144           \frac{j_1(qr_\text{out})}{qr_\text{out}}
145        - 3\rho_\text{in}V(r_{\text{shell}-1})
146            \frac{j_1(qr_\text{in})}{qr_\text{in}}
147
148.. figure:: img/onion_annotated_profile.gif
149
150    Example of an onion model profile.
151
152The 2D scattering intensity is the same as $P(q)$ above, regardless of the
153orientation of the $q$ vector which is defined as
154
155.. math::
156
157    q = \sqrt{q_x^2 + q_y^2}
158
159NB: The outer most radius is used as the effective radius for $S(q)$
160when $P(q) S(q)$ is applied.
161
162.. figure:: img/onion_1d.jpg
163
164    1D plot using the default values (w/400 point)
165
166.. figure:: img/onion_profile.jpg
167
168    SLD profile from the default values.
169
170References
171----------
172
173L A Feigin and D I Svergun,
174*Structure Analysis by Small-Angle X-Ray and Neutron Scattering*,
175Plenum Press, New York, 1987.
176"""
177
178#
179# Give a polynomial $\rho(r) = Ar^3 + Br^2 + Cr + D$ for density,
180#
181# .. math::
182#
183#    f = 4 \pi \int_a^b \rho(r) \sin(qr)/(qr) \mathrm{d}r  = h(b) - h(a)
184#
185# where
186#
187# .. math::
188#
189#    h(r) = \frac{4 \pi}{q^6}\left[
190#        (q^3(4Ar^3 + 3Br^2 + 2Cr + D) - q(24Ar + 6B)) \sin(qr)
191#      - (q^4(Ar^4 + Br^3 + Cr^2 + Dr) - q^2(12Ar^2 + 6Br + 2C) + 24A) \cos(qr)
192#    \right]
193#
194# Use the monotonic spline to get the polynomial coefficients for each shell.
195#
196# Order 0
197#
198# .. math::
199#
200#    h(r) = \frac{4 \pi}{q^3} \left[
201#       - \cos(qr) (Ar) q
202#       + \sin(qr) (A)
203#    \right]
204#
205# Order 1
206#
207# .. math::
208#
209#   h(r) = \frac{4 \pi}{q^4} \left[
210#       - \cos(qr) ( Ar^2 + Br) q^2
211#       + \sin(qr) ( Ar   + B ) q
212#       + \cos(qr) (2A        )
213#   \right]
214#
215# Order 2
216#
217# .. math::
218#  h(r) = \frac{4 \pi}{q^5} \left[
219#        - \cos(qr) ( Ar^3 +  Br^2 + Cr) q^3
220#        + \sin(qr) (3Ar^2 + 2Br   + C ) q^2
221#        + \cos(qr) (6Ar   + 2B        ) q
222#        - \sin(qr) (6A                )
223#
224# Order 3
225#
226#    h(r) = \frac{4 \pi}{q^6}\left[
227#      - \cos(qr) (  Ar^4 +  Br^3 +  Cr^2 + Dr) q^4
228#      + \sin(qr) ( 4Ar^3 + 3Br^2 + 2Cr   + D ) q^3
229#      + \cos(qr) (12Ar^2 + 6Br   + 2C        ) q^2
230#      - \sin(qr) (24Ar   + 6B                ) q
231#      - \cos(qr) (24A                        )
232#    \right]
233#
234# Order p
235#
236#    h(r) = \frac{4 \pi}{q^{2}}
237#      \sum_{k=0}^p -\frac{d^k\cos(qr)}{dr^k} \frac{d^k r\rho(r)}{dr^k} (qr)^{-k}
238#
239# Given the equation
240#
241#    f = sum_(k=0)^(n-1) h_k(r_(k+1)) - h_k(r_k)
242#
243# we can rearrange the terms so that
244#
245#    f = sum_0^(n-1) h_k(r_(k+1)) - sum_0^(n-1) h_k(r_k)
246#      = sum_1^n h_(k-1)(r_k) - sum_0^(n-1) h_k(r_k)
247#      = h_(n-1)(r_n) - h_0(r_0) + sum_1^(n-1) [h_(k-1)(r_k) - h_k(r_k)]
248#      = h_(n-1)(r_n) - h_0(r_0) - sum_1^(n-1) h_(Delta k)(r_k)
249#
250# where
251#
252#    h_(Delta k)(r) = h(Delta rho_k, r)
253#
254# for
255#
256#    Delta rho_k = (A_k-A_(k-1)) r^p + (B_k-B_(k-1)) r^(p-1) + ...
257#
258# Using l'H\^opital's Rule 6 times on the order 3 polynomial,
259#
260#   lim_(q->0) h(r) = (140D r^3 + 180C r^4 + 144B r^5 + 120A r^6)/720
261#
262
263
264from __future__ import division
265
266import numpy as np
267from numpy import inf, nan
268from math import fabs, exp, expm1
269
270name = "onion"
271title = "Onion shell model with constant, linear or exponential density"
272
273description = """\
274Form factor of mutishells normalized by the volume. Here each shell is
275described by an exponential function;
276
277        I) For A_shell != 0,
278                f(r) = B*exp(A_shell*(r-r_in)/thick_shell)+C
279        where
280                B=(sld_out-sld_in)/(exp(A_shell)-1)
281                C=sld_in-B.
282        Note that in the above case, the function becomes a linear function
283        as A_shell --> 0+ or 0-.
284
285        II) For the exact point of A_shell == 0,
286                f(r) = sld_in ,i.e., it crosses over flat function
287        Note that the 'sld_out' becaomes NULL in this case.
288
289        background:background,
290        rad_core0: radius of sphere(core)
291        thick_shell#:the thickness of the shell#
292        sld_core0: the SLD of the sphere
293        sld_solv: the SLD of the solvent
294        sld_shell: the SLD of the shell#
295        A_shell#: the coefficient in the exponential function
296"""
297
298category = "shape:sphere"
299
300
301#             ["name", "units", default, [lower, upper], "type","description"],
302parameters = [["core_sld", "1e-6/Ang^2", 1.0, [-inf, inf], "",
303               "Core scattering length density"],
304              ["core_radius", "Ang", 200., [0, inf], "",
305               "Radius of the core"],
306              ["solvent_sld", "1e-6/Ang^2", 6.4, [-inf, inf], "",
307               "Solvent scattering length density"],
308              ["n", "", 1, [0, 10], "volume",
309               "number of shells"],
310              ["in_sld[n]", "1e-6/Ang^2", 1.7, [-inf, inf], "",
311               "scattering length density at the inner radius of shell k"],
312              ["out_sld[n]", "1e-6/Ang^2", 2.0, [-inf, inf], "",
313               "scattering length density at the outer radius of shell k"],
314              ["thickness[n]", "Ang", 40., [0, inf], "volume",
315               "Thickness of shell k"],
316              ["A[n]", "", 1.0, [-inf, inf], "",
317               "Decay rate of shell k"],
318              ]
319
320source = ["lib/sph_j1c.c", "onion.c"]
321
322
323def profile(core_sld, core_radius, solvent_sld, n, in_sld, out_sld, thickness, A):
324    """
325    Get SLD profile
326
327    Returns *(r, rho(r))* where *r* is the radius (Ang) and *rho(r)* is the
328    SLD (1/Ang^2).
329    """
330
331    total_radius = 1.25*(sum(thickness[:n]) + core_radius + 1)
332    dr = total_radius/400  # 400 points for a smooth plot
333
334    r = []
335    beta = []
336
337    # add in the core
338    r.append(0)
339    beta.append(core_sld)
340    r.append(core_radius)
341    beta.append(core_sld)
342
343    # add in the shells
344    for k in range(n):
345        # Left side of each shells
346        r0 = r[-1]
347        r.append(r0)
348        beta.append(in_sld[k])
349
350        if fabs(A[k]) < 1.0e-16:
351            # flat shell
352            r.append(r0 + thickness[k])
353            beta.append(out_sld[k])
354        else:
355            # exponential shell
356            # num_steps must be at least 1, so use floor()+1 rather than ceil
357            # to protect against a thickness0.
358            num_steps = np.floor(thickness[k]/dr) + 1
359            slope = (out_sld[k] - in_sld[k])/expm1(A[k])
360            const = (in_sld[k] - slope)
361            for rk in np.linspace(0, thickness[k], num_steps+1):
362                r.append(r0+rk)
363                beta.append(slope*exp(A[k]*rk/thickness[k]) + const)
364
365    # add in the solvent
366    r.append(r[-1])
367    beta.append(solvent_sld)
368    r.append(total_radius)
369    beta.append(solvent_sld)
370
371    return np.asarray(r), np.asarray(beta)
372
373def ER(core_radius, n, thickness):
374    return np.sum(thickness[:n], axis=0) + core_radius
375
376def VR(core_radius, n, thickness):
377    return 1.0
378
379demo = {
380    "solvent_sld": 2.2,
381    "core_sld": 1.0,
382    "core_radius": 100,
383    "n": 4,
384    "in_sld": [0.5, 1.5, 0.9, 2.0],
385    "out_sld": [nan, 0.9, 1.2, 1.6],
386    "thickness": [50, 75, 150, 75],
387    "A": [0, -1, 1e-4, 1],
388    # Could also specify them individually as
389    # "A[1]": 0, "A[2]": -1, "A[3]": 1e-4, "A[4]": 1,
390    }
391
392oldname = "OnionExpShellModel"
393oldpars = dict(
394    core_sld="sld_core0",
395    core_radius="rad_core0",
396    solvent_sld="sld_solv",
397    n="n_shells",
398    in_sld="sld_in_shell",
399    out_sld="sld_out_shell",
400    A="A_shell",
401    thickness="thick_shell",
402    # func_shell is always 2 in the user interface, so isn't included
403    )
Note: See TracBrowser for help on using the repository browser.