[e1a46d] | 1 | #!@PYTHON@
|
---|
| 2 | #
|
---|
| 3 | # This code takes the tesselation in tecplot TRIANGLE format and calculates the surface
|
---|
| 4 |
|
---|
| 5 | import sys, math
|
---|
| 6 | wrerr = sys.stderr.write
|
---|
| 7 | wrout = sys.stdout.write
|
---|
| 8 |
|
---|
| 9 | if (len(sys.argv) < 2):
|
---|
| 10 | print "Usage: "+sys.argv[0]+" <dat>"
|
---|
| 11 | sys.exit(1)
|
---|
| 12 |
|
---|
| 13 | points=[]
|
---|
| 14 | def distance( id1, id2 ):
|
---|
| 15 | result = 0
|
---|
| 16 | for i in range(3):
|
---|
| 17 | tmp = points[id1][1+i]-points[id2][1+i]
|
---|
| 18 | result += tmp*tmp
|
---|
| 19 | return math.sqrt(result)
|
---|
| 20 |
|
---|
| 21 | # parse the tecplot dat file (the first three lines are tecplot INFO)
|
---|
| 22 | datfile=open(sys.argv[1], "r")
|
---|
| 23 | line=datfile.readline()
|
---|
| 24 | line=datfile.readline()
|
---|
| 25 | line=datfile.readline()
|
---|
| 26 | counter=0
|
---|
| 27 | for line in datfile:
|
---|
| 28 | if not line.strip(): # end on first empty line, indicating end of points and beginning of triangles
|
---|
| 29 | break
|
---|
| 30 | else:
|
---|
| 31 | fields=line.split()
|
---|
| 32 | points.append([counter, float(fields[0]), float(fields[1]), float(fields[2])])
|
---|
| 33 | counter=counter+1
|
---|
| 34 | print "There are %d endpoints of triangles." % (counter)
|
---|
| 35 |
|
---|
| 36 | counter=0
|
---|
| 37 | triangles=[]
|
---|
| 38 | for line in datfile:
|
---|
| 39 | fields=line.split()
|
---|
| 40 | triangles.append([counter, int(fields[0])-1, int(fields[1])-1, int(fields[2])-1])
|
---|
| 41 | counter=counter+1
|
---|
| 42 | print "There are %d triangles." % (counter)
|
---|
| 43 |
|
---|
| 44 | # calculate surface (using Heron's formula)
|
---|
| 45 | surface=0
|
---|
| 46 | for delta in triangles:
|
---|
| 47 | if delta[1] < len(points) and delta[2] < len(points) and delta[3] < len(points):
|
---|
| 48 | a = distance(delta[1], delta[2])
|
---|
| 49 | b = distance(delta[2], delta[3])
|
---|
| 50 | c = distance(delta[3], delta[1])
|
---|
| 51 | s = (a+b+c)/2.
|
---|
| 52 | print "Triangle %d has sides (%g, %g, %g) and s = %g." % (delta[0], a, b, c, s)
|
---|
| 53 | surface += math.sqrt(s*(s-a)*(s-b)*(s-c))
|
---|
| 54 | else:
|
---|
| 55 | print "Triangle %d consists of points (%d, %d, %d) of which one is unknown." % (delta[0], delta[1], delta[2], delta[3])
|
---|
| 56 | print "The calculated surface is %g angstrom^3." % (surface)
|
---|