source: sasmodels/sasmodels/models/capped_cylinder.c @ 994d77f

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

Convert double to float rather than using real

  • Property mode set to 100644
File size: 6.3 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    // 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 double hc = cap_radius - sqrt(cap_radius*cap_radius - radius*radius);
63    return M_PI*(radius*radius*(length+hc) + 0.333333333333333*hc*hc*hc);
64}
65
66double Iq(double q,
67    double sld,
68    double solvent_sld,
69    double radius,
70    double cap_radius,
71    double length)
72{
73    double sn, cn; // slots to hold sincos function output
74
75    // Exclude invalid inputs.
76    if (cap_radius < radius) return -1.0;
77
78    const double lower = 0.0;
79    const double upper = M_PI_2;
80    const double h = sqrt(cap_radius*cap_radius - radius*radius); // negative h
81    double total = 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 double alpha= 0.5*(Gauss76Z[i]*(upper-lower) + upper + lower);
85        SINCOS(alpha, sn, cn);
86
87        const double 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 double besarg = q*radius*sn;
93        const double siarg = q*0.5*length*cn;
94        // lim_{x->0} J1(x)/x = 1/2,   lim_{x->0} sin(x)/x = 1
95        const double bj = (besarg == 0.0 ? 0.5 : J1(besarg)/besarg);
96        const double si = (siarg == 0.0 ? 1.0 : sin(siarg)/siarg);
97        const double cyl_Fq = M_PI*radius*radius*length*2.0*bj*si;
98
99        // Volume weighted average F(q)
100        const double 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 double form = total * 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 double s = (sld - solvent_sld);
110    return 1.0e-4 * form * s * s; // form_volume(radius, cap_radius, length);
111}
112
113
114double Iqxy(double qx, double qy,
115    double sld,
116    double solvent_sld,
117    double radius,
118    double cap_radius,
119    double length,
120    double theta,
121    double phi)
122{
123    double sn, cn; // slots to hold sincos function output
124
125    // Exclude invalid inputs.
126    if (cap_radius < radius) return -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 double q = sqrt(qx*qx+qy*qy);
133    const double cos_val = cn*cos(phi*M_PI_180)*(qx/q) + sn*(qy/q);
134    const double alpha = acos(cos_val); // rod angle relative to q
135    SINCOS(alpha, sn, cn);
136
137    const double h = sqrt(cap_radius*cap_radius - radius*radius); // negative h
138    const double cap_Fq = _cap_kernel(q, h, cap_radius, length, sn, cn);
139
140    const double besarg = q*radius*sn;
141    const double siarg = q*0.5*length*cn;
142    // lim_{x->0} J1(x)/x = 1/2,   lim_{x->0} sin(x)/x = 1
143    const double bj = (besarg == 0.0 ? 0.5 : J1(besarg)/besarg);
144    const double si = (siarg == 0.0 ? 1.0 : sin(siarg)/siarg);
145    const double cyl_Fq = M_PI*radius*radius*length*2.0*bj*si;
146
147    // Volume weighted average F(q)
148    const double Aq = cyl_Fq + cap_Fq;
149
150    // Multiply by contrast^2, normalize by cylinder volume and convert to cm-1
151    const double s = (sld - solvent_sld);
152    return 1.0e-4 * Aq * Aq * s * s; // form_volume(radius, cap_radius, length);
153}
Note: See TracBrowser for help on using the repository browser.