source: sasmodels/sasmodels/models/two_lorentzian.py @ c1e44e5

Last change on this file since c1e44e5 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: 5.7 KB
Line 
1r"""
2Definition
3----------
4
5The scattering intensity $I(q)$ is calculated as
6
7.. math::
8
9    I(q) = \frac{A}{1 +(Q\xi_1)^n} + \frac{C}{1 +(Q\xi_2)^m} + \text{B}
10
11where $A$ = Lorentzian scale factor #1, $C$ = Lorentzian scale #2,
12$\xi_1$ and $\xi_2$ are the corresponding correlation lengths, and $n$ and
13$m$ are the respective power law exponents (set $n = m = 2$ for
14Ornstein-Zernicke behaviour).
15
16For 2D data the scattering intensity is calculated in the same way as 1D,
17where the $q$ vector is defined as
18
19.. math::
20
21    q = \sqrt{q_x^2 + q_y^2}
22
23
24References
25----------
26
27None.
28
29Authorship and Verification
30----------------------------
31
32* **Author:** NIST IGOR/DANSE **Date:** pre 2010
33* **Last Modified by:** Piotr rozyczko **Date:** January 29, 2016
34* **Last Reviewed by:** Paul Butler **Date:** March 21, 2016
35"""
36
37import numpy as np
38from numpy import inf, power
39
40name = "two_lorentzian"
41title = "This model calculates an empirical functional form for SAS data \
42characterized by two Lorentzian-type functions."
43description = """I(q) = scale_1/(1.0 + pow((q*length_1),exponent_1))
44             + scale_2/(1.0 + pow((q*length_2),exponent_2) )+ background
45
46             scale_1    = Lorentzian term scaling #1
47             length_1   = Lorentzian screening length #1 [A]
48             exponent_1 = Lorentzian exponent #1
49             scale_2    = Lorentzian term scaling #2
50             length_2   = Lorentzian screening length #2 [A]
51             exponent_2 = Lorentzian exponent #2
52             background = Incoherent background
53        """
54category = "shape-independent"
55
56# pylint: disable=bad-whitespace, line-too-long
57#            ["name", "units", default, [lower, upper], "type", "description"],
58parameters = [["lorentz_scale_1",  "",     10.0, [-inf, inf], "", "First power law scale factor"],
59              ["lorentz_length_1", "Ang", 100.0, [-inf, inf], "", "First Lorentzian screening length"],
60              ["lorentz_exp_1",    "",      3.0, [-inf, inf], "", "First exponent of power law"],
61              ["lorentz_scale_2",  "",      1.0, [-inf, inf], "", "Second scale factor for broad Lorentzian peak"],
62              ["lorentz_length_2", "Ang",  10.0, [-inf, inf], "", "Second Lorentzian screening length"],
63              ["lorentz_exp_2",    "",      2.0, [-inf, inf], "", "Second exponent of power law"],
64             ]
65# pylint: enable=bad-whitespace, line-too-long
66
67
68def Iq(q,
69       lorentz_scale_1=10.0,
70       lorentz_length_1=100.0,
71       lorentz_exp_1=3.0,
72       lorentz_scale_2=1.0,
73       lorentz_length_2=10.0,
74       lorentz_exp_2=2.0):
75
76    """
77    :param q:                   Input q-value (float or [float, float])
78    :param lorentz_scale_1:     Second scale factor for broad Lorentzian peak
79    :param lorentz_length_1:    First Lorentzian screening length
80    :param lorentz_exp_1:       Exponent of the second Lorentz function
81    :param lorentz_scale_2:     Second scale factor for broad Lorentzian peak
82    :param lorentz_length_2:    Second Lorentzian screening length
83    :param lorentz_exp_2:       Exponent of the second Lorentz function
84    :return:                    Calculated intensity
85    """
86# pylint: disable=bad-whitespace
87    intensity  = lorentz_scale_1/(1.0 +
88                                  power(q*lorentz_length_1, lorentz_exp_1))
89    intensity += lorentz_scale_2/(1.0 +
90                                  power(q*lorentz_length_2, lorentz_exp_2))
91# pylint: enable=bad-whitespace
92    return intensity
93
94Iq.vectorized = True  # Iq accepts an array of q values
95
96def random():
97    """Return a random parameter set for the model."""
98    scale = 10**np.random.uniform(0, 4, 2)
99    length = 10**np.random.uniform(1, 4, 2)
100    expon = np.random.uniform(1, 6, 2)
101
102    pars = dict(
103        #background=0,
104        scale=1, # scale provided in model
105        lorentz_scale_1=scale[0],
106        lorentz_length_1=length[0],
107        lorentz_exp_1=expon[0],
108        lorentz_scale_2=scale[1],
109        lorentz_length_2=length[1],
110        lorentz_exp_2=expon[1],
111    )
112    return pars
113
114
115demo = dict(scale=1, background=0.1,
116            lorentz_scale_1=10,
117            lorentz_length_1=100.0,
118            lorentz_exp_1=3.0,
119            lorentz_scale_2=1,
120            lorentz_length_2=10,
121            lorentz_exp_2=2.0)
122
123tests = [
124
125    # Accuracy tests based on content in test/utest_extra_models.py
126    [{'lorentz_scale_1':   10.0,
127      'lorentz_length_1': 100.0,
128      'lorentz_exp_1':      3.0,
129      'lorentz_scale_2':    1.0,
130      'lorentz_length_2':  10.0,
131      'lorentz_exp_2':      2.0,
132      'background':         0.1,
133     }, 0.001, 11.08991],
134
135    [{'lorentz_scale_1':   10.0,
136      'lorentz_length_1': 100.0,
137      'lorentz_exp_1':      3.0,
138      'lorentz_scale_2':    1.0,
139      'lorentz_length_2':  10.0,
140      'lorentz_exp_2':      2.0,
141      'background':         0.1,
142     }, 0.150141, 0.410245],
143
144    [{'lorentz_scale_1':   10.0,
145      'lorentz_length_1': 100.0,
146      'lorentz_exp_1':      3.0,
147      'lorentz_scale_2':    1.0,
148      'lorentz_length_2':  10.0,
149      'lorentz_exp_2':      2.0,
150      'background':         0.1,
151     }, 0.442528, 0.148699],
152
153    # Additional tests with larger range of parameters
154    [{'lorentz_scale_1':   10.0,
155      'lorentz_length_1': 100.0,
156      'lorentz_exp_1':      3.0,
157      'lorentz_scale_2':    1.0,
158      'lorentz_length_2':  10.0,
159      'lorentz_exp_2':      2.0,
160     }, 0.000332070182643, 10.9996228107],
161
162    [{'lorentz_scale_1':  0.0,
163      'lorentz_length_1': 0.0,
164      'lorentz_exp_1':    0.0,
165      'lorentz_scale_2':  0.0,
166      'lorentz_length_2': 0.0,
167      'lorentz_exp_2':    0.0,
168      'background':     100.0
169     }, 5.0, 100.0],
170
171    [{'lorentz_scale_1': 200.0,
172      'lorentz_length_1': 10.0,
173      'lorentz_exp_1':     0.1,
174      'lorentz_scale_2':   0.1,
175      'lorentz_length_2':  5.0,
176      'lorentz_exp_2':     2.0
177     }, 20000., 45.5659201896],
178    ]
Note: See TracBrowser for help on using the repository browser.