Package gromacs :: Package analysis :: Package plugins :: Module energy
[hide private]
[frames] | no frames]

Source Code for Module gromacs.analysis.plugins.energy

  1  # $Id$ 
  2  # Copyright (c) 2009 Oliver Beckstein <orbeckst@gmail.com> 
  3  # Released under the GNU Public License 3 (or higher, your choice) 
  4  # See the file COPYING for details. 
  5   
  6  """ 
  7  Various terms from the energy (edr) file 
  8  ======================================== 
  9   
 10  Simplified invocation of :func:`gromacs.g_energy`; only a few selected 
 11  terms are plotted. 
 12   
 13   
 14  Plugin class 
 15  ------------ 
 16   
 17  .. autoclass:: Energy 
 18     :members: worker_class 
 19     :undoc-members: 
 20   
 21  Worker class 
 22  ------------ 
 23   
 24  The worker class performs the analysis. 
 25   
 26  .. autoclass:: _Energy 
 27     :members: 
 28   
 29   
 30  """ 
 31  from __future__ import with_statement 
 32   
 33  __docformat__ = "restructuredtext en" 
 34   
 35  import os.path 
 36  import warnings 
 37   
 38  import gromacs 
 39  from gromacs.utilities import AttributeDict 
 40  from gromacs.analysis.core import Worker, Plugin 
 41   
 42  import logging 
 43  logger = logging.getLogger('gromacs.analysis.plugins.energy') 
 44   
 45  # Worker classes that are registered via Plugins (see below) 
 46  # ---------------------------------------------------------- 
 47  # These must be defined before the plugins. 
 48   
