[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() |
---|
| 91 | self.setupModel() |
---|
| 92 | self.setupMapper() |
---|
| 93 | |
---|
| 94 | def _getOutputs(self): |
---|
| 95 | return { |
---|
| 96 | MODEL.NEUTRON_SLD_REAL: self.ui.editNeutronSldReal, |
---|
| 97 | MODEL.NEUTRON_SLD_IMAG: self.ui.editNeutronSldImag, |
---|
[5c0e717] | 98 | MODEL.XRAY_SLD_REAL: self.ui.editXraySldReal, |
---|
| 99 | MODEL.XRAY_SLD_IMAG: self.ui.editXraySldImag, |
---|
[ef36eb2] | 100 | MODEL.NEUTRON_INC_XS: self.ui.editNeutronIncXs, |
---|
| 101 | MODEL.NEUTRON_ABS_XS: self.ui.editNeutronAbsXs, |
---|
| 102 | MODEL.NEUTRON_LENGTH: self.ui.editNeutronLength |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | def setupUi(self): |
---|
| 106 | self.ui = Ui_SldPanel() |
---|
| 107 | self.ui.setupUi(self) |
---|
| 108 | |
---|
| 109 | # set validators |
---|
[fbfc488] | 110 | # TODO: GuiUtils.FormulaValidator() crashes with Qt5 - fix |
---|
| 111 | #self.ui.editMolecularFormula.setValidator(GuiUtils.FormulaValidator(self.ui.editMolecularFormula)) |
---|
[ef36eb2] | 112 | |
---|
| 113 | rx = QtCore.QRegExp("[+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?") |
---|
| 114 | self.ui.editMassDensity.setValidator(QtGui.QRegExpValidator(rx, self.ui.editMassDensity)) |
---|
[5c0e717] | 115 | self.ui.editNeutronWavelength.setValidator(QtGui.QRegExpValidator(rx, self.ui.editNeutronWavelength)) |
---|
| 116 | self.ui.editXrayWavelength.setValidator(QtGui.QRegExpValidator(rx, self.ui.editXrayWavelength)) |
---|
[ef36eb2] | 117 | |
---|
| 118 | # signals |
---|
[d738feb] | 119 | self.ui.helpButton.clicked.connect(self.displayHelp) |
---|
| 120 | self.ui.closeButton.clicked.connect(self.closePanel) |
---|
| 121 | self.ui.recalculateButton.clicked.connect(self.calculateSLD) |
---|
| 122 | |
---|
| 123 | def calculateSLD(self): |
---|
| 124 | self.recalculateSLD() |
---|
[ef36eb2] | 125 | |
---|
| 126 | def setupModel(self): |
---|
| 127 | self.model = QtGui.QStandardItemModel(self) |
---|
[5c0e717] | 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()) |
---|
[ef36eb2] | 132 | |
---|
[b3e8629] | 133 | for key in list(self._getOutputs().keys()): |
---|
[ef36eb2] | 134 | self.model.setItem(key, QtGui.QStandardItem()) |
---|
| 135 | |
---|
[4992ff2] | 136 | self.model.dataChanged.connect(self.dataChanged) |
---|
[ef36eb2] | 137 | |
---|
[bd39d6c] | 138 | self.ui.editMassDensity.textChanged.connect(self.recalculateSLD) |
---|
| 139 | self.ui.editMolecularFormula.textChanged.connect(self.recalculateSLD) |
---|
| 140 | self.ui.editNeutronWavelength.textChanged.connect(self.recalculateSLD) |
---|
| 141 | self.ui.editXrayWavelength.textChanged.connect(self.recalculateSLD) |
---|
[5c0e717] | 142 | |
---|
[ef36eb2] | 143 | self.modelReset() |
---|
| 144 | |
---|
| 145 | def setupMapper(self): |
---|
[4992ff2] | 146 | self.mapper = QtWidgets.QDataWidgetMapper(self) |
---|
[ef36eb2] | 147 | self.mapper.setModel(self.model) |
---|
| 148 | self.mapper.setOrientation(QtCore.Qt.Vertical) |
---|
[5c0e717] | 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) |
---|
[ef36eb2] | 153 | |
---|
[b3e8629] | 154 | for key, edit in self._getOutputs().items(): |
---|
[ef36eb2] | 155 | self.mapper.addMapping(edit, key) |
---|
| 156 | |
---|
[fbfc488] | 157 | self.mapper.toFirst() |
---|
[ef36eb2] | 158 | |
---|
| 159 | def dataChanged(self, top, bottom): |
---|
| 160 | update = False |
---|
[b3e8629] | 161 | for index in range(top.row(), bottom.row() + 1): |
---|
[5c0e717] | 162 | if (index == MODEL.MOLECULAR_FORMULA) or (index == MODEL.MASS_DENSITY) or (index == MODEL.NEUTRON_WAVELENGTH) or (index == MODEL.XRAY_WAVELENGTH): |
---|
[ef36eb2] | 163 | update = True |
---|
| 164 | |
---|
[d738feb] | 165 | # calculation |
---|
[ef36eb2] | 166 | if update: |
---|
[d738feb] | 167 | self.recalculateSLD() |
---|
| 168 | |
---|
| 169 | def recalculateSLD(self): |
---|
[5c0e717] | 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: |
---|
[d738feb] | 176 | try: |
---|
[5c0e717] | 177 | results = sldAlgorithm(str(formula), float(density), float(neutronWavelength), float(xrayWavelength)) |
---|
[ef36eb2] | 178 | |
---|
[d738feb] | 179 | def format(value): |
---|
| 180 | return ("%-5.3g" % value).strip() |
---|
[ef36eb2] | 181 | |
---|
[d738feb] | 182 | self.model.item(MODEL.NEUTRON_SLD_REAL).setText(format(results.neutron_sld_real)) |
---|
| 183 | self.model.item(MODEL.NEUTRON_SLD_IMAG).setText(format(results.neutron_sld_imag)) |
---|
[ef36eb2] | 184 | |
---|
[5c0e717] | 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)) |
---|
[ef36eb2] | 187 | |
---|
[d738feb] | 188 | self.model.item(MODEL.NEUTRON_INC_XS).setText(format(results.neutron_inc_xs)) |
---|
| 189 | self.model.item(MODEL.NEUTRON_ABS_XS).setText(format(results.neutron_abs_xs)) |
---|
| 190 | self.model.item(MODEL.NEUTRON_LENGTH).setText(format(results.neutron_length)) |
---|
[ef36eb2] | 191 | |
---|
[d738feb] | 192 | return |
---|
[ef36eb2] | 193 | |
---|
[d738feb] | 194 | except Exception as e: |
---|
| 195 | pass |
---|
| 196 | |
---|
| 197 | for key in list(self._getOutputs().keys()): |
---|
| 198 | self.model.item(key).setText("") |
---|
[ef36eb2] | 199 | |
---|
| 200 | def modelReset(self): |
---|
| 201 | #self.model.beginResetModel() |
---|
| 202 | try: |
---|
[5c0e717] | 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() |
---|
[ef36eb2] | 208 | finally: |
---|
| 209 | pass |
---|
[fbfc488] | 210 | #self.model.endResetModel() |
---|
[1d85b5e] | 211 | |
---|
| 212 | def displayHelp(self): |
---|
[aed0532] | 213 | location = "/user/qtgui/Calculators/sld_calculator_help.html" |
---|
[e90988c] | 214 | self.manager.showHelp(location) |
---|
| 215 | |
---|
[1d85b5e] | 216 | |
---|
[d738feb] | 217 | def closePanel(self): |
---|
| 218 | """ |
---|
| 219 | close the window containing this panel |
---|
| 220 | """ |
---|
| 221 | self.close() |
---|
| 222 | |
---|