source: sasmodels/sasmodels/models/capped_cylinder.c @ 26141cb

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 26141cb was 26141cb, checked in by wojciech, 8 years ago

A few extra models fixed

  • Property mode set to 100644
File size: 5.4 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.
15static double
16_cap_kernel(double q, double h, double cap_radius,
17                      double half_length, double sin_alpha, double cos_alpha)
18{
19    // translate a point in [-1,1] to a point in [lower,upper]
20    const double upper = 1.0;
21    const double lower = h/cap_radius; // integral lower bound
22    const double zm = 0.5*(upper-lower);
23    const double zb = 0.5*(upper+lower);
24
25    // cos term in integral is:
26    //    cos (q (R t - h + L/2) cos(alpha))
27    // so turn it into:
28    //    cos (m t + b)
29    // where:
30    //    m = q R cos(alpha)
31    //    b = q(L/2-h) cos(alpha)
32    const double m = q*cap_radius*cos_alpha; // cos argument slope
33    const double b = q*(half_length-h)*cos_alpha; // cos argument intercept
34    const double qrst = q*cap_radius*sin_alpha; // Q*R*sin(theta)
35    double total = 0.0;
36    for (int i=0; i<76 ;i++) {
37        const double t = Gauss76Z[i]*zm + zb;
38        const double radical = 1.0 - t*t;
39        const double bj = sas_J1c(qrst*sqrt(radical));
40        const double Fq = cos(m*t + b) * radical * bj;
41        total += Gauss76Wt[i] * Fq;
42    }
43    // translate dx in [-1,1] to dx in [lower,upper]
44    const double integral = total*zm;
45    const double cap_Fq = 2*M_PI*cube(cap_radius)*integral;
46    return cap_Fq;
47}
48
49double form_volume(double radius, double cap_radius, double length)
50{
51    // cap radius should never be less than radius when this is called
52
53    // Note: volume V = 2*V_cap + V_cyl
54    //
55    // V_cyl = pi r_cyl^2 L
56    // 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)
57    //
58    // The docs for capped cylinder give the volume as:
59    //    V = pi r^2 L + 2/3 pi (R-h)^2 (2R + h)
60    // where r_cap=R and h = R - h_c.
61    //
62    // The first part is clearly V_cyl.  The second part requires some work:
63    //    (R-h)^2 => h_c^2
64    //    (2R+h) => 2R+ h_c-h_c + h => 2R + (R-h)-h_c + h => 3R-h_c
65    // And so:
66    //    2/3 pi (R-h)^2 (2R + h) => 2/3 pi h_c^2 (3 r_cap - h_c)
67    // which is 2 V_cap, using the second form above.
68    //
69    // In this function we are going to use the first form of V_cap
70    //
71    //      V = V_cyl + 2 V_cap
72    //        = pi r^2 L + pi hc (r^2 + hc^2/3)
73    //        = pi (r^2 (L+hc) + hc^3/3)
74    const double hc = cap_radius - sqrt(cap_radius*cap_radius - radius*radius);
75    return M_PI*(radius*radius*(length+hc) + hc*hc*hc/3.0);
76}
77
78double Iq(double q, double sld, double solvent_sld,
79          double radius, double cap_radius, double length)
80{
81    // Exclude invalid inputs.
82    if (cap_radius < radius) return NAN;
83    const double h = sqrt(cap_radius*cap_radius - radius*radius);
84    const double half_length = 0.5*length;
85
86    // translate a point in [-1,1] to a point in [0, pi/2]
87    const double zm = M_PI_4;
88    const double zb = M_PI_4;
89    double total = 0.0;
90    for (int i=0; i<76 ;i++) {
91        const double alpha= Gauss76Z[i]*zm + zb;
92        double sin_alpha, cos_alpha; // slots to hold sincos function output
93        SINCOS(alpha, sin_alpha, cos_alpha);
94
95        const double cap_Fq = _cap_kernel(q, h, cap_radius, half_length, sin_alpha, cos_alpha);
96        const double bj = sas_J1c(q*radius*sin_alpha);
97        const double si = sinc(q*half_length*cos_alpha);
98        const double cyl_Fq = M_PI*radius*radius*length*bj*si;
99        const double Aq = cap_Fq + cyl_Fq;
100        total += Gauss76Wt[i] * Aq * Aq * sin_alpha; // sin_alpha for spherical coord integration
101    }
102    // translate dx in [-1,1] to dx in [lower,upper]
103    const double form = total * zm;
104
105    // Contrast
106    const double s = (sld - solvent_sld);
107    return 1.0e-4 * s * s * form;
108}
109
110
111double Iqxy(double qx, double qy,
112    double sld, double solvent_sld, double radius,
113    double cap_radius, double length,
114    double theta, double phi)
115{
116    // Compute angle alpha between q and the cylinder axis
117    double sn, cn;
118    SINCOS(theta*M_PI_180, sn, cn);
119    const double q = sqrt(qx*qx+qy*qy);
120    const double cos_val = cn*cos(phi*M_PI_180)*(qx/q) + sn*(qy/q);
121    const double alpha = acos(cos_val); // rod angle relative to q
122
123    // Exclude invalid inputs.
124    if (cap_radius < radius) return NAN;
125    const double h = sqrt(cap_radius*cap_radius - radius*radius);
126    const double half_length = 0.5*length;
127
128    double sin_alpha, cos_alpha; // slots to hold sincos function output
129    SINCOS(alpha, sin_alpha, cos_alpha);
130    const double cap_Fq = _cap_kernel(q, h, cap_radius, half_length, sin_alpha, cos_alpha);
131    const double bj = sas_J1c(q*radius*sin_alpha);
132    const double si = sinc(q*half_length*cos_alpha);
133    const double cyl_Fq = M_PI*radius*radius*length*bj*si;
134    const double Aq = cap_Fq + cyl_Fq;
135
136    // Multiply by contrast^2 and convert to cm-1
137    const double s = (sld - solvent_sld);
138    return 1.0e-4 * square(s * Aq);
139}
Note: See TracBrowser for help on using the repository browser.