source: sasmodels/sasmodels/models/lib/wrc_cyl.c @ 237c9cf

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

flexible_cylinder: improve low q Sdebye calculation; adjust code so different branches use the same equation numbers

  • Property mode set to 100644
File size: 6.4 KB
RevLine 
[f94d8a2]1/*
2    Functions for WRC implementation of flexible cylinders
3*/
[ba32cdd]4
[f94d8a2]5static double
[6e72989]6Rgsquare(double L, double b)
[f94d8a2]7{
[6e72989]8    const double x = L/b;
[237c9cf]9    // Use Horner's method to evaluate Pedersen eq 15:
10    //     alpha^2 = [1.0 + (x/3.12)^2 + (x/8.67)^3] ^ (0.176/3)
[6e72989]11    const double alphasq =
[237c9cf]12        pow(1.0 + x*x*(1.027284681130835e-01 + 1.534414548417740e-03*x),
[6e72989]13            5.866666666666667e-02);
14    return alphasq*L*b/6.0;
[f94d8a2]15}
16
17static double
[6e72989]18Rgsquareshort(double L, double b)
[f94d8a2]19{
[237c9cf]20    const double r = b/L;  // = 1/n_b in Pedersen ref.
[6e72989]21    return Rgsquare(L, b) * (1.0 + r*(-1.5 + r*(1.5 + r*0.75*expm1(-2.0/r))));
[f94d8a2]22}
23
24static double
[237c9cf]25w_WR(double x)
26{
27    // Pedersen eq. 16:
28    //    w = 1 - [1 + tanh((x-C4)/C5)]/2
29    const double C4 = 1.523;
30    const double C5 = 0.1477;
31    return 0.5 + 0.5*tanh((x - C4)/C5);
32}
33
34static double
35Sdebye(double qsq)
36{
37#if FLOAT_SIZE>4
38#  define DEBYE_CUTOFF 0.25  // 1e-15 error
39#else
40#  define DEBYE_CUTOFF 1.0  // 4e-7 error
41#endif
42
43    if (qsq < DEBYE_CUTOFF) {
44        const double x = qsq;
45        // mathematica: PadeApproximant[2*Exp[-x^2] + x^2-1)/x^4, {x, 0, 8}]
46        const double A1=1./15., A2=1./60, A3=0., A4=1./75600.;
47        const double B1=2./5., B2=1./15., B3=1./180., B4=1./5040.;
48        return ((((A4*x + A3)*x + A2)*x + A1)*x + 1.)
49             / ((((B4*x + B3)*x + B2)*x + B1)*x + 1.);
50    } else {
51        return 2.*(expm1(-qsq) + qsq)/(qsq*qsq);
52    }
53}
54
55static double
[6e72989]56a_long(double qp, double L, double b/*, double p1, double p2, double q0*/)
[f94d8a2]57{
[6e72989]58    const double p1 = 4.12;
59    const double p2 = 4.42;
60    const double q0 = 3.1;
[f94d8a2]61
[237c9cf]62    // Constants C1, ..., C5 derived from least squares fit (Pedersen, eq 13,16)
[e7678b2]63    const double C1 = 1.22;
64    const double C2 = 0.4288;
65    const double C3 = -1.651;
66    const double C4 = 1.523;
67    const double C5 = 0.1477;
68    const double miu = 0.585;
69
[6e72989]70    const double C = (L/b>10.0 ? 3.06*pow(L/b, -0.44) : 1.0);
71    const double r2 = Rgsquare(L,b);
72    const double r = sqrt(r2);
73    const double qr_b = q0*r/b;
74    const double qr_b_sq = qr_b*qr_b;
75    const double qr_b_4 = qr_b_sq*qr_b_sq;
76    const double qr_b_miu = pow(qr_b, -1.0/miu);
77    const double em1_qr_b_sq = expm1(-qr_b_sq);
78    const double sech2 = 1.0/square(cosh((qr_b-C4)/C5));
[237c9cf]79    const double w = w_WR(qr_b);
[6e72989]80
81    const double t1 = pow(q0, 1.0 + p1 + p2)/(b*(p1-p2));
82    const double t2 = C/(15.0*L) * (
83        + 14.0*b*b*em1_qr_b_sq/(q0*qr_b_sq)
84        + 2.0*q0*r2*exp(-qr_b_sq)*(11.0 + 7.0/qr_b_sq));
85    const double t11 = ((C3*qr_b_miu + C2)*qr_b_miu + C1)*qr_b_miu;
86    const double t3 = r*sech2/(2.*C5)*t11;
87    const double t4 = r*(em1_qr_b_sq + qr_b_sq)*sech2 / (C5*qr_b_4);
[237c9cf]88    const double t5 = -4.0*r*qr_b*em1_qr_b_sq/qr_b_4 * (1.0 - w);
89    const double t10 = 2.0*(em1_qr_b_sq + qr_b_sq)/qr_b_4 * (1.0 - w); //=Sdebye*(1-w)
[6e72989]90    const double t6 = 4.0*b/q0 * t10;
91    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);
[237c9cf]92    const double t9 = C*b/L * (4.0 - exp(-qr_b_sq) * (11.0 + 7.0/qr_b_sq) + 7.0/qr_b_sq)/15.0;
93    const double t12 = b*b*M_PI/(L*q0*q0) + t2 + t3 - t4 + t5 - t6 + t7*w;
94    const double t13 = -b*M_PI/(L*q0) + t9 + t10 + t11*w;
[6e72989]95
96    const double a1 = pow(q0,p1)*t13 - t1*pow(q0,-p2)*(t12 + b*p1/q0*t13);
97    const double a2 = t1*pow(q0,-p1)*(t12 + b*p1/q0*t13);
98
99    const double ans = a1*pow(qp*b, -p1) + a2*pow(qp*b, -p2) + M_PI/(qp*L);
100    return ans;
101}
102
103static double
104_short(double r2, double exp_qr_b, double L, double b, double p1short,
105        double p2short, double q0)
106{
107    const double qr2 = q0*q0 * r2;
[e7678b2]108    const double b3 = b*b*b;
[6e72989]109    const double q0p = pow(q0, -4.0 + p1short);
[f94d8a2]110
[6e72989]111    double yy = 1.0/(L*r2*r2) * b/exp_qr_b*q0p
112        * (8.0*b3*L
113           - 8.0*b3*exp_qr_b*L
114           + 2.0*b3*exp_qr_b*L*p2short
115           - 2.0*b*exp_qr_b*L*p2short*qr2
116           + 4.0*b*exp_qr_b*L*qr2
117           - 2.0*b3*L*p2short
118           + 4.0*b*L*qr2
119           - M_PI*exp_qr_b*qr2*q0*r2
120           + M_PI*exp_qr_b*p2short*qr2*q0*r2);
[f94d8a2]121
[6e72989]122    return yy;
[f94d8a2]123}
124static double
[6e72989]125a_short(double qp, double L, double b
126        /*double p1short, double p2short*/, double q0)
[f94d8a2]127{
[6e72989]128    const double p1short = 5.36;
129    const double p2short = 5.62;
[f94d8a2]130
[6e72989]131    const double r2 = Rgsquareshort(L,b);
132    const double exp_qr_b = exp(r2*square(q0/b));
133    const double pdiff = p1short - p2short;
134    const double a1 = _short(r2,exp_qr_b,L,b,p1short,p2short,q0)/pdiff;
135    const double a2= -_short(r2,exp_qr_b,L,b,p2short,p1short,q0)/pdiff;
136    const double ans = a1*pow(qp*b, -p1short) + a2*pow(qp*b, -p2short) + M_PI/(qp*L);
137    return ans;
[f94d8a2]138}
139
140
141static double
142Sexv(double q, double L, double b)
143{
[237c9cf]144    // Pedersen eq 13, corrected by Chen eq A.5, swapping w and 1-w
[e7678b2]145    const double C1=1.22;
146    const double C2=0.4288;
147    const double C3=-1.651;
148    const double miu = 0.585;
[6e72989]149    const double qr = q*sqrt(Rgsquare(L,b));
[237c9cf]150    const double qr_miu = pow(qr, -1.0/miu);
[6e72989]151    const double w = w_WR(qr);
[237c9cf]152    const double t10 = qr*Sdebye(qr*qr)*(1.0 - w);
153    const double t11 = ((C3*qr_miu + C2)*qr_miu + C1)*qr_miu;
[f94d8a2]154
[237c9cf]155    return t10 + w*t11;
[f94d8a2]156}
157
[237c9cf]158// Modified by Yun on Oct. 15,
[f94d8a2]159static double
[6e72989]160Sexv_new(double q, double L, double b)
[f94d8a2]161{
[6e72989]162    const double qr = q*sqrt(Rgsquare(L,b));
163    const double qr2 = qr*qr;
[237c9cf]164    const double C = (L/b > 10.0) ? 3.06*pow(L/b, -0.44) : 1.0;
165    const double t9 = C*b/L * (4.0 - exp(-qr2) * (11.0 + 7.0/qr2) + 7.0/qr2)/15.0;
[f94d8a2]166
[6e72989]167    const double Sexv_orig = Sexv(q, L, b);
[f94d8a2]168
[6e72989]169    // calculating the derivative to decide on the correction (cutoff) term?
170    // Note: this is modified from WRs original code
171    const double del=1.05;
172    const double qdel = (Sexv(q*del,L,b) - Sexv_orig)/(q*(del - 1.0));
[f94d8a2]173
[6e72989]174    if (qdel < 0) {
[237c9cf]175        return t9 + Sexv_orig;
[6e72989]176    } else {
177        const double w = w_WR(qr);
[237c9cf]178        const double t10 = Sdebye(qr*qr)*(1.0 - w);
179        return t9 + t10;
[6e72989]180    }
[f94d8a2]181}
182
183
[6e72989]184static double
185Sk_WR(double q, double L, double b)
186{
187    const double Rg_short = sqrt(Rgsquareshort(L, b));
188    double q0short = fmax(1.9/Rg_short, 3.0);
189    double ans;
[f94d8a2]190
[6e72989]191    if( L > 4*b ) { // L > 4*b : Longer Chains
192        if (q*b <= 3.1) {
193            ans = Sexv_new(q, L, b);
194        } else { //q(i)*b > 3.1
195            ans = a_long(q, L, b /*, p1, p2, q0*/);
196        }
197    } else { // L <= 4*b : Shorter Chains
198        if (q*b <= q0short) { // q*b <= fmax(1.9/Rg_short, 3)
[237c9cf]199            // 2017-10-01 pkienzle: moved low q approximation into Sdebye()
200            ans = Sdebye(square(q*Rg_short));
[6e72989]201        } else {  // q*b > max(1.9/Rg_short, 3)
202            ans = a_short(q, L, b /*, p1short, p2short*/, q0short);
203        }
[f94d8a2]204    }
205
[6e72989]206    return ans;
[f94d8a2]207}
Note: See TracBrowser for help on using the repository browser.