source: sasmodels/sasmodels/models/hardsphere.py @ 8e45182

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

adding structure_factor flag to hard sphere an sticky hard sphere

  • Property mode set to 100644
File size: 5.6 KB
Line 
1# Note: model title and parameter table are inserted automatically
2r"""Calculate the interparticle structure factor for monodisperse
3spherical particles interacting through hard sphere (excluded volume)
4interactions.
5
6The calculation uses the Percus-Yevick closure where the interparticle
7potential is
8
9.. math::
10
11    U(r) = \begin{cases}
12    \infty & r < 2R \\
13    0 & r \geq 2R
14    \end{cases}
15
16where $r$ is the distance from the center of the sphere of a radius $R$.
17
18For a 2D plot, the wave transfer is defined as
19
20.. math::
21
22    q = \sqrt{q_x^2 + q_y^2}
23
24
25.. figure:: img/hardSphere_1d.jpg
26
27    1D plot using the default values (in linear scale).
28
29References
30----------
31
32J K Percus, J Yevick, *J. Phys. Rev.*, 110, (1958) 1
33"""
34
35from numpy import inf
36
37name = "hardsphere"
38title = "Hard sphere structure factor, with Percus-Yevick closure"
39description = """\
40    [Hard sphere structure factor, with Percus-Yevick closure]
41        Interparticle S(Q) for random, non-interacting spheres.
42    May be a reasonable approximation for other shapes of
43    particles that freely rotate, and for moderately polydisperse
44        systems. Though strictly the maths needs to be modified -
45    which sasview does not do yet.
46    effect_radius is the hard sphere radius
47    volfraction is the volume fraction occupied by the spheres.
48"""
49category = "structure-factor"
50structure_factor = True
51
52#             ["name", "units", default, [lower, upper], "type","description"],
53parameters = [["effect_radius", "Ang", 50.0, [0, inf], "volume",
54               "effective radius of hard sphere"],
55              ["volfraction", "", 0.2, [0, 0.74], "",
56               "volume fraction of hard spheres"],
57             ]
58
59# No volume normalization despite having a volume parameter
60# This should perhaps be volume normalized?
61form_volume = """
62    return 1.0;
63    """
64
65Iq = """
66      double D,A,B,G,X,X2,X4,S,C,FF,HARDSPH;
67
68      if(fabs(effect_radius) < 1.E-12) {
69               HARDSPH=1.0;
70               return(HARDSPH);
71      }
72      // removing use of pow(xxx,2) and rearranging the calcs of A, B & G cut ~40% off execution time ( 0.5 to 0.3 msec)
73      X = 1.0/( 1.0 -volfraction);
74      D= X*X;
75      A= (1.+2.*volfraction)*D;
76      A *=A;
77      X=fabs(q*effect_radius*2.0);
78
79      if(X < 5.E-06) {
80                 HARDSPH=1./A;
81                 return(HARDSPH);
82      }
83      X2 =X*X;
84      B = (1.0 +0.5*volfraction)*D;
85      B *= B;
86      B *= -6.*volfraction;
87      G=0.5*volfraction*A;
88
89      if(X < 0.2) {
90      // RKH Feb 2016, use Taylor series expansion for small X, IT IS VERY PICKY ABOUT THE X CUT OFF VALUE, ought to be lower in double.
91      // else no obvious way to rearrange the equations to avoid needing a very high number of significant figures.
92      // Series expansion found using Mathematica software. Numerical test in .xls showed terms to X^2 are sufficient
93      // for 5 or 6 significant figures, but I put the X^4 one in anyway
94            //FF = 8*A +6*B + 4*G - (0.8*A +2.0*B/3.0 +0.5*G)*X2 +(A/35. +B/40. +G/50.)*X4;
95            // refactoring the polynomial makes it very slightly faster (0.5 not 0.6 msec)
96            //FF = 8*A +6*B + 4*G + ( -0.8*A -2.0*B/3.0 -0.5*G +(A/35. +B/40. +G/50.)*X2)*X2;
97
98            FF = 8.0*A +6.0*B + 4.0*G + ( -0.8*A -B/1.5 -0.5*G +(A/35. +0.0125*B +0.02*G)*X2)*X2;
99
100            // combining the terms makes things worse at smallest Q in single precision
101            //FF = (8-0.8*X2)*A +(3.0-X2/3.)*2*B + (4+0.5*X2)*G +(A/35. +B/40. +G/50.)*X4;
102            // note that G = -volfraction*A/2, combining this makes no further difference at smallest Q
103            //FF = (8 +2.*volfraction + ( volfraction/4. -0.8 +(volfraction/100. -1./35.)*X2 )*X2 )*A  + (3.0 -X2/3. +X4/40.)*2.*B;
104            HARDSPH= 1./(1. + volfraction*FF );
105            return(HARDSPH);
106      }
107      X4=X2*X2;
108      SINCOS(X,S,C);
109
110// RKH Feb 2016, use version FISH code as is better than original sasview one at small Q in single precision, and more than twice as fast in double.
111      //FF=A*(S-X*C)/X + B*(2.*X*S -(X2-2.)*C -2.)/X2 + G*( (4.*X2*X -24.*X)*S -(X4 -12.*X2 +24.)*C +24. )/X4;
112      // refactoring the polynomial here & above makes it slightly faster
113
114      FF=  (( G*( (4.*X2 -24.)*X*S -(X4 -12.*X2 +24.)*C +24. )/X2 + B*(2.*X*S -(X2-2.)*C -2.) )/X + A*(S-X*C))/X ;
115      HARDSPH= 1./(1. + 24.*volfraction*FF/X2 );
116
117      // changing /X and /X2 to *MX1 and *MX2, no significantg difference?
118      //MX=1.0/X;
119      //MX2=MX*MX;
120      //FF=  (( G*( (4.*X2 -24.)*X*S -(X4 -12.*X2 +24.)*C +24. )*MX2 + B*(2.*X*S -(X2-2.)*C -2.) )*MX + A*(S-X*C)) ;
121      //HARDSPH= 1./(1. + 24.*volfraction*FF*MX2*MX );
122
123// grouping the terms, was about same as sasmodels for single precision issues
124//     FF=A*(S/X-C) + B*(2.*S/X - C +2.0*(C-1.0)/X2) + G*( (4./X -24./X3)*S -(1.0 -12./X2 +24./X4)*C +24./X4 );
125//     HARDSPH= 1./(1. + 24.*volfraction*FF/X2 );
126// remove 1/X2 from final line, take more powers of X inside the brackets, stil bad
127//      FF=A*(S/X3-C/X2) + B*(2.*S/X3 - C/X2 +2.0*(C-1.0)/X4) + G*( (4./X -24./X3)*S -(1.0 -12./X2 +24./X4)*C +24./X4 )/X2;
128//      HARDSPH= 1./(1. + 24.*volfraction*FF );
129      return(HARDSPH);
130   """
131
132Iqxy = """
133    // never called since no orientation or magnetic parameters.
134    return Iq(sqrt(qx*qx+qy*qy), IQ_PARAMETERS);
135    """
136
137# ER defaults to 0.0
138# VR defaults to 1.0
139
140demo = dict(effect_radius=200, volfraction=0.2, effect_radius_pd=0.1, effect_radius_pd_n=40)
141oldname = 'HardsphereStructure'
142oldpars = dict()
143# Q=0.001 is in the Taylor series, low Q part, so add Q=0.1, assuming double precision sasview is correct
144tests = [
145        [ {'scale': 1.0, 'background' : 0.0, 'effect_radius' : 50.0, 'volfraction' : 0.2,
146           'effect_radius_pd' : 0}, [0.001,0.1], [0.209128,0.930587]]
147        ]
148
Note: See TracBrowser for help on using the repository browser.