Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2011-2023 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/Pbc.h"
26 : #include "tools/File.h"
27 : #include "core/PlumedMain.h"
28 : #include "core/Atoms.h"
29 : #include "tools/Units.h"
30 : #include <cstdio>
31 : #include <memory>
32 : #include "core/GenericMolInfo.h"
33 : #include "core/ActionSet.h"
34 : #include "xdrfile/xdrfile_xtc.h"
35 : #include "xdrfile/xdrfile_trr.h"
36 :
37 :
38 : namespace PLMD
39 : {
40 : namespace generic {
41 :
42 : //+PLUMEDOC PRINTANALYSIS DUMPATOMS
43 : /*
44 : Dump selected atoms on a file.
45 :
46 : This command can be used to output the positions of a particular set of atoms.
47 : The atoms required are output in a xyz or gro formatted file.
48 : If PLUMED has been compiled with xdrfile support, then also xtc and trr files can be written.
49 : To this aim one should install xdrfile library (http://www.gromacs.org/Developer_Zone/Programming_Guide/XTC_Library).
50 : If the xdrfile library is installed properly the PLUMED configure script should be able to
51 : detect it and enable it.
52 : The type of file is automatically detected from the file extension, but can be also
53 : enforced with TYPE.
54 : Importantly, if your
55 : input file contains actions that edit the atoms position (e.g. \ref WHOLEMOLECULES)
56 : and the DUMPATOMS command appears after this instruction, then the edited
57 : atom positions are output.
58 : You can control the buffering of output using the \ref FLUSH keyword on a separate line.
59 :
60 : Units of the printed file can be controlled with the UNITS keyword. By default PLUMED units as
61 : controlled in the \ref UNITS command are used, but one can override it e.g. with UNITS=A.
62 : Notice that gro/xtc/trr files can only contain coordinates in nm.
63 :
64 : \par Examples
65 :
66 : The following input instructs plumed to print out the positions of atoms
67 : 1-10 together with the position of the center of mass of atoms 11-20 every
68 : 10 steps to a file called file.xyz.
69 : \plumedfile
70 : COM ATOMS=11-20 LABEL=c1
71 : DUMPATOMS STRIDE=10 FILE=file.xyz ATOMS=1-10,c1
72 : \endplumedfile
73 : Notice that the coordinates in the xyz file will be expressed in nm, since these
74 : are the defaults units in PLUMED. If you want the xyz file to be expressed in A, you should use the
75 : following input
76 : \plumedfile
77 : COM ATOMS=11-20 LABEL=c1
78 : DUMPATOMS STRIDE=10 FILE=file.xyz ATOMS=1-10,c1 UNITS=A
79 : \endplumedfile
80 : As an alternative, you might want to set all the length used by PLUMED to Angstrom using the \ref UNITS
81 : action. However, this latter choice will affect all your input and output.
82 :
83 : The following input is very similar but dumps a .gro (gromacs) file,
84 : which also contains atom and residue names.
85 : \plumedfile
86 : #SETTINGS MOLFILE=regtest/basic/rt32/helix.pdb
87 : # this is required to have proper atom names:
88 : MOLINFO STRUCTURE=reference.pdb
89 : # if omitted, atoms will have "X" name...
90 :
91 : COM ATOMS=11-20 LABEL=c1
92 : DUMPATOMS STRIDE=10 FILE=file.gro ATOMS=1-10,c1
93 : # notice that last atom is a virtual one and will not have
94 : # a correct name in the resulting gro file
95 : \endplumedfile
96 :
97 : The `file.gro` will contain coordinates expressed in nm, since this is the convention for gro files.
98 :
99 : In case you have compiled PLUMED with `xdrfile` library, you might even write xtc or trr files as follows
100 : \plumedfile
101 : COM ATOMS=11-20 LABEL=c1
102 : DUMPATOMS STRIDE=10 FILE=file.xtc ATOMS=1-10,c1
103 : \endplumedfile
104 : Notice that xtc files are significantly smaller than gro and xyz files.
105 :
106 : Finally, consider that gro and xtc file store coordinates with limited precision set by the
107 : `PRECISION` keyword. Default value is 3, which means "3 digits after dot" in nm (1/1000 of a nm).
108 : The following will write a larger xtc file with high resolution coordinates:
109 : \plumedfile
110 : COM ATOMS=11-20 LABEL=c1
111 : DUMPATOMS STRIDE=10 FILE=file.xtc ATOMS=1-10,c1 PRECISION=7
112 : \endplumedfile
113 :
114 :
115 :
116 : */
117 : //+ENDPLUMEDOC
118 :
119 : class DumpAtoms:
120 : public ActionAtomistic,
121 : public ActionPilot
122 : {
123 : OFile of;
124 : double lenunit;
125 : int iprecision;
126 : std::vector<std::string> names;
127 : std::vector<unsigned> residueNumbers;
128 : std::vector<std::string> residueNames;
129 : std::string type;
130 : std::string fmt_gro_pos;
131 : std::string fmt_gro_box;
132 : std::string fmt_xyz;
133 : xdrfile::XDRFILE* xd;
134 : public:
135 : explicit DumpAtoms(const ActionOptions&);
136 : ~DumpAtoms();
137 : static void registerKeywords( Keywords& keys );
138 885 : void calculate() override {}
139 885 : void apply() override {}
140 : void update() override ;
141 : };
142 :
143 10599 : PLUMED_REGISTER_ACTION(DumpAtoms,"DUMPATOMS")
144 :
145 91 : void DumpAtoms::registerKeywords( Keywords& keys ) {
146 91 : Action::registerKeywords( keys );
147 91 : ActionPilot::registerKeywords( keys );
148 91 : ActionAtomistic::registerKeywords( keys );
149 182 : keys.add("compulsory","STRIDE","1","the frequency with which the atoms should be output");
150 182 : keys.add("atoms", "ATOMS", "the atom indices whose positions you would like to print out");
151 182 : keys.add("compulsory", "FILE", "file on which to output coordinates; extension is automatically detected");
152 182 : keys.add("compulsory", "UNITS","PLUMED","the units in which to print out the coordinates. PLUMED means internal PLUMED units");
153 182 : keys.add("optional", "PRECISION","The number of digits in trajectory file");
154 182 : keys.add("optional", "TYPE","file type, either xyz, gro, xtc, or trr, can override an automatically detected file extension");
155 91 : keys.use("RESTART");
156 91 : keys.use("UPDATE_FROM");
157 91 : keys.use("UPDATE_UNTIL");
158 91 : }
159 :
160 90 : DumpAtoms::DumpAtoms(const ActionOptions&ao):
161 : Action(ao),
162 : ActionAtomistic(ao),
163 : ActionPilot(ao),
164 90 : iprecision(3)
165 : {
166 : std::vector<AtomNumber> atoms;
167 : std::string file;
168 180 : parse("FILE",file);
169 90 : if(file.length()==0) error("name out output file was not specified");
170 90 : type=Tools::extension(file);
171 90 : log<<" file name "<<file<<"\n";
172 179 : if(type=="gro" || type=="xyz" || type=="xtc" || type=="trr") {
173 74 : log<<" file extension indicates a "<<type<<" file\n";
174 : } else {
175 16 : log<<" file extension not detected, assuming xyz\n";
176 : type="xyz";
177 : }
178 : std::string ntype;
179 180 : parse("TYPE",ntype);
180 90 : if(ntype.length()>0) {
181 5 : if(ntype!="xyz" && ntype!="gro" && ntype!="xtc" && ntype!="trr"
182 0 : ) error("TYPE cannot be understood");
183 2 : log<<" file type enforced to be "<<ntype<<"\n";
184 : type=ntype;
185 : }
186 :
187 : fmt_gro_pos="%8.3f";
188 : fmt_gro_box="%12.7f";
189 : fmt_xyz="%f";
190 :
191 : std::string precision;
192 180 : parse("PRECISION",precision);
193 90 : if(precision.length()>0) {
194 9 : Tools::convert(precision,iprecision);
195 9 : log<<" with precision "<<iprecision<<"\n";
196 : std::string a,b;
197 9 : Tools::convert(iprecision+5,a);
198 9 : Tools::convert(iprecision,b);
199 18 : fmt_gro_pos="%"+a+"."+b+"f";
200 : fmt_gro_box=fmt_gro_pos;
201 : fmt_xyz=fmt_gro_box;
202 : }
203 :
204 180 : parseAtomList("ATOMS",atoms);
205 :
206 180 : std::string unitname; parse("UNITS",unitname);
207 90 : if(unitname!="PLUMED") {
208 4 : Units myunit; myunit.setLength(unitname);
209 6 : if(myunit.getLength()!=1.0 && type=="gro") error("gro files should be in nm");
210 6 : if(myunit.getLength()!=1.0 && type=="xtc") error("xtc files should be in nm");
211 6 : if(myunit.getLength()!=1.0 && type=="trr") error("trr files should be in nm");
212 4 : lenunit=plumed.getAtoms().getUnits().getLength()/myunit.getLength();
213 164 : } else if(type=="gro" || type=="xtc" || type=="trr") lenunit=plumed.getAtoms().getUnits().getLength();
214 30 : else lenunit=1.0;
215 :
216 90 : checkRead();
217 90 : of.link(*this);
218 90 : of.open(file);
219 : std::string path=of.getPath();
220 90 : log<<" Writing on file "<<path<<"\n";
221 : std::string mode=of.getMode();
222 90 : if(type=="xtc") {
223 6 : of.close();
224 6 : xd=xdrfile::xdrfile_open(path.c_str(),mode.c_str());
225 84 : } else if(type=="trr") {
226 4 : of.close();
227 4 : xd=xdrfile::xdrfile_open(path.c_str(),mode.c_str());
228 : }
229 90 : log.printf(" printing the following atoms in %s :", unitname.c_str() );
230 18282 : for(unsigned i=0; i<atoms.size(); ++i) log.printf(" %d",atoms[i].serial() );
231 90 : log.printf("\n");
232 90 : requestAtoms(atoms);
233 90 : auto* moldat=plumed.getActionSet().selectLatest<GenericMolInfo*>(this);
234 90 : if( moldat ) {
235 54 : log<<" MOLINFO DATA found with label " <<moldat->getLabel()<<", using proper atom names\n";
236 54 : names.resize(atoms.size());
237 5587 : for(unsigned i=0; i<atoms.size(); i++) if(atoms[i].index()<moldat->getPDBsize()) names[i]=moldat->getAtomName(atoms[i]);
238 54 : residueNumbers.resize(atoms.size());
239 5587 : for(unsigned i=0; i<residueNumbers.size(); ++i) if(atoms[i].index()<moldat->getPDBsize()) residueNumbers[i]=moldat->getResidueNumber(atoms[i]);
240 54 : residueNames.resize(atoms.size());
241 5587 : for(unsigned i=0; i<residueNames.size(); ++i) if(atoms[i].index()<moldat->getPDBsize()) residueNames[i]=moldat->getResidueName(atoms[i]);
242 : }
243 90 : }
244 :
245 790 : void DumpAtoms::update() {
246 790 : if(type=="xyz") {
247 204 : of.printf("%d\n",getNumberOfAtoms());
248 204 : const Tensor & t(getPbc().getBox());
249 204 : if(getPbc().isOrthorombic()) {
250 336 : of.printf((" "+fmt_xyz+" "+fmt_xyz+" "+fmt_xyz+"\n").c_str(),lenunit*t(0,0),lenunit*t(1,1),lenunit*t(2,2));
251 : } else {
252 72 : of.printf((" "+fmt_xyz+" "+fmt_xyz+" "+fmt_xyz+" "+fmt_xyz+" "+fmt_xyz+" "+fmt_xyz+" "+fmt_xyz+" "+fmt_xyz+" "+fmt_xyz+"\n").c_str(),
253 36 : lenunit*t(0,0),lenunit*t(0,1),lenunit*t(0,2),
254 36 : lenunit*t(1,0),lenunit*t(1,1),lenunit*t(1,2),
255 36 : lenunit*t(2,0),lenunit*t(2,1),lenunit*t(2,2)
256 : );
257 : }
258 35160 : for(unsigned i=0; i<getNumberOfAtoms(); ++i) {
259 : const char* defname="X";
260 : const char* name=defname;
261 34956 : if(names.size()>0) if(names[i].length()>0) name=names[i].c_str();
262 69912 : of.printf(("%s "+fmt_xyz+" "+fmt_xyz+" "+fmt_xyz+"\n").c_str(),name,lenunit*getPosition(i)(0),lenunit*getPosition(i)(1),lenunit*getPosition(i)(2));
263 : }
264 586 : } else if(type=="gro") {
265 466 : const Tensor & t(getPbc().getBox());
266 466 : of.printf("Made with PLUMED t=%f\n",getTime()/plumed.getAtoms().getUnits().getTime());
267 466 : of.printf("%d\n",getNumberOfAtoms());
268 38404 : for(unsigned i=0; i<getNumberOfAtoms(); ++i) {
269 : const char* defname="X";
270 : const char* name=defname;
271 : unsigned residueNumber=0;
272 37938 : if(names.size()>0) if(names[i].length()>0) name=names[i].c_str();
273 37938 : if(residueNumbers.size()>0) residueNumber=residueNumbers[i];
274 37938 : std::string resname="";
275 37938 : if(residueNames.size()>0) resname=residueNames[i];
276 75876 : of.printf(("%5u%-5s%5s%5d"+fmt_gro_pos+fmt_gro_pos+fmt_gro_pos+"\n").c_str(),
277 : residueNumber%100000,resname.c_str(),name,getAbsoluteIndex(i).serial()%100000,
278 37938 : lenunit*getPosition(i)(0),lenunit*getPosition(i)(1),lenunit*getPosition(i)(2));
279 : }
280 932 : of.printf((fmt_gro_box+" "+fmt_gro_box+" "+fmt_gro_box+" "+fmt_gro_box+" "+fmt_gro_box+" "+fmt_gro_box+" "+fmt_gro_box+" "+fmt_gro_box+" "+fmt_gro_box+"\n").c_str(),
281 466 : lenunit*t(0,0),lenunit*t(1,1),lenunit*t(2,2),
282 466 : lenunit*t(0,1),lenunit*t(0,2),lenunit*t(1,0),
283 466 : lenunit*t(1,2),lenunit*t(2,0),lenunit*t(2,1));
284 168 : } else if(type=="xtc" || type=="trr") {
285 : xdrfile::matrix box;
286 120 : const Tensor & t(getPbc().getBox());
287 120 : int natoms=getNumberOfAtoms();
288 120 : int step=getStep();
289 120 : float time=getTime()/plumed.getAtoms().getUnits().getTime();
290 120 : float precision=Tools::fastpow(10.0,iprecision);
291 1560 : for(int i=0; i<3; i++) for(int j=0; j<3; j++) box[i][j]=lenunit*t(i,j);
292 120 : auto pos = Tools::make_unique<xdrfile::rvec[]>(natoms);
293 25464 : for(int i=0; i<natoms; i++) for(int j=0; j<3; j++) pos[i][j]=lenunit*getPosition(i)(j);
294 120 : if(type=="xtc") {
295 72 : write_xtc(xd,natoms,step,time,box,&pos[0],precision);
296 48 : } else if(type=="trr") {
297 48 : write_trr(xd,natoms,step,time,0.0,box,&pos[0],NULL,NULL);
298 : }
299 0 : } else plumed_merror("unknown file type "+type);
300 790 : }
301 :
302 180 : DumpAtoms::~DumpAtoms() {
303 90 : if(type=="xtc") {
304 6 : xdrfile_close(xd);
305 84 : } else if(type=="trr") {
306 4 : xdrfile_close(xd);
307 : }
308 270 : }
309 :
310 :
311 : }
312 : }
|