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 | 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 | |
---|