1 | #!/usr/bin/env python |
---|
2 | """ |
---|
3 | Failed attempt at a Beaucage model. |
---|
4 | """ |
---|
5 | #TODO: Clean this up |
---|
6 | #TODO: Remove setValueParam, which doesn't belong in this class! |
---|
7 | # It totally breaks the philosophy of this class |
---|
8 | |
---|
9 | from sans.models.BaseComponent import BaseComponent |
---|
10 | import math |
---|
11 | import matplotlib |
---|
12 | from scipy.special import erf |
---|
13 | class UnifiedPowerLawModel(BaseComponent): |
---|
14 | |
---|
15 | """ |
---|
16 | Class that evaluates a Unified Power_Law model. |
---|
17 | |
---|
18 | F(x) = bkd + sum(G[i]*exp(-x**2 *Rg[i]**2 /3)) \ |
---|
19 | + [B[i]( erf(x*Rg[i]/math.sqrt(6)))** (3*p[i])]/x**p[i] ) |
---|
20 | |
---|
21 | For each level four parameters must be chosen |
---|
22 | The model has 5 parameters: |
---|
23 | Gi = ? |
---|
24 | Rgi = Guinier Radius |
---|
25 | Pi = power for each level |
---|
26 | Bi = ? |
---|
27 | Four Levels can be used |
---|
28 | i is each level |
---|
29 | |
---|
30 | """ |
---|
31 | |
---|
32 | def __init__(self): |
---|
33 | """ Initialization """ |
---|
34 | |
---|
35 | # Initialize BaseComponent first, then sphere |
---|
36 | BaseComponent.__init__(self) |
---|
37 | |
---|
38 | ## Name of the model |
---|
39 | self.name = " UnifiedPowerLaw " |
---|
40 | # default Number of levels |
---|
41 | |
---|
42 | self.LevelTotal =2 |
---|
43 | |
---|
44 | ## Define parameters |
---|
45 | self.params = {} |
---|
46 | |
---|
47 | for i in xrange(self.LevelTotal): |
---|
48 | self.params['G'+ str(i+1)]= 0.0 |
---|
49 | self.params['B'+ str(i+1)]= 0.0 |
---|
50 | self.params['Rg'+ str(i+1)]= 0.0 |
---|
51 | self.params['P'+ str(i+1)]= 0.0 |
---|
52 | |
---|
53 | ## input Variables with default values for two levels |
---|
54 | |
---|
55 | self.params['G1'] = 400.0 |
---|
56 | self.params['G2'] = 3.0 |
---|
57 | |
---|
58 | self.params['B1'] = 5 * math.exp(-6) |
---|
59 | self.params['B2'] = 0.0006 |
---|
60 | |
---|
61 | self.params['Rg1'] = 200.0 |
---|
62 | self.params['Rg2'] = 100.0 |
---|
63 | |
---|
64 | self.params['P1'] = 4.0 |
---|
65 | self.params['P2'] = 2.0 |
---|
66 | |
---|
67 | self.params['bkd'] = 0.0 |
---|
68 | |
---|
69 | |
---|
70 | |
---|
71 | ## Parameter details [units, min, max] |
---|
72 | self.details = {} |
---|
73 | self.details['G1'] = ['cm^{-1} sr^{-1}', None, None ] |
---|
74 | self.details['G2'] = ['cm^{-1} sr^{-1}', None, None ] |
---|
75 | self.details['B1'] = ['cm^{-1} sr^{-1}', None, None ] |
---|
76 | self.details['B2'] = ['cm^{-1} sr^{-1}', None, None ] |
---|
77 | self.details['Rg1'] = ['A', None, None ] |
---|
78 | self.details['Rg2'] = ['A', None, None ] |
---|
79 | self.details['P1'] = ['', None, None ] |
---|
80 | self.details['P2'] = ['', None, None ] |
---|
81 | self.details['bkd'] = ['cm^{-1} sr^{-1}', None, None] |
---|
82 | |
---|
83 | def getValue(self, paramType,level): |
---|
84 | |
---|
85 | return self.getParam(str(paramType)+str(level)) |
---|
86 | |
---|
87 | def setValueParam(self,level,Bvalue,Rgvalue,Gvalue,Pvalue): |
---|
88 | """ |
---|
89 | set value of parameter given a level |
---|
90 | """ |
---|
91 | self.params['G'+str(level)] = Gvalue |
---|
92 | self.params['B'+str(level)] = Bvalue |
---|
93 | self.params['Rg'+str(level)]= Rgvalue |
---|
94 | self.params['P'+str(level)] = Pvalue |
---|
95 | # set the number total of level for the sum |
---|
96 | if self.LevelTotal < level: |
---|
97 | self.LevelTotal = level |
---|
98 | |
---|
99 | # set parameter details |
---|
100 | self.details['G'+str(level)] = ['cm^{-1} sr^{-1}', None, None ] |
---|
101 | self.details['B'+str(level)] = ['cm^{-1} sr^{-1}', None, None ] |
---|
102 | self.details['Rg'+str(level)] = ['A', None, None ] |
---|
103 | self.details['P'+str(level)] = ['', None, None ] |
---|
104 | |
---|
105 | |
---|
106 | def _Sum(self,x): |
---|
107 | """ |
---|
108 | Calculate the sum of Gi*exp(-x^(2)*Rgi^(2)/3) |
---|
109 | """ |
---|
110 | sum =0 |
---|
111 | # the default level is 2 and we are using the default values |
---|
112 | if (self.LevelTotal <= 2)and (self.LevelTotal > 1): |
---|
113 | return self.params['G1']*math.exp(-(x**2)* (self.params['Rg1']**2)/3)+\ |
---|
114 | self.params['G2']*math.exp(-(x**2)* (self.params['Rg2']**2)/3) |
---|
115 | # For level higher than 2 |
---|
116 | else: |
---|
117 | for i in xrange(self.LevelTotal): |
---|
118 | sum =sum +self.params['G'+str(i+1)]*math.exp(-(x**2)* (self.params['Rg'+str(i+1)]**2)/3) |
---|
119 | return sum |
---|
120 | |
---|
121 | |
---|
122 | def _UnifiedPow(self,level,x ): |
---|
123 | """ |
---|
124 | Calculates the values of the Unified Power Law function |
---|
125 | @param level: user should enters the level of computation else level =2 |
---|
126 | will be used |
---|
127 | @param x: a number |
---|
128 | """ |
---|
129 | |
---|
130 | return self.params['bkd']+self._Sum(x)+ (self.params['B'+str(level)]*\ |
---|
131 | math.pow(erf(x * self.params['Rg'+str(level)]/math.sqrt(6))\ |
---|
132 | ,3 * self.params['P'+str(level)]))/math.pow(x,self.params['P'+str(level)]) |
---|
133 | |
---|
134 | def run(self, level,x = 0.0): |
---|
135 | """ Evaluate the model |
---|
136 | @param x: simple value |
---|
137 | @param level : level of computation |
---|
138 | @return: (Unified Power law value) |
---|
139 | """ |
---|
140 | if x.__class__.__name__ == 'list': |
---|
141 | return self._UnifiedPow(level, x[0]*math.cos(x[1]))*\ |
---|
142 | self.__UnifiedPow(level, x[0]*math.sin(x[1])) |
---|
143 | elif x.__class__.__name__ == 'tuple': |
---|
144 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
145 | else: |
---|
146 | return self._UnifiedPow(level,x) |
---|
147 | |
---|
148 | def runXY(self,level, x = 0.0): |
---|
149 | """ Evaluate the model |
---|
150 | @param x: simple value |
---|
151 | @param level : level of computation |
---|
152 | @return: Unified Power law value |
---|
153 | """ |
---|
154 | if x.__class__.__name__ == 'list': |
---|
155 | return self._UnifiedPow(level, x[0])*self._UnifiedPow(level,x[1]) |
---|
156 | elif x.__class__.__name__ == 'tuple': |
---|
157 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
158 | else: |
---|
159 | return self._UnifiedPow(level, x) |
---|