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

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since e7678b2 was e7678b2, checked in by piotr, 8 years ago

Code review from PAK

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