[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 | """ |
---|
[7292e8a] | 77 | #if x<0 and self.params['fractal_dim']>0: |
---|
| 78 | # raise ValueError, "negative number cannot be raised to a fractional power" |
---|
| 79 | #if x==0 and self.params['fractal_dim']==0: |
---|
| 80 | # return 1+self.params['background'] |
---|
| 81 | #elif x<0 and self.params['fractal_dim']==0: |
---|
| 82 | # return 1e+32 |
---|
| 83 | #else: |
---|
[3db3895] | 84 | return self.params['background']+ self._scatterRanDom(x)* self._Block(x) |
---|
[36948c92] | 85 | |
---|
[829eee9] | 86 | |
---|
| 87 | def _Block(self,x): |
---|
[7292e8a] | 88 | #if self.params['fractal_dim']<0: |
---|
| 89 | # self.params['fractal_dim']=-self.params['fractal_dim'] |
---|
| 90 | try: |
---|
| 91 | if x<0: |
---|
| 92 | x=-x |
---|
| 93 | if self.params['radius']<0: |
---|
| 94 | self.params['radius']=-self.params['radius'] |
---|
| 95 | |
---|
| 96 | if x==0 or self.params['radius']==0 : |
---|
| 97 | return 1e+32 |
---|
| 98 | elif self.params['fractal_dim']==0: |
---|
| 99 | return 1.0 + (math.sin((self.params['fractal_dim']-1.0) * math.atan(x * self.params['corr_length']))\ |
---|
| 100 | * self.params['fractal_dim'] * gamma(self.params['fractal_dim']-1.0))\ |
---|
| 101 | *( math.pow( 1.0 + 1.0/((x**2)*(self.params['corr_length']**2)),1/2.0)) |
---|
| 102 | elif self.params['corr_length']==0 or self.params['fractal_dim']==1: |
---|
| 103 | return 1.0 + (math.sin((self.params['fractal_dim']-1.0) * math.atan(x * self.params['corr_length']))\ |
---|
| 104 | * self.params['fractal_dim'] * gamma(self.params['fractal_dim']-1.0))\ |
---|
| 105 | /( math.pow( (x*self.params['radius']), self.params['fractal_dim'])) |
---|
| 106 | |
---|
| 107 | elif self.params['fractal_dim']<1: |
---|
| 108 | return 1.0 + (math.sin((self.params['fractal_dim']-1.0) * math.atan(x * self.params['corr_length']))\ |
---|
| 109 | * self.params['fractal_dim'] * gamma(self.params['fractal_dim']-1.0))\ |
---|
| 110 | /( math.pow( (x*self.params['radius']), self.params['fractal_dim']))*\ |
---|
| 111 | math.pow( 1.0 + 1.0/((x**2)*(self.params['corr_length']**2)),(1-self.params['fractal_dim'])/2.0) |
---|
| 112 | else: |
---|
| 113 | return 1.0 + (math.sin((self.params['fractal_dim']-1.0) * math.atan(x * self.params['corr_length']))\ |
---|
| 114 | * self.params['fractal_dim'] * gamma(self.params['fractal_dim']-1.0))\ |
---|
| 115 | / math.pow( (x*self.params['radius']), self.params['fractal_dim'])\ |
---|
| 116 | /math.pow( 1.0 + 1.0/((x**2)*(self.params['corr_length']**2)),(self.params['fractal_dim']-1.0)/2.0) |
---|
| 117 | except: |
---|
| 118 | return 1 # Need a real fix. |
---|
[829eee9] | 119 | def _Spherical(self,x): |
---|
| 120 | """ |
---|
[f629e346] | 121 | F(x) = 3*[sin(x)-xcos(x)]/x**3 |
---|
[829eee9] | 122 | """ |
---|
[7292e8a] | 123 | if x==0: |
---|
| 124 | return 0 |
---|
| 125 | else: |
---|
| 126 | return 3.0*(math.sin(x)-x*math.cos(x))/(math.pow(x,3.0)) |
---|
[3db3895] | 127 | |
---|
[829eee9] | 128 | def _scatterRanDom(self,x): |
---|
| 129 | """ |
---|
| 130 | calculate p(x)= scale* V^(2)*delta^(2)* F(x*Radius)^(2) |
---|
| 131 | """ |
---|
[3db3895] | 132 | V =(4.0/3.0)*math.pi* math.pow(self.params['radius'],3.0) |
---|
| 133 | delta = self.params['block_sld']-self.params['solvent_sld'] |
---|
[829eee9] | 134 | |
---|
[f629e346] | 135 | return 1.0e8*self.params['scale']* V *(delta**2)*\ |
---|
[3db3895] | 136 | (self._Spherical(x*self.params['radius'])**2) |
---|
[829eee9] | 137 | |
---|
| 138 | def run(self, x = 0.0): |
---|
| 139 | """ Evaluate the model |
---|
[3db3895] | 140 | @param x: input q-value (float or [float, float] as [r, theta]) |
---|
[829eee9] | 141 | @return: (Fractal value) |
---|
| 142 | """ |
---|
| 143 | if x.__class__.__name__ == 'list': |
---|
[36948c92] | 144 | # Take absolute value of Q, since this model is really meant to |
---|
| 145 | # be defined in 1D for a given length of Q |
---|
[a55fac1] | 146 | #qx = math.fabs(x[0]*math.cos(x[1])) |
---|
| 147 | #qy = math.fabs(x[0]*math.sin(x[1])) |
---|
[36948c92] | 148 | |
---|
[a55fac1] | 149 | return self._Fractal(math.fabs(x[0])) |
---|
[829eee9] | 150 | elif x.__class__.__name__ == 'tuple': |
---|
| 151 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
| 152 | else: |
---|
| 153 | return self._Fractal(x) |
---|
| 154 | |
---|
| 155 | def runXY(self, x = 0.0): |
---|
| 156 | """ Evaluate the model |
---|
[3db3895] | 157 | @param x: input q-value (float or [float, float] as [qx, qy]) |
---|
[829eee9] | 158 | @return: Fractal value |
---|
| 159 | """ |
---|
| 160 | if x.__class__.__name__ == 'list': |
---|
[a55fac1] | 161 | q = math.sqrt(x[0]**2 + x[1]**2) |
---|
| 162 | return self._Fractal(q) |
---|
[829eee9] | 163 | elif x.__class__.__name__ == 'tuple': |
---|
| 164 | raise ValueError, "Tuples are not allowed as input to BaseComponent models" |
---|
| 165 | else: |
---|
| 166 | return self._Fractal(x) |
---|