[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" |
---|
[96672c0] | 37 | self.description=""" |
---|
| 38 | I(x)= P(x)*S(x) + bkd |
---|
| 39 | p(x)= scale* V^(2)*delta^(2)* F(x*Radius)^(2) |
---|
| 40 | F(x) = 3*[sin(x)-xcos(x)]/x**3 |
---|
| 41 | The model has Seven parameters: |
---|
| 42 | scale = Volume fraction |
---|
| 43 | radius = Block radius |
---|
| 44 | fractal_dim = Fractal dimension |
---|
| 45 | corr_length = correlation Length |
---|
| 46 | block_sld = SDL block |
---|
| 47 | solvent_sld = SDL solvent |
---|
| 48 | background = background |
---|
| 49 | """ |
---|
[829eee9] | 50 | ## Define parameters |
---|
| 51 | self.params = {} |
---|
[3db3895] | 52 | self.params['scale'] = 0.05 |
---|
| 53 | self.params['radius'] = 5.0 |
---|
| 54 | self.params['fractal_dim'] = 2.0 |
---|
| 55 | self.params['corr_length'] = 100.0 |
---|
| 56 | self.params['block_sld'] = 2.0e-6 |
---|
| 57 | self.params['solvent_sld'] = 6.0e-6 |
---|
| 58 | self.params['background'] = 0.0 |
---|
[829eee9] | 59 | |
---|
| 60 | |
---|
| 61 | ## Parameter details [units, min, max] |
---|
| 62 | self.details = {} |
---|
[36948c92] | 63 | self.details['scale'] = ['', None, None] |
---|
| 64 | self.details['radius'] = ['A', None, None] |
---|
| 65 | self.details['fractal_dim'] = ['', 0, None] |
---|
| 66 | self.details['corr_length'] = ['A', None, None] |
---|
| 67 | self.details['block_sld'] = ['A-2', None, None] |
---|
| 68 | self.details['solvent_sld'] = ['A-2', None, None] |
---|
[3db3895] | 69 | self.details['background'] = ['cm-1', None, None] |
---|
[829eee9] | 70 | |
---|
| 71 | |
---|
| 72 | def _Fractal(self, x): |
---|
| 73 | """ |
---|
| 74 | Evaluate |
---|
[3db3895] | 75 | F(x) = p(x) * s(x) + bkd |
---|
[829eee9] | 76 | """ |
---|
[36948c92] | 77 | if x<0 and self.params['fractal_dim']>0: |
---|
| 78 | raise ValueError, "negative number cannot be raised to a fractional power" |
---|
| 79 | |
---|
[3db3895] | 80 | return self.params['background']+ self._scatterRanDom(x)* self._Block(x) |
---|
[36948c92] | 81 | |
---|
[829eee9] | 82 | |
---|
| 83 | def _Block(self,x): |
---|
[3db3895] | 84 | return 1.0 + (math.sin((self.params['fractal_dim']-1.0) * math.atan(x * self.params['corr_length']))\ |
---|
| 85 | * self.params['fractal_dim'] * gamma(self.params['fractal_dim']-1.0))\ |
---|
[f629e346] | 86 | /( math.pow( (x*self.params['radius']), self.params['fractal_dim'])*\ |
---|
| 87 | math.pow( 1.0 + 1.0/((x**2)*(self.params['corr_length']**2)),(self.params['fractal_dim']-1.0)/2.0)) |
---|
[829eee9] | 88 | |
---|
| 89 | def _Spherical(self,x): |
---|
| 90 | """ |
---|
[f629e346] | 91 | F(x) = 3*[sin(x)-xcos(x)]/x**3 |
---|
[829eee9] | 92 | """ |
---|
[f629e346] | 93 | return 3.0*(math.sin(x)-x*math.cos(x))/(math.pow(x,3.0)) |
---|
[3db3895] | 94 | |
---|
[829eee9] | 95 | def _scatterRanDom(self,x): |
---|
| 96 | """ |
---|
| 97 | calculate p(x)= scale* V^(2)*delta^(2)* F(x*Radius)^(2) |
---|
| 98 | """ |
---|
[3db3895] | 99 | V =(4.0/3.0)*math.pi* math.pow(self.params['radius'],3.0) |
---|
| 100 | delta = self.params['block_sld']-self.params['solvent_sld'] |
---|
[829eee9] | 101 | |
---|
[f629e346] | 102 | return 1.0e8*self.params['scale']* V *(delta**2)*\ |
---|
[3db3895] | 103 | (self._Spherical(x*self.params['radius'])**2) |
---|
[829eee9] | 104 | |
---|
| 105 | def run(self, x = 0.0): |
---|
| 106 | """ Evaluate the model |
---|
[3db3895] | 107 | @param x: input q-value (float or [float, float] as [r, theta]) |
---|
[829eee9] | 108 | @return: (Fractal value) |
---|
| 109 | """ |
---|
| 110 | if x.__class__.__name__ == 'list': |
---|
[36948c92] | 111 | # Take absolute value of Q, since this model is really meant to |
---|
| 112 | # be defined in 1D for a given length of Q |
---|
[a55fac1] | 113 | #qx = math.fabs(x[0]*math.cos(x[1])) |
---|
| 114 | #qy = math.fabs(x[0]*math.sin(x[1])) |
---|
[36948c92] | 115 | |
---|
[a55fac1] | 116 | return self._Fractal(math.fabs(x[0])) |
---|
[829eee9] | 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) |
---|
| 121 | |
---|
| 122 | def runXY(self, x = 0.0): |
---|
| 123 | """ Evaluate the model |
---|
[3db3895] | 124 | @param x: input q-value (float or [float, float] as [qx, qy]) |
---|
[829eee9] | 125 | @return: Fractal value |
---|
| 126 | """ |
---|
| 127 | if x.__class__.__name__ == 'list': |
---|
[a55fac1] | 128 | q = math.sqrt(x[0]**2 + x[1]**2) |
---|
| 129 | return self._Fractal(q) |
---|
[829eee9] | 130 | elif x.__class__.__name__ == 'tuple': |
---|
| 131 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
| 132 | else: |
---|
| 133 | return self._Fractal(x) |
---|