Changeset 14e9eb3 in sasview for calculator/sld_calculator.py


Ignore:
Timestamp:
May 28, 2010 1:24:47 PM (14 years ago)
Author:
Gervaise Alina <gervyh@…>
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:
25a2ee3
Parents:
2882fb5
Message:

working on documentation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • calculator/sld_calculator.py

    rbeb374a r14e9eb3  
    11""" 
    2     This module intends to compute the neutron scattering length density  
    3     of a molecule. It uses methods of the periodictable package to provide  
    4     easy user interface for  Sld calculator applications. 
     2This module intends to compute the neutron scattering length density  
     3of a molecule.It uses methods of the periodictable package to provide  
     4easy user interface for  Sld calculator applications. 
    55""" 
    66 
     
    1010from periodictable.constants import avogadro_number 
    1111import periodictable.nsf 
    12 neutron_sld_from_atoms= periodictable.nsf.neutron_sld_from_atoms  
     12neutron_sld_from_atoms = periodictable.nsf.neutron_sld_from_atoms  
    1313 
    1414class SldCalculator(object): 
     
    1616    Given a molecule, a density and a wavelength, this class  
    1717    determine scattering length density. 
     18     
     19    Example: To get the sld value and the length 1/e the following  
     20    methods need to be called in this later order:: 
     21             formula = "H2O" 
     22             density = 1.0 
     23             wavelength = 6.0 
     24             sld_calculator = SldCalculator() 
     25             sld_calculator.set_value(formula, density, wavelength)  
     26             sld_real, sld_im, _ = sld_calculator.calculate_neutron_sld() 
     27             result : sld = sld_real +i sld_im 
     28              
     29    Note: **set_value()** and **calculate_neutron_sld()** methods must 
     30    be called in this order prior calling **calculate_length()** to get  
     31    the proper result. 
    1832    """ 
    1933    def __init__(self): 
     34        #Private variable 
     35        self._volume = 0.0 
     36        #Inputs 
    2037        self.wavelength  = 6.0 
     38        self.sld_formula = None 
     39        self.density = None 
     40        #Outputs 
     41        self.sld_real = None 
     42        self.sld_im = None 
    2143        self.coherence   = 0.0 
    2244        self.absorption  = 0.0 
    2345        self.incoherence = 0.0 
    24         self.sld_formula = None 
    25         self.volume = 0.0 
    26         self.density = None 
    27         self.length= 0.0 
     46        self.length = 0.0 
    2847         
    29     def set_value(self, user_formula, density, wavelength=6.0): 
     48    def set_value(self, formula, density, wavelength=6.0): 
    3049        """ 
    3150        Store values into the sld calculator and compute the corresponding 
     
    3453        self.wavelength = wavelength 
    3554        self.density    = float(density) 
    36         self.sld_formula = formula(str(user_formula), density=self.density) 
     55        self.sld_formula = formula(str(formula), density=self.density) 
    3756        
    3857        if self.density == 0: 
    3958            raise ZeroDivisionError("integer division or modulo\ 
    4059                         by zero for density") 
    41         self.volume = (self.sld_formula.mass / self.density) / avogadro_number\ 
     60        self._volume = (self.sld_formula.mass / self.density) / avogadro_number\ 
    4261                                *1.0e24    
    4362         
     
    6382        """ 
    6483        Compute the neutron SLD for a given molecule 
    65         @return absorp: absorption 
    66         return coh: coherence cross section 
     84        @return sld_real : real part of the sld value 
     85        @return sld_im: imaginary part of the sld value 
    6786        @return inc: incoherence cross section 
    6887        """ 
     
    7291            return  
    7392        atom = self.sld_formula.atoms 
    74         coh, absorp, inc = neutron_sld_from_atoms(atom, self.density,  
     93        sld_real, sld_im, inc = neutron_sld_from_atoms(atom, self.density,  
    7594                                                  self.wavelength) 
    76         #Don't know if value is return in cm or  cm^(-1).assume return in cm 
    77         # to match result of neutron inc of Alan calculator 
    7895        self.incoherence = inc 
    79         self.absorption = absorp  
    80         self.coherence  = coh 
    81         return self.coherence, self.absorption, self.incoherence 
    82      
     96        self.sld_real = sld_real 
     97        self.sld_im  = sld_im 
     98        return self.sld_real, self.sld_im, self.incoherence 
    8399     
    84100    def calculate_length(self): 
     
    87103        """ 
    88104        self.length = (self.coherence + self.absorption +\ 
    89                             self.incoherence) / self.volume 
     105                            self.incoherence) / self._volume 
    90106        return self.length 
    91107         
    92          
    93     def calculate_coherence_im(self): 
    94         """ 
    95         Compute imaginary part of the absorption  
    96         """ 
    97         atom = self.sld_formula.atoms  
    98         #im: imaginary part of neutron SLD 
    99         im = 0 
    100         for el, count in atom.iteritems(): 
    101             if el.neutron.b_c_i is not None: 
    102                 im += el.neutron.b_c_i * count  
    103                  
    104         if self.volume != 0: 
    105             im = im/self.volume 
    106         else: 
    107             raise ZeroDivisionError("integer division or modulo\ 
    108                                  by zero for volume") 
    109         return im 
Note: See TracChangeset for help on using the changeset viewer.