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