Changeset 5c0e717 in sasview for src/sas/qtgui/Calculators/SldPanel.py
- Timestamp:
- Sep 5, 2018 8:13:45 AM (6 years ago)
- 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
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/qtgui/Calculators/SldPanel.py
rd738feb r5c0e717 6 6 7 7 from periodictable import formula as Formula 8 from periodictable.xsf import xray_energy, xray_sld _from_atoms8 from periodictable.xsf import xray_energy, xray_sld 9 9 from periodictable.nsf import neutron_scattering 10 10 … … 21 21 'MOLECULAR_FORMULA', 22 22 'MASS_DENSITY', 23 ' WAVELENGTH',23 'NEUTRON_WAVELENGTH', 24 24 'NEUTRON_SLD_REAL', 25 25 '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', 30 29 'NEUTRON_INC_XS', 31 30 'NEUTRON_ABS_XS', … … 34 33 35 34 class 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, 40 38 neutron_inc_xs, neutron_abs_xs, neutron_length): 41 39 42 40 self.molecular_formula = molecular_formula 43 41 self.mass_density = mass_density 44 self. wavelength =wavelength42 self.neutron_wavelength = neutron_wavelength 45 43 self.neutron_sld_real = neutron_sld_real 46 44 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 51 48 self.neutron_inc_xs = neutron_inc_xs 52 49 self.neutron_abs_xs = neutron_abs_xs 53 50 self.neutron_length = neutron_length 54 51 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, 52 def sldAlgorithm(molecular_formula, mass_density, neutron_wavelength, xray_wavelength): 53 54 xray_sld_real, xray_sld_imag = xray_sld( 55 compound=molecular_formula, 65 56 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 = \ 72 60 neutron_scattering( 73 61 compound=molecular_formula, 74 62 density=mass_density, 75 wavelength= wavelength)63 wavelength=neutron_wavelength) 76 64 77 65 SCALE = 1e-6 78 66 79 67 # 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 90 75 91 76 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, 96 80 neutron_inc_xs, neutron_abs_xs, neutron_length) 97 81 … … 112 96 MODEL.NEUTRON_SLD_REAL: self.ui.editNeutronSldReal, 113 97 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, 118 100 MODEL.NEUTRON_INC_XS: self.ui.editNeutronIncXs, 119 101 MODEL.NEUTRON_ABS_XS: self.ui.editNeutronAbsXs, … … 131 113 rx = QtCore.QRegExp("[+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?") 132 114 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)) 134 117 135 118 # signals … … 143 126 def setupModel(self): 144 127 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()) 148 132 149 133 for key in list(self._getOutputs().keys()): … … 151 135 152 136 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) 153 142 154 143 self.modelReset() … … 158 147 self.mapper.setModel(self.model) 159 148 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) 163 153 164 154 for key, edit in self._getOutputs().items(): … … 170 160 update = False 171 161 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): 173 163 update = True 174 164 … … 178 168 179 169 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: 184 176 try: 185 results = sldAlgorithm(str(formula), float(density), float( wavelength))177 results = sldAlgorithm(str(formula), float(density), float(neutronWavelength), float(xrayWavelength)) 186 178 187 179 def format(value): … … 191 183 self.model.item(MODEL.NEUTRON_SLD_IMAG).setText(format(results.neutron_sld_imag)) 192 184 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)) 198 187 199 188 self.model.item(MODEL.NEUTRON_INC_XS).setText(format(results.neutron_inc_xs)) … … 212 201 #self.model.beginResetModel() 213 202 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() 217 208 finally: 218 209 pass
Note: See TracChangeset
for help on using the changeset viewer.