[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 = {} |
---|
[36948c92] | 51 | self.details['scale'] = ['', None, None] |
---|
| 52 | self.details['radius'] = ['A', None, None] |
---|
| 53 | self.details['fractal_dim'] = ['', 0, 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] |
---|
[3db3895] | 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 | """ |
---|
[36948c92] | 65 | if x<0 and self.params['fractal_dim']>0: |
---|
| 66 | raise ValueError, "negative number cannot be raised to a fractional power" |
---|
| 67 | |
---|
[3db3895] | 68 | return self.params['background']+ self._scatterRanDom(x)* self._Block(x) |
---|
[36948c92] | 69 | |
---|
[829eee9] | 70 | |
---|
| 71 | def _Block(self,x): |
---|
[3db3895] | 72 | return 1.0 + (math.sin((self.params['fractal_dim']-1.0) * math.atan(x * self.params['corr_length']))\ |
---|
| 73 | * self.params['fractal_dim'] * gamma(self.params['fractal_dim']-1.0))\ |
---|
[f629e346] | 74 | /( math.pow( (x*self.params['radius']), self.params['fractal_dim'])*\ |
---|
| 75 | math.pow( 1.0 + 1.0/((x**2)*(self.params['corr_length']**2)),(self.params['fractal_dim']-1.0)/2.0)) |
---|
[829eee9] | 76 | |
---|
| 77 | def _Spherical(self,x): |
---|
| 78 | """ |
---|
[f629e346] | 79 | F(x) = 3*[sin(x)-xcos(x)]/x**3 |
---|
[829eee9] | 80 | """ |
---|
[f629e346] | 81 | return 3.0*(math.sin(x)-x*math.cos(x))/(math.pow(x,3.0)) |
---|
[3db3895] | 82 | |
---|
[829eee9] | 83 | def _scatterRanDom(self,x): |
---|
| 84 | """ |
---|
| 85 | calculate p(x)= scale* V^(2)*delta^(2)* F(x*Radius)^(2) |
---|
| 86 | """ |
---|
[3db3895] | 87 | V =(4.0/3.0)*math.pi* math.pow(self.params['radius'],3.0) |
---|
| 88 | delta = self.params['block_sld']-self.params['solvent_sld'] |
---|
[829eee9] | 89 | |
---|
[f629e346] | 90 | return 1.0e8*self.params['scale']* V *(delta**2)*\ |
---|
[3db3895] | 91 | (self._Spherical(x*self.params['radius'])**2) |
---|
[829eee9] | 92 | |
---|
| 93 | def run(self, x = 0.0): |
---|
| 94 | """ Evaluate the model |
---|
[3db3895] | 95 | @param x: input q-value (float or [float, float] as [r, theta]) |
---|
[829eee9] | 96 | @return: (Fractal value) |
---|
| 97 | """ |
---|
| 98 | if x.__class__.__name__ == 'list': |
---|
[36948c92] | 99 | # Take absolute value of Q, since this model is really meant to |
---|
| 100 | # be defined in 1D for a given length of Q |
---|
| 101 | qx = math.fabs(x[0]*math.cos(x[1])) |
---|
| 102 | qy = math.fabs(x[0]*math.sin(x[1])) |
---|
| 103 | |
---|
| 104 | return self._Fractal(qx)*self._Fractal(qy) |
---|
[829eee9] | 105 | elif x.__class__.__name__ == 'tuple': |
---|
| 106 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
| 107 | else: |
---|
| 108 | return self._Fractal(x) |
---|
| 109 | |
---|
| 110 | def runXY(self, x = 0.0): |
---|
| 111 | """ Evaluate the model |
---|
[3db3895] | 112 | @param x: input q-value (float or [float, float] as [qx, qy]) |
---|
[829eee9] | 113 | @return: Fractal value |
---|
| 114 | """ |
---|
| 115 | if x.__class__.__name__ == 'list': |
---|
| 116 | return self._Fractal(x[0])*self._Fractal(x[1]) |
---|
| 117 | elif x.__class__.__name__ == 'tuple': |
---|
| 118 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
| 119 | else: |
---|
| 120 | return self._Fractal(x) |
---|