[8f20419d] | 1 | #!/usr/bin/env python |
---|
| 2 | """ |
---|
| 3 | Provide I(q) = A*pow(qval,-1.0*m1) for q<=qc |
---|
| 4 | =scale*pow(qval,-1.0*m2) for q>qc |
---|
| 5 | TwoPowerLaw function as a BaseComponent model |
---|
| 6 | """ |
---|
| 7 | |
---|
| 8 | from sans.models.BaseComponent import BaseComponent |
---|
[18695bf] | 9 | from numpy import power |
---|
[3fc01c5] | 10 | import math |
---|
[8f20419d] | 11 | |
---|
| 12 | class TwoPowerLawModel(BaseComponent): |
---|
| 13 | """ |
---|
| 14 | Class that evaluates a TwoPowerLawModel. |
---|
| 15 | |
---|
| 16 | I(q) = coef_A*pow(qval,-1.0*power1) for q<=qc |
---|
| 17 | =C*pow(qval,-1.0*power2) for q>qc |
---|
| 18 | where C=coef_A*pow(qc,-1.0*power1)/pow(qc,-1.0*power2). |
---|
| 19 | List of default parameters: |
---|
| 20 | coef_A = coefficient |
---|
| 21 | power1 = (-) Power @ low Q |
---|
| 22 | power2 = (-) Power @ high Q |
---|
| 23 | qc = crossover Q-value |
---|
| 24 | background = incoherent background |
---|
| 25 | """ |
---|
| 26 | |
---|
| 27 | def __init__(self): |
---|
| 28 | """ Initialization """ |
---|
| 29 | |
---|
| 30 | # Initialize BaseComponent first, then sphere |
---|
| 31 | BaseComponent.__init__(self) |
---|
| 32 | |
---|
| 33 | ## Name of the model |
---|
| 34 | self.name = "TwoPowerLaw" |
---|
| 35 | self.description="""I(q) = coef_A*pow(qval,-1.0*power1) for q<=qc |
---|
| 36 | =C*pow(qval,-1.0*power2) for q>qc |
---|
| 37 | where C=coef_A*pow(qc,-1.0*power1)/pow(qc,-1.0*power2). |
---|
| 38 | List of default parameters: |
---|
| 39 | coef_A = coefficient |
---|
| 40 | power1 = (-) Power @ low Q |
---|
| 41 | power2 = (-) Power @ high Q |
---|
| 42 | qc = crossover Q-value |
---|
| 43 | background = incoherent background |
---|
| 44 | """ |
---|
| 45 | ## Define parameters |
---|
| 46 | self.params = {} |
---|
| 47 | self.params['coef_A'] = 1.0 |
---|
| 48 | self.params['power1'] = 1.0 |
---|
| 49 | self.params['power2'] = 4.0 |
---|
| 50 | self.params['qc'] = 0.04 |
---|
| 51 | self.params['background'] = 0.0 |
---|
| 52 | ## Parameter details [units, min, max] |
---|
| 53 | self.details = {} |
---|
| 54 | self.details['coef_A'] = ['', None, None] |
---|
| 55 | self.details['power1'] = ['', None, None] |
---|
| 56 | self.details['power2'] = ['', None, None] |
---|
| 57 | self.details['qc'] = ['1/A', None, None] |
---|
| 58 | self.details['background'] = ['[1/cm]', None, None] |
---|
| 59 | |
---|
| 60 | #list of parameter that cannot be fitted |
---|
| 61 | self.fixed= [] |
---|
| 62 | def _twopowerlaw(self, x): |
---|
| 63 | """ |
---|
| 64 | Model definition |
---|
| 65 | """ |
---|
| 66 | qc= self.params['qc'] |
---|
| 67 | if(x<=qc): |
---|
[18695bf] | 68 | inten = self.params['coef_A']*power(x,-1.0*self.params['power1']) |
---|
[8f20419d] | 69 | else: |
---|
[18695bf] | 70 | scale = self.params['coef_A']*power(qc,-1.0*self.params['power1']) \ |
---|
| 71 | / power(qc,-1.0*self.params['power2']) |
---|
| 72 | inten = scale*power(x,-1.0*self.params['power2']) |
---|
[8f20419d] | 73 | inten += self.params['background'] |
---|
| 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._twopowerlaw(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._twopowerlaw(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 = math.sqrt(x[0]**2 + x[1]**2) |
---|
| 96 | return self._twopowerlaw(q) |
---|
| 97 | elif x.__class__.__name__ == 'tuple': |
---|
| 98 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
| 99 | else: |
---|
| 100 | return self._twopowerlaw(x) |
---|