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