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