Changeset 810f196 in sasview


Ignore:
Timestamp:
Jun 22, 2010 1:28:16 PM (14 years ago)
Author:
Gervaise Alina <gervyh@…>
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:
704a4df
Parents:
3ad91de
Message:

working on sld calculator

Files:
1 added
1 deleted
1 edited

Legend:

Unmodified
Added
Removed
  • calculatorview/perspectives/calculator/sld_panel.py

    redb3a5d0 r810f196  
    99from sans.guiframe.utils import format_number, check_float 
    1010from sans.guicomm.events import StatusEvent   
    11 from sans.calculator.sld_calculator import SldCalculator 
    12  
     11 
     12# the calculator default value for wavelength is 6 
     13import periodictable 
     14from periodictable import formula 
     15from periodictable.xsf import xray_energy, xray_sld_from_atoms 
     16from periodictable.constants import avogadro_number 
     17from  periodictable.nsf import neutron_scattering 
     18        
     19WAVELENGTH = 6.0 
    1320_BOX_WIDTH = 76 
    1421_STATICBOX_WIDTH = 350 
     
    3542    ## Flag to tell the AUI manager to put this panel in the center pane 
    3643    CENTER_PANE = True 
    37     def __init__(self, parent,base=None, id = -1): 
     44    def __init__(self, parent, base=None, id=-1): 
    3845        wx.Panel.__init__(self, parent, id = id) 
    3946        #Font size  
    4047        self.SetWindowVariant(variant=FONT_VARIANT) 
    41          
    4248        # Object that receive status event 
    4349        self.base = base 
    44         self.calculator = SldCalculator() 
    45         self.wavelength = self.calculator.wavelength 
    46          
     50        self.wavelength = WAVELENGTH 
     51        #Draw the panel 
    4752        self._do_layout() 
    4853        self.SetAutoLayout(True) 
    4954        self.Layout() 
    5055        
    51  
    5256    def _do_layout(self): 
    5357        """ 
     
    146150        self.neutron_inc_ctl.SetToolTipString("Neutron Inc. Xs") 
    147151        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() 
     152        
    151153        neutron_abs_txt = wx.StaticText(self, -1, 'Neutron Abs. Xs')      
    152154        self.neutron_abs_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,-1)) 
     
    154156        self.neutron_abs_ctl.SetToolTipString("Neutron Abs. Xs") 
    155157        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() 
     158       
    159159        neutron_length_txt = wx.StaticText(self, -1, 'Neutron 1/e length') 
    160160        self.neutron_length_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,-1)) 
     
    162162        self.neutron_length_ctl.SetToolTipString("Neutron 1/e length") 
    163163        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() 
     164       
    167165        iy = 0 
    168166        ix = 0 
     
    263261        self.SetSizer(vbox) 
    264262         
    265          
     263    def calculate_xray_sld(self, element): 
     264        """ 
     265        Get an element and compute the corresponding SLD for a given formula 
     266        :param element:  elements a string of existing atom 
     267        """ 
     268        myformula = formula(str(element)) 
     269        if len(myformula.atoms) != 1: 
     270            return  
     271        element = myformula.atoms.keys()[0]  
     272        energy = xray_energy(element.K_alpha) 
     273         
     274        self.sld_formula = formula(str(user_formula), density=self.density) 
     275        atom = self.sld_formula.atoms 
     276        return xray_sld_from_atoms(atom, density=self.density, energy= energy) 
     277     
    266278    def check_inputs(self): 
    267279        """Check validity user inputs""" 
     
    276288        self.wavelength = self.wavelength_ctl.GetValue() 
    277289        if self.wavelength.lstrip().rstrip() == "": 
    278             self.wavelength = self.calculator.wavelength 
     290            self.wavelength = WAVELENGTH 
    279291        else: 
    280292            if check_float(self.wavelength_ctl): 
     
    282294            else: 
    283295                flag = False 
    284                 raise ValueError,"Error for wavelenth value :expect float" 
     296                raise ValueError,"Error for wavelength value :expect float" 
    285297                 
    286         self.formulata_text = self.compound_ctl.GetValue().lstrip().rstrip() 
    287         if self.formulata_text != "": 
     298        self.compound = self.compound_ctl.GetValue().lstrip().rstrip() 
     299        if self.compound != "": 
    288300            self.compound_ctl.SetBackgroundColour(wx.WHITE) 
    289301            self.compound_ctl.Refresh() 
     
    295307        return flag  
    296308         
     309    def calculate_sld_helper(self, element, density, molecule_formula): 
     310        """ 
     311        Get an element and compute the corresponding SLD for a given formula 
     312         
     313        :param element:  elements a string of existing atom 
     314         
     315        """ 
     316        element_formula = formula(str(element)) 
     317        if len(element_formula.atoms) != 1: 
     318            return  
     319        element = element_formula.atoms.keys()[0]  
     320        energy = xray_energy(element.K_alpha) 
     321        atom = molecule_formula.atoms 
     322        return xray_sld_from_atoms(atom, density=density, energy=energy) 
     323 
     324 
    297325    def calculateSld(self, event): 
    298326        """ 
     
    304332                #get ready to compute 
    305333                try: 
    306                     self.calculator.set_value(self.formulata_text, 
    307                                               self.density, self.wavelength) 
     334                    self.sld_formula = formula(self.compound, density=self.density) 
    308335                except: 
    309336                    if self.base is not None: 
     
    314341                    return 
    315342                 
     343                 
     344                (sld_real, sld_im, sld_inc), (coh,absorp,incoh), \ 
     345                            length = neutron_scattering(self.compound, 
     346                                       self.density, self.wavelength)  
     347                Cu_reel, Cu_im = self.calculate_sld_helper(element="Cu", 
     348                                                     density=self.density, 
     349                                            molecule_formula=self.sld_formula) 
     350                Mo_reel, Mo_im = self.calculate_sld_helper(element="Mo",  
     351                                                           density=self.density, 
     352                                         molecule_formula=self.sld_formula) 
     353                # set neutron sld values 
     354                self.neutron_sld_reel_ctl.SetValue(format_number(sld_real * _SCALE)) 
     355                self.neutron_sld_im_ctl.SetValue(format_number(sld_im * _SCALE)) 
    316356                # Compute the Cu SLD 
    317                 Cu_reel, Cu_im = self.calculator.calculate_xray_sld( "Cu") 
    318357                self.cu_ka_sld_reel_ctl.SetValue(format_number(Cu_reel *_SCALE)) 
    319358                self.cu_ka_sld_im_ctl.SetValue(format_number(Cu_im * _SCALE)) 
    320                  
    321359                # Compute the Mo SLD 
    322                 Mo_reel, Mo_im = self.calculator.calculate_xray_sld( "Mo") 
    323360                self.mo_ka_sld_reel_ctl.SetValue(format_number(Mo_reel *_SCALE)) 
    324361                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)) 
     362                # set incoherence and absorption 
     363                self.neutron_inc_ctl.SetValue(format_number(incoh)) 
    335364                self.neutron_abs_ctl.SetValue(format_number(absorp)) 
    336365                # Neutron length 
     
    347376 
    348377    
    349          
    350    
    351      
    352     
    353   
    354          
    355                 
    356378class SldWindow(wx.Frame): 
    357379    def __init__(self, parent=None, id=1, title="SLD Calculator",base=None): 
Note: See TracChangeset for help on using the changeset viewer.