source: util/src/PairCorrelationToCluster.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: 3.2 KB
Line 
1#!@PYTHON@
2#
3# This code calculates the pair correlation between the water molecules and the center of gravity of the cluster (defined by the Si and Ca atoms)
4
5import sys, string, re, math
6wrerr = sys.stderr.write
7wrout = sys.stdout.write
8
9if (len(sys.argv) < 2):
10 print "Usage: "+sys.argv[0]+" <pdb> <dbond>"
11 sys.exit(1)
12
13# parse the pdb file and obtain maxid
14print "Parsing the PDB file "+sys.argv[1]+"."
15pdbfile = open(sys.argv[1], "r")
16atoms = {}
17maxid=-1
18for line in pdbfile:
19 if "END" in line or "CONECT" in line or "MASTER" in line:
20 continue
21 fields = line.split()
22 if maxid < int(fields[1]):
23 maxid=int(fields[1])
24 # id, element, x, y, z and ten neighbour ids
25 last = fields[10].split('0')
26 atoms[fields[1]] = [int(fields[1]), last[3], float(fields[5]), float(fields[6]), float(fields[7]), -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
27
28print "The biggest Id found was %d" % (maxid)
29
30# Parsing the dbond file
31print "Parsing the DBOND file "+sys.argv[2]+"."
32dbondfile = open(sys.argv[2], "r")
33test = dbondfile.readline() # skip the first two lines
34test = dbondfile.readline()
35for line in dbondfile:
36 fields = line.split()
37 ids = [int(fields[0]), int(fields[1])]
38 set = [0,0]
39 if str(ids[0]) in atoms and str(ids[1]) in atoms:
40 for i in range(5,15):
41 for j in range(2):
42 if set[j] == 0 and (atoms[ str(ids[j]) ])[i] == -1:
43 (atoms[ str(ids[j]) ])[i] = ids [ (j+1) %2 ]
44 set[j] = 1
45 if set[0] == 1 and set[1] == 1:
46 break
47 if set[0] == 0 or set[1] == 0:
48 print "ERROR: Either id already had ten neighbours, increase number in code!"
49 sys.exit(1)
50 else:
51 print "ERROR: Either %d or %d are not listed in the PDB file!" % (ids[0], ids[1])
52 sys.exit(1)
53
54# go through all Si and Ca and calculate CoG
55CoG = [ 0, 0, 0]
56nr=0
57for atom in atoms:
58 #print atoms[atom]
59 if "Si" in atoms[atom][1] or "Ca" in atoms[atom][1]:
60 for i in range(3):
61 CoG[i] += atoms[atom][2+i]
62 nr = nr + 1
63for i in range(3):
64 CoG[i] /= nr
65print "The Center of Gravity is at %g,%g,%g." % (CoG[0], CoG[1], CoG[2])
66
67# initialize the bin array
68NrBins=100
69BinWidth=0.5
70bins = []
71for i in range(NrBins):
72 bins.append(0)
73
74# go through every atom
75nrwaters = 0
76nrothers = 0
77for atom in atoms:
78 if "O" in atoms[atom][1]:
79 water = 1
80 for i in range(5,15):
81 if atoms[atom][i] == -1:
82 break
83 if str(atoms[atom][i]) in atoms:
84 if "Si" in atoms[ str(atoms[atom][i]) ][1]:
85 water = 0
86 break
87 else:
88 print "ERROR: key %d is not in list!" % (atoms[atom][i])
89 sys.exit(1)
90 if water == 1:
91 dist = 0
92 for i in range(3):
93 dist += (CoG[i] - atoms[atom][2+i])*(CoG[i] - atoms[atom][2+i])
94 dist = int(math.sqrt(dist)/BinWidth)
95 if dist < NrBins:
96 bins[dist] = bins[dist] + 1
97 nrwaters = nrwaters+1
98 else:
99 nrothers = nrothers + 1
100
101print "There were %d water molecules and %d oxygens connected to silica." % (nrwaters, nrothers)
102
103#print "The Bins are as follows:"
104output = open("histo_pair_correlation.dat", "w")
105output.write("#Distance [A]\tnumber in bin\n")
106for i in range(NrBins):
107 #print "%g\t%d" % (i*BinWidth, bins[i])
108 output.write("%g\t%g\n" % (i*BinWidth, bins[i]))
109output.close()
110
111print "finished."
Note: See TracBrowser for help on using the repository browser.