[829eee9] | 1 | #!/usr/bin/env python |
---|
| 2 | """ |
---|
[3db3895] | 3 | Provide F(x) = scale/( 1 + (x*L)^2 )^(2) + background |
---|
[829eee9] | 4 | DAB (Debye Anderson Brumberger) function as a BaseComponent model |
---|
| 5 | """ |
---|
| 6 | |
---|
| 7 | from sans.models.BaseComponent import BaseComponent |
---|
| 8 | import math |
---|
| 9 | |
---|
| 10 | class DABModel(BaseComponent): |
---|
| 11 | |
---|
| 12 | """ |
---|
| 13 | Class that evaluates a DAB model. |
---|
| 14 | |
---|
[8d97277] | 15 | F(x) = scale*(L^3)/( 1 + (x*L)^2 )^(2) + background |
---|
[829eee9] | 16 | |
---|
| 17 | The model has three parameters: |
---|
[3db3895] | 18 | L = Correlation Length |
---|
| 19 | scale = scale factor |
---|
| 20 | background = incoherent background |
---|
[829eee9] | 21 | """ |
---|
| 22 | |
---|
| 23 | def __init__(self): |
---|
| 24 | """ Initialization """ |
---|
| 25 | |
---|
| 26 | # Initialize BaseComponent first, then sphere |
---|
| 27 | BaseComponent.__init__(self) |
---|
| 28 | |
---|
| 29 | ## Name of the model |
---|
[3db3895] | 30 | self.name = "DAB_Model" |
---|
[3ee4d96] | 31 | self.description = """ |
---|
| 32 | F(x) = scale*(L^3)/( 1 + (x*L)^2 )^(2) + background |
---|
[753552d] | 33 | |
---|
| 34 | The model has three parameters: |
---|
| 35 | L = Correlation Length |
---|
| 36 | scale = scale factor |
---|
| 37 | background = incoherent background""" |
---|
[829eee9] | 38 | ## Define parameters |
---|
| 39 | self.params = {} |
---|
[3db3895] | 40 | self.params['length'] = 50.0 |
---|
| 41 | self.params['scale'] = 1.0 |
---|
| 42 | self.params['background'] = 0.0 |
---|
[829eee9] | 43 | |
---|
| 44 | ## Parameter details [units, min, max] |
---|
| 45 | self.details = {} |
---|
[1ed3834] | 46 | self.details['length'] = ['[A]', None, None] |
---|
[3db3895] | 47 | self.details['scale'] = ['', None, None] |
---|
[0824909] | 48 | self.details['background'] = ['[1/cm]', None, None] |
---|
[988130c6] | 49 | #list of parameter that cannot be fitted |
---|
[3ee4d96] | 50 | self.fixed = [] |
---|
| 51 | |
---|
[829eee9] | 52 | def _DAB(self, x): |
---|
| 53 | """ |
---|
[8d97277] | 54 | Evaluate F(x) = (scale*L^3)/( 1 + (x*L)^2 )^(2) + background |
---|
[829eee9] | 55 | """ |
---|
[3ee4d96] | 56 | # According to SRK (Igor/NIST code: 6 JUL 2009 changed definition |
---|
| 57 | # of 'scale' to be uncorrelated with 'length') |
---|
| 58 | return self.params['scale']*math.pow(self.params['length'], 3)/\ |
---|
| 59 | math.pow(( 1 + math.pow(x * self.params['length'], 2)), 2) \ |
---|
| 60 | + self.params['background'] |
---|
[829eee9] | 61 | |
---|
| 62 | def run(self, x = 0.0): |
---|
| 63 | """ Evaluate the model |
---|
[3db3895] | 64 | @param x: input q-value (float or [float, float] as [r, theta]) |
---|
[829eee9] | 65 | @return: (DAB value) |
---|
| 66 | """ |
---|
| 67 | if x.__class__.__name__ == 'list': |
---|
[a55fac1] | 68 | return self._DAB(x[0]) |
---|
[829eee9] | 69 | elif x.__class__.__name__ == 'tuple': |
---|
[3ee4d96] | 70 | raise ValueError, "Tuples are not allowed as input to models" |
---|
[829eee9] | 71 | else: |
---|
| 72 | return self._DAB(x) |
---|
| 73 | |
---|
| 74 | def runXY(self, x = 0.0): |
---|
| 75 | """ Evaluate the model |
---|
[3db3895] | 76 | @param x: input q-value (float or [float, float] as [qx, qy]) |
---|
[829eee9] | 77 | @return: DAB value |
---|
| 78 | """ |
---|
| 79 | if x.__class__.__name__ == 'list': |
---|
[a55fac1] | 80 | q = math.sqrt(x[0]**2 + x[1]**2) |
---|
| 81 | return self._DAB(q) |
---|
[829eee9] | 82 | elif x.__class__.__name__ == 'tuple': |
---|
[3ee4d96] | 83 | raise ValueError, "Tuples are not allowed as input to models" |
---|
[829eee9] | 84 | else: |
---|
| 85 | return self._DAB(x) |
---|