source: sasmodels/sasmodels/models/line.py

Last change on this file was c1e44e5, checked in by Paul Kienzle <pkienzle@…>, 5 years ago

Add local link to source files. Refs #1263.

  • Property mode set to 100644
File size: 3.0 KB
Line 
1r"""
2This model calculates intensity using simple linear function
3
4Definition
5----------
6
7The scattering intensity $I(q)$ is calculated as
8
9.. math::
10
11    I(q) = \text{scale} (A + B \cdot q) + \text{background}
12
13.. note::
14    For 2D plots intensity has different definition than other shape independent models
15
16.. math::
17
18    I(q) = \text{scale} (I(qx) \cdot I(qy)) + \text{background}
19
20References
21----------
22
23None.
24
25Authorship and Verification
26----------------------------
27
28* **Author:**
29* **Last Modified by:**
30* **Last Reviewed by:**
31"""
32
33import numpy as np
34from numpy import inf
35
36name = "line"
37title = "Line model"
38description = """\
39      I(q) = A + B*q
40
41      List of default parameters:
42      A = intercept
43      B = slope
44      """
45category = "shape-independent"
46
47# pylint: disable=bad-whitespace, line-too-long
48#             ["name", "units", default, [lower, upper], "type", "description"],
49parameters = [["intercept", "1/cm",   1.0, [-inf, inf], "", "intercept in linear model"],
50              ["slope",     "Ang/cm", 1.0, [-inf, inf], "", "slope in linear model"],
51             ]
52# pylint: enable=bad-whitespace, line-too-long
53
54def Iq(q, intercept, slope):
55    """
56    :param q:           Input q-value
57    :param intercept:   Intrecept in linear model
58    :param slope:       Slope in linear model
59    :return:            Calculated Intensity
60    """
61    inten = intercept + slope*q
62    return inten
63
64Iq.vectorized = True # Iq accepts an array of q values
65
66
67def Iqxy(qx, qy, intercept, slope):
68    """
69    :param qx:   Input q_x-value
70    :param qy:   Input q_y-value
71    :param intercept:   Intrecept in linear model
72    :param slope:       Slope in linear model
73    :return:     2D-Intensity
74    """
75    # TODO: SasView documents 2D intensity as Iq(qx)*Iq(qy), but returns Iq(qy)
76    # Note: SasView.run([r, theta]) does return Iq(qx)*Iq(qy)
77    return Iq(qx, intercept, slope)*Iq(qy, intercept, slope)
78
79Iqxy.vectorized = True  # Iqxy accepts an array of qx qy values
80
81# uncomment the following to test Iqxy in C models
82#del Iq, Iqxy
83#c_code = """
84#static double Iq(double q, double b, double m) { return m*q+b; }
85#static double Iqxy(double qx, double qy, double b, double m)
86#{ return (m*qx+b)*(m*qy+b); }
87#"""
88
89def random():
90    """Return a random parameter set for the model."""
91    scale = 10**np.random.uniform(0, 3)
92    slope = np.random.uniform(-1, 1)*1e2
93    offset = 1e-5 + (0 if slope > 0 else -slope)
94    intercept = 10**np.random.uniform(0, 1) + offset
95    pars = dict(
96        #background=0,
97        scale=scale,
98        slope=slope,
99        intercept=intercept,
100    )
101    return pars
102
103tests = [
104    [{'intercept': 1.0, 'slope': 1.0, }, 1.0, 2.001],
105    [{'intercept': 1.0, 'slope': 1.0, }, 0.0, 1.001],
106    [{'intercept': 1.0, 'slope': 1.0, }, 0.4, 1.401],
107    [{'intercept': 1.0, 'slope': 1.0, }, 1.3, 2.301],
108    [{'intercept': 1.0, 'slope': 1.0, }, 0.5, 1.501],
109    [{'intercept': 1.0, 'slope': 1.0, }, [0.4, 0.5], [1.401, 1.501]],
110    [{'intercept': 1.0, 'slope': 1.0, 'background': 0.0, }, (1.3, 1.57), 5.911],
111]
Note: See TracBrowser for help on using the repository browser.