source: sasmodels/sasmodels/models/lib/wrc_cyl.c @ 6e72989

core_shell_microgelscostrafo411magnetic_modelticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 6e72989 was 6e72989, checked in by Paul Kienzle <pkienzle@…>, 7 years ago

clean up flexible cylinder; allow code to run on Intel HD 6000; improve precision

  • Property mode set to 100644
File size: 7.1 KB
Line 
1/*
2    Functions for WRC implementation of flexible cylinders
3*/
4
5static double
6Rgsquare(double L, double b)
7{
8    const double x = L/b;
9    // Use Horner's method to evaluate:
10    //     pow(1.0+square(x/3.12)+cube(x/8.67), 0.176/3.0)
11    // Too many digits on the coefficients, but necessary for consistency
12    const double alphasq =
13        pow(1.0 + square(x)*(1.534414548417740e-03*x + 1.027284681130835e-01),
14            5.866666666666667e-02);
15    return alphasq*L*b/6.0;
16}
17
18static double
19Rgsquareshort(double L, double b)
20{
21    const double r = b/L;
22    return Rgsquare(L, b) * (1.0 + r*(-1.5 + r*(1.5 + r*0.75*expm1(-2.0/r))));
23}
24
25static double
26a_long(double qp, double L, double b/*, double p1, double p2, double q0*/)
27{
28    // Note: caller sends p1, p2, q0 in as constants.
29    const double p1 = 4.12;
30    const double p2 = 4.42;
31    const double q0 = 3.1;
32
33    const double C1 = 1.22;
34    const double C2 = 0.4288;
35    const double C3 = -1.651;
36    const double C4 = 1.523;
37    const double C5 = 0.1477;
38    const double miu = 0.585;
39
40
41    const double C = (L/b>10.0 ? 3.06*pow(L/b, -0.44) : 1.0);
42    const double r2 = Rgsquare(L,b);
43    const double r = sqrt(r2);
44    const double qr_b = q0*r/b;
45    const double qr_b_sq = qr_b*qr_b;
46    const double qr_b_4 = qr_b_sq*qr_b_sq;
47    const double qr_b_miu = pow(qr_b, -1.0/miu);
48    const double em1_qr_b_sq = expm1(-qr_b_sq);
49    const double sech2 = 1.0/square(cosh((qr_b-C4)/C5));
50    const double tanh1m = 1.0 - tanh(-C4 + qr_b/C5);
51
52    const double t1 = pow(q0, 1.0 + p1 + p2)/(b*(p1-p2));
53    const double t2 = C/(15.0*L) * (
54        + 14.0*b*b*em1_qr_b_sq/(q0*qr_b_sq)
55        + 2.0*q0*r2*exp(-qr_b_sq)*(11.0 + 7.0/qr_b_sq));
56    const double t11 = ((C3*qr_b_miu + C2)*qr_b_miu + C1)*qr_b_miu;
57    const double t3 = r*sech2/(2.*C5)*t11;
58    const double t4 = r*(em1_qr_b_sq + qr_b_sq)*sech2 / (C5*qr_b_4);
59    const double t5 = -2.0 * r*qr_b*em1_qr_b_sq * tanh1m / qr_b_4;
60    const double t10 = (em1_qr_b_sq + qr_b_sq) * tanh1m / qr_b_4;
61    const double t6 = 4.0*b/q0 * t10;
62    const double t7 = r*((-3.0*C3*qr_b_miu -2.0*C2)*qr_b_miu -1.0*C1)*qr_b_miu/(miu*qr_b);
63    const double t8 = 2.0 - tanh1m;
64    const double t9 = b*C/(15.0*L) * (4.0 - exp(-qr_b_sq) * (11.0 + 7.0/qr_b_sq) + 7.0/qr_b_sq);
65    const double t12 = b*b*M_PI/(L*q0*q0) + t2 + t3 - t4 + t5 - t6 + 0.5*t7*t8;
66    const double t13 = -b*M_PI/(L*q0) + t9 + t10 + 0.5*t11*t8;
67
68    const double a1 = pow(q0,p1)*t13 - t1*pow(q0,-p2)*(t12 + b*p1/q0*t13);
69    const double a2 = t1*pow(q0,-p1)*(t12 + b*p1/q0*t13);
70
71    const double ans = a1*pow(qp*b, -p1) + a2*pow(qp*b, -p2) + M_PI/(qp*L);
72    return ans;
73}
74
75static double
76_short(double r2, double exp_qr_b, double L, double b, double p1short,
77        double p2short, double q0)
78{
79    const double qr2 = q0*q0 * r2;
80    const double b3 = b*b*b;
81    const double q0p = pow(q0, -4.0 + p1short);
82
83    double yy = 1.0/(L*r2*r2) * b/exp_qr_b*q0p
84        * (8.0*b3*L
85           - 8.0*b3*exp_qr_b*L
86           + 2.0*b3*exp_qr_b*L*p2short
87           - 2.0*b*exp_qr_b*L*p2short*qr2
88           + 4.0*b*exp_qr_b*L*qr2
89           - 2.0*b3*L*p2short
90           + 4.0*b*L*qr2
91           - M_PI*exp_qr_b*qr2*q0*r2
92           + M_PI*exp_qr_b*p2short*qr2*q0*r2);
93
94    return yy;
95}
96static double
97a_short(double qp, double L, double b
98        /*double p1short, double p2short*/, double q0)
99{
100    const double p1short = 5.36;
101    const double p2short = 5.62;
102
103    const double r2 = Rgsquareshort(L,b);
104    const double exp_qr_b = exp(r2*square(q0/b));
105    const double pdiff = p1short - p2short;
106    const double a1 = _short(r2,exp_qr_b,L,b,p1short,p2short,q0)/pdiff;
107    const double a2= -_short(r2,exp_qr_b,L,b,p2short,p1short,q0)/pdiff;
108    const double ans = a1*pow(qp*b, -p1short) + a2*pow(qp*b, -p2short) + M_PI/(qp*L);
109    return ans;
110}
111
112//WR named this w (too generic)
113static double
114w_WR(double x)
115{
116    return 0.5*(1 + tanh((x - 1.523)/0.1477));
117}
118
119static double
120Sdebye(double qsq)
121{
122#if FLOAT_SIZE>4
123#define DEBYE_CUTOFF 0.01  // 1e-13 error
124#else
125#define DEBYE_CUTOFF 1.0  // 1e-7 error
126#endif
127
128    if (qsq < DEBYE_CUTOFF) {
129        const double x = qsq;
130        const double C0 = +1.;
131        const double C1 = -1./3.;
132        const double C2 = +1./12.;
133        const double C3 = -1./60.;
134        const double C4 = +1./360.;
135        const double C5 = -1./2520.;
136        const double C6 = +1./20160.;
137        const double C7 = -1./181440.;
138        return ((((((C7*x + C6)*x + C5)*x + C4)*x + C3)*x + C2)*x + C1)*x + C0;
139    /* failed attempt to improve double precision better than 1e-13
140    } else if (qsq < 1.0) {
141        // taylor series around q^2 = 0.5
142        const double x = qsq - 0.5;
143        const double sqrt_e = sqrt(M_E);
144        const double C0 = 8./sqrt_e - 4.;
145        const double C1 = 24. - 40./sqrt_e;
146        const double C2 = 132./sqrt_e - 80.;
147        const double C3 = 224. - 1108./(3.*sqrt_e);
148        const double C4 = 2849./(3.*sqrt_e) - 576.;
149        const double C5 = 1408 - 11607./(5.*sqrt_e);
150        return ((((C5*x + C4)*x + C3)*x + C2)*x + C1)*x + C0;
151    */
152    } else {
153        return 2.*(exp(-qsq) + qsq - 1.)/(qsq*qsq);
154    }
155}
156
157static double
158Sexv(double q, double L, double b)
159{
160    const double C1=1.22;
161    const double C2=0.4288;
162    const double C3=-1.651;
163    const double miu = 0.585;
164    const double qr = q*sqrt(Rgsquare(L,b));
165    const double x = pow(qr, -1.0/miu);
166    const double w = w_WR(qr);
167
168    const double base = (1.0 - w)*Sdebye(qr*qr);
169    const double correction = w*x*(C1 + x*(C2 + x*C3));
170    return base + correction;
171}
172
173
174static double
175Sexv_new(double q, double L, double b)
176{
177    // Modified by Yun on Oct. 15,
178
179    // Correction factor to apply to the returned Sexv
180    const double qr = q*sqrt(Rgsquare(L,b));
181    const double qr2 = qr*qr;
182    const double t1 = (4.0/15.0 + 7.0/(15.0*qr2) - (11.0/15.0 + 7.0/(15.0*qr2))*exp(-qr2))*(b/L);
183    const double C = (L/b > 10.0) ? 3.06*pow(L/b, -0.44)*t1 : t1;
184
185    const double Sexv_orig = Sexv(q, L, b);
186
187    // calculating the derivative to decide on the correction (cutoff) term?
188    // Note: this is modified from WRs original code
189    const double del=1.05;
190    const double qdel = (Sexv(q*del,L,b) - Sexv_orig)/(q*(del - 1.0));
191
192    if (qdel < 0) {
193        // return Sexv with the additional correction
194        return Sexv_orig + C;
195    } else {
196        // recalculate Sexv base and return it with the additional correction
197        const double w = w_WR(qr);
198        return (1.0 - w)*Sdebye(qr2) + C;
199    }
200}
201
202
203static double
204Sk_WR(double q, double L, double b)
205{
206    const double Rg_short = sqrt(Rgsquareshort(L, b));
207    double q0short = fmax(1.9/Rg_short, 3.0);
208    double ans;
209
210    if( L > 4*b ) { // L > 4*b : Longer Chains
211        if (q*b <= 3.1) {
212            ans = Sexv_new(q, L, b);
213        } else { //q(i)*b > 3.1
214            ans = a_long(q, L, b /*, p1, p2, q0*/);
215        }
216    } else { // L <= 4*b : Shorter Chains
217        if (q*b <= q0short) { // q*b <= fmax(1.9/Rg_short, 3)
218            if (q*b <= 0.01) {
219                ans = 1.0 - square(q*Rg_short)/3.0;
220            } else {
221                ans = Sdebye(square(q*Rg_short));
222            }
223        } else {  // q*b > max(1.9/Rg_short, 3)
224            ans = a_short(q, L, b /*, p1short, p2short*/, q0short);
225        }
226    }
227
228    return ans;
229}
Note: See TracBrowser for help on using the repository browser.