[cfaf6d9] | 1 | """ |
---|
[14e9eb3] | 2 | This module intends to compute the neutron scattering length density |
---|
| 3 | of a molecule.It uses methods of the periodictable package to provide |
---|
| 4 | easy user interface for Sld calculator applications. |
---|
[cfaf6d9] | 5 | """ |
---|
| 6 | |
---|
| 7 | import periodictable |
---|
| 8 | from periodictable import formula |
---|
| 9 | from periodictable.xsf import xray_energy, xray_sld_from_atoms |
---|
| 10 | from periodictable.constants import avogadro_number |
---|
| 11 | import periodictable.nsf |
---|
[14e9eb3] | 12 | neutron_sld_from_atoms = periodictable.nsf.neutron_sld_from_atoms |
---|
[cfaf6d9] | 13 | |
---|
| 14 | class SldCalculator(object): |
---|
| 15 | """ |
---|
[beb374a] | 16 | Given a molecule, a density and a wavelength, this class |
---|
| 17 | determine scattering length density. |
---|
[14e9eb3] | 18 | |
---|
| 19 | Example: To get the sld value and the length 1/e the following |
---|
| 20 | methods need to be called in this later order:: |
---|
| 21 | formula = "H2O" |
---|
| 22 | density = 1.0 |
---|
| 23 | wavelength = 6.0 |
---|
| 24 | sld_calculator = SldCalculator() |
---|
| 25 | sld_calculator.set_value(formula, density, wavelength) |
---|
| 26 | sld_real, sld_im, _ = sld_calculator.calculate_neutron_sld() |
---|
| 27 | result : sld = sld_real +i sld_im |
---|
| 28 | |
---|
| 29 | Note: **set_value()** and **calculate_neutron_sld()** methods must |
---|
| 30 | be called in this order prior calling **calculate_length()** to get |
---|
| 31 | the proper result. |
---|
[cfaf6d9] | 32 | """ |
---|
| 33 | def __init__(self): |
---|
[14e9eb3] | 34 | #Private variable |
---|
| 35 | self._volume = 0.0 |
---|
| 36 | #Inputs |
---|
[cfaf6d9] | 37 | self.wavelength = 6.0 |
---|
[14e9eb3] | 38 | self.sld_formula = None |
---|
| 39 | self.density = None |
---|
| 40 | #Outputs |
---|
| 41 | self.sld_real = None |
---|
| 42 | self.sld_im = None |
---|
[cfaf6d9] | 43 | self.coherence = 0.0 |
---|
| 44 | self.absorption = 0.0 |
---|
| 45 | self.incoherence = 0.0 |
---|
[14e9eb3] | 46 | self.length = 0.0 |
---|
[cfaf6d9] | 47 | |
---|
[14e9eb3] | 48 | def set_value(self, formula, density, wavelength=6.0): |
---|
[cfaf6d9] | 49 | """ |
---|
[beb374a] | 50 | Store values into the sld calculator and compute the corresponding |
---|
| 51 | volume. |
---|
[cfaf6d9] | 52 | """ |
---|
| 53 | self.wavelength = wavelength |
---|
| 54 | self.density = float(density) |
---|
[14e9eb3] | 55 | self.sld_formula = formula(str(formula), density=self.density) |
---|
[89108a0] | 56 | |
---|
| 57 | if self.density == 0: |
---|
| 58 | raise ZeroDivisionError("integer division or modulo\ |
---|
| 59 | by zero for density") |
---|
[14e9eb3] | 60 | self._volume = (self.sld_formula.mass / self.density) / avogadro_number\ |
---|
[89108a0] | 61 | *1.0e24 |
---|
[cfaf6d9] | 62 | |
---|
| 63 | |
---|
[89108a0] | 64 | def calculate_xray_sld(self, element): |
---|
[cfaf6d9] | 65 | """ |
---|
[beb374a] | 66 | Get an element and compute the corresponding SLD for a given formula |
---|
| 67 | @param element: elementis a string of existing atom |
---|
[cfaf6d9] | 68 | """ |
---|
[89108a0] | 69 | myformula = formula(str(element)) |
---|
| 70 | if len(myformula.atoms) != 1: |
---|
[cfaf6d9] | 71 | return |
---|
[89108a0] | 72 | element = myformula.atoms.keys()[0] |
---|
[cfaf6d9] | 73 | energy = xray_energy(element.K_alpha) |
---|
| 74 | atom = self.sld_formula.atoms |
---|
[89108a0] | 75 | atom_reel, atom_im = xray_sld_from_atoms(atom, |
---|
[cfaf6d9] | 76 | density= self.density, |
---|
[89108a0] | 77 | energy= energy) |
---|
[cfaf6d9] | 78 | return atom_reel, atom_im |
---|
| 79 | |
---|
| 80 | |
---|
[89108a0] | 81 | def calculate_neutron_sld(self): |
---|
[cfaf6d9] | 82 | """ |
---|
[beb374a] | 83 | Compute the neutron SLD for a given molecule |
---|
[14e9eb3] | 84 | @return sld_real : real part of the sld value |
---|
| 85 | @return sld_im: imaginary part of the sld value |
---|
[beb374a] | 86 | @return inc: incoherence cross section |
---|
[cfaf6d9] | 87 | """ |
---|
[89108a0] | 88 | if self.density == 0: |
---|
| 89 | raise ZeroDivisionError("integer division or modulo\ |
---|
| 90 | by zero for density") |
---|
[cfaf6d9] | 91 | return |
---|
| 92 | atom = self.sld_formula.atoms |
---|
[14e9eb3] | 93 | sld_real, sld_im, inc = neutron_sld_from_atoms(atom, self.density, |
---|
[89108a0] | 94 | self.wavelength) |
---|
[beb374a] | 95 | self.incoherence = inc |
---|
[14e9eb3] | 96 | self.sld_real = sld_real |
---|
| 97 | self.sld_im = sld_im |
---|
| 98 | return self.sld_real, self.sld_im, self.incoherence |
---|
[cfaf6d9] | 99 | |
---|
[89108a0] | 100 | def calculate_length(self): |
---|
[cfaf6d9] | 101 | """ |
---|
[beb374a] | 102 | Compute the neutron 1/e length |
---|
[cfaf6d9] | 103 | """ |
---|
[89108a0] | 104 | self.length = (self.coherence + self.absorption +\ |
---|
[14e9eb3] | 105 | self.incoherence) / self._volume |
---|
[cfaf6d9] | 106 | return self.length |
---|
| 107 | |
---|