[829eee9] | 1 | #!/usr/bin/env python |
---|
| 2 | """ |
---|
| 3 | |
---|
| 4 | Provide F(x)= P(x)*S(x) + bkd |
---|
| 5 | Fractal as a BaseComponent model |
---|
| 6 | """ |
---|
| 7 | |
---|
| 8 | from sans.models.BaseComponent import BaseComponent |
---|
| 9 | import math |
---|
| 10 | from scipy.special import gamma |
---|
| 11 | |
---|
| 12 | class FractalModel(BaseComponent): |
---|
| 13 | |
---|
| 14 | """ |
---|
| 15 | Class that evaluates a Fractal function. |
---|
| 16 | |
---|
| 17 | F(x)= P(x)*S(x) + bkd |
---|
| 18 | The model has Seven parameters: |
---|
[3db3895] | 19 | scale = Volume fraction |
---|
| 20 | radius = Block radius |
---|
| 21 | fractal_dim = Fractal dimension |
---|
| 22 | corr_length = correlation Length |
---|
| 23 | block_sld = SDL block |
---|
| 24 | solvent_sld = SDL solvent |
---|
| 25 | background = background |
---|
[829eee9] | 26 | |
---|
| 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 = "Number Density Fractal" |
---|
| 37 | |
---|
| 38 | ## Define parameters |
---|
| 39 | self.params = {} |
---|
[3db3895] | 40 | self.params['scale'] = 0.05 |
---|
| 41 | self.params['radius'] = 5.0 |
---|
| 42 | self.params['fractal_dim'] = 2.0 |
---|
| 43 | self.params['corr_length'] = 100.0 |
---|
| 44 | self.params['block_sld'] = 2.0e-6 |
---|
| 45 | self.params['solvent_sld'] = 6.0e-6 |
---|
| 46 | self.params['background'] = 0.0 |
---|
[829eee9] | 47 | |
---|
| 48 | |
---|
| 49 | ## Parameter details [units, min, max] |
---|
| 50 | self.details = {} |
---|
[3db3895] | 51 | self.details['scale'] = ['', None, None] |
---|
| 52 | self.details['radius'] = ['A', None, None] |
---|
| 53 | self.details['fractal_dim'] = ['', None, None] |
---|
| 54 | self.details['corr_length'] = ['A', None, None] |
---|
| 55 | self.details['block_sld'] = ['A-2', None, None] |
---|
| 56 | self.details['solvent_sld'] = ['A-2', None, None] |
---|
| 57 | self.details['background'] = ['cm-1', None, None] |
---|
[829eee9] | 58 | |
---|
| 59 | |
---|
| 60 | def _Fractal(self, x): |
---|
| 61 | """ |
---|
| 62 | Evaluate |
---|
[3db3895] | 63 | F(x) = p(x) * s(x) + bkd |
---|
[829eee9] | 64 | """ |
---|
[3db3895] | 65 | return self.params['background']+ self._scatterRanDom(x)* self._Block(x) |
---|
[829eee9] | 66 | |
---|
| 67 | def _Block(self,x): |
---|
| 68 | |
---|
[f629e346] | 69 | |
---|
| 70 | |
---|
[3db3895] | 71 | return 1.0 + (math.sin((self.params['fractal_dim']-1.0) * math.atan(x * self.params['corr_length']))\ |
---|
| 72 | * self.params['fractal_dim'] * gamma(self.params['fractal_dim']-1.0))\ |
---|
[f629e346] | 73 | /( math.pow( (x*self.params['radius']), self.params['fractal_dim'])*\ |
---|
| 74 | math.pow( 1.0 + 1.0/((x**2)*(self.params['corr_length']**2)),(self.params['fractal_dim']-1.0)/2.0)) |
---|
[829eee9] | 75 | |
---|
| 76 | def _Spherical(self,x): |
---|
| 77 | """ |
---|
[f629e346] | 78 | F(x) = 3*[sin(x)-xcos(x)]/x**3 |
---|
[829eee9] | 79 | """ |
---|
[f629e346] | 80 | return 3.0*(math.sin(x)-x*math.cos(x))/(math.pow(x,3.0)) |
---|
[3db3895] | 81 | |
---|
[829eee9] | 82 | def _scatterRanDom(self,x): |
---|
| 83 | """ |
---|
| 84 | calculate p(x)= scale* V^(2)*delta^(2)* F(x*Radius)^(2) |
---|
| 85 | """ |
---|
[3db3895] | 86 | V =(4.0/3.0)*math.pi* math.pow(self.params['radius'],3.0) |
---|
| 87 | delta = self.params['block_sld']-self.params['solvent_sld'] |
---|
[829eee9] | 88 | |
---|
[f629e346] | 89 | return 1.0e8*self.params['scale']* V *(delta**2)*\ |
---|
[3db3895] | 90 | (self._Spherical(x*self.params['radius'])**2) |
---|
[829eee9] | 91 | |
---|
| 92 | def run(self, x = 0.0): |
---|
| 93 | """ Evaluate the model |
---|
[3db3895] | 94 | @param x: input q-value (float or [float, float] as [r, theta]) |
---|
[829eee9] | 95 | @return: (Fractal value) |
---|
| 96 | """ |
---|
| 97 | if x.__class__.__name__ == 'list': |
---|
| 98 | return self._Fractal(x[0]*math.cos(x[1]))*self._Fractal(x[0]*math.sin(x[1])) |
---|
| 99 | elif x.__class__.__name__ == 'tuple': |
---|
| 100 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
| 101 | else: |
---|
| 102 | return self._Fractal(x) |
---|
| 103 | |
---|
| 104 | def runXY(self, x = 0.0): |
---|
| 105 | """ Evaluate the model |
---|
[3db3895] | 106 | @param x: input q-value (float or [float, float] as [qx, qy]) |
---|
[829eee9] | 107 | @return: Fractal value |
---|
| 108 | """ |
---|
| 109 | if x.__class__.__name__ == 'list': |
---|
| 110 | return self._Fractal(x[0])*self._Fractal(x[1]) |
---|
| 111 | elif x.__class__.__name__ == 'tuple': |
---|
| 112 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
| 113 | else: |
---|
| 114 | return self._Fractal(x) |
---|