Ignore:
Timestamp:
Sep 5, 2018 8:13:45 AM (6 years ago)
Author:
Tim Snow <tim.snow@…>
Branches:
ESS_GUI, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc
Children:
c3e83d3
Parents:
d738feb
Message:

Adding more general x-ray SLD calculation funcitonality

Rather than using/limiting ourselves to Cu and Mo this approach lets you use any wavelength of x-ray radiation and display the SLD.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/qtgui/Calculators/SldPanel.py

    rd738feb r5c0e717  
    66 
    77from periodictable import formula as Formula 
    8 from periodictable.xsf import xray_energy, xray_sld_from_atoms 
     8from periodictable.xsf import xray_energy, xray_sld 
    99from periodictable.nsf import neutron_scattering 
    1010 
     
    2121    'MOLECULAR_FORMULA', 
    2222    'MASS_DENSITY', 
    23     'WAVELENGTH', 
     23    'NEUTRON_WAVELENGTH', 
    2424    'NEUTRON_SLD_REAL', 
    2525    'NEUTRON_SLD_IMAG', 
    26     'CU_KA_SLD_REAL', 
    27     'CU_KA_SLD_IMAG', 
    28     'MO_KA_SLD_REAL', 
    29     'MO_KA_SLD_IMAG', 
     26    'XRAY_WAVELENGTH', 
     27    'XRAY_SLD_REAL', 
     28    'XRAY_SLD_IMAG', 
    3029    'NEUTRON_INC_XS', 
    3130    'NEUTRON_ABS_XS', 
     
    3433 
    3534class SldResult(object): 
    36     def __init__(self, molecular_formula, mass_density, wavelength, 
    37         neutron_sld_real, neutron_sld_imag, 
    38         cu_ka_sld_real, cu_ka_sld_imag, 
    39         mo_ka_sld_real, mo_ka_sld_imag, 
     35    def __init__(self, molecular_formula, mass_density, 
     36        neutron_wavelength, neutron_sld_real, neutron_sld_imag, 
     37        xray_wavelength, xray_sld_real, xray_sld_imag, 
    4038        neutron_inc_xs, neutron_abs_xs, neutron_length): 
    4139 
    4240        self.molecular_formula = molecular_formula 
    4341        self.mass_density = mass_density 
    44         self.wavelength = wavelength 
     42        self.neutron_wavelength = neutron_wavelength 
    4543        self.neutron_sld_real = neutron_sld_real 
    4644        self.neutron_sld_imag = neutron_sld_imag 
    47         self.cu_ka_sld_real = cu_ka_sld_real 
    48         self.cu_ka_sld_imag = cu_ka_sld_imag 
    49         self.mo_ka_sld_real = mo_ka_sld_real 
    50         self.mo_ka_sld_imag = mo_ka_sld_imag 
     45        self.xray_wavelength = xray_wavelength 
     46        self.xray_sld_real = xray_sld_real 
     47        self.xray_sld_imag = xray_sld_imag 
    5148        self.neutron_inc_xs = neutron_inc_xs 
    5249        self.neutron_abs_xs = neutron_abs_xs 
    5350        self.neutron_length = neutron_length 
    5451 
    55 def sldAlgorithm(molecular_formula, mass_density, wavelength): 
    56  
    57     sld_formula = Formula(molecular_formula, density=mass_density) 
    58  
    59     def calculate_sld(formula): 
    60         if len(formula.atoms) != 1: 
    61             raise NotImplementedError() 
    62         energy = xray_energy(list(formula.atoms.keys())[0].K_alpha) 
    63         return xray_sld_from_atoms( 
    64             sld_formula.atoms, 
     52def sldAlgorithm(molecular_formula, mass_density, neutron_wavelength, xray_wavelength): 
     53 
     54    xray_sld_real, xray_sld_imag = xray_sld( 
     55            compound=molecular_formula, 
    6556            density=mass_density, 
    66             energy=energy) 
    67  
    68     cu_real, cu_imag = calculate_sld(Formula("Cu")) 
    69     mo_real, mo_imag = calculate_sld(Formula("Mo")) 
    70  
    71     (sld_real, sld_imag, _), (_, neutron_abs_xs, neutron_inc_xs), neutron_length = \ 
     57            wavelength=xray_wavelength) 
     58 
     59    (neutron_sld_real, neutron_sld_imag, _), (_, neutron_abs_xs, neutron_inc_xs), neutron_length = \ 
    7260        neutron_scattering( 
    7361            compound=molecular_formula, 
    7462            density=mass_density, 
    75             wavelength=wavelength) 
     63            wavelength=neutron_wavelength) 
    7664 
    7765    SCALE = 1e-6 
    7866 
    7967    # neutron sld 
    80     neutron_sld_real = SCALE * sld_real 
    81     neutron_sld_imag = SCALE * abs(sld_imag) 
    82  
    83     # Cu sld 
    84     cu_ka_sld_real = SCALE * cu_real 
    85     cu_ka_sld_imag = SCALE * abs(cu_imag) 
    86  
    87     # Mo sld 
    88     mo_ka_sld_real = SCALE * mo_real 
    89     mo_ka_sld_imag = SCALE * abs(mo_imag) 
     68    scaled_neutron_sld_real = SCALE * neutron_sld_real 
     69    scaled_neutron_sld_imag = SCALE * abs(neutron_sld_imag) 
     70 
     71    # xray sld 
     72    scaled_xray_sld_real = SCALE * xray_sld_real 
     73    scaled_xray_sld_imag = SCALE * abs(xray_sld_imag) 
     74 
    9075 
    9176    return SldResult( 
    92         molecular_formula, mass_density, wavelength, 
    93         neutron_sld_real, neutron_sld_imag, 
    94         cu_ka_sld_real, cu_ka_sld_imag, 
    95         mo_ka_sld_real, mo_ka_sld_imag, 
     77        molecular_formula, mass_density, 
     78        neutron_wavelength, scaled_neutron_sld_real, scaled_neutron_sld_imag, 
     79        xray_wavelength, scaled_xray_sld_real, scaled_xray_sld_imag, 
    9680        neutron_inc_xs, neutron_abs_xs, neutron_length) 
    9781 
     
    11296            MODEL.NEUTRON_SLD_REAL: self.ui.editNeutronSldReal, 
    11397            MODEL.NEUTRON_SLD_IMAG: self.ui.editNeutronSldImag, 
    114             MODEL.CU_KA_SLD_REAL: self.ui.editCuKaSldReal, 
    115             MODEL.CU_KA_SLD_IMAG: self.ui.editCuKaSldImag, 
    116             MODEL.MO_KA_SLD_REAL: self.ui.editMoKaSldReal, 
    117             MODEL.MO_KA_SLD_IMAG: self.ui.editMoKaSldImag, 
     98            MODEL.XRAY_SLD_REAL: self.ui.editXraySldReal, 
     99            MODEL.XRAY_SLD_IMAG: self.ui.editXraySldImag, 
    118100            MODEL.NEUTRON_INC_XS: self.ui.editNeutronIncXs, 
    119101            MODEL.NEUTRON_ABS_XS: self.ui.editNeutronAbsXs, 
     
    131113        rx = QtCore.QRegExp("[+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?") 
    132114        self.ui.editMassDensity.setValidator(QtGui.QRegExpValidator(rx, self.ui.editMassDensity)) 
    133         self.ui.editWavelength.setValidator(QtGui.QRegExpValidator(rx, self.ui.editWavelength)) 
     115        self.ui.editNeutronWavelength.setValidator(QtGui.QRegExpValidator(rx, self.ui.editNeutronWavelength)) 
     116        self.ui.editXrayWavelength.setValidator(QtGui.QRegExpValidator(rx, self.ui.editXrayWavelength)) 
    134117 
    135118        # signals 
     
    143126    def setupModel(self): 
    144127        self.model = QtGui.QStandardItemModel(self) 
    145         self.model.setItem(MODEL.MOLECULAR_FORMULA, QtGui.QStandardItem()) 
    146         self.model.setItem(MODEL.MASS_DENSITY     , QtGui.QStandardItem()) 
    147         self.model.setItem(MODEL.WAVELENGTH       , QtGui.QStandardItem()) 
     128        self.model.setItem(MODEL.MOLECULAR_FORMULA , QtGui.QStandardItem()) 
     129        self.model.setItem(MODEL.MASS_DENSITY      , QtGui.QStandardItem()) 
     130        self.model.setItem(MODEL.NEUTRON_WAVELENGTH, QtGui.QStandardItem()) 
     131        self.model.setItem(MODEL.XRAY_WAVELENGTH   , QtGui.QStandardItem()) 
    148132 
    149133        for key in list(self._getOutputs().keys()): 
     
    151135 
    152136        self.model.dataChanged.connect(self.dataChanged) 
     137 
     138        self.ui.editMassDensity.textEdited.connect(self.recalculateSLD) 
     139        self.ui.editMolecularFormula.textEdited.connect(self.recalculateSLD) 
     140        self.ui.editNeutronWavelength.textEdited.connect(self.recalculateSLD) 
     141        self.ui.editXrayWavelength.textEdited.connect(self.recalculateSLD) 
    153142 
    154143        self.modelReset() 
     
    158147        self.mapper.setModel(self.model) 
    159148        self.mapper.setOrientation(QtCore.Qt.Vertical) 
    160         self.mapper.addMapping(self.ui.editMolecularFormula, MODEL.MOLECULAR_FORMULA) 
    161         self.mapper.addMapping(self.ui.editMassDensity     , MODEL.MASS_DENSITY) 
    162         self.mapper.addMapping(self.ui.editWavelength      , MODEL.WAVELENGTH) 
     149        self.mapper.addMapping(self.ui.editMolecularFormula , MODEL.MOLECULAR_FORMULA) 
     150        self.mapper.addMapping(self.ui.editMassDensity      , MODEL.MASS_DENSITY) 
     151        self.mapper.addMapping(self.ui.editNeutronWavelength, MODEL.NEUTRON_WAVELENGTH) 
     152        self.mapper.addMapping(self.ui.editXrayWavelength   , MODEL.XRAY_WAVELENGTH) 
    163153 
    164154        for key, edit in self._getOutputs().items(): 
     
    170160        update = False 
    171161        for index in range(top.row(), bottom.row() + 1): 
    172             if (index == MODEL.MOLECULAR_FORMULA) or (index == MODEL.MASS_DENSITY) or (index == MODEL.WAVELENGTH): 
     162            if (index == MODEL.MOLECULAR_FORMULA) or (index == MODEL.MASS_DENSITY) or (index == MODEL.NEUTRON_WAVELENGTH) or (index == MODEL.XRAY_WAVELENGTH): 
    173163                update = True 
    174164 
     
    178168 
    179169    def recalculateSLD(self): 
    180         formula = self.model.item(MODEL.MOLECULAR_FORMULA).text() 
    181         density = self.model.item(MODEL.MASS_DENSITY).text() 
    182         wavelength = self.model.item(MODEL.WAVELENGTH).text() 
    183         if len(formula) > 0 and len(density) > 0 and len(wavelength) > 0: 
     170        formula = self.ui.editMolecularFormula.text() 
     171        density = self.ui.editMassDensity.text() 
     172        neutronWavelength = self.ui.editNeutronWavelength.text() 
     173        xrayWavelength = self.ui.editXrayWavelength.text() 
     174 
     175        if len(formula) > 0 and len(density) > 0 and len(neutronWavelength) > 0 and len(xrayWavelength) > 0: 
    184176            try: 
    185                 results = sldAlgorithm(str(formula), float(density), float(wavelength)) 
     177                results = sldAlgorithm(str(formula), float(density), float(neutronWavelength), float(xrayWavelength)) 
    186178 
    187179                def format(value): 
     
    191183                self.model.item(MODEL.NEUTRON_SLD_IMAG).setText(format(results.neutron_sld_imag)) 
    192184 
    193                 self.model.item(MODEL.CU_KA_SLD_REAL).setText(format(results.cu_ka_sld_real)) 
    194                 self.model.item(MODEL.CU_KA_SLD_IMAG).setText(format(results.cu_ka_sld_imag)) 
    195  
    196                 self.model.item(MODEL.MO_KA_SLD_REAL).setText(format(results.mo_ka_sld_real)) 
    197                 self.model.item(MODEL.MO_KA_SLD_IMAG).setText(format(results.mo_ka_sld_imag)) 
     185                self.model.item(MODEL.XRAY_SLD_REAL).setText(format(results.xray_sld_real)) 
     186                self.model.item(MODEL.XRAY_SLD_IMAG).setText(format(results.xray_sld_imag)) 
    198187 
    199188                self.model.item(MODEL.NEUTRON_INC_XS).setText(format(results.neutron_inc_xs)) 
     
    212201        #self.model.beginResetModel() 
    213202        try: 
    214             self.model.item(MODEL.MOLECULAR_FORMULA).setText("H2O") 
    215             self.model.item(MODEL.MASS_DENSITY     ).setText("1") 
    216             self.model.item(MODEL.WAVELENGTH       ).setText("6") 
     203            self.model.item(MODEL.MOLECULAR_FORMULA ).setText("H2O") 
     204            self.model.item(MODEL.MASS_DENSITY      ).setText("1.0") 
     205            self.model.item(MODEL.NEUTRON_WAVELENGTH).setText("6.0") 
     206            self.model.item(MODEL.XRAY_WAVELENGTH   ).setText("1.0") 
     207            self.recalculateSLD() 
    217208        finally: 
    218209            pass 
Note: See TracChangeset for help on using the changeset viewer.