source: sasview/calculator/sld_calculator.py @ beb374a

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

working on documentation

  • Property mode set to 100644
File size: 3.9 KB
Line 
1"""
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.
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    def __init__(self):
20        self.wavelength  = 6.0
21        self.coherence   = 0.0
22        self.absorption  = 0.0
23        self.incoherence = 0.0
24        self.sld_formula = None
25        self.volume = 0.0
26        self.density = None
27        self.length= 0.0
28       
29    def set_value(self, user_formula, density, wavelength=6.0):
30        """
31        Store values into the sld calculator and compute the corresponding
32        volume.
33        """
34        self.wavelength = wavelength
35        self.density    = float(density)
36        self.sld_formula = formula(str(user_formula), density=self.density)
37       
38        if self.density == 0:
39            raise ZeroDivisionError("integer division or modulo\
40                         by zero for density")
41        self.volume = (self.sld_formula.mass / self.density) / avogadro_number\
42                                *1.0e24   
43       
44       
45    def calculate_xray_sld(self, element):
46        """
47        Get an element and compute the corresponding SLD for a given formula
48        @param element:  elementis a string of existing atom
49        """
50        myformula = formula(str(element))
51        if len(myformula.atoms) != 1:
52            return 
53        element = myformula.atoms.keys()[0] 
54        energy = xray_energy(element.K_alpha)
55        atom = self.sld_formula.atoms
56        atom_reel, atom_im = xray_sld_from_atoms(atom,
57                                              density= self.density,
58                                              energy= energy)
59        return atom_reel, atom_im
60     
61       
62    def calculate_neutron_sld(self):
63        """
64        Compute the neutron SLD for a given molecule
65        @return absorp: absorption
66        return coh: coherence cross section
67        @return inc: incoherence cross section
68        """
69        if self.density == 0:
70            raise ZeroDivisionError("integer division or modulo\
71                         by zero for density")
72            return 
73        atom = self.sld_formula.atoms
74        coh, absorp, inc = neutron_sld_from_atoms(atom, self.density, 
75                                                  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
78        self.incoherence = inc
79        self.absorption = absorp
80        self.coherence  = coh
81        return self.coherence, self.absorption, self.incoherence
82   
83   
84    def calculate_length(self):
85        """
86        Compute the neutron 1/e length
87        """
88        self.length = (self.coherence + self.absorption +\
89                            self.incoherence) / self.volume
90        return self.length
91       
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 TracBrowser for help on using the repository browser.