Changeset 912f438 in sasview for src


Ignore:
Timestamp:
Mar 26, 2019 7:14:53 AM (6 years ago)
Author:
awashington
Branches:
ESS_GUI, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_opencl
Children:
82efbe9
Parents:
14e1ff0
Message:

Separate neutron and xray sld calculation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/qtgui/Calculators/SldPanel.py

    r33c0561 r912f438  
    3232) 
    3333 
    34 class SldResult(object): 
    35     def __init__(self, molecular_formula, mass_density, 
    36         neutron_wavelength, neutron_sld_real, neutron_sld_imag, 
    37         xray_wavelength, xray_sld_real, xray_sld_imag, 
    38         neutron_inc_xs, neutron_abs_xs, neutron_length): 
    39  
    40         self.molecular_formula = molecular_formula 
    41         self.mass_density = mass_density 
     34class NeutronSldResult(object): 
     35    def __init__(self, neutron_wavelength, neutron_sld_real, 
     36                 neutron_sld_imag, neutron_inc_xs, neutron_abs_xs, 
     37                 neutron_length): 
     38 
    4239        self.neutron_wavelength = neutron_wavelength 
    4340        self.neutron_sld_real = neutron_sld_real 
    4441        self.neutron_sld_imag = neutron_sld_imag 
     42        self.neutron_inc_xs = neutron_inc_xs 
     43        self.neutron_abs_xs = neutron_abs_xs 
     44        self.neutron_length = neutron_length 
     45 
     46class XraySldResult(object): 
     47    def __init__(self, xray_wavelength, xray_sld_real, xray_sld_imag): 
     48 
    4549        self.xray_wavelength = xray_wavelength 
    4650        self.xray_sld_real = xray_sld_real 
    4751        self.xray_sld_imag = xray_sld_imag 
    48         self.neutron_inc_xs = neutron_inc_xs 
    49         self.neutron_abs_xs = neutron_abs_xs 
    50         self.neutron_length = neutron_length 
    51  
    52 def sldAlgorithm(molecular_formula, mass_density, neutron_wavelength, xray_wavelength): 
    53  
    54     xray_sld_real, xray_sld_imag = xray_sld( 
    55             compound=molecular_formula, 
    56             density=mass_density, 
    57             wavelength=xray_wavelength) 
     52 
     53def neutronSldAlgorithm(molecular_formula, mass_density, neutron_wavelength): 
    5854 
    5955    (neutron_sld_real, neutron_sld_imag, _), (_, neutron_abs_xs, neutron_inc_xs), neutron_length = \ 
     
    6965    scaled_neutron_sld_imag = SCALE * abs(neutron_sld_imag) 
    7066 
     67    return NeutronSldResult(neutron_wavelength, scaled_neutron_sld_real, 
     68                            scaled_neutron_sld_imag, neutron_inc_xs, 
     69                            neutron_abs_xs, neutron_length) 
     70 
     71def xraySldAlgorithm(molecular_formula, mass_density, xray_wavelength): 
     72 
     73    xray_sld_real, xray_sld_imag = xray_sld( 
     74            compound=molecular_formula, 
     75            density=mass_density, 
     76            wavelength=xray_wavelength) 
     77 
     78    SCALE = 1e-6 
     79 
    7180    # xray sld 
    7281    scaled_xray_sld_real = SCALE * xray_sld_real 
     
    7483 
    7584 
    76     return SldResult( 
    77         molecular_formula, mass_density, 
    78         neutron_wavelength, scaled_neutron_sld_real, scaled_neutron_sld_imag, 
    79         xray_wavelength, scaled_xray_sld_real, scaled_xray_sld_imag, 
    80         neutron_inc_xs, neutron_abs_xs, neutron_length) 
     85    return XraySldResult(xray_wavelength, scaled_xray_sld_real, 
     86                         scaled_xray_sld_imag) 
    8187 
    8288 
     
    176182        xrayWavelength = self.ui.editXrayWavelength.text() 
    177183 
    178         if len(formula) > 0 and len(density) > 0 and len(neutronWavelength) > 0 and len(xrayWavelength) > 0: 
    179             try: 
    180                 results = sldAlgorithm(str(formula), float(density), float(neutronWavelength), float(xrayWavelength)) 
    181  
    182                 def format(value): 
    183                     return ("%-5.3g" % value).strip() 
    184  
    185                 self.model.item(MODEL.NEUTRON_SLD_REAL).setText(format(results.neutron_sld_real)) 
    186                 self.model.item(MODEL.NEUTRON_SLD_IMAG).setText(format(results.neutron_sld_imag)) 
    187  
    188                 self.model.item(MODEL.XRAY_SLD_REAL).setText(format(results.xray_sld_real)) 
    189                 self.model.item(MODEL.XRAY_SLD_IMAG).setText(format(results.xray_sld_imag)) 
    190  
    191                 self.model.item(MODEL.NEUTRON_INC_XS).setText(format(results.neutron_inc_xs)) 
    192                 self.model.item(MODEL.NEUTRON_ABS_XS).setText(format(results.neutron_abs_xs)) 
    193                 self.model.item(MODEL.NEUTRON_LENGTH).setText(format(results.neutron_length)) 
    194  
    195                 return 
    196  
    197             except Exception as e: 
    198                 pass 
    199  
    200         for key in list(self._getOutputs().keys()): 
    201             self.model.item(key).setText("") 
     184        if not formula or not density: 
     185            return 
     186 
     187        def format(value): 
     188            return ("%-5.3g" % value).strip() 
     189 
     190        if neutronWavelength: 
     191            results = neutronSldAlgorithm(str(formula), float(density), float(neutronWavelength)) 
     192 
     193            self.model.item(MODEL.NEUTRON_SLD_REAL).setText(format(results.neutron_sld_real)) 
     194            self.model.item(MODEL.NEUTRON_SLD_IMAG).setText(format(results.neutron_sld_imag)) 
     195            self.model.item(MODEL.NEUTRON_INC_XS).setText(format(results.neutron_inc_xs)) 
     196            self.model.item(MODEL.NEUTRON_ABS_XS).setText(format(results.neutron_abs_xs)) 
     197            self.model.item(MODEL.NEUTRON_LENGTH).setText(format(results.neutron_length)) 
     198        else: 
     199            self.model.item(MODEL.NEUTRON_SLD_REAL).setText("") 
     200            self.model.item(MODEL.NEUTRON_SLD_IMAG).setText("") 
     201            self.model.item(MODEL.NEUTRON_INC_XS).setText("") 
     202            self.model.item(MODEL.NEUTRON_ABS_XS).setText("") 
     203            self.model.item(MODEL.NEUTRON_LENGTH).setText("") 
     204 
     205        if xrayWavelength: 
     206            results = xraySldAlgorithm(str(formula), float(density), float(xrayWavelength)) 
     207 
     208            self.model.item(MODEL.XRAY_SLD_REAL).setText(format(results.xray_sld_real)) 
     209            self.model.item(MODEL.XRAY_SLD_IMAG).setText(format(results.xray_sld_imag)) 
     210        else: 
     211            self.model.item(MODEL.XRAY_SLD_REAL).setText("") 
     212            self.model.item(MODEL.XRAY_SLD_IMAG).setText("") 
    202213 
    203214    def modelReset(self): 
Note: See TracChangeset for help on using the changeset viewer.