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

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since ba32cdd was ba32cdd, checked in by Paul Kienzle <pkienzle@…>, 8 years ago

fix opencl compile; still doesn't run

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