Changeset 97e6d3c in sasmodels for sasmodels/models/hardsphere.py


Ignore:
Timestamp:
Feb 26, 2016 10:22:00 AM (8 years ago)
Author:
richardh
Branches:
master, core_shell_microgels, costrafo411, magnetic_model, release_v0.94, release_v0.95, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
Children:
e4d8726
Parents:
093f754
Message:

made hardsphere faster

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/models/hardsphere.py

    r093f754 r97e6d3c  
    6969               return(HARDSPH); 
    7070      } 
    71       D=pow((1.-volfraction),2); 
    72       A=pow((1.+2.*volfraction)/D, 2); 
     71      // 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) 
     72      X = 1.0/( 1.0 -volfraction); 
     73      D= X*X; 
     74      A= (1.+2.*volfraction)*D; 
     75      A *=A; 
    7376      X=fabs(q*effect_radius*2.0); 
    7477 
     
    7780                 return(HARDSPH); 
    7881      } 
    79       X2=pow(X,2); 
    80       X4=pow(X2,2); 
    81       B=-6.*volfraction* pow((1.+0.5*volfraction)/D ,2); 
     82      X2 =X*X; 
     83      B = (1.0 +0.5*volfraction)*D; 
     84      B *= B; 
     85      B *= -6.*volfraction; 
    8286      G=0.5*volfraction*A; 
    8387 
    8488      if(X < 0.2) { 
    85       // use Taylor series expansion for small X, IT IS VERY PICKY ABOUT THE X CUT OFF VALUE, ought to be lower in double.  
    86       // No obvious way to rearrange the equations to avoid needing a very high number of significant figures.  
     89      // 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.  
     90      // else no obvious way to rearrange the equations to avoid needing a very high number of significant figures.  
    8791      // Series expansion found using Mathematica software. Numerical test in .xls showed terms to X^2 are sufficient  
    88       // for 5 or 6 significant figures but I put the X^4 one in anyway  
    89             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; 
     92      // for 5 or 6 significant figures, but I put the X^4 one in anyway  
     93            //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; 
     94            // refactoring the polynomial makes it very slightly faster (0.5 not 0.6 msec) 
     95            //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; 
     96 
     97            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; 
     98 
    9099            // combining the terms makes things worse at smallest Q in single precision 
    91100            //FF = (8-0.8*X2)*A +(3.0-X2/3.)*2*B + (4+0.5*X2)*G +(A/35. +B/40. +G/50.)*X4; 
    92101            // note that G = -volfraction*A/2, combining this makes no further difference at smallest Q 
    93             //FF = (8 +2.*volfraction + ( volfraction/4. -0.8 +(volfraction/100. -1./35.)*X2 )*X2 )*A  + (3.0 -X2/3. +X4/40)*2*B; 
     102            //FF = (8 +2.*volfraction + ( volfraction/4. -0.8 +(volfraction/100. -1./35.)*X2 )*X2 )*A  + (3.0 -X2/3. +X4/40.)*2.*B; 
    94103            HARDSPH= 1./(1. + volfraction*FF ); 
    95104            return(HARDSPH); 
    96105      } 
     106      X4=X2*X2; 
    97107      SINCOS(X,S,C); 
    98108 
    99 // RKH Feb 2016, use version from FISH code as it is better than original sasview one at small Q in single precision 
    100       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; 
     109// 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. 
     110      //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; 
     111      // refactoring the polynomial here & above makes it slightly faster 
     112 
     113      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 ; 
    101114      HARDSPH= 1./(1. + 24.*volfraction*FF/X2 ); 
    102115 
    103 // rearrange the terms, is now about same as sasmodels 
     116      // changing /X and /X2 to *MX1 and *MX2, no significantg difference? 
     117      //MX=1.0/X; 
     118      //MX2=MX*MX; 
     119      //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)) ; 
     120      //HARDSPH= 1./(1. + 24.*volfraction*FF*MX2*MX ); 
     121 
     122// grouping the terms, was about same as sasmodels for single precision issues 
    104123//     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 ); 
    105124//     HARDSPH= 1./(1. + 24.*volfraction*FF/X2 ); 
Note: See TracChangeset for help on using the changeset viewer.