source: sasview/sansmodels/src/sans/models/TwoLorentzianModel.py @ 9055b06

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 9055b06 was 18695bf, checked in by Jae Cho <jhjcho@…>, 14 years ago

replaced math.pow to numpy.power since math.pow is not working with list of x values(remember we use the list in fitting for speed)

  • Property mode set to 100644
File size: 3.5 KB
Line 
1#!/usr/bin/env python
2"""
3TwoLorentzianModel function as a BaseComponent model
4"""
5
6from sans.models.BaseComponent import BaseComponent
7from numpy import power
8
9class TwoLorentzianModel(BaseComponent):
10    """
11    Class that evaluates a TwoLorentzianModel.
12    I(q) = II(q) = scale_1/(1.0 + pow((q*length_1),exponent_1))
13    + scale_2/(1.0 + pow((q*length_2),exponent_2) )+ background
14    """
15       
16    def __init__(self):
17        """ Initialization """
18       
19        # Initialize BaseComponent first, then sphere
20        BaseComponent.__init__(self)
21       
22        ## Name of the model
23        self.name = "TwoLorentzian"
24        self.description="""I(q) = scale_1/(1.0 + pow((q*length_1),exponent_1))
25             + scale_2/(1.0 + pow((q*length_2),exponent_2) )+ background
26             List of default parameters:
27             scale_1 = Lorentzian term scaling #1
28             length_1 = Lorentzian screening length #1 [A]
29             exponent_1 = Lorentzian exponent #1
30             scale_2 = Lorentzian term scaling #2
31             length_2 = Lorentzian screening length #2 [A]
32             exponent_2 = Lorentzian exponent #2
33             background = Incoherent background
34        """
35        ## Define parameters
36        self.params = {}
37        self.params['scale_1']  = 10.0
38        self.params['length_1']     = 100.0
39        self.params['exponent_1']     = 3.0
40        self.params['scale_2']  = 1.0
41        self.params['length_2']     = 10.0
42        self.params['exponent_2']     = 2.0
43        self.params['background']     = 0.1
44        ## Parameter details [units, min, max]
45        self.details = {}
46        self.details['scale_1'] = ['', None, None]
47        self.details['length_1'] = ['A', None, None]
48        self.details['exponent_1'] =  ['', None, None]
49        self.details['scale_2']  =  ['', None, None]
50        self.details['length_2']  =   ['A', None, None]
51        self.details['exponent_2']  =   ['', None, None]
52        self.details['background']   =  ['[1/cm]', None, None]
53
54        #list of parameter that cannot be fitted
55        self.fixed= [] 
56    def _twolorentzian(self, x):
57        """
58        Model definition
59        """
60        inten = self.params['scale_1']/(1.0 + \
61                power((x*self.params['length_1']),self.params['exponent_1']))
62        inten += self.params['scale_2']/(1.0 + \
63                power((x*self.params['length_2']),self.params['exponent_2']))
64        inten += self.params['background']
65
66        return inten 
67   
68    def run(self, x = 0.0):
69        """
70        Evaluate the model
71       
72        param x: input q-value (float or [float, float] as [r, theta])
73        return: (scattering value)
74        """
75        if x.__class__.__name__ == 'list':
76            return self._twolorentzian(x[0])
77        elif x.__class__.__name__ == 'tuple':
78            raise ValueError, "Tuples are not allowed as input to BaseComponent models"
79        else:
80            return self._twolorentzian(x)
81   
82    def runXY(self, x = 0.0):
83        """
84        Evaluate the model
85       
86        param x: input q-value (float or [float, float] as [qx, qy])
87        return: scattering value
88        """
89        if x.__class__.__name__ == 'list':
90            q = math.sqrt(x[0]**2 + x[1]**2)
91            return self._twolorentzian(q)
92        elif x.__class__.__name__ == 'tuple':
93            raise ValueError, "Tuples are not allowed as input to BaseComponent models"
94        else:
95            return self._twolorentzian(x)
Note: See TracBrowser for help on using the repository browser.