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