Changeset e2da671 in sasmodels for sasmodels/sasview_model.py
- Timestamp:
- Mar 6, 2019 6:10:02 PM (5 years ago)
- 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. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sasmodels/sasview_model.py
r4e96703 re2da671 25 25 from . import core 26 26 from . import custom 27 from . import kernelcl 27 28 from . import product 28 29 from . import generate … … 30 31 from . import modelinfo 31 32 from .details import make_kernel_args, dispersion_mesh 33 from .kernelcl import reset_environment 32 34 33 35 # Hack: load in any custom distributions … … 73 75 #: has changed since we last reloaded. 74 76 _CACHED_MODULE = {} # type: Dict[str, "module"] 77 78 def 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 75 89 76 90 def find_model(modelname): … … 387 401 hidden.add('scale') 388 402 hidden.add('background') 389 self._model_info.parameters.defaults['background'] = 0.390 403 391 404 # Update the parameter lists to exclude any hidden parameters … … 700 713 return self._calculate_Iq(qx, qy) 701 714 702 def _calculate_Iq(self, qx, qy=None , Fq=False, effective_radius_type=1):715 def _calculate_Iq(self, qx, qy=None): 703 716 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) 705 722 if qy is not None: 706 723 q_vectors = [np.asarray(qx), np.asarray(qy)] … … 720 737 #print("values", values) 721 738 #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)726 739 result = calculator(call_details, values, cutoff=self.cutoff, 727 740 magnetic=is_magnetic) … … 741 754 Calculate the effective radius for P(q)*S(q) 742 755 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 743 759 :return: the value of the effective radius 744 760 """ 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.") 747 781 748 782 def calculate_VR(self): … … 753 787 :return: the value of the form:shell volume ratio 754 788 """ 755 Fq = self._calculate_Iq([0.1], True, mode)756 r eturn Fq[4]789 # See comments in calculate_ER. 790 raise NotImplementedError("VR function is no longer available.") 757 791 758 792 def set_dispersion(self, parameter, dispersion): … … 919 953 CylinderModel().evalDistribution([0.1, 0.1]) 920 954 955 def 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 921 983 def magnetic_demo(): 922 984 Model = _make_standard_model('sphere') … … 939 1001 #print("rpa:", test_rpa()) 940 1002 #test_empty_distribution() 1003 #test_structure_factor_background()
Note: See TracChangeset
for help on using the changeset viewer.