Changeset 499fe7a in sasview for sansmodels
- Timestamp:
- Apr 18, 2012 11:51:32 AM (13 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 2dda7ae1
- Parents:
- 279e371
- Location:
- sansmodels/src/sans/models
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
sansmodels/src/sans/models/PorodModel.py
r411d8bf r499fe7a 1 #!/usr/bin/env python2 1 """ 3 2 Provide I(q) = C/q^4, 4 3 Porod function as a BaseComponent model 5 4 """ 6 7 5 from sans.models.BaseComponent import BaseComponent 8 6 import math 9 7 10 8 class PorodModel(BaseComponent): 11 """ Class that evaluates a Porod model. 12 13 I(q) = scale/q^4 +background 14 9 """ 10 Class that evaluates a Porod model. 11 I(q) = scale/q^4 +background 15 12 """ 16 13 … … 28 25 self.params['scale'] = 1.0 29 26 self.params['background'] = 0.0 30 self.description = """The Porod model.27 self.description = """The Porod model. 31 28 I(q) = scale/q^4 +background""" 32 29 … … 39 36 40 37 def _porod(self, x): 38 """ 39 Evaluate Porod function 40 :param x: q-value 41 """ 41 42 return self.params['scale']/x**4.0 + self.params['background'] 42 43 … … 49 50 return self._porod(x[0]) 50 51 elif x.__class__.__name__ == 'tuple': 51 raise ValueError, "Tuples are not allowed as input to BaseComponentmodels"52 raise ValueError, "Tuples are not allowed as input to models" 52 53 else: 53 54 return self._porod(x) … … 62 63 return self._porod(q) 63 64 elif x.__class__.__name__ == 'tuple': 64 raise ValueError, "Tuples are not allowed as input to BaseComponentmodels"65 raise ValueError, "Tuples are not allowed as input to models" 65 66 else: 66 67 return self._porod(x) -
sansmodels/src/sans/models/PowerLawAbsModel.py
r9ce41c6 r499fe7a 1 #!/usr/bin/env python2 1 """ 3 2 Provide F(x) = scale* (|x|)^(-m) + bkd 4 3 Power law function as a BaseComponent model 5 4 """ 6 7 5 from sans.models.PowerLawModel import PowerLawModel 8 6 import math 7 9 8 class PowerLawAbsModel(PowerLawModel): 10 9 """ … … 25 24 ## Name of the model 26 25 self.name = "Absolute Power_Law" 27 self.description =""" The Power_Law model.26 self.description = """ The Power_Law model. 28 27 F(x) = scale* (|x|)^(-m) + bkd 29 28 -
sansmodels/src/sans/models/PowerLawModel.py
r9ce41c6 r499fe7a 1 #!/usr/bin/env python2 1 """ 3 2 Provide F(x) = scale* (x)^(-m) + bkd 4 3 Power law function as a BaseComponent model 5 4 """ 6 7 5 from sans.models.BaseComponent import BaseComponent 8 6 import math 9 7 10 8 class PowerLawModel(BaseComponent): 11 12 9 """ 13 10 Class that evaluates a Power_Law model. … … 35 32 self.params['scale'] = 1.0 36 33 self.params['background'] = 0.0 37 self.description =""" The Power_Law model.34 self.description = """ The Power_Law model. 38 35 F(x) = scale* (x)^(-m) + bkd 39 36 … … 48 45 self.details['background'] = ['[1/cm]', None, None] 49 46 #list of parameter that cannot be fitted 50 self.fixed = []47 self.fixed = [] 51 48 def _PowerLaw(self, x): 52 49 """ 53 50 Evaluate F(x) = scale* (x)^(-m) + bkd 54 51 :param x: q-value 55 52 """ 56 #if x!=0 and self.params['m']!=0: 57 # raise ValueError, "negative number cannot be raised to a fractional power" 58 if self.params['m']>0 and x==0: 53 if self.params['m'] > 0 and x == 0: 59 54 return 1e+32 60 elif self.params['m'] ==0 and x==0:55 elif self.params['m'] == 0 and x == 0: 61 56 return 1 62 57 else: 63 return self.params['scale']*math.pow(x ,-1.0*self.params['m'])\58 return self.params['scale']*math.pow(x, -1.0*self.params['m'])\ 64 59 + self.params['background'] 65 60 66 61 def run(self, x = 0.0): 67 62 """ Evaluate the model 68 @param x: input q-value (float or [float, float] as [r, theta])69 @return: (PowerLaw value)63 :param x: input q-value (float or [float, float] as [r, theta]) 64 :return: (PowerLaw value) 70 65 """ 71 66 if x.__class__.__name__ == 'list': … … 76 71 return self._PowerLaw(math.fabs(x[0])) 77 72 elif x.__class__.__name__ == 'tuple': 78 raise ValueError, "Tuples are not allowed as input to BaseComponentmodels"73 raise ValueError, "Tuples are not allowed as input to models" 79 74 else: 80 75 return self._PowerLaw(x) … … 89 84 return self._PowerLaw(q) 90 85 elif x.__class__.__name__ == 'tuple': 91 raise ValueError, "Tuples are not allowed as input to BaseComponentmodels"86 raise ValueError, "Tuples are not allowed as input to models" 92 87 else: 93 88 return self._PowerLaw(x) -
sansmodels/src/sans/models/ReflectivityIIModel.py
r1352c78 r499fe7a 5 5 from math import floor 6 6 from math import fabs 7 from scipy.special import erf8 7 func_list = {'Erf(|nu|*z)':0, 'RPower(z^|nu|)':1, 'LPower(z^|nu|)':2, \ 9 8 'RExp(-|nu|*z)':3, 'LExp(-|nu|*z)':4} … … 15 14 """ 16 15 def __init__(self, multfactor=1): 16 """ 17 :param multfactor: number of layers in the model, 18 assumes 0<= n_layers <=10. 19 """ 17 20 BaseComponent.__init__(self) 18 """19 :param multfactor: number of layers in the model,20 assumes 0<= n_layers <=10.21 """22 21 23 22 ## Setting model name model description 24 self.description =""23 self.description = "" 25 24 model = ReflAdvModel() 26 25 self.model = model 27 26 self.name = "ReflectivityIIModel" 28 self.description =model.description27 self.description = model.description 29 28 self.n_layers = int(multfactor) 30 29 ## Define parameters … … 54 53 # [int(maximum no. of functionality),"str(Titl), 55 54 # [str(name of function0),...], [str(x-asix name of sld),...]] 56 self.multiplicity_info = [max_nshells, "No. of Layers:",[],['Depth']]55 self.multiplicity_info = [max_nshells, "No. of Layers:", [], ['Depth']] 57 56 ## independent parameter name and unit [string] 58 57 self.input_name = "Q" … … 92 91 """ 93 92 # rearrange the parameters for the given # of shells 94 for name 93 for name, value in self.model.params.iteritems(): 95 94 n = 0 96 95 pos = len(name.split('_'))-1 … … 103 102 continue 104 103 elif first_name == 'func': 105 n = -1106 while n <self.n_layers:104 n = -1 105 while n < self.n_layers: 107 106 n += 1 108 107 if last_name == 'inter%s' % str(n): 109 self.params[name] =value108 self.params[name] = value 110 109 continue 111 110 112 111 #continue 113 112 elif last_name[0:5] == 'inter': 114 n = -1115 while n <self.n_layers:113 n = -1 114 while n < self.n_layers: 116 115 n += 1 117 116 if last_name == 'inter%s' % str(n): 118 self.params[name] = value117 self.params[name] = value 119 118 continue 120 119 elif last_name[0:4] == 'flat': 121 while n <self.n_layers:120 while n < self.n_layers: 122 121 n += 1 123 122 if last_name == 'flat%s' % str(n): 124 self.params[name] = value123 self.params[name] = value 125 124 continue 126 125 elif name == 'n_layers': 127 126 continue 128 127 else: 129 self.params[name] = value128 self.params[name] = value 130 129 131 130 self.model.params['n_layers'] = self.n_layers … … 139 138 this model details 140 139 """ 141 for name ,detail in self.model.details.iteritems():140 for name, detail in self.model.details.iteritems(): 142 141 if name in self.params.iterkeys(): 143 self.details[name] = detail142 self.details[name] = detail 144 143 145 144 … … 155 154 continue 156 155 if key.split('_')[0] == 'func': 157 158 156 self.model.setParam(key, 0) 157 continue 159 158 160 159 for nshell in range(self.n_layers,max_nshells): … … 166 165 value = self.model.params['sldIM_medium'] 167 166 self.model.setParam(key, value) 168 except: pass 167 except: 168 message = "ReflectivityIIModel evaluation problem" 169 raise RuntimeError, message 169 170 170 171 def _get_func_list(self): 171 172 """ 172 Get the list of functions in each layer (shell) 173 """ 174 #func_list = {} 173 Get the list of functions in each layer (shell) 174 """ 175 175 return func_list 176 176 … … 186 186 z = [] 187 187 beta = [] 188 sub_range = int(floor(n_sub/2.0))189 188 z.append(0) 190 189 beta.append(self.params['sld_bottom0']) … … 193 192 dz = 0.0 194 193 # for layers from the top 195 for n_lyr in range(1, self.n_layers+2):194 for n_lyr in range(1, self.n_layers+2): 196 195 i = n_lyr 197 196 # j=0 for interface, j=1 for flat layer 198 for j in range(0, 2):197 for j in range(0, 2): 199 198 # interation for sub-layers 200 for n_s in range(0, n_sub):199 for n_s in range(0, n_sub): 201 200 # for flat layer 202 if j ==1:203 if i ==self.n_layers+1:201 if j == 1: 202 if i == self.n_layers+1: 204 203 break 205 204 # shift half sub thickness for the first point 206 205 z0 -= dz/2.0 207 206 z.append(z0) 208 sld_i = self.params['sld_flat%s' % str(i)]207 sld_i = self.params['sld_flat%s' % str(i)] 209 208 beta.append(sld_i) 210 dz = self.params['thick_flat%s' % str(i)]209 dz = self.params['thick_flat%s' % str(i)] 211 210 z0 += dz 212 211 else: 213 dz = self.params['thick_inter%s' % str(i-1)]/n_sub214 nu = fabs(self.params['nu_inter%s' % str(i-1)])212 dz = self.params['thick_inter%s' % str(i-1)]/n_sub 213 nu = fabs(self.params['nu_inter%s' % str(i-1)]) 215 214 if n_s == 0: 216 215 # shift half sub thickness for the point … … 220 219 sld_l = self.params['sld_bottom0'] 221 220 else: 222 sld_l = self.params['sld_flat%s' % str(i-1)]221 sld_l = self.params['sld_flat%s' % str(i-1)] 223 222 if i == self.n_layers+1: 224 223 sld_r = self.params['sld_medium'] 225 224 else: 226 sld_r = self.params['sld_flat%s' % str(i)]225 sld_r = self.params['sld_flat%s' % str(i)] 227 226 if sld_r == sld_l: 228 227 sld_i = sld_r 229 228 else: 230 func_idx = self.params['func_inter%s' % str(i-1)]229 func_idx = self.params['func_inter%s' % str(i-1)] 231 230 # calculate the sld 232 231 sld_i = self._get_sld(func_idx, n_sub, n_s+0.5, nu, … … 235 234 z.append(z0) 236 235 beta.append(sld_i) 237 if j==1: break 238 else: z0 += dz 236 if j == 1: 237 break 238 else: 239 z0 += dz 239 240 # put substrate and superstrate profile 240 241 z.append(z0) … … 246 247 z.append(z0+z_ext) 247 248 beta.append(self.params['sld_medium']) 248 z.insert(0, -z_ext)249 beta.insert(0, self.params['sld_bottom0'])249 z.insert(0, -z_ext) 250 beta.insert(0, self.params['sld_bottom0']) 250 251 # rearrange the profile for NR sld profile style 251 252 z = [z0 - x for x in z] … … 269 270 sld_cal = SLDCalFunc() 270 271 # set params 271 sld_cal.setParam('fun_type', func_idx)272 sld_cal.setParam('npts_inter', n_sub)273 sld_cal.setParam('shell_num', n_s)274 sld_cal.setParam('nu_inter', nu)275 sld_cal.setParam('sld_left', sld_l)276 sld_cal.setParam('sld_right', sld_r)272 sld_cal.setParam('fun_type', func_idx) 273 sld_cal.setParam('npts_inter', n_sub) 274 sld_cal.setParam('shell_num', n_s) 275 sld_cal.setParam('nu_inter', nu) 276 sld_cal.setParam('sld_left', sld_l) 277 sld_cal.setParam('sld_right', sld_r) 277 278 # return sld value 278 279 return sld_cal.run() … … 289 290 290 291 ## setParam to model 291 if name =='sld_medium':292 if name == 'sld_medium': 292 293 # the sld_*** model.params not in params must set 293 294 # to value of sld_solv 294 295 for key in self.model.params.iterkeys(): 295 296 if key not in self.params.keys()and key.split('_')[0] == 'sld': 296 297 self.model.setParam(key, value) 297 298 298 299 self.model.setParam( name, value) … … 342 343 ## Now (May27,10) directly uses the model eval function 343 344 ## instead of the for-loop in Base Component. 344 def evalDistribution(self, x = []):345 def evalDistribution(self, x): 345 346 """ 346 347 Evaluate the model in cartesian coordinates -
sansmodels/src/sans/models/ReflectivityModel.py
r1352c78 r499fe7a 7 7 func_list = {'Erf':0, 'Linear':1} 8 8 max_nshells = 10 9 9 10 class ReflectivityModel(BaseComponent): 10 11 """ … … 13 14 """ 14 15 def __init__(self, multfactor=1): 16 """ 17 :param multfactor: number of layers in the model, 18 assumes 0<= n_shells <=10. 19 """ 15 20 BaseComponent.__init__(self) 16 """17 :param multfactor: number of layers in the model,18 assumes 0<= n_shells <=10.19 """20 21 21 22 ## Setting model name model description 22 self.description =""23 self.description = "" 23 24 model = ReflModel() 24 25 self.model = model 25 26 self.name = "ReflectivityModel" 26 self.description =model.description27 self.description = model.description 27 28 self.n_layers = int(multfactor) 28 29 ## Define parameters … … 52 53 # [int(maximum no. of functionality),"str(Titl), 53 54 # [str(name of function0),...], [str(x-asix name of sld),...]] 54 self.multiplicity_info = [max_nshells, "No. of Layers:",[],['Depth']]55 self.multiplicity_info = [max_nshells, "No. of Layers:", [], ['Depth']] 55 56 ## independent parameter name and unit [string] 56 57 self.input_name = "Q" … … 95 96 continue 96 97 elif name.split('_')[0] == 'func': 97 n = -198 while n <self.n_layers:98 n = -1 99 while n < self.n_layers: 99 100 n += 1 100 101 if name.split('_')[pos] == 'inter%s' % str(n): 101 self.params[name] =value102 self.params[name] = value 102 103 continue 103 104 #continue 104 105 elif name.split('_')[pos][0:5] == 'inter': 105 n = -1106 while n <self.n_layers:106 n = -1 107 while n < self.n_layers: 107 108 n += 1 108 109 if name.split('_')[pos] == 'inter%s' % str(n): 109 self.params[name] = value110 self.params[name] = value 110 111 continue 111 112 elif name.split('_')[pos][0:4] == 'flat': 112 while n <self.n_layers:113 while n < self.n_layers: 113 114 n += 1 114 115 if name.split('_')[pos] == 'flat%s' % str(n): 115 self.params[name] = value116 self.params[name] = value 116 117 continue 117 118 elif name == 'n_layers': 118 119 continue 119 120 else: 120 self.params[name] = value121 self.params[name] = value 121 122 122 123 self.model.params['n_layers'] = self.n_layers … … 130 131 this model details 131 132 """ 132 for name ,detail in self.model.details.iteritems():133 for name, detail in self.model.details.iteritems(): 133 134 if name in self.params.iterkeys(): 134 self.details[name] = detail135 self.details[name] = detail 135 136 136 137 … … 156 157 value = self.model.params['sldIM_medium'] 157 158 self.model.setParam(key, value) 158 except: pass 159 except: 160 raise RuntimeError, "ReflectivityModel problem" 159 161 160 162 def _get_func_list(self): … … 182 184 z0 = 0 183 185 # for layers from the top 184 for n in range(1, self.n_layers+2):186 for n in range(1, self.n_layers+2): 185 187 i = n 186 188 187 for j in range(0,2): 188 for n_s in range(-sub_range,sub_range+1): 189 if j==1: 190 if i==self.n_layers+1: 189 for j in range(0, 2): 190 for n_s in range(-sub_range, sub_range+1): 191 dz = self.params['thick_inter%s' % str(i-1)]/n_sub 192 if j == 1: 193 if i == self.n_layers+1: 191 194 break 192 195 # shift half sub thickness for the first point … … 194 197 z.append(z0) 195 198 #z0 -= dz/2.0 196 z0 += self.params['thick_flat%s' % str(i)]199 z0 += self.params['thick_flat%s' % str(i)] 197 200 198 sld_i = self.params['sld_flat%s' % str(i)]199 beta.append(self.params['sld_flat%s' % str(i)])201 sld_i = self.params['sld_flat%s' % str(i)] 202 beta.append(self.params['sld_flat%s' % str(i)]) 200 203 else: 201 202 dz = self.params['thick_inter%s'% str(i-1)]/n_sub203 204 204 if n_s == -sub_range: 205 205 # shift half sub thickness for the first point … … 212 212 sld_l = self.params['sld_bottom0'] 213 213 else: 214 sld_l = self.params['sld_flat%s' % str(i-1)]214 sld_l = self.params['sld_flat%s' % str(i-1)] 215 215 if i == self.n_layers+1: 216 216 sld_r = self.params['sld_medium'] 217 217 else: 218 sld_r = self.params['sld_flat%s' % str(i)]219 func_idx = self.params['func_inter%s' % str(i-1)]218 sld_r = self.params['sld_flat%s' % str(i)] 219 func_idx = self.params['func_inter%s' % str(i-1)] 220 220 func = self._get_func(n_s, n_sub, func_idx) 221 if sld_r >sld_l:221 if sld_r > sld_l: 222 222 sld_i = (sld_r-sld_l)*func+sld_l 223 elif sld_r <sld_l:223 elif sld_r < sld_l: 224 224 sld_i = (sld_l-sld_r)*(1-func)+sld_r 225 225 else: 226 226 sld_i = sld_r 227 227 z.append(z0) 228 228 beta.append(sld_i) 229 if j==1: break 229 if j == 1: 230 break 230 231 # put substrate and superstrate profile 231 232 # shift half sub thickness for the first point … … 239 240 z.append(z0+z_ext) 240 241 beta.append(self.params['sld_medium']) 241 z.insert(0, -z_ext)242 beta.insert(0, self.params['sld_bottom0'])242 z.insert(0, -z_ext) 243 beta.insert(0, self.params['sld_bottom0']) 243 244 z = [z0 - x for x in z] 244 245 z.reverse() … … 298 299 299 300 ## setParam to model 300 if name =='sld_medium':301 if name == 'sld_medium': 301 302 # the sld_*** model.params not in params must set 302 303 # to value of sld_solv 303 304 for key in self.model.params.iterkeys(): 304 305 if key not in self.params.keys()and key.split('_')[0] == 'sld': 305 306 self.model.setParam(key, value) 306 307 307 308 self.model.setParam( name, value) … … 351 352 ## Now (May27,10) directly uses the model eval function 352 353 ## instead of the for-loop in Base Component. 353 def evalDistribution(self, x = []):354 def evalDistribution(self, x): 354 355 """ 355 356 Evaluate the model in cartesian coordinates … … 360 361 # set effective radius and scaling factor before run 361 362 return self.model.evalDistribution(x) 363 362 364 def calculate_ER(self): 363 365 """ 364 366 """ 365 367 return self.model.calculate_ER() 368 366 369 def set_dispersion(self, parameter, dispersion): 367 370 """
Note: See TracChangeset
for help on using the changeset viewer.