[829eee9] | 1 | #!/usr/bin/env python |
---|
| 2 | """ |
---|
| 3 | Provide F(x) = scale/( 1 + (x*L)^2 ) + bkd |
---|
[3db3895] | 4 | Lorentz (Ornstein-Zernicke) function as a BaseComponent model |
---|
[829eee9] | 5 | """ |
---|
| 6 | |
---|
| 7 | from sans.models.BaseComponent import BaseComponent |
---|
| 8 | import math |
---|
| 9 | |
---|
| 10 | class LorentzModel(BaseComponent): |
---|
| 11 | |
---|
| 12 | """ |
---|
[3db3895] | 13 | Class that evaluates a Lorentz (Ornstein-Zernicke) model. |
---|
[829eee9] | 14 | |
---|
| 15 | F(x) = scale/( 1 + (x*L)^2 ) + bkd |
---|
| 16 | |
---|
| 17 | The model has three parameters: |
---|
| 18 | L = screen Length |
---|
| 19 | scale = scale factor |
---|
| 20 | bkd = incoherent background |
---|
| 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 |
---|
| 30 | self.name = "Lorentz" |
---|
| 31 | |
---|
| 32 | ## Define parameters |
---|
| 33 | self.params = {} |
---|
[3db3895] | 34 | self.params['length'] = 50.0 |
---|
| 35 | self.params['scale'] = 1.0 |
---|
| 36 | self.params['background'] = 0.0 |
---|
[829eee9] | 37 | |
---|
| 38 | ## Parameter details [units, min, max] |
---|
| 39 | self.details = {} |
---|
[3db3895] | 40 | self.details['length'] = ['A', None, None] |
---|
| 41 | self.details['scale'] = ['', None, None] |
---|
| 42 | self.details['background'] = ['cm-1', None, None] |
---|
[829eee9] | 43 | |
---|
| 44 | def _lorentz(self, x): |
---|
| 45 | """ |
---|
| 46 | Evaluate F(x) = scale/( 1 + (x*L)^2 ) + bkd |
---|
| 47 | |
---|
| 48 | """ |
---|
[3db3895] | 49 | return self.params['scale']/( 1 + math.pow((x * self.params['length']),2))\ |
---|
| 50 | + self.params['background'] |
---|
[829eee9] | 51 | |
---|
| 52 | |
---|
| 53 | def run(self, x = 0.0): |
---|
| 54 | """ Evaluate the model |
---|
[3db3895] | 55 | @param x: input q-value (float or [float, float] as [r, theta]) |
---|
[829eee9] | 56 | @return: (Lorentz value) |
---|
| 57 | """ |
---|
| 58 | if x.__class__.__name__ == 'list': |
---|
[a55fac1] | 59 | return self._lorentz(x[0]) |
---|
[829eee9] | 60 | elif x.__class__.__name__ == 'tuple': |
---|
| 61 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
| 62 | else: |
---|
| 63 | return self._lorentz(x) |
---|
| 64 | |
---|
| 65 | def runXY(self, x = 0.0): |
---|
| 66 | """ Evaluate the model |
---|
[3db3895] | 67 | @param x: input q-value (float or [float, float] as [qx, qy]) |
---|
[829eee9] | 68 | @return: Lorentz value |
---|
| 69 | """ |
---|
| 70 | if x.__class__.__name__ == 'list': |
---|
[a55fac1] | 71 | q = math.sqrt(x[0]**2 + x[1]**2) |
---|
| 72 | return self._lorentz(q) |
---|
[829eee9] | 73 | elif x.__class__.__name__ == 'tuple': |
---|
| 74 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
| 75 | else: |
---|
| 76 | return self._lorentz(x) |
---|