Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2012-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 "CLTool.h"
23 : #include "CLToolRegister.h"
24 : #include "tools/Tools.h"
25 : #include "wrapper/Plumed.h"
26 : #include "tools/Communicator.h"
27 : #include "tools/Random.h"
28 : #include "tools/Pbc.h"
29 : #include <cstdio>
30 : #include <cstring>
31 : #include <vector>
32 : #include <map>
33 : #include "tools/Units.h"
34 : #include "tools/PDB.h"
35 : #include "tools/FileBase.h"
36 : #include "tools/IFile.h"
37 :
38 : // when using molfile plugin
39 : #ifdef __PLUMED_HAS_MOLFILE_PLUGINS
40 : #ifndef __PLUMED_HAS_EXTERNAL_MOLFILE_PLUGINS
41 : /* Use the internal ones. Alternatively:
42 : * ifeq (,$(findstring __PLUMED_HAS_EXTERNAL_MOLFILE_PLUGINS,$(CPPFLAGS)))
43 : * CPPFLAGS+=-I../molfile
44 : */
45 : #include "molfile/libmolfile_plugin.h"
46 : #include "molfile/molfile_plugin.h"
47 : using namespace PLMD::molfile;
48 : #else
49 : #include <libmolfile_plugin.h>
50 : #include <molfile_plugin.h>
51 : #endif
52 : #endif
53 :
54 : #ifdef __PLUMED_HAS_XDRFILE
55 : #include <xdrfile/xdrfile_trr.h>
56 : #include <xdrfile/xdrfile_xtc.h>
57 : #endif
58 :
59 : using namespace std;
60 :
61 : namespace PLMD {
62 : namespace cltools {
63 :
64 : //+PLUMEDOC TOOLS driver-float
65 : /*
66 : Equivalent to \ref driver, but using single precision reals.
67 :
68 : The purpose of this tool is just to test what PLUMED does when linked from
69 : a single precision code.
70 :
71 : \par Examples
72 :
73 : \verbatim
74 : plumed driver-float --plumed plumed.dat --ixyz trajectory.xyz
75 : \endverbatim
76 :
77 : See also examples in \ref driver
78 :
79 : */
80 : //+ENDPLUMEDOC
81 : //
82 :
83 :
84 : //+PLUMEDOC TOOLS driver
85 : /*
86 : driver is a tool that allows one to to use plumed to post-process an existing trajectory.
87 :
88 : The input to driver is specified using the command line arguments described below.
89 :
90 : In addition, you can use the special \subpage READ command inside your plumed input
91 : to read in colvar files that were generated during your MD simulation. The values
92 : read in can then be treated like calculated colvars.
93 :
94 : \warning
95 : Notice that by default the driver has no knowledge about the masses and charges
96 : of your atoms! Thus, if you want to compute quantities depending charges (e.g. \ref DHENERGY)
97 : or masses (e.g. \ref COM) you should pass the proper information to the driver.
98 : You can do it either with the --pdb option or with the --mc option. The latter
99 : will read a file produced by \ref DUMPMASSCHARGE .
100 :
101 :
102 : \par Examples
103 :
104 : The following command tells plumed to postprocess the trajectory contained in `trajectory.xyz`
105 : by performing the actions described in the input file `plumed.dat`. If an action that takes the
106 : stride keyword is given a stride equal to \f$n\f$ then it will be performed only on every \f$n\f$th
107 : frames in the trajectory file.
108 : \verbatim
109 : plumed driver --plumed plumed.dat --ixyz trajectory.xyz
110 : \endverbatim
111 :
112 : Notice that `xyz` files are expected to be in internal PLUMED units, that is by default nm.
113 : You can change this behavior by using the `--length-units` option:
114 : \verbatim
115 : plumed driver --plumed plumed.dat --ixyz trajectory.xyz --length-units A
116 : \endverbatim
117 : The strings accepted by the `--length-units` options are the same ones accepted by the \ref UNITS action.
118 : Other file formats typically have their default coordinates (e.g., `gro` files are always in nm)
119 : and it thus should not be necessary to use the `--length-units` option. Additionally,
120 : consider that the units used by the `driver` might be different by the units used in the PLUMED input
121 : file `plumed.dat`. For instance consider the command:
122 : \verbatim
123 : plumed driver --plumed plumed.dat --ixyz trajectory.xyz --length-units A
124 : \endverbatim
125 : where `plumed.dat` is
126 : \plumedfile
127 : # no explicit UNITS action here
128 : d: DISTANCE ATOMS=1,2
129 : PRINT ARG=d FILE=colvar
130 : \endplumedfile
131 : In this case, the driver reads the `xyz` file assuming it to contain coordinates in Angstrom units.
132 : However, the resulting `colvar` file contains a distance expressed in nm.
133 :
134 : The following command tells plumed to postprocess the trajectory contained in trajectory.xyz.
135 : by performing the actions described in the input file plumed.dat.
136 : \verbatim
137 : plumed driver --plumed plumed.dat --ixyz trajectory.xyz --trajectory-stride 100 --timestep 0.001
138 : \endverbatim
139 : Here though
140 : `--trajectory-stride` is set equal to the frequency with which frames were output during the trajectory
141 : and the `--timestep` is equal to the simulation timestep. As such the `STRIDE` parameters in the `plumed.dat`
142 : files are referred to the original timestep and any files output resemble those that would have been generated
143 : had we run the calculation we are running with driver when the MD simulation was running.
144 :
145 : PLUMED can read natively xyz files (in PLUMED units) and gro files (in nm). In addition,
146 : PLUMED includes by default support for a
147 : subset of the trajectory file formats supported by VMD, e.g. xtc and dcd:
148 :
149 : \verbatim
150 : plumed driver --plumed plumed.dat --pdb diala.pdb --mf_xtc traj.xtc --trajectory-stride 100 --timestep 0.001
151 : \endverbatim
152 :
153 : where `--mf_` prefixes the extension of one of the accepted molfile plugin format.
154 : If PLUMED has been \ref Installation "installed" with full molfile support, other formats will be available.
155 : Just type `plumed driver --help` to see which plugins are available.
156 :
157 : Molfile plugin require periodic cell to be triangular (i.e. first vector oriented along x and
158 : second vector in xy plane). This is true for many MD codes. However, it could be false
159 : if you rotate the coordinates in your trajectory before reading them in the driver.
160 : Also notice that some formats (e.g. amber crd) do not specify atom number. In this case you can use
161 : the `--natoms` option:
162 : \verbatim
163 : plumed driver --plumed plumed.dat --imf_crd trajectory.crd --natoms 128
164 : \endverbatim
165 :
166 : Check the available molfile plugins and limitations at [this link](http://www.ks.uiuc.edu/Research/vmd/plugins/molfile/).
167 :
168 : Additionally, you can use the xdrfile implementation of xtc and trr. To this aim, just
169 : download and install properly the xdrfile library (see [this link](http://www.gromacs.org/Developer_Zone/Programming_Guide/XTC_Library)).
170 : If the xdrfile library is installed properly the PLUMED configure script should be able to
171 : detect it and enable it.
172 : Notice that the xdrfile implementation of xtc and trr
173 : is more robust than the molfile one, since it provides support for generic cell shapes.
174 : In addition, it allows \ref DUMPATOMS to write compressed xtc files.
175 :
176 :
177 : */
178 : //+ENDPLUMEDOC
179 : //
180 :
181 : #ifdef __PLUMED_HAS_MOLFILE_PLUGINS
182 4839 : static vector<molfile_plugin_t *> plugins;
183 4839 : static map <string, unsigned> pluginmap;
184 29034 : static int register_cb(void *v, vmdplugin_t *p) {
185 : //const char *key = p->name;
186 87102 : const auto ret = pluginmap.insert ( std::pair<string,unsigned>(string(p->name),plugins.size()) );
187 29034 : if (ret.second==false) {
188 : //cerr<<"MOLFILE: found duplicate plugin for "<<key<<" : not inserted "<<endl;
189 : } else {
190 : //cerr<<"MOLFILE: loading plugin "<<key<<" number "<<plugins.size()-1<<endl;
191 58068 : plugins.push_back(reinterpret_cast<molfile_plugin_t *>(p));
192 : }
193 29034 : return VMDPLUGIN_SUCCESS;
194 : }
195 : #endif
196 :
197 : template<typename real>
198 512 : class Driver : public CLTool {
199 : public:
200 : static void registerKeywords( Keywords& keys );
201 : explicit Driver(const CLToolOptions& co );
202 : int main(FILE* in,FILE*out,Communicator& pc);
203 : void evaluateNumericalDerivatives( const long int& step, Plumed& p, const std::vector<real>& coordinates,
204 : const std::vector<real>& masses, const std::vector<real>& charges,
205 : std::vector<real>& cell, const double& base, std::vector<real>& numder );
206 : string description()const;
207 : };
208 :
209 : template<typename real>
210 3226 : void Driver<real>::registerKeywords( Keywords& keys ) {
211 3226 : CLTool::registerKeywords( keys ); keys.isDriver();
212 9678 : keys.addFlag("--help-debug",false,"print special options that can be used to create regtests");
213 16130 : keys.add("compulsory","--plumed","plumed.dat","specify the name of the plumed input file");
214 16130 : keys.add("compulsory","--timestep","1.0","the timestep that was used in the calculation that produced this trajectory in picoseconds");
215 16130 : keys.add("compulsory","--trajectory-stride","1","the frequency with which frames were output to this trajectory during the simulation"
216 : #ifdef __PLUMED_HAS_XDRFILE
217 : " (0 means that the number of the step is read from the trajectory file,"
218 : " currently working only for xtc/trr files read with --ixtc/--trr)"
219 : #endif
220 : );
221 16130 : keys.add("compulsory","--multi","0","set number of replicas for multi environment (needs mpi)");
222 9678 : keys.addFlag("--noatoms",false,"don't read in a trajectory. Just use colvar files as specified in plumed.dat");
223 12904 : keys.add("atoms","--ixyz","the trajectory in xyz format");
224 12904 : keys.add("atoms","--igro","the trajectory in gro format");
225 : #ifdef __PLUMED_HAS_XDRFILE
226 12904 : keys.add("atoms","--ixtc","the trajectory in xtc format (xdrfile implementation)");
227 12904 : keys.add("atoms","--itrr","the trajectory in trr format (xdrfile implementation)");
228 : #endif
229 12904 : keys.add("optional","--length-units","units for length, either as a string or a number");
230 12904 : keys.add("optional","--mass-units","units for mass in pdb and mc file, either as a string or a number");
231 12904 : keys.add("optional","--charge-units","units for charge in pdb and mc file, either as a string or a number");
232 12904 : keys.add("optional","--kt","set kBT, it will not be necessary to specify temperature in input file");
233 12904 : keys.add("optional","--dump-forces","dump the forces on a file");
234 12904 : keys.add("optional","--dump-forces-fmt","( default=%%f ) the format to use to dump the forces");
235 9678 : keys.addFlag("--dump-full-virial",false,"with --dump-forces, it dumps the 9 components of the virial");
236 12904 : keys.add("optional","--pdb","provides a pdb with masses and charges");
237 12904 : keys.add("optional","--mc","provides a file with masses and charges as produced with DUMPMASSCHARGE");
238 12904 : keys.add("optional","--box","comma-separated box dimensions (3 for orthorombic, 9 for generic)");
239 12904 : keys.add("optional","--natoms","provides number of atoms - only used if file format does not contain number of atoms");
240 12904 : keys.add("optional","--initial-step","provides a number for the initial step, default is 0");
241 12904 : keys.add("optional","--debug-forces","output a file containing the forces due to the bias evaluated using numerical derivatives "
242 : "and using the analytical derivatives implemented in plumed");
243 12904 : keys.add("hidden","--debug-float","[yes/no] turns on the single precision version (to check float interface)");
244 12904 : keys.add("hidden","--debug-dd","[yes/no] use a fake domain decomposition");
245 12904 : keys.add("hidden","--debug-pd","[yes/no] use a fake particle decomposition");
246 12904 : keys.add("hidden","--debug-grex","use a fake gromacs-like replica exchange, specify exchange stride");
247 12904 : keys.add("hidden","--debug-grex-log","log file for debug=grex");
248 : #ifdef __PLUMED_HAS_MOLFILE_PLUGINS
249 3226 : MOLFILE_INIT_ALL
250 3226 : MOLFILE_REGISTER_ALL(NULL, register_cb)
251 93554 : for(int i=0; i<plugins.size(); i++) {
252 58068 : string kk="--mf_"+string(plugins[i]->name);
253 87102 : string mm=" molfile: the trajectory in "+string(plugins[i]->name)+" format " ;
254 : //cerr<<"REGISTERING "<<kk<<mm<<endl;
255 58068 : keys.add("atoms",kk,mm);
256 : }
257 : #endif
258 3226 : }
259 : template<typename real>
260 512 : Driver<real>::Driver(const CLToolOptions& co ):
261 512 : CLTool(co)
262 : {
263 512 : inputdata=commandline;
264 512 : }
265 : template<typename real>
266 0 : string Driver<real>::description()const { return "analyze trajectories with plumed"; }
267 :
268 : template<typename real>
269 512 : int Driver<real>::main(FILE* in,FILE*out,Communicator& pc) {
270 :
271 1024 : Units units;
272 1024 : PDB pdb;
273 :
274 : // Parse everything
275 1024 : bool printhelpdebug; parseFlag("--help-debug",printhelpdebug);
276 512 : if( printhelpdebug ) {
277 : fprintf(out,"%s",
278 : "Additional options for debug (only to be used in regtest):\n"
279 : " [--debug-float yes] : turns on the single precision version (to check float interface)\n"
280 : " [--debug-dd yes] : use a fake domain decomposition\n"
281 : " [--debug-pd yes] : use a fake particle decomposition\n"
282 : );
283 : return 0;
284 : }
285 : // Are we reading trajectory data
286 1024 : bool noatoms; parseFlag("--noatoms",noatoms);
287 :
288 : std::string fakein;
289 :
290 : bool debug_float=false;
291 : fakein="";
292 1024 : if(parse("--debug-float",fakein)) {
293 0 : if(fakein=="yes") debug_float=true;
294 0 : else if(fakein=="no") debug_float=false;
295 0 : else error("--debug-float should have argument yes or no");
296 : }
297 :
298 : if(debug_float && sizeof(real)!=sizeof(float)) {
299 0 : CLTool* cl=cltoolRegister().create(CLToolOptions("driver-float")); //new Driver<float>(*this);
300 : cl->setInputData(this->getInputData());
301 0 : int ret=cl->main(in,out,pc);
302 0 : delete cl;
303 : return ret;
304 : }
305 :
306 : bool debug_pd=false;
307 : fakein="";
308 1024 : if(parse("--debug-pd",fakein)) {
309 8 : if(fakein=="yes") debug_pd=true;
310 0 : else if(fakein=="no") debug_pd=false;
311 0 : else error("--debug-pd should have argument yes or no");
312 : }
313 : if(debug_pd) fprintf(out,"DEBUGGING PARTICLE DECOMPOSITION\n");
314 :
315 : bool debug_dd=false;
316 : fakein="";
317 1024 : if(parse("--debug-dd",fakein)) {
318 52 : if(fakein=="yes") debug_dd=true;
319 0 : else if(fakein=="no") debug_dd=false;
320 0 : else error("--debug-dd should have argument yes or no");
321 : }
322 : if(debug_dd) fprintf(out,"DEBUGGING DOMAIN DECOMPOSITION\n");
323 :
324 512 : if( debug_pd || debug_dd ) {
325 60 : if(noatoms) error("cannot debug without atoms");
326 : }
327 :
328 : // set up for multi replica driver:
329 512 : int multi=0;
330 1024 : parse("--multi",multi);
331 1024 : Communicator intracomm;
332 1024 : Communicator intercomm;
333 512 : if(multi) {
334 122 : int ntot=pc.Get_size();
335 122 : int nintra=ntot/multi;
336 122 : if(multi*nintra!=ntot) error("invalid number of processes for multi environment");
337 122 : pc.Split(pc.Get_rank()/nintra,pc.Get_rank(),intracomm);
338 122 : pc.Split(pc.Get_rank()%nintra,pc.Get_rank(),intercomm);
339 : } else {
340 390 : intracomm.Set_comm(pc.Get_comm());
341 : }
342 :
343 : // set up for debug replica exchange:
344 1024 : bool debug_grex=parse("--debug-grex",fakein);
345 512 : int grex_stride=0;
346 : FILE*grex_log=NULL;
347 512 : if(debug_grex) {
348 24 : if(noatoms) error("must have atoms to debug_grex");
349 24 : if(multi<2) error("--debug_grex needs --multi with at least two replicas");
350 24 : Tools::convert(fakein,grex_stride);
351 24 : string n; Tools::convert(intercomm.Get_rank(),n);
352 : string file;
353 48 : parse("--debug-grex-log",file);
354 24 : if(file.length()>0) {
355 48 : file+="."+n;
356 24 : grex_log=fopen(file.c_str(),"w");
357 : }
358 : }
359 :
360 : // Read the plumed input file name
361 1024 : string plumedFile; parse("--plumed",plumedFile);
362 : // the timestep
363 1024 : double t; parse("--timestep",t);
364 512 : real timestep=real(t);
365 : // the stride
366 1024 : unsigned stride; parse("--trajectory-stride",stride);
367 : // are we writing forces
368 512 : string dumpforces(""), debugforces(""), dumpforcesFmt("%f");;
369 512 : bool dumpfullvirial=false;
370 512 : if(!noatoms) {
371 962 : parse("--dump-forces",dumpforces);
372 962 : parse("--debug-forces",debugforces);
373 : }
374 1033 : if(dumpforces!="" || debugforces!="" ) parse("--dump-forces-fmt",dumpforcesFmt);
375 839 : if(dumpforces!="") parseFlag("--dump-full-virial",dumpfullvirial);
376 512 : if( debugforces!="" && (debug_dd || debug_pd) ) error("cannot debug forces and domain/particle decomposition at same time");
377 0 : if( debugforces!="" && sizeof(real)!=sizeof(double) ) error("cannot debug forces in single precision mode");
378 :
379 512 : real kt=-1.0;
380 1024 : parse("--kt",kt);
381 : string trajectory_fmt;
382 :
383 : bool use_molfile=false;
384 : #ifdef __PLUMED_HAS_MOLFILE_PLUGINS
385 : molfile_plugin_t *api=NULL;
386 : void *h_in=NULL;
387 : molfile_timestep_t ts_in; // this is the structure that has the timestep
388 512 : ts_in.coords=NULL;
389 512 : ts_in.velocities=NULL;
390 512 : ts_in.A=-1; // we use this to check whether cell is provided or not
391 : #endif
392 :
393 : // Read in an xyz file
394 512 : string trajectoryFile(""), pdbfile(""), mcfile("");
395 512 : bool pbc_cli_given=false; vector<double> pbc_cli_box(9,0.0);
396 512 : int command_line_natoms=-1;
397 :
398 512 : if(!noatoms) {
399 962 : std::string traj_xyz; parse("--ixyz",traj_xyz);
400 962 : std::string traj_gro; parse("--igro",traj_gro);
401 : std::string traj_xtc;
402 : std::string traj_trr;
403 : #ifdef __PLUMED_HAS_XDRFILE
404 962 : parse("--ixtc",traj_xtc);
405 962 : parse("--itrr",traj_trr);
406 : #endif
407 : #ifdef __PLUMED_HAS_MOLFILE_PLUGINS
408 13949 : for(int i=0; i<plugins.size(); i++) {
409 8658 : string molfile_key="--mf_"+string(plugins[i]->name);
410 : string traj_molfile;
411 4329 : parse(molfile_key,traj_molfile);
412 4329 : if(traj_molfile.length()>0) {
413 54 : fprintf(out,"\nDRIVER: Found molfile format trajectory %s with name %s\n",plugins[i]->name,traj_molfile.c_str());
414 : trajectoryFile=traj_molfile;
415 108 : trajectory_fmt=string(plugins[i]->name);
416 : use_molfile=true;
417 54 : api = plugins[i];
418 : }
419 : }
420 : #endif
421 : { // check that only one fmt is specified
422 : int nn=0;
423 481 : if(traj_xyz.length()>0) nn++;
424 481 : if(traj_gro.length()>0) nn++;
425 481 : if(traj_xtc.length()>0) nn++;
426 481 : if(traj_trr.length()>0) nn++;
427 481 : if(nn>1) {
428 0 : fprintf(stderr,"ERROR: cannot provide more than one trajectory file\n");
429 0 : if(grex_log)fclose(grex_log);
430 : return 1;
431 : }
432 : }
433 804 : if(traj_xyz.length()>0 && trajectoryFile.length()==0) {
434 : trajectoryFile=traj_xyz;
435 : trajectory_fmt="xyz";
436 : }
437 580 : if(traj_gro.length()>0 && trajectoryFile.length()==0) {
438 : trajectoryFile=traj_gro;
439 : trajectory_fmt="gro";
440 : }
441 483 : if(traj_xtc.length()>0 && trajectoryFile.length()==0) {
442 : trajectoryFile=traj_xtc;
443 : trajectory_fmt="xdr-xtc";
444 : }
445 484 : if(traj_trr.length()>0 && trajectoryFile.length()==0) {
446 : trajectoryFile=traj_trr;
447 : trajectory_fmt="xdr-trr";
448 : }
449 481 : if(trajectoryFile.length()==0) {
450 0 : fprintf(stderr,"ERROR: missing trajectory data\n");
451 0 : if(grex_log)fclose(grex_log);
452 : return 1;
453 : }
454 962 : string lengthUnits(""); parse("--length-units",lengthUnits);
455 481 : if(lengthUnits.length()>0) units.setLength(lengthUnits);
456 962 : string chargeUnits(""); parse("--charge-units",chargeUnits);
457 481 : if(chargeUnits.length()>0) units.setCharge(chargeUnits);
458 962 : string massUnits(""); parse("--mass-units",massUnits);
459 481 : if(massUnits.length()>0) units.setMass(massUnits);
460 :
461 962 : parse("--pdb",pdbfile);
462 481 : if(pdbfile.length()>0) {
463 57 : bool check=pdb.read(pdbfile,false,1.0);
464 57 : if(!check) error("error reading pdb file");
465 : }
466 :
467 962 : parse("--mc",mcfile);
468 :
469 962 : string pbc_cli_list; parse("--box",pbc_cli_list);
470 481 : if(pbc_cli_list.length()>0) {
471 : pbc_cli_given=true;
472 34 : vector<string> words=Tools::getWords(pbc_cli_list,",");
473 17 : if(words.size()==3) {
474 170 : for(int i=0; i<3; i++) sscanf(words[i].c_str(),"%100lf",&(pbc_cli_box[4*i]));
475 0 : } else if(words.size()==9) {
476 0 : for(int i=0; i<9; i++) sscanf(words[i].c_str(),"%100lf",&(pbc_cli_box[i]));
477 : } else {
478 0 : string msg="ERROR: cannot parse command-line box "+pbc_cli_list;
479 0 : fprintf(stderr,"%s\n",msg.c_str());
480 : return 1;
481 : }
482 :
483 : }
484 :
485 962 : parse("--natoms",command_line_natoms);
486 :
487 : }
488 :
489 :
490 512 : if(debug_dd && debug_pd) error("cannot use debug-dd and debug-pd at the same time");
491 512 : if(debug_pd || debug_dd) {
492 60 : if( !Communicator::initialized() ) error("needs mpi for debug-pd");
493 : }
494 :
495 : Plumed p;
496 512 : int rr=sizeof(real);
497 : p.cmd("setRealPrecision",&rr);
498 : int checknatoms=-1;
499 512 : long int step=0;
500 1024 : parse("--initial-step",step);
501 :
502 512 : if(Communicator::initialized()) {
503 212 : if(multi) {
504 122 : if(intracomm.Get_rank()==0) p.cmd("GREX setMPIIntercomm",&intercomm.Get_comm());
505 122 : p.cmd("GREX setMPIIntracomm",&intracomm.Get_comm());
506 : p.cmd("GREX init");
507 : }
508 212 : p.cmd("setMPIComm",&intracomm.Get_comm());
509 : }
510 : p.cmd("setMDLengthUnits",&units.getLength());
511 : p.cmd("setMDChargeUnits",&units.getCharge());
512 : p.cmd("setMDMassUnits",&units.getMass());
513 : p.cmd("setMDEngine","driver");
514 : p.cmd("setTimestep",×tep);
515 : p.cmd("setPlumedDat",plumedFile.c_str());
516 : p.cmd("setLog",out);
517 :
518 : int natoms;
519 :
520 1024 : FILE* fp=NULL; FILE* fp_forces=NULL; OFile fp_dforces;
521 : #ifdef __PLUMED_HAS_XDRFILE
522 : XDRFILE* xd=NULL;
523 : #endif
524 512 : if(!noatoms) {
525 481 : if (trajectoryFile=="-")
526 : fp=in;
527 : else {
528 481 : if(multi) {
529 : string n;
530 122 : Tools::convert(intercomm.Get_rank(),n);
531 244 : std::string testfile=FileBase::appendSuffix(trajectoryFile,"."+n);
532 122 : FILE* tmp_fp=fopen(testfile.c_str(),"r");
533 122 : if(tmp_fp) { fclose(tmp_fp); trajectoryFile=testfile.c_str();}
534 : }
535 481 : if(use_molfile==true) {
536 : #ifdef __PLUMED_HAS_MOLFILE_PLUGINS
537 108 : h_in = api->open_file_read(trajectoryFile.c_str(), trajectory_fmt.c_str(), &natoms);
538 54 : if(natoms==MOLFILE_NUMATOMS_UNKNOWN) {
539 2 : if(command_line_natoms>=0) natoms=command_line_natoms;
540 0 : else error("this file format does not provide number of atoms; use --natoms on the command line");
541 : }
542 54 : ts_in.coords = new float [3*natoms];
543 : #endif
544 852 : } else if(trajectory_fmt=="xdr-xtc" || trajectory_fmt=="xdr-trr") {
545 : #ifdef __PLUMED_HAS_XDRFILE
546 5 : xd=xdrfile_open(trajectoryFile.c_str(),"r");
547 5 : if(!xd) {
548 0 : string msg="ERROR: Error opening trajectory file "+trajectoryFile;
549 0 : fprintf(stderr,"%s\n",msg.c_str());
550 : return 1;
551 : }
552 7 : if(trajectory_fmt=="xdr-xtc") read_xtc_natoms(&trajectoryFile[0],&natoms);
553 8 : if(trajectory_fmt=="xdr-trr") read_trr_natoms(&trajectoryFile[0],&natoms);
554 : #endif
555 : } else {
556 422 : fp=fopen(trajectoryFile.c_str(),"r");
557 422 : if(!fp) {
558 0 : string msg="ERROR: Error opening trajectory file "+trajectoryFile;
559 0 : fprintf(stderr,"%s\n",msg.c_str());
560 : // cppcheck detects a false positive here. I suppress it:
561 : // cppcheck-suppress memleak
562 : return 1;
563 : }
564 : }
565 : }
566 481 : if(dumpforces.length()>0) {
567 327 : if(Communicator::initialized() && pc.Get_size()>1) {
568 : string n;
569 160 : Tools::convert(pc.Get_rank(),n);
570 320 : dumpforces+="."+n;
571 : }
572 327 : fp_forces=fopen(dumpforces.c_str(),"w");
573 : }
574 481 : if(debugforces.length()>0) {
575 9 : if(Communicator::initialized() && pc.Get_size()>1) {
576 : string n;
577 6 : Tools::convert(pc.Get_rank(),n);
578 12 : debugforces+="."+n;
579 : }
580 9 : fp_dforces.open(debugforces);
581 : }
582 : }
583 :
584 : std::string line;
585 : std::vector<real> coordinates;
586 : std::vector<real> forces;
587 : std::vector<real> masses;
588 : std::vector<real> charges;
589 : std::vector<real> cell;
590 : std::vector<real> virial;
591 : std::vector<real> numder;
592 :
593 : // variables to test particle decomposition
594 : int pd_nlocal;
595 : int pd_start;
596 : // variables to test random decomposition (=domain decomposition)
597 : std::vector<int> dd_gatindex;
598 : std::vector<int> dd_g2l;
599 : std::vector<real> dd_masses;
600 : std::vector<real> dd_charges;
601 : std::vector<real> dd_forces;
602 : std::vector<real> dd_coordinates;
603 : int dd_nlocal;
604 : // random stream to choose decompositions
605 512 : Random rnd;
606 :
607 24990 : while(true) {
608 25502 : if(!noatoms) {
609 24826 : if(use_molfile==true) {
610 : #ifdef __PLUMED_HAS_MOLFILE_PLUGINS
611 : int rc;
612 2373 : rc = api->read_next_timestep(h_in, natoms, &ts_in);
613 : //if(rc==MOLFILE_SUCCESS){
614 : // printf(" read this one :success \n");
615 : //}
616 2373 : if(rc==MOLFILE_EOF) {
617 : //printf(" read this one :eof or error \n");
618 : break;
619 : }
620 : #endif
621 24636 : } else if(trajectory_fmt=="xyz" || trajectory_fmt=="gro") {
622 22384 : if(!Tools::getline(fp,line)) break;
623 : }
624 : }
625 :
626 : bool first_step=false;
627 25026 : if(!noatoms) {
628 48465 : if(use_molfile==false && (trajectory_fmt=="xyz" || trajectory_fmt=="gro")) {
629 21962 : if(trajectory_fmt=="gro") if(!Tools::getline(fp,line)) error("premature end of trajectory file");
630 21962 : sscanf(line.c_str(),"%100d",&natoms);
631 : }
632 : }
633 25026 : if(checknatoms<0 && !noatoms) {
634 481 : pd_nlocal=natoms;
635 481 : pd_start=0;
636 : first_step=true;
637 962 : masses.assign(natoms,NAN);
638 962 : charges.assign(natoms,NAN);
639 : //case pdb: structure
640 481 : if(pdbfile.length()>0) {
641 10845 : for(unsigned i=0; i<pdb.size(); ++i) {
642 10788 : AtomNumber an=pdb.getAtomNumbers()[i];
643 : unsigned index=an.index();
644 5394 : if( index>=unsigned(natoms) ) error("atom index in pdb exceeds the number of atoms in trajectory");
645 16182 : masses[index]=pdb.getOccupancy()[i];
646 10788 : charges[index]=pdb.getBeta()[i];
647 : }
648 : }
649 481 : if(mcfile.length()>0) {
650 8 : IFile ifile;
651 4 : ifile.open(mcfile);
652 : int index; double mass; double charge;
653 2176 : while(ifile.scanField("index",index).scanField("mass",mass).scanField("charge",charge).scanField()) {
654 864 : masses[index]=mass;
655 864 : charges[index]=charge;
656 : }
657 : }
658 24545 : } else if( checknatoms<0 && noatoms ) {
659 31 : natoms=0;
660 : }
661 25026 : if( checknatoms<0 ) {
662 512 : if(kt>=0) {
663 : p.cmd("setKbT",&kt);
664 : }
665 512 : checknatoms=natoms;
666 : p.cmd("setNatoms",&natoms);
667 : p.cmd("init");
668 : }
669 25026 : if(checknatoms!=natoms) {
670 0 : std::string stepstr; Tools::convert(step,stepstr);
671 0 : error("number of atoms in frame " + stepstr + " does not match number of atoms in first frame");
672 : }
673 :
674 50052 : coordinates.assign(3*natoms,real(0.0));
675 50052 : forces.assign(3*natoms,real(0.0));
676 50052 : cell.assign(9,real(0.0));
677 50052 : virial.assign(9,real(0.0));
678 :
679 25026 : if( first_step || rnd.U01()>0.5) {
680 13099 : if(debug_pd) {
681 136 : int npe=intracomm.Get_size();
682 136 : vector<int> loc(npe,0);
683 136 : vector<int> start(npe,0);
684 952 : for(int i=0; i<npe-1; i++) {
685 408 : int cc=(natoms*2*rnd.U01())/npe;
686 816 : if(start[i]+cc>natoms) cc=natoms-start[i];
687 408 : loc[i]=cc;
688 1224 : start[i+1]=start[i]+loc[i];
689 : }
690 408 : loc[npe-1]=natoms-start[npe-1];
691 136 : intracomm.Bcast(loc,0);
692 136 : intracomm.Bcast(start,0);
693 272 : pd_nlocal=loc[intracomm.Get_rank()];
694 272 : pd_start=start[intracomm.Get_rank()];
695 136 : if(intracomm.Get_rank()==0) {
696 : fprintf(out,"\nDRIVER: Reassigning particle decomposition\n");
697 442 : fprintf(out,"DRIVER: "); for(int i=0; i<npe; i++) fprintf(out,"%d ",loc[i]); printf("\n");
698 442 : fprintf(out,"DRIVER: "); for(int i=0; i<npe; i++) fprintf(out,"%d ",start[i]); printf("\n");
699 : }
700 : p.cmd("setAtomsNlocal",&pd_nlocal);
701 : p.cmd("setAtomsContiguous",&pd_start);
702 12963 : } else if(debug_dd) {
703 910 : int npe=intracomm.Get_size();
704 910 : int rank=intracomm.Get_rank();
705 1820 : dd_charges.assign(natoms,0.0);
706 1820 : dd_masses.assign(natoms,0.0);
707 1820 : dd_gatindex.assign(natoms,-1);
708 1820 : dd_g2l.assign(natoms,-1);
709 1820 : dd_coordinates.assign(3*natoms,0.0);
710 1820 : dd_forces.assign(3*natoms,0.0);
711 910 : dd_nlocal=0;
712 96154 : for(int i=0; i<natoms; ++i) {
713 47622 : double r=rnd.U01()*npe;
714 104582 : int n; for(n=0; n<npe; n++) if(n+1>r)break;
715 47622 : plumed_assert(n<npe);
716 47622 : if(n==rank) {
717 34446 : dd_gatindex[dd_nlocal]=i;
718 34446 : dd_g2l[i]=dd_nlocal;
719 34446 : dd_charges[dd_nlocal]=charges[i];
720 34446 : dd_masses[dd_nlocal]=masses[i];
721 17223 : dd_nlocal++;
722 : }
723 : }
724 910 : if(intracomm.Get_rank()==0) {
725 : fprintf(out,"\nDRIVER: Reassigning domain decomposition\n");
726 : }
727 : p.cmd("setAtomsNlocal",&dd_nlocal);
728 : p.cmd("setAtomsGatindex",&dd_gatindex[0]);
729 : }
730 : }
731 :
732 25026 : int plumedStopCondition=0;
733 25026 : if(!noatoms) {
734 24350 : if(use_molfile) {
735 : #ifdef __PLUMED_HAS_MOLFILE_PLUGINS
736 2319 : if(pbc_cli_given==false) {
737 2316 : if(ts_in.A>0.0) { // this is negative if molfile does not provide box
738 : // info on the cell: convert using pbcset.tcl from pbctools in vmd distribution
739 2313 : real cosBC=cos(ts_in.alpha*pi/180.);
740 : //double sinBC=sin(ts_in.alpha*pi/180.);
741 2313 : real cosAC=cos(ts_in.beta*pi/180.);
742 2313 : real cosAB=cos(ts_in.gamma*pi/180.);
743 2313 : real sinAB=sin(ts_in.gamma*pi/180.);
744 2313 : real Ax=ts_in.A;
745 2313 : real Bx=ts_in.B*cosAB;
746 2313 : real By=ts_in.B*sinAB;
747 2313 : real Cx=ts_in.C*cosAC;
748 2313 : real Cy=(ts_in.C*ts_in.B*cosBC-Cx*Bx)/By;
749 2313 : real Cz=sqrt(ts_in.C*ts_in.C-Cx*Cx-Cy*Cy);
750 4626 : cell[0]=Ax/10.; cell[1]=0.; cell[2]=0.;
751 2313 : cell[3]=Bx/10.; cell[4]=By/10.; cell[5]=0.;
752 2313 : cell[6]=Cx/10.; cell[7]=Cy/10.; cell[8]=Cz/10.;
753 : } else {
754 6 : cell[0]=0.0; cell[1]=0.0; cell[2]=0.0;
755 3 : cell[3]=0.0; cell[4]=0.0; cell[5]=0.0;
756 3 : cell[6]=0.0; cell[7]=0.0; cell[8]=0.0;
757 : }
758 : } else {
759 57 : for(unsigned i=0; i<9; i++)cell[i]=pbc_cli_box[i];
760 : }
761 : // info on coords
762 : // the order is xyzxyz...
763 4567677 : for(unsigned i=0; i<3*natoms; i++) {
764 4565358 : coordinates[i]=real(ts_in.coords[i]/10.); //convert to nm
765 : //cerr<<"COOR "<<coordinates[i]<<endl;
766 : }
767 : #endif
768 44032 : } else if(trajectory_fmt=="xdr-xtc" || trajectory_fmt=="xdr-trr") {
769 : #ifdef __PLUMED_HAS_XDRFILE
770 : int localstep;
771 : float time;
772 : matrix box;
773 69 : rvec* pos=new rvec[natoms];
774 : float prec,lambda;
775 : int ret=exdrOK;
776 69 : if(trajectory_fmt=="xdr-xtc") ret=read_xtc(xd,natoms,&localstep,&time,box,pos,&prec);
777 69 : if(trajectory_fmt=="xdr-trr") ret=read_trr(xd,natoms,&localstep,&time,&lambda,box,pos,NULL,NULL);
778 69 : if(stride==0) step=localstep;
779 69 : if(ret==exdrENDOFFILE) break;
780 67 : if(ret!=exdrOK) break;
781 832 : for(unsigned i=0; i<3; i++) for(unsigned j=0; j<3; j++) cell[3*i+j]=box[i][j];
782 10976 : for(unsigned i=0; i<natoms; i++) for(unsigned j=0; j<3; j++)
783 16368 : coordinates[3*i+j]=real(pos[i][j]);
784 64 : delete [] pos;
785 : #endif
786 : } else {
787 21962 : if(trajectory_fmt=="xyz") {
788 19947 : if(!Tools::getline(fp,line)) error("premature end of trajectory file");
789 :
790 19947 : std::vector<double> celld(9,0.0);
791 19947 : if(pbc_cli_given==false) {
792 19747 : std::vector<std::string> words;
793 39494 : words=Tools::getWords(line);
794 19747 : if(words.size()==3) {
795 19285 : sscanf(line.c_str(),"%100lf %100lf %100lf",&celld[0],&celld[4],&celld[8]);
796 462 : } else if(words.size()==9) {
797 462 : sscanf(line.c_str(),"%100lf %100lf %100lf %100lf %100lf %100lf %100lf %100lf %100lf",
798 : &celld[0], &celld[1], &celld[2],
799 : &celld[3], &celld[4], &celld[5],
800 : &celld[6], &celld[7], &celld[8]);
801 0 : } else error("needed box in second line of xyz file");
802 : } else { // from command line
803 200 : celld=pbc_cli_box;
804 : }
805 378993 : for(unsigned i=0; i<9; i++)cell[i]=real(celld[i]);
806 : }
807 : int ddist=0;
808 : // Read coordinates
809 3331894 : for(int i=0; i<natoms; i++) {
810 1654966 : bool ok=Tools::getline(fp,line);
811 1654966 : if(!ok) error("premature end of trajectory file");
812 : double cc[3];
813 1654966 : if(trajectory_fmt=="xyz") {
814 : char dummy[1000];
815 1579770 : int ret=std::sscanf(line.c_str(),"%999s %100lf %100lf %100lf",dummy,&cc[0],&cc[1],&cc[2]);
816 1579770 : if(ret!=4) error("cannot read line"+line);
817 75196 : } else if(trajectory_fmt=="gro") {
818 : // do the gromacs way
819 75196 : if(!i) {
820 : //
821 : // calculate the distance between dots (as in gromacs gmxlib/confio.c, routine get_w_conf )
822 : //
823 : const char *p1, *p2, *p3;
824 : p1 = strchr(line.c_str(), '.');
825 2015 : if (p1 == NULL) error("seems there are no coordinates in the gro file");
826 2015 : p2 = strchr(&p1[1], '.');
827 2015 : if (p2 == NULL) error("seems there is only one coordinates in the gro file");
828 2015 : ddist = p2 - p1;
829 2015 : p3 = strchr(&p2[1], '.');
830 2015 : if (p3 == NULL)error("seems there are only two coordinates in the gro file");
831 2015 : if (p3 - p2 != ddist)error("not uniform spacing in fields in the gro file");
832 : }
833 150392 : Tools::convert(line.substr(20,ddist),cc[0]);
834 150392 : Tools::convert(line.substr(20+ddist,ddist),cc[1]);
835 150392 : Tools::convert(line.substr(20+ddist+ddist,ddist),cc[2]);
836 0 : } else plumed_error();
837 1654966 : if(!debug_pd || ( i>=pd_start && i<pd_start+pd_nlocal) ) {
838 3274292 : coordinates[3*i]=real(cc[0]);
839 3274292 : coordinates[3*i+1]=real(cc[1]);
840 3274292 : coordinates[3*i+2]=real(cc[2]);
841 : }
842 : }
843 21962 : if(trajectory_fmt=="gro") {
844 2015 : if(!Tools::getline(fp,line)) error("premature end of trajectory file");
845 4030 : std::vector<string> words=Tools::getWords(line);
846 2015 : if(words.size()<3) error("cannot understand box format");
847 2015 : Tools::convert(words[0],cell[0]);
848 2015 : Tools::convert(words[1],cell[4]);
849 2015 : Tools::convert(words[2],cell[8]);
850 2233 : if(words.size()>3) Tools::convert(words[3],cell[1]);
851 2233 : if(words.size()>4) Tools::convert(words[4],cell[2]);
852 2233 : if(words.size()>5) Tools::convert(words[5],cell[3]);
853 2233 : if(words.size()>6) Tools::convert(words[6],cell[5]);
854 2233 : if(words.size()>7) Tools::convert(words[7],cell[6]);
855 2233 : if(words.size()>8) Tools::convert(words[8],cell[7]);
856 : }
857 :
858 : }
859 :
860 : p.cmd("setStepLong",&step);
861 : p.cmd("setStopFlag",&plumedStopCondition);
862 :
863 24345 : if(debug_dd) {
864 66368 : for(int i=0; i<dd_nlocal; ++i) {
865 64664 : int kk=dd_gatindex[i];
866 96996 : dd_coordinates[3*i+0]=coordinates[3*kk+0];
867 96996 : dd_coordinates[3*i+1]=coordinates[3*kk+1];
868 96996 : dd_coordinates[3*i+2]=coordinates[3*kk+2];
869 : }
870 : p.cmd("setForces",&dd_forces[0]);
871 : p.cmd("setPositions",&dd_coordinates[0]);
872 : p.cmd("setMasses",&dd_masses[0]);
873 : p.cmd("setCharges",&dd_charges[0]);
874 : } else {
875 : // this is required to avoid troubles when the last domain
876 : // contains zero atoms
877 : // Basically, for empty domains we pass null pointers
878 : #define fix_pd(xx) (pd_nlocal!=0?&xx:NULL)
879 22641 : p.cmd("setForces",fix_pd(forces[3*pd_start]));
880 22641 : p.cmd("setPositions",fix_pd(coordinates[3*pd_start]));
881 22641 : p.cmd("setMasses",fix_pd(masses[pd_start]));
882 22641 : p.cmd("setCharges",fix_pd(charges[pd_start]));
883 : }
884 : p.cmd("setBox",&cell[0]);
885 : p.cmd("setVirial",&virial[0]);
886 : } else {
887 : p.cmd("setStepLong",&step);
888 : p.cmd("setStopFlag",&plumedStopCondition);
889 : }
890 : p.cmd("calc");
891 :
892 : // this is necessary as only processor zero is adding to the virial:
893 25021 : intracomm.Bcast(virial,0);
894 25021 : if(debug_pd) intracomm.Sum(forces);
895 25021 : if(debug_dd) {
896 66368 : for(int i=0; i<dd_nlocal; i++) {
897 129328 : forces[3*dd_gatindex[i]+0]=dd_forces[3*i+0];
898 96996 : forces[3*dd_gatindex[i]+1]=dd_forces[3*i+1];
899 96996 : forces[3*dd_gatindex[i]+2]=dd_forces[3*i+2];
900 : }
901 3408 : dd_forces.assign(3*natoms,0.0);
902 1704 : intracomm.Sum(forces);
903 : }
904 25021 : if(debug_grex &&step%grex_stride==0) {
905 : p.cmd("GREX savePositions");
906 84 : if(intracomm.Get_rank()>0) {
907 : p.cmd("GREX prepare");
908 : } else {
909 42 : int r=intercomm.Get_rank();
910 42 : int n=intercomm.Get_size();
911 42 : int partner=r+(2*((r+step/grex_stride)%2))-1;
912 42 : if(partner<0)partner=0;
913 42 : if(partner>=n) partner=n-1;
914 : p.cmd("GREX setPartner",&partner);
915 : p.cmd("GREX calculate");
916 : p.cmd("GREX shareAllDeltaBias");
917 294 : for(int i=0; i<n; i++) {
918 126 : string s; Tools::convert(i,s);
919 252 : real a=NAN; s="GREX getDeltaBias "+s; p.cmd(s.c_str(),&a);
920 126 : if(grex_log) fprintf(grex_log," %f",a);
921 : }
922 42 : if(grex_log) fprintf(grex_log,"\n");
923 : }
924 : }
925 :
926 :
927 25021 : if(fp_forces) {
928 15895 : fprintf(fp_forces,"%d\n",natoms);
929 270215 : string fmtv=dumpforcesFmt+" "+dumpforcesFmt+" "+dumpforcesFmt+" "+dumpforcesFmt+" "+dumpforcesFmt+" "+dumpforcesFmt+" "+dumpforcesFmt+" "+dumpforcesFmt+" "+dumpforcesFmt+"\n";
930 79475 : string fmt=dumpforcesFmt+" "+dumpforcesFmt+" "+dumpforcesFmt+"\n";
931 15895 : if(dumpfullvirial) {
932 117 : fprintf(fp_forces,fmtv.c_str(),virial[0],virial[1],virial[2],virial[3],virial[4],virial[5],virial[6],virial[7],virial[8]);
933 : } else {
934 15778 : fprintf(fp_forces,fmt.c_str(),virial[0],virial[4],virial[8]);
935 : }
936 31790 : fmt="X "+fmt;
937 2502671 : for(int i=0; i<natoms; i++)
938 4973552 : fprintf(fp_forces,fmt.c_str(),forces[3*i],forces[3*i+1],forces[3*i+2]);
939 : }
940 25021 : if(debugforces.length()>0) {
941 : // Now call the routine to work out the derivatives numerically
942 30 : numder.assign(3*natoms+9,real(0.0)); real base=0;
943 : p.cmd("getBias",&base);
944 15 : if( fabs(base)<epsilon ) printf("WARNING: bias for configuration appears to be zero so debugging forces is trivial");
945 15 : evaluateNumericalDerivatives( step, p, coordinates, masses, charges, cell, base, numder );
946 :
947 : // And output everything to a file
948 30 : fp_dforces.fmtField(" " + dumpforcesFmt);
949 225 : for(int i=0; i<3*natoms; ++i) {
950 210 : fp_dforces.printField("parameter",(int)i);
951 315 : fp_dforces.printField("analytical",forces[i]);
952 210 : fp_dforces.printField("numerical",-numder[i]);
953 105 : fp_dforces.printField();
954 : }
955 : // And print the virial
956 285 : for(int i=0; i<9; ++i) {
957 270 : fp_dforces.printField("parameter",3*natoms+i );
958 405 : fp_dforces.printField("analytical",virial[i] );
959 405 : fp_dforces.printField("numerical",-numder[3*natoms+i]);
960 135 : fp_dforces.printField();
961 : }
962 : }
963 :
964 25021 : if(noatoms && plumedStopCondition) break;
965 :
966 24990 : step+=stride;
967 : }
968 : p.cmd("runFinalJobs");
969 :
970 512 : if(fp_forces) fclose(fp_forces);
971 512 : if(debugforces.length()>0) fp_dforces.close();
972 512 : if(fp && fp!=in)fclose(fp);
973 : #ifdef __PLUMED_HAS_XDRFILE
974 512 : if(xd) xdrfile_close(xd);
975 : #endif
976 : #ifdef __PLUMED_HAS_MOLFILE_PLUGINS
977 512 : if(h_in) api->close_file_read(h_in);
978 512 : if(ts_in.coords) delete [] ts_in.coords;
979 : #endif
980 512 : if(grex_log) fclose(grex_log);
981 :
982 : return 0;
983 : }
984 :
985 : template<typename real>
986 15 : void Driver<real>::evaluateNumericalDerivatives( const long int& step, Plumed& p, const std::vector<real>& coordinates,
987 : const std::vector<real>& masses, const std::vector<real>& charges,
988 : std::vector<real>& cell, const double& base, std::vector<real>& numder ) {
989 :
990 15 : int natoms = coordinates.size() / 3; real delta = sqrt(epsilon);
991 15 : std::vector<Vector> pos(natoms); real bias=0;
992 15 : std::vector<real> fake_forces( 3*natoms ), fake_virial(9);
993 85 : for(int i=0; i<natoms; ++i) {
994 350 : for(unsigned j=0; j<3; ++j) pos[i][j]=coordinates[3*i+j];
995 : }
996 :
997 85 : for(int i=0; i<natoms; ++i) {
998 245 : for(unsigned j=0; j<3; ++j) {
999 210 : pos[i][j]=pos[i][j]+delta;
1000 : p.cmd("setStepLong",&step);
1001 105 : p.cmd("setPositions",&pos[0][0]);
1002 : p.cmd("setForces",&fake_forces[0]);
1003 : p.cmd("setMasses",&masses[0]);
1004 : p.cmd("setCharges",&charges[0]);
1005 : p.cmd("setBox",&cell[0]);
1006 : p.cmd("setVirial",&fake_virial[0]);
1007 : p.cmd("prepareCalc");
1008 : p.cmd("performCalcNoUpdate");
1009 : p.cmd("getBias",&bias);
1010 210 : pos[i][j]=coordinates[3*i+j];
1011 210 : numder[3*i+j] = (bias - base) / delta;
1012 : }
1013 : }
1014 :
1015 : // Create the cell
1016 15 : Tensor box( cell[0], cell[1], cell[2], cell[3], cell[4], cell[5], cell[6], cell[7], cell[8] );
1017 : // And the virial
1018 15 : Pbc pbc; pbc.setBox( box ); Tensor nvirial;
1019 195 : for(unsigned i=0; i<3; i++) for(unsigned k=0; k<3; k++) {
1020 135 : double arg0=box(i,k);
1021 765 : for(int j=0; j<natoms; ++j) pos[j]=pbc.realToScaled( pos[j] );
1022 270 : cell[3*i+k]=box(i,k)=box(i,k)+delta; pbc.setBox(box);
1023 765 : for(int j=0; j<natoms; j++) pos[j]=pbc.scaledToReal( pos[j] );
1024 : p.cmd("setStepLong",&step);
1025 135 : p.cmd("setPositions",&pos[0][0]);
1026 : p.cmd("setForces",&fake_forces[0]);
1027 : p.cmd("setMasses",&masses[0]);
1028 : p.cmd("setCharges",&charges[0]);
1029 : p.cmd("setBox",&cell[0]);
1030 : p.cmd("setVirial",&fake_virial[0]);
1031 : p.cmd("prepareCalc");
1032 : p.cmd("performCalcNoUpdate");
1033 : p.cmd("getBias",&bias);
1034 270 : cell[3*i+k]=box(i,k)=arg0; pbc.setBox(box);
1035 2340 : for(int j=0; j<natoms; j++) for(unsigned n=0; n<3; ++n) pos[j][n]=coordinates[3*j+n];
1036 135 : nvirial(i,k) = ( bias - base ) / delta;
1037 : }
1038 15 : nvirial=-matmul(box.transpose(),nvirial);
1039 195 : for(unsigned i=0; i<3; i++) for(unsigned k=0; k<3; k++) numder[3*natoms+3*i+k] = nvirial(i,k);
1040 :
1041 15 : }
1042 :
1043 : }
1044 4839 : }
|