Changeset 14e9eb3 in sasview for calculator/sld_calculator.py
- Timestamp:
- May 28, 2010 1:24:47 PM (14 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 25a2ee3
- Parents:
- 2882fb5
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
calculator/sld_calculator.py
rbeb374a r14e9eb3 1 1 """ 2 3 of a molecule.It uses methods of the periodictable package to provide4 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. 5 5 """ 6 6 … … 10 10 from periodictable.constants import avogadro_number 11 11 import periodictable.nsf 12 neutron_sld_from_atoms = periodictable.nsf.neutron_sld_from_atoms12 neutron_sld_from_atoms = periodictable.nsf.neutron_sld_from_atoms 13 13 14 14 class SldCalculator(object): … … 16 16 Given a molecule, a density and a wavelength, this class 17 17 determine scattering length density. 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. 18 32 """ 19 33 def __init__(self): 34 #Private variable 35 self._volume = 0.0 36 #Inputs 20 37 self.wavelength = 6.0 38 self.sld_formula = None 39 self.density = None 40 #Outputs 41 self.sld_real = None 42 self.sld_im = None 21 43 self.coherence = 0.0 22 44 self.absorption = 0.0 23 45 self.incoherence = 0.0 24 self.sld_formula = None 25 self.volume = 0.0 26 self.density = None 27 self.length= 0.0 46 self.length = 0.0 28 47 29 def set_value(self, user_formula, density, wavelength=6.0):48 def set_value(self, formula, density, wavelength=6.0): 30 49 """ 31 50 Store values into the sld calculator and compute the corresponding … … 34 53 self.wavelength = wavelength 35 54 self.density = float(density) 36 self.sld_formula = formula(str( user_formula), density=self.density)55 self.sld_formula = formula(str(formula), density=self.density) 37 56 38 57 if self.density == 0: 39 58 raise ZeroDivisionError("integer division or modulo\ 40 59 by zero for density") 41 self. volume = (self.sld_formula.mass / self.density) / avogadro_number\60 self._volume = (self.sld_formula.mass / self.density) / avogadro_number\ 42 61 *1.0e24 43 62 … … 63 82 """ 64 83 Compute the neutron SLD for a given molecule 65 @return absorp: absorption66 return coh: coherence cross section84 @return sld_real : real part of the sld value 85 @return sld_im: imaginary part of the sld value 67 86 @return inc: incoherence cross section 68 87 """ … … 72 91 return 73 92 atom = self.sld_formula.atoms 74 coh, absorp, inc = neutron_sld_from_atoms(atom, self.density,93 sld_real, sld_im, inc = neutron_sld_from_atoms(atom, self.density, 75 94 self.wavelength) 76 #Don't know if value is return in cm or cm^(-1).assume return in cm77 # to match result of neutron inc of Alan calculator78 95 self.incoherence = inc 79 self.absorption = absorp 80 self.coherence = coh 81 return self.coherence, self.absorption, self.incoherence 82 96 self.sld_real = sld_real 97 self.sld_im = sld_im 98 return self.sld_real, self.sld_im, self.incoherence 83 99 84 100 def calculate_length(self): … … 87 103 """ 88 104 self.length = (self.coherence + self.absorption +\ 89 self.incoherence) / self. volume105 self.incoherence) / self._volume 90 106 return self.length 91 107 92 93 def calculate_coherence_im(self):94 """95 Compute imaginary part of the absorption96 """97 atom = self.sld_formula.atoms98 #im: imaginary part of neutron SLD99 im = 0100 for el, count in atom.iteritems():101 if el.neutron.b_c_i is not None:102 im += el.neutron.b_c_i * count103 104 if self.volume != 0:105 im = im/self.volume106 else:107 raise ZeroDivisionError("integer division or modulo\108 by zero for volume")109 return im
Note: See TracChangeset
for help on using the changeset viewer.