Changeset 8375 for trunk/anuga_work/development/gareth/tests/util.py
- Timestamp:
- Mar 31, 2012, 2:47:12 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/anuga_work/development/gareth/tests/util.py
r8353 r8375 73 73 ############## 74 74 75 def animate_1D(time, var, x, ylab=' '): #, x=range(var.shape[1]), vmin=var.min(), vmax=var.max()):76 # Input: time = one-dimensional time vector;77 # var = array with first dimension = len(time) ;78 # x = (optional) vector width dimension equal to var.shape[1];79 80 import pylab81 import numpy82 83 84 85 pylab.close()86 pylab.ion()87 88 # Initial plot89 vmin=var.min()90 vmax=var.max()91 line, = pylab.plot( (x.min(), x.max()), (vmin, vmax), 'o')92 93 # Lots of plots94 for i in range(len(time)):95 line.set_xdata(x)96 line.set_ydata(var[i,:])97 pylab.draw()98 pylab.xlabel('x')99 pylab.ylabel(ylab)100 pylab.title('time = ' + str(time[i]))101 102 return103 75 104 76 … … 198 170 #pylab.quiver(x,y,xvel[n,:],yvel[n,:]) 199 171 #pylab.savefig('Plot1.png') 172 173 def animate_1D(time, var, x, ylab=' '): #, x=range(var.shape[1]), vmin=var.min(), vmax=var.max()): 174 # Input: time = one-dimensional time vector; 175 # var = array with first dimension = len(time) ; 176 # x = (optional) vector width dimension equal to var.shape[1]; 177 178 import pylab 179 import numpy 180 181 182 183 pylab.close() 184 pylab.ion() 185 186 # Initial plot 187 vmin=var.min() 188 vmax=var.max() 189 line, = pylab.plot( (x.min(), x.max()), (vmin, vmax), 'o') 190 191 # Lots of plots 192 for i in range(len(time)): 193 line.set_xdata(x) 194 line.set_ydata(var[i,:]) 195 pylab.draw() 196 pylab.xlabel('x') 197 pylab.ylabel(ylab) 198 pylab.title('time = ' + str(time[i])) 199 200 return 201 202 def near_transect(p, point1, point2, tol=1.): 203 # Function to get the indices of points in p less than 'tol' from the line 204 # joining (x1,y1), and (x2,y2) 205 # p comes from util.get_output('mysww.sww') 206 # 207 # e.g. 208 # import util 209 # #import transect_interpolate 210 # from matplotlib import pyplot 211 # p=util.get_output('merewether_1m.sww',0.01) 212 # p2=util.get_centroids(p,velocity_extrapolation=True) 213 # #xxx=transect_interpolate.near_transect(p,[95., 85.], [120.,68.],tol=2.) 214 # xxx=util.near_transect(p,[95., 85.], [120.,68.],tol=2.) 215 # pyplot.scatter(xxx[1],p.vel[140,xxx[0]],color='red') 216 217 x1=point1[0] 218 y1=point1[1] 219 220 x2=point2[0] 221 y2=point2[1] 222 223 # Find line equation a*x + b*y + c = 0 224 # based on y=gradient*x +intercept 225 if x1!=x2: 226 gradient= (y2-y1)/(x2-x1) 227 intercept = y1 - gradient*x1 228 229 a = -gradient 230 b = 1. 231 c = -intercept 232 else: 233 #print 'FIXME: Still need to treat 0 and infinite gradients' 234 #assert 0==1 235 a=1. 236 b=0. 237 c=-x2 238 239 # Distance formula 240 inv_denom = 1./(a**2 + b**2)**0.5 241 distp = abs(p.x*a + p.y*b + c)*inv_denom 242 243 near_points = (distp<tol).nonzero()[0] 244 245 # Now find a 'local' coordinate for the point, projected onto the line 246 # g1 = unit vector parallel to the line 247 # g2 = vector joining (x1,y1) and (p.x,p.y) 248 g1x = x2-x1 249 g1y = y2-y1 250 g1_norm = (g1x**2 + g1y**2)**0.5 251 g1x=g1x/g1_norm 252 g1y=g1x/g1_norm 253 254 g2x = p.x[near_points] - x1 255 g2y = p.y[near_points] - y1 256 257 # Dot product = projected distance == a local coordinate 258 local_coord = g1x*g2x + g1y*g2y 259 260 return near_points, local_coord
Note: See TracChangeset
for help on using the changeset viewer.