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

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

resolve remaining differences between sasview and sasmodels

  • Property mode set to 100644
File size: 6.3 KB
Line 
1real form_volume(real radius, real cap_radius, real length);
2real Iq(real q, real sld, real solvent_sld,
3    real radius, real cap_radius, real length);
4real Iqxy(real qx, real qy, real sld, real solvent_sld,
5    real radius, real cap_radius, real length, real theta, real 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.
15real _cap_kernel(real q, real h, real cap_radius, real length,
16                 real sin_alpha, real cos_alpha);
17real _cap_kernel(real q, real h, real cap_radius, real length,
18                 real sin_alpha, real cos_alpha)
19{
20    // For speed, we are pre-calculating terms which are constant over the
21    // kernel.
22    const real upper = REAL(1.0);
23    const real 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 real m = q*cap_radius*cos_alpha; // cos argument slope
32    const real b = q*(REAL(0.5)*length-h)*cos_alpha; // cos argument intercept
33    const real qrst = q*cap_radius*sin_alpha; // Q*R*sin(theta)
34    real total = REAL(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 real t = ( Gauss76Z[i]*(upper-lower) + upper + lower )/2.0;
38        const real t = REAL(0.5)*(Gauss76Z[i]*(upper-lower)+upper+lower);
39        const real radical = REAL(1.0) - t*t;
40        const real arg = qrst*sqrt(radical); // cap bessel function arg
41        const real be = (arg == REAL(0.0) ? REAL(0.5) : J1(arg)/arg);
42        const real 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 real form = (upper-lower)/2.0*total;
47    const real integral = REAL(0.5)*(upper-lower)*total;
48    return REAL(4.0)*M_PI*cap_radius*cap_radius*cap_radius*integral;
49}
50
51real form_volume(real radius, real cap_radius, real length)
52{
53    // cap radius should never be less than radius when this is called
54    // Note: cap volume = pi hc/6 * (3 a^2 + hc^2), where a is the cylinder
55    // radius and hc is the height of the cap.  Multiply by two for both ends.
56    // So:
57    //      cap V = pi hc (r^2 + hc^2/3)
58    //      cylinder V = pi r^2 L
59    //      V = cylinder V + cap V
60    //        = pi r^2 L + pi hc (r^2 + hc^2/3)
61    //        = pi * (r^2 (L+hc) + hc^3/3)
62    const real hc = cap_radius - sqrt(cap_radius*cap_radius - radius*radius);
63    return M_PI*(radius*radius*(length+hc) + REAL(0.333333333333333)*hc*hc*hc);
64}
65
66real Iq(real q,
67    real sld,
68    real solvent_sld,
69    real radius,
70    real cap_radius,
71    real length)
72{
73    real sn, cn; // slots to hold sincos function output
74
75    // Exclude invalid inputs.
76    if (cap_radius < radius) return REAL(-1.0);
77
78    const real lower = REAL(0.0);
79    const real upper = M_PI_2;
80    const real h = sqrt(cap_radius*cap_radius - radius*radius); // negative h
81    real total = REAL(0.0);
82    for (int i=0; i<76 ;i++) {
83        // translate a point in [-1,1] to a point in [lower,upper]
84        const real alpha= REAL(0.5)*(Gauss76Z[i]*(upper-lower) + upper + lower);
85        SINCOS(alpha, sn, cn);
86
87        const real cap_Fq = _cap_kernel(q, h, cap_radius, length, sn, cn);
88
89        // The following is CylKernel() / sin(alpha), but we are doing it in place
90        // to avoid sin(alpha)/sin(alpha) for alpha = 0.  It is also a teensy bit
91        // faster since we don't multiply and divide sin(alpha).
92        const real besarg = q*radius*sn;
93        const real siarg = q*REAL(0.5)*length*cn;
94        // lim_{x->0} J1(x)/x = 1/2,   lim_{x->0} sin(x)/x = 1
95        const real bj = (besarg == REAL(0.0) ? REAL(0.5) : J1(besarg)/besarg);
96        const real si = (siarg == REAL(0.0) ? REAL(1.0) : sin(siarg)/siarg);
97        const real cyl_Fq = M_PI*radius*radius*length*REAL(2.0)*bj*si;
98
99        // Volume weighted average F(q)
100        const real Aq = cyl_Fq + cap_Fq;
101        total += Gauss76Wt[i] * Aq * Aq * sn; // sn for spherical coord integration
102    }
103    // translate dx in [-1,1] to dx in [lower,upper]
104    const real form = total * REAL(0.5)*(upper-lower);
105
106    // Multiply by contrast^2, normalize by cylinder volume and convert to cm-1
107    // NOTE that for this (Fournet) definition of the integral, one must MULTIPLY by Vcyl
108    // The additional volume factor is for polydisperse volume normalization.
109    const real s = (sld - solvent_sld);
110    return REAL(1.0e-4) * form * s * s; // form_volume(radius, cap_radius, length);
111}
112
113
114real Iqxy(real qx, real qy,
115    real sld,
116    real solvent_sld,
117    real radius,
118    real cap_radius,
119    real length,
120    real theta,
121    real phi)
122{
123    real sn, cn; // slots to hold sincos function output
124
125    // Exclude invalid inputs.
126    if (cap_radius < radius) return REAL(-1.0);
127
128    // Compute angle alpha between q and the cylinder axis
129    SINCOS(theta*M_PI_180, sn, cn);
130    // # The following correction factor exists in sasview, but it can't be
131    // # right, so we are leaving it out for now.
132    const real q = sqrt(qx*qx+qy*qy);
133    const real cos_val = cn*cos(phi*M_PI_180)*(qx/q) + sn*(qy/q);
134    const real alpha = acos(cos_val); // rod angle relative to q
135    SINCOS(alpha, sn, cn);
136
137    const real h = sqrt(cap_radius*cap_radius - radius*radius); // negative h
138    const real cap_Fq = _cap_kernel(q, h, cap_radius, length, sn, cn);
139
140    const real besarg = q*radius*sn;
141    const real siarg = q*REAL(0.5)*length*cn;
142    // lim_{x->0} J1(x)/x = 1/2,   lim_{x->0} sin(x)/x = 1
143    const real bj = (besarg == REAL(0.0) ? REAL(0.5) : J1(besarg)/besarg);
144    const real si = (siarg == REAL(0.0) ? REAL(1.0) : sin(siarg)/siarg);
145    const real cyl_Fq = M_PI*radius*radius*length*REAL(2.0)*bj*si;
146
147    // Volume weighted average F(q)
148    const real Aq = cyl_Fq + cap_Fq;
149
150    // Multiply by contrast^2, normalize by cylinder volume and convert to cm-1
151    const real s = (sld - solvent_sld);
152    return REAL(1.0e-4) * Aq * Aq * s * s; // form_volume(radius, cap_radius, length);
153}
Note: See TracBrowser for help on using the repository browser.