source: sasmodels/doc/guide/pd/polydispersity.rst @ 9ce5bcb

Last change on this file since 9ce5bcb was 9ce5bcb, checked in by GitHub <noreply@…>, 6 years ago

Update polydispersity.rst

Added how to activate the user-defined weight file.

  • Property mode set to 100644
File size: 14.1 KB
RevLine 
[990d8df]1.. pd_help.rst
2
3.. This is a port of the original SasView html help file to ReSTructured text
4.. by S King, ISIS, during SasView CodeCamp-III in Feb 2015.
5
6.. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
7
[eda8b30]8.. _polydispersityhelp:
9
[990d8df]10Polydispersity Distributions
11----------------------------
12
[eda8b30]13With some models in sasmodels we can calculate the average intensity for a
[990d8df]14population of particles that exhibit size and/or orientational
[eda8b30]15polydispersity. The resultant intensity is normalized by the average
[990d8df]16particle volume such that
17
18.. math::
19
20  P(q) = \text{scale} \langle F^* F \rangle / V + \text{background}
21
22where $F$ is the scattering amplitude and $\langle\cdot\rangle$ denotes an
23average over the size distribution.
24
[ed5b109]25Each distribution is characterized by a center value $\bar x$ or
26$x_\text{med}$, a width parameter $\sigma$ (note this is *not necessarily*
27the standard deviation, so read the description carefully), the number of
28sigmas $N_\sigma$ to include from the tails of the distribution, and the
29number of points used to compute the average. The center of the distribution
[aa25fc7]30is set by the value of the model parameter. The meaning of a polydispersity
31parameter *PD* (not to be confused with a molecular weight distributions
32in polymer science) in a model depends on the type of parameter it is being
[29afc50]33applied too.
[ed5b109]34
[aa25fc7]35The distribution width applied to *volume* (ie, shape-describing) parameters
36is relative to the center value such that $\sigma = \mathrm{PD} \cdot \bar x$.
37However, the distribution width applied to *orientation* (ie, angle-describing)
[29afc50]38parameters is just $\sigma = \mathrm{PD}$.
[ed5b109]39
40$N_\sigma$ determines how far into the tails to evaluate the distribution,
41with larger values of $N_\sigma$ required for heavier tailed distributions.
[990d8df]42The scattering in general falls rapidly with $qr$ so the usual assumption
43that $G(r - 3\sigma_r)$ is tiny and therefore $f(r - 3\sigma_r)G(r - 3\sigma_r)$
44will not contribute much to the average may not hold when particles are large.
45This, too, will require increasing $N_\sigma$.
46
47Users should note that the averaging computation is very intensive. Applying
48polydispersion to multiple parameters at the same time or increasing the
49number of points in the distribution will require patience! However, the
50calculations are generally more robust with more data points or more angles.
51
[22279a4]52The following distribution functions are provided:
[990d8df]53
[75e4319]54*  *Uniform Distribution*
[5026e05]55*  *Rectangular Distribution*
[990d8df]56*  *Gaussian Distribution*
[5026e05]57*  *Boltzmann Distribution*
[990d8df]58*  *Lognormal Distribution*
59*  *Schulz Distribution*
60*  *Array Distribution*
[0e04dd7]61*  *User-defined Distributions*
[990d8df]62
63These are all implemented as *number-average* distributions.
64
65
[5026e05]66Suggested Applications
67^^^^^^^^^^^^^^^^^^^^^^
[990d8df]68
[ed5b109]69If applying polydispersion to parameters describing particle sizes, use
[5026e05]70the Lognormal or Schulz distributions.
[990d8df]71
[ed5b109]72If applying polydispersion to parameters describing interfacial thicknesses
[5026e05]73or angular orientations, use the Gaussian or Boltzmann distributions.
[990d8df]74
[aa25fc7]75If applying polydispersion to parameters describing angles, use the Uniform
76distribution. Beware of using distributions that are always positive (eg, the
[29afc50]77Lognormal) because angles can be negative!
78
[0e04dd7]79The array distribution provides a very simple means of implementing a user-
80defined distribution, but without any fittable parameters. Greater flexibility
81is conferred by the user-defined distribution.
[990d8df]82
[5026e05]83.. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
[990d8df]84
[5026e05]85Uniform Distribution
86^^^^^^^^^^^^^^^^^^^^
[990d8df]87
[5026e05]88The Uniform Distribution is defined as
[990d8df]89
[f4ae8c4]90.. math::
[990d8df]91
[f4ae8c4]92    f(x) = \frac{1}{\text{Norm}}
93    \begin{cases}
94        1 & \text{for } |x - \bar x| \leq \sigma \\
95        0 & \text{for } |x - \bar x| > \sigma
96    \end{cases}
[990d8df]97
[f4ae8c4]98where $\bar x$ ($x_\text{mean}$ in the figure) is the mean of the
99distribution, $\sigma$ is the half-width, and *Norm* is a normalization
100factor which is determined during the numerical calculation.
[990d8df]101
[f4ae8c4]102The polydispersity in sasmodels is given by
[990d8df]103
[f4ae8c4]104.. math:: \text{PD} = \sigma / \bar x
[92d330fd]105
[f4ae8c4]106.. figure:: pd_uniform.jpg
[3d58247]107
[f4ae8c4]108    Uniform distribution.
[990d8df]109
[5026e05]110The value $N_\sigma$ is ignored for this distribution.
111
112.. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
113
114Rectangular Distribution
[75e4319]115^^^^^^^^^^^^^^^^^^^^^^^^
116
[5026e05]117The Rectangular Distribution is defined as
[75e4319]118
[f4ae8c4]119.. math::
[75e4319]120
[f4ae8c4]121    f(x) = \frac{1}{\text{Norm}}
122    \begin{cases}
123        1 & \text{for } |x - \bar x| \leq w \\
124        0 & \text{for } |x - \bar x| > w
125    \end{cases}
[75e4319]126
[f4ae8c4]127where $\bar x$ ($x_\text{mean}$ in the figure) is the mean of the
128distribution, $w$ is the half-width, and *Norm* is a normalization
129factor which is determined during the numerical calculation.
[75e4319]130
[f4ae8c4]131Note that the standard deviation and the half width $w$ are different!
[75e4319]132
[f4ae8c4]133The standard deviation is
[75e4319]134
[f4ae8c4]135.. math:: \sigma = w / \sqrt{3}
[75e4319]136
[f4ae8c4]137whilst the polydispersity in sasmodels is given by
[92d330fd]138
[f4ae8c4]139.. math:: \text{PD} = \sigma / \bar x
[5026e05]140
[f4ae8c4]141.. figure:: pd_rectangular.jpg
[5026e05]142
[f4ae8c4]143    Rectangular distribution.
[ed5b109]144
[f4ae8c4]145.. note:: The Rectangular Distribution is deprecated in favour of the
146            Uniform Distribution above and is described here for backwards
147            compatibility with earlier versions of SasView only.
[75e4319]148
[990d8df]149.. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
150
151Gaussian Distribution
152^^^^^^^^^^^^^^^^^^^^^
153
154The Gaussian Distribution is defined as
155
[f4ae8c4]156.. math::
[5026e05]157
[f4ae8c4]158    f(x) = \frac{1}{\text{Norm}}
159            \exp\left(-\frac{(x - \bar x)^2}{2\sigma^2}\right)
[990d8df]160
[f4ae8c4]161where $\bar x$ ($x_\text{mean}$ in the figure) is the mean of the
162distribution and *Norm* is a normalization factor which is determined
163during the numerical calculation.
[990d8df]164
[f4ae8c4]165The polydispersity in sasmodels is given by
[990d8df]166
[f4ae8c4]167.. math:: \text{PD} = \sigma / \bar x
[5026e05]168
[f4ae8c4]169.. figure:: pd_gaussian.jpg
[5026e05]170
[f4ae8c4]171    Normal distribution.
[5026e05]172
173.. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
174
175Boltzmann Distribution
176^^^^^^^^^^^^^^^^^^^^^^
177
178The Boltzmann Distribution is defined as
[990d8df]179
[f4ae8c4]180.. math::
[990d8df]181
[f4ae8c4]182    f(x) = \frac{1}{\text{Norm}}
183            \exp\left(-\frac{ | x - \bar x | }{\sigma}\right)
[990d8df]184
[f4ae8c4]185where $\bar x$ ($x_\text{mean}$ in the figure) is the mean of the
186distribution and *Norm* is a normalization factor which is determined
187during the numerical calculation.
[5026e05]188
[f4ae8c4]189The width is defined as
[5026e05]190
[f4ae8c4]191.. math:: \sigma=\frac{k T}{E}
[5026e05]192
[f4ae8c4]193which is the inverse Boltzmann factor, where $k$ is the Boltzmann constant,
194$T$ the temperature in Kelvin and $E$ a characteristic energy per particle.
[5026e05]195
[f4ae8c4]196.. figure:: pd_boltzmann.jpg
[5026e05]197
[f4ae8c4]198    Boltzmann distribution.
[990d8df]199
200.. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
201
202Lognormal Distribution
203^^^^^^^^^^^^^^^^^^^^^^
204
[ed5b109]205The Lognormal Distribution describes a function of $x$ where $\ln (x)$ has
206a normal distribution. The result is a distribution that is skewed towards
207larger values of $x$.
[5026e05]208
[990d8df]209The Lognormal Distribution is defined as
210
[f4ae8c4]211.. math::
[990d8df]212
[f4ae8c4]213    f(x) = \frac{1}{\text{Norm}}\frac{1}{x\sigma}
214            \exp\left(-\frac{1}{2}
215                        \bigg(\frac{\ln(x) - \mu}{\sigma}\bigg)^2\right)
[990d8df]216
[f4ae8c4]217where *Norm* is a normalization factor which will be determined during
218the numerical calculation, $\mu=\ln(x_\text{med})$ and $x_\text{med}$
219is the *median* value of the *lognormal* distribution, but $\sigma$ is
220a parameter describing the width of the underlying *normal* distribution.
[ed5b109]221
[f4ae8c4]222$x_\text{med}$ will be the value given for the respective size parameter
223in sasmodels, for example, *radius=60*.
[990d8df]224
[f4ae8c4]225The polydispersity in sasmodels is given by
[990d8df]226
[29afc50]227.. math:: \text{PD} = \sigma = p / x_\text{med}
[990d8df]228
[29afc50]229The mean value of the distribution is given by $\bar x = \exp(\mu+ \sigma^2/2)$
230and the peak value by $\max x = \exp(\mu - \sigma^2)$.
[990d8df]231
[f4ae8c4]232The variance (the square of the standard deviation) of the *lognormal*
233distribution is given by
[990d8df]234
[f4ae8c4]235.. math::
[990d8df]236
[f4ae8c4]237    \nu = [\exp({\sigma}^2) - 1] \exp({2\mu + \sigma^2})
[990d8df]238
[f4ae8c4]239Note that larger values of PD might need a larger number of points
240and $N_\sigma$.
[ed5b109]241
[f4ae8c4]242.. figure:: pd_lognormal.jpg
[990d8df]243
[29afc50]244    Lognormal distribution for PD=0.1.
[990d8df]245
[5026e05]246For further information on the Lognormal distribution see:
[ed5b109]247http://en.wikipedia.org/wiki/Log-normal_distribution and
[5026e05]248http://mathworld.wolfram.com/LogNormalDistribution.html
[990d8df]249
250.. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
251
252Schulz Distribution
253^^^^^^^^^^^^^^^^^^^
254
[ed5b109]255The Schulz (sometimes written Schultz) distribution is similar to the
256Lognormal distribution, in that it is also skewed towards larger values of
257$x$, but which has computational advantages over the Lognormal distribution.
[5026e05]258
[990d8df]259The Schulz distribution is defined as
260
[f4ae8c4]261.. math::
[990d8df]262
[f4ae8c4]263    f(x) = \frac{1}{\text{Norm}} (z+1)^{z+1}(x/\bar x)^z
264            \frac{\exp[-(z+1)x/\bar x]}{\bar x\Gamma(z+1)}
[990d8df]265
[f4ae8c4]266where $\bar x$ ($x_\text{mean}$ in the figure) is the mean of the
267distribution, *Norm* is a normalization factor which is determined
268during the numerical calculation, and $z$ is a measure of the width
269of the distribution such that
[990d8df]270
[f4ae8c4]271.. math:: z = (1-p^2) / p^2
[990d8df]272
[f4ae8c4]273where $p$ is the polydispersity in sasmodels given by
[990d8df]274
[f4ae8c4]275.. math:: PD = p = \sigma / \bar x
[990d8df]276
[f4ae8c4]277and $\sigma$ is the RMS deviation from $\bar x$.
[ed5b109]278
[f4ae8c4]279Note that larger values of PD might need a larger number of points
280and $N_\sigma$. For example, for PD=0.7 with radius=60 |Ang|, at least
281Npts>=160 and Nsigmas>=15 are required.
[990d8df]282
[f4ae8c4]283.. figure:: pd_schulz.jpg
[990d8df]284
[f4ae8c4]285    Schulz distribution.
[990d8df]286
287For further information on the Schulz distribution see:
[5026e05]288M Kotlarchyk & S-H Chen, *J Chem Phys*, (1983), 79, 2461 and
[ed5b109]289M Kotlarchyk, RB Stephens, and JS Huang, *J Phys Chem*, (1988), 92, 1533
[990d8df]290
291.. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
292
293Array Distribution
294^^^^^^^^^^^^^^^^^^
295
[a5a12ca]296This user-definable distribution should be given as a simple ASCII text
[990d8df]297file where the array is defined by two columns of numbers: $x$ and $f(x)$.
298The $f(x)$ will be normalized to 1 during the computation.
299
300Example of what an array distribution file should look like:
301
302====  =====
303 30    0.1
304 32    0.3
305 35    0.4
306 36    0.5
307 37    0.6
308 39    0.7
309 41    0.9
310====  =====
311
312Only these array values are used computation, therefore the parameter value
313given for the model will have no affect, and will be ignored when computing
314the average.  This means that any parameter with an array distribution will
[a5a12ca]315not be fitable.
316
317.. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
318
[aa25fc7]319User-defined Distributions
320^^^^^^^^^^^^^^^^^^^^^^^^^^
321
[0e04dd7]322You can also define your own distribution by creating a python file defining a
[36873d1]323*Distribution* object with a *_weights* method.  The *_weights* method takes
324*center*, *sigma*, *lb* and *ub* as arguments, and can access *self.npts*
325and *self.nsigmas* from the distribution.  They are interpreted as follows:
[aa25fc7]326
[36873d1]327* *center* the value of the shape parameter (for size dispersity) or zero
328  if it is an angular dispersity.  This parameter may be fitted.
329
[0e04dd7]330* *sigma* the width of the distribution, which is the polydispersity parameter
[36873d1]331  times the center for size dispersity, or the polydispersity parameter alone
332  for angular dispersity.  This parameter may be fitted.
333
[0e04dd7]334* *lb*, *ub* are the parameter limits (lower & upper bounds) given in the model
335  definition file.  For example, a radius parameter has *lb* equal to zero.  A
336  volume fraction parameter would have *lb* equal to zero and *ub* equal to one.
[36873d1]337
338* *self.nsigmas* the distance to go into the tails when evaluating the
339  distribution.  For a two parameter distribution, this value could be
340  co-opted to use for the second parameter, though it will not be available
341  for fitting.
342
343* *self.npts* the number of points to use when evaluating the distribution.
344  The user will adjust this to trade calculation time for accuracy, but the
345  distribution code is free to return more or fewer, or use it for the third
346  parameter in a three parameter distribution.
347
[0e04dd7]348As an example, the code following wraps the Laplace distribution from scipy stats::
[aa25fc7]349
350    import numpy as np
351    from scipy.stats import laplace
352
353    from sasmodels import weights
354
355    class Dispersion(weights.Dispersion):
356        r"""
357        Laplace distribution
358
359        .. math::
360
361            w(x) = e^{-\sigma |x - \mu|}
362        """
363        type = "laplace"
364        default = dict(npts=35, width=0, nsigmas=3)  # default values
365        def _weights(self, center, sigma, lb, ub):
366            x = self._linspace(center, sigma, lb, ub)
367            wx = laplace.pdf(x, center, sigma)
368            return x, wx
369
[36873d1]370You can plot the weights for a given value and width using the following::
[aa25fc7]371
372    from numpy import inf
373    from matplotlib import pyplot as plt
374    from sasmodels import weights
375
376    # reload the user-defined weights
377    weights.load_weights()
378    x, wx = weights.get_weights('laplace', n=35, width=0.1, nsigmas=3, value=50,
379                                limits=[0, inf], relative=True)
380
381    # plot the weights
382    plt.interactive(True)
383    plt.plot(x, wx, 'x')
384
[36873d1]385The *self.nsigmas* and *self.npts* parameters are normally used to control
386the accuracy of the distribution integral. The *self._linspace* function
387uses them to define the *x* values (along with the *center*, *sigma*,
388*lb*, and *ub* which are passed as parameters).  If you repurpose npts or
389nsigmas you will need to generate your own *x*.  Be sure to honour the
390limits *lb* and *ub*, for example to disallow a negative radius or constrain
391the volume fraction to lie between zero and one.
[aa25fc7]392
[9ce5bcb]393To activate a user-defined distribution, set the following environment variable:
394
395    SASMODELS_WEIGHTS=path/to/folder/name_of_distribution.py
396
[aa25fc7]397.. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
398
[990d8df]399Note about DLS polydispersity
400^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
401
402Many commercial Dynamic Light Scattering (DLS) instruments produce a size
[1f058ea]403polydispersity parameter, sometimes even given the symbol $p$\ ! This
[990d8df]404parameter is defined as the relative standard deviation coefficient of
405variation of the size distribution and is NOT the same as the polydispersity
406parameters in the Lognormal and Schulz distributions above (though they all
407related) except when the DLS polydispersity parameter is <0.13.
408
[5026e05]409.. math::
410
411    p_{DLS} = \sqrt(\nu / \bar x^2)
412
[ed5b109]413where $\nu$ is the variance of the distribution and $\bar x$ is the mean
[f4ae8c4]414value of $x$.
[5026e05]415
[990d8df]416For more information see:
417S King, C Washington & R Heenan, *Phys Chem Chem Phys*, (2005), 7, 143
418
419.. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
420
421*Document History*
422
423| 2015-05-01 Steve King
424| 2017-05-08 Paul Kienzle
[5026e05]425| 2018-03-20 Steve King
[29afc50]426| 2018-04-04 Steve King
Note: See TracBrowser for help on using the repository browser.