Changeset e2da671 in sasmodels for sasmodels/sasview_model.py


Ignore:
Timestamp:
Mar 6, 2019 6:10:02 PM (5 years ago)
Author:
Paul Kienzle <pkienzle@…>
Branches:
master
Children:
f64b154
Parents:
4e96703 (diff), 9150036 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'beta_approx' into ticket-608-user-defined-weights

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/sasview_model.py

    r4e96703 re2da671  
    2525from . import core 
    2626from . import custom 
     27from . import kernelcl 
    2728from . import product 
    2829from . import generate 
     
    3031from . import modelinfo 
    3132from .details import make_kernel_args, dispersion_mesh 
     33from .kernelcl import reset_environment 
    3234 
    3335# Hack: load in any custom distributions 
     
    7375#: has changed since we last reloaded. 
    7476_CACHED_MODULE = {}  # type: Dict[str, "module"] 
     77 
     78def reset_environment(): 
     79    # type: () -> None 
     80    """ 
     81    Clear the compute engine context so that the GUI can change devices. 
     82 
     83    This removes all compiled kernels, even those that are active on fit 
     84    pages, but they will be restored the next time they are needed. 
     85    """ 
     86    kernelcl.reset_environment() 
     87    for model in MODELS.values(): 
     88        model._model = None 
    7589 
    7690def find_model(modelname): 
     
    387401            hidden.add('scale') 
    388402            hidden.add('background') 
    389             self._model_info.parameters.defaults['background'] = 0. 
    390403 
    391404        # Update the parameter lists to exclude any hidden parameters 
     
    700713            return self._calculate_Iq(qx, qy) 
    701714 
    702     def _calculate_Iq(self, qx, qy=None, Fq=False, effective_radius_type=1): 
     715    def _calculate_Iq(self, qx, qy=None): 
    703716        if self._model is None: 
    704             self._model = core.build_model(self._model_info) 
     717            # Only need one copy of the compiled kernel regardless of how many 
     718            # times it is used, so store it in the class.  Also, to reset the 
     719            # compute engine, need to clear out all existing compiled kernels, 
     720            # which is much easier to do if we store them in the class. 
     721            self.__class__._model = core.build_model(self._model_info) 
    705722        if qy is not None: 
    706723            q_vectors = [np.asarray(qx), np.asarray(qy)] 
     
    720737        #print("values", values) 
    721738        #print("is_mag", is_magnetic) 
    722         if Fq: 
    723             result = calculator.Fq(call_details, values, cutoff=self.cutoff, 
    724                                    magnetic=is_magnetic, 
    725                                    effective_radius_type=effective_radius_type) 
    726739        result = calculator(call_details, values, cutoff=self.cutoff, 
    727740                            magnetic=is_magnetic) 
     
    741754        Calculate the effective radius for P(q)*S(q) 
    742755 
     756        *mode* is the R_eff type, which defaults to 1 to match the ER 
     757        calculation for sasview models from version 3.x. 
     758 
    743759        :return: the value of the effective radius 
    744760        """ 
    745         Fq = self._calculate_Iq([0.1], True, mode) 
    746         return Fq[2] 
     761        # ER and VR are only needed for old multiplication models, based on 
     762        # sas.sascalc.fit.MultiplicationModel.  Fail for now.  If we want to 
     763        # continue supporting them then add some test cases so that the code 
     764        # is exercised.  We can access ER/VR using the kernel Fq function by 
     765        # extending _calculate_Iq so that it calls: 
     766        #    if er_mode > 0: 
     767        #        res = calculator.Fq(call_details, values, cutoff=self.cutoff, 
     768        #                            magnetic=False, effective_radius_type=mode) 
     769        #        R_eff, form_shell_ratio = res[2], res[4] 
     770        #        return R_eff, form_shell_ratio 
     771        # Then use the following in calculate_ER: 
     772        #    ER, VR = self._calculate_Iq(q=[0.1], er_mode=mode) 
     773        #    return ER 
     774        # Similarly, for calculate_VR: 
     775        #    ER, VR = self._calculate_Iq(q=[0.1], er_mode=1) 
     776        #    return VR 
     777        # Obviously a combined calculate_ER_VR method would be better, but 
     778        # we only need them to support very old models, so ignore the 2x 
     779        # performance hit. 
     780        raise NotImplementedError("ER function is no longer available.") 
    747781 
    748782    def calculate_VR(self): 
     
    753787        :return: the value of the form:shell volume ratio 
    754788        """ 
    755         Fq = self._calculate_Iq([0.1], True, mode) 
    756         return Fq[4] 
     789        # See comments in calculate_ER. 
     790        raise NotImplementedError("VR function is no longer available.") 
    757791 
    758792    def set_dispersion(self, parameter, dispersion): 
     
    919953    CylinderModel().evalDistribution([0.1, 0.1]) 
    920954 
     955def test_structure_factor_background(): 
     956    # type: () -> None 
     957    """ 
     958    Check that sasview model and direct model match, with background=0. 
     959    """ 
     960    from .data import empty_data1D 
     961    from .core import load_model_info, build_model 
     962    from .direct_model import DirectModel 
     963 
     964    model_name = "hardsphere" 
     965    q = [0.0] 
     966 
     967    sasview_model = _make_standard_model(model_name)() 
     968    sasview_value = sasview_model.evalDistribution(np.array(q))[0] 
     969 
     970    data = empty_data1D(q) 
     971    model_info = load_model_info(model_name) 
     972    model = build_model(model_info) 
     973    direct_model = DirectModel(data, model) 
     974    direct_value_zero_background = direct_model(background=0.0) 
     975 
     976    assert sasview_value == direct_value_zero_background 
     977 
     978    # Additionally check that direct value background defaults to zero 
     979    direct_value_default = direct_model() 
     980    assert sasview_value == direct_value_default 
     981 
     982 
    921983def magnetic_demo(): 
    922984    Model = _make_standard_model('sphere') 
     
    9391001    #print("rpa:", test_rpa()) 
    9401002    #test_empty_distribution() 
     1003    #test_structure_factor_background() 
Note: See TracChangeset for help on using the changeset viewer.