Changes in / [fa70e04:26a6608] in sasmodels


Ignore:
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • doc/guide/pd/polydispersity.rst

    r1f058ea r75e4319  
    4040calculations are generally more robust with more data points or more angles. 
    4141 
    42 The following five distribution functions are provided: 
     42The following six distribution functions are provided: 
    4343 
    4444*  *Rectangular Distribution* 
     45*  *Uniform Distribution* 
    4546*  *Gaussian Distribution* 
    4647*  *Lognormal Distribution* 
    4748*  *Schulz Distribution* 
    4849*  *Array Distribution* 
     50*  *Boltzmann Distribution* 
    4951 
    5052These are all implemented as *number-average* distributions. 
     
    8385    Rectangular distribution. 
    8486 
     87Uniform Distribution 
     88^^^^^^^^^^^^^^^^^^^^^^^^ 
     89 
     90The Uniform Distribution is defined as 
     91 
     92    .. math:: 
     93 
     94        f(x) = \frac{1}{\text{Norm}} 
     95        \begin{cases} 
     96          1 & \text{for } |x - \bar x| \leq \sigma \\ 
     97          0 & \text{for } |x - \bar x| > \sigma 
     98        \end{cases} 
     99 
     100    where $\bar x$ is the mean of the distribution, $\sigma$ is the half-width, and 
     101    *Norm* is a normalization factor which is determined during the numerical 
     102    calculation. 
     103 
     104    Note that the polydispersity is given by 
     105 
     106    .. math:: \text{PD} = \sigma / \bar x 
     107 
     108    .. figure:: pd_uniform.jpg 
     109 
     110        Uniform distribution. 
     111 
    85112.. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ 
    86113 
     
    181208^^^^^^^^^^^^^^^^^^ 
    182209 
    183 This user-definable distribution should be given as as a simple ASCII text 
     210This user-definable distribution should be given as a simple ASCII text 
    184211file where the array is defined by two columns of numbers: $x$ and $f(x)$. 
    185212The $f(x)$ will be normalized to 1 during the computation. 
     
    200227given for the model will have no affect, and will be ignored when computing 
    201228the average.  This means that any parameter with an array distribution will 
    202 not be fittable. 
     229not be fitable. 
     230 
     231.. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ 
     232 
     233Boltzmann Distribution 
     234^^^^^^^^^^^^^^^^^^^^^^ 
     235 
     236The Boltzmann Distribution is defined as 
     237 
     238.. math:: 
     239 
     240    f(x) = \frac{1}{\text{Norm}} 
     241           \exp\left(-\frac{ | x - \bar x | }{\sigma}\right) 
     242 
     243where $\bar x$ is the mean of the distribution and *Norm* is a normalization 
     244factor which is determined during the numerical calculation. 
     245The width is defined as 
     246 
     247.. math:: \sigma=\frac{k T}{E} 
     248 
     249which is the inverse Boltzmann factor, 
     250where $k$ is the Boltzmann constant, $T$ the temperature in Kelvin and $E$ a 
     251characteristic energy per particle. 
     252 
     253.. figure:: pd_boltzmann.jpg 
     254 
     255    Boltzmann distribution. 
    203256 
    204257.. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ 
  • sasmodels/weights.py

    rf1a8811 r75e4319  
    5555        """ 
    5656        sigma = self.width * center if relative else self.width 
     57        if not relative: 
     58            # For orientation, the jitter is relative to 0 not the angle 
     59            center = 0 
     60            pass 
    5761        if sigma == 0 or self.npts < 2: 
    5862            if lb <= center <= ub: 
     
    9397        return x, px 
    9498 
     99class UniformDispersion(Dispersion): 
     100    r""" 
     101    Uniform dispersion, with width $\sigma$. 
     102 
     103    .. math:: 
     104 
     105        w = 1 
     106    """ 
     107    type = "uniform" 
     108    default = dict(npts=35, width=0, nsigmas=1) 
     109    def _weights(self, center, sigma, lb, ub): 
     110        x = self._linspace(center, sigma, lb, ub) 
     111        x = x[np.fabs(x-center) <= np.fabs(sigma)] 
     112        return x, np.ones_like(x) 
    95113 
    96114class RectangleDispersion(Dispersion): 
     
    186204        return x, px 
    187205 
     206class BoltzmannDispersion(Dispersion): 
     207    r""" 
     208    Boltzmann dispersion, with $\sigma=k T/E$. 
     209 
     210    .. math:: 
     211 
     212        w = \exp\left( -|x - c|/\sigma\right) 
     213    """ 
     214    type = "boltzmann" 
     215    default = dict(npts=35, width=0, nsigmas=3) 
     216    def _weights(self, center, sigma, lb, ub): 
     217        x = self._linspace(center, sigma, lb, ub) 
     218        px = np.exp(-np.abs(x-center) / np.abs(sigma)) 
     219        return x, px 
    188220 
    189221# dispersion name -> disperser lookup table. 
     
    192224MODELS = OrderedDict((d.type, d) for d in ( 
    193225    RectangleDispersion, 
     226    UniformDispersion, 
    194227    ArrayDispersion, 
    195228    LogNormalDispersion, 
    196229    GaussianDispersion, 
    197230    SchulzDispersion, 
     231    BoltzmannDispersion 
    198232)) 
    199233 
     
    225259    obj = cls(n, width, nsigmas) 
    226260    v, w = obj.get_weights(value, limits[0], limits[1], relative) 
    227     return v, w 
    228  
    229  
    230 def plot_weights(model_info, pairs): 
    231     # type: (ModelInfo, List[Tuple[np.ndarray, np.ndarray]]) -> None 
     261    return v, w/np.sum(w) 
     262 
     263 
     264def plot_weights(model_info, mesh): 
     265    # type: (ModelInfo, List[Tuple[float, np.ndarray, np.ndarray]]) -> None 
    232266    """ 
    233267    Plot the weights returned by :func:`get_weights`. 
    234268 
    235     *model_info* is 
    236     :param model_info: 
    237     :param pairs: 
    238     :return: 
     269    *model_info* defines model parameters, etc. 
     270 
     271    *mesh* is a list of tuples containing (*value*, *dispersity*, *weights*) 
     272    for each parameter, where (*dispersity*, *weights*) pairs are the 
     273    distributions to be plotted. 
    239274    """ 
    240275    import pylab 
    241276 
    242     if any(len(values)>1 for values, weights in pairs): 
     277    if any(len(dispersity)>1 for value, dispersity, weights in mesh): 
    243278        labels = [p.name for p in model_info.parameters.call_parameters] 
    244         pylab.interactive(True) 
     279        #pylab.interactive(True) 
    245280        pylab.figure() 
    246         for (v,w), s in zip(pairs, labels): 
    247             if len(v) > 1: 
    248                 #print("weights for", s, v, w) 
    249                 pylab.plot(v, w, '-o', label=s) 
     281        for (v,x,w), s in zip(mesh, labels): 
     282            if len(x) > 1: 
     283                pylab.plot(x, w, '-o', label=s) 
    250284        pylab.grid(True) 
    251285        pylab.legend() 
Note: See TracChangeset for help on using the changeset viewer.