1 | """ |
---|
2 | This module intends to compute the neutron scattering length density of molecule |
---|
3 | @author: Gervaise B. Alina |
---|
4 | """ |
---|
5 | |
---|
6 | import periodictable |
---|
7 | from periodictable import formula |
---|
8 | from periodictable.xsf import xray_energy, xray_sld_from_atoms |
---|
9 | from periodictable.constants import avogadro_number |
---|
10 | import periodictable.nsf |
---|
11 | neutron_sld_from_atoms= periodictable.nsf.neutron_sld_from_atoms |
---|
12 | |
---|
13 | class 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 | if self.density==0: |
---|
37 | raise ZeroDivisionError,"integer division or modulo by zero for density" |
---|
38 | return |
---|
39 | self.volume = (self.sld_formula.mass /self.density)/avogadro_number*1.0e24 |
---|
40 | |
---|
41 | |
---|
42 | def calculateXRaySld(self, element): |
---|
43 | """ |
---|
44 | Get an element and compute the corresponding SLD for a given formula |
---|
45 | @param element: elementis a string of existing atom |
---|
46 | """ |
---|
47 | myformula = formula(str (element)) |
---|
48 | if len(myformula.atoms)!=1: |
---|
49 | return |
---|
50 | element= myformula.atoms.keys()[0] |
---|
51 | energy = xray_energy(element.K_alpha) |
---|
52 | atom = self.sld_formula.atoms |
---|
53 | atom_reel, atom_im = xray_sld_from_atoms( atom, |
---|
54 | density= self.density, |
---|
55 | energy= energy ) |
---|
56 | return atom_reel, atom_im |
---|
57 | |
---|
58 | |
---|
59 | def calculateNSld(self): |
---|
60 | """ |
---|
61 | Compute the neutron SLD for a given molecule |
---|
62 | @return absorp: absorption |
---|
63 | @return coh: coherence cross section |
---|
64 | @return inc: incoherence cross section |
---|
65 | """ |
---|
66 | if self.density ==0: |
---|
67 | raise ZeroDivisionError,"integer division or modulo by zero for density" |
---|
68 | return |
---|
69 | atom = self.sld_formula.atoms |
---|
70 | coh,absorp,inc = neutron_sld_from_atoms(atom,self.density,self.wavelength) |
---|
71 | #Don't know if value is return in cm or cm^(-1).assume return in cm |
---|
72 | # to match result of neutron inc of Alan calculator |
---|
73 | self.incoherence = inc*1/10 |
---|
74 | #Doesn't match result of Alan calculator for absorption factor of 2 |
---|
75 | #multiplication of 100 is going around |
---|
76 | self.absorption = absorp*2*100 |
---|
77 | self.coherence = coh |
---|
78 | return self.coherence, self.absorption, self.incoherence |
---|
79 | |
---|
80 | |
---|
81 | def calculateLength(self): |
---|
82 | """ |
---|
83 | Compute the neutron 1/e length |
---|
84 | """ |
---|
85 | self.length= (self.coherence+ self.absorption+ self.incoherence)/self.volume |
---|
86 | return self.length |
---|
87 | |
---|
88 | |
---|
89 | def calculateAbsorptionIm(self): |
---|
90 | """ |
---|
91 | Compute imaginary part of the absorption |
---|
92 | """ |
---|
93 | atom = self.sld_formula.atoms |
---|
94 | #im: imaginary part of neutron SLD |
---|
95 | im=0 |
---|
96 | for el, count in atom.iteritems(): |
---|
97 | if el.neutron.b_c_i !=None: |
---|
98 | im += el.neutron.b_c_i*count |
---|
99 | if self.volume !=0: |
---|
100 | im = im/self.volume |
---|
101 | else: |
---|
102 | raise ZeroDivisionError,"integer division or modulo by zero for volume" |
---|
103 | return im |
---|
104 | |
---|