source: sasview/sansmodels/src/sans/models/PowerLawModel.py @ e71440c

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 e71440c was a55fac1, checked in by Mathieu Doucet <doucetm@…>, 17 years ago

Modified 2D non-shape models to be the same as 1D when not defined otherwise

  • Property mode set to 100644
File size: 2.7 KB
Line 
1#!/usr/bin/env python
2"""
3    Provide F(x) = scale* (x)^(-m) + bkd
4    Power law function as a BaseComponent model
5"""
6
7from sans.models.BaseComponent import BaseComponent
8import math
9
10class PowerLawModel(BaseComponent):
11   
12    """
13        Class that evaluates a Power_Law model.
14       
15        F(x) = scale* (x)^(-m) + bkd
16       
17        The model has three parameters:
18            m     =  power
19            scale  =  scale factor
20            bkd    =  incoherent background
21    """
22       
23    def __init__(self):
24        """ Initialization """
25       
26        # Initialize BaseComponent first, then sphere
27        BaseComponent.__init__(self)
28       
29        ## Name of the model
30        self.name = "Power_Law"
31
32        ## Define parameters
33        self.params = {}
34        self.params['m']            = 4.0
35        self.params['scale']        = 1.0
36        self.params['background']   = 0.0
37
38        ## Parameter details [units, min, max]
39        self.details = {}
40        self.details['m']           = ['', 0,    None]
41        self.details['scale']       = ['', None, None]
42        self.details['background']  = ['', None, None]
43               
44    def _PowerLaw(self, x):
45        """
46            Evaluate  F(x) = scale* (x)^(-m) + bkd
47           
48        """
49        if x<0 and self.params['m']>0:
50            raise ValueError, "negative number cannot be raised to a fractional power"
51       
52        return self.params['scale']*math.pow(x ,-1.0*self.params['m'])\
53                + self.params['background']
54       
55    def run(self, x = 0.0):
56        """ Evaluate the model
57            @param x: input q-value (float or [float, float] as [r, theta])
58            @return: (PowerLaw value)
59        """
60        if x.__class__.__name__ == 'list':
61            # Take absolute value of Q, since this model is really meant to
62            # be defined in 1D for a given length of Q
63            #qx = math.fabs(x[0]*math.cos(x[1]))
64            #qy = math.fabs(x[0]*math.sin(x[1]))
65            return self._PowerLaw(math.fabs(x[0]))
66        elif x.__class__.__name__ == 'tuple':
67            raise ValueError, "Tuples are not allowed as input to BaseComponent models"
68        else:
69            return self._PowerLaw(x)
70   
71    def runXY(self, x = 0.0):
72        """ Evaluate the model
73            @param x: input q-value (float or [float, float] as [qx, qy])
74            @return: PowerLaw value
75        """
76        if x.__class__.__name__ == 'list':
77            q = math.sqrt(x[0]**2 + x[1]**2)
78            return self._PowerLaw(q)
79        elif x.__class__.__name__ == 'tuple':
80            raise ValueError, "Tuples are not allowed as input to BaseComponent models"
81        else:
82            return self._PowerLaw(x)
Note: See TracBrowser for help on using the repository browser.