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.sasgui.guiframe.panel_base import PanelBase |
---|
11 | |
---|
12 | from sas.sasgui.guiframe.utils import format_number |
---|
13 | from sas.sasgui.guiframe.utils import check_float |
---|
14 | from sas.sasgui.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.sasgui.perspectives.calculator import calculator_widgets as widget |
---|
23 | from sas.sasgui.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.neutron_wavelength = WAVELENGTH |
---|
63 | self.xray_source_input = WAVELENGTH |
---|
64 | self.parent = parent |
---|
65 | #layout attribute |
---|
66 | self.compound_ctl = None |
---|
67 | self.density_ctl = None |
---|
68 | self.compound = "" |
---|
69 | self.density = "" |
---|
70 | self.neutron_wavelength_ctl = None |
---|
71 | self.xray_source_input_ctl = None |
---|
72 | self.xray_cbox = None |
---|
73 | self.neutron_sld_real_ctl = None |
---|
74 | self.neutron_sld_im_ctl = None |
---|
75 | self.xray_sld_real_ctl = None |
---|
76 | self.xray_sld_im_ctl = None |
---|
77 | self.neutron_abs_ctl = None |
---|
78 | self.neutron_inc_ctl = None |
---|
79 | self.neutron_length_ctl = None |
---|
80 | self.button_calculate = None |
---|
81 | self.xray_source = None |
---|
82 | #Draw the panel |
---|
83 | self._do_layout() |
---|
84 | self.SetAutoLayout(True) |
---|
85 | self.Layout() |
---|
86 | self.fill_xray_cbox() |
---|
87 | |
---|
88 | def _do_layout(self): |
---|
89 | """ |
---|
90 | Draw window content |
---|
91 | """ |
---|
92 | unit_a = '[A]' |
---|
93 | unit_density = '[g/cm^(3)]' |
---|
94 | unit_sld = '[1/A^(2)]' |
---|
95 | unit_cm1 = '[1/cm]' |
---|
96 | unit_cm = '[cm]' |
---|
97 | sizer_input = wx.GridBagSizer(5, 5) |
---|
98 | sizer_output = wx.GridBagSizer(5, 5) |
---|
99 | sizer_button = wx.BoxSizer(wx.HORIZONTAL) |
---|
100 | sizer1 = wx.BoxSizer(wx.HORIZONTAL) |
---|
101 | sizer2 = wx.BoxSizer(wx.HORIZONTAL) |
---|
102 | sizer3 = wx.BoxSizer(wx.HORIZONTAL) |
---|
103 | #---------inputs---------------- |
---|
104 | inputbox = wx.StaticBox(self, -1, "Input") |
---|
105 | boxsizer1 = wx.StaticBoxSizer(inputbox, wx.VERTICAL) |
---|
106 | boxsizer1.SetMinSize((_STATICBOX_WIDTH, -1)) |
---|
107 | |
---|
108 | compound_txt = wx.StaticText(self, -1, 'Compound ') |
---|
109 | self.compound_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH * 2, -1)) |
---|
110 | density_txt = wx.StaticText(self, -1, 'Density ') |
---|
111 | self.density_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, -1)) |
---|
112 | unit_density_txt = wx.StaticText(self, -1, unit_density) |
---|
113 | neutron_wavelength_txt = wx.StaticText(self, -1, 'Neutron wavelength') |
---|
114 | self.neutron_wavelength_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, -1)) |
---|
115 | self.neutron_wavelength_ctl.SetValue(str(self.neutron_wavelength)) |
---|
116 | self.xray_source_input_txt = wx.StaticText(self, -1, 'X-ray wavelength') |
---|
117 | self.xray_source_input_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, -1)) |
---|
118 | self.xray_source_input_ctl.SetValue(str(self.xray_source_input)) |
---|
119 | neutron_unit_a_txt = wx.StaticText(self, -1, unit_a) |
---|
120 | |
---|
121 | self.xray_cbox = wx.ComboBox(self, -1, size=(70, 20), style=wx.CB_READONLY) |
---|
122 | xray_cbox_tip = "Select an element, wavelength or energy" |
---|
123 | self.xray_cbox.SetToolTipString(xray_cbox_tip) |
---|
124 | wx.EVT_COMBOBOX(self.xray_cbox, -1, self.on_select_xray) |
---|
125 | |
---|
126 | iy = 0 |
---|
127 | ix = 0 |
---|
128 | sizer_input.Add(compound_txt, (iy, ix), (1, 1), |
---|
129 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
130 | ix += 1 |
---|
131 | sizer_input.Add(self.compound_ctl, (iy, ix), (1, 1), |
---|
132 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
133 | iy += 1 |
---|
134 | ix = 0 |
---|
135 | sizer_input.Add(density_txt, (iy, ix), (1, 1), |
---|
136 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
137 | ix += 1 |
---|
138 | sizer_input.Add(self.density_ctl, (iy, ix), (1, 1), |
---|
139 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
140 | ix += 1 |
---|
141 | sizer_input.Add(unit_density_txt, (iy, ix), (1, 1), |
---|
142 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
143 | iy += 1 |
---|
144 | ix = 0 |
---|
145 | sizer_input.Add(neutron_wavelength_txt, (iy, ix), (1, 1), |
---|
146 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
147 | ix += 1 |
---|
148 | sizer_input.Add(self.neutron_wavelength_ctl, (iy, ix), (1, 1), |
---|
149 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
150 | ix += 1 |
---|
151 | sizer_input.Add(neutron_unit_a_txt, (iy, ix), (1, 1), |
---|
152 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
153 | iy += 1 |
---|
154 | ix = 0 |
---|
155 | sizer_input.Add(self.xray_source_input_txt, (iy, ix), (1, 1), |
---|
156 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
157 | ix += 1 |
---|
158 | sizer_input.Add(self.xray_source_input_ctl, (iy, ix), (1, 1), |
---|
159 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
160 | ix += 1 |
---|
161 | sizer_input.Add(self.xray_cbox, (iy, ix), (1, 1), |
---|
162 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
163 | boxsizer1.Add(sizer_input) |
---|
164 | sizer1.Add(boxsizer1, 0, wx.EXPAND | wx.ALL, 10) |
---|
165 | #---------Outputs sizer-------- |
---|
166 | outputbox = wx.StaticBox(self, -1, "Output") |
---|
167 | boxsizer2 = wx.StaticBoxSizer(outputbox, wx.VERTICAL) |
---|
168 | boxsizer2.SetMinSize((_STATICBOX_WIDTH, -1)) |
---|
169 | |
---|
170 | i_complex = '- i' |
---|
171 | neutron_sld_txt = wx.StaticText(self, -1, 'Neutron SLD') |
---|
172 | self.neutron_sld_real_ctl = wx.TextCtrl(self, -1, |
---|
173 | size=(_BOX_WIDTH, -1)) |
---|
174 | self.neutron_sld_real_ctl.SetEditable(False) |
---|
175 | self.neutron_sld_real_ctl.SetToolTipString("Neutron SLD real") |
---|
176 | self.neutron_sld_im_ctl = wx.TextCtrl(self, -1, |
---|
177 | size=(_BOX_WIDTH, -1)) |
---|
178 | self.neutron_sld_im_ctl.SetEditable(False) |
---|
179 | self.neutron_sld_im_ctl.SetToolTipString("Neutron SLD imaginary") |
---|
180 | neutron_sld_units_txt = wx.StaticText(self, -1, unit_sld) |
---|
181 | |
---|
182 | xray_sld_txt = wx.StaticText(self, -1, 'X-ray SLD') |
---|
183 | self.xray_sld_real_ctl = wx.TextCtrl(self, -1, |
---|
184 | size=(_BOX_WIDTH, -1)) |
---|
185 | self.xray_sld_real_ctl.SetEditable(False) |
---|
186 | self.xray_sld_real_ctl.SetToolTipString("X-ray SLD real") |
---|
187 | self.xray_sld_im_ctl = wx.TextCtrl(self, -1, |
---|
188 | size=(_BOX_WIDTH, -1)) |
---|
189 | self.xray_sld_im_ctl.SetEditable(False) |
---|
190 | self.xray_sld_im_ctl.SetToolTipString("X-ray SLD imaginary") |
---|
191 | xray_sld_units_txt = wx.StaticText(self, -1, unit_sld) |
---|
192 | |
---|
193 | neutron_inc_txt = wx.StaticText(self, -1, 'Neutron Inc. Xs') |
---|
194 | self.neutron_inc_ctl = wx.TextCtrl(self, -1, |
---|
195 | size=(_BOX_WIDTH, -1)) |
---|
196 | self.neutron_inc_ctl.SetEditable(False) |
---|
197 | self.neutron_inc_ctl.SetToolTipString("Neutron Inc. Xs") |
---|
198 | neutron_inc_units_txt = wx.StaticText(self, -1, unit_cm1) |
---|
199 | |
---|
200 | neutron_abs_txt = wx.StaticText(self, -1, 'Neutron Abs. Xs') |
---|
201 | self.neutron_abs_ctl = wx.TextCtrl(self, -1, |
---|
202 | size=(_BOX_WIDTH, -1)) |
---|
203 | self.neutron_abs_ctl.SetEditable(False) |
---|
204 | self.neutron_abs_ctl.SetToolTipString("Neutron Abs. Xs") |
---|
205 | neutron_abs_units_txt = wx.StaticText(self, -1, unit_cm1) |
---|
206 | |
---|
207 | neutron_length_txt = wx.StaticText(self, -1, 'Neutron 1/e length') |
---|
208 | self.neutron_length_ctl = wx.TextCtrl(self, -1, |
---|
209 | size=(_BOX_WIDTH, -1)) |
---|
210 | self.neutron_length_ctl.SetEditable(False) |
---|
211 | self.neutron_length_ctl.SetToolTipString("Neutron 1/e length") |
---|
212 | neutron_length_units_txt = wx.StaticText(self, -1, unit_cm) |
---|
213 | |
---|
214 | iy = 0 |
---|
215 | ix = 0 |
---|
216 | sizer_output.Add(neutron_sld_txt, (iy, ix), (1, 1), |
---|
217 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
218 | ix += 1 |
---|
219 | sizer_output.Add(self.neutron_sld_real_ctl, (iy, ix), (1, 1), |
---|
220 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
221 | ix += 1 |
---|
222 | sizer_output.Add(wx.StaticText(self, -1, i_complex), |
---|
223 | (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
224 | ix += 1 |
---|
225 | sizer_output.Add(self.neutron_sld_im_ctl, |
---|
226 | (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
227 | ix += 1 |
---|
228 | sizer_output.Add(neutron_sld_units_txt, |
---|
229 | (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
230 | iy += 1 |
---|
231 | ix = 0 |
---|
232 | sizer_output.Add(xray_sld_txt, (iy, ix), (1, 1), |
---|
233 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
234 | ix += 1 |
---|
235 | sizer_output.Add(self.xray_sld_real_ctl, (iy, ix), (1, 1), |
---|
236 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
237 | ix += 1 |
---|
238 | sizer_output.Add(wx.StaticText(self, -1, i_complex), |
---|
239 | (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
240 | ix += 1 |
---|
241 | sizer_output.Add(self.xray_sld_im_ctl, |
---|
242 | (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
243 | ix += 1 |
---|
244 | sizer_output.Add(xray_sld_units_txt, |
---|
245 | (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
246 | iy += 1 |
---|
247 | ix = 0 |
---|
248 | sizer_output.Add(neutron_inc_txt, (iy, ix), (1, 1), |
---|
249 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
250 | ix += 1 |
---|
251 | sizer_output.Add(self.neutron_inc_ctl, (iy, ix), (1, 1), |
---|
252 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
253 | ix += 2 |
---|
254 | sizer_output.Add(neutron_inc_units_txt, (iy, ix), (1, 1), |
---|
255 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
256 | iy += 1 |
---|
257 | ix = 0 |
---|
258 | sizer_output.Add(neutron_abs_txt, (iy, ix), (1, 1), |
---|
259 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
260 | ix += 1 |
---|
261 | sizer_output.Add(self.neutron_abs_ctl, (iy, ix), (1, 1), |
---|
262 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
263 | ix += 2 |
---|
264 | sizer_output.Add(neutron_abs_units_txt, (iy, ix), (1, 1), |
---|
265 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
266 | iy += 1 |
---|
267 | ix = 0 |
---|
268 | sizer_output.Add(neutron_length_txt, (iy, ix), (1, 1), |
---|
269 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
270 | ix += 1 |
---|
271 | sizer_output.Add(self.neutron_length_ctl, (iy, ix), (1, 1), |
---|
272 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
273 | ix += 2 |
---|
274 | sizer_output.Add(neutron_length_units_txt, (iy, ix), (1, 1), |
---|
275 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
276 | boxsizer2.Add(sizer_output) |
---|
277 | sizer2.Add(boxsizer2, 0, wx.EXPAND | wx.ALL, 10) |
---|
278 | #-----Button sizer------------ |
---|
279 | |
---|
280 | id = wx.NewId() |
---|
281 | self.button_calculate = wx.Button(self, id, "Calculate") |
---|
282 | self.button_calculate.SetToolTipString("Calculate SLD.") |
---|
283 | self.Bind(wx.EVT_BUTTON, self.calculateSld, id=id) |
---|
284 | |
---|
285 | id = wx.NewId() |
---|
286 | self.button_help = wx.Button(self, id, "HELP") |
---|
287 | self.button_help.SetToolTipString("help on SLD calculator.") |
---|
288 | self.Bind(wx.EVT_BUTTON, self.on_help, id=id) |
---|
289 | |
---|
290 | self.button_close = wx.Button(self, wx.ID_CANCEL, 'Close') |
---|
291 | self.button_close.Bind(wx.EVT_BUTTON, self.on_close) |
---|
292 | self.button_close.SetToolTipString("Close this window.") |
---|
293 | |
---|
294 | sizer_button.Add((150, 20), 1, wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
295 | sizer_button.Add(self.button_calculate, 0, wx.RIGHT | wx.ADJUST_MINSIZE, 20) |
---|
296 | sizer_button.Add(self.button_help, 0, wx.RIGHT | wx.ADJUST_MINSIZE, 20) |
---|
297 | sizer_button.Add(self.button_close, 0, wx.RIGHT | wx.ADJUST_MINSIZE, 20) |
---|
298 | sizer3.Add(sizer_button) |
---|
299 | #---------layout---------------- |
---|
300 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
301 | vbox.Add(sizer1) |
---|
302 | vbox.Add(sizer2) |
---|
303 | vbox.Add(sizer3) |
---|
304 | vbox.Fit(self) |
---|
305 | self.SetSizer(vbox) |
---|
306 | |
---|
307 | def fill_xray_cbox(self): |
---|
308 | """ |
---|
309 | fill the x-ray combobox with the sources |
---|
310 | """ |
---|
311 | source_list = ['[A]', '[keV]', 'Element'] |
---|
312 | for source in source_list: |
---|
313 | pos = self.xray_cbox.Append(str(source)) |
---|
314 | self.xray_cbox.SetClientData(pos, str(source.strip())) |
---|
315 | self.xray_cbox.SetSelection(0) |
---|
316 | self.xray_source = source_list[0] |
---|
317 | |
---|
318 | def on_select_xray(self, event=None): |
---|
319 | """ |
---|
320 | On Selecting a source |
---|
321 | """ |
---|
322 | item = event.GetEventObject() |
---|
323 | self.xray_source = item.GetValue().strip() |
---|
324 | |
---|
325 | if self.xray_source == "[A]": |
---|
326 | self.xray_source_input_txt.SetLabel("X-ray wavelength") |
---|
327 | elif self.xray_source == "[keV]": |
---|
328 | self.xray_source_input_txt.SetLabel("X-ray energy") |
---|
329 | elif self.xray_source == "Element": |
---|
330 | self.xray_source_input_txt.SetLabel("X-ray source") |
---|
331 | |
---|
332 | def on_help(self, event): |
---|
333 | """ |
---|
334 | Bring up the SLD Documentation whenever |
---|
335 | the HELP button is clicked. |
---|
336 | |
---|
337 | Calls DocumentationWindow with the path of the location within the |
---|
338 | documentation tree (after /doc/ ....". Note that when using old |
---|
339 | versions of Wx (before 2.9) and thus not the release version of |
---|
340 | installers, the help comes up at the top level of the file as |
---|
341 | webbrowser does not pass anything past the # to the browser when it is |
---|
342 | running "file:///...." |
---|
343 | |
---|
344 | :param evt: Triggers on clicking the help button |
---|
345 | """ |
---|
346 | |
---|
347 | _TreeLocation = "user/sasgui/perspectives/calculator/" |
---|
348 | _TreeLocation += "sld_calculator_help.html" |
---|
349 | _doc_viewer = DocumentationWindow(self, -1, _TreeLocation, "", |
---|
350 | "General Scattering Calculator Help") |
---|
351 | |
---|
352 | def on_close(self, event): |
---|
353 | """ |
---|
354 | close the window containing this panel |
---|
355 | """ |
---|
356 | self.parent.Close() |
---|
357 | |
---|
358 | def calculate_xray_sld(self, element): |
---|
359 | """ |
---|
360 | Get an element and compute the corresponding SLD for a given formula |
---|
361 | |
---|
362 | :param element: elements a string of existing atom |
---|
363 | |
---|
364 | """ |
---|
365 | myformula = formula(str(element)) |
---|
366 | if len(myformula.atoms) != 1: |
---|
367 | return |
---|
368 | element = myformula.atoms.keys()[0] |
---|
369 | energy = xray_energy(element.K_alpha) |
---|
370 | |
---|
371 | self.sld_formula = formula(str(self.compound), density=self.density) |
---|
372 | atom = self.sld_formula.atoms |
---|
373 | return xray_sld_from_atoms(atom, density=self.density, energy=energy) |
---|
374 | |
---|
375 | def check_inputs(self): |
---|
376 | """Check validity user inputs""" |
---|
377 | flag = True |
---|
378 | msg = "" |
---|
379 | if check_float(self.density_ctl): |
---|
380 | self.density = float(self.density_ctl.GetValue()) |
---|
381 | else: |
---|
382 | flag = False |
---|
383 | msg += "Error for Density value :expect float" |
---|
384 | |
---|
385 | self.neutron_wavelength = self.neutron_wavelength_ctl.GetValue() |
---|
386 | self.xray_source_input = self.xray_source_input_ctl.GetValue() |
---|
387 | |
---|
388 | if str(self.neutron_wavelength).lstrip().rstrip() == "": |
---|
389 | self.neutron_wavelength = WAVELENGTH |
---|
390 | self.neutron_wavelength_ctl.SetValue(str(WAVELENGTH)) |
---|
391 | self.neutron_wavelength_ctl.SetBackgroundColour(wx.WHITE) |
---|
392 | self.neutron_wavelength_ctl.Refresh() |
---|
393 | msg += "Default value for wavelength is 6.0" |
---|
394 | else: |
---|
395 | if check_float(self.neutron_wavelength_ctl): |
---|
396 | self.neutron_wavelength = float(self.neutron_wavelength) |
---|
397 | else: |
---|
398 | flag = False |
---|
399 | msg += "Error for wavelength value :expect float" |
---|
400 | |
---|
401 | if str(self.xray_source_input).lstrip().rstrip() == "": |
---|
402 | self.xray_source_input = WAVELENGTH |
---|
403 | self.xray_source_input_ctl.SetValue(str(WAVELENGTH)) |
---|
404 | self.xray_source_input_ctl.SetBackgroundColour(wx.WHITE) |
---|
405 | self.xray_source_input_ctl.Refresh() |
---|
406 | msg += "Default value for wavelength is 6.0" |
---|
407 | else: |
---|
408 | if (self.xray_source == '[A]') or (self.xray_source == '[keV]'): |
---|
409 | if check_float(self.xray_source_input_ctl): |
---|
410 | self.xray_source_input = float(self.xray_source_input) |
---|
411 | else: |
---|
412 | flag = False |
---|
413 | msg += "Error for wavelength value :expect float" |
---|
414 | elif (self.xray_source == 'Element'): |
---|
415 | try: |
---|
416 | import periodictable |
---|
417 | exec("periodictable." + self.xray_source_input) |
---|
418 | except AttributeError: |
---|
419 | flag = False |
---|
420 | msg += "X-ray element supplied isn't in the database" |
---|
421 | |
---|
422 | |
---|
423 | |
---|
424 | self.compound = self.compound_ctl.GetValue().lstrip().rstrip() |
---|
425 | if self.compound != "": |
---|
426 | try : |
---|
427 | formula(self.compound) |
---|
428 | self.compound_ctl.SetBackgroundColour(wx.WHITE) |
---|
429 | self.compound_ctl.Refresh() |
---|
430 | except: |
---|
431 | self.compound_ctl.SetBackgroundColour("pink") |
---|
432 | self.compound_ctl.Refresh() |
---|
433 | flag = False |
---|
434 | msg += "Enter correct formula" |
---|
435 | else: |
---|
436 | self.compound_ctl.SetBackgroundColour("pink") |
---|
437 | self.compound_ctl.Refresh() |
---|
438 | flag = False |
---|
439 | msg += "Enter a formula" |
---|
440 | return flag, msg |
---|
441 | |
---|
442 | def calculate_sld_helper(self, element, density, molecule_formula): |
---|
443 | """ |
---|
444 | Get an element and compute the corresponding SLD for a given formula |
---|
445 | |
---|
446 | :param element: elements a string of existing atom |
---|
447 | |
---|
448 | """ |
---|
449 | element_formula = formula(str(element)) |
---|
450 | if len(element_formula.atoms) != 1: |
---|
451 | return |
---|
452 | element = element_formula.atoms.keys()[0] |
---|
453 | energy = xray_energy(element.K_alpha) |
---|
454 | atom = molecule_formula.atoms |
---|
455 | return xray_sld_from_atoms(atom, density=density, energy=energy) |
---|
456 | |
---|
457 | def calculateSld(self, event): |
---|
458 | """ |
---|
459 | Calculate the neutron scattering density length of a molecule |
---|
460 | """ |
---|
461 | self.clear_outputs() |
---|
462 | try: |
---|
463 | #Check validity user inputs |
---|
464 | flag, msg = self.check_inputs() |
---|
465 | if self.base is not None and msg.lstrip().rstrip() != "": |
---|
466 | msg = "SLD Calculator: %s" % str(msg) |
---|
467 | wx.PostEvent(self.base, StatusEvent(status=msg)) |
---|
468 | if not flag: |
---|
469 | return |
---|
470 | #get ready to compute |
---|
471 | self.sld_formula = formula(self.compound, |
---|
472 | density=self.density) |
---|
473 | (sld_real, sld_im, _), (_, absorp, incoh), \ |
---|
474 | length = neutron_scattering(compound=self.compound, |
---|
475 | density=self.density, |
---|
476 | wavelength=self.neutron_wavelength) |
---|
477 | if self.xray_source == "[A]": |
---|
478 | energy = xray_energy(self.xray_source_input) |
---|
479 | xray_real, xray_im = xray_sld_from_atoms(self.sld_formula.atoms, |
---|
480 | density=self.density, |
---|
481 | energy=energy) |
---|
482 | elif self.xray_source == "[keV]": |
---|
483 | xray_real, xray_im = xray_sld_from_atoms(self.sld_formula.atoms, |
---|
484 | density=self.density, |
---|
485 | energy=self.xray_source_input) |
---|
486 | elif self.xray_source == "Element": |
---|
487 | xray_real, xray_im = self.calculate_sld_helper(element=self.xray_source_input, |
---|
488 | density=self.density, |
---|
489 | molecule_formula=self.sld_formula) |
---|
490 | # set neutron sld values |
---|
491 | val = format_number(sld_real * _SCALE) |
---|
492 | self.neutron_sld_real_ctl.SetValue(val) |
---|
493 | val = format_number(math.fabs(sld_im) * _SCALE) |
---|
494 | self.neutron_sld_im_ctl.SetValue(val) |
---|
495 | # Compute the Cu SLD |
---|
496 | self.xray_sld_real_ctl.SetValue(format_number(xray_real * _SCALE)) |
---|
497 | val = format_number(math.fabs(xray_im) * _SCALE) |
---|
498 | self.xray_sld_im_ctl.SetValue(val) |
---|
499 | # set incoherence and absorption |
---|
500 | self.neutron_inc_ctl.SetValue(format_number(incoh)) |
---|
501 | self.neutron_abs_ctl.SetValue(format_number(absorp)) |
---|
502 | # Neutron length |
---|
503 | self.neutron_length_ctl.SetValue(format_number(length)) |
---|
504 | # display wavelength |
---|
505 | #self.wavelength_ctl.SetValue(str(self.wavelength)) |
---|
506 | #self.wavelength_ctl.SetValue(str(self.wavelength)) |
---|
507 | except: |
---|
508 | if self.base is not None: |
---|
509 | msg = "SLD Calculator: %s" % (sys.exc_value) |
---|
510 | wx.PostEvent(self.base, StatusEvent(status=msg)) |
---|
511 | if event is not None: |
---|
512 | event.Skip() |
---|
513 | |
---|
514 | def clear_outputs(self): |
---|
515 | """ |
---|
516 | Clear the outputs textctrl |
---|
517 | """ |
---|
518 | self.neutron_sld_real_ctl.SetValue("") |
---|
519 | self.neutron_sld_im_ctl.SetValue("") |
---|
520 | self.xray_sld_real_ctl.SetValue("") |
---|
521 | self.xray_sld_im_ctl.SetValue("") |
---|
522 | self.neutron_abs_ctl.SetValue("") |
---|
523 | self.neutron_inc_ctl.SetValue("") |
---|
524 | self.neutron_length_ctl.SetValue("") |
---|
525 | |
---|
526 | |
---|
527 | class SldWindow(widget.CHILD_FRAME): |
---|
528 | """ |
---|
529 | """ |
---|
530 | def __init__(self, parent=None, title="SLD Calculator", |
---|
531 | base=None, manager=None, |
---|
532 | size=(PANEL_SIZE, PANEL_SIZE), *args, **kwds): |
---|
533 | """ |
---|
534 | """ |
---|
535 | kwds['title'] = title |
---|
536 | kwds['size'] = size |
---|
537 | widget.CHILD_FRAME.__init__(self, parent, *args, **kwds) |
---|
538 | """ |
---|
539 | """ |
---|
540 | self.parent = parent |
---|
541 | self.base = base |
---|
542 | self.manager = manager |
---|
543 | self.panel = SldPanel(self, base=base) |
---|
544 | self.Bind(wx.EVT_CLOSE, self.on_close) |
---|
545 | self.SetPosition((wx.LEFT, PANEL_TOP)) |
---|
546 | self.Show(True) |
---|
547 | |
---|
548 | def on_close(self, event): |
---|
549 | """ |
---|
550 | On close event |
---|
551 | """ |
---|
552 | if self.manager is not None: |
---|
553 | self.manager.sld_frame = None |
---|
554 | self.Destroy() |
---|
555 | |
---|
556 | |
---|
557 | class ViewApp(wx.App): |
---|
558 | """ |
---|
559 | """ |
---|
560 | def OnInit(self): |
---|
561 | """ |
---|
562 | """ |
---|
563 | widget.CHILD_FRAME = wx.Frame |
---|
564 | frame = SldWindow(None, title='SLD Calculator') |
---|
565 | frame.Show(True) |
---|
566 | self.SetTopWindow(frame) |
---|
567 | return True |
---|
568 | |
---|
569 | |
---|
570 | if __name__ == "__main__": |
---|
571 | app = ViewApp(0) |
---|
572 | app.MainLoop() |
---|