OpenMPCD
PotentialEnergy.py
1 from .Base import Base
2 
3 class PotentialEnergy(Base):
4  """
5  Class for analysis of potential energies.
6  """
7 
8  def __init__(self, run):
9  """
10  The constructor.
11 
12  @param[in] run
13  The run to analyze, as an instance of `Run`.
14  """
15 
16  super(PotentialEnergy, self).__init__(run)
17 
18 
20  """
21  Returns a `matplotlib.axes.Axes` object that contains a plot of the
22  potential energy, with the horizontal axis showing the simulation
23  time `t`, and the vertical axis showing the potential energy at that
24  point in time.
25  """
26 
27  import matplotlib.figure
28 
29  figure = matplotlib.figure.Figure()
30  axes = figure.add_subplot(1, 1, 1)
31  lines = []
32  legendLabels = []
33 
34  data = self.getValueAsFunctionOfTime()
35  _line, = axes.plot(data.keys(), data.values())
36  lines.append(_line)
37  legendLabels.append("Potential Energy")
38 
39  axes.legend(lines, legendLabels)
40 
41  axes.set_title("Star Polymer Potential Energy")
42  axes.set_xlabel("Simulation Time t")
43  axes.set_ylabel("Potential Energy")
44 
45  return axes
46 
47 
48  def _computeValueAsFunctionOfTime(self):
49  """
50  Takes the raw simulation output, and computes the potential energy as
51  a function of time. The result is returned as an
52  `collcections.OrderedDict`.
53  """
54 
55  from collections import OrderedDict
56 
57  config = self.getRun().getConfiguration()
58 
59  snapshots = self._getSnapshots()
60  starPolymers = self._getStarPolymers()
61 
62  ret = OrderedDict()
63  timestep = config["mpc.timestep"]
64  time = timestep * config["mpc.warmupSteps"]
65 
66  lastTimestepAnalyzed = -9e9
67  while True:
68  time += timestep
69 
70  particles = snapshots.readTimestep()
71 
72  if time - lastTimestepAnalyzed < self._getAnalysisTimestep():
73  continue
74  lastTimestepAnalyzed = time
75 
76 
77  if particles.isEmpty():
78  break
79 
80  starPolymers.setParticles(particles)
81 
82  ret[time] = starPolymers.getPotentialEnergy()
83 
84  return ret
85 
86 
87  def _getCacheFilename(self):
88  """
89  Returns the filename where cached data is (expected to be) saved to.
90  """
91 
92  return "potentialEnergy.txt"
93 
94 
95  def _getCacheMetadata(self):
96  """
97  Returns the cache metadata.
98  """
99 
100  cacheVersion = 1
101 
102  metadata = \
103  {
104  "cacheVersion": cacheVersion,
105  "analysisTimestep": self._getAnalysisTimestep()
106  }
107  return metadata
108 
109 
110  def _getAnalysisTimestep(self):
111  """
112  Returns the timestep between to analysis points.
113  """
114 
115  return 100
MPCDAnalysis.StarPolymersAnalysis.PotentialEnergy.PotentialEnergy.getMPLAxesForValueAsFunctionOfTime
def getMPLAxesForValueAsFunctionOfTime(self)
Definition: PotentialEnergy.py:28
MPCDAnalysis.StarPolymersAnalysis.PotentialEnergy.PotentialEnergy._getAnalysisTimestep
def _getAnalysisTimestep(self)
Definition: PotentialEnergy.py:120
MPCDAnalysis.StarPolymersAnalysis.Base.Base.getRun
def getRun(self)
Definition: Base.py:44
MPCDAnalysis.StarPolymersAnalysis.Base.Base._getSnapshots
def _getSnapshots(self)
Definition: Base.py:112
MPCDAnalysis.StarPolymersAnalysis.Base.Base.getValueAsFunctionOfTime
def getValueAsFunctionOfTime(self)
Definition: Base.py:56
MPCDAnalysis.StarPolymersAnalysis.PotentialEnergy.PotentialEnergy.__init__
def __init__(self, run)
Definition: PotentialEnergy.py:16
MPCDAnalysis.StarPolymersAnalysis.Base.Base._getStarPolymers
def _getStarPolymers(self)
Definition: Base.py:123