[3330bb4] | 1 | r""" |
---|
| 2 | This model calculates intensity using simple linear function |
---|
| 3 | |
---|
| 4 | Definition |
---|
| 5 | ---------- |
---|
| 6 | |
---|
| 7 | The 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:: |
---|
[c63a7c8] | 17 | |
---|
[3330bb4] | 18 | I(q) = \text{scale} (I(qx) \cdot I(qy)) + \text{background} |
---|
| 19 | |
---|
| 20 | References |
---|
| 21 | ---------- |
---|
| 22 | |
---|
| 23 | None. |
---|
[0507e09] | 24 | |
---|
| 25 | Source |
---|
| 26 | ------ |
---|
| 27 | |
---|
| 28 | `line.py <https://github.com/SasView/sasmodels/blob/master/sasmodels/models/line.py>`_ |
---|
| 29 | |
---|
| 30 | Authorship and Verification |
---|
| 31 | ---------------------------- |
---|
| 32 | |
---|
| 33 | * **Author:** |
---|
| 34 | * **Last Modified by:** |
---|
| 35 | * **Last Reviewed by:** |
---|
| 36 | * **Source added by :** Steve King **Date:** March 25, 2019""" |
---|
[2d81cfe] | 37 | |
---|
| 38 | import numpy as np |
---|
[3330bb4] | 39 | from numpy import inf |
---|
| 40 | |
---|
| 41 | name = "line" |
---|
| 42 | title = "Line model" |
---|
| 43 | description = """\ |
---|
| 44 | I(q) = A + B*q |
---|
| 45 | |
---|
| 46 | List of default parameters: |
---|
| 47 | A = intercept |
---|
| 48 | B = slope |
---|
| 49 | """ |
---|
| 50 | category = "shape-independent" |
---|
| 51 | |
---|
| 52 | # pylint: disable=bad-whitespace, line-too-long |
---|
| 53 | # ["name", "units", default, [lower, upper], "type", "description"], |
---|
| 54 | parameters = [["intercept", "1/cm", 1.0, [-inf, inf], "", "intercept in linear model"], |
---|
| 55 | ["slope", "Ang/cm", 1.0, [-inf, inf], "", "slope in linear model"], |
---|
| 56 | ] |
---|
| 57 | # pylint: enable=bad-whitespace, line-too-long |
---|
| 58 | |
---|
| 59 | def Iq(q, intercept, slope): |
---|
| 60 | """ |
---|
| 61 | :param q: Input q-value |
---|
| 62 | :param intercept: Intrecept in linear model |
---|
| 63 | :param slope: Slope in linear model |
---|
| 64 | :return: Calculated Intensity |
---|
| 65 | """ |
---|
| 66 | inten = intercept + slope*q |
---|
| 67 | return inten |
---|
| 68 | |
---|
| 69 | Iq.vectorized = True # Iq accepts an array of q values |
---|
| 70 | |
---|
[108e70e] | 71 | |
---|
[b297ba9] | 72 | def Iqxy(qx, qy, intercept, slope): |
---|
[3330bb4] | 73 | """ |
---|
| 74 | :param qx: Input q_x-value |
---|
| 75 | :param qy: Input q_y-value |
---|
[b297ba9] | 76 | :param intercept: Intrecept in linear model |
---|
| 77 | :param slope: Slope in linear model |
---|
[3330bb4] | 78 | :return: 2D-Intensity |
---|
| 79 | """ |
---|
| 80 | # TODO: SasView documents 2D intensity as Iq(qx)*Iq(qy), but returns Iq(qy) |
---|
| 81 | # Note: SasView.run([r, theta]) does return Iq(qx)*Iq(qy) |
---|
[b297ba9] | 82 | return Iq(qx, intercept, slope)*Iq(qy, intercept, slope) |
---|
[3330bb4] | 83 | |
---|
| 84 | Iqxy.vectorized = True # Iqxy accepts an array of qx qy values |
---|
| 85 | |
---|
[108e70e] | 86 | # uncomment the following to test Iqxy in C models |
---|
| 87 | #del Iq, Iqxy |
---|
| 88 | #c_code = """ |
---|
| 89 | #static double Iq(double q, double b, double m) { return m*q+b; } |
---|
| 90 | #static double Iqxy(double qx, double qy, double b, double m) |
---|
| 91 | #{ return (m*qx+b)*(m*qy+b); } |
---|
| 92 | #""" |
---|
| 93 | |
---|
[404ebbd] | 94 | def random(): |
---|
[b297ba9] | 95 | """Return a random parameter set for the model.""" |
---|
[48462b0] | 96 | scale = 10**np.random.uniform(0, 3) |
---|
| 97 | slope = np.random.uniform(-1, 1)*1e2 |
---|
[404ebbd] | 98 | offset = 1e-5 + (0 if slope > 0 else -slope) |
---|
[48462b0] | 99 | intercept = 10**np.random.uniform(0, 1) + offset |
---|
[404ebbd] | 100 | pars = dict( |
---|
[48462b0] | 101 | #background=0, |
---|
| 102 | scale=scale, |
---|
[404ebbd] | 103 | slope=slope, |
---|
| 104 | intercept=intercept, |
---|
| 105 | ) |
---|
| 106 | return pars |
---|
[3330bb4] | 107 | |
---|
| 108 | tests = [ |
---|
| 109 | [{'intercept': 1.0, 'slope': 1.0, }, 1.0, 2.001], |
---|
| 110 | [{'intercept': 1.0, 'slope': 1.0, }, 0.0, 1.001], |
---|
| 111 | [{'intercept': 1.0, 'slope': 1.0, }, 0.4, 1.401], |
---|
| 112 | [{'intercept': 1.0, 'slope': 1.0, }, 1.3, 2.301], |
---|
| 113 | [{'intercept': 1.0, 'slope': 1.0, }, 0.5, 1.501], |
---|
| 114 | [{'intercept': 1.0, 'slope': 1.0, }, [0.4, 0.5], [1.401, 1.501]], |
---|
| 115 | [{'intercept': 1.0, 'slope': 1.0, 'background': 0.0, }, (1.3, 1.57), 5.911], |
---|
| 116 | ] |
---|