OpenMPCD
DensityProfile.py
1 from pylibconfig import Config
2 import subprocess
3 
5  def __init__(self, path):
6  config = Config()
7  config.readFile(path + "/config.txt")
8 
9  self.file = open(path + "/densityProfile.data")
10  self.points = {}
11  for line in self.file:
12  self.parseDataLine(line)
13 
14  def plot(self):
15  data = self.reduceToXY()
16 
17  gnuplotData = ""
18  for x, xval in sorted(data.items()):
19  for y, yval in sorted(xval.items()):
20  gnuplotData += str(x) + "\t" + str(y) + "\t" + str(yval) + "\n"
21  gnuplotData += "\n"
22 
23  gnuplot = subprocess.Popen(['gnuplot'], stdin=subprocess.PIPE)
24 
25  gnuplotCommand = "set terminal wxt persist\n"
26  gnuplotCommand += "unset surface\n"
27  gnuplotCommand += "set view map\n"
28  gnuplotCommand += "set pm3d\n"
29 
30  title = self.file.name
31  gnuplotCommand += "set title \"" + title + "\" \n"
32 
33  gnuplotCommand += "splot '-' using 1:2:3 notitle\n"
34  gnuplotCommand += gnuplotData
35  gnuplotCommand += "e"
36 
37  gnuplot.stdin.write(gnuplotCommand.encode("UTF-8"))
38 
39  def parseDataLine(self, line):
40  columns = line.split("\t")
41  x, y, z = [ float(i) for i in columns[0:3] ]
42  density = float(columns[3])
43 
44  self.setPoint(x, y, z, density)
45 
46  def setPoint(self, x, y, z, density):
47  if not x in self.points:
48  self.points[x] = {}
49 
50  if not y in self.points[x]:
51  self.points[x][y] = {}
52 
53  self.points[x][y][z] = density
54 
55  def reduceToXY(self):
56  data = {}
57 
58  for x, xval in self.points.items():
59  data[x] = {}
60 
61  for y, yval in xval.items():
62  data[x][y] = 0
63  for _, zval in yval.items():
64  data[x][y] += zval
65  data[x][y] /= len(yval)
66 
67  return data
MPCDAnalysis.DensityProfile.DensityProfile.points
points
Definition: DensityProfile.py:10
MPCDAnalysis.DensityProfile.DensityProfile.file
file
Definition: DensityProfile.py:9
MPCDAnalysis.DensityProfile.DensityProfile.reduceToXY
def reduceToXY(self)
Definition: DensityProfile.py:55
MPCDAnalysis.DensityProfile.DensityProfile.parseDataLine
def parseDataLine(self, line)
Definition: DensityProfile.py:39
MPCDAnalysis.DensityProfile.DensityProfile
Definition: DensityProfile.py:4
MPCDAnalysis.DensityProfile.DensityProfile.setPoint
def setPoint(self, x, y, z, density)
Definition: DensityProfile.py:46