[8f20419d] | 1 | #!/usr/bin/env python |
---|
| 2 | """ |
---|
| 3 | TwoLorentzianModel function as a BaseComponent model |
---|
| 4 | """ |
---|
| 5 | |
---|
| 6 | from sans.models.BaseComponent import BaseComponent |
---|
[18695bf] | 7 | from numpy import power |
---|
[8f20419d] | 8 | |
---|
| 9 | class TwoLorentzianModel(BaseComponent): |
---|
| 10 | """ |
---|
| 11 | Class that evaluates a TwoLorentzianModel. |
---|
| 12 | I(q) = II(q) = scale_1/(1.0 + pow((q*length_1),exponent_1)) |
---|
| 13 | + scale_2/(1.0 + pow((q*length_2),exponent_2) )+ background |
---|
| 14 | """ |
---|
| 15 | |
---|
| 16 | def __init__(self): |
---|
| 17 | """ Initialization """ |
---|
| 18 | |
---|
| 19 | # Initialize BaseComponent first, then sphere |
---|
| 20 | BaseComponent.__init__(self) |
---|
| 21 | |
---|
| 22 | ## Name of the model |
---|
| 23 | self.name = "TwoLorentzian" |
---|
| 24 | self.description="""I(q) = scale_1/(1.0 + pow((q*length_1),exponent_1)) |
---|
| 25 | + scale_2/(1.0 + pow((q*length_2),exponent_2) )+ background |
---|
| 26 | List of default parameters: |
---|
| 27 | scale_1 = Lorentzian term scaling #1 |
---|
| 28 | length_1 = Lorentzian screening length #1 [A] |
---|
| 29 | exponent_1 = Lorentzian exponent #1 |
---|
| 30 | scale_2 = Lorentzian term scaling #2 |
---|
| 31 | length_2 = Lorentzian screening length #2 [A] |
---|
| 32 | exponent_2 = Lorentzian exponent #2 |
---|
| 33 | background = Incoherent background |
---|
| 34 | """ |
---|
| 35 | ## Define parameters |
---|
| 36 | self.params = {} |
---|
| 37 | self.params['scale_1'] = 10.0 |
---|
| 38 | self.params['length_1'] = 100.0 |
---|
| 39 | self.params['exponent_1'] = 3.0 |
---|
| 40 | self.params['scale_2'] = 1.0 |
---|
| 41 | self.params['length_2'] = 10.0 |
---|
| 42 | self.params['exponent_2'] = 2.0 |
---|
| 43 | self.params['background'] = 0.1 |
---|
| 44 | ## Parameter details [units, min, max] |
---|
| 45 | self.details = {} |
---|
| 46 | self.details['scale_1'] = ['', None, None] |
---|
| 47 | self.details['length_1'] = ['A', None, None] |
---|
| 48 | self.details['exponent_1'] = ['', None, None] |
---|
| 49 | self.details['scale_2'] = ['', None, None] |
---|
| 50 | self.details['length_2'] = ['A', None, None] |
---|
| 51 | self.details['exponent_2'] = ['', None, None] |
---|
| 52 | self.details['background'] = ['[1/cm]', None, None] |
---|
| 53 | |
---|
| 54 | #list of parameter that cannot be fitted |
---|
| 55 | self.fixed= [] |
---|
| 56 | def _twolorentzian(self, x): |
---|
| 57 | """ |
---|
| 58 | Model definition |
---|
| 59 | """ |
---|
| 60 | inten = self.params['scale_1']/(1.0 + \ |
---|
[18695bf] | 61 | power((x*self.params['length_1']),self.params['exponent_1'])) |
---|
[8f20419d] | 62 | inten += self.params['scale_2']/(1.0 + \ |
---|
[18695bf] | 63 | power((x*self.params['length_2']),self.params['exponent_2'])) |
---|
[8f20419d] | 64 | inten += self.params['background'] |
---|
| 65 | |
---|
| 66 | return inten |
---|
| 67 | |
---|
| 68 | def run(self, x = 0.0): |
---|
| 69 | """ |
---|
| 70 | Evaluate the model |
---|
| 71 | |
---|
| 72 | param x: input q-value (float or [float, float] as [r, theta]) |
---|
| 73 | return: (scattering value) |
---|
| 74 | """ |
---|
| 75 | if x.__class__.__name__ == 'list': |
---|
| 76 | return self._twolorentzian(x[0]) |
---|
| 77 | elif x.__class__.__name__ == 'tuple': |
---|
| 78 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
| 79 | else: |
---|
| 80 | return self._twolorentzian(x) |
---|
| 81 | |
---|
| 82 | def runXY(self, x = 0.0): |
---|
| 83 | """ |
---|
| 84 | Evaluate the model |
---|
| 85 | |
---|
| 86 | param x: input q-value (float or [float, float] as [qx, qy]) |
---|
| 87 | return: scattering value |
---|
| 88 | """ |
---|
| 89 | if x.__class__.__name__ == 'list': |
---|
| 90 | q = math.sqrt(x[0]**2 + x[1]**2) |
---|
| 91 | return self._twolorentzian(q) |
---|
| 92 | elif x.__class__.__name__ == 'tuple': |
---|
| 93 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
| 94 | else: |
---|
| 95 | return self._twolorentzian(x) |
---|