[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 | |
---|
[33f551f] | 287 | id = wx.NewId() |
---|
[fc8f132] | 288 | self.button_help = wx.Button(self, id, "HELP") |
---|
| 289 | self.button_help.SetToolTipString("help on SLD calculator.") |
---|
| 290 | self.Bind(wx.EVT_BUTTON, self.on_help, id=id) |
---|
| 291 | |
---|
| 292 | sizer_button.Add((150, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[66bfacf] | 293 | sizer_button.Add(self.button_calculate, 0, wx.RIGHT|wx.ADJUST_MINSIZE, 20) |
---|
[fc8f132] | 294 | sizer_button.Add(self.button_help, 0, wx.RIGHT|wx.ADJUST_MINSIZE, 20) |
---|
[427fa87] | 295 | sizer3.Add(sizer_button) |
---|
| 296 | #---------layout---------------- |
---|
| 297 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
| 298 | vbox.Add(sizer1) |
---|
| 299 | vbox.Add(sizer2) |
---|
| 300 | vbox.Add(sizer3) |
---|
| 301 | vbox.Fit(self) |
---|
| 302 | self.SetSizer(vbox) |
---|
| 303 | |
---|
[fc8f132] | 304 | def on_help(self, event): |
---|
| 305 | """ |
---|
| 306 | Bring up the SLD Documentation whenever |
---|
| 307 | the HELP button is clicked. |
---|
| 308 | |
---|
| 309 | Calls DocumentationWindow with the path of the location within the |
---|
| 310 | documentation tree (after /doc/ ....". Note that when using old |
---|
| 311 | versions of Wx (before 2.9) and thus not the release version of |
---|
| 312 | installers, the help comes up at the top level of the file as |
---|
| 313 | webbrowser does not pass anything past the # to the browser when it is |
---|
| 314 | running "file:///...." |
---|
| 315 | |
---|
| 316 | :param evt: Triggers on clicking the help button |
---|
| 317 | """ |
---|
| 318 | |
---|
| 319 | _TreeLocation = "user/perspectives/calculator/sld_calculator_help.html" |
---|
| 320 | _doc_viewer = DocumentationWindow(self, -1, \ |
---|
| 321 | _TreeLocation,"General Scattering Calculator Help") |
---|
| 322 | |
---|
[810f196] | 323 | def calculate_xray_sld(self, element): |
---|
| 324 | """ |
---|
| 325 | Get an element and compute the corresponding SLD for a given formula |
---|
[855546d] | 326 | |
---|
[810f196] | 327 | :param element: elements a string of existing atom |
---|
[855546d] | 328 | |
---|
[810f196] | 329 | """ |
---|
| 330 | myformula = formula(str(element)) |
---|
| 331 | if len(myformula.atoms) != 1: |
---|
| 332 | return |
---|
| 333 | element = myformula.atoms.keys()[0] |
---|
| 334 | energy = xray_energy(element.K_alpha) |
---|
[427fa87] | 335 | |
---|
[4752c31] | 336 | self.sld_formula = formula(str(self.compound), density=self.density) |
---|
[810f196] | 337 | atom = self.sld_formula.atoms |
---|
| 338 | return xray_sld_from_atoms(atom, density=self.density, energy= energy) |
---|
| 339 | |
---|
[427fa87] | 340 | def check_inputs(self): |
---|
| 341 | """Check validity user inputs""" |
---|
[39e49a1] | 342 | flag = True |
---|
[66bfacf] | 343 | msg = "" |
---|
[427fa87] | 344 | if check_float(self.density_ctl): |
---|
| 345 | self.density = float(self.density_ctl.GetValue()) |
---|
| 346 | else: |
---|
[39e49a1] | 347 | flag = False |
---|
[66bfacf] | 348 | msg += "Error for Density value :expect float" |
---|
[427fa87] | 349 | |
---|
[39e49a1] | 350 | self.wavelength = self.wavelength_ctl.GetValue() |
---|
[4752c31] | 351 | if str(self.wavelength).lstrip().rstrip() == "": |
---|
[810f196] | 352 | self.wavelength = WAVELENGTH |
---|
[66bfacf] | 353 | self.wavelength_ctl.SetValue(str(WAVELENGTH)) |
---|
| 354 | self.wavelength_ctl.SetBackgroundColour(wx.WHITE) |
---|
| 355 | self.wavelength_ctl.Refresh() |
---|
| 356 | msg += "Default value for wavelength is 6.0" |
---|
[427fa87] | 357 | else: |
---|
| 358 | if check_float(self.wavelength_ctl): |
---|
[39e49a1] | 359 | self.wavelength = float(self.wavelength) |
---|
[427fa87] | 360 | else: |
---|
| 361 | flag = False |
---|
[66bfacf] | 362 | msg += "Error for wavelength value :expect float" |
---|
[427fa87] | 363 | |
---|
[810f196] | 364 | self.compound = self.compound_ctl.GetValue().lstrip().rstrip() |
---|
| 365 | if self.compound != "": |
---|
[66bfacf] | 366 | try : |
---|
| 367 | formula(self.compound) |
---|
| 368 | self.compound_ctl.SetBackgroundColour(wx.WHITE) |
---|
| 369 | self.compound_ctl.Refresh() |
---|
| 370 | except: |
---|
| 371 | self.compound_ctl.SetBackgroundColour("pink") |
---|
| 372 | self.compound_ctl.Refresh() |
---|
| 373 | flag = False |
---|
| 374 | msg += "Enter correct formula" |
---|
[427fa87] | 375 | else: |
---|
| 376 | self.compound_ctl.SetBackgroundColour("pink") |
---|
| 377 | self.compound_ctl.Refresh() |
---|
[39e49a1] | 378 | flag = False |
---|
[66bfacf] | 379 | msg += "Enter a formula" |
---|
| 380 | return flag, msg |
---|
[427fa87] | 381 | |
---|
[810f196] | 382 | def calculate_sld_helper(self, element, density, molecule_formula): |
---|
| 383 | """ |
---|
| 384 | Get an element and compute the corresponding SLD for a given formula |
---|
| 385 | |
---|
| 386 | :param element: elements a string of existing atom |
---|
| 387 | |
---|
| 388 | """ |
---|
| 389 | element_formula = formula(str(element)) |
---|
| 390 | if len(element_formula.atoms) != 1: |
---|
| 391 | return |
---|
| 392 | element = element_formula.atoms.keys()[0] |
---|
| 393 | energy = xray_energy(element.K_alpha) |
---|
| 394 | atom = molecule_formula.atoms |
---|
| 395 | return xray_sld_from_atoms(atom, density=density, energy=energy) |
---|
| 396 | |
---|
| 397 | |
---|
[427fa87] | 398 | def calculateSld(self, event): |
---|
| 399 | """ |
---|
| 400 | Calculate the neutron scattering density length of a molecule |
---|
| 401 | """ |
---|
[66bfacf] | 402 | self.clear_outputs() |
---|
[427fa87] | 403 | try: |
---|
| 404 | #Check validity user inputs |
---|
[66bfacf] | 405 | flag, msg = self.check_inputs() |
---|
| 406 | if self.base is not None and msg.lstrip().rstrip() != "": |
---|
| 407 | msg = "SLD Calculator: %s" % str(msg) |
---|
| 408 | wx.PostEvent(self.base, StatusEvent(status=msg)) |
---|
| 409 | if not flag: |
---|
| 410 | return |
---|
| 411 | #get ready to compute |
---|
| 412 | self.sld_formula = formula(self.compound, |
---|
| 413 | density=self.density) |
---|
| 414 | (sld_real, sld_im, _), (_, absorp, incoh), \ |
---|
| 415 | length = neutron_scattering(compound=self.compound, |
---|
| 416 | density=self.density, |
---|
| 417 | wavelength=self.wavelength) |
---|
| 418 | cu_real, cu_im = self.calculate_sld_helper(element="Cu", |
---|
| 419 | density=self.density, |
---|
| 420 | molecule_formula=self.sld_formula) |
---|
| 421 | mo_real, mo_im = self.calculate_sld_helper(element="Mo", |
---|
| 422 | density=self.density, |
---|
| 423 | molecule_formula=self.sld_formula) |
---|
| 424 | # set neutron sld values |
---|
| 425 | val = format_number(sld_real * _SCALE) |
---|
[1e96a66] | 426 | self.neutron_sld_real_ctl.SetValue(val) |
---|
[66bfacf] | 427 | val = format_number(math.fabs(sld_im) * _SCALE) |
---|
| 428 | self.neutron_sld_im_ctl.SetValue(val) |
---|
| 429 | # Compute the Cu SLD |
---|
[1e96a66] | 430 | self.cu_ka_sld_real_ctl.SetValue(format_number(cu_real *_SCALE)) |
---|
[66bfacf] | 431 | val = format_number(math.fabs(cu_im )* _SCALE) |
---|
| 432 | self.cu_ka_sld_im_ctl.SetValue(val) |
---|
| 433 | # Compute the Mo SLD |
---|
[1e96a66] | 434 | self.mo_ka_sld_real_ctl.SetValue(format_number(mo_real *_SCALE)) |
---|
[66bfacf] | 435 | val = format_number(math.fabs(mo_im)* _SCALE) |
---|
| 436 | self.mo_ka_sld_im_ctl.SetValue(val) |
---|
| 437 | # set incoherence and absorption |
---|
| 438 | self.neutron_inc_ctl.SetValue(format_number(incoh)) |
---|
| 439 | self.neutron_abs_ctl.SetValue(format_number(absorp)) |
---|
| 440 | # Neutron length |
---|
| 441 | self.neutron_length_ctl.SetValue(format_number(length)) |
---|
| 442 | # display wavelength |
---|
| 443 | self.wavelength_ctl.SetValue(str(self.wavelength)) |
---|
[427fa87] | 444 | except: |
---|
[39e49a1] | 445 | if self.base is not None: |
---|
[b0ab6cb] | 446 | msg = "SLD Calculator: %s"%(sys.exc_value) |
---|
| 447 | wx.PostEvent(self.base, StatusEvent(status=msg)) |
---|
| 448 | if event is not None: |
---|
| 449 | event.Skip() |
---|
[66bfacf] | 450 | |
---|
| 451 | def clear_outputs(self): |
---|
| 452 | """ |
---|
| 453 | Clear the outputs textctrl |
---|
| 454 | """ |
---|
[1e96a66] | 455 | self.neutron_sld_real_ctl.SetValue("") |
---|
[66bfacf] | 456 | self.neutron_sld_im_ctl.SetValue("") |
---|
[1e96a66] | 457 | self.mo_ka_sld_real_ctl.SetValue("") |
---|
[66bfacf] | 458 | self.mo_ka_sld_im_ctl.SetValue("") |
---|
[1e96a66] | 459 | self.cu_ka_sld_real_ctl.SetValue("") |
---|
[66bfacf] | 460 | self.cu_ka_sld_im_ctl.SetValue("") |
---|
| 461 | self.neutron_abs_ctl.SetValue("") |
---|
| 462 | self.neutron_inc_ctl.SetValue("") |
---|
| 463 | self.neutron_length_ctl.SetValue("") |
---|
| 464 | |
---|
| 465 | |
---|
[d0923a3] | 466 | class SldWindow(widget.CHILD_FRAME): |
---|
[b0ab6cb] | 467 | """ |
---|
| 468 | """ |
---|
[44148f0] | 469 | def __init__(self, parent=None, title="SLD Calculator", |
---|
[ae84427] | 470 | base=None, manager=None, |
---|
| 471 | size=(PANEL_SIZE, PANEL_SIZE), *args, **kwds): |
---|
[b0ab6cb] | 472 | """ |
---|
| 473 | """ |
---|
[44148f0] | 474 | kwds['title'] = title |
---|
| 475 | kwds['size'] = size |
---|
[d0923a3] | 476 | widget.CHILD_FRAME.__init__(self, parent, *args, **kwds) |
---|
[b0ab6cb] | 477 | """ |
---|
| 478 | """ |
---|
[ae84427] | 479 | self.parent = parent |
---|
| 480 | self.base = base |
---|
| 481 | self.manager = manager |
---|
[427fa87] | 482 | self.panel = SldPanel(self, base=base) |
---|
[ae84427] | 483 | self.Bind(wx.EVT_CLOSE, self.on_close) |
---|
[fc8f132] | 484 | self.SetPosition((20, 10)) |
---|
[427fa87] | 485 | self.Show(True) |
---|
[ae84427] | 486 | |
---|
| 487 | def on_close(self, event): |
---|
| 488 | """ |
---|
| 489 | On close event |
---|
| 490 | """ |
---|
| 491 | if self.manager != None: |
---|
| 492 | self.manager.sld_frame = None |
---|
| 493 | self.Destroy() |
---|
| 494 | |
---|
[427fa87] | 495 | |
---|
| 496 | class ViewApp(wx.App): |
---|
[b0ab6cb] | 497 | """ |
---|
| 498 | """ |
---|
[427fa87] | 499 | def OnInit(self): |
---|
[b0ab6cb] | 500 | """ |
---|
| 501 | """ |
---|
[62af27a9] | 502 | widget.CHILD_FRAME = wx.Frame |
---|
[44148f0] | 503 | frame = SldWindow(None, title='SLD Calculator') |
---|
[427fa87] | 504 | frame.Show(True) |
---|
| 505 | self.SetTopWindow(frame) |
---|
| 506 | return True |
---|
| 507 | |
---|
| 508 | |
---|
| 509 | if __name__ == "__main__": |
---|
| 510 | app = ViewApp(0) |
---|
| 511 | app.MainLoop() |
---|