Changeset d4c33d6 in sasmodels for sasmodels/details.py


Ignore:
Timestamp:
Apr 12, 2017 10:50:29 AM (7 years ago)
Author:
Paul Kienzle <pkienzle@…>
Branches:
master, core_shell_microgels, magnetic_model, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
Children:
597878a
Parents:
535fee6
Message:

send cos-weighted theta values to kernel rather than computing them inside

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/details.py

    rccd5f01 rd4c33d6  
    1515 
    1616import numpy as np  # type: ignore 
    17 from numpy import pi, cos, sin 
     17from numpy import pi, cos, sin, radians 
    1818 
    1919try: 
     
    2929 
    3030try: 
    31     from typing import List 
     31    from typing import List, Tuple, Sequence 
    3232except ImportError: 
    3333    pass 
    3434else: 
    3535    from .modelinfo import ModelInfo 
     36    from .modelinfo import ParameterTable 
    3637 
    3738 
     
    5354    coordinates, the total circumference decreases as latitude varies from 
    5455    pi r^2 at the equator to 0 at the pole, and the weight associated 
    55     with a range of phi values needs to be scaled by this circumference. 
     56    with a range of latitude values needs to be scaled by this circumference. 
    5657    This scale factor needs to be updated each time the theta value 
    5758    changes.  *theta_par* indicates which of the values in the parameter 
     
    231232    nvalues = kernel.info.parameters.nvalues 
    232233    scalars = [(v[0] if len(v) else np.NaN) for v, w in pairs] 
    233     values, weights = zip(*pairs[2:npars+2]) if npars else ((),()) 
     234    values, weights = zip(*pairs[2:npars+2]) if npars else ((), ()) 
     235    weights = correct_theta_weights(kernel.info.parameters, values, weights) 
    234236    length = np.array([len(w) for w in weights]) 
    235237    offset = np.cumsum(np.hstack((0, length))) 
     
    244246    return call_details, data, is_magnetic 
    245247 
     248def correct_theta_weights(parameters, values, weights): 
     249    # type: (ParameterTable, Sequence[np.ndarray], Sequence[np.ndarray]) -> Sequence[np.ndarray] 
     250    """ 
     251    If there is a theta parameter, update the weights of that parameter so that 
     252    the cosine weighting required for polar integration is preserved.  Avoid 
     253    evaluation strictly at the pole, which would otherwise send the weight to 
     254    zero. 
     255    """ 
     256    if parameters.theta_offset >= 0: 
     257        index = parameters.theta_offset+len(parameters.COMMON) 
     258        theta = values[index] 
     259        theta_weight = np.minimum(cos(radians(theta)), 1e-6) 
     260        # copy the weights list so we can update it 
     261        weights = list(weights) 
     262        weights[index] = theta_weight*weights[index] 
     263        weights = tuple(weights) 
     264    return weights 
     265 
    246266 
    247267def convert_magnetism(parameters, values): 
     268    # type: (ParameterTable, Sequence[np.ndarray]) -> bool 
    248269    """ 
    249270    Convert magnetism values from polar to rectangular coordinates. 
     
    255276    scale = mag[:,0] 
    256277    if np.any(scale): 
    257         theta, phi = mag[:, 1]*pi/180., mag[:, 2]*pi/180. 
     278        theta, phi = radians(mag[:, 1]), radians(mag[:, 2]) 
    258279        cos_theta = cos(theta) 
    259280        mag[:, 0] = scale*cos_theta*cos(phi)  # mx 
     
    269290    """ 
    270291    Create a mesh grid of dispersion parameters and weights. 
     292 
     293    pars is a list of pairs (values, weights), where the values are the 
     294    individual parameter values at which to evaluate the polydispersity 
     295    distribution and weights are the weights associated with each value. 
     296 
     297    Only the volume parameters should be included in this list.  Orientation 
     298    parameters do not affect the calculation of effective radius or volume 
     299    ratio. 
    271300 
    272301    Returns [p1,p2,...],w where pj is a vector of values for parameter j 
Note: See TracChangeset for help on using the changeset viewer.