Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2015-2018 The plumed team
3 : (see the PEOPLE file at the root of the distribution for a list of names)
4 :
5 : See http://www.plumed.org for more information.
6 :
7 : This file is part of plumed, version 2.
8 :
9 : plumed is free software: you can redistribute it and/or modify
10 : it under the terms of the GNU Lesser General Public License as published by
11 : the Free Software Foundation, either version 3 of the License, or
12 : (at your option) any later version.
13 :
14 : plumed 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
17 : GNU Lesser General Public License for more details.
18 :
19 : You should have received a copy of the GNU Lesser General Public License
20 : along with plumed. If not, see <http://www.gnu.org/licenses/>.
21 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 : #include "core/ActionAtomistic.h"
23 : #include "core/ActionPilot.h"
24 : #include "core/ActionRegister.h"
25 : #include "tools/File.h"
26 : #include "core/PlumedMain.h"
27 : #include "core/Atoms.h"
28 :
29 : using namespace std;
30 :
31 : namespace PLMD
32 : {
33 : namespace generic {
34 :
35 : //+PLUMEDOC PRINTANALYSIS DUMPMASSCHARGE
36 : /*
37 : Dump masses and charges on a selected file.
38 :
39 : This command dumps a file containing charges and masses.
40 : It does so only once in the simulation (at first step).
41 : File can be recycled in the \ref driver tool.
42 :
43 : Notice that masses and charges are only written once at the beginning
44 : of the simulation. In case no atom list is provided, charges and
45 : masses for all atoms are written.
46 :
47 : \par Examples
48 :
49 : You can add the DUMPMASSCHARGE action at the end of the plumed.dat
50 : file that you use during an MD simulations:
51 :
52 : \verbatim
53 : c1: COM ATOMS=1-10
54 : c2: COM ATOMS=11-20
55 : PRINT ARG=c1,c2 FILE=colvar STRIDE=100
56 :
57 : DUMPMASSCHARGE FILE=mcfile
58 : \endverbatim
59 : (see also \ref COM and \ref PRINT)
60 :
61 : In this way, you will be able to use the same masses while processing
62 : a trajectory from the \ref driver . To do so, you need to
63 : add the --mc flag on the driver command line, e.g.
64 : \verbatim
65 : plumed driver --mc mcfile --plumed plumed.dat --ixyz traj.xyz
66 : \endverbatim
67 :
68 : With the following input you can dump only the charges for a specific
69 : group.
70 : \verbatim
71 : solute_ions: GROUP ATOMS=1-121,200-2012
72 : DUMPATOMS FILE=traj.gro ATOMS=solute_ions STRIDE=100
73 : DUMPMASSCHARGE FILE=mcfile ATOMS=solute_ions
74 : \endverbatim
75 : Notice however that if you want to process the charges
76 : with the driver (e.g. reading traj.gro) you have to fix atom
77 : numbers first, e.g. with the script
78 : \verbatim
79 : awk 'BEGIN{c=0}{
80 : if(match($0,"#")) print ; else {print c,$2,$3; c++}
81 : }' < mc > newmc
82 : }'
83 : \endverbatim
84 : then
85 : \verbatim
86 : plumed driver --mc newmc --plumed plumed.dat --ixyz traj.gro
87 : \endverbatim
88 :
89 :
90 : */
91 : //+ENDPLUMEDOC
92 :
93 : class DumpMassCharge:
94 : public ActionAtomistic,
95 : public ActionPilot
96 : {
97 : string file;
98 : bool first;
99 : bool second;
100 : public:
101 : explicit DumpMassCharge(const ActionOptions&);
102 : ~DumpMassCharge();
103 : static void registerKeywords( Keywords& keys );
104 : void prepare();
105 30 : void calculate() {}
106 30 : void apply() {}
107 : void update();
108 : };
109 :
110 2529 : PLUMED_REGISTER_ACTION(DumpMassCharge,"DUMPMASSCHARGE")
111 :
112 7 : void DumpMassCharge::registerKeywords( Keywords& keys ) {
113 7 : Action::registerKeywords( keys );
114 7 : ActionPilot::registerKeywords( keys );
115 7 : ActionAtomistic::registerKeywords( keys );
116 7 : keys.add("compulsory","STRIDE","1","the frequency with which the atoms should be output");
117 7 : keys.add("atoms", "ATOMS", "the atom indices whose positions you would like to print out");
118 7 : keys.add("compulsory", "FILE", "file on which to output coordinates. .gro extension is automatically detected");
119 7 : }
120 :
121 6 : DumpMassCharge::DumpMassCharge(const ActionOptions&ao):
122 : Action(ao),
123 : ActionAtomistic(ao),
124 : ActionPilot(ao),
125 : first(true),
126 6 : second(true)
127 : {
128 6 : vector<AtomNumber> atoms;
129 6 : parse("FILE",file);
130 6 : if(file.length()==0) error("name out output file was not specified");
131 :
132 6 : parseAtomList("ATOMS",atoms);
133 :
134 6 : if(atoms.size()==0) {
135 436 : for(unsigned i=0; i<plumed.getAtoms().getNatoms(); i++) {
136 432 : atoms.push_back(AtomNumber::index(i));
137 : }
138 : }
139 :
140 6 : checkRead();
141 :
142 6 : log.printf(" printing the following atoms:" );
143 6 : for(unsigned i=0; i<atoms.size(); ++i) log.printf(" %d",atoms[i].serial() );
144 6 : log.printf("\n");
145 6 : requestAtoms(atoms);
146 6 : }
147 :
148 30 : void DumpMassCharge::prepare() {
149 30 : if(!first && second) {
150 6 : requestAtoms(vector<AtomNumber>());
151 6 : second=false;
152 : }
153 30 : }
154 :
155 30 : void DumpMassCharge::update() {
156 60 : if(!first) return;
157 6 : first=false;
158 :
159 6 : OFile of;
160 6 : of.link(*this);
161 6 : of.open(file);
162 :
163 454 : for(int i=0; i<getNumberOfAtoms(); i++) {
164 448 : int ii=getAbsoluteIndex(i).index();
165 448 : of.printField("index",ii);
166 448 : of.printField("mass",getMass(i));
167 448 : of.printField("charge",getCharge(i));
168 448 : of.printField();
169 6 : }
170 : }
171 :
172 18 : DumpMassCharge::~DumpMassCharge() {
173 18 : }
174 :
175 :
176 : }
177 2523 : }
|