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