[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 | |
---|
[79492222] | 7 | from sas.models.BaseComponent import BaseComponent |
---|
[829eee9] | 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" |
---|
[1ed3834] | 31 | self.description="""Lorentz (Ornstein-Zernicke) model. |
---|
| 32 | F(x) = scale/( 1 + (x*L)^2 ) + bkd |
---|
| 33 | |
---|
| 34 | The model has three parameters: |
---|
[4e2f6ef8] | 35 | L = screen Length\n\ |
---|
| 36 | scale = scale factor\n\ |
---|
[1ed3834] | 37 | bkd = 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 |
---|
| 50 | self.fixed= [] |
---|
[829eee9] | 51 | def _lorentz(self, x): |
---|
| 52 | """ |
---|
| 53 | Evaluate F(x) = scale/( 1 + (x*L)^2 ) + bkd |
---|
| 54 | |
---|
| 55 | """ |
---|
[3db3895] | 56 | return self.params['scale']/( 1 + math.pow((x * self.params['length']),2))\ |
---|
| 57 | + self.params['background'] |
---|
[829eee9] | 58 | |
---|
| 59 | |
---|
| 60 | def run(self, x = 0.0): |
---|
| 61 | """ Evaluate the model |
---|
[3db3895] | 62 | @param x: input q-value (float or [float, float] as [r, theta]) |
---|
[829eee9] | 63 | @return: (Lorentz value) |
---|
| 64 | """ |
---|
| 65 | if x.__class__.__name__ == 'list': |
---|
[a55fac1] | 66 | return self._lorentz(x[0]) |
---|
[829eee9] | 67 | elif x.__class__.__name__ == 'tuple': |
---|
| 68 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
| 69 | else: |
---|
| 70 | return self._lorentz(x) |
---|
| 71 | |
---|
| 72 | def runXY(self, x = 0.0): |
---|
| 73 | """ Evaluate the model |
---|
[3db3895] | 74 | @param x: input q-value (float or [float, float] as [qx, qy]) |
---|
[829eee9] | 75 | @return: Lorentz value |
---|
| 76 | """ |
---|
| 77 | if x.__class__.__name__ == 'list': |
---|
[a55fac1] | 78 | q = math.sqrt(x[0]**2 + x[1]**2) |
---|
| 79 | return self._lorentz(q) |
---|
[829eee9] | 80 | elif x.__class__.__name__ == 'tuple': |
---|
| 81 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
| 82 | else: |
---|
| 83 | return self._lorentz(x) |
---|