1 | #!/usr/bin/env python |
---|
2 | """ |
---|
3 | I(q) = scale/q^s* exp ( - R_g^2 q^2 / (3-s) ) for q<= ql |
---|
4 | = scale/q^m*exp((-ql^2*Rg^2)/(3-s))*ql^(m-s) for q>=ql |
---|
5 | Guinier function as a BaseComponent model |
---|
6 | """ |
---|
7 | |
---|
8 | from sans.models.BaseComponent import BaseComponent |
---|
9 | from math import sqrt,exp |
---|
10 | |
---|
11 | class GuinierPorodModel(BaseComponent): |
---|
12 | """ |
---|
13 | Class that evaluates a GuinierPorod model. |
---|
14 | |
---|
15 | I(q) = scale/q^s* exp ( - R_g^2 q^2 / (3-s) ) for q<= ql |
---|
16 | = scale/q^m*exp((-ql^2*Rg^2)/(3-s))*ql^(m-s) for q>=ql |
---|
17 | """ |
---|
18 | def __init__(self): |
---|
19 | """ Initialization """ |
---|
20 | |
---|
21 | # Initialize BaseComponent first, then sphere |
---|
22 | BaseComponent.__init__(self) |
---|
23 | |
---|
24 | ## Name of the model |
---|
25 | self.name = "GuinierPorod" |
---|
26 | self.description=""" I(q) = scale/q^s* exp ( - R_g^2 q^2 / (3-s) ) for q<= ql |
---|
27 | = scale/q^m*exp((-ql^2*Rg^2)/(3-s))*ql^(m-s) for q>=ql |
---|
28 | where ql = sqrt((m-s)(3-s)/2)/Rg. |
---|
29 | List of parameters: |
---|
30 | scale = Guinier Scale |
---|
31 | s = Dimension Variable |
---|
32 | Rg = Radius of Gyration [A] |
---|
33 | m = Porod Exponent |
---|
34 | background = Background [1/cm]""" |
---|
35 | ## Define parameters |
---|
36 | self.params = {} |
---|
37 | self.params['scale'] = 1.0 |
---|
38 | self.params['dim'] = 1.0 |
---|
39 | self.params['rg'] = 100.0 |
---|
40 | self.params['m'] = 3.0 |
---|
41 | self.params['background'] = 0.1 |
---|
42 | ## Parameter details [units, min, max] |
---|
43 | self.details = {} |
---|
44 | self.details['scale'] = ['', None, None] |
---|
45 | self.details['dim'] = ['', None, None] |
---|
46 | self.details['rg'] = ['[A]', None, None] |
---|
47 | self.details['m'] = ['', None, None] |
---|
48 | self.details['background'] = ['[1/cm]', None, None] |
---|
49 | |
---|
50 | #list of parameter that cannot be fitted |
---|
51 | self.fixed= [] |
---|
52 | |
---|
53 | def _guinier_porod(self, x): |
---|
54 | """ |
---|
55 | Guinier-Porod Model |
---|
56 | """ |
---|
57 | # parameters |
---|
58 | G = self.params['scale'] |
---|
59 | s = self.params['dim'] |
---|
60 | Rg = self.params['rg'] |
---|
61 | m = self.params['m'] |
---|
62 | bgd = self.params['background'] |
---|
63 | n = 3.0 - s |
---|
64 | qval = x |
---|
65 | |
---|
66 | #do the calculation and return the function value |
---|
67 | q1=sqrt((n-3.0+m)*n/2.0)/Rg |
---|
68 | if qval < q1: |
---|
69 | F = (G/pow(qval,(3.0-n)))*exp((-qval*qval*Rg*Rg)/n) |
---|
70 | else: |
---|
71 | F = (G/pow(qval,m))*exp(-(n-3.0+m)/2.0)*pow(((n-3.0+m)*n/2.0), |
---|
72 | ((n-3.0+m)/2.0))/pow(Rg,(n-3.0+m)) |
---|
73 | inten = F + bgd |
---|
74 | |
---|
75 | return inten |
---|
76 | |
---|
77 | def run(self, x = 0.0): |
---|
78 | """ Evaluate the model |
---|
79 | @param x: input q-value (float or [float, float] as [r, theta]) |
---|
80 | @return: (guinier value) |
---|
81 | """ |
---|
82 | if x.__class__.__name__ == 'list': |
---|
83 | return self._guinier_porod(x[0]) |
---|
84 | elif x.__class__.__name__ == 'tuple': |
---|
85 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
86 | else: |
---|
87 | return self._guinier_porod(x) |
---|
88 | |
---|
89 | def runXY(self, x = 0.0): |
---|
90 | """ Evaluate the model |
---|
91 | @param x: input q-value (float or [float, float] as [qx, qy]) |
---|
92 | @return: guinier value |
---|
93 | """ |
---|
94 | if x.__class__.__name__ == 'list': |
---|
95 | q = sqrt(x[0]**2 + x[1]**2) |
---|
96 | return self._guinier_porod(q) |
---|
97 | elif x.__class__.__name__ == 'tuple': |
---|
98 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
99 | else: |
---|
100 | return self._guinier_porod(x) |
---|