source: sasview/src/sas/qtgui/Calculators/SldPanel.py @ 912f438

ESS_GUIESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_opencl
Last change on this file since 912f438 was 912f438, checked in by awashington, 5 months ago

Separate neutron and xray sld calculation

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