Ignore:
Timestamp:
Oct 18, 2016 11:34:18 AM (8 years ago)
Author:
Paul Kienzle <pkienzle@…>
Branches:
master, core_shell_microgels, costrafo411, magnetic_model, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
Children:
ed23abe
Parents:
251f54b
Message:

hollow_rectangular_prism: code cleanup, and 2x faster with precomputed trig.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/models/hollow_rectangular_prism.c

    rab2aea8 r6f676fb  
    2424    double thickness) 
    2525{ 
    26     double termA1, termA2, termB1, termB2, termC1, termC2; 
    27      
    28     double length_b = length_a * b2a_ratio; 
    29     double length_c = length_a * c2a_ratio; 
    30     double a_half = 0.5 * length_a; 
    31     double b_half = 0.5 * length_b; 
    32     double c_half = 0.5 * length_c; 
    33     double vol_total = length_a * length_b * length_c; 
    34     double vol_core = 8.0 * (a_half-thickness) * (b_half-thickness) * (c_half-thickness); 
     26    const double length_b = length_a * b2a_ratio; 
     27    const double length_c = length_a * c2a_ratio; 
     28    const double a_half = 0.5 * length_a; 
     29    const double b_half = 0.5 * length_b; 
     30    const double c_half = 0.5 * length_c; 
     31    const double vol_total = length_a * length_b * length_c; 
     32    const double vol_core = 8.0 * (a_half-thickness) * (b_half-thickness) * (c_half-thickness); 
    3533 
    3634    //Integration limits to use in Gaussian quadrature 
    37     double v1a = 0.0; 
    38     double v1b = M_PI_2;  //theta integration limits 
    39     double v2a = 0.0; 
    40     double v2b = M_PI_2;  //phi integration limits 
     35    const double v1a = 0.0; 
     36    const double v1b = M_PI_2;  //theta integration limits 
     37    const double v2a = 0.0; 
     38    const double v2b = M_PI_2;  //phi integration limits 
    4139     
    4240    double outer_sum = 0.0; 
    43      
    4441    for(int i=0; i<76; i++) { 
    4542 
    46         double theta = 0.5 * ( Gauss76Z[i]*(v1b-v1a) + v1a + v1b );     
     43        const double theta = 0.5 * ( Gauss76Z[i]*(v1b-v1a) + v1a + v1b ); 
     44        double sin_theta, cos_theta; 
     45        SINCOS(theta, sin_theta, cos_theta); 
    4746 
    48         double termC1 = sinc(q * c_half * cos(theta)); 
    49         double termC2 = sinc(q * (c_half-thickness)*cos(theta)); 
     47        const double termC1 = sinc(q * c_half * cos(theta)); 
     48        const double termC2 = sinc(q * (c_half-thickness)*cos(theta)); 
    5049 
    5150        double inner_sum = 0.0; 
    52          
    5351        for(int j=0; j<76; j++) { 
    5452 
    55             double phi = 0.5 * ( Gauss76Z[j]*(v2b-v2a) + v2a + v2b );  
     53            const double phi = 0.5 * ( Gauss76Z[j]*(v2b-v2a) + v2a + v2b ); 
     54            double sin_phi, cos_phi; 
     55            SINCOS(phi, sin_phi, cos_phi); 
    5656 
    5757            // Amplitude AP from eqn. (13), rewritten to avoid round-off effects when arg=0 
    5858 
    59             termA1 = sinc(q * a_half * sin(theta) * sin(phi)); 
    60             termA2 = sinc(q * (a_half-thickness) * sin(theta) * sin(phi)); 
     59            const double termA1 = sinc(q * a_half * sin_theta * sin_phi); 
     60            const double termA2 = sinc(q * (a_half-thickness) * sin_theta * sin_phi); 
    6161 
    62             termB1 = sinc(q * b_half * sin(theta) * cos(phi)); 
    63             termB2 = sinc(q * (b_half-thickness) * sin(theta) * cos(phi)); 
     62            const double termB1 = sinc(q * b_half * sin_theta * cos_phi); 
     63            const double termB2 = sinc(q * (b_half-thickness) * sin_theta * cos_phi); 
    6464 
    65             double AP1 = vol_total * termA1 * termB1 * termC1; 
    66             double AP2 = vol_core * termA2 * termB2 * termC2; 
     65            const double AP1 = vol_total * termA1 * termB1 * termC1; 
     66            const double AP2 = vol_core * termA2 * termB2 * termC2; 
    6767 
    6868            inner_sum += Gauss76Wt[j] * square(AP1-AP2); 
     69        } 
     70        inner_sum *= 0.5 * (v2b-v2a); 
    6971 
    70         } 
    71  
    72         inner_sum = 0.5 * (v2b-v2a) * inner_sum; 
    7372        outer_sum += Gauss76Wt[i] * inner_sum * sin(theta); 
    74  
    7573    } 
    76  
    77     double answer = 0.5*(v1b-v1a)*outer_sum; 
     74    outer_sum *= 0.5*(v1b-v1a); 
    7875 
    7976    // Normalize as in Eqn. (15) without the volume factor (as cancels with (V*DelRho)^2 normalization) 
    8077    // The factor 2 is due to the different theta integration limit (pi/2 instead of pi) 
    81     answer /= M_PI_2; 
     78    const double form = outer_sum/M_PI_2; 
    8279 
    8380    // Multiply by contrast^2. Factor corresponding to volume^2 cancels with previous normalization. 
    84     answer *= square(sld-solvent_sld); 
     81    const double delrho = sld - solvent_sld; 
    8582 
    8683    // Convert from [1e-12 A-1] to [cm-1] 
    87     answer *= 1.0e-4; 
    88  
    89     return answer; 
    90      
     84    return 1.0e-4 * delrho * delrho * form; 
    9185} 
Note: See TracChangeset for help on using the changeset viewer.