source: sasview/calculator/sld_calculator.py @ 8dcfb2e

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 8dcfb2e 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
RevLine 
[cfaf6d9]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       
[89108a0]27    def set_value(self, user_formula, density, wavelength=6.0):
[cfaf6d9]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)
[89108a0]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")
[cfaf6d9]39            return 
[89108a0]40        self.volume = (self.sld_formula.mass / self.density) / avogadro_number\
41                                *1.0e24   
[cfaf6d9]42       
43       
[89108a0]44    def calculate_xray_sld(self, element):
[cfaf6d9]45        """
46            Get an element and compute the corresponding SLD for a given formula
47            @param element:  elementis a string of existing atom
48        """
[89108a0]49        myformula = formula(str(element))
50        if len(myformula.atoms) != 1:
[cfaf6d9]51            return 
[89108a0]52        element = myformula.atoms.keys()[0] 
[cfaf6d9]53        energy = xray_energy(element.K_alpha)
54        atom = self.sld_formula.atoms
[89108a0]55        atom_reel, atom_im = xray_sld_from_atoms(atom,
[cfaf6d9]56                                              density= self.density,
[89108a0]57                                              energy= energy)
[cfaf6d9]58        return atom_reel, atom_im
59     
60       
[89108a0]61    def calculate_neutron_sld(self):
[cfaf6d9]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        """
[89108a0]68        if self.density == 0:
69            raise ZeroDivisionError("integer division or modulo\
70                         by zero for density")
[cfaf6d9]71            return 
72        atom = self.sld_formula.atoms
[89108a0]73        coh, absorp, inc = neutron_sld_from_atoms(atom, self.density, 
74                                                  self.wavelength)
[cfaf6d9]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
[89108a0]77        self.incoherence = inc * 1/10
[cfaf6d9]78        #Doesn't match result of Alan calculator for absorption factor of 2
79        #multiplication of 100 is going around
[89108a0]80        self.absorption = absorp * 2 * 100
[cfaf6d9]81        self.coherence  = coh
82        return self.coherence, self.absorption, self.incoherence
83   
84   
[89108a0]85    def calculate_length(self):
[cfaf6d9]86        """
87            Compute the neutron 1/e length
88        """
[89108a0]89        self.length = (self.coherence + self.absorption +\
90                            self.incoherence) / self.volume
[cfaf6d9]91        return self.length
92       
93       
[89108a0]94    def calculate_coherence_im(self):
[cfaf6d9]95        """
96            Compute imaginary part of the absorption
97        """
98        atom = self.sld_formula.atoms
99        #im: imaginary part of neutron SLD
[89108a0]100        im = 0
[cfaf6d9]101        for el, count in atom.iteritems():
[89108a0]102            if el.neutron.b_c_i is not None:
103                im += el.neutron.b_c_i * count
104               
105        if self.volume != 0:
[cfaf6d9]106            im = im/self.volume
107        else:
[89108a0]108            raise ZeroDivisionError("integer division or modulo\
109                                 by zero for volume")
[cfaf6d9]110        return im
Note: See TracBrowser for help on using the repository browser.