ac91337f085c25e3f99b6ad2e1c82c6c96495f6a
[alexxy/gromacs.git] / src / python / test.py
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2015, by the GROMACS development team, led by
5 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 # and including many others, as listed in the AUTHORS file in the
7 # top-level source directory and at http://www.gromacs.org.
8 #
9 # GROMACS is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public License
11 # as published by the Free Software Foundation; either version 2.1
12 # of the License, or (at your option) any later version.
13 #
14 # GROMACS is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # Lesser General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with GROMACS; if not, see
21 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23 #
24 # If you want to redistribute modifications to GROMACS, please
25 # consider that scientific software is very special. Version
26 # control is crucial - bugs must be traceable. We will be happy to
27 # consider code for inclusion in the official distribution, but
28 # derived work must not be called official GROMACS. Details are found
29 # in the README & COPYING files - if they are missing, get the
30 # official version at http://www.gromacs.org.
31 #
32 # To help us fund GROMACS development, we humbly ask that you cite
33 # the research papers on the package. Check out http://www.gromacs.org.
34
35 import sys
36 from gromacs import Options
37 from gromacs import TrajectoryAnalysis
38
39 class M(TrajectoryAnalysis.TrajectoryAnalysisModule):
40     def __init__(self):
41         super(M, self).__init__("a", "a")
42
43     def initOptions(self, options, settings):
44         print('python: initOptions')
45
46         settings.setHelpText('A stupid test module')
47
48         self.optionsHolder = Options.PyOptionsHolder()
49
50         options.addOption(self.optionsHolder.selectionOption('sel').required())
51         options.addOption(self.optionsHolder.fileNameOption('file').defaultBasename('test').description('filename from python to rule them all').outputFile().required().filetype(Options.eftGenericData))
52         settings.setFlag(TrajectoryAnalysis.TrajectoryAnalysisSettings.efRequireTop)
53
54         self.angle = TrajectoryAnalysis.AngleInfo.create()
55
56         print('python: inited')
57
58     def getBatch(self):
59         print('python: getBatch')
60         return [self.angle]
61
62     def getArgv(self, i):
63         print('python: getArgv')
64         if i == 0:
65             #First element of list should be module name -- gets discarded by parser anyway
66             return ["gangle", "-group1", "Backbone", "-oav", "angles.xvg"]
67
68     def initAnalysis(self, settings, top):
69         print('python: initAnalysis')
70         print('There are {} atoms'.format(top.topology().atoms.nr))
71         print('Topology name: {}'.format(top.topology().name))
72
73         #Tell GROMACS to keep last frame in storage, required in analyzeFrame()
74         self.angle.datasetFromIndex(1).requestStorage(1)
75
76     def analyzeFrame(self, frnr, frame, pbc, data):
77         sel = self.optionsHolder['sel']
78
79         dataset = self.angle.datasetFromIndex(1)
80
81         print('frame =', frnr, ', columnCount =', dataset.columnCount(), ', y =', dataset.getDataFrame(frnr).y(0))
82
83     def finishAnalysis(self, nframes):
84         print('python: Analyzed {} frames'.format(nframes))
85
86     def writeOutput(self):
87         print('python: writeOutput')
88         print('file = {}'.format(self.optionsHolder['file']))
89
90         fo = open(self.optionsHolder['file'], 'w')
91         fo.write('Test output\n')
92
93 TrajectoryAnalysis.runAsMain(M(), sys.argv)