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