source: sasmodels/sasmodels/models/lib/wrc_cyl.c @ 3a220e6

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

tweak cutoff point for debye taylor series expansion

  • 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.1  // 1e-14 error
124#else
125#define DEBYE_CUTOFF 0.9  // 4e-7 error
126#endif
127
128/* For double precision, the following gets 1e-15 error rather than 1e-14
129    if (qsq < 9./16.) {
130        // PadeApproximant[2*Exp[-x^2] + x^2-1)/x^4, {x, 0, 8}]
131        const double A1=1./12., A2=2./99., A3=1./2640., A4=1./23760., A5=-1./1995840.;
132        const double B1=5./12., B2=5./66., B3=1./132., B4=1./2376., B5=1./95040.;
133        const double x = qsq;
134        return (((((A5*x + A4)*x + A3)*x + A2)*x + A1)*x + 1.)
135                /(((((B5*x + B4)*x + B3)*x + B2)*x + B1)*x + 1.);
136    }
137*/
138
139    if (qsq < DEBYE_CUTOFF) {
140        const double x = qsq;
141        const double C0 = +1.;
142        const double C1 = -1./3.;
143        const double C2 = +1./12.;
144        const double C3 = -1./60.;
145        const double C4 = +1./360.;
146        const double C5 = -1./2520.;
147        const double C6 = +1./20160.;
148        const double C7 = -1./181440.;
149        return ((((((C7*x + C6)*x + C5)*x + C4)*x + C3)*x + C2)*x + C1)*x + C0;
150    } else {
151        return 2.*(exp(-qsq) + qsq - 1.)/(qsq*qsq);
152    }
153}
154
155static double
156Sexv(double q, double L, double b)
157{
158    const double C1=1.22;
159    const double C2=0.4288;
160    const double C3=-1.651;
161    const double miu = 0.585;
162    const double qr = q*sqrt(Rgsquare(L,b));
163    const double x = pow(qr, -1.0/miu);
164    const double w = w_WR(qr);
165
166    const double base = (1.0 - w)*Sdebye(qr*qr);
167    const double correction = w*x*(C1 + x*(C2 + x*C3));
168    return base + correction;
169}
170
171
172static double
173Sexv_new(double q, double L, double b)
174{
175    // Modified by Yun on Oct. 15,
176
177    // Correction factor to apply to the returned Sexv
178    const double qr = q*sqrt(Rgsquare(L,b));
179    const double qr2 = qr*qr;
180    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);
181    const double C = (L/b > 10.0) ? 3.06*pow(L/b, -0.44)*t1 : t1;
182
183    const double Sexv_orig = Sexv(q, L, b);
184
185    // calculating the derivative to decide on the correction (cutoff) term?
186    // Note: this is modified from WRs original code
187    const double del=1.05;
188    const double qdel = (Sexv(q*del,L,b) - Sexv_orig)/(q*(del - 1.0));
189
190    if (qdel < 0) {
191        // return Sexv with the additional correction
192        return Sexv_orig + C;
193    } else {
194        // recalculate Sexv base and return it with the additional correction
195        const double w = w_WR(qr);
196        return (1.0 - w)*Sdebye(qr2) + C;
197    }
198}
199
200
201static double
202Sk_WR(double q, double L, double b)
203{
204    const double Rg_short = sqrt(Rgsquareshort(L, b));
205    double q0short = fmax(1.9/Rg_short, 3.0);
206    double ans;
207
208    if( L > 4*b ) { // L > 4*b : Longer Chains
209        if (q*b <= 3.1) {
210            ans = Sexv_new(q, L, b);
211        } else { //q(i)*b > 3.1
212            ans = a_long(q, L, b /*, p1, p2, q0*/);
213        }
214    } else { // L <= 4*b : Shorter Chains
215        if (q*b <= q0short) { // q*b <= fmax(1.9/Rg_short, 3)
216            if (q*b <= 0.01) {
217                ans = 1.0 - square(q*Rg_short)/3.0;
218            } else {
219                ans = Sdebye(square(q*Rg_short));
220            }
221        } else {  // q*b > max(1.9/Rg_short, 3)
222            ans = a_short(q, L, b /*, p1short, p2short*/, q0short);
223        }
224    }
225
226    return ans;
227}
Note: See TracBrowser for help on using the repository browser.