[51f14603] | 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 | |
---|
[79492222] | 8 | from sas.models.BaseComponent import BaseComponent |
---|
[51f14603] | 9 | from numpy import power |
---|
| 10 | import math |
---|
| 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 | |
---|
| 20 | List of default parameters: |
---|
| 21 | |
---|
| 22 | * coef_A = coefficient |
---|
| 23 | * power1 = (-) Power @ low Q |
---|
| 24 | * power2 = (-) Power @ high Q |
---|
| 25 | * qc = crossover Q-value |
---|
| 26 | * background = incoherent background |
---|
| 27 | """ |
---|
| 28 | |
---|
| 29 | def __init__(self): |
---|
| 30 | """ Initialization """ |
---|
| 31 | |
---|
| 32 | # Initialize BaseComponent first, then sphere |
---|
| 33 | BaseComponent.__init__(self) |
---|
| 34 | |
---|
| 35 | ## Name of the model |
---|
| 36 | self.name = "TwoPowerLaw" |
---|
| 37 | self.description="""I(q) = coef_A*pow(qval,-1.0*power1) for q<=qc |
---|
| 38 | =C*pow(qval,-1.0*power2) for q>qc |
---|
| 39 | where C=coef_A*pow(qc,-1.0*power1)/pow(qc,-1.0*power2). |
---|
| 40 | List of default parameters: |
---|
| 41 | coef_A = coefficient |
---|
| 42 | power1 = (-) Power @ low Q |
---|
| 43 | power2 = (-) Power @ high Q |
---|
| 44 | qc = crossover Q-value |
---|
| 45 | background = incoherent background |
---|
| 46 | """ |
---|
| 47 | ## Define parameters |
---|
| 48 | self.params = {} |
---|
| 49 | self.params['coef_A'] = 1.0 |
---|
| 50 | self.params['power1'] = 1.0 |
---|
| 51 | self.params['power2'] = 4.0 |
---|
| 52 | self.params['qc'] = 0.04 |
---|
| 53 | self.params['background'] = 0.0 |
---|
| 54 | ## Parameter details [units, min, max] |
---|
| 55 | self.details = {} |
---|
| 56 | self.details['coef_A'] = ['', None, None] |
---|
| 57 | self.details['power1'] = ['', None, None] |
---|
| 58 | self.details['power2'] = ['', None, None] |
---|
| 59 | self.details['qc'] = ['1/A', None, None] |
---|
| 60 | self.details['background'] = ['[1/cm]', None, None] |
---|
| 61 | |
---|
| 62 | #list of parameter that cannot be fitted |
---|
| 63 | self.fixed= [] |
---|
| 64 | def _twopowerlaw(self, x): |
---|
| 65 | """ |
---|
| 66 | Model definition |
---|
| 67 | """ |
---|
| 68 | qc= self.params['qc'] |
---|
| 69 | if(x<=qc): |
---|
| 70 | inten = self.params['coef_A']*power(x,-1.0*self.params['power1']) |
---|
| 71 | else: |
---|
| 72 | scale = self.params['coef_A']*power(qc,-1.0*self.params['power1']) \ |
---|
| 73 | / power(qc,-1.0*self.params['power2']) |
---|
| 74 | inten = scale*power(x,-1.0*self.params['power2']) |
---|
| 75 | inten += self.params['background'] |
---|
| 76 | |
---|
| 77 | return inten |
---|
| 78 | |
---|
| 79 | def run(self, x = 0.0): |
---|
| 80 | """ Evaluate the model |
---|
| 81 | @param x: input q-value (float or [float, float] as [r, theta]) |
---|
| 82 | @return: (guinier value) |
---|
| 83 | """ |
---|
| 84 | if x.__class__.__name__ == 'list': |
---|
| 85 | return self._twopowerlaw(x[0]) |
---|
| 86 | elif x.__class__.__name__ == 'tuple': |
---|
| 87 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
| 88 | else: |
---|
| 89 | return self._twopowerlaw(x) |
---|
| 90 | |
---|
| 91 | def runXY(self, x = 0.0): |
---|
| 92 | """ Evaluate the model |
---|
| 93 | @param x: input q-value (float or [float, float] as [qx, qy]) |
---|
| 94 | @return: guinier value |
---|
| 95 | """ |
---|
| 96 | if x.__class__.__name__ == 'list': |
---|
| 97 | q = math.sqrt(x[0]**2 + x[1]**2) |
---|
| 98 | return self._twopowerlaw(q) |
---|
| 99 | elif x.__class__.__name__ == 'tuple': |
---|
| 100 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
| 101 | else: |
---|
| 102 | return self._twopowerlaw(x) |
---|