[829eee9] | 1 | #!/usr/bin/env python |
---|
| 2 | """ |
---|
[3db3895] | 3 | Provide F(x) = 2( exp(-x) + x - 1 )/x**2 |
---|
| 4 | with x = (q*R_g)**2 |
---|
| 5 | |
---|
[829eee9] | 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 | |
---|
[3db3895] | 17 | F(x) = 2( exp(-x) + x - 1 )/x**2 |
---|
| 18 | with x = (q*R_g)**2 |
---|
[829eee9] | 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" |
---|
[96672c0] | 34 | self.description=""" |
---|
| 35 | F(x) = 2( exp(-x) + x - 1 )/x**2 |
---|
| 36 | with x = (q*R_g)**2 |
---|
[1ed3834] | 37 | |
---|
[96672c0] | 38 | The model has three parameters: |
---|
| 39 | Rg = radius of gyration |
---|
| 40 | scale = scale factor |
---|
| 41 | bkd = Constant background |
---|
| 42 | """ |
---|
[829eee9] | 43 | ## Define parameters |
---|
| 44 | self.params = {} |
---|
[3db3895] | 45 | self.params['rg'] = 50.0 |
---|
| 46 | self.params['scale'] = 1.0 |
---|
| 47 | self.params['background'] = 0.0 |
---|
[829eee9] | 48 | |
---|
| 49 | ## Parameter details [units, min, max] |
---|
| 50 | self.details = {} |
---|
[1ed3834] | 51 | self.details['rg'] = ['[A]', None, None] |
---|
[3db3895] | 52 | self.details['scale'] = ['', None, None] |
---|
[0824909] | 53 | self.details['background'] = ['[1/cm]', None, None] |
---|
[988130c6] | 54 | #list of parameter that cannot be fitted |
---|
| 55 | self.fixed= [] |
---|
[829eee9] | 56 | def _debye(self, x): |
---|
| 57 | """ |
---|
| 58 | Evaluate F(x)= scale * D + bkd |
---|
| 59 | has 2 internal parameters : |
---|
[3db3895] | 60 | D = 2 * (exp(-y) + y - 1)/y**2 |
---|
[829eee9] | 61 | y = (x * Rg)^(2) |
---|
| 62 | """ |
---|
[3db3895] | 63 | # Note that a zero denominator value will raise |
---|
| 64 | # an exception |
---|
[f629e346] | 65 | y = (x * self.params['rg'])**2.0 |
---|
[d37ba31] | 66 | if y == 0: |
---|
| 67 | return 1e+16 |
---|
| 68 | else: |
---|
| 69 | D = 2.0*( math.exp(-y) + y -1.0 )/y**2.0 |
---|
| 70 | return self.params['scale']* D + self.params['background'] |
---|
[829eee9] | 71 | |
---|
| 72 | def run(self, x = 0.0): |
---|
| 73 | """ Evaluate the model |
---|
[3db3895] | 74 | @param x: input q-value (float or [float, float] as [r, theta]) |
---|
[829eee9] | 75 | @return: (debye value) |
---|
| 76 | """ |
---|
| 77 | if x.__class__.__name__ == 'list': |
---|
[a55fac1] | 78 | return self._debye(x[0]) |
---|
[829eee9] | 79 | elif x.__class__.__name__ == 'tuple': |
---|
| 80 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
| 81 | else: |
---|
| 82 | return self._debye(x) |
---|
| 83 | |
---|
| 84 | def runXY(self, x = 0.0): |
---|
| 85 | """ Evaluate the model |
---|
[3db3895] | 86 | @param x: input q-value (float or [float, float] as [qx, qy]) |
---|
[829eee9] | 87 | @return: debye value |
---|
| 88 | """ |
---|
| 89 | if x.__class__.__name__ == 'list': |
---|
[a55fac1] | 90 | q = math.sqrt(x[0]**2 + x[1]**2) |
---|
| 91 | return self._debye(q) |
---|
[829eee9] | 92 | elif x.__class__.__name__ == 'tuple': |
---|
| 93 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
| 94 | else: |
---|
| 95 | return self._debye(x) |
---|