[2e0053e] | 1 | """ |
---|
| 2 | Spherical SLD model |
---|
| 3 | """ |
---|
[79492222] | 4 | from sas.models.BaseComponent import BaseComponent |
---|
| 5 | from sas.models.SphereSLDModel import SphereSLDModel |
---|
[4b3d25b] | 6 | from copy import deepcopy |
---|
| 7 | func_list = {'Erf(|nu|*z)':0, 'RPower(z^|nu|)':1, 'LPower(z^|nu|)':2, \ |
---|
| 8 | 'RExp(-|nu|*z)':3, 'LExp(-|nu|*z)':4} |
---|
| 9 | max_nshells = 10 |
---|
[2e0053e] | 10 | |
---|
[4b3d25b] | 11 | class SphericalSLDModel(BaseComponent): |
---|
| 12 | """ |
---|
| 13 | This multi-model is based on Parratt formalism and provides the capability |
---|
| 14 | of changing the number of layers between 0 and 10. |
---|
| 15 | """ |
---|
| 16 | def __init__(self, multfactor=1): |
---|
| 17 | """ |
---|
[2e0053e] | 18 | :param multfactor: number of layers in the model, |
---|
| 19 | assumes 0<= n_shells <=10. |
---|
[4b3d25b] | 20 | """ |
---|
[2e0053e] | 21 | BaseComponent.__init__(self) |
---|
[4b3d25b] | 22 | |
---|
| 23 | ## Setting model name model description |
---|
[2e0053e] | 24 | self.description = "" |
---|
[4b3d25b] | 25 | model = SphereSLDModel() |
---|
| 26 | self.model = model |
---|
| 27 | self.name = "SphericalSLDModel" |
---|
[2e0053e] | 28 | self.description = model.description |
---|
[4b3d25b] | 29 | self.n_shells = multfactor |
---|
| 30 | ## Define parameters |
---|
| 31 | self.params = {} |
---|
| 32 | |
---|
| 33 | ## Parameter details [units, min, max] |
---|
| 34 | self.details = {} |
---|
| 35 | |
---|
| 36 | # non-fittable parameters |
---|
| 37 | self.non_fittable = model.non_fittable |
---|
| 38 | |
---|
| 39 | # list of function in order of the function number |
---|
| 40 | self.fun_list = self._get_func_list() |
---|
| 41 | ## dispersion |
---|
| 42 | self._set_dispersion() |
---|
| 43 | ## Define parameters |
---|
| 44 | self._set_params() |
---|
| 45 | |
---|
| 46 | ## Parameter details [units, min, max] |
---|
| 47 | self._set_details() |
---|
| 48 | |
---|
| 49 | #list of parameter that can be fitted |
---|
| 50 | self._set_fixed_params() |
---|
| 51 | self.model.params['n_shells'] = self.n_shells |
---|
| 52 | |
---|
| 53 | ## functional multiplicity info of the model |
---|
| 54 | # [int(maximum no. of functionality),"str(Titl), |
---|
| 55 | # [str(name of function0),...], [str(x-asix name of sld),...]] |
---|
[2e0053e] | 56 | self.multiplicity_info = [max_nshells, "No. of Shells:", [], ['Radius']] |
---|
[4b3d25b] | 57 | |
---|
| 58 | |
---|
| 59 | def _clone(self, obj): |
---|
| 60 | """ |
---|
| 61 | Internal utility function to copy the internal |
---|
| 62 | data members to a fresh copy. |
---|
| 63 | """ |
---|
| 64 | obj.params = deepcopy(self.params) |
---|
| 65 | obj.non_fittable = deepcopy(self.non_fittable) |
---|
| 66 | obj.description = deepcopy(self.description) |
---|
| 67 | obj.details = deepcopy(self.details) |
---|
| 68 | obj.dispersion = deepcopy(self.dispersion) |
---|
| 69 | obj.model = self.model.clone() |
---|
| 70 | |
---|
| 71 | return obj |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | def _set_dispersion(self): |
---|
| 75 | """ |
---|
| 76 | model dispersions |
---|
| 77 | """ |
---|
| 78 | ##set dispersion from model |
---|
[7a8faf8] | 79 | for name , value in self.model.dispersion.iteritems(): |
---|
| 80 | |
---|
| 81 | nshell = -1 |
---|
| 82 | if name.split('_')[0] == 'thick': |
---|
[2e0053e] | 83 | while nshell < 1: |
---|
[7a8faf8] | 84 | nshell += 1 |
---|
| 85 | if name.split('_')[1] == 'inter%s' % str(nshell): |
---|
[2e0053e] | 86 | self.dispersion[name] = value |
---|
[7a8faf8] | 87 | else: |
---|
| 88 | continue |
---|
| 89 | else: |
---|
[2e0053e] | 90 | self.dispersion[name] = value |
---|
[4b3d25b] | 91 | |
---|
| 92 | def _set_params(self): |
---|
| 93 | """ |
---|
| 94 | Concatenate the parameters of the model to create |
---|
| 95 | this model parameters |
---|
| 96 | """ |
---|
| 97 | # rearrange the parameters for the given # of shells |
---|
| 98 | for name , value in self.model.params.iteritems(): |
---|
| 99 | n = 0 |
---|
| 100 | pos = len(name.split('_'))-1 |
---|
| 101 | first_name = name.split('_')[0] |
---|
| 102 | last_name = name.split('_')[pos] |
---|
| 103 | if first_name == 'npts': |
---|
[2e0053e] | 104 | self.params[name] = value |
---|
[4b3d25b] | 105 | continue |
---|
| 106 | elif first_name == 'func': |
---|
[2e0053e] | 107 | n = -1 |
---|
| 108 | while n < self.n_shells: |
---|
[4b3d25b] | 109 | n += 1 |
---|
| 110 | if last_name == 'inter%s' % str(n): |
---|
[2e0053e] | 111 | self.params[name] = value |
---|
[4b3d25b] | 112 | continue |
---|
| 113 | elif last_name[0:5] == 'inter': |
---|
[2e0053e] | 114 | n = -1 |
---|
| 115 | while n < self.n_shells: |
---|
[4b3d25b] | 116 | n += 1 |
---|
| 117 | if last_name == 'inter%s' % str(n): |
---|
[2e0053e] | 118 | self.params[name] = value |
---|
[4b3d25b] | 119 | continue |
---|
| 120 | elif last_name[0:4] == 'flat': |
---|
[2e0053e] | 121 | while n < self.n_shells: |
---|
[4b3d25b] | 122 | n += 1 |
---|
| 123 | if last_name == 'flat%s' % str(n): |
---|
[2e0053e] | 124 | self.params[name] = value |
---|
[4b3d25b] | 125 | continue |
---|
| 126 | elif name == 'n_shells': |
---|
| 127 | continue |
---|
| 128 | else: |
---|
[2e0053e] | 129 | self.params[name] = value |
---|
[4b3d25b] | 130 | |
---|
| 131 | self.model.params['n_shells'] = self.n_shells |
---|
| 132 | |
---|
| 133 | # set constrained values for the original model params |
---|
| 134 | self._set_xtra_model_param() |
---|
| 135 | |
---|
| 136 | def _set_details(self): |
---|
| 137 | """ |
---|
| 138 | Concatenate details of the original model to create |
---|
| 139 | this model details |
---|
| 140 | """ |
---|
[2e0053e] | 141 | for name, detail in self.model.details.iteritems(): |
---|
[4b3d25b] | 142 | if name in self.params.iterkeys(): |
---|
[2e0053e] | 143 | self.details[name] = detail |
---|
[4b3d25b] | 144 | |
---|
| 145 | |
---|
| 146 | def _set_xtra_model_param(self): |
---|
| 147 | """ |
---|
| 148 | Set params of original model that are hidden from this model |
---|
| 149 | """ |
---|
| 150 | # look for the model parameters that are not in param list |
---|
| 151 | for key in self.model.params.iterkeys(): |
---|
| 152 | if key not in self.params.keys(): |
---|
[2e0053e] | 153 | if key.split('_')[0] == 'thick': |
---|
| 154 | self.model.setParam(key, 0) |
---|
| 155 | continue |
---|
| 156 | if key.split('_')[0] == 'func': |
---|
[4b3d25b] | 157 | self.model.setParam(key, 0) |
---|
| 158 | continue |
---|
| 159 | |
---|
[364658d] | 160 | for nshell in range(self.n_shells, max_nshells): |
---|
[4b3d25b] | 161 | if key.split('_')[1] == 'flat%s' % str(nshell+1): |
---|
| 162 | try: |
---|
| 163 | if key.split('_')[0] == 'sld': |
---|
| 164 | value = self.model.params['sld_solv'] |
---|
| 165 | self.model.setParam(key, value) |
---|
[2e0053e] | 166 | except: |
---|
| 167 | raise RuntimeError, "SphericalSLD model problem" |
---|
[4b3d25b] | 168 | |
---|
| 169 | def _get_func_list(self): |
---|
| 170 | """ |
---|
| 171 | Get the list of functions in each layer (shell) |
---|
| 172 | """ |
---|
| 173 | return func_list |
---|
| 174 | |
---|
| 175 | def getProfile(self): |
---|
| 176 | """ |
---|
| 177 | Get SLD profile |
---|
| 178 | |
---|
| 179 | : return: (z, beta) where z is a list of depth of the transition points |
---|
| 180 | beta is a list of the corresponding SLD values |
---|
| 181 | """ |
---|
| 182 | # max_pts for each layers |
---|
[cd075e8] | 183 | n_sub = int(self.params['npts_inter']) |
---|
[4b3d25b] | 184 | z = [] |
---|
| 185 | beta = [] |
---|
| 186 | z0 = 0 |
---|
| 187 | # two sld points for core |
---|
| 188 | z.append(0) |
---|
| 189 | beta.append(self.params['sld_core0']) |
---|
| 190 | z.append(self.params['rad_core0']) |
---|
| 191 | beta.append(self.params['sld_core0']) |
---|
| 192 | z0 += self.params['rad_core0'] |
---|
| 193 | # for layers from the core |
---|
[364658d] | 194 | for i in range(1, self.n_shells+2): |
---|
| 195 | dz = self.params['thick_inter%s' % str(i-1)]/n_sub |
---|
[4b3d25b] | 196 | # j=0 for interface, j=1 for flat layer |
---|
[2e0053e] | 197 | for j in range(0, 2): |
---|
[4b3d25b] | 198 | # interation for sub-layers |
---|
[2e0053e] | 199 | for n_s in range(0, n_sub+1): |
---|
| 200 | if j == 1: |
---|
| 201 | if i == self.n_shells+1: |
---|
[4b3d25b] | 202 | break |
---|
| 203 | # shift half sub thickness for the first point |
---|
| 204 | z0 -= dz#/2.0 |
---|
| 205 | z.append(z0) |
---|
| 206 | #z0 -= dz/2.0 |
---|
[2e0053e] | 207 | z0 += self.params['thick_flat%s' % str(i)] |
---|
[4b3d25b] | 208 | |
---|
[2e0053e] | 209 | sld_i = self.params['sld_flat%s' % str(i)] |
---|
| 210 | beta.append(self.params['sld_flat%s' % str(i)]) |
---|
[4b3d25b] | 211 | dz = 0 |
---|
| 212 | else: |
---|
[2e0053e] | 213 | nu = self.params['nu_inter%s' % str(i-1)] |
---|
[4b3d25b] | 214 | # decide which sld is which, sld_r or sld_l |
---|
| 215 | if i == 1: |
---|
| 216 | sld_l = self.params['sld_core0'] |
---|
| 217 | else: |
---|
[2e0053e] | 218 | sld_l = self.params['sld_flat%s' % str(i-1)] |
---|
[4b3d25b] | 219 | if i == self.n_shells+1: |
---|
| 220 | sld_r = self.params['sld_solv'] |
---|
| 221 | else: |
---|
[2e0053e] | 222 | sld_r = self.params['sld_flat%s' % str(i)] |
---|
[4b3d25b] | 223 | # get function type |
---|
[2e0053e] | 224 | func_idx = self.params['func_inter%s' % str(i-1)] |
---|
[4b3d25b] | 225 | # calculate the sld |
---|
| 226 | sld_i = self._get_sld(func_idx, n_sub, n_s, nu, |
---|
| 227 | sld_l, sld_r) |
---|
| 228 | # append to the list |
---|
| 229 | z.append(z0) |
---|
| 230 | beta.append(sld_i) |
---|
| 231 | z0 += dz |
---|
[2e0053e] | 232 | if j == 1: |
---|
| 233 | break |
---|
[4b3d25b] | 234 | # put sld of solvent |
---|
| 235 | z.append(z0) |
---|
| 236 | beta.append(self.params['sld_solv']) |
---|
| 237 | z_ext = z0/5.0 |
---|
| 238 | z.append(z0+z_ext) |
---|
| 239 | beta.append(self.params['sld_solv']) |
---|
| 240 | # return sld profile (r, beta) |
---|
| 241 | return z, beta |
---|
| 242 | |
---|
| 243 | def _get_sld(self, func_idx, n_sub, n_s, nu, sld_l, sld_r): |
---|
| 244 | """ |
---|
| 245 | Get the function asked to build sld profile |
---|
| 246 | : param func_idx: func type number |
---|
| 247 | : param n_sub: total number of sub_layer |
---|
| 248 | : param n_s: index of sub_layer |
---|
| 249 | : param nu: coefficient of the function |
---|
| 250 | : param sld_l: sld on the left side |
---|
| 251 | : param sld_r: sld on the right side |
---|
| 252 | : return: sld value, float |
---|
| 253 | """ |
---|
[79492222] | 254 | from sas.models.SLDCalFunc import SLDCalFunc |
---|
[4b3d25b] | 255 | # sld_cal init |
---|
| 256 | sld_cal = SLDCalFunc() |
---|
| 257 | # set params |
---|
[2e0053e] | 258 | sld_cal.setParam('fun_type', func_idx) |
---|
| 259 | sld_cal.setParam('npts_inter', n_sub) |
---|
| 260 | sld_cal.setParam('shell_num', n_s) |
---|
| 261 | sld_cal.setParam('nu_inter', nu) |
---|
| 262 | sld_cal.setParam('sld_left', sld_l) |
---|
| 263 | sld_cal.setParam('sld_right', sld_r) |
---|
[4b3d25b] | 264 | # return sld value |
---|
| 265 | return sld_cal.run() |
---|
| 266 | |
---|
| 267 | def setParam(self, name, value): |
---|
| 268 | """ |
---|
| 269 | Set the value of a model parameter |
---|
| 270 | |
---|
| 271 | : param name: name of the parameter |
---|
| 272 | : param value: value of the parameter |
---|
| 273 | """ |
---|
| 274 | # set param to new model |
---|
[2e0053e] | 275 | self._setParamHelper(name, value) |
---|
[4b3d25b] | 276 | |
---|
| 277 | ## setParam to model |
---|
[2e0053e] | 278 | if name == 'sld_solv': |
---|
[4b3d25b] | 279 | # the sld_*** model.params not in params must set to |
---|
| 280 | # value of sld_solv |
---|
| 281 | for key in self.model.params.iterkeys(): |
---|
[2e0053e] | 282 | if key not in self.params.keys() and key.split('_')[0] == 'sld': |
---|
| 283 | self.model.setParam(key, value) |
---|
[4b3d25b] | 284 | |
---|
[2e0053e] | 285 | self.model.setParam(name, value) |
---|
[4b3d25b] | 286 | |
---|
| 287 | def _setParamHelper(self, name, value): |
---|
| 288 | """ |
---|
| 289 | Helper function to setParam |
---|
| 290 | """ |
---|
[7a8faf8] | 291 | toks = name.split('.') |
---|
[2e0053e] | 292 | if len(toks) == 2: |
---|
[7a8faf8] | 293 | for item in self.dispersion.keys(): |
---|
[2e0053e] | 294 | if item.lower() == toks[0].lower(): |
---|
[7a8faf8] | 295 | for par in self.dispersion[item]: |
---|
| 296 | if par.lower() == toks[1].lower(): |
---|
| 297 | self.dispersion[item][par] = value |
---|
| 298 | return |
---|
[4b3d25b] | 299 | # Look for standard parameter |
---|
| 300 | for item in self.params.keys(): |
---|
[2e0053e] | 301 | if item.lower() == name.lower(): |
---|
[4b3d25b] | 302 | self.params[item] = value |
---|
| 303 | return |
---|
| 304 | |
---|
| 305 | raise ValueError, "Model does not contain parameter %s" % name |
---|
| 306 | |
---|
| 307 | |
---|
| 308 | def _set_fixed_params(self): |
---|
| 309 | """ |
---|
| 310 | Fill the self.fixed list with the model fixed list |
---|
| 311 | """ |
---|
[7a8faf8] | 312 | for item in self.model.fixed: |
---|
| 313 | if item.split('.')[0] in self.params.keys(): |
---|
| 314 | self.fixed.append(item) |
---|
| 315 | |
---|
| 316 | self.fixed.sort() |
---|
[4b3d25b] | 317 | |
---|
| 318 | def run(self, x = 0.0): |
---|
| 319 | """ |
---|
| 320 | Evaluate the model |
---|
| 321 | |
---|
| 322 | :param x: input q, or [q,phi] |
---|
| 323 | |
---|
| 324 | :return: scattering function P(q) |
---|
| 325 | |
---|
| 326 | """ |
---|
| 327 | |
---|
| 328 | return self.model.run(x) |
---|
| 329 | |
---|
| 330 | def runXY(self, x = 0.0): |
---|
| 331 | """ |
---|
| 332 | Evaluate the model |
---|
| 333 | |
---|
| 334 | : param x: input q-value (float or [float, float] as [qx, qy]) |
---|
| 335 | : return: scattering function value |
---|
| 336 | """ |
---|
| 337 | |
---|
| 338 | return self.model.runXY(x) |
---|
| 339 | |
---|
| 340 | ## Now (May27,10) directly uses the model eval function |
---|
| 341 | ## instead of the for-loop in Base Component. |
---|
[2e0053e] | 342 | def evalDistribution(self, x): |
---|
[4b3d25b] | 343 | """ |
---|
| 344 | Evaluate the model in cartesian coordinates |
---|
| 345 | |
---|
| 346 | : param x: input q[], or [qx[], qy[]] |
---|
| 347 | : return: scattering function P(q[]) |
---|
| 348 | """ |
---|
| 349 | # set effective radius and scaling factor before run |
---|
| 350 | return self.model.evalDistribution(x) |
---|
[2e0053e] | 351 | |
---|
[4b3d25b] | 352 | def calculate_ER(self): |
---|
| 353 | """ |
---|
| 354 | """ |
---|
| 355 | return self.model.calculate_ER() |
---|
[2e0053e] | 356 | |
---|
[4b3d25b] | 357 | def set_dispersion(self, parameter, dispersion): |
---|
| 358 | """ |
---|
| 359 | Set the dispersion object for a model parameter |
---|
| 360 | |
---|
| 361 | : param parameter: name of the parameter [string] |
---|
| 362 | :dispersion: dispersion object of type DispersionModel |
---|
| 363 | """ |
---|
[2e0053e] | 364 | value = None |
---|
| 365 | if parameter in self.model.dispersion.keys(): |
---|
| 366 | value = self.model.set_dispersion(parameter, dispersion) |
---|
| 367 | self._set_dispersion() |
---|
| 368 | return value |
---|