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