source: sasview/calculator/sld_calculator.py @ 6d20b46

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 6d20b46 was 89108a0, checked in by Gervaise Alina <gervyh@…>, 15 years ago

commit unittest for sld calculator

  • Property mode set to 100644
File size: 4.0 KB
Line 
1"""
2    This module intends to compute the neutron scattering length density of molecule
3    @author: Gervaise B. Alina
4"""
5
6import periodictable
7from periodictable import formula
8from periodictable.xsf import xray_energy, xray_sld_from_atoms
9from periodictable.constants import avogadro_number
10import periodictable.nsf
11neutron_sld_from_atoms= periodictable.nsf.neutron_sld_from_atoms
12
13class SldCalculator(object):
14    """
15        compute neutron SLD and related parameters
16    """
17    def __init__(self):
18        self.wavelength  = 6.0
19        self.coherence   = 0.0
20        self.absorption  = 0.0
21        self.incoherence = 0.0
22        self.sld_formula = None
23        self.volume = 0.0
24        self.density = None
25        self.length= 0.0
26       
27    def set_value(self, user_formula, density, wavelength=6.0):
28        """
29            Store values of density and wavelength into the calculator
30            and compute the volume
31        """
32        self.wavelength = wavelength
33        self.density    = float(density)
34        self.sld_formula = formula(str(user_formula), density=self.density)
35       
36        if self.density == 0:
37            raise ZeroDivisionError("integer division or modulo\
38                         by zero for density")
39            return 
40        self.volume = (self.sld_formula.mass / self.density) / avogadro_number\
41                                *1.0e24   
42       
43       
44    def calculate_xray_sld(self, element):
45        """
46            Get an element and compute the corresponding SLD for a given formula
47            @param element:  elementis a string of existing atom
48        """
49        myformula = formula(str(element))
50        if len(myformula.atoms) != 1:
51            return 
52        element = myformula.atoms.keys()[0] 
53        energy = xray_energy(element.K_alpha)
54        atom = self.sld_formula.atoms
55        atom_reel, atom_im = xray_sld_from_atoms(atom,
56                                              density= self.density,
57                                              energy= energy)
58        return atom_reel, atom_im
59     
60       
61    def calculate_neutron_sld(self):
62        """
63            Compute the neutron SLD for a given molecule
64            @return absorp: absorption
65            @return coh: coherence cross section
66            @return inc: incoherence cross section
67        """
68        if self.density == 0:
69            raise ZeroDivisionError("integer division or modulo\
70                         by zero for density")
71            return 
72        atom = self.sld_formula.atoms
73        coh, absorp, inc = neutron_sld_from_atoms(atom, self.density, 
74                                                  self.wavelength)
75        #Don't know if value is return in cm or  cm^(-1).assume return in cm
76        # to match result of neutron inc of Alan calculator
77        self.incoherence = inc * 1/10
78        #Doesn't match result of Alan calculator for absorption factor of 2
79        #multiplication of 100 is going around
80        self.absorption = absorp * 2 * 100
81        self.coherence  = coh
82        return self.coherence, self.absorption, self.incoherence
83   
84   
85    def calculate_length(self):
86        """
87            Compute the neutron 1/e length
88        """
89        self.length = (self.coherence + self.absorption +\
90                            self.incoherence) / self.volume
91        return self.length
92       
93       
94    def calculate_coherence_im(self):
95        """
96            Compute imaginary part of the absorption
97        """
98        atom = self.sld_formula.atoms
99        #im: imaginary part of neutron SLD
100        im = 0
101        for el, count in atom.iteritems():
102            if el.neutron.b_c_i is not None:
103                im += el.neutron.b_c_i * count
104               
105        if self.volume != 0:
106            im = im/self.volume
107        else:
108            raise ZeroDivisionError("integer division or modulo\
109                                 by zero for volume")
110        return im
Note: See TracBrowser for help on using the repository browser.