source: sasview/calculator/build/lib/sans/sld_calculator/sld_calculator.py @ cfaf6d9

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

calculator module created

  • Property mode set to 100644
File size: 3.8 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       
28    def setValue(self, user_formula, density, wavelength=6.0):
29        """
30            Store values of density and wavelength into the calculator
31            and compute the volume
32        """
33        self.wavelength = wavelength
34        self.density    = float(density)
35        self.sld_formula = formula(str(user_formula), density= self.density)
36        print self.sld_formula.atoms
37        if self.density==0:
38            raise ZeroDivisionError,"integer division or modulo by zero for density"
39            return 
40        self.volume = (self.sld_formula.mass /self.density)/avogadro_number*1.0e24   
41       
42       
43    def calculateXRaySld(self, element):
44        """
45            Get an element and compute the corresponding SLD for a given formula
46            @param element:  elementis a string of existing atom
47        """
48        myformula = formula(str (element))
49        if len(myformula.atoms)!=1:
50            return 
51        element= myformula.atoms.keys()[0] 
52        energy = xray_energy(element.K_alpha)
53        atom = self.sld_formula.atoms
54        atom_reel, atom_im = xray_sld_from_atoms( atom,
55                                              density= self.density,
56                                              energy= energy )
57        return atom_reel, atom_im
58     
59       
60    def calculateNSld(self):
61        """
62            Compute the neutron SLD for a given molecule
63            @return absorp: absorption
64            @return coh: coherence cross section
65            @return inc: incoherence cross section
66        """
67        if self.density ==0:
68            raise ZeroDivisionError,"integer division or modulo by zero for density"
69            return 
70        atom = self.sld_formula.atoms
71        coh,absorp,inc = neutron_sld_from_atoms(atom,self.density,self.wavelength)
72        #Don't know if value is return in cm or  cm^(-1).assume return in cm
73        # to match result of neutron inc of Alan calculator
74        self.incoherence = inc*1/10
75        #Doesn't match result of Alan calculator for absorption factor of 2
76        #multiplication of 100 is going around
77        self.absorption = absorp*2*100
78        self.coherence  = coh
79        return self.coherence, self.absorption, self.incoherence
80   
81   
82    def calculateLength(self):
83        """
84            Compute the neutron 1/e length
85        """
86        self.length= (self.coherence+ self.absorption+ self.incoherence)/self.volume
87        return self.length
88       
89       
90    def calculateAbsorptionIm(self):
91        """
92            Compute imaginary part of the absorption
93        """
94        atom = self.sld_formula.atoms
95        #im: imaginary part of neutron SLD
96        im=0
97        for el, count in atom.iteritems():
98            if el.neutron.b_c_i !=None:
99                im += el.neutron.b_c_i*count
100        if self.volume !=0:
101            im = im/self.volume
102        else:
103            raise ZeroDivisionError,"integer division or modulo by zero for volume"
104        return im
105       
Note: See TracBrowser for help on using the repository browser.