1 | #!/usr/bin/env python |
---|
2 | """ |
---|
3 | Provide F(x) = 2( exp(-x) + x - 1 )/x**2 |
---|
4 | with x = (q*R_g)**2 |
---|
5 | |
---|
6 | Debye function as a BaseComponent model |
---|
7 | """ |
---|
8 | |
---|
9 | from sans.models.BaseComponent import BaseComponent |
---|
10 | import math |
---|
11 | |
---|
12 | class DebyeModel(BaseComponent): |
---|
13 | |
---|
14 | """ |
---|
15 | Class that evaluates a Debye model. |
---|
16 | |
---|
17 | F(x) = 2( exp(-x) + x - 1 )/x**2 |
---|
18 | with x = (q*R_g)**2 |
---|
19 | |
---|
20 | The model has three parameters: |
---|
21 | Rg = radius of gyration |
---|
22 | scale = scale factor |
---|
23 | bkd = Constant background |
---|
24 | """ |
---|
25 | |
---|
26 | def __init__(self): |
---|
27 | """ Initialization """ |
---|
28 | |
---|
29 | # Initialize BaseComponent first, then sphere |
---|
30 | BaseComponent.__init__(self) |
---|
31 | |
---|
32 | ## Name of the model |
---|
33 | self.name = "Debye" |
---|
34 | self.description=""" |
---|
35 | F(x) = 2( exp(-x) + x - 1 )/x**2 |
---|
36 | with x = (q*R_g)**2 |
---|
37 | The model has three parameters: |
---|
38 | Rg = radius of gyration |
---|
39 | scale = scale factor |
---|
40 | bkd = Constant background |
---|
41 | """ |
---|
42 | ## Define parameters |
---|
43 | self.params = {} |
---|
44 | self.params['rg'] = 50.0 |
---|
45 | self.params['scale'] = 1.0 |
---|
46 | self.params['background'] = 0.0 |
---|
47 | |
---|
48 | ## Parameter details [units, min, max] |
---|
49 | self.details = {} |
---|
50 | self.details['rg'] = ['', None, None] |
---|
51 | self.details['scale'] = ['', None, None] |
---|
52 | self.details['background'] = ['', None, None] |
---|
53 | #list of parameter that cannot be fitted |
---|
54 | self.fixed= [] |
---|
55 | def _debye(self, x): |
---|
56 | """ |
---|
57 | Evaluate F(x)= scale * D + bkd |
---|
58 | has 2 internal parameters : |
---|
59 | D = 2 * (exp(-y) + y - 1)/y**2 |
---|
60 | y = (x * Rg)^(2) |
---|
61 | """ |
---|
62 | # Note that a zero denominator value will raise |
---|
63 | # an exception |
---|
64 | y = (x * self.params['rg'])**2.0 |
---|
65 | D = 2.0*( math.exp(-y) + y -1.0 )/y**2.0 |
---|
66 | return self.params['scale']* D + self.params['background'] |
---|
67 | |
---|
68 | def run(self, x = 0.0): |
---|
69 | """ Evaluate the model |
---|
70 | @param x: input q-value (float or [float, float] as [r, theta]) |
---|
71 | @return: (debye value) |
---|
72 | """ |
---|
73 | if x.__class__.__name__ == 'list': |
---|
74 | return self._debye(x[0]) |
---|
75 | elif x.__class__.__name__ == 'tuple': |
---|
76 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
77 | else: |
---|
78 | return self._debye(x) |
---|
79 | |
---|
80 | def runXY(self, x = 0.0): |
---|
81 | """ Evaluate the model |
---|
82 | @param x: input q-value (float or [float, float] as [qx, qy]) |
---|
83 | @return: debye value |
---|
84 | """ |
---|
85 | if x.__class__.__name__ == 'list': |
---|
86 | q = math.sqrt(x[0]**2 + x[1]**2) |
---|
87 | return self._debye(q) |
---|
88 | elif x.__class__.__name__ == 'tuple': |
---|
89 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
90 | else: |
---|
91 | return self._debye(x) |
---|