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