[959eb01] | 1 | """ |
---|
| 2 | This module provide GUI for the neutron scattering length density calculator |
---|
| 3 | |
---|
| 4 | """ |
---|
| 5 | |
---|
| 6 | import wx |
---|
| 7 | import math |
---|
| 8 | import sys |
---|
| 9 | |
---|
| 10 | from sas.sasgui.guiframe.panel_base import PanelBase |
---|
| 11 | |
---|
| 12 | from sas.sasgui.guiframe.utils import format_number |
---|
| 13 | from sas.sasgui.guiframe.utils import check_float |
---|
| 14 | from sas.sasgui.guiframe.events import StatusEvent |
---|
| 15 | |
---|
| 16 | # the calculator default value for wavelength is 6 |
---|
| 17 | #import periodictable |
---|
| 18 | from periodictable import formula |
---|
| 19 | from periodictable.xsf import xray_energy |
---|
| 20 | from periodictable.xsf import xray_sld_from_atoms |
---|
| 21 | from periodictable.nsf import neutron_scattering |
---|
| 22 | from sas.sasgui.perspectives.calculator import calculator_widgets as widget |
---|
| 23 | from sas.sasgui.guiframe.documentation_window import DocumentationWindow |
---|
| 24 | |
---|
| 25 | WAVELENGTH = 6.0 |
---|
| 26 | _BOX_WIDTH = 76 |
---|
| 27 | _STATICBOX_WIDTH = 350 |
---|
| 28 | _SCALE = 1e-6 |
---|
| 29 | |
---|
| 30 | #SLD panel size |
---|
| 31 | if sys.platform.count("win32") > 0: |
---|
| 32 | PANEL_TOP = 0 |
---|
| 33 | _STATICBOX_WIDTH = 350 |
---|
| 34 | PANEL_SIZE = 400 |
---|
| 35 | FONT_VARIANT = 0 |
---|
| 36 | else: |
---|
| 37 | PANEL_TOP = 60 |
---|
| 38 | _STATICBOX_WIDTH = 380 |
---|
| 39 | PANEL_SIZE = 410 |
---|
| 40 | FONT_VARIANT = 1 |
---|
| 41 | |
---|
| 42 | class SldPanel(wx.Panel, PanelBase): |
---|
| 43 | """ |
---|
| 44 | Provides the SLD calculator GUI. |
---|
| 45 | """ |
---|
| 46 | ## Internal nickname for the window, used by the AUI manager |
---|
| 47 | window_name = "SLD Calculator" |
---|
| 48 | ## Name to appear on the window title bar |
---|
| 49 | window_caption = "SLD Calculator" |
---|
| 50 | ## Flag to tell the AUI manager to put this panel in the center pane |
---|
| 51 | CENTER_PANE = True |
---|
| 52 | |
---|
| 53 | def __init__(self, parent, base=None, *args, **kwds): |
---|
| 54 | """ |
---|
| 55 | """ |
---|
| 56 | wx.Panel.__init__(self, parent, *args, **kwds) |
---|
| 57 | PanelBase.__init__(self) |
---|
| 58 | #Font size |
---|
| 59 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
| 60 | # Object that receive status event |
---|
| 61 | self.base = base |
---|
[8963a55] | 62 | self.neutron_wavelength = WAVELENGTH |
---|
[bd8411d5] | 63 | self.xray_source_input = WAVELENGTH |
---|
[959eb01] | 64 | self.parent = parent |
---|
| 65 | #layout attribute |
---|
| 66 | self.compound_ctl = None |
---|
| 67 | self.density_ctl = None |
---|
| 68 | self.compound = "" |
---|
| 69 | self.density = "" |
---|
[8963a55] | 70 | self.neutron_wavelength_ctl = None |
---|
[bd8411d5] | 71 | self.xray_source_input_ctl = None |
---|
[8963a55] | 72 | self.xray_cbox = None |
---|
[959eb01] | 73 | self.neutron_sld_real_ctl = None |
---|
| 74 | self.neutron_sld_im_ctl = None |
---|
[8963a55] | 75 | self.xray_sld_real_ctl = None |
---|
| 76 | self.xray_sld_im_ctl = None |
---|
[959eb01] | 77 | self.neutron_abs_ctl = None |
---|
| 78 | self.neutron_inc_ctl = None |
---|
| 79 | self.neutron_length_ctl = None |
---|
| 80 | self.button_calculate = None |
---|
[8963a55] | 81 | self.xray_source = None |
---|
[959eb01] | 82 | #Draw the panel |
---|
| 83 | self._do_layout() |
---|
| 84 | self.SetAutoLayout(True) |
---|
| 85 | self.Layout() |
---|
[8963a55] | 86 | self.fill_xray_cbox() |
---|
[959eb01] | 87 | |
---|
| 88 | def _do_layout(self): |
---|
| 89 | """ |
---|
| 90 | Draw window content |
---|
| 91 | """ |
---|
| 92 | unit_a = '[A]' |
---|
| 93 | unit_density = '[g/cm^(3)]' |
---|
| 94 | unit_sld = '[1/A^(2)]' |
---|
| 95 | unit_cm1 = '[1/cm]' |
---|
| 96 | unit_cm = '[cm]' |
---|
| 97 | sizer_input = wx.GridBagSizer(5, 5) |
---|
| 98 | sizer_output = wx.GridBagSizer(5, 5) |
---|
| 99 | sizer_button = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 100 | sizer1 = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 101 | sizer2 = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 102 | sizer3 = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 103 | #---------inputs---------------- |
---|
| 104 | inputbox = wx.StaticBox(self, -1, "Input") |
---|
| 105 | boxsizer1 = wx.StaticBoxSizer(inputbox, wx.VERTICAL) |
---|
| 106 | boxsizer1.SetMinSize((_STATICBOX_WIDTH, -1)) |
---|
| 107 | |
---|
| 108 | compound_txt = wx.StaticText(self, -1, 'Compound ') |
---|
| 109 | self.compound_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH * 2, -1)) |
---|
| 110 | density_txt = wx.StaticText(self, -1, 'Density ') |
---|
| 111 | self.density_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, -1)) |
---|
| 112 | unit_density_txt = wx.StaticText(self, -1, unit_density) |
---|
[8963a55] | 113 | neutron_wavelength_txt = wx.StaticText(self, -1, 'Neutron wavelength') |
---|
| 114 | self.neutron_wavelength_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, -1)) |
---|
| 115 | self.neutron_wavelength_ctl.SetValue(str(self.neutron_wavelength)) |
---|
[fe09e9b] | 116 | self.xray_source_input_txt = wx.StaticText(self, -1, 'X-ray wavelength') |
---|
[bd8411d5] | 117 | self.xray_source_input_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, -1)) |
---|
| 118 | self.xray_source_input_ctl.SetValue(str(self.xray_source_input)) |
---|
[8963a55] | 119 | neutron_unit_a_txt = wx.StaticText(self, -1, unit_a) |
---|
| 120 | |
---|
| 121 | self.xray_cbox = wx.ComboBox(self, -1, size=(70, 20), style=wx.CB_READONLY) |
---|
| 122 | xray_cbox_tip = "Select an element, wavelength or energy" |
---|
| 123 | self.xray_cbox.SetToolTipString(xray_cbox_tip) |
---|
| 124 | wx.EVT_COMBOBOX(self.xray_cbox, -1, self.on_select_xray) |
---|
| 125 | |
---|
[959eb01] | 126 | iy = 0 |
---|
| 127 | ix = 0 |
---|
| 128 | sizer_input.Add(compound_txt, (iy, ix), (1, 1), |
---|
| 129 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 130 | ix += 1 |
---|
| 131 | sizer_input.Add(self.compound_ctl, (iy, ix), (1, 1), |
---|
| 132 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 133 | iy += 1 |
---|
| 134 | ix = 0 |
---|
| 135 | sizer_input.Add(density_txt, (iy, ix), (1, 1), |
---|
| 136 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 137 | ix += 1 |
---|
| 138 | sizer_input.Add(self.density_ctl, (iy, ix), (1, 1), |
---|
| 139 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 140 | ix += 1 |
---|
| 141 | sizer_input.Add(unit_density_txt, (iy, ix), (1, 1), |
---|
| 142 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 143 | iy += 1 |
---|
| 144 | ix = 0 |
---|
[8963a55] | 145 | sizer_input.Add(neutron_wavelength_txt, (iy, ix), (1, 1), |
---|
[959eb01] | 146 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 147 | ix += 1 |
---|
[8963a55] | 148 | sizer_input.Add(self.neutron_wavelength_ctl, (iy, ix), (1, 1), |
---|
[959eb01] | 149 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 150 | ix += 1 |
---|
[8963a55] | 151 | sizer_input.Add(neutron_unit_a_txt, (iy, ix), (1, 1), |
---|
| 152 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 153 | iy += 1 |
---|
| 154 | ix = 0 |
---|
[fe09e9b] | 155 | sizer_input.Add(self.xray_source_input_txt, (iy, ix), (1, 1), |
---|
[8963a55] | 156 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 157 | ix += 1 |
---|
[bd8411d5] | 158 | sizer_input.Add(self.xray_source_input_ctl, (iy, ix), (1, 1), |
---|
[8963a55] | 159 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 160 | ix += 1 |
---|
| 161 | sizer_input.Add(self.xray_cbox, (iy, ix), (1, 1), |
---|
[959eb01] | 162 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 163 | boxsizer1.Add(sizer_input) |
---|
| 164 | sizer1.Add(boxsizer1, 0, wx.EXPAND | wx.ALL, 10) |
---|
| 165 | #---------Outputs sizer-------- |
---|
| 166 | outputbox = wx.StaticBox(self, -1, "Output") |
---|
| 167 | boxsizer2 = wx.StaticBoxSizer(outputbox, wx.VERTICAL) |
---|
| 168 | boxsizer2.SetMinSize((_STATICBOX_WIDTH, -1)) |
---|
| 169 | |
---|
| 170 | i_complex = '- i' |
---|
| 171 | neutron_sld_txt = wx.StaticText(self, -1, 'Neutron SLD') |
---|
| 172 | self.neutron_sld_real_ctl = wx.TextCtrl(self, -1, |
---|
| 173 | size=(_BOX_WIDTH, -1)) |
---|
| 174 | self.neutron_sld_real_ctl.SetEditable(False) |
---|
[8963a55] | 175 | self.neutron_sld_real_ctl.SetToolTipString("Neutron SLD real") |
---|
[959eb01] | 176 | self.neutron_sld_im_ctl = wx.TextCtrl(self, -1, |
---|
| 177 | size=(_BOX_WIDTH, -1)) |
---|
| 178 | self.neutron_sld_im_ctl.SetEditable(False) |
---|
[8963a55] | 179 | self.neutron_sld_im_ctl.SetToolTipString("Neutron SLD imaginary") |
---|
[959eb01] | 180 | neutron_sld_units_txt = wx.StaticText(self, -1, unit_sld) |
---|
| 181 | |
---|
[8963a55] | 182 | xray_sld_txt = wx.StaticText(self, -1, 'X-ray SLD') |
---|
| 183 | self.xray_sld_real_ctl = wx.TextCtrl(self, -1, |
---|
[959eb01] | 184 | size=(_BOX_WIDTH, -1)) |
---|
[8963a55] | 185 | self.xray_sld_real_ctl.SetEditable(False) |
---|
| 186 | self.xray_sld_real_ctl.SetToolTipString("X-ray SLD real") |
---|
| 187 | self.xray_sld_im_ctl = wx.TextCtrl(self, -1, |
---|
[959eb01] | 188 | size=(_BOX_WIDTH, -1)) |
---|
[8963a55] | 189 | self.xray_sld_im_ctl.SetEditable(False) |
---|
| 190 | self.xray_sld_im_ctl.SetToolTipString("X-ray SLD imaginary") |
---|
| 191 | xray_sld_units_txt = wx.StaticText(self, -1, unit_sld) |
---|
[959eb01] | 192 | |
---|
| 193 | neutron_inc_txt = wx.StaticText(self, -1, 'Neutron Inc. Xs') |
---|
| 194 | self.neutron_inc_ctl = wx.TextCtrl(self, -1, |
---|
| 195 | size=(_BOX_WIDTH, -1)) |
---|
| 196 | self.neutron_inc_ctl.SetEditable(False) |
---|
| 197 | self.neutron_inc_ctl.SetToolTipString("Neutron Inc. Xs") |
---|
| 198 | neutron_inc_units_txt = wx.StaticText(self, -1, unit_cm1) |
---|
| 199 | |
---|
| 200 | neutron_abs_txt = wx.StaticText(self, -1, 'Neutron Abs. Xs') |
---|
| 201 | self.neutron_abs_ctl = wx.TextCtrl(self, -1, |
---|
| 202 | size=(_BOX_WIDTH, -1)) |
---|
| 203 | self.neutron_abs_ctl.SetEditable(False) |
---|
| 204 | self.neutron_abs_ctl.SetToolTipString("Neutron Abs. Xs") |
---|
| 205 | neutron_abs_units_txt = wx.StaticText(self, -1, unit_cm1) |
---|
| 206 | |
---|
| 207 | neutron_length_txt = wx.StaticText(self, -1, 'Neutron 1/e length') |
---|
| 208 | self.neutron_length_ctl = wx.TextCtrl(self, -1, |
---|
| 209 | size=(_BOX_WIDTH, -1)) |
---|
| 210 | self.neutron_length_ctl.SetEditable(False) |
---|
| 211 | self.neutron_length_ctl.SetToolTipString("Neutron 1/e length") |
---|
| 212 | neutron_length_units_txt = wx.StaticText(self, -1, unit_cm) |
---|
| 213 | |
---|
| 214 | iy = 0 |
---|
| 215 | ix = 0 |
---|
| 216 | sizer_output.Add(neutron_sld_txt, (iy, ix), (1, 1), |
---|
| 217 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 218 | ix += 1 |
---|
| 219 | sizer_output.Add(self.neutron_sld_real_ctl, (iy, ix), (1, 1), |
---|
| 220 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 221 | ix += 1 |
---|
| 222 | sizer_output.Add(wx.StaticText(self, -1, i_complex), |
---|
| 223 | (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 224 | ix += 1 |
---|
| 225 | sizer_output.Add(self.neutron_sld_im_ctl, |
---|
| 226 | (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 227 | ix += 1 |
---|
| 228 | sizer_output.Add(neutron_sld_units_txt, |
---|
| 229 | (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 230 | iy += 1 |
---|
| 231 | ix = 0 |
---|
[8963a55] | 232 | sizer_output.Add(xray_sld_txt, (iy, ix), (1, 1), |
---|
[959eb01] | 233 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 234 | ix += 1 |
---|
[8963a55] | 235 | sizer_output.Add(self.xray_sld_real_ctl, (iy, ix), (1, 1), |
---|
[959eb01] | 236 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 237 | ix += 1 |
---|
| 238 | sizer_output.Add(wx.StaticText(self, -1, i_complex), |
---|
| 239 | (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 240 | ix += 1 |
---|
[8963a55] | 241 | sizer_output.Add(self.xray_sld_im_ctl, |
---|
[959eb01] | 242 | (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 243 | ix += 1 |
---|
[8963a55] | 244 | sizer_output.Add(xray_sld_units_txt, |
---|
[959eb01] | 245 | (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 246 | iy += 1 |
---|
| 247 | ix = 0 |
---|
| 248 | sizer_output.Add(neutron_inc_txt, (iy, ix), (1, 1), |
---|
| 249 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 250 | ix += 1 |
---|
| 251 | sizer_output.Add(self.neutron_inc_ctl, (iy, ix), (1, 1), |
---|
| 252 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 253 | ix += 2 |
---|
| 254 | sizer_output.Add(neutron_inc_units_txt, (iy, ix), (1, 1), |
---|
| 255 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 256 | iy += 1 |
---|
| 257 | ix = 0 |
---|
| 258 | sizer_output.Add(neutron_abs_txt, (iy, ix), (1, 1), |
---|
| 259 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 260 | ix += 1 |
---|
| 261 | sizer_output.Add(self.neutron_abs_ctl, (iy, ix), (1, 1), |
---|
| 262 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 263 | ix += 2 |
---|
| 264 | sizer_output.Add(neutron_abs_units_txt, (iy, ix), (1, 1), |
---|
| 265 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 266 | iy += 1 |
---|
| 267 | ix = 0 |
---|
| 268 | sizer_output.Add(neutron_length_txt, (iy, ix), (1, 1), |
---|
| 269 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 270 | ix += 1 |
---|
| 271 | sizer_output.Add(self.neutron_length_ctl, (iy, ix), (1, 1), |
---|
| 272 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 273 | ix += 2 |
---|
| 274 | sizer_output.Add(neutron_length_units_txt, (iy, ix), (1, 1), |
---|
| 275 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 276 | boxsizer2.Add(sizer_output) |
---|
| 277 | sizer2.Add(boxsizer2, 0, wx.EXPAND | wx.ALL, 10) |
---|
| 278 | #-----Button sizer------------ |
---|
| 279 | |
---|
| 280 | id = wx.NewId() |
---|
| 281 | self.button_calculate = wx.Button(self, id, "Calculate") |
---|
| 282 | self.button_calculate.SetToolTipString("Calculate SLD.") |
---|
| 283 | self.Bind(wx.EVT_BUTTON, self.calculateSld, id=id) |
---|
| 284 | |
---|
| 285 | id = wx.NewId() |
---|
| 286 | self.button_help = wx.Button(self, id, "HELP") |
---|
| 287 | self.button_help.SetToolTipString("help on SLD calculator.") |
---|
| 288 | self.Bind(wx.EVT_BUTTON, self.on_help, id=id) |
---|
| 289 | |
---|
| 290 | self.button_close = wx.Button(self, wx.ID_CANCEL, 'Close') |
---|
| 291 | self.button_close.Bind(wx.EVT_BUTTON, self.on_close) |
---|
| 292 | self.button_close.SetToolTipString("Close this window.") |
---|
| 293 | |
---|
| 294 | sizer_button.Add((150, 20), 1, wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
| 295 | sizer_button.Add(self.button_calculate, 0, wx.RIGHT | wx.ADJUST_MINSIZE, 20) |
---|
| 296 | sizer_button.Add(self.button_help, 0, wx.RIGHT | wx.ADJUST_MINSIZE, 20) |
---|
| 297 | sizer_button.Add(self.button_close, 0, wx.RIGHT | wx.ADJUST_MINSIZE, 20) |
---|
| 298 | sizer3.Add(sizer_button) |
---|
| 299 | #---------layout---------------- |
---|
| 300 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
| 301 | vbox.Add(sizer1) |
---|
| 302 | vbox.Add(sizer2) |
---|
| 303 | vbox.Add(sizer3) |
---|
| 304 | vbox.Fit(self) |
---|
| 305 | self.SetSizer(vbox) |
---|
| 306 | |
---|
[8963a55] | 307 | def fill_xray_cbox(self): |
---|
| 308 | """ |
---|
| 309 | fill the x-ray combobox with the sources |
---|
| 310 | """ |
---|
| 311 | source_list = ['[A]', '[keV]', 'Element'] |
---|
| 312 | for source in source_list: |
---|
| 313 | pos = self.xray_cbox.Append(str(source)) |
---|
| 314 | self.xray_cbox.SetClientData(pos, str(source.strip())) |
---|
| 315 | self.xray_cbox.SetSelection(0) |
---|
[bd8411d5] | 316 | self.xray_source = source_list[0] |
---|
[8963a55] | 317 | |
---|
| 318 | def on_select_xray(self, event=None): |
---|
| 319 | """ |
---|
| 320 | On Selecting a source |
---|
| 321 | """ |
---|
| 322 | item = event.GetEventObject() |
---|
| 323 | self.xray_source = item.GetValue().strip() |
---|
| 324 | |
---|
[fe09e9b] | 325 | if self.xray_source == "[A]": |
---|
| 326 | self.xray_source_input_txt.SetLabel("X-ray wavelength") |
---|
| 327 | elif self.xray_source == "[keV]": |
---|
| 328 | self.xray_source_input_txt.SetLabel("X-ray energy") |
---|
| 329 | elif self.xray_source == "Element": |
---|
| 330 | self.xray_source_input_txt.SetLabel("X-ray source") |
---|
| 331 | |
---|
[959eb01] | 332 | def on_help(self, event): |
---|
| 333 | """ |
---|
| 334 | Bring up the SLD Documentation whenever |
---|
| 335 | the HELP button is clicked. |
---|
| 336 | |
---|
| 337 | Calls DocumentationWindow with the path of the location within the |
---|
| 338 | documentation tree (after /doc/ ....". Note that when using old |
---|
| 339 | versions of Wx (before 2.9) and thus not the release version of |
---|
| 340 | installers, the help comes up at the top level of the file as |
---|
| 341 | webbrowser does not pass anything past the # to the browser when it is |
---|
| 342 | running "file:///...." |
---|
| 343 | |
---|
| 344 | :param evt: Triggers on clicking the help button |
---|
| 345 | """ |
---|
| 346 | |
---|
| 347 | _TreeLocation = "user/sasgui/perspectives/calculator/" |
---|
| 348 | _TreeLocation += "sld_calculator_help.html" |
---|
| 349 | _doc_viewer = DocumentationWindow(self, -1, _TreeLocation, "", |
---|
| 350 | "General Scattering Calculator Help") |
---|
| 351 | |
---|
| 352 | def on_close(self, event): |
---|
| 353 | """ |
---|
| 354 | close the window containing this panel |
---|
| 355 | """ |
---|
| 356 | self.parent.Close() |
---|
| 357 | |
---|
| 358 | def calculate_xray_sld(self, element): |
---|
| 359 | """ |
---|
| 360 | Get an element and compute the corresponding SLD for a given formula |
---|
| 361 | |
---|
| 362 | :param element: elements a string of existing atom |
---|
| 363 | |
---|
| 364 | """ |
---|
[5251ec6] | 365 | # TODO: use periodictable.elements object |
---|
| 366 | # energy = xray_energy(periodictable.elements[element].K_alpha) |
---|
| 367 | # TODO: code is very similar to sld helper |
---|
[959eb01] | 368 | myformula = formula(str(element)) |
---|
| 369 | if len(myformula.atoms) != 1: |
---|
| 370 | return |
---|
[5251ec6] | 371 | element = list(myformula.atoms.keys())[0] |
---|
[959eb01] | 372 | energy = xray_energy(element.K_alpha) |
---|
| 373 | |
---|
| 374 | self.sld_formula = formula(str(self.compound), density=self.density) |
---|
| 375 | atom = self.sld_formula.atoms |
---|
| 376 | return xray_sld_from_atoms(atom, density=self.density, energy=energy) |
---|
| 377 | |
---|
| 378 | def check_inputs(self): |
---|
| 379 | """Check validity user inputs""" |
---|
| 380 | flag = True |
---|
| 381 | msg = "" |
---|
| 382 | if check_float(self.density_ctl): |
---|
| 383 | self.density = float(self.density_ctl.GetValue()) |
---|
| 384 | else: |
---|
| 385 | flag = False |
---|
| 386 | msg += "Error for Density value :expect float" |
---|
| 387 | |
---|
[8963a55] | 388 | self.neutron_wavelength = self.neutron_wavelength_ctl.GetValue() |
---|
[bd8411d5] | 389 | self.xray_source_input = self.xray_source_input_ctl.GetValue() |
---|
[8963a55] | 390 | |
---|
| 391 | if str(self.neutron_wavelength).lstrip().rstrip() == "": |
---|
| 392 | self.neutron_wavelength = WAVELENGTH |
---|
| 393 | self.neutron_wavelength_ctl.SetValue(str(WAVELENGTH)) |
---|
| 394 | self.neutron_wavelength_ctl.SetBackgroundColour(wx.WHITE) |
---|
| 395 | self.neutron_wavelength_ctl.Refresh() |
---|
| 396 | msg += "Default value for wavelength is 6.0" |
---|
| 397 | else: |
---|
| 398 | if check_float(self.neutron_wavelength_ctl): |
---|
| 399 | self.neutron_wavelength = float(self.neutron_wavelength) |
---|
| 400 | else: |
---|
| 401 | flag = False |
---|
| 402 | msg += "Error for wavelength value :expect float" |
---|
| 403 | |
---|
[bd8411d5] | 404 | if str(self.xray_source_input).lstrip().rstrip() == "": |
---|
| 405 | self.xray_source_input = WAVELENGTH |
---|
| 406 | self.xray_source_input_ctl.SetValue(str(WAVELENGTH)) |
---|
| 407 | self.xray_source_input_ctl.SetBackgroundColour(wx.WHITE) |
---|
| 408 | self.xray_source_input_ctl.Refresh() |
---|
[959eb01] | 409 | msg += "Default value for wavelength is 6.0" |
---|
| 410 | else: |
---|
[bd8411d5] | 411 | if (self.xray_source == '[A]') or (self.xray_source == '[keV]'): |
---|
| 412 | if check_float(self.xray_source_input_ctl): |
---|
| 413 | self.xray_source_input = float(self.xray_source_input) |
---|
| 414 | else: |
---|
| 415 | flag = False |
---|
| 416 | msg += "Error for wavelength value :expect float" |
---|
| 417 | elif (self.xray_source == 'Element'): |
---|
[5251ec6] | 418 | # TODO: use periodictable.elements instead of exec() hacks |
---|
| 419 | # if self.xray_source_input not in periodictable.elements: |
---|
| 420 | # ... |
---|
[bd8411d5] | 421 | try: |
---|
| 422 | import periodictable |
---|
| 423 | exec("periodictable." + self.xray_source_input) |
---|
| 424 | except AttributeError: |
---|
| 425 | flag = False |
---|
| 426 | msg += "X-ray element supplied isn't in the database" |
---|
| 427 | |
---|
| 428 | |
---|
[959eb01] | 429 | |
---|
| 430 | self.compound = self.compound_ctl.GetValue().lstrip().rstrip() |
---|
| 431 | if self.compound != "": |
---|
| 432 | try : |
---|
| 433 | formula(self.compound) |
---|
| 434 | self.compound_ctl.SetBackgroundColour(wx.WHITE) |
---|
| 435 | self.compound_ctl.Refresh() |
---|
| 436 | except: |
---|
| 437 | self.compound_ctl.SetBackgroundColour("pink") |
---|
| 438 | self.compound_ctl.Refresh() |
---|
| 439 | flag = False |
---|
| 440 | msg += "Enter correct formula" |
---|
| 441 | else: |
---|
| 442 | self.compound_ctl.SetBackgroundColour("pink") |
---|
| 443 | self.compound_ctl.Refresh() |
---|
| 444 | flag = False |
---|
| 445 | msg += "Enter a formula" |
---|
| 446 | return flag, msg |
---|
| 447 | |
---|
| 448 | def calculate_sld_helper(self, element, density, molecule_formula): |
---|
| 449 | """ |
---|
| 450 | Get an element and compute the corresponding SLD for a given formula |
---|
| 451 | |
---|
| 452 | :param element: elements a string of existing atom |
---|
| 453 | |
---|
| 454 | """ |
---|
[5251ec6] | 455 | # TODO: use periodictable.elements object |
---|
| 456 | # energy = xray_energy(periodictable.elements[element].K_alpha) |
---|
[959eb01] | 457 | element_formula = formula(str(element)) |
---|
| 458 | if len(element_formula.atoms) != 1: |
---|
| 459 | return |
---|
[5251ec6] | 460 | element = list(element_formula.atoms.keys())[0] |
---|
[959eb01] | 461 | energy = xray_energy(element.K_alpha) |
---|
[5251ec6] | 462 | |
---|
[959eb01] | 463 | atom = molecule_formula.atoms |
---|
| 464 | return xray_sld_from_atoms(atom, density=density, energy=energy) |
---|
| 465 | |
---|
| 466 | def calculateSld(self, event): |
---|
| 467 | """ |
---|
| 468 | Calculate the neutron scattering density length of a molecule |
---|
| 469 | """ |
---|
| 470 | self.clear_outputs() |
---|
| 471 | try: |
---|
| 472 | #Check validity user inputs |
---|
| 473 | flag, msg = self.check_inputs() |
---|
| 474 | if self.base is not None and msg.lstrip().rstrip() != "": |
---|
| 475 | msg = "SLD Calculator: %s" % str(msg) |
---|
| 476 | wx.PostEvent(self.base, StatusEvent(status=msg)) |
---|
| 477 | if not flag: |
---|
| 478 | return |
---|
| 479 | #get ready to compute |
---|
| 480 | self.sld_formula = formula(self.compound, |
---|
| 481 | density=self.density) |
---|
| 482 | (sld_real, sld_im, _), (_, absorp, incoh), \ |
---|
| 483 | length = neutron_scattering(compound=self.compound, |
---|
| 484 | density=self.density, |
---|
[8963a55] | 485 | wavelength=self.neutron_wavelength) |
---|
| 486 | if self.xray_source == "[A]": |
---|
[bd8411d5] | 487 | energy = xray_energy(self.xray_source_input) |
---|
| 488 | xray_real, xray_im = xray_sld_from_atoms(self.sld_formula.atoms, |
---|
| 489 | density=self.density, |
---|
| 490 | energy=energy) |
---|
[8963a55] | 491 | elif self.xray_source == "[keV]": |
---|
[bd8411d5] | 492 | xray_real, xray_im = xray_sld_from_atoms(self.sld_formula.atoms, |
---|
| 493 | density=self.density, |
---|
| 494 | energy=self.xray_source_input) |
---|
[8963a55] | 495 | elif self.xray_source == "Element": |
---|
[bd8411d5] | 496 | xray_real, xray_im = self.calculate_sld_helper(element=self.xray_source_input, |
---|
| 497 | density=self.density, |
---|
| 498 | molecule_formula=self.sld_formula) |
---|
[959eb01] | 499 | # set neutron sld values |
---|
| 500 | val = format_number(sld_real * _SCALE) |
---|
| 501 | self.neutron_sld_real_ctl.SetValue(val) |
---|
| 502 | val = format_number(math.fabs(sld_im) * _SCALE) |
---|
| 503 | self.neutron_sld_im_ctl.SetValue(val) |
---|
| 504 | # Compute the Cu SLD |
---|
[8963a55] | 505 | self.xray_sld_real_ctl.SetValue(format_number(xray_real * _SCALE)) |
---|
| 506 | val = format_number(math.fabs(xray_im) * _SCALE) |
---|
| 507 | self.xray_sld_im_ctl.SetValue(val) |
---|
[959eb01] | 508 | # set incoherence and absorption |
---|
| 509 | self.neutron_inc_ctl.SetValue(format_number(incoh)) |
---|
| 510 | self.neutron_abs_ctl.SetValue(format_number(absorp)) |
---|
| 511 | # Neutron length |
---|
| 512 | self.neutron_length_ctl.SetValue(format_number(length)) |
---|
| 513 | # display wavelength |
---|
[8963a55] | 514 | #self.wavelength_ctl.SetValue(str(self.wavelength)) |
---|
| 515 | #self.wavelength_ctl.SetValue(str(self.wavelength)) |
---|
[5251ec6] | 516 | except Exception as exc: |
---|
[959eb01] | 517 | if self.base is not None: |
---|
[5251ec6] | 518 | msg = "SLD Calculator: %s" % exc |
---|
[959eb01] | 519 | wx.PostEvent(self.base, StatusEvent(status=msg)) |
---|
| 520 | if event is not None: |
---|
| 521 | event.Skip() |
---|
| 522 | |
---|
| 523 | def clear_outputs(self): |
---|
| 524 | """ |
---|
| 525 | Clear the outputs textctrl |
---|
| 526 | """ |
---|
| 527 | self.neutron_sld_real_ctl.SetValue("") |
---|
| 528 | self.neutron_sld_im_ctl.SetValue("") |
---|
[bd8411d5] | 529 | self.xray_sld_real_ctl.SetValue("") |
---|
| 530 | self.xray_sld_im_ctl.SetValue("") |
---|
[959eb01] | 531 | self.neutron_abs_ctl.SetValue("") |
---|
| 532 | self.neutron_inc_ctl.SetValue("") |
---|
| 533 | self.neutron_length_ctl.SetValue("") |
---|
| 534 | |
---|
| 535 | |
---|
| 536 | class SldWindow(widget.CHILD_FRAME): |
---|
| 537 | """ |
---|
| 538 | """ |
---|
| 539 | def __init__(self, parent=None, title="SLD Calculator", |
---|
| 540 | base=None, manager=None, |
---|
| 541 | size=(PANEL_SIZE, PANEL_SIZE), *args, **kwds): |
---|
| 542 | """ |
---|
| 543 | """ |
---|
| 544 | kwds['title'] = title |
---|
| 545 | kwds['size'] = size |
---|
| 546 | widget.CHILD_FRAME.__init__(self, parent, *args, **kwds) |
---|
| 547 | """ |
---|
| 548 | """ |
---|
| 549 | self.parent = parent |
---|
| 550 | self.base = base |
---|
| 551 | self.manager = manager |
---|
| 552 | self.panel = SldPanel(self, base=base) |
---|
| 553 | self.Bind(wx.EVT_CLOSE, self.on_close) |
---|
| 554 | self.SetPosition((wx.LEFT, PANEL_TOP)) |
---|
| 555 | self.Show(True) |
---|
| 556 | |
---|
| 557 | def on_close(self, event): |
---|
| 558 | """ |
---|
| 559 | On close event |
---|
| 560 | """ |
---|
[7432acb] | 561 | if self.manager is not None: |
---|
[959eb01] | 562 | self.manager.sld_frame = None |
---|
| 563 | self.Destroy() |
---|
| 564 | |
---|
| 565 | |
---|
| 566 | class ViewApp(wx.App): |
---|
| 567 | """ |
---|
| 568 | """ |
---|
| 569 | def OnInit(self): |
---|
| 570 | """ |
---|
| 571 | """ |
---|
| 572 | widget.CHILD_FRAME = wx.Frame |
---|
| 573 | frame = SldWindow(None, title='SLD Calculator') |
---|
| 574 | frame.Show(True) |
---|
| 575 | self.SetTopWindow(frame) |
---|
| 576 | return True |
---|
| 577 | |
---|
| 578 | |
---|
| 579 | if __name__ == "__main__": |
---|
| 580 | app = ViewApp(0) |
---|
| 581 | app.MainLoop() |
---|