[ef36eb2] | 1 | # global |
---|
| 2 | import logging |
---|
[4992ff2] | 3 | from PyQt5 import QtCore |
---|
| 4 | from PyQt5 import QtGui |
---|
| 5 | from PyQt5 import QtWidgets |
---|
[e4676c8] | 6 | |
---|
[ef36eb2] | 7 | from periodictable import formula as Formula |
---|
[5c0e717] | 8 | from periodictable.xsf import xray_energy, xray_sld |
---|
[ef36eb2] | 9 | from periodictable.nsf import neutron_scattering |
---|
| 10 | |
---|
[b0c5e8c] | 11 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
| 12 | |
---|
[cd2cc745] | 13 | from sas.qtgui.UI import main_resources_rc |
---|
[ef36eb2] | 14 | |
---|
| 15 | # Local UI |
---|
[83eb5208] | 16 | from sas.qtgui.Calculators.UI.SldPanel import Ui_SldPanel |
---|
[ef36eb2] | 17 | |
---|
[d4881f6a] | 18 | from sas.qtgui.Utilities.GuiUtils import enum |
---|
[ef36eb2] | 19 | |
---|
| 20 | MODEL = enum( |
---|
| 21 | 'MOLECULAR_FORMULA', |
---|
| 22 | 'MASS_DENSITY', |
---|
[5c0e717] | 23 | 'NEUTRON_WAVELENGTH', |
---|
[ef36eb2] | 24 | 'NEUTRON_SLD_REAL', |
---|
| 25 | 'NEUTRON_SLD_IMAG', |
---|
[5c0e717] | 26 | 'XRAY_WAVELENGTH', |
---|
| 27 | 'XRAY_SLD_REAL', |
---|
| 28 | 'XRAY_SLD_IMAG', |
---|
[ef36eb2] | 29 | 'NEUTRON_INC_XS', |
---|
| 30 | 'NEUTRON_ABS_XS', |
---|
| 31 | 'NEUTRON_LENGTH', |
---|
| 32 | ) |
---|
| 33 | |
---|
| 34 | class SldResult(object): |
---|
[5c0e717] | 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, |
---|
[ef36eb2] | 38 | neutron_inc_xs, neutron_abs_xs, neutron_length): |
---|
| 39 | |
---|
| 40 | self.molecular_formula = molecular_formula |
---|
| 41 | self.mass_density = mass_density |
---|
[5c0e717] | 42 | self.neutron_wavelength = neutron_wavelength |
---|
[ef36eb2] | 43 | self.neutron_sld_real = neutron_sld_real |
---|
| 44 | self.neutron_sld_imag = neutron_sld_imag |
---|
[5c0e717] | 45 | self.xray_wavelength = xray_wavelength |
---|
| 46 | self.xray_sld_real = xray_sld_real |
---|
| 47 | self.xray_sld_imag = xray_sld_imag |
---|
[ef36eb2] | 48 | self.neutron_inc_xs = neutron_inc_xs |
---|
| 49 | self.neutron_abs_xs = neutron_abs_xs |
---|
| 50 | self.neutron_length = neutron_length |
---|
| 51 | |
---|
[5c0e717] | 52 | def sldAlgorithm(molecular_formula, mass_density, neutron_wavelength, xray_wavelength): |
---|
[ef36eb2] | 53 | |
---|
[5c0e717] | 54 | xray_sld_real, xray_sld_imag = xray_sld( |
---|
| 55 | compound=molecular_formula, |
---|
[ef36eb2] | 56 | density=mass_density, |
---|
[5c0e717] | 57 | wavelength=xray_wavelength) |
---|
[ef36eb2] | 58 | |
---|
[5c0e717] | 59 | (neutron_sld_real, neutron_sld_imag, _), (_, neutron_abs_xs, neutron_inc_xs), neutron_length = \ |
---|
[ef36eb2] | 60 | neutron_scattering( |
---|
| 61 | compound=molecular_formula, |
---|
| 62 | density=mass_density, |
---|
[5c0e717] | 63 | wavelength=neutron_wavelength) |
---|
[ef36eb2] | 64 | |
---|
| 65 | SCALE = 1e-6 |
---|
| 66 | |
---|
| 67 | # neutron sld |
---|
[5c0e717] | 68 | scaled_neutron_sld_real = SCALE * neutron_sld_real |
---|
| 69 | scaled_neutron_sld_imag = SCALE * abs(neutron_sld_imag) |
---|
[ef36eb2] | 70 | |
---|
[5c0e717] | 71 | # xray sld |
---|
| 72 | scaled_xray_sld_real = SCALE * xray_sld_real |
---|
| 73 | scaled_xray_sld_imag = SCALE * abs(xray_sld_imag) |
---|
[ef36eb2] | 74 | |
---|
| 75 | |
---|
| 76 | return SldResult( |
---|
[5c0e717] | 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, |
---|
[ef36eb2] | 80 | neutron_inc_xs, neutron_abs_xs, neutron_length) |
---|
| 81 | |
---|
| 82 | |
---|
[4992ff2] | 83 | class SldPanel(QtWidgets.QDialog): |
---|
[ef36eb2] | 84 | |
---|
| 85 | def __init__(self, parent=None): |
---|
[1d85b5e] | 86 | super(SldPanel, self).__init__() |
---|
| 87 | |
---|
| 88 | self.manager = parent |
---|
[ef36eb2] | 89 | |
---|
| 90 | self.setupUi() |
---|
[33c0561] | 91 | # disable the context help icon |
---|
| 92 | self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowContextHelpButtonHint) |
---|
| 93 | |
---|
[ef36eb2] | 94 | self.setupModel() |
---|
| 95 | self.setupMapper() |
---|
| 96 | |
---|
| 97 | def _getOutputs(self): |
---|
| 98 | return { |
---|
| 99 | MODEL.NEUTRON_SLD_REAL: self.ui.editNeutronSldReal, |
---|
| 100 | MODEL.NEUTRON_SLD_IMAG: self.ui.editNeutronSldImag, |
---|
[5c0e717] | 101 | MODEL.XRAY_SLD_REAL: self.ui.editXraySldReal, |
---|
| 102 | MODEL.XRAY_SLD_IMAG: self.ui.editXraySldImag, |
---|
[ef36eb2] | 103 | MODEL.NEUTRON_INC_XS: self.ui.editNeutronIncXs, |
---|
| 104 | MODEL.NEUTRON_ABS_XS: self.ui.editNeutronAbsXs, |
---|
| 105 | MODEL.NEUTRON_LENGTH: self.ui.editNeutronLength |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | def setupUi(self): |
---|
| 109 | self.ui = Ui_SldPanel() |
---|
| 110 | self.ui.setupUi(self) |
---|
| 111 | |
---|
| 112 | # set validators |
---|
[fbfc488] | 113 | # TODO: GuiUtils.FormulaValidator() crashes with Qt5 - fix |
---|
| 114 | #self.ui.editMolecularFormula.setValidator(GuiUtils.FormulaValidator(self.ui.editMolecularFormula)) |
---|
[ef36eb2] | 115 | |
---|
| 116 | rx = QtCore.QRegExp("[+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?") |
---|
| 117 | self.ui.editMassDensity.setValidator(QtGui.QRegExpValidator(rx, self.ui.editMassDensity)) |
---|
[5c0e717] | 118 | self.ui.editNeutronWavelength.setValidator(QtGui.QRegExpValidator(rx, self.ui.editNeutronWavelength)) |
---|
| 119 | self.ui.editXrayWavelength.setValidator(QtGui.QRegExpValidator(rx, self.ui.editXrayWavelength)) |
---|
[ef36eb2] | 120 | |
---|
| 121 | # signals |
---|
[d738feb] | 122 | self.ui.helpButton.clicked.connect(self.displayHelp) |
---|
| 123 | self.ui.closeButton.clicked.connect(self.closePanel) |
---|
| 124 | self.ui.recalculateButton.clicked.connect(self.calculateSLD) |
---|
| 125 | |
---|
| 126 | def calculateSLD(self): |
---|
| 127 | self.recalculateSLD() |
---|
[ef36eb2] | 128 | |
---|
| 129 | def setupModel(self): |
---|
| 130 | self.model = QtGui.QStandardItemModel(self) |
---|
[5c0e717] | 131 | self.model.setItem(MODEL.MOLECULAR_FORMULA , QtGui.QStandardItem()) |
---|
| 132 | self.model.setItem(MODEL.MASS_DENSITY , QtGui.QStandardItem()) |
---|
| 133 | self.model.setItem(MODEL.NEUTRON_WAVELENGTH, QtGui.QStandardItem()) |
---|
| 134 | self.model.setItem(MODEL.XRAY_WAVELENGTH , QtGui.QStandardItem()) |
---|
[ef36eb2] | 135 | |
---|
[b3e8629] | 136 | for key in list(self._getOutputs().keys()): |
---|
[ef36eb2] | 137 | self.model.setItem(key, QtGui.QStandardItem()) |
---|
| 138 | |
---|
[4992ff2] | 139 | self.model.dataChanged.connect(self.dataChanged) |
---|
[ef36eb2] | 140 | |
---|
[bd39d6c] | 141 | self.ui.editMassDensity.textChanged.connect(self.recalculateSLD) |
---|
| 142 | self.ui.editMolecularFormula.textChanged.connect(self.recalculateSLD) |
---|
| 143 | self.ui.editNeutronWavelength.textChanged.connect(self.recalculateSLD) |
---|
| 144 | self.ui.editXrayWavelength.textChanged.connect(self.recalculateSLD) |
---|
[5c0e717] | 145 | |
---|
[ef36eb2] | 146 | self.modelReset() |
---|
| 147 | |
---|
| 148 | def setupMapper(self): |
---|
[4992ff2] | 149 | self.mapper = QtWidgets.QDataWidgetMapper(self) |
---|
[ef36eb2] | 150 | self.mapper.setModel(self.model) |
---|
| 151 | self.mapper.setOrientation(QtCore.Qt.Vertical) |
---|
[5c0e717] | 152 | self.mapper.addMapping(self.ui.editMolecularFormula , MODEL.MOLECULAR_FORMULA) |
---|
| 153 | self.mapper.addMapping(self.ui.editMassDensity , MODEL.MASS_DENSITY) |
---|
| 154 | self.mapper.addMapping(self.ui.editNeutronWavelength, MODEL.NEUTRON_WAVELENGTH) |
---|
| 155 | self.mapper.addMapping(self.ui.editXrayWavelength , MODEL.XRAY_WAVELENGTH) |
---|
[ef36eb2] | 156 | |
---|
[b3e8629] | 157 | for key, edit in self._getOutputs().items(): |
---|
[ef36eb2] | 158 | self.mapper.addMapping(edit, key) |
---|
| 159 | |
---|
[fbfc488] | 160 | self.mapper.toFirst() |
---|
[ef36eb2] | 161 | |
---|
| 162 | def dataChanged(self, top, bottom): |
---|
| 163 | update = False |
---|
[b3e8629] | 164 | for index in range(top.row(), bottom.row() + 1): |
---|
[5c0e717] | 165 | if (index == MODEL.MOLECULAR_FORMULA) or (index == MODEL.MASS_DENSITY) or (index == MODEL.NEUTRON_WAVELENGTH) or (index == MODEL.XRAY_WAVELENGTH): |
---|
[ef36eb2] | 166 | update = True |
---|
| 167 | |
---|
[d738feb] | 168 | # calculation |
---|
[ef36eb2] | 169 | if update: |
---|
[d738feb] | 170 | self.recalculateSLD() |
---|
| 171 | |
---|
| 172 | def recalculateSLD(self): |
---|
[5c0e717] | 173 | formula = self.ui.editMolecularFormula.text() |
---|
| 174 | density = self.ui.editMassDensity.text() |
---|
| 175 | neutronWavelength = self.ui.editNeutronWavelength.text() |
---|
| 176 | xrayWavelength = self.ui.editXrayWavelength.text() |
---|
| 177 | |
---|
| 178 | if len(formula) > 0 and len(density) > 0 and len(neutronWavelength) > 0 and len(xrayWavelength) > 0: |
---|
[d738feb] | 179 | try: |
---|
[5c0e717] | 180 | results = sldAlgorithm(str(formula), float(density), float(neutronWavelength), float(xrayWavelength)) |
---|
[ef36eb2] | 181 | |
---|
[d738feb] | 182 | def format(value): |
---|
| 183 | return ("%-5.3g" % value).strip() |
---|
[ef36eb2] | 184 | |
---|
[d738feb] | 185 | self.model.item(MODEL.NEUTRON_SLD_REAL).setText(format(results.neutron_sld_real)) |
---|
| 186 | self.model.item(MODEL.NEUTRON_SLD_IMAG).setText(format(results.neutron_sld_imag)) |
---|
[ef36eb2] | 187 | |
---|
[5c0e717] | 188 | self.model.item(MODEL.XRAY_SLD_REAL).setText(format(results.xray_sld_real)) |
---|
| 189 | self.model.item(MODEL.XRAY_SLD_IMAG).setText(format(results.xray_sld_imag)) |
---|
[ef36eb2] | 190 | |
---|
[d738feb] | 191 | self.model.item(MODEL.NEUTRON_INC_XS).setText(format(results.neutron_inc_xs)) |
---|
| 192 | self.model.item(MODEL.NEUTRON_ABS_XS).setText(format(results.neutron_abs_xs)) |
---|
| 193 | self.model.item(MODEL.NEUTRON_LENGTH).setText(format(results.neutron_length)) |
---|
[ef36eb2] | 194 | |
---|
[d738feb] | 195 | return |
---|
[ef36eb2] | 196 | |
---|
[d738feb] | 197 | except Exception as e: |
---|
| 198 | pass |
---|
| 199 | |
---|
| 200 | for key in list(self._getOutputs().keys()): |
---|
| 201 | self.model.item(key).setText("") |
---|
[ef36eb2] | 202 | |
---|
| 203 | def modelReset(self): |
---|
| 204 | #self.model.beginResetModel() |
---|
| 205 | try: |
---|
[5c0e717] | 206 | self.model.item(MODEL.MOLECULAR_FORMULA ).setText("H2O") |
---|
| 207 | self.model.item(MODEL.MASS_DENSITY ).setText("1.0") |
---|
| 208 | self.model.item(MODEL.NEUTRON_WAVELENGTH).setText("6.0") |
---|
| 209 | self.model.item(MODEL.XRAY_WAVELENGTH ).setText("1.0") |
---|
| 210 | self.recalculateSLD() |
---|
[ef36eb2] | 211 | finally: |
---|
| 212 | pass |
---|
[fbfc488] | 213 | #self.model.endResetModel() |
---|
[1d85b5e] | 214 | |
---|
| 215 | def displayHelp(self): |
---|
[aed0532] | 216 | location = "/user/qtgui/Calculators/sld_calculator_help.html" |
---|
[e90988c] | 217 | self.manager.showHelp(location) |
---|
| 218 | |
---|
[1d85b5e] | 219 | |
---|
[d738feb] | 220 | def closePanel(self): |
---|
| 221 | """ |
---|
| 222 | close the window containing this panel |
---|
| 223 | """ |
---|
| 224 | self.close() |
---|
| 225 | |
---|