source: sasmodels/sasmodels/models/capped_cylinder.c @ 139c528

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

slightly optimized capped cylinder

  • Property mode set to 100644
File size: 6.8 KB
Line 
1double form_volume(double radius, double cap_radius, double length);
2double Iq(double q, double sld, double solvent_sld,
3    double radius, double cap_radius, double length);
4double Iqxy(double qx, double qy, double sld, double solvent_sld,
5    double radius, double cap_radius, double length, double theta, double phi);
6
7// Integral over a convex lens kernel for t in [h/R,1].  See the docs for
8// the definition of the function being integrated.
9//   q is the magnitude of the q vector.
10//   h is the length of the lens "inside" the cylinder.  This negative wrt the
11//       definition of h in the docs.
12//   cap_radius is the radius of the lens
13//   length is the cylinder length, or the separation between the lens halves
14//   alpha is the angle of the cylinder wrt q.
15double _cap_kernel(double q, double h, double cap_radius, double length,
16                 double sin_alpha, double cos_alpha);
17double _cap_kernel(double q, double h, double cap_radius, double length,
18                 double sin_alpha, double cos_alpha)
19{
20    // For speed, we are pre-calculating terms which are constant over the
21    // kernel.
22    const double upper = 1.0;
23    const double lower = h/cap_radius; // integral lower bound
24    const double zm = 0.5*(upper-lower);
25    const double zb = 0.5*(upper+lower);
26    // cos term in integral is:
27    //    cos (q (R t - h + L/2) cos(alpha))
28    // so turn it into:
29    //    cos (m t + b)
30    // where:
31    //    m = q R cos(alpha)
32    //    b = q(L/2-h) cos(alpha)
33    const double m = q*cap_radius*cos_alpha; // cos argument slope
34    const double b = q*(0.5*length-h)*cos_alpha; // cos argument intercept
35    const double qrst = q*cap_radius*sin_alpha; // Q*R*sin(theta)
36    double total = 0.0;
37    for (int i=0; i<76 ;i++) {
38        // translate a point in [-1,1] to a point in [lower,upper]
39        //const double t = ( Gauss76Z[i]*(upper-lower) + upper + lower )/2.0;
40        const double t = Gauss76Z[i]*zm + zb;
41        const double radical = 1.0 - t*t;
42        const double arg = qrst*sqrt(radical); // cap bessel function arg
43        const double be = (arg == 0.0 ? 0.5 : J1(arg)/arg);
44        const double Fq = cos(m*t + b) * radical * be;
45        total += Gauss76Wt[i] * Fq;
46    }
47    // translate dx in [-1,1] to dx in [lower,upper]
48    //const double form = (upper-lower)/2.0*total;
49    const double integral = 0.5*(upper-lower)*total;
50    return 4.0*M_PI*cap_radius*cap_radius*cap_radius*integral;
51}
52
53double form_volume(double radius, double cap_radius, double length)
54{
55    // cap radius should never be less than radius when this is called
56
57    // Note: volume V = 2*V_cap + V_cyl
58    //
59    // V_cyl = pi r_cyl^2 L
60    // V_cap = 1/6 pi h_c (3 r_cyl^2 + h_c^2) = 1/3 pi h_c^2 (3 r_cap - h_c)
61    //
62    // The docs for capped cylinder give the volume as:
63    //    V = pi r^2 L + 2/3 pi (R-h)^2 (2R + h)
64    // where r_cap=R and h = R - h_c.
65    //
66    // The first part is clearly V_cyl.  The second part requires some work:
67    //    (R-h)^2 => h_c^2
68    //    (2R+h) => 2R+ h_c-h_c + h => 2R + (R-h)-hc + h => 3R-h_c
69    // And so:
70    //    2/3 pi (R-h)^2 (2R + h) => 2/3 pi h_c^2 (3 r_cap - h_c)
71    // which is 2 V_cap, using the second form above.
72    //
73    // In this function we are going to use the first form of V_cap
74    //
75    //      V = V_cyl + 2 V_cap
76    //        = pi r^2 L + pi hc (r^2 + hc^2/3)
77    //        = pi (r^2 (L+hc) + hc^3/3)
78    const double hc = cap_radius - sqrt(cap_radius*cap_radius - radius*radius);
79    return M_PI*(radius*radius*(length+hc) + 0.333333333333333*hc*hc*hc);
80}
81
82double Iq(double q,
83    double sld,
84    double solvent_sld,
85    double radius,
86    double cap_radius,
87    double length)
88{
89    double sn, cn; // slots to hold sincos function output
90
91    // Exclude invalid inputs.
92    if (cap_radius < radius) return NAN;
93
94    const double lower = 0.0;
95    const double upper = M_PI_2;
96    const double h = sqrt(cap_radius*cap_radius - radius*radius); // negative h
97    double total = 0.0;
98    for (int i=0; i<76 ;i++) {
99        // translate a point in [-1,1] to a point in [lower,upper]
100        const double alpha= 0.5*(Gauss76Z[i]*(upper-lower) + upper + lower);
101        SINCOS(alpha, sn, cn);
102
103        const double cap_Fq = _cap_kernel(q, h, cap_radius, length, sn, cn);
104
105        // The following is CylKernel() / sin(alpha), but we are doing it in place
106        // to avoid sin(alpha)/sin(alpha) for alpha = 0.  It is also a teensy bit
107        // faster since we don't multiply and divide sin(alpha).
108        const double besarg = q*radius*sn;
109        const double siarg = q*0.5*length*cn;
110        // lim_{x->0} J1(x)/x = 1/2,   lim_{x->0} sin(x)/x = 1
111        const double bj = (besarg == 0.0 ? 0.5 : J1(besarg)/besarg);
112        const double si = (siarg == 0.0 ? 1.0 : sin(siarg)/siarg);
113        const double cyl_Fq = M_PI*radius*radius*length*2.0*bj*si;
114
115        // Volume weighted average F(q)
116        const double Aq = cyl_Fq + cap_Fq;
117        total += Gauss76Wt[i] * Aq * Aq * sn; // sn for spherical coord integration
118    }
119    // translate dx in [-1,1] to dx in [lower,upper]
120    const double form = total * 0.5*(upper-lower);
121
122    // Multiply by contrast^2, normalize by cylinder volume and convert to cm-1
123    // NOTE that for this (Fournet) definition of the integral, one must MULTIPLY by Vcyl
124    // The additional volume factor is for polydisperse volume normalization.
125    const double s = (sld - solvent_sld);
126    return 1.0e-4 * form * s * s; // form_volume(radius, cap_radius, length);
127}
128
129
130double Iqxy(double qx, double qy,
131    double sld,
132    double solvent_sld,
133    double radius,
134    double cap_radius,
135    double length,
136    double theta,
137    double phi)
138{
139    double sn, cn; // slots to hold sincos function output
140
141    // Exclude invalid inputs.
142    if (cap_radius < radius) return NAN;
143
144    // Compute angle alpha between q and the cylinder axis
145    SINCOS(theta*M_PI_180, sn, cn);
146    // # The following correction factor exists in sasview, but it can't be
147    // # right, so we are leaving it out for now.
148    const double q = sqrt(qx*qx+qy*qy);
149    const double cos_val = cn*cos(phi*M_PI_180)*(qx/q) + sn*(qy/q);
150    const double alpha = acos(cos_val); // rod angle relative to q
151    SINCOS(alpha, sn, cn);
152
153    const double h = sqrt(cap_radius*cap_radius - radius*radius); // negative h
154    const double cap_Fq = _cap_kernel(q, h, cap_radius, length, sn, cn);
155
156    const double besarg = q*radius*sn;
157    const double siarg = q*0.5*length*cn;
158    // lim_{x->0} J1(x)/x = 1/2,   lim_{x->0} sin(x)/x = 1
159    const double bj = (besarg == 0.0 ? 0.5 : J1(besarg)/besarg);
160    const double si = (siarg == 0.0 ? 1.0 : sin(siarg)/siarg);
161    const double cyl_Fq = M_PI*radius*radius*length*2.0*bj*si;
162
163    // Volume weighted average F(q)
164    const double Aq = cyl_Fq + cap_Fq;
165
166    // Multiply by contrast^2, normalize by cylinder volume and convert to cm-1
167    const double s = (sld - solvent_sld);
168    return 1.0e-4 * Aq * Aq * s * s; // form_volume(radius, cap_radius, length);
169}
Note: See TracBrowser for help on using the repository browser.