source: sasmodels/sasmodels/models/lib/wrc_cyl.c @ 4962519

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

use square and cube instead of pow()

  • Property mode set to 100644
File size: 11.2 KB
Line 
1/*
2    Functions for WRC implementation of flexible cylinders
3*/
4double Sk_WR(double q, double L, double b);
5
6
7static double
8AlphaSquare(double x)
9{
10    // Potentially faster. Needs proper benchmarking.
11    // add native_powr to kernel_template
12    //double t = native_powr( (1.0 + (x/3.12)*(x/3.12) +
13    //     (x/8.67)*(x/8.67)*(x/8.67)),(0.176/3.0) );
14    //return t;
15
16    return pow(1.0+square(x/3.12)+cube(x/8.67), 0.176/3.0);
17}
18
19//
20static double
21Rgsquarezero(double q, double L, double b)
22{
23    const double r = b/L;
24    return (L*b/6.0) * (1.0 - r*(1.5 + r*(1.5 + r*0.75*expm1(-2.0/r))));
25}
26
27//
28static double
29Rgsquareshort(double q, double L, double b)
30{
31    return AlphaSquare(L/b) * Rgsquarezero(q,L,b);
32}
33
34//
35static double
36Rgsquare(double q, double L, double b)
37{
38    return AlphaSquare(L/b)*L*b/6.0;
39}
40
41static double
42sech_WR(double x)
43{
44    return(1/cosh(x));
45}
46
47static double
48a1long(double q, double L, double b, double p1, double p2, double q0)
49{
50    double C;
51
52    if( L/b > 10.0) {
53        C = 3.06/pow((L/b),0.44);
54    } else {
55        C = 1.0;
56    }
57
58    const double C1 = 1.22;
59    const double C2 = 0.4288;
60    const double C3 = -1.651;
61    const double C4 = 1.523;
62    const double C5 = 0.1477;
63    const double miu = 0.585;
64
65    const double Rg2 = Rgsquare(q,L,b);
66    const double Rg22 = Rg2*Rg2;
67    const double Rg = sqrt(Rg2);
68    const double Rgb = Rg*q0/b;
69
70    const double b2 = b*b;
71    const double b3 = b*b*b;
72    const double b4 = b3*b;
73    const double q02 = q0*q0;
74    const double q03 = q0*q0*q0;
75    const double q04 = q03*q0;
76    const double q05 = q04*q0;
77
78    const double Rg02 = Rg2*q02;
79
80    const double t1 = (b*C*((4.0/15.0 - pow((double)M_E,(-(Rg02/b2))) *
81         ((11.0/15.0 + (7.0*b2)/(15.0*Rg02))) +
82         (7.0*b2)/(15.0*Rg02))));
83
84    const double t2 = (2.0*b4*(((-1.0) + pow((double)M_E,(-(Rg02/b2))) +
85         Rg02/b2))*((1.0 + 0.5*(((-1.0) -
86         tanh((-C4 + Rgb/C5)))))));
87
88    const double t3 = ((C3*pow(Rgb,((-3.0)/miu)) +
89         C2*pow(Rgb,((-2.0)/miu)) +
90         C1*pow(Rgb,((-1.0)/miu))));
91
92    const double t4 = ((1.0 + tanh(((-C4) + Rgb)/C5)));
93
94    const double t5 = (1.0/(b*p1*pow(q0,((-1.0) - p1 - p2)) -
95         b*p2*pow(q0,((-1.0) - p1 - p2))));
96
97    const double t6 = (b*C*(((-((14.0*b3)/(15.0*q03*Rg2))) +
98         (14.0*b3*pow((double)M_E,(-(Rg02/b2))))/(15.0*q03*Rg2) +
99         (2.0*pow((double)M_E,(-(Rg02/b2)))*q0*((11.0/15.0 +
100         (7.0*b2)/(15.0*Rg02)))*Rg2)/b)));
101
102    const double t7 = (Rg*((C3*pow(((Rgb)),((-3.0)/miu)) +
103         C2*pow(((Rgb)),((-2.0)/miu)) +
104         C1*pow(((Rgb)),((-1.0)/miu))))*pow(sech_WR(((-C4) +
105         Rgb)/C5),2));
106
107    const double t8 = (b4*Rg*(((-1.0) + pow((double)M_E,(-(Rg02/b2))) +
108         Rg02/b2))*pow(sech_WR(((-C4) + Rgb)/C5),2));
109
110    const double t9 = (2.0*b4*(((2.0*q0*Rg2)/b -
111         (2.0*pow((double)M_E,(-(Rg02/b2)))*q0*Rg2)/b))*((1.0 + 0.5*(((-1.0) -
112         tanh(((-C4) + Rgb)/C5))))));
113
114    const double t10 = (8.0*b4*b*(((-1.0) + pow((double)M_E,(-(Rg02/b2))) +
115         Rg02/b2))*((1.0 + 0.5*(((-1.0) - tanh(((-C4) +
116         Rgb)/C5))))));
117
118    const double t11 = (((-((3.0*C3*Rg*pow(((Rgb)),((-1.0) -
119          3.0/miu)))/miu)) - (2.0*C2*Rg*pow(((Rgb)),((-1.0) -
120          2.0/miu)))/miu - (C1*Rg*pow(((Rgb)),((-1.0) -
121          1.0/miu)))/miu));
122
123    const double t12 = ((1.0 + tanh(((-C4) + Rgb)/C5)));
124
125    const double t13 = (b*C*((4.0/15.0 - pow((double)M_E,(-(Rg02/b2)))*((11.0/15.0 +
126          (7.0*b2)/(15.0*q02* Rg2))) +
127          (7.0*b2)/(15.0*Rg02))));
128
129    const double t14 = (2.0*b4*(((-1.0) + pow((double)M_E,(-(Rg02/b2))) +
130          Rg02/b2))*((1.0 + 0.5*(((-1.0) - tanh(((-C4) +
131          Rgb)/C5))))));
132
133    const double t15 = ((C3*pow(((Rgb)),((-3.0)/miu)) +
134        C2*pow(((Rgb)),((-2.0)/miu)) +
135        C1*pow(((Rgb)),((-1.0)/miu))));
136
137
138    double yy = (pow(q0,p1)*(((-((b*M_PI)/(L*q0))) +t1/L +t2/(q04*Rg22) +
139        0.5*t3*t4)) + (t5*((pow(q0,(p1 - p2))*
140        (((-pow(q0,(-p1)))*(((b2*M_PI)/(L*q02) +t6/L +t7/(2.0*C5) -
141        t8/(C5*q04*Rg22) + t9/(q04*Rg22) -t10/(q05*Rg22) + 0.5*t11*t12)) -
142        b*p1*pow(q0,((-1.0) - p1))*(((-((b*M_PI)/(L*q0))) + t13/L +
143        t14/(q04*Rg22) + 0.5*t15*((1.0 + tanh(((-C4) +
144        Rgb)/C5)))))))))));
145
146    return (yy);
147}
148
149static double
150a2long(double q, double L, double b, double p1, double p2, double q0)
151{
152    double C;
153
154    if( L/b > 10.0) {
155        C = 3.06/pow((L/b),0.44);
156    } else {
157        C = 1.0;
158    }
159
160    const double C1 = 1.22;
161    const double C2 = 0.4288;
162    const double C3 = -1.651;
163    const double C4 = 1.523;
164    const double C5 = 0.1477;
165    const double miu = 0.585;
166
167    const double Rg2 = Rgsquare(q,L,b);
168    const double Rg22 = Rg2*Rg2;
169    const double b2 = b*b;
170    const double b3 = b*b*b;
171    const double b4 = b3*b;
172    const double q02 = q0*q0;
173    const double q03 = q0*q0*q0;
174    const double q04 = q03*q0;
175    const double q05 = q04*q0;
176    const double Rg = sqrt(Rg2);
177    const double Rgb = Rg*q0/b;
178    const double Rg02 = Rg2*q02;
179
180    const double t1 = (1.0/(b* p1*pow(q0,((-1.0) - p1 - p2)) -
181         b*p2*pow(q0,((-1.0) - p1 - p2)) ));
182
183    const double t2 = (b*C*(((-1.0*((14.0*b3)/(15.0*q03*Rg2))) +
184         (14.0*b3*pow((double)M_E,(-(Rg02/b2))))/(15.0*q03*Rg2) +
185         (2.0*pow((double)M_E,(-(Rg02/b2)))*q0*((11.0/15.0 +
186         (7*b2)/(15.0*Rg02)))*Rg2)/b)))/L;
187
188    const double t3 = (Rg*((C3*pow(((Rgb)),((-3.0)/miu)) +
189         C2*pow(((Rgb)),((-2.0)/miu)) +
190         C1*pow(((Rgb)),((-1.0)/miu))))*
191         pow(sech_WR(((-C4) +Rgb)/C5),2.0))/(2.0*C5);
192
193    const double t4 = (b4*Rg*(((-1.0) + pow((double)M_E,(-(Rg02/b2))) +
194         Rg02/b2))*pow(sech_WR(((-C4) +
195         Rgb)/C5),2))/(C5*q04*Rg22);
196
197    const double t5 = (2.0*b4*(((2.0*q0*Rg2)/b -
198         (2.0*pow((double)M_E,(-(Rg02/b2)))*q0*Rg2)/b))*
199         ((1.0 + 0.5*(((-1.0) - tanh(((-C4) +
200         Rgb)/C5))))))/(q04*Rg22);
201
202    const double t6 = (8.0*b4*b*(((-1.0) + pow((double)M_E,(-(Rg02/b2))) +
203         Rg02/b2))*((1.0 + 0.5*(((-1) - tanh(((-C4) +
204         Rgb)/C5))))))/(q05*Rg22);
205
206    const double t7 = (((-((3.0*C3*Rg*pow(((Rgb)),((-1.0) -
207         3.0/miu)))/miu)) - (2.0*C2*Rg*pow(((Rgb)),
208         ((-1.0) - 2.0/miu)))/miu - (C1*Rg*pow(((Rgb)),
209         ((-1.0) - 1.0/miu)))/miu));
210
211    const double t8 = ((1.0 + tanh(((-C4) + Rgb)/C5)));
212
213    const double t9 = (b*C*((4.0/15.0 - pow((double)M_E,(-(Rg02/b2)))*((11.0/15.0 +
214         (7.0*b2)/(15*Rg02))) + (7.0*b2)/(15.0*Rg02))))/L;
215
216    const double t10 = (2.0*b4*(((-1) + pow((double)M_E,(-(Rg02/b2))) +
217          Rg02/b2))*((1.0 + 0.5*(((-1) - tanh(((-C4) +
218          Rgb)/C5))))))/(q04*Rg22);
219
220    const double yy = ((-1.0*(t1* ((-pow(q0,-p1)*(((b2*M_PI)/(L*q02) +
221         t2 + t3 - t4 + t5 - t6 + 0.5*t7*t8)) - b*p1*pow(q0,((-1.0) - p1))*
222         (((-((b*M_PI)/(L*q0))) + t9 + t10 +
223         0.5*((C3*pow(((Rgb)),((-3.0)/miu)) +
224         C2*pow(((Rgb)),((-2.0)/miu)) +
225         C1*pow(((Rgb)),((-1.0)/miu))))*
226         ((1.0 + tanh(((-C4) + Rgb)/C5))))))))));
227
228    return (yy);
229}
230
231//
232static double
233a_short(double q, double L, double b, double p1short,
234        double p2short, double factor, double pdiff, double q0)
235{
236    const double Rg2_sh = Rgsquareshort(q,L,b);
237    const double Rg2_sh2 = Rg2_sh*Rg2_sh;
238    const double b3 = b*b*b;
239    const double t1 = ((q0*q0*Rg2_sh)/(b*b));
240    const double Et1 = exp(t1);
241    const double Emt1 = 1.0/Et1;
242    const double q02 = q0*q0;
243    const double q0p = pow(q0,(-4.0 + p1short) );
244
245    double yy = ((factor/(L*(pdiff)*Rg2_sh2)*
246        ((b*Emt1*q0p*((8.0*b3*L - 8.0*b3*Et1*L - 2.0*b3*L*p2short +
247        2.0*b3*Et1*L*p2short + 4.0*b*L*q02*Rg2_sh + 4.0*b*Et1*L*q02*Rg2_sh -
248        2.0*b*Et1*L*p2short*q02*Rg2_sh - Et1*M_PI*q02*q0*Rg2_sh2 +
249        Et1*p2short*M_PI*q02*q0*Rg2_sh2))))));
250
251    return(yy);
252}
253static double
254a1short(double q, double L, double b, double p1short, double p2short, double q0)
255{
256
257    double factor = 1.0;
258    return a_short(q, L, b, p1short, p2short, factor, p1short - p2short, q0);
259}
260
261static double
262a2short(double q, double L, double b, double p1short, double p2short, double q0)
263{
264    double factor = -1.0;
265    return a_short(q, L, b, p2short, p1short, factor, p1short-p2short, q0);
266}
267
268//WR named this w (too generic)
269static double
270w_WR(double x)
271{
272    return 0.5*(1 + tanh((x - 1.523)/0.1477));
273}
274
275//
276static double
277u1(double q, double L, double b)
278{
279    return Rgsquareshort(q,L,b)*q*q;
280}
281
282static double
283u_WR(double q, double L, double b)
284{
285    return Rgsquare(q,L,b)*q*q;
286}
287
288static double
289Sdebye_kernel(double arg)
290{
291    // ORIGINAL
292    double result = 2.0*(exp(-arg) + arg -1.0)/(arg*arg);
293
294    // CONVERSION 1 from http://herbie.uwplse.org/
295    //
296    // exhibits discontinuity - needs more investigation
297    //double a1 = 1.0/6.0;
298    //double a2 = 1.0/72.0;
299    //double a3 = 1.0/24.0;
300    //double result = pow((1.0 - a1*arg - (a2+a3)*arg*arg), 2);
301
302    return result;
303}
304static double
305Sdebye(double q, double L, double b)
306{
307    double arg = u_WR(q,L,b);
308    return Sdebye_kernel(arg);
309}
310
311//
312static double
313Sdebye1(double q, double L, double b)
314{
315    double arg = u1(q,L,b);
316    return Sdebye_kernel(arg);
317
318}
319
320//
321static double
322Sexv(double q, double L, double b)
323{
324
325    const double C1=1.22;
326    const double C2=0.4288;
327    const double C3=-1.651;
328    const double miu = 0.585;
329    const double qRg = q*sqrt(Rgsquare(q,L,b));
330    const double x = pow(qRg, -1.0/miu);
331
332    double yy = (1.0 - w_WR(qRg))*Sdebye(q,L,b) +
333            w_WR(qRg)*x*(C1 + x*(C2 + x*C3));
334    return (yy);
335}
336
337
338static double
339Sexvnew(double q, double L, double b)
340{
341    double yy;
342
343    const double C1 =1.22;
344    const double C2 =0.4288;
345    const double C3 =-1.651;
346    const double miu = 0.585;
347    const double del=1.05;
348    const double qRg = q*sqrt(Rgsquare(q,L,b));
349    const double x = pow(qRg, -1.0/miu);
350
351
352    //calculating the derivative to decide on the corection (cutoff) term?
353    // I have modified this from WRs original code
354    const double qdel = (Sexv(q*del,L,b)-Sexv(q,L,b))/(q*del - q);
355    const double C_star2 =(qdel >= 0.0) ? 0.0 : 1.0;
356
357    yy = (1.0 - w_WR(qRg))*Sdebye(q,L,b) +
358         C_star2*w_WR(qRg)*
359         x*(C1 + x*(C2 + x*C3));
360
361    return (yy);
362}
363
364double Sk_WR(double q, double L, double b)
365{
366    //
367    const double p1 = 4.12;
368    const double p2 = 4.42;
369    const double p1short = 5.36;
370    const double p2short = 5.62;
371    const double q0 = 3.1;
372
373    double q0short = fmax(1.9/sqrt(Rgsquareshort(q,L,b)),3.0);
374    double Sexvmodify, ans;
375
376    const double C =(L/b > 10.0) ? 3.06/pow((L/b),0.44) : 1.0;
377
378    if( L > 4*b ) { // Longer Chains
379       if (q*b <= 3.1) {   //Modified by Yun on Oct. 15,
380         Sexvmodify = Sexvnew(q, L, b);
381         ans = Sexvmodify + C * (4.0/15.0 + 7.0/(15.0*u_WR(q,L,b)) -
382            (11.0/15.0 + 7.0/(15.0*u_WR(q,L,b)))*exp(-u_WR(q,L,b)))*(b/L);
383
384       } else { //q(i)*b > 3.1
385         ans = a1long(q, L, b, p1, p2, q0)/(pow((q*b),p1)) +
386               a2long(q, L, b, p1, p2, q0)/(pow((q*b),p2)) + M_PI/(q*L);
387       }
388    } else { //L <= 4*b Shorter Chains
389       if (q*b <= fmax(1.9/sqrt(Rgsquareshort(q,L,b)),3.0) ) {
390         if (q*b<=0.01) {
391            ans = 1.0 - Rgsquareshort(q,L,b)*(q*q)/3.0;
392         } else {
393            ans = Sdebye1(q,L,b);
394         }
395       } else {  //q*b > max(1.9/sqrt(Rgsquareshort(q(i),L,b)),3)
396         ans = a1short(q,L,b,p1short,p2short,q0short)/(pow((q*b),p1short)) +
397               a2short(q,L,b,p1short,p2short,q0short)/(pow((q*b),p2short)) +
398               M_PI/(q*L);
399       }
400    }
401
402  return(ans);
403}
Note: See TracBrowser for help on using the repository browser.