Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2015-2019 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 : \plumedfile
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 : \endplumedfile
59 :
60 : In this way, you will be able to use the same masses while processing
61 : a trajectory from the \ref driver . To do so, you need to
62 : add the --mc flag on the driver command line, e.g.
63 : \verbatim
64 : plumed driver --mc mcfile --plumed plumed.dat --ixyz traj.xyz
65 : \endverbatim
66 :
67 : With the following input you can dump only the charges for a specific
68 : group.
69 : \plumedfile
70 : solute_ions: GROUP ATOMS=1-121,200-2012
71 : DUMPATOMS FILE=traj.gro ATOMS=solute_ions STRIDE=100
72 : DUMPMASSCHARGE FILE=mcfile ATOMS=solute_ions
73 : \endplumedfile
74 : Notice however that if you want to process the charges
75 : with the driver (e.g. reading traj.gro) you have to fix atom
76 : numbers first, e.g. with the script
77 : \verbatim
78 : awk 'BEGIN{c=0}{
79 : if(match($0,"#")) print ; else {print c,$2,$3; c++}
80 : }' < mc > newmc
81 : }'
82 : \endverbatim
83 : then
84 : \verbatim
85 : plumed driver --mc newmc --plumed plumed.dat --ixyz traj.gro
86 : \endverbatim
87 :
88 :
89 : */
90 : //+ENDPLUMEDOC
91 :
92 : class DumpMassCharge:
93 : public ActionAtomistic,
94 : public ActionPilot
95 : {
96 : string file;
97 : bool first;
98 : bool second;
99 : public:
100 : explicit DumpMassCharge(const ActionOptions&);
101 : ~DumpMassCharge();
102 : static void registerKeywords( Keywords& keys );
103 : void prepare();
104 30 : void calculate() {}
105 30 : void apply() {}
106 : void update();
107 : };
108 :
109 6458 : PLUMED_REGISTER_ACTION(DumpMassCharge,"DUMPMASSCHARGE")
110 :
111 7 : void DumpMassCharge::registerKeywords( Keywords& keys ) {
112 7 : Action::registerKeywords( keys );
113 7 : ActionPilot::registerKeywords( keys );
114 7 : ActionAtomistic::registerKeywords( keys );
115 35 : keys.add("compulsory","STRIDE","1","the frequency with which the atoms should be output");
116 28 : keys.add("atoms", "ATOMS", "the atom indices whose positions you would like to print out");
117 28 : keys.add("compulsory", "FILE", "file on which to output coordinates. .gro extension is automatically detected");
118 7 : }
119 :
120 6 : DumpMassCharge::DumpMassCharge(const ActionOptions&ao):
121 : Action(ao),
122 : ActionAtomistic(ao),
123 : ActionPilot(ao),
124 : first(true),
125 12 : second(true)
126 : {
127 : vector<AtomNumber> atoms;
128 12 : parse("FILE",file);
129 6 : if(file.length()==0) error("name out output file was not specified");
130 :
131 12 : parseAtomList("ATOMS",atoms);
132 :
133 6 : if(atoms.size()==0) {
134 1304 : for(unsigned i=0; i<plumed.getAtoms().getNatoms(); i++) {
135 864 : atoms.push_back(AtomNumber::index(i));
136 : }
137 : }
138 :
139 6 : checkRead();
140 :
141 6 : log.printf(" printing the following atoms:" );
142 1356 : for(unsigned i=0; i<atoms.size(); ++i) log.printf(" %d",atoms[i].serial() );
143 6 : log.printf("\n");
144 6 : requestAtoms(atoms);
145 6 : }
146 :
147 30 : void DumpMassCharge::prepare() {
148 30 : if(!first && second) {
149 18 : requestAtoms(vector<AtomNumber>());
150 6 : second=false;
151 : }
152 30 : }
153 :
154 30 : void DumpMassCharge::update() {
155 54 : if(!first) return;
156 6 : first=false;
157 :
158 12 : OFile of;
159 6 : of.link(*this);
160 6 : of.open(file);
161 :
162 902 : for(int i=0; i<getNumberOfAtoms(); i++) {
163 448 : int ii=getAbsoluteIndex(i).index();
164 896 : of.printField("index",ii);
165 896 : of.printField("mass",getMass(i));
166 896 : of.printField("charge",getCharge(i));
167 448 : of.printField();
168 : }
169 : }
170 :
171 18 : DumpMassCharge::~DumpMassCharge() {
172 12 : }
173 :
174 :
175 : }
176 4839 : }
|