-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtawafField.py
82 lines (71 loc) · 3.24 KB
/
tawafField.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# A throw away script designed to instantiate a vector field for the tawaf
from obstacles import readObstacles
from vField import VectorField
import numpy as np
def spiralField( field, radWeight, flip=False ):
'''The tawaf spiral field -- velocities have a tangential component and some
component inwards'''
# this is quick and dirty - it relies on knowing the implementation of
# VectorField
print "spiralField"
centers = field.centers
print "\tcenters.shape:", centers.shape
mag = np.sqrt( np.sum( centers * centers, axis=2 ) )
mag.shape = (mag.shape[0], mag.shape[1], 1)
print "\tmag.shape:", mag.shape
unitDir = centers / mag
tanDir = np.zeros_like( centers )
tanDir[ :, :, 0 ] = -unitDir[ :, :, 1 ]
tanDir[ :, :, 1 ] = unitDir[ :, :, 0 ]
radDir = -unitDir
dir = radWeight * radDir + (1 - radWeight ) * tanDir
# renormalize
mag = np.sqrt( np.sum( dir * dir , axis=2 ) )
mag.shape = (mag.shape[0], mag.shape[1], 1)
dir *= 1.0 / mag
if ( flip ):
field.data = -dir
else:
field.data = dir
def main():
import optparse
import sys
parser = optparse.OptionParser()
parser.add_option( "-o", "--obstacle", help="Obstacle file to load.",
action="store", dest='obstName', default='' )
parser.add_option( "-f", "--field", help="The name of the output file. Default value is tawafField_##.txt, where ** is the radial weight.",
action="store", dest='fieldName', default='' )
parser.add_option( "-r", "--radiusWeight", help="The component of the velocity in each cell that is towards the center. Default is 0.2.)",
action="store", dest='radWeight', default=0.23, type='float' )
parser.add_option( "-c", "--cellSize", help="The size of a grid cell (in meters). Defaults to 2.0",
action="store", dest="cellSize", default=2.0, type='float' )
parser.add_option( "-q", "--square", help="Forces the grid to be square",
action="store_true", dest="square", default=False )
parser.add_option( "-l", "--flip", help="Reverses the direction of the field",
action="store_true", dest="flip", default=False )
options, args = parser.parse_args()
if ( options.obstName == '' ):
parser.print_help()
sys.exit(1)
if ( options.fieldName == '' ):
# construct a default name
fieldName = 'tawafField_{0:.2f}'.format( options.radWeight )
fieldName = fieldName.replace( '.', '_' ) + ".txt"
makeSquare = options.square
print "Creating vector field based on:", options.obstName
obstacles, bb = readObstacles( options.obstName )
print "\tObstacle bounding box:", bb
print "\tVector field cell size:", options.cellSize
bbSize = bb.max - bb.min
if ( makeSquare ):
maxSize = max( bbSize.x, bbSize.y )
bbSize.x = maxSize
bbSize.y = maxSize
field = VectorField( ( bb.min.x, bb.min.y ), ( bbSize.x, bbSize.y ), options.cellSize )
# do work
spiralField( field, options.radWeight, options.flip )
print "\tVector field saving to:", options.fieldName,
field.write( options.fieldName )
print "SAVED!"
if __name__ == '__main__':
main()