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