Changeset ec658c85 in sasview


Ignore:
Timestamp:
Apr 12, 2010 10:33:56 AM (14 years ago)
Author:
Jae Cho <jhjcho@…>
Branches:
master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
19107f0
Parents:
c724ccd
Message:

python files of new models and unit tests

Location:
sansmodels/src/sans/models
Files:
5 added
3 edited

Legend:

Unmodified
Added
Removed
  • sansmodels/src/sans/models/FlexibleCylinderModel.py

    rf10063e rec658c85  
    3636         kuhn_length     = 100.0 [A] 
    3737         radius          = 20.0 [A] 
    38          sldCyl          = 6.3e-006 [1/A^(2)] 
    39          sldSolv         = 1e-006 [1/A^(2)] 
     38         sldCyl          = 1e-006 [1/A^(2)] 
     39         sldSolv         = 6.3e-006 [1/A^(2)] 
    4040         background      = 0.0001 [1/cm] 
    4141 
     
    5252        self.name = "FlexibleCylinderModel" 
    5353        ## Model description 
    54         self.description =""" Note : 'scale' and 'contrast' are both multiplicative factors in the 
     54        self.description =""" Note : scale and contrast=sldCyl-sldSolv are both multiplicative factors in the 
    5555                model and are perfectly correlated. One or 
    5656                both of these parameters must be held fixed 
  • sansmodels/src/sans/models/FractalModel.py

    r27972c1d rec658c85  
    11#!/usr/bin/env python 
    2 """  
    3      
    4     Provide F(x)= P(x)*S(x) + bkd 
    5     Fractal as a BaseComponent model 
     2""" 
     3        This software was developed by the University of Tennessee as part of the 
     4        Distributed Data Analysis of Neutron Scattering Experiments (DANSE) 
     5        project funded by the US National Science Foundation. 
     6 
     7        If you use DANSE applications to do scientific research that leads to 
     8        publication, we ask that you acknowledge the use of the software with the 
     9        following sentence: 
     10 
     11        "This work benefited from DANSE software developed under NSF award DMR-0520547." 
     12 
     13        copyright 2008, University of Tennessee 
     14""" 
     15 
     16""" Provide functionality for a C extension model 
     17 
     18        WARNING: THIS FILE WAS GENERATED BY WRAPPERGENERATOR.PY 
     19                 DO NOT MODIFY THIS FILE, MODIFY ..\c_extensions\fractal.h 
     20                 AND RE-RUN THE GENERATOR SCRIPT 
     21 
    622""" 
    723 
    824from sans.models.BaseComponent import BaseComponent 
    9 import math 
    10 from scipy.special import gamma 
     25from sans_extension.c_models import CFractalModel 
     26import copy     
     27     
     28class FractalModel(CFractalModel, BaseComponent): 
     29    """ Class that evaluates a FractalModel model.  
     30        This file was auto-generated from ..\c_extensions\fractal.h. 
     31        Refer to that file and the structure it contains 
     32        for details of the model. 
     33        List of default parameters: 
     34         scale           = 0.05  
     35         radius          = 5.0 [A] 
     36         fractal_dim     = 2.0  
     37         cor_length      = 100.0 [A] 
     38         sldBlock        = 2e-006 [1/A^(2)] 
     39         sldSolv         = 6.35e-006 [1/A^(2)] 
     40         background      = 0.0 [1/cm] 
    1141 
    12 class 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             
    2742    """ 
    2843         
     
    3045        """ Initialization """ 
    3146         
    32         # Initialize BaseComponent first, then fractal 
     47        # Initialize BaseComponent first, then sphere 
    3348        BaseComponent.__init__(self) 
     49        CFractalModel.__init__(self) 
    3450         
    3551        ## Name of the model 
    36         self.name = "Number Density Fractal" 
    37         self.description=""" 
    38         I(x)= P(x)*S(x) + bkd 
    39          
    40         p(x)= scale* V^(2)*delta^(2)* F(x*radius)^(2) 
    41         F(x) = 3*[sin(x)-x cos(x)]/x**3 
    42          
    43         The model has Seven parameters:  
    44         scale        =  Volume fraction 
    45         radius       =  Block radius 
    46         fractal_dim  =  Fractal dimension 
    47         corr_length  =  correlation Length 
    48         block_sld    =  SDL block 
    49         solvent_sld  =  SDL solvent 
    50         background   =  background 
    51         """ 
    52         ## Define parameters 
    53         self.params = {} 
    54         self.params['scale']       = 0.05 
    55         self.params['radius']      = 5.0 
    56         self.params['fractal_dim'] = 2.0 
    57         self.params['corr_length'] = 100.0 
    58         self.params['block_sld']   = 2.0e-6 
    59         self.params['solvent_sld'] = 6.0e-6 
    60         self.params['background']  = 0.0 
    61          
    62  
     52        self.name = "FractalModel" 
     53        ## Model description 
     54        self.description =""" The scattering intensity  I(x) = P(|x|)*S(|x|) + background, where 
     55                p(x)= scale * V * delta^(2)* F(x*radius)^(2) 
     56                F(x) = 3*[sin(x)-x cos(x)]/x**3 
     57                where delta = sldBlock -sldSolv. 
     58                scale        =  scale factor * Volume fraction 
     59                radius       =  Block radius 
     60                fractal_dim  =  Fractal dimension 
     61                cor_length  =  Correlation Length 
     62                sldBlock    =  SDL block 
     63                sldSolv  =  SDL solvent 
     64                background   =  background""" 
     65        
    6366        ## Parameter details [units, min, max] 
    6467        self.details = {} 
    65         self.details['scale']       = ['',     None, None] 
    66         self.details['radius']      = ['[A]',    None, None] 
    67         self.details['fractal_dim'] = ['',       0,  None] 
    68         self.details['corr_length'] = ['[A]',    None, None] 
    69         self.details['block_sld']   = ['[1/A^(2)]',  None, None] 
    70         self.details['solvent_sld'] = ['[1/A^(2)]',  None, None] 
    71         self.details['background']  = ['[1/cm]', None, None] 
    72         
    73                 
    74     def _Fractal(self, x): 
    75         """ 
    76             Evaluate   
    77             F(x) = p(x) * s(x) + bkd   
    78         """ 
    79         #if x<0 and self.params['fractal_dim']>0: 
    80          #   raise ValueError, "negative number cannot be raised to a fractional power" 
    81         #if x==0 and self.params['fractal_dim']==0: 
    82          #   return 1+self.params['background'] 
    83         #elif x<0 and self.params['fractal_dim']==0: 
    84         #    return 1e+32 
    85         #else: 
    86         return self.params['background']+ self._scatterRanDom(x)* self._Block(x) 
     68        self.details['scale'] = ['', None, None] 
     69        self.details['radius'] = ['[A]', None, None] 
     70        self.details['fractal_dim'] = ['', None, None] 
     71        self.details['cor_length'] = ['[A]', None, None] 
     72        self.details['sldBlock'] = ['[1/A^(2)]', None, None] 
     73        self.details['sldSolv'] = ['[1/A^(2)]', None, None] 
     74        self.details['background'] = ['[1/cm]', None, None] 
    8775 
    88      
    89     def _Block(self,x): 
    90         #if self.params['fractal_dim']<0: 
    91         #    self.params['fractal_dim']=-self.params['fractal_dim'] 
    92         try: 
    93             if x<0: 
    94                 x=-x 
    95             if self.params['radius']<0: 
    96                 self.params['radius']=-self.params['radius'] 
    97                  
    98             if x==0 or self.params['radius']==0 : 
    99                  return 1e+32 
    100             elif self.params['fractal_dim']==0: 
    101                 return 1.0 + (math.sin((self.params['fractal_dim']-1.0) * math.atan(x * self.params['corr_length']))\ 
    102                               * self.params['fractal_dim'] * gamma(self.params['fractal_dim']-1.0))\ 
    103                               *( math.pow( 1.0 + 1.0/((x**2)*(self.params['corr_length']**2)),1/2.0))  
    104             elif self.params['corr_length']==0 or self.params['fractal_dim']==1: 
    105                 return 1.0 + (math.sin((self.params['fractal_dim']-1.0) * math.atan(x * self.params['corr_length']))\ 
    106                               * self.params['fractal_dim'] * gamma(self.params['fractal_dim']-1.0))\ 
    107                               /( math.pow( (x*self.params['radius']), self.params['fractal_dim']))    
    108                  
    109             elif self.params['fractal_dim']<1: 
    110                 return 1.0 + (math.sin((self.params['fractal_dim']-1.0) * math.atan(x * self.params['corr_length']))\ 
    111                               * self.params['fractal_dim'] * gamma(self.params['fractal_dim']-1.0))\ 
    112                               /( math.pow( (x*self.params['radius']), self.params['fractal_dim']))*\ 
    113                                  math.pow( 1.0 + 1.0/((x**2)*(self.params['corr_length']**2)),(1-self.params['fractal_dim'])/2.0)    
    114             else: 
    115                 return 1.0 + (math.sin((self.params['fractal_dim']-1.0) * math.atan(x * self.params['corr_length']))\ 
    116                               * self.params['fractal_dim'] * gamma(self.params['fractal_dim']-1.0))\ 
    117                               / math.pow( (x*self.params['radius']), self.params['fractal_dim'])\ 
    118                                  /math.pow( 1.0 + 1.0/((x**2)*(self.params['corr_length']**2)),(self.params['fractal_dim']-1.0)/2.0)    
    119         except: 
    120             return 1 # Need a real fix.  
    121     def _Spherical(self,x): 
    122         """ 
    123             F(x) = 3*[sin(x)-xcos(x)]/x**3 
    124         """ 
    125         if x==0: 
    126             return 0 
    127         else: 
    128             return 3.0*(math.sin(x)-x*math.cos(x))/(math.pow(x,3.0)) 
     76        ## fittable parameters 
     77        self.fixed=[] 
    12978         
    130     def _scatterRanDom(self,x): 
    131         """ 
    132              calculate p(x)= scale* V^(2)*delta^(2)* F(x*Radius)^(2) 
    133         """ 
    134         V =(4.0/3.0)*math.pi* math.pow(self.params['radius'],3.0)  
    135         delta = self.params['block_sld']-self.params['solvent_sld'] 
     79        ## parameters with orientation 
     80        self.orientation_params =[] 
     81    
     82    def clone(self): 
     83        """ Return a identical copy of self """ 
     84        return self._clone(FractalModel())    
    13685         
    137         return 1.0e8*self.params['scale']* V *(delta**2)*\ 
    138                 (self._Spherical(x*self.params['radius'])**2) 
     86    def __getstate__(self): 
     87        """ return object state for pickling and copying """ 
     88        model_state = {'params': self.params, 'dispersion': self.dispersion, 'log': self.log} 
    13989         
     90        return self.__dict__, model_state 
     91         
     92    def __setstate__(self, state): 
     93        """ create object from pickled state """ 
     94         
     95        self.__dict__, model_state = state 
     96        self.params = model_state['params'] 
     97        self.dispersion = model_state['dispersion'] 
     98        self.log = model_state['log'] 
     99         
     100    
    140101    def run(self, x = 0.0): 
    141102        """ Evaluate the model 
    142             @param x: input q-value (float or [float, float] as [r, theta]) 
    143             @return: (Fractal value) 
     103            @param x: input q, or [q,phi] 
     104            @return: scattering function P(q) 
    144105        """ 
    145         if x.__class__.__name__ == 'list': 
    146             # Take absolute value of Q, since this model is really meant to 
    147             # be defined in 1D for a given length of Q 
    148             #qx = math.fabs(x[0]*math.cos(x[1])) 
    149             #qy = math.fabs(x[0]*math.sin(x[1])) 
    150              
    151             return self._Fractal(math.fabs(x[0])) 
    152         elif x.__class__.__name__ == 'tuple': 
    153             raise ValueError, "Tuples are not allowed as input to BaseComponent models" 
    154         else: 
    155             return self._Fractal(x) 
     106         
     107        return CFractalModel.run(self, x) 
    156108    
    157109    def runXY(self, x = 0.0): 
    158         """ Evaluate the model 
    159             @param x: input q-value (float or [float, float] as [qx, qy]) 
    160             @return: Fractal value 
     110        """ Evaluate the model in cartesian coordinates 
     111            @param x: input q, or [qx, qy] 
     112            @return: scattering function P(q) 
    161113        """ 
    162         if x.__class__.__name__ == 'list': 
    163             q = math.sqrt(x[0]**2 + x[1]**2) 
    164             return self._Fractal(q) 
    165         elif x.__class__.__name__ == 'tuple': 
    166             raise ValueError, "Tuples are not allowed as input to BaseComponent models" 
    167         else: 
    168             return self._Fractal(x) 
     114         
     115        return CFractalModel.runXY(self, x) 
     116         
     117    def evalDistribution(self, x = []): 
     118        """ Evaluate the model in cartesian coordinates 
     119            @param x: input q[], or [qx[], qy[]] 
     120            @return: scattering function P(q[]) 
     121        """ 
     122        return CFractalModel.evalDistribution(self, x) 
     123         
     124    def calculate_ER(self): 
     125        """ Calculate the effective radius for P(q)*S(q) 
     126            @return: the value of the effective radius 
     127        """        
     128        return CFractalModel.calculate_ER(self) 
     129         
     130    def set_dispersion(self, parameter, dispersion): 
     131        """ 
     132            Set the dispersion object for a model parameter 
     133            @param parameter: name of the parameter [string] 
     134            @dispersion: dispersion object of type DispersionModel 
     135        """ 
     136        return CFractalModel.set_dispersion(self, parameter, dispersion.cdisp) 
     137         
     138    
     139# End of file 
  • sansmodels/src/sans/models/test/utest_nonshape.py

    r8809e48 rec658c85  
    474474         
    475475        self.model.setParam('scale', self.phi)  
    476         self.model.setParam('Radius', self.r0)  
     476        self.model.setParam('radius', self.r0)  
    477477        self.model.setParam('fractal_dim',self.Df) 
    478         self.model.setParam('corr_length', self.corr) 
    479         self.model.setParam('block_sld', self.sldp)  
    480         self.model.setParam('solvent_sld', self.sldm)  
     478        self.model.setParam('cor_length', self.corr) 
     479        self.model.setParam('sldBlock', self.sldp)  
     480        self.model.setParam('sldSolv', self.sldm)  
    481481        self.model.setParam('background', self.bck) 
    482482         
Note: See TracChangeset for help on using the changeset viewer.