source: sasmodels/sasmodels/models/capped_cylinder.c @ d1fe925

gh-pages
Last change on this file since d1fe925 was d1fe925, checked in by ajj, 8 years ago

Creating gh_pages branch for docs

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