source: sasview/src/sas/models/TwoPowerLawModel.py @ 898a8b9

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 898a8b9 was 79492222, checked in by krzywon, 10 years ago

Changed the file and folder names to remove all SANS references.

  • Property mode set to 100644
File size: 3.4 KB
Line 
1#!/usr/bin/env python
2"""
3Provide I(q) = A*pow(qval,-1.0*m1) for q<=qc
4    =scale*pow(qval,-1.0*m2) for q>qc
5TwoPowerLaw function as a BaseComponent model
6"""
7
8from sas.models.BaseComponent import BaseComponent
9from numpy import power
10import math
11
12class TwoPowerLawModel(BaseComponent):
13    """
14    Class that evaluates a TwoPowerLawModel.
15
16    I(q) = coef_A*pow(qval,-1.0*power1) for q<=qc
17        =C*pow(qval,-1.0*power2) for q>qc
18    where C=coef_A*pow(qc,-1.0*power1)/pow(qc,-1.0*power2).
19   
20    List of default parameters:
21   
22    * coef_A = coefficient
23    * power1 = (-) Power @ low Q
24    * power2 = (-) Power @ high Q
25    * qc = crossover Q-value
26    * background = incoherent background
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 = "TwoPowerLaw"
37        self.description="""I(q) = coef_A*pow(qval,-1.0*power1) for q<=qc
38            =C*pow(qval,-1.0*power2) for q>qc
39            where C=coef_A*pow(qc,-1.0*power1)/pow(qc,-1.0*power2).
40             List of default parameters:
41             coef_A = coefficient
42             power1 = (-) Power @ low Q
43             power2 = (-) Power @ high Q
44             qc = crossover Q-value
45             background = incoherent background
46        """
47        ## Define parameters
48        self.params = {}
49        self.params['coef_A']  = 1.0
50        self.params['power1']     = 1.0
51        self.params['power2']  = 4.0
52        self.params['qc']     = 0.04
53        self.params['background']     = 0.0
54        ## Parameter details [units, min, max]
55        self.details = {}
56        self.details['coef_A'] = ['', None, None]
57        self.details['power1'] =  ['', None, None]
58        self.details['power2']  =  ['', None, None]
59        self.details['qc']  =   ['1/A', None, None]
60        self.details['background']   =  ['[1/cm]', None, None]
61
62        #list of parameter that cannot be fitted
63        self.fixed= [] 
64    def _twopowerlaw(self, x):
65        """
66        Model definition
67        """
68        qc= self.params['qc']
69        if(x<=qc):
70            inten = self.params['coef_A']*power(x,-1.0*self.params['power1'])
71        else:
72            scale = self.params['coef_A']*power(qc,-1.0*self.params['power1']) \
73                                    / power(qc,-1.0*self.params['power2'])
74            inten = scale*power(x,-1.0*self.params['power2'])
75        inten += self.params['background']
76
77        return inten 
78   
79    def run(self, x = 0.0):
80        """ Evaluate the model
81            @param x: input q-value (float or [float, float] as [r, theta])
82            @return: (guinier value)
83        """
84        if x.__class__.__name__ == 'list':
85            return self._twopowerlaw(x[0])
86        elif x.__class__.__name__ == 'tuple':
87            raise ValueError, "Tuples are not allowed as input to BaseComponent models"
88        else:
89            return self._twopowerlaw(x)
90   
91    def runXY(self, x = 0.0):
92        """ Evaluate the model
93            @param x: input q-value (float or [float, float] as [qx, qy])
94            @return: guinier value
95        """
96        if x.__class__.__name__ == 'list':
97            q = math.sqrt(x[0]**2 + x[1]**2)
98            return self._twopowerlaw(q)
99        elif x.__class__.__name__ == 'tuple':
100            raise ValueError, "Tuples are not allowed as input to BaseComponent models"
101        else:
102            return self._twopowerlaw(x)
Note: See TracBrowser for help on using the repository browser.