source: util/src/CalculateSurfaceOfCluster.py.in@ a3ffb44

Last change on this file since a3ffb44 was e1a46d, checked in by Frederik Heber <heber@…>, 16 years ago

All of my python script put into ESPACK and adapted with @PYTHON@ and so on.

  • Property mode set to 100755
File size: 1.7 KB
Line 
1#!@PYTHON@
2#
3# This code takes the tesselation in tecplot TRIANGLE format and calculates the surface
4
5import sys, math
6wrerr = sys.stderr.write
7wrout = sys.stdout.write
8
9if (len(sys.argv) < 2):
10 print "Usage: "+sys.argv[0]+" <dat>"
11 sys.exit(1)
12
13points=[]
14def 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)
22datfile=open(sys.argv[1], "r")
23line=datfile.readline()
24line=datfile.readline()
25line=datfile.readline()
26counter=0
27for 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
34print "There are %d endpoints of triangles." % (counter)
35
36counter=0
37triangles=[]
38for 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
42print "There are %d triangles." % (counter)
43
44# calculate surface (using Heron's formula)
45surface=0
46for 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])
56print "The calculated surface is %g angstrom^3." % (surface)
Note: See TracBrowser for help on using the repository browser.