1 | # cylinder model |
---|
2 | # Note: model title and parameter table are inserted automatically |
---|
3 | r""" |
---|
4 | |
---|
5 | For information about polarised and magnetic scattering, see |
---|
6 | the :ref:`magnetism` documentation. |
---|
7 | |
---|
8 | Definition |
---|
9 | ---------- |
---|
10 | |
---|
11 | The output of the 2D scattering intensity function for oriented cylinders is |
---|
12 | given by (Guinier, 1955) |
---|
13 | |
---|
14 | .. math:: |
---|
15 | |
---|
16 | P(q,\alpha) = \frac{\text{scale}}{V} F^2(q,\alpha).sin(\alpha) + \text{background} |
---|
17 | |
---|
18 | where |
---|
19 | |
---|
20 | .. math:: |
---|
21 | |
---|
22 | F(q,\alpha) = 2 (\Delta \rho) V |
---|
23 | \frac{\sin \left(\tfrac12 qL\cos\alpha \right)} |
---|
24 | {\tfrac12 qL \cos \alpha} |
---|
25 | \frac{J_1 \left(q R \sin \alpha\right)}{q R \sin \alpha} |
---|
26 | |
---|
27 | and $\alpha$ is the angle between the axis of the cylinder and $\vec q$, $V =\pi R^2L$ |
---|
28 | is the volume of the cylinder, $L$ is the length of the cylinder, $R$ is the |
---|
29 | radius of the cylinder, and $\Delta\rho$ (contrast) is the scattering length |
---|
30 | density difference between the scatterer and the solvent. $J_1$ is the |
---|
31 | first order Bessel function. |
---|
32 | |
---|
33 | For randomly oriented particles: |
---|
34 | |
---|
35 | .. math:: |
---|
36 | |
---|
37 | F^2(q)=\int_{0}^{\pi/2}{F^2(q,\alpha)\sin(\alpha)d\alpha}=\int_{0}^{1}{F^2(q,u)du} |
---|
38 | |
---|
39 | |
---|
40 | Numerical integration is simplified by a change of variable to $u = cos(\alpha)$ with |
---|
41 | $sin(\alpha)=\sqrt{1-u^2}$. |
---|
42 | |
---|
43 | The output of the 1D scattering intensity function for randomly oriented |
---|
44 | cylinders is thus given by |
---|
45 | |
---|
46 | .. math:: |
---|
47 | |
---|
48 | P(q) = \frac{\text{scale}}{V} |
---|
49 | \int_0^{\pi/2} F^2(q,\alpha) \sin \alpha\ d\alpha + \text{background} |
---|
50 | |
---|
51 | |
---|
52 | NB: The 2nd virial coefficient of the cylinder is calculated based on the |
---|
53 | radius and length values, and used as the effective radius for $S(q)$ |
---|
54 | when $P(q) \cdot S(q)$ is applied. |
---|
55 | |
---|
56 | For 2d scattering from oriented cylinders, we define the direction of the |
---|
57 | axis of the cylinder using two angles $\theta$ (note this is not the |
---|
58 | same as the scattering angle used in q) and $\phi$. Those angles |
---|
59 | are defined in :numref:`cylinder-angle-definition` , for further details see :ref:`orientation` . |
---|
60 | |
---|
61 | .. _cylinder-angle-definition: |
---|
62 | |
---|
63 | .. figure:: img/cylinder_angle_definition.png |
---|
64 | |
---|
65 | Angles $\theta$ and $\phi$ orient the cylinder relative |
---|
66 | to the beam line coordinates, where the beam is along the $z$ axis. Rotation $\theta$, initially |
---|
67 | in the $xz$ plane, is carried out first, then rotation $\phi$ about the $z$ axis. Orientation distributions |
---|
68 | are described as rotations about two perpendicular axes $\delta_1$ and $\delta_2$ |
---|
69 | in the frame of the cylinder itself, which when $\theta = \phi = 0$ are parallel to the $Y$ and $X$ axes. |
---|
70 | |
---|
71 | .. figure:: img/cylinder_angle_projection.png |
---|
72 | |
---|
73 | Examples for oriented cylinders. |
---|
74 | |
---|
75 | The $\theta$ and $\phi$ parameters to orient the cylinder only appear in the model when fitting 2d data. |
---|
76 | |
---|
77 | Validation |
---|
78 | ---------- |
---|
79 | |
---|
80 | Validation of the code was done by comparing the output of the 1D model |
---|
81 | to the output of the software provided by the NIST (Kline, 2006). |
---|
82 | The implementation of the intensity for fully oriented cylinders was done |
---|
83 | by averaging over a uniform distribution of orientations using |
---|
84 | |
---|
85 | .. math:: |
---|
86 | |
---|
87 | P(q) = \int_0^{\pi/2} d\phi |
---|
88 | \int_0^\pi p(\theta) P_0(q,\theta) \sin \theta\ d\theta |
---|
89 | |
---|
90 | |
---|
91 | where $p(\theta,\phi) = 1$ is the probability distribution for the orientation |
---|
92 | and $P_0(q,\theta)$ is the scattering intensity for the fully oriented |
---|
93 | system, and then comparing to the 1D result. |
---|
94 | |
---|
95 | References |
---|
96 | ---------- |
---|
97 | |
---|
98 | .. [#] J. Pedersen, *Adv. Colloid Interface Sci.*, 70 (1997) 171-210 |
---|
99 | .. [#] G. Fournet, *Bull. Soc. Fr. Mineral. Cristallogr.*, 74 (1951) 39-113 |
---|
100 | .. [#] L. Onsager, *Ann. New York Acad. Sci.*, 51 (1949) 627-659 |
---|
101 | |
---|
102 | Source |
---|
103 | ------ |
---|
104 | |
---|
105 | `cylinder.py <https://github.com/SasView/sasmodels/blob/master/sasmodels/models/cylinder.py>`_ |
---|
106 | |
---|
107 | `cylinder.c <https://github.com/SasView/sasmodels/blob/master/sasmodels/models/cylinder.c>`_ |
---|
108 | |
---|
109 | Authorship and Verification |
---|
110 | ---------------------------- |
---|
111 | |
---|
112 | * **Author:** |
---|
113 | * **Last Modified by:** |
---|
114 | * **Last Reviewed by:** |
---|
115 | * **Source added by :** Steve King **Date:** March 25, 2019 |
---|
116 | """ |
---|
117 | |
---|
118 | import numpy as np # type: ignore |
---|
119 | from numpy import pi, inf # type: ignore |
---|
120 | |
---|
121 | name = "cylinder" |
---|
122 | title = "Right circular cylinder with uniform scattering length density." |
---|
123 | description = """ |
---|
124 | f(q,alpha) = 2*(sld - sld_solvent)*V*sin(qLcos(alpha)/2)) |
---|
125 | /[qLcos(alpha)/2]*J1(qRsin(alpha))/[qRsin(alpha)] |
---|
126 | |
---|
127 | P(q,alpha)= scale/V*f(q,alpha)^(2)+background |
---|
128 | V: Volume of the cylinder |
---|
129 | R: Radius of the cylinder |
---|
130 | L: Length of the cylinder |
---|
131 | J1: The bessel function |
---|
132 | alpha: angle between the axis of the |
---|
133 | cylinder and the q-vector for 1D |
---|
134 | :the ouput is P(q)=scale/V*integral |
---|
135 | from pi/2 to zero of... |
---|
136 | f(q,alpha)^(2)*sin(alpha)*dalpha + background |
---|
137 | """ |
---|
138 | category = "shape:cylinder" |
---|
139 | |
---|
140 | # [ "name", "units", default, [lower, upper], "type", "description"], |
---|
141 | parameters = [["sld", "1e-6/Ang^2", 4, [-inf, inf], "sld", |
---|
142 | "Cylinder scattering length density"], |
---|
143 | ["sld_solvent", "1e-6/Ang^2", 1, [-inf, inf], "sld", |
---|
144 | "Solvent scattering length density"], |
---|
145 | ["radius", "Ang", 20, [0, inf], "volume", |
---|
146 | "Cylinder radius"], |
---|
147 | ["length", "Ang", 400, [0, inf], "volume", |
---|
148 | "Cylinder length"], |
---|
149 | ["theta", "degrees", 60, [-360, 360], "orientation", |
---|
150 | "cylinder axis to beam angle"], |
---|
151 | ["phi", "degrees", 60, [-360, 360], "orientation", |
---|
152 | "rotation about beam"], |
---|
153 | ] |
---|
154 | |
---|
155 | source = ["lib/polevl.c", "lib/sas_J1.c", "lib/gauss76.c", "cylinder.c"] |
---|
156 | have_Fq = True |
---|
157 | radius_effective_modes = [ |
---|
158 | "excluded volume", "equivalent volume sphere", "radius", |
---|
159 | "half length", "half min dimension", "half max dimension", "half diagonal", |
---|
160 | ] |
---|
161 | |
---|
162 | def random(): |
---|
163 | """Return a random parameter set for the model.""" |
---|
164 | volume = 10**np.random.uniform(5, 12) |
---|
165 | length = 10**np.random.uniform(-2, 2)*volume**0.333 |
---|
166 | radius = np.sqrt(volume/length/np.pi) |
---|
167 | pars = dict( |
---|
168 | #scale=1, |
---|
169 | #background=0, |
---|
170 | length=length, |
---|
171 | radius=radius, |
---|
172 | ) |
---|
173 | return pars |
---|
174 | |
---|
175 | |
---|
176 | # parameters for demo |
---|
177 | demo = dict(scale=1, background=0, |
---|
178 | sld=6, sld_solvent=1, |
---|
179 | radius=20, length=300, |
---|
180 | theta=60, phi=60, |
---|
181 | radius_pd=.2, radius_pd_n=9, |
---|
182 | length_pd=.2, length_pd_n=10, |
---|
183 | theta_pd=10, theta_pd_n=5, |
---|
184 | phi_pd=10, phi_pd_n=5) |
---|
185 | |
---|
186 | # Test 1-D and 2-D models |
---|
187 | qx, qy = 0.2 * np.cos(2.5), 0.2 * np.sin(2.5) |
---|
188 | theta, phi = 80.1534480601659, 10.1510817110481 # (10, 10) in sasview 3.x |
---|
189 | tests = [ |
---|
190 | [{}, 0.2, 0.042761386790780453], |
---|
191 | [{}, [0.2], [0.042761386790780453]], |
---|
192 | [{'theta': theta, 'phi': phi}, (qx, qy), 0.03514647218513852], |
---|
193 | [{'theta': theta, 'phi': phi}, [(qx, qy)], [0.03514647218513852]], |
---|
194 | ] |
---|
195 | del qx, qy, theta, phi # not necessary to delete, but cleaner |
---|
196 | |
---|
197 | def _extend_with_reff_tests(radius, length): |
---|
198 | """Test R_eff and form volume calculations""" |
---|
199 | # V and Vr are the same for each R_eff mode |
---|
200 | V = pi*radius**2*length # shell volume = form volume for solid objects |
---|
201 | Vr = 1.0 # form:shell volume ratio |
---|
202 | # Use test value for I(0.2) from above to check Fsq value. Need to |
---|
203 | # remove scale and background before testing. |
---|
204 | q = 0.2 |
---|
205 | scale, background = V, 0.001 |
---|
206 | Fsq = (0.042761386790780453 - background)*scale |
---|
207 | F = None # Need target value for <F> |
---|
208 | # Various values for R_eff, depending on mode |
---|
209 | r_effs = [ |
---|
210 | 0., |
---|
211 | 0.5*(0.75*radius*(2.0*radius*length |
---|
212 | + (radius + length)*(pi*radius + length)))**(1./3.), |
---|
213 | (0.75*radius**2*length)**(1./3.), |
---|
214 | radius, |
---|
215 | length/2., |
---|
216 | min(radius, length/2.), |
---|
217 | max(radius, length/2.), |
---|
218 | np.sqrt(4*radius**2 + length**2)/2., |
---|
219 | ] |
---|
220 | tests.extend([ |
---|
221 | ({'radius_effective_mode': 0}, q, F, Fsq, r_effs[0], V, Vr), |
---|
222 | ({'radius_effective_mode': 1}, q, F, Fsq, r_effs[1], V, Vr), |
---|
223 | ({'radius_effective_mode': 2}, q, F, Fsq, r_effs[2], V, Vr), |
---|
224 | ({'radius_effective_mode': 3}, q, F, Fsq, r_effs[3], V, Vr), |
---|
225 | ({'radius_effective_mode': 4}, q, F, Fsq, r_effs[4], V, Vr), |
---|
226 | ({'radius_effective_mode': 5}, q, F, Fsq, r_effs[5], V, Vr), |
---|
227 | ({'radius_effective_mode': 6}, q, F, Fsq, r_effs[6], V, Vr), |
---|
228 | ({'radius_effective_mode': 7}, q, F, Fsq, r_effs[7], V, Vr), |
---|
229 | ]) |
---|
230 | |
---|
231 | # Test Reff and volume with default model parameters |
---|
232 | _extend_with_reff_tests(parameters[2][2], parameters[3][2]) |
---|
233 | del _extend_with_reff_tests |
---|
234 | |
---|
235 | # ADDED by: RKH ON: 18Mar2016 renamed sld's etc |
---|