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