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