source: sasmodels/sasmodels/models/capped_cylinder.c @ 99658f6

core_shell_microgelsmagnetic_modelticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 99658f6 was 99658f6, checked in by grethevj, 8 months ago

updated ER functions including cylinder excluded volume, to match 4.x

  • Property mode set to 100644
File size: 6.0 KB
Line 
1#define INVALID(v) (v.radius_cap < v.radius)
2
3// Integral over a convex lens kernel for t in [h/R,1].  See the docs for
4// the definition of the function being integrated.
5//   q is the magnitude of the q vector.
6//   h is the length of the lens "inside" the cylinder.  This negative wrt the
7//       definition of h in the docs.
8//   radius_cap is the radius of the lens
9//   length is the cylinder length, or the separation between the lens halves
10//   theta is the angle of the cylinder wrt q.
11static double
12_cap_kernel(double qab, double qc, double h, double radius_cap,
13    double half_length)
14{
15    // translate a point in [-1,1] to a point in [lower,upper]
16    const double upper = 1.0;
17    const double lower = h/radius_cap; // integral lower bound
18    const double zm = 0.5*(upper-lower);
19    const double zb = 0.5*(upper+lower);
20
21    // cos term in integral is:
22    //    cos (q (R t - h + L/2) cos(theta))
23    // so turn it into:
24    //    cos (m t + b)
25    // where:
26    //    m = q R cos(theta)
27    //    b = q(L/2-h) cos(theta)
28    const double m = radius_cap*qc; // cos argument slope
29    const double b = (half_length-h)*qc; // cos argument intercept
30    const double qab_r = radius_cap*qab; // Q*R*sin(theta)
31    double total = 0.0;
32    for (int i=0; i<GAUSS_N; i++) {
33        const double t = GAUSS_Z[i]*zm + zb;
34        const double radical = 1.0 - t*t;
35        const double bj = sas_2J1x_x(qab_r*sqrt(radical));
36        const double Fq = cos(m*t + b) * radical * bj;
37        total += GAUSS_W[i] * Fq;
38    }
39    // translate dx in [-1,1] to dx in [lower,upper]
40    const double integral = total*zm;
41    const double cap_Fq = 2.0*M_PI*cube(radius_cap)*integral;
42    return cap_Fq;
43}
44
45static double
46_fq(double qab, double qc, double h, double radius_cap, double radius, double half_length)
47{
48    const double cap_Fq = _cap_kernel(qab, qc, h, radius_cap, half_length);
49    const double bj = sas_2J1x_x(radius*qab);
50    const double si = sas_sinx_x(half_length*qc);
51    const double cyl_Fq = 2.0*M_PI*radius*radius*half_length*bj*si;
52    const double Aq = cap_Fq + cyl_Fq;
53    return Aq;
54}
55
56static double
57form_volume(double radius, double radius_cap, double length)
58{
59    // cap radius should never be less than radius when this is called
60
61    // Note: volume V = 2*V_cap + V_cyl
62    //
63    // V_cyl = pi r_cyl^2 L
64    // 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)
65    //
66    // The docs for capped cylinder give the volume as:
67    //    V = pi r^2 L + 2/3 pi (R-h)^2 (2R + h)
68    // where r_cap=R and h = R - h_c.
69    //
70    // The first part is clearly V_cyl.  The second part requires some work:
71    //    (R-h)^2 => h_c^2
72    //    (2R+h) => 2R+ h_c-h_c + h => 2R + (R-h)-h_c + h => 3R-h_c
73    // And so:
74    //    2/3 pi (R-h)^2 (2R + h) => 2/3 pi h_c^2 (3 r_cap - h_c)
75    // which is 2 V_cap, using the second form above.
76    //
77    // In this function we are going to use the first form of V_cap
78    //
79    //      V = V_cyl + 2 V_cap
80    //        = pi r^2 L + pi hc (r^2 + hc^2/3)
81    //        = pi (r^2 (L+hc) + hc^3/3)
82    const double hc = radius_cap - sqrt(radius_cap*radius_cap - radius*radius);
83    return M_PI*(radius*radius*(length+hc) + hc*hc*hc/3.0);
84}
85
86static double
87radius_from_excluded_volume(double radius, double radius_cap, double length)
88{
89    const double hc = radius_cap - sqrt(radius_cap*radius_cap - radius*radius);
90    const double length_tot = length + 2.0*hc;
91    return 0.5*cbrt(0.75*radius*(2.0*radius*length_tot + (radius + length_tot)*(M_PI*radius + length_tot)));
92}
93
94static double
95radius_from_volume(double radius, double radius_cap, double length)
96{
97    const double vol_cappedcyl = form_volume(radius,radius_cap,length);
98    return cbrt(vol_cappedcyl/M_4PI_3);
99}
100
101static double
102radius_from_totallength(double radius, double radius_cap, double length)
103{
104    const double hc = radius_cap - sqrt(radius_cap*radius_cap - radius*radius);
105    return 0.5*length + hc;
106}
107
108static double
109effective_radius(int mode, double radius, double radius_cap, double length)
110{
111    switch (mode) {
112    default:
113    case 1: // equivalent cylinder excluded volume
114        return radius_from_excluded_volume(radius, radius_cap, length);
115    case 2: // equivalent volume sphere
116        return radius_from_volume(radius, radius_cap, length);
117    case 3: // radius
118        return radius;
119    case 4: // half length
120        return 0.5*length;
121    case 5: // half total length
122        return radius_from_totallength(radius, radius_cap,length);
123    }
124}
125
126static void
127Fq(double q,double *F1, double *F2, double sld, double solvent_sld,
128    double radius, double radius_cap, double length)
129{
130    const double h = sqrt(radius_cap*radius_cap - radius*radius);
131    const double half_length = 0.5*length;
132
133    // translate a point in [-1,1] to a point in [0, pi/2]
134    const double zm = M_PI_4;
135    const double zb = M_PI_4;
136    double total_F1 = 0.0;
137    double total_F2 = 0.0;
138    for (int i=0; i<GAUSS_N ;i++) {
139        const double theta = GAUSS_Z[i]*zm + zb;
140        double sin_theta, cos_theta; // slots to hold sincos function output
141        SINCOS(theta, sin_theta, cos_theta);
142        const double qab = q*sin_theta;
143        const double qc = q*cos_theta;
144        const double Aq = _fq(qab, qc, h, radius_cap, radius, half_length);
145        // scale by sin_theta for spherical coord integration
146        total_F1 += GAUSS_W[i] * Aq * sin_theta;
147        total_F2 += GAUSS_W[i] * Aq * Aq * sin_theta;
148    }
149    // translate dx in [-1,1] to dx in [lower,upper]
150    const double form_avg = total_F1 * zm;
151    const double form_squared_avg = total_F2 * zm;
152
153    // Contrast
154    const double s = (sld - solvent_sld);
155    *F1 = 1.0e-2 * s * form_avg;
156    *F2 = 1.0e-4 * s * s * form_squared_avg;
157}
158
159
160static double
161Iqac(double qab, double qc,
162    double sld, double solvent_sld, double radius,
163    double radius_cap, double length)
164{
165    const double h = sqrt(radius_cap*radius_cap - radius*radius);
166    const double Aq = _fq(qab, qc, h, radius_cap, radius, 0.5*length);
167
168    // Multiply by contrast^2 and convert to cm-1
169    const double s = (sld - solvent_sld);
170    return 1.0e-4 * square(s * Aq);
171}
Note: See TracBrowser for help on using the repository browser.