OpenMPCD
Base.py
1 import abc
2 
3 class Base(object):
4  __metaclass__ = abc.ABCMeta
5 
6  """
7  Base class for analysis of star polymer simulations.
8  """
9 
10  def __init__(self, run):
11  """
12  The constructor.
13 
14  @param[in] run
15  The run in question, which must be an instance of `Run`.
16  """
17 
18  from ..Run import Run
19 
20  if not isinstance(run, Run):
21  raise TypeError()
22 
23  self._run = run
24  self._writePath = None
25 
26 
27  def setWritePath(self, writePath):
28  """
29  Sets the path to the directory that data files are written to.
30 
31  @param[in] writePath
32  If not `None`, a path used to write data to.
33  """
34 
35  self._writePath = writePath
36 
37 
38  def getRun(self):
39  """
40  Returns the run that is being analyzed.
41  """
42 
43  return self._run
44 
45 
46  def getValueAsFunctionOfTime(self):
47  """
48  Returns the asphericity as a function of time, in the form of a
49  `collections.OrderedDict`.
50 
51  This function will make use of cache results, if available.
52  """
53 
54  if self._hasDataInCache():
55  return self._getDataInCache()
56 
57  data = self._computeValueAsFunctionOfTime()
58 
59  self._storeDataInCache(data)
60 
61  return data
62 
63 
64  def updateCache(self, forced = False):
65  """
66  Updates the cache, if necessary or if forced.
67 
68  @param[in] forced
69  Whether to force an update, even if the cache is up to date.
70  """
71 
72  if not forced and self._hasDataInCache():
73  return
74 
75  data = self._computeValueAsFunctionOfTime()
76 
77  self._storeDataInCache(data)
78 
79 
80  def _getCache(self):
81  """
82  Returns the cache instance for this run.
83  """
84 
85  from .Cache import Cache
86 
87  return Cache(self.getRun(), self._writePath)
88 
89 
90  def _getSnapshotsPath(self):
91  """
92  Returns the path to this run's snapshots.
93  """
94 
95  return self.getRun().getPath() + "/StarPolymers/snapshots.vtf"
96 
97 
98  def _getSnapshots(self):
99  """
100  Returns an instance of `VTFSnapshotFile` for this run.
101 
102  @throw RuntimeError
103  Throws if the snapshot file does not exist.
104  """
105 
106  from ..VTFSnapshotFile import VTFSnapshotFile
107 
108  return VTFSnapshotFile(self._getSnapshotsPath(), assertReadMode = True)
109 
110 
111  def _getStarPolymers(self):
112  """
113  Returns an instance of `StarPolymers` that corresponds to this run.
114  """
115 
116  from ..StarPolymers import StarPolymers
117 
118  config = self.getRun().getConfiguration()["solute.StarPolymers"]
119  return StarPolymers(config)
120 
121 
122  def _getCacheMetadata(self):
123  """
124  Returns the cache metadata.
125  """
126 
127  cacheVersion = 1
128 
129  metadata = {"cacheVersion": cacheVersion}
130  return metadata
131 
132 
133  def _hasDataInCache(self):
134  """
135  Returns whether there are cached data.
136  """
137 
138  metadata = self._getCacheMetadata()
139  return self._getCache().hasData(self._getCacheFilename(), metadata)
140 
141 
142  def _storeDataInCache(self, data):
143  """
144  Stores the given data in the cache.
145  """
146 
147  metadata = self._getCacheMetadata()
148  self._getCache().storeData(self._getCacheFilename(), data, metadata)
149 
150 
151  def _getCacheKeyInterpreter(self):
152  """
153  Returns what is to be used as the `keyInterpreter` argument to
154  `Cache.getDataOrderedDict`.
155  """
156 
157  return float
158 
159 
160  def _getCacheValueInterpreter(self):
161  """
162  Returns what is to be used as the `valueInterpreter` argument to
163  `Cache.getDataOrderedDict`.
164  """
165 
166  return float
167 
168 
169  def _getDataInCache(self):
170  """
171  Returns the cached data.
172 
173  @throw RuntimeError
174  Throws if `not self._hasDataInCache()`.
175  """
176 
177  if not self._hasDataInCache():
178  raise RuntimeError()
179 
180  filename = self._getCacheFilename()
181  metadata = self._getCacheMetadata()
182 
183  return \
184  self._getCache().getDataOrderedDict(
185  filename, metadata,
MPCDAnalysis.StarPolymersAnalysis.Base.Base
Definition: Base.py:3
MPCDAnalysis.StarPolymersAnalysis.Base.Base.updateCache
def updateCache(self, forced=False)
Definition: Base.py:75
MPCDAnalysis.StarPolymersAnalysis.Base.Base.getRun
def getRun(self)
Definition: Base.py:44
MPCDAnalysis.StarPolymersAnalysis.Base.Base._getSnapshotsPath
def _getSnapshotsPath(self)
Definition: Base.py:100
MPCDAnalysis.StarPolymersAnalysis.Base.Base._storeDataInCache
def _storeDataInCache(self, data)
Definition: Base.py:157
MPCDAnalysis.StarPolymersAnalysis.Base.Base._getCacheKeyInterpreter
def _getCacheKeyInterpreter(self)
Definition: Base.py:168
MPCDAnalysis.StarPolymersAnalysis.Base.Base._getCache
def _getCache(self)
Definition: Base.py:89
MPCDAnalysis.StarPolymersAnalysis.Base.Base._getCacheValueInterpreter
def _getCacheValueInterpreter(self)
Definition: Base.py:178
MPCDAnalysis.StarPolymersAnalysis.Base.Base._getDataInCache
def _getDataInCache(self)
Definition: Base.py:190
MPCDAnalysis.StarPolymersAnalysis.Base.Base._hasDataInCache
def _hasDataInCache(self)
Definition: Base.py:147
MPCDAnalysis.StarPolymersAnalysis.Base.Base.getValueAsFunctionOfTime
def getValueAsFunctionOfTime(self)
Definition: Base.py:56
MPCDAnalysis.StarPolymersAnalysis.Base.Base._writePath
_writePath
Definition: Base.py:25
MPCDAnalysis.StarPolymersAnalysis.Base.Base.__init__
def __init__(self, run)
Definition: Base.py:17
MPCDAnalysis.StarPolymersAnalysis.Base.Base._run
_run
Definition: Base.py:24
MPCDAnalysis.StarPolymersAnalysis.Base.Base.setWritePath
def setWritePath(self, writePath)
Definition: Base.py:35
MPCDAnalysis.StarPolymersAnalysis.Base.Base._getCacheMetadata
def _getCacheMetadata(self)
Definition: Base.py:135