source: sasview/sansmodels/src/sans/models/FractalModel.py @ f52bea1

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since f52bea1 was f629e346, checked in by Mathieu Doucet <doucetm@…>, 17 years ago

Updated tests to reflect proper math (took test funcs from Igor). Still need to look into incoherence btw Igor docs and Igor code!

  • Property mode set to 100644
File size: 4.0 KB
Line 
1#!/usr/bin/env python
2"""
3   
4    Provide F(x)= P(x)*S(x) + bkd
5    Fractal as a BaseComponent model
6"""
7
8from sans.models.BaseComponent import BaseComponent
9import math
10from scipy.special import gamma
11
12class 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:
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
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 = {}
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
47       
48
49        ## Parameter details [units, min, max]
50        self.details = {}
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]
58       
59               
60    def _Fractal(self, x):
61        """
62            Evaluate 
63            F(x) = p(x) * s(x) + bkd 
64        """
65        return self.params['background']+ self._scatterRanDom(x)* self._Block(x)
66   
67    def _Block(self,x):
68       
69       
70       
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))\
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))   
75           
76    def _Spherical(self,x):
77        """
78            F(x) = 3*[sin(x)-xcos(x)]/x**3
79        """
80        return 3.0*(math.sin(x)-x*math.cos(x))/(math.pow(x,3.0))
81       
82    def _scatterRanDom(self,x):
83        """
84             calculate p(x)= scale* V^(2)*delta^(2)* F(x*Radius)^(2)
85        """
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']
88       
89        return 1.0e8*self.params['scale']* V *(delta**2)*\
90                (self._Spherical(x*self.params['radius'])**2)
91       
92    def run(self, x = 0.0):
93        """ Evaluate the model
94            @param x: input q-value (float or [float, float] as [r, theta])
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
106            @param x: input q-value (float or [float, float] as [qx, qy])
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)
Note: See TracBrowser for help on using the repository browser.