source: sasview/src/sas/models/CoreMultiShellModel.py @ e0c16ce

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since e0c16ce was e0c16ce, checked in by butler, 9 years ago

Fix sphinx compile issues (and a bunch of pyling white spaces) in
CoreMultiShellModel?

  • Property mode set to 100644
File size: 10.9 KB
Line 
1"""
2    Core-Multi-Shell model
3"""
4from sas.models.BaseComponent import BaseComponent
5from sas.models.CoreFourShellModel import CoreFourShellModel
6import copy
7max_nshells = 5
8class CoreMultiShellModel(BaseComponent):
9    """
10    This multi-model is based on CoreFourShellModel and provides the capability
11    of changing the number of shells between 1 and 4.
12    """
13    def __init__(self, multfactor=1):
14        BaseComponent.__init__(self)
15        """
16        :param n_shells: number of shells in the model, assumes 1<= n_shells <=4.
17        """
18
19        ## Setting  model name model description
20        self.description=""
21        model = CoreFourShellModel()
22        self.model = model
23        self.name = "CoreMultiShellModel"
24        self.description=""
25        self.n_shells = multfactor
26        ## Define parameters
27        self.params = {}
28
29        ## Parameter details [units, min, max]
30        self.details = {}
31       
32        # non-fittable parameters
33        self.non_fittable = model.non_fittable
34       
35        ## dispersion
36        self._set_dispersion()
37        ## Define parameters
38        self._set_params()
39       
40        ## Parameter details [units, min, max]
41        self._set_details()
42       
43        #list of parameter that can be fitted
44        self._set_fixed_params() 
45        self.orientation_params = []
46        self.magnetic_params = []
47       
48        ## functional multiplicity info of the model
49        # [int(maximum no. of functionality),"str(Titl),
50        # [str(name of function0),...], [str(x-asix name of sld),...]]
51        self.multiplicity_info = [max_nshells, "No. of Shells:", [], ['Radius']]
52
53        ## parameters with orientation: can be removed since there is no orientational params
54        self._set_orientation_params()
55
56    def _clone(self, obj):
57        """
58        Internal utility function to copy the internal
59        data members to a fresh copy.
60        """
61        obj.params = copy.deepcopy(self.params)
62        obj.description = copy.deepcopy(self.description)
63        obj.details = copy.deepcopy(self.details)
64        obj.dispersion = copy.deepcopy(self.dispersion)
65        obj.model = self.model.clone()
66
67        return obj
68
69    def _set_dispersion(self):
70        """
71        model dispersions
72        Polydispersion should not be applied to s_model
73        """
74        ##set dispersion from model
75        for name , value in self.model.dispersion.iteritems():     
76            nshell = 0
77            if name.split('_').count('thick') > 0:
78                while nshell < self.n_shells:
79                    nshell += 1
80                    if name.split('_')[-1] == 'shell%s' % str(nshell):
81                        self.dispersion[name] = value
82                    else:
83                        continue
84            else:
85                self.dispersion[name] = value
86
87    def _set_orientation_params(self):
88        """
89        model orientation and magnetic parameters, same params for this model
90        """
91        ##set dispersion from model
92        for param in self.model.orientation_params:     
93            nshell = 0
94            if param.split('_')[-1].count('shell') < 1:
95                #print "param", param, param.split('_')[-1].count('shell')
96                self.orientation_params.append(param)
97                self.magnetic_params.append(param)
98                continue
99            while nshell < self.n_shells:
100                nshell += 1
101                if param.split('_')[-1] == 'shell%s' % str(nshell):
102                    self.orientation_params.append(param)
103                    self.magnetic_params.append(param)
104                    continue
105
106    def _set_params(self):
107        """
108        Concatenate the parameters of the model to create
109        this model parameters
110        """
111        # rearrange the parameters for the given # of shells
112        for name , value in self.model.params.iteritems():
113            nshell = 0
114            if name.split('_').count('thick') > 0 or \
115                        name.split('_').count('sld') > 0 or \
116                        name[0] == 'M':
117                if name.split('_')[-1] == 'solv' or \
118                        name.split('_')[-1] == 'core0':
119                    self.params[name]= value
120                    continue
121                while nshell < self.n_shells:
122                    nshell += 1
123                    if name.split('_')[-1] == 'shell%s' % str(nshell):
124                        self.params[name]= value
125                        continue
126            else:
127                self.params[name]= value
128
129        # set constrained values for the original model params
130        self._set_xtra_model_param()
131
132    def _set_details(self):
133        """
134        Concatenate details of the original model to create
135        this model details
136        """
137        for name ,detail in self.model.details.iteritems():
138            if name in self.params.iterkeys():
139                self.details[name]= detail
140
141
142    def _set_xtra_model_param(self):
143        """
144        Set params of original model that are hidden from this model
145        """
146        # look for the model parameters that are not in param list
147        for key in self.model.params.iterkeys():
148            if key not in self.params.keys():
149                if  key.split('_').count('thick') > 0:
150                    self.model.setParam(key, 0)
151                    continue
152
153                for nshell in range(self.n_shells,max_nshells):
154                     if key.split('_').count('sld') > 0 and \
155                            key.split('_')[-1] == 'shell%s' % str(nshell+1):
156                        try:
157                            if key[0] != 'M':
158                                value = self.model.params['sld_solv']
159                                self.model.setParam(key, value)
160                            else:
161                                self.model.setParam(key, 0.0)
162                        except: pass
163
164
165    def getProfile(self):
166        """
167        Get SLD profile
168        **Note:** This works only for func_shell num = 2.
169
170        :return: (r, beta) where r is a list of radius of the transition points\
171         and beta is a list of the corresponding SLD values.
172        """
173        r = []
174        beta = []
175        # for core at r=0
176        r.append(0)
177        beta.append(self.params['sld_core0'])
178        # for core at r=rad_core
179        r.append(self.params['rad_core0'])
180        beta.append(self.params['sld_core0'])
181
182        # for shells
183        for n in range(1, self.n_shells+1):
184            # Left side of each shells
185            r0 = r[len(r)-1]
186            r.append(r0)
187            exec "beta.append(self.params['sld_shell%s'% str(n)])"
188
189            # Right side of each shells
190            exec "r0 += self.params['thick_shell%s'% str(n)]"
191            r.append(r0)
192            exec "beta.append(self.params['sld_shell%s'% str(n)])"
193
194        # for solvent
195        r0 = r[len(r)-1]           
196        r.append(r0)
197        beta.append(self.params['sld_solv'])
198        r_solv = 5*r0/4
199        r.append(r_solv)
200        beta.append(self.params['sld_solv'])
201
202        return r, beta
203
204    def setParam(self, name, value):
205        """
206        Set the value of a model parameter
207
208        :param name: name of the parameter
209        :param value: value of the parameter
210        """
211        # set param to new model
212        self._setParamHelper( name, value)
213        ## setParam to model
214        if name == 'sld_solv':
215            # the sld_*** model.params not in params must set to value of sld_solv
216            for key in self.model.params.iterkeys():
217                if key not in self.params.keys():
218                    if key.split('_')[0] == 'sld':
219                        self.model.setParam(key, value)
220                    elif key.split('_')[1] == 'sld':
221                        # mag params
222                        self.model.setParam(key, 0.0)
223        self.model.setParam( name, value)
224
225    def _setParamHelper(self, name, value):
226        """
227        Helper function to setParam
228        """
229        #look for dispersion parameters
230        toks = name.split('.')
231        if len(toks) == 2:
232            for item in self.dispersion.keys():
233                if item.lower()==toks[0].lower():
234                    for par in self.dispersion[item]:
235                        if par.lower() == toks[1].lower():
236                            self.dispersion[item][par] = value
237                            return
238        # Look for standard parameter
239        for item in self.params.keys():
240            if item.lower()==name.lower():
241                self.params[item] = value
242                return
243        #raise ValueError, "Model does not contain parameter %s" % name
244
245
246    def _set_fixed_params(self):
247        """
248        Fill the self.fixed list with the model fixed list
249        """
250        for item in self.model.fixed:
251            if item.split('.')[0] in self.params.keys():
252                self.fixed.append(item)
253
254        self.fixed.sort()
255
256    def run(self, x = 0.0):
257        """
258        Evaluate the model
259
260        :param x: input q-value (float or [float, float] as [r, theta])
261        :return: (DAB value)
262        """
263        # set effective radius and scaling factor before run
264
265        return self.model.run(x)
266
267    def runXY(self, x = 0.0):
268        """
269        Evaluate the model
270
271        :param x: input q-value (float or [float, float] as [qx, qy])
272        :return: DAB value
273        """ 
274        # set effective radius and scaling factor before run
275
276        return self.model.runXY(x)
277
278    ## Now (May27,10) directly uses the model eval function
279    ## instead of the for-loop in Base Component.
280    def evalDistribution(self, x = []):
281        """
282        Evaluate the model in cartesian coordinates
283
284        :param x: input q[], or [qx[], qy[]]
285        :return: scattering function P(q[])
286        """
287        # set effective radius and scaling factor before run
288        return self.model.evalDistribution(x)
289
290    def calculate_ER(self):
291        """
292        Calculate the effective radius for P(q)*S(q)
293
294        :return: the value of the effective radius
295        """
296        return self.model.calculate_ER() 
297
298    def calculate_VR(self):
299        """
300        Calculate the volf ratio for P(q)*S(q)
301
302        :return: the value of the volf ratio
303        """
304        return self.model.calculate_VR()
305
306    def set_dispersion(self, parameter, dispersion):
307        """
308        Set the dispersion object for a model parameter
309
310        :param parameter: name of the parameter [string]
311        :param dispersion: dispersion object of type DispersionModel
312        """
313        value = None
314        try:
315            if parameter in self.model.dispersion.keys():
316                value = self.model.set_dispersion(parameter, dispersion)
317            self._set_dispersion()
318            return value
319        except:
320            raise
Note: See TracBrowser for help on using the repository browser.