[e096270] | 1 | |
---|
| 2 | from sans.models.BaseComponent import BaseComponent |
---|
| 3 | from sans.models.OnionModel import OnionModel |
---|
| 4 | import copy |
---|
| 5 | max_nshells = 10 |
---|
| 6 | class SphereExpShellModel(BaseComponent): |
---|
| 7 | """ |
---|
| 8 | This multi-model is based on CoreMultiShellModel with exponential func shells and provides the capability |
---|
| 9 | of changing the number of shells between 1 and 10. |
---|
| 10 | """ |
---|
| 11 | def __init__(self, n_shells=1): |
---|
| 12 | BaseComponent.__init__(self) |
---|
| 13 | """ |
---|
| 14 | :param n_shells: number of shells in the model, assumes 1<= n_shells <=10. |
---|
| 15 | """ |
---|
| 16 | |
---|
| 17 | ## Setting model name model description |
---|
| 18 | self.description="" |
---|
| 19 | model = OnionModel() |
---|
| 20 | self.model = model |
---|
| 21 | self.name = "SphereExpShellModel" |
---|
| 22 | self.description=model.description |
---|
| 23 | self.n_shells = n_shells |
---|
| 24 | ## Define parameters |
---|
| 25 | self.params = {} |
---|
| 26 | |
---|
| 27 | ## Parameter details [units, min, max] |
---|
| 28 | self.details = {} |
---|
| 29 | |
---|
| 30 | ## dispersion |
---|
| 31 | self._set_dispersion() |
---|
| 32 | ## Define parameters |
---|
| 33 | self._set_params() |
---|
| 34 | |
---|
| 35 | ## Parameter details [units, min, max] |
---|
| 36 | self._set_details() |
---|
| 37 | |
---|
| 38 | #list of parameter that can be fitted |
---|
| 39 | self._set_fixed_params() |
---|
| 40 | self.model.params['n_shells'] = self.n_shells |
---|
[339ce67] | 41 | ## functional multiplicity info of the model |
---|
| 42 | # [int(maximum no. of functionality),"str(Titl), |
---|
| 43 | # [str(name of function0),...], [str(x-asix name of sld),...]] |
---|
| 44 | self.multiplicity_info = [max_nshells,"No. of Shells:",[],['Radius']] |
---|
| 45 | |
---|
[e096270] | 46 | ## parameters with orientation: can be removed since there is no orientational params |
---|
| 47 | for item in self.model.orientation_params: |
---|
| 48 | self.orientation_params.append(item) |
---|
[a1b2471] | 49 | self.getProfile() |
---|
[e096270] | 50 | def _clone(self, obj): |
---|
| 51 | """ |
---|
| 52 | Internal utility function to copy the internal |
---|
| 53 | data members to a fresh copy. |
---|
| 54 | """ |
---|
| 55 | obj.params = copy.deepcopy(self.params) |
---|
| 56 | obj.description = copy.deepcopy(self.description) |
---|
| 57 | obj.details = copy.deepcopy(self.details) |
---|
| 58 | obj.dispersion = copy.deepcopy(self.dispersion) |
---|
| 59 | obj.model = self.model.clone() |
---|
| 60 | |
---|
| 61 | return obj |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | def _set_dispersion(self): |
---|
| 65 | """ |
---|
| 66 | model dispersions |
---|
| 67 | """ |
---|
| 68 | ##set dispersion from model |
---|
| 69 | for name , value in self.model.dispersion.iteritems(): |
---|
| 70 | |
---|
| 71 | nshell = 0 |
---|
| 72 | if name.split('_')[0] == 'thick': |
---|
| 73 | while nshell<self.n_shells: |
---|
| 74 | nshell += 1 |
---|
| 75 | if name.split('_')[1] == 'shell%s' % str(nshell): |
---|
| 76 | self.dispersion[name]= value |
---|
| 77 | else: |
---|
| 78 | continue |
---|
| 79 | else: |
---|
| 80 | self.dispersion[name]= value |
---|
| 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 | pos = len(name.split('_'))-1 |
---|
| 92 | if name.split('_')[0] == 'func': |
---|
| 93 | continue |
---|
| 94 | elif name.split('_')[pos][0:5] == 'shell': |
---|
| 95 | while nshell<self.n_shells: |
---|
| 96 | nshell += 1 |
---|
| 97 | if name.split('_')[pos] == 'shell%s' % str(nshell): |
---|
| 98 | self.params[name]= value |
---|
| 99 | continue |
---|
| 100 | else: |
---|
| 101 | self.params[name]= value |
---|
| 102 | |
---|
| 103 | self.model.params['n_shells'] = self.n_shells |
---|
| 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(): |
---|
| 115 | self.details[name]= detail |
---|
| 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 | if key.split('_')[0] == 'A': |
---|
| 129 | self.model.setParam(key, 0) |
---|
| 130 | continue |
---|
| 131 | if key.split('_')[0] == 'func': |
---|
| 132 | self.model.setParam(key, 2) |
---|
| 133 | continue |
---|
| 134 | for nshell in range(self.n_shells,max_nshells): |
---|
| 135 | if key.split('_')[1] == 'sld_in_shell%s' % str(nshell+1): |
---|
| 136 | try: |
---|
| 137 | value = self.model.params['sld_solv'] |
---|
| 138 | self.model.setParam(key, value) |
---|
| 139 | except: pass |
---|
[a1b2471] | 140 | def getProfile(self): |
---|
| 141 | """ |
---|
| 142 | Get SLD profile |
---|
| 143 | |
---|
| 144 | : return: (r, beta) where r is a list of radius of the transition points |
---|
| 145 | beta is a list of the corresponding SLD values |
---|
| 146 | """ |
---|
| 147 | # max_pts for each shells |
---|
| 148 | max_pts = 10 |
---|
| 149 | r = [] |
---|
| 150 | beta = [] |
---|
| 151 | # for core at r=0 |
---|
| 152 | r.append(0) |
---|
[339ce67] | 153 | beta.append(self.params['sld_core0']) |
---|
[a1b2471] | 154 | # for core at r=rad_core |
---|
[339ce67] | 155 | r.append(self.params['rad_core0']) |
---|
| 156 | beta.append(self.params['sld_core0']) |
---|
[a1b2471] | 157 | |
---|
| 158 | # for shells |
---|
| 159 | for n in range(1,self.n_shells+1): |
---|
| 160 | # Left side of each shells |
---|
| 161 | r0 = r[len(r)-1] |
---|
| 162 | r.append(r0) |
---|
[a7107a6] | 163 | beta.append(self.params['sld_in_shell%s'% str(n)]) |
---|
[a1b2471] | 164 | |
---|
[a7107a6] | 165 | A = self.params['A_shell%s'% str(n)] |
---|
[fbae881] | 166 | if A <1.0e-16: |
---|
[a1b2471] | 167 | # Right side of each shells |
---|
[a7107a6] | 168 | r0 += self.params['thick_shell%s'% str(n)] |
---|
[a1b2471] | 169 | r.append(r0) |
---|
[a7107a6] | 170 | beta.append(self.params['sld_in_shell%s'% str(n)]) |
---|
[a1b2471] | 171 | else: |
---|
| 172 | from math import exp |
---|
| 173 | rn = r0 |
---|
| 174 | for n_sub in range(0,max_pts): |
---|
| 175 | # Right side of each sub_shells |
---|
[a7107a6] | 176 | rn += self.params['thick_shell%s'% str(n)]/10.0 |
---|
[a1b2471] | 177 | r.append(rn) |
---|
[a7107a6] | 178 | slope = (self.params['sld_out_shell%s'% str(n)] \ |
---|
[a1b2471] | 179 | -self.params['sld_in_shell%s'% str(n)]) \ |
---|
[a7107a6] | 180 | /(exp(self.params['A_shell%s'% str(n)])-1) |
---|
| 181 | const = (self.params['sld_in_shell%s'% str(n)]-slope) |
---|
| 182 | beta_n = slope*exp((self.params['A_shell%s'% str(n)]*(rn-r0) \ |
---|
| 183 | /self.params['thick_shell%s'% str(n)])) + const |
---|
[a1b2471] | 184 | beta.append(beta_n) |
---|
| 185 | |
---|
| 186 | # for solvent |
---|
| 187 | r0 = r[len(r)-1] |
---|
| 188 | r.append(r0) |
---|
| 189 | beta.append(self.params['sld_solv']) |
---|
| 190 | r_solv = 5*r0/4 |
---|
| 191 | r.append(r_solv) |
---|
| 192 | beta.append(self.params['sld_solv']) |
---|
| 193 | |
---|
| 194 | return r, beta |
---|
| 195 | |
---|
[e096270] | 196 | def setParam(self, name, value): |
---|
| 197 | """ |
---|
| 198 | Set the value of a model parameter |
---|
| 199 | |
---|
| 200 | : param name: name of the parameter |
---|
| 201 | : param value: value of the parameter |
---|
| 202 | """ |
---|
| 203 | # set param to new model |
---|
| 204 | self._setParamHelper( name, value) |
---|
| 205 | |
---|
| 206 | ## setParam to model |
---|
| 207 | if name=='sld_solv': |
---|
| 208 | # the sld_*** model.params not in params must set to value of sld_solv |
---|
| 209 | for key in self.model.params.iterkeys(): |
---|
| 210 | if key not in self.params.keys()and key.split('_')[0] == 'sld': |
---|
| 211 | self.model.setParam(key, value) |
---|
| 212 | self.model.setParam( name, value) |
---|
| 213 | |
---|
| 214 | def _setParamHelper(self, name, value): |
---|
| 215 | """ |
---|
| 216 | Helper function to setParam |
---|
| 217 | """ |
---|
| 218 | #look for dispersion parameters |
---|
| 219 | toks = name.split('.') |
---|
| 220 | if len(toks)==2: |
---|
| 221 | for item in self.dispersion.keys(): |
---|
| 222 | if item.lower()==toks[0].lower(): |
---|
| 223 | for par in self.dispersion[item]: |
---|
| 224 | if par.lower() == toks[1].lower(): |
---|
| 225 | self.dispersion[item][par] = value |
---|
| 226 | return |
---|
| 227 | # Look for standard parameter |
---|
| 228 | for item in self.params.keys(): |
---|
| 229 | if item.lower()==name.lower(): |
---|
| 230 | self.params[item] = value |
---|
| 231 | return |
---|
| 232 | |
---|
| 233 | raise ValueError, "Model does not contain parameter %s" % name |
---|
| 234 | |
---|
| 235 | |
---|
| 236 | def _set_fixed_params(self): |
---|
| 237 | """ |
---|
| 238 | Fill the self.fixed list with the model fixed list |
---|
| 239 | """ |
---|
| 240 | for item in self.model.fixed: |
---|
| 241 | if item.split('.')[0] in self.params.keys(): |
---|
| 242 | self.fixed.append(item) |
---|
| 243 | |
---|
| 244 | self.fixed.sort() |
---|
| 245 | pass |
---|
| 246 | |
---|
| 247 | def run(self, x = 0.0): |
---|
| 248 | """ |
---|
| 249 | Evaluate the model |
---|
| 250 | |
---|
| 251 | : param x: input q-value (float or [float, float] as [r, theta]) |
---|
[a7107a6] | 252 | : return: (I value) |
---|
[e096270] | 253 | """ |
---|
| 254 | |
---|
| 255 | return self.model.run(x) |
---|
| 256 | |
---|
| 257 | def runXY(self, x = 0.0): |
---|
| 258 | """ |
---|
| 259 | Evaluate the model |
---|
| 260 | |
---|
| 261 | : param x: input q-value (float or [float, float] as [qx, qy]) |
---|
[a7107a6] | 262 | : return: I value |
---|
[e096270] | 263 | """ |
---|
| 264 | |
---|
| 265 | return self.model.runXY(x) |
---|
| 266 | |
---|
| 267 | ## Now (May27,10) directly uses the model eval function |
---|
| 268 | ## instead of the for-loop in Base Component. |
---|
| 269 | def evalDistribution(self, x = []): |
---|
| 270 | """ |
---|
| 271 | Evaluate the model in cartesian coordinates |
---|
| 272 | |
---|
| 273 | : param x: input q[], or [qx[], qy[]] |
---|
| 274 | : return: scattering function P(q[]) |
---|
| 275 | """ |
---|
| 276 | # set effective radius and scaling factor before run |
---|
| 277 | return self.model.evalDistribution(x) |
---|
| 278 | |
---|
| 279 | def set_dispersion(self, parameter, dispersion): |
---|
| 280 | """ |
---|
| 281 | Set the dispersion object for a model parameter |
---|
| 282 | |
---|
| 283 | : param parameter: name of the parameter [string] |
---|
| 284 | :dispersion: dispersion object of type DispersionModel |
---|
| 285 | """ |
---|
| 286 | value= None |
---|
| 287 | try: |
---|
| 288 | if parameter in self.model.dispersion.keys(): |
---|
| 289 | value= self.model.set_dispersion(parameter, dispersion) |
---|
| 290 | self._set_dispersion() |
---|
| 291 | return value |
---|
| 292 | except: |
---|
| 293 | raise |
---|