Changeset 1d67243 in sasview for sansview/perspectives/fitting/sldPanel.py
- Timestamp:
- Aug 20, 2009 12:37:45 PM (15 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 6c5239c
- Parents:
- 8e36cdd
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sansview/perspectives/fitting/sldPanel.py
r4faf4ba r1d67243 1 1 """ 2 This module intends to compute the neutron scattering length density of molecule2 This module provide GUI for the neutron scattering length density calculator 3 3 @author: Gervaise B. Alina 4 4 """ … … 7 7 import sys 8 8 9 import periodictable10 from periodictable import formula11 from periodictable.xsf import xray_energy, xray_sld_from_atoms12 from periodictable.constants import avogadro_number13 import periodictable.nsf14 neutron_sld_from_atoms= periodictable.nsf.neutron_sld_from_atoms15 16 9 from sans.guiframe.utils import format_number, check_float 17 10 from sans.guicomm.events import StatusEvent 18 11 from sldCalculator import SldCalculator 19 12 20 13 _BOX_WIDTH = 76 21 14 _STATICBOX_WIDTH = 350 22 15 _SCALE = 1e-6 23 _DEFAULT_WAVELENGTH = 1.79824 25 16 26 17 class SldPanel(wx.Panel): … … 32 23 # Object that receive status event 33 24 self.base= base 25 self.calculator = SldCalculator() 26 self.wavelength = self.calculator.wavelength 27 34 28 self._do_layout() 35 29 self.SetAutoLayout(True) … … 58 52 wavelength_txt = wx.StaticText(self, -1, 'Wavelength (A)') 59 53 self.wavelength_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,-1)) 54 self.wavelength_ctl.SetValue(str(self.wavelength)) 60 55 iy = 0 61 56 ix = 0 … … 231 226 #---------layout---------------- 232 227 vbox = wx.BoxSizer(wx.VERTICAL) 233 234 228 vbox.Add(sizer1) 235 229 vbox.Add(sizer2) … … 237 231 vbox.Fit(self) 238 232 self.SetSizer(vbox) 233 239 234 240 235 def check_inputs(self): … … 250 245 self.wavelength= self.wavelength_ctl.GetValue() 251 246 if self.wavelength.lstrip().rstrip()=="": 252 self.wavelength = _DEFAULT_WAVELENGTH247 self.wavelength = self.calculator.wavelength 253 248 else: 254 249 if check_float(self.wavelength_ctl): … … 269 264 return flag 270 265 266 271 267 def onHelp(self, event): 272 268 """ … … 294 290 #get ready to compute 295 291 try: 296 self.new_formula = formula(self.formulata_text, density= self.density) 297 atom = self.new_formula.atoms 292 self.calculator.setValue(self.formulata_text,self.density,self.wavelength) 298 293 except: 299 294 if self.base !=None: … … 303 298 raise 304 299 return 300 305 301 # Compute the Cu SLD 306 Cu_reel, Cu_im = self.calculateXRaySld( "Cu", density= self.density, 307 user_formula= self.new_formula) 302 Cu_reel, Cu_im = self.calculator.calculateXRaySld( "Cu") 308 303 self.cu_ka_sld_reel_ctl.SetValue(format_number(Cu_reel*_SCALE)) 309 304 self.cu_ka_sld_im_ctl.SetValue(format_number(Cu_im*_SCALE)) 305 310 306 # Compute the Mo SLD 311 Mo_reel, Mo_im = self.calculateXRaySld( "Mo", density= self.density, 312 user_formula= self.new_formula) 307 Mo_reel, Mo_im = self.calculator.calculateXRaySld( "Mo") 313 308 self.mo_ka_sld_reel_ctl.SetValue(format_number(Mo_reel*_SCALE)) 314 309 self.mo_ka_sld_im_ctl.SetValue(format_number(Mo_im*_SCALE)) 315 310 316 coh,absorp,inc= self.calculateNSld(self.density, wavelength= self.wavelength, 317 user_formula= self.new_formula) 318 #Don't know if value is return in cm or cm^(-1).assume return in cm 319 # to match result of neutron inc of Alan calculator 320 inc= inc*1/10 321 #Doesn't match result of Alan calculator for absorption factor of 2 322 #multiplication of 100 is going around 323 absorp= absorp *2*100 324 volume= (self.new_formula.mass /self.density)/avogadro_number*1.0e24 325 #im: imaginary part of neutron SLD 326 im=0 327 for el, count in atom.iteritems(): 328 if el.neutron.b_c_i !=None: 329 im += el.neutron.b_c_i*count 330 im = im/volume 331 311 coh,absorp,inc= self.calculator.calculateNSld() 312 im = self.calculator.absorptionIm() 313 length = self.calculator.computeLength() 314 # Neutron SLD 332 315 self.neutron_sld_reel_ctl.SetValue(format_number(coh*_SCALE)) 333 316 self.neutron_sld_im_ctl.SetValue(format_number(im*_SCALE)) 334 317 self.neutron_inc_ctl.SetValue(format_number(inc )) 335 318 self.neutron_abs_ctl.SetValue(format_number(absorp)) 336 #Don't know if value is return in cm or cm^(-1).assume return in cm 337 # to match result of neutron inc of Alan calculator 338 length= (coh+ absorp+ inc)/volume 319 # Neutron length 339 320 self.neutron_length_ctl.SetValue(format_number(length)) 321 # display wavelength 322 self.wavelength_ctl.SetValue(str(self.wavelength)) 340 323 except: 341 324 if self.base !=None: … … 346 329 return 347 330 348 def calculateXRaySld(self, element, density,user_formula): 349 """ 350 Get an element and compute the corresponding SLD for a given formula 351 @param element: elementis a string of existing atom 352 @param formula: molecule enters by the user 353 """ 354 try: 355 myformula = formula(str (element)) 356 if len(myformula.atoms)!=1: 357 return 358 element= myformula.atoms.keys()[0] 359 energy = xray_energy(element.K_alpha) 360 atom = user_formula.atoms 361 atom_reel, atom_im = xray_sld_from_atoms( atom, 362 density=density, 363 energy= energy ) 364 return atom_reel, atom_im 365 except: 366 if self.base !=None: 367 msg= "SLD Calculator: %s" % (sys.exc_value) 368 wx.PostEvent(self.base, StatusEvent(status= msg )) 369 else: 370 raise 371 return 372 373 def calculateNSld(self,density,wavelength,user_formula ): 374 """ 375 Compute the neutron SLD for a given molecule 376 @return absorp: absorption 377 @return coh: coherence cross section 378 @return inc: incoherence cross section 379 380 """ 381 if density ==0: 382 raise ZeroDivisionError,"integer division or modulo by zero for density" 383 return 384 atom = user_formula.atoms 385 coh,absorp,inc = neutron_sld_from_atoms(atom,density,wavelength) 386 387 return coh,absorp,inc 331 332 333 388 334 389 335
Note: See TracChangeset
for help on using the changeset viewer.