source: sasview/calculator/sld_calculator.py @ 6137b150

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 6137b150 was 14e9eb3, checked in by Gervaise Alina <gervyh@…>, 14 years ago

working on documentation

  • Property mode set to 100644
File size: 3.9 KB
Line 
1"""
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.
5"""
6
7import periodictable
8from periodictable import formula
9from periodictable.xsf import xray_energy, xray_sld_from_atoms
10from periodictable.constants import avogadro_number
11import periodictable.nsf
12neutron_sld_from_atoms = periodictable.nsf.neutron_sld_from_atoms
13
14class SldCalculator(object):
15    """
16    Given a molecule, a density and a wavelength, this class
17    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.
32    """
33    def __init__(self):
34        #Private variable
35        self._volume = 0.0
36        #Inputs
37        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
43        self.coherence   = 0.0
44        self.absorption  = 0.0
45        self.incoherence = 0.0
46        self.length = 0.0
47       
48    def set_value(self, formula, density, wavelength=6.0):
49        """
50        Store values into the sld calculator and compute the corresponding
51        volume.
52        """
53        self.wavelength = wavelength
54        self.density    = float(density)
55        self.sld_formula = formula(str(formula), density=self.density)
56       
57        if self.density == 0:
58            raise ZeroDivisionError("integer division or modulo\
59                         by zero for density")
60        self._volume = (self.sld_formula.mass / self.density) / avogadro_number\
61                                *1.0e24   
62       
63       
64    def calculate_xray_sld(self, element):
65        """
66        Get an element and compute the corresponding SLD for a given formula
67        @param element:  elementis a string of existing atom
68        """
69        myformula = formula(str(element))
70        if len(myformula.atoms) != 1:
71            return 
72        element = myformula.atoms.keys()[0] 
73        energy = xray_energy(element.K_alpha)
74        atom = self.sld_formula.atoms
75        atom_reel, atom_im = xray_sld_from_atoms(atom,
76                                              density= self.density,
77                                              energy= energy)
78        return atom_reel, atom_im
79     
80       
81    def calculate_neutron_sld(self):
82        """
83        Compute the neutron SLD for a given molecule
84        @return sld_real : real part of the sld value
85        @return sld_im: imaginary part of the sld value
86        @return inc: incoherence cross section
87        """
88        if self.density == 0:
89            raise ZeroDivisionError("integer division or modulo\
90                         by zero for density")
91            return 
92        atom = self.sld_formula.atoms
93        sld_real, sld_im, inc = neutron_sld_from_atoms(atom, self.density, 
94                                                  self.wavelength)
95        self.incoherence = inc
96        self.sld_real = sld_real
97        self.sld_im  = sld_im
98        return self.sld_real, self.sld_im, self.incoherence
99   
100    def calculate_length(self):
101        """
102        Compute the neutron 1/e length
103        """
104        self.length = (self.coherence + self.absorption +\
105                            self.incoherence) / self._volume
106        return self.length
107       
Note: See TracBrowser for help on using the repository browser.