Changeset 1221
- Timestamp:
- Apr 12, 2005, 3:43:56 PM (20 years ago)
- Location:
- inundation/ga/storm_surge/pmesh
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pmesh/mesh.py
r1217 r1221 2179 2179 def triangles_to_polySet(self,setName): 2180 2180 seg2line = self.seg2line 2181 ver2point 2181 ver2point= self.ver2point 2182 2182 line2seg = self.line2seg 2183 point2ver 2183 point2ver= self.point2ver 2184 2184 2185 2185 from Numeric import array,allclose … … 2189 2189 for triangle in Triangles: 2190 2190 Triangles_dict[triangle]=None 2191 2191 2192 2192 2193 point_keys = {} … … 2198 2199 userVertices[vertex]=True 2199 2200 2201 ####### 2202 for vert in userVertices.keys(): 2203 assert point_keys[(vert.x,vert.y)]==vert 2204 assert len(point_keys.keys())==len(userVertices.keys()) 2205 ####### 2206 2207 2200 2208 userSegments = self.segs_to_dict(self.userSegments) 2201 2209 affine_lines = Affine_Linespace() … … 2214 2222 a = triangle.vertices[i-1] 2215 2223 b = triangle.vertices[i-2] 2216 for vert in (a,b):2217 point = ver2point(vert)2218 if not point_keys.has_key(point):2219 userVertices[vert]=True2220 point_keys[point]=vert2221 2222 2224 #Get possible matches: 2223 2225 point_a = ver2point(a) … … 2230 2232 for user_line in possible_lines: 2231 2233 if self.point_on_line(midpoint,user_line): 2232 if user _line in userSegments:2234 if userSegments.has_key(user_line): 2233 2235 parent_segment = userSegments.pop(user_line) 2234 2236 else: … … 2257 2259 for point in line: 2258 2260 if point_keys.has_key(point): 2259 2261 vert = point_keys[point] 2260 2262 else: 2261 2263 vert = Vertex(point[0],point[1]) … … 2267 2269 affine_lines.append(line) 2268 2270 2271 ####### 2272 for vert in userVertices.keys(): 2273 assert point_keys[(vert.x,vert.y)]==vert 2274 assert len(point_keys.keys())==len(userVertices.keys()) 2275 ####### 2269 2276 self.userVerticies = [] 2270 2277 self.userSegments = [] … … 2277 2284 #FIXME change alpha to user and sync with pmesh 2278 2285 #ie self.alphaSegments.append(alphaSegments[key]) 2279 2286 #print point_keys.keys() 2287 #print userVertices.keys() 2288 #print userVertices.keys() 2280 2289 return userVertices,userSegments,alphaSegments 2281 2290 … … 2343 2352 y0=line[0][1] 2344 2353 y1=line[1][1] 2345 return point_on_line(x,y,x0,y0,x1,y1) 2346 #a wrapper for the c func. 2354 from Numeric import array, dot, allclose 2355 from math import sqrt 2356 2357 a = array([x - x0, y - y0]) 2358 a_normal = array([a[1], -a[0]]) 2359 2360 b = array([x1 - x0, y1 - y0]) 2361 2362 if allclose(dot(a_normal, b),0): 2363 #Point is somewhere on the infinite extension of the line 2364 2365 len_a = sqrt(sum(a**2)) 2366 len_b = sqrt(sum(b**2)) 2367 if dot(a, b) >= 0 and len_a <= len_b: 2368 return True 2369 else: 2370 return False 2371 else: 2372 return False 2347 2373 2348 2374 def line_length(self,line): … … 3122 3148 #NOT IMPLIMENTED 3123 3149 at a.precision = 2: 3124 a.round_up [0.0]=3150 a.round_up_rel[0.0]= 3125 3151 a.round_flat[0.0]= 3126 a.round_down p0.0]=3152 a.round_down_rel[0.0]= 3127 3153 3128 3154 a.up((0.1,2.04))= 3129 3155 3130 If t olerance= 0, nothing gets rounded into3131 two bins. If t olerance= 0.5, everything does.3156 If t_rel = 0, nothing gets rounded into 3157 two bins. If t_rel = 0.5, everything does. 3132 3158 3133 3159 Ideally, precision can be set high, so that multiple 3134 entries are rarely in the same bin. And t oleranceshould3160 entries are rarely in the same bin. And t_rel should 3135 3161 be low (<0.1 for 1 dimension!,<(0.1/n) for small n!!) 3136 3162 so that it is rare to put items in mutiple bins. 3137 3163 3138 3164 Ex bins per entry = product(a,b,c...,n) 3139 a = 1 or 2 s.t. Ex(a) = 1+2*t olerance3165 a = 1 or 2 s.t. Ex(a) = 1+2*t_rel 3140 3166 b = 1 or 2 ... 3141 3167 3142 3168 BUT!!! to avoid missing the right bin: 3143 (-10)**(precision+1)*t olerancemust be greater than the3169 (-10)**(precision+1)*t_rel must be greater than the 3144 3170 greatest possible variation that an identical element 3145 3171 can display. 3146 """ 3147 def __init__(self,precision = 6,tolerance = 0.01): 3148 self.__precision__ = precision 3149 self.__tolerance__ = tolerance 3150 assert tolerance <= 0.5 3172 3173 3174 Note that if tol = 0.5 (the max allowed) 0.6 will round to .7 and .5 3175 but not .6 - this looks wrong, but note that *everything* will round, 3176 so .6 wont be missed as everything close to it will check in .7 and .5. 3177 """ 3178 def __init__(self,p_rel = 6,t_rel = 0.01): 3179 self.__p_rel__ = p_rel 3180 self.__t_rel__ = t_rel 3181 3182 self.__p_abs__ = p_rel+1 3183 self.__t_abs__ = t_rel 3184 3185 assert t_rel <= 0.5 3151 3186 self.__items__ = {} 3152 3187 from math import frexp … … 3164 3199 def __rounded_keys__(self,key): 3165 3200 keys = [] 3166 up_key=list(key) 3167 for i in range(len(up_key)): 3168 up_key[i]=self.round_up(key[i]) 3169 keys.append(tuple(up_key)) 3170 3171 #round down the value 3172 for i in range(len(up_key)): 3173 if not keys[0][i]==self.round_down(key[i]): 3174 #unless round_up = round_down 3175 #so the keys stay unique 3176 for j in range(len(keys)): 3177 down_key = list(keys[j]) 3178 down_key[i]=self.round_down(key[i]) 3179 down_key=tuple(down_key) 3180 keys.append(down_key) 3201 rounded_key=list(key) 3202 rounded_values=list(key) 3203 3204 roundings = [self.round_up_rel,\ 3205 self.round_down_rel,self.round_flat_rel,\ 3206 self.round_down_abs,self.round_up_abs,\ 3207 self.round_flat_abs] 3208 3209 3210 roundings = \ 3211 [self.round_up_rel, self.round_down_rel, \ 3212 self.round_up_rel2,self.round_down_rel2,\ 3213 self.round_up_abs, self.round_down_abs] 3214 3215 #initialise rounded_values 3216 round = roundings.pop(0) 3217 for i in range(len(rounded_values)): 3218 rounded_key[i]=round(key[i]) 3219 rounded_values[i]={} 3220 rounded_values[i][rounded_key[i]]=None 3221 keys.append(tuple(rounded_key)) 3222 3223 for round in roundings: 3224 for i in range(len(rounded_key)): 3225 rounded_value=round(key[i]) 3226 if not rounded_values[i].has_key(rounded_value): 3227 #ie unless round_up_rel = round_down_rel 3228 #so the keys stay unique 3229 for j in range(len(keys)): 3230 rounded_key = list(keys[j]) 3231 rounded_key[i]=rounded_value 3232 keys.append(tuple(rounded_key)) 3181 3233 return keys 3182 3234 … … 3230 3282 return self.__contains__(item) 3231 3283 3232 def round_up(self,value): 3233 tolerance=self.__tolerance__ 3284 def round_up_rel2(self,value): 3285 t_rel=self.__t_rel__ 3286 #Rounding up the value 3287 m,e = self.frexp(value) 3288 m = m/2 3289 e = e + 1 3290 #m is the mantissa, e the exponent 3291 # 0.5 < |m| < 1.0 3292 m = m+t_rel*(10**-(self.__p_rel__)) 3293 #bump m up 3294 m = round(m,self.__p_rel__) 3295 return m*(2.**e) 3296 3297 def round_down_rel2(self,value): 3298 t_rel=self.__t_rel__ 3299 #Rounding down the value 3300 m,e = self.frexp(value) 3301 m = m/2 3302 e = e + 1 3303 #m is the mantissa, e the exponent 3304 # 0.5 < m < 1.0 3305 m = m-t_rel*(10**-(self.__p_rel__)) 3306 #bump the |m| down, by 5% or whatever 3307 #self.p_rel dictates 3308 m = round(m,self.__p_rel__) 3309 return m*(2.**e) 3310 3311 def round_flat_rel2(self,value): 3312 #redundant 3313 m,e = self.frexp(value) 3314 m = m/2 3315 e = e + 1 3316 m = round(m,self.__p_rel__) 3317 return m*(2.**e) 3318 3319 def round_up_rel(self,value): 3320 t_rel=self.__t_rel__ 3234 3321 #Rounding up the value 3235 3322 m,e = self.frexp(value) 3236 3323 #m is the mantissa, e the exponent 3237 3324 # 0.5 < |m| < 1.0 3238 m = m*(1+tolerance*(10**-(self.__precision__+1))/((m**2)**0.5)) 3239 #bump m up, by 5% or whatever 3240 #self.precision dictates 3241 m = round(m,self.__precision__) 3242 #round m off so that the bump up 3243 #will be discernable if and only if 3325 m = m+t_rel*(10**-(self.__p_rel__)) 3326 #bump m up 3327 m = round(m,self.__p_rel__) 3244 3328 return m*(2.**e) 3245 3329 3246 def round_down (self,value):3247 t olerance=self.__tolerance__3330 def round_down_rel(self,value): 3331 t_rel=self.__t_rel__ 3248 3332 #Rounding down the value 3249 3333 m,e = self.frexp(value) 3250 3334 #m is the mantissa, e the exponent 3251 3335 # 0.5 < m < 1.0 3252 m = m *(1-tolerance*(10**-(self.__precision__+1))/((m**2)**0.5))3336 m = m-t_rel*(10**-(self.__p_rel__)) 3253 3337 #bump the |m| down, by 5% or whatever 3254 #self.precision dictates 3255 m = round(m,self.__precision__) 3256 #round m off so that the bump down 3257 #will be discernable if and only if 3338 #self.p_rel dictates 3339 m = round(m,self.__p_rel__) 3258 3340 return m*(2.**e) 3259 3341 3260 def round_flat (self,value):3342 def round_flat_rel(self,value): 3261 3343 #redundant 3262 3344 m,e = self.frexp(value) 3263 m = round(m,self.__p recision__)3345 m = round(m,self.__p_rel__) 3264 3346 return m*(2.**e) 3347 3348 def round_up_abs(self,value): 3349 t_abs=self.__t_abs__ 3350 #Rounding up the value 3351 m = value+t_abs*(10**-(self.__p_abs__)) 3352 #bump m up 3353 m = round(m,self.__p_abs__) 3354 return m 3355 3356 def round_down_abs(self,value): 3357 t_abs=self.__t_abs__ 3358 #Rounding down the value 3359 m = value-t_abs*(10**-(self.__p_abs__)) 3360 #bump the |m| down, by 5% or whatever 3361 #self.p_rel dictates 3362 m = round(m,self.__p_abs__) 3363 return m 3364 3365 def round_flat_abs(self,value): 3366 #redundant? 3367 m = round(value,self.__p_abs__) 3368 return m 3265 3369 3266 3370 def keys(self): … … 3289 3393 a[elephant] -> [] 3290 3394 """ 3291 def __init__(self,mapping,p recision = 6, tolerance=0.01):3395 def __init__(self,mapping,p_rel = 6, t_rel=0.01): 3292 3396 Discretised_Tuple_Set.__init__\ 3293 (self,p recision,tolerance = tolerance)3397 (self,p_rel,t_rel = t_rel) 3294 3398 self.mapping = mapping 3295 3399 … … 3308 3412 Nearly. 3309 3413 """ 3310 def __init__(self,p recision = 4,tolerance=0.04):3414 def __init__(self,p_rel = 3,t_rel=0.1): 3311 3415 Mapped_Discretised_Tuple_Set.__init__\ 3312 3416 (self,self.affine_line,\ 3313 p recision=precision,tolerance=tolerance)3417 p_rel=p_rel,t_rel=t_rel) 3314 3418 3315 3419 def affine_line(self,line): … … 3357 3461 else: 3358 3462 sign_a = a/((a**2)**0.5) 3463 #This does not change the mathematical 3464 #properties, but it makes comparism 3359 3465 if b == 0: 3360 3466 sign_b = 1. … … 3370 3476 return a,b,c 3371 3477 3478 3372 3479 3373 3480 if __name__ == "__main__": -
inundation/ga/storm_surge/pmesh/pmesh.py
r1217 r1221 354 354 print 'len(alphaSegments.keys())' 355 355 print len(alphaSegments.keys()) 356 357 358 ####### 359 point_keys = {} 360 for vert in userVertices.keys(): 361 assert not point_keys.has_key((vert.x,vert.y)) 362 point_keys[(vert.x,vert.y)]=vert 363 assert len(point_keys.keys())==len(userVertices.keys()) 364 ####### 365 356 366 for v in userVertices.keys(): 357 367 if userVertices[v] is True:
Note: See TracChangeset
for help on using the changeset viewer.