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