source: sasview/sansmodels/src/sans/models/PeakLorentzModel.py @ 27f1ddb

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 27f1ddb was 27fea3f, checked in by Gervaise Alina <gervyh@…>, 15 years ago

add a few models 1D

  • Property mode set to 100644
File size: 3.2 KB
Line 
1#!/usr/bin/env python
2"""
3    Model describes a Lorentzian shaped peak including a flat background
4    Provide F(q) = scale/(1+[(q-q0)/B]^2 ) + background
5    PeakLorentzModel function as a BaseComponent model
6"""
7
8from sans.models.BaseComponent import BaseComponent
9import math
10
11class PeakLorentzModel(BaseComponent):
12   
13    """
14        Class that evaluates a gaussian  shaped peak.
15       
16        F(q) = scale/(1+[(q-q0)/B]^2 ) + background
17       
18        The model has three parameters:
19            scale     =  scale
20            q0        =  peak position
21            B         =  ( hwhm) half-width-halfmaximum
22            background=  incoherent background
23    """
24       
25    def __init__(self):
26        """ Initialization """
27       
28        # Initialize BaseComponent first, then sphere
29        BaseComponent.__init__(self)
30       
31        ## Name of the model
32        self.name = "Peak Lorentz Model"
33        self.description=""" F(q) = scale/(1+[(q-q0)/B]^2 ) + background
34       
35        The model has three parameters:
36        scale     =  scale
37        q0        =  peak position
38        B         =  ( hwhm) half-width-halfmaximum
39        background=  incoherent background"""
40        ## Define parameters
41        self.params = {}
42        self.params['scale']              = 100.0
43        self.params['q0']                 = 0.05
44        self.params['B']              = 0.005
45        self.params['background']         = 1.0
46
47        ## Parameter details [units, min, max]
48        self.details = {}
49        self.details['q0']            = ['[1/A]', None, None]
50        self.details['scale']             = ['', 0, None]
51        self.details['B']            = ['[1/A]', None, None]
52        self.details['background']        = ['[1/cm]', None, None]
53        #list of parameter that cannot be fitted
54        self.fixed= [] 
55           
56    def _PeakLorentz(self, x):
57        """
58            Evaluate  F(x) = scale/(1+[(x-q0)/B]^2 ) + background
59           
60        """
61        return self.params['scale']/(1+\
62                         math.pow((x - self.params['q0'])/self.params['B'],2)) \
63                            + self.params['background']
64       
65   
66    def run(self, x = 0.0):
67        """ Evaluate the model
68            @param x: input q-value (float or [float, float] as [r, theta])
69            @return: Peak Lorentzian value
70        """
71        if x.__class__.__name__ == 'list':
72            return self._PeakLorentz(x[0])
73        elif x.__class__.__name__ == 'tuple':
74            raise ValueError, "Tuples are not allowed as input to BaseComponent models"
75        else:
76            return self._PeakLorentz(x)
77   
78    def runXY(self, x = 0.0):
79        """ Evaluate the model
80            @param x: input q-value (float or [float, float] as [qx, qy])
81            @return: Peak Lorentzian value
82        """
83        if x.__class__.__name__ == 'list':
84            q = math.sqrt(x[0]**2 + x[1]**2)
85            return self._PeakLorentz(q)
86        elif x.__class__.__name__ == 'tuple':
87            raise ValueError, "Tuples are not allowed as input to BaseComponent models"
88        else:
89            return self._PeakLorentz(x)
Note: See TracBrowser for help on using the repository browser.