49 -class _Energy(Worker):
50 """Energy worker class. 51 52 **Gromacs 4-git energy terms** 53 54 Select the terms you want from the following list by selecting 55 either (part of) the name or the number or a combination. End 56 your selection with an empty line or a zero :: 57 58 1 Bond 2 Angle 3 Proper-Dih. 4 Ryckaert-Bell. 59 5 Improper-Dih. 6 LJ-14 7 Coulomb-14 8 LJ-(SR) 60 9 Disper.-corr. 10 Coulomb-(SR) 11 Coul.-recip. 12 Potential 61 13 Kinetic-En. 14 Total-Energy 15 Temperature 16 Pressure-(bar) 62 17 Cons.-rmsd-() 18 Box-X 19 Box-Y 20 Box-Z 63 21 Volume 22 Density-(SI) 23 pV 24 Vir-XX 64 25 Vir-XY 26 Vir-XZ 27 Vir-YX 28 Vir-YY 65 29 Vir-YZ 30 Vir-ZX 31 Vir-ZY 32 Vir-ZZ 66 33 Pres-XX-(bar) 34 Pres-XY-(bar) 35 Pres-XZ-(bar) 36 Pres-YX-(bar) 67 37 Pres-YY-(bar) 38 Pres-YZ-(bar) 39 Pres-ZX-(bar) 40 Pres-ZY-(bar) 68 41 Pres-ZZ-(bar) 42 #Surf*SurfTen 43 Mu-X 44 Mu-Y 69 45 Mu-Z 46 Coul-SR:SOLVENT-SOLVENT 70 47 LJ-SR:SOLVENT-SOLVENT 48 Coul-14:SOLVENT-SOLVENT 71 49 LJ-14:SOLVENT-SOLVENT 50 Coul-SR:SOLVENT-LIPIDS 72 51 LJ-SR:SOLVENT-LIPIDS 52 Coul-14:SOLVENT-LIPIDS 73 53 LJ-14:SOLVENT-LIPIDS 54 Coul-SR:SOLVENT-Protein 74 55 LJ-SR:SOLVENT-Protein 56 Coul-14:SOLVENT-Protein 75 57 LJ-14:SOLVENT-Protein 58 Coul-SR:LIPIDS-LIPIDS 76 59 LJ-SR:LIPIDS-LIPIDS 60 Coul-14:LIPIDS-LIPIDS 77 61 LJ-14:LIPIDS-LIPIDS 62 Coul-SR:LIPIDS-Protein 78 63 LJ-SR:LIPIDS-Protein 64 Coul-14:LIPIDS-Protein 79 65 LJ-14:LIPIDS-Protein 66 Coul-SR:Protein-Protein 80 67 LJ-SR:Protein-Protein 68 Coul-14:Protein-Protein 81 69 LJ-14:Protein-Protein 70 T-SOLVENT 82 71 T-LIPIDS 72 T-Protein 73 Lamb-SOLVENT 74 Lamb-LIPIDS 83 75 Lamb-Protein 84 """ 85 86 terms = ['Potential', 'Kinetic-En.', 'Total-Energy', 87 'Temperature', 'Pressure', 88 'Volume', 'Box-X', 'Box-Y', 'Box-Z', 89 ] 90
91 - def __init__(self,**kwargs):
92 """Set up Energy analysis. 93 94 This is the worker class; this is where all the real analysis is done. 95 """ 96 super(_Energy, self).__init__(**kwargs) 97 if not self.simulation is None: 98 self._register_hook()
99
100 - def _register_hook(self, **kwargs):
101 """Run when registering; requires simulation.""" 102 103 super(_Energy, self)._register_hook(**kwargs) 104 assert not self.simulation is None 105 106 self.parameters.filenames = { 107 'Energy': self.plugindir('energy.xvg'), 108 } 109 # default filename for the plot 110 self.parameters.figname = self.figdir('energy')
111
112 - def run(self, force=False, **gmxargs):
113 """Analyze trajectory and write energy files. 114 115 :Arguments: 116 - *force*: do analysis and overwrite existing files 117 - *gmxargs*: additional keyword arguments for :func:`gromacs.g_energy` 118 """ 119 if not self.check_file_exists(self.parameters.filenames['Energy'], resolve='warning') or force: 120 logger.info("Analyzing energy file...") 121 gromacs.g_energy(s=self.simulation.tpr, f=self.simulation.edr, 122 o=self.parameters.filenames['Energy'], 123 input=self.terms + [0], **gmxargs)
124
125 - def analyze(self,**kwargs):
126 """Collect output xvg files as :class:`gromacs.formats.XVG` objects. 127 128 :Returns: a dictionary of the results and also sets ``self.results``. 129 """ 130 from gromacs.formats import XVG 131 132 logger.info("Preparing Energy graphs as XVG objects.") 133 results = AttributeDict(Energy=XVG(self.parameters.filenames['Energy'])) 134 self.results = results 135 return results
136
137 - def plot(self, **kwargs):
138 """Plot all results in one graph, labelled by the result keys. 139 140 :Keywords: 141 figure 142 - ``True``: save figures in the given formats 143 - "name.ext": save figure under this filename (``ext`` -> format) 144 - ``False``: only show on screen 145 formats : sequence 146 sequence of all formats that should be saved [('png', 'pdf')] 147 plotargs 148 keyword arguments for pylab.plot() 149 """ 150 151 import pylab 152 figure = kwargs.pop('figure', False) 153 extensions = kwargs.pop('formats', ('pdf','png')) 154 for name,result in self.results.items(): 155 kwargs['label'] = name 156 try: 157 result.plot(**kwargs) # This requires result classes with a plot() method!! 158 except AttributeError: 159 warnings.warn("Sorry, plotting of result %(name)r is not implemented" % vars(), 160 category=UserWarning) 161 pylab.legend(loc='best') 162 if figure is True: 163 for ext in extensions: 164 self.savefig(ext=ext) 165 elif figure: 166 self.savefig(filename=figure)
167 168 169 170 171 # Public classes that register the worker classes 172 #------------------------------------------------ 173
174 -class Energy(Plugin):
175 """*Energy* plugin. 176 177 Analysis of terms in the Gromacs energy (edr) file. 178 179 .. class:: Energy([name[, simulation]]) 180 181 :Arguments: 182 *name* : string 183 plugin name (used to access it) 184 *simulation* : instance 185 The :class:`gromacs.analysis.Simulation` instance that owns the plugin. 186 187 """ 188 worker_class = _Energy
189