Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2017 of Pipolo Silvio and Fabio Pietrucci.
3 :
4 : The piv module is free software: you can redistribute it and/or modify
5 : it under the terms of the GNU Lesser General Public License as published by
6 : the Free Software Foundation, either version 3 of the License, or
7 : (at your option) any later version.
8 :
9 : The piv module is distributed in the hope that it will be useful,
10 : but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : GNU Lesser General Public License for more details.
13 :
14 : You should have received a copy of the GNU Lesser General Public License
15 : along with plumed. If not, see <http://www.gnu.org/licenses/>.
16 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
17 : #include "colvar/Colvar.h"
18 : #include "core/ActionRegister.h"
19 : #include "core/PlumedMain.h"
20 : #include "core/ActionWithVirtualAtom.h"
21 : #include "tools/NeighborList.h"
22 : #include "tools/SwitchingFunction.h"
23 : #include "tools/PDB.h"
24 : #include "tools/Pbc.h"
25 : #include "tools/Communicator.h"
26 : #include "tools/Stopwatch.h"
27 : #include "core/ActionSet.h"
28 :
29 : #include <string>
30 : #include <cmath>
31 : #include <iostream>
32 :
33 : using namespace std;
34 :
35 : namespace PLMD {
36 : namespace piv {
37 :
38 : //+PLUMEDOC COLVAR PIV
39 : /*
40 : Calculates the PIV-distance.
41 :
42 : PIV distance is the squared Cartesian distance between the PIV
43 : associated to the configuration of the system during the dynamics and a reference configuration provided
44 : as input (PDB file format).
45 : PIV can be used together with [FUNCPATHMSD](FUNCPATHMSD.md) to define a path in the PIV space.
46 :
47 : ## Examples
48 :
49 : The following example calculates PIV-distances from three reference configurations in Ref1.pdb, Ref2.pdb and Ref3.pdb
50 : and prints the results in a file named colvar.
51 : Three atoms (PIVATOMS=3) with names (pdb file) A B and C are used to construct the PIV and all PIV blocks (AA, BB, CC, AB, AC, BC) are considered.
52 : SFACTOR is a scaling factor that multiplies the contribution to the PIV-distance given by the single PIV block.
53 : NLIST sets the use of neighbor lists for calculating atom-atom distances.
54 : The SWITCH keyword specifies the parameters of the switching function that transforms atom-atom distances.
55 : SORT=1 means that the PIV block elements are sorted (SORT=0 no sorting.)
56 : Values for SORT, SFACTOR and the neighbor list parameters have to be specified for each block.
57 : The order is the following: AA,BB,CC,AB,AC,BC. If ONLYDIRECT (ONLYCROSS) is used the order is AA,BB,CC (AB,AC,BC).
58 : The sorting operation within each PIV block is performed using the counting sort algorithm, PRECISION specifies the size of the counting array.
59 :
60 : ```plumed
61 : PIV ...
62 : LABEL=Pivd1
63 : PRECISION=1000
64 : NLIST
65 : REF_FILE=Ref1.pdb
66 : PIVATOMS=3
67 : ATOMTYPES=A,B,C
68 : SFACTOR=0.3,0.5,1.0,0.2,0.2,0.2
69 : SORT=1,1,1,1,1,1
70 : SWITCH1={RATIONAL R_0=0.6 MM=12 NN=4}
71 : SWITCH2={RATIONAL R_0=0.4 MM=10 NN=5}
72 : SWITCH3={RATIONAL R_0=0.4 MM=10 NN=5}
73 : SWITCH4={RATIONAL R_0=0.5 MM=12 NN=6}
74 : SWITCH5={RATIONAL R_0=0.5 MM=12 NN=6}
75 : SWITCH6={RATIONAL R_0=0.5 MM=12 NN=6}
76 : NL_CUTOFF=0.8,0.6,0.6,0.7,0.7,0.7
77 : NL_STRIDE=10,10,10,10,10,10
78 : NL_SKIN=0.1,0.1,0.1,0.1,0.1,0.1
79 : ... PIV
80 : PIV ...
81 : LABEL=Pivd2
82 : PRECISION=1000
83 : NLIST
84 : REF_FILE=Ref2.pdb
85 : PIVATOMS=3
86 : ATOMTYPES=A,B,C
87 : SFACTOR=0.3,0.5,1.0,0.2,0.2,0.2
88 : SORT=1,1,1,1,1,1
89 : SWITCH1={RATIONAL R_0=0.6 MM=12 NN=4}
90 : SWITCH2={RATIONAL R_0=0.4 MM=10 NN=5}
91 : SWITCH3={RATIONAL R_0=0.4 MM=10 NN=5}
92 : SWITCH4={RATIONAL R_0=0.5 MM=12 NN=6}
93 : SWITCH5={RATIONAL R_0=0.5 MM=12 NN=6}
94 : SWITCH6={RATIONAL R_0=0.5 MM=12 NN=6}
95 : NL_CUTOFF=0.8,0.6,0.6,0.7,0.7,0.7
96 : NL_STRIDE=10,10,10,10,10,10
97 : NL_SKIN=0.1,0.1,0.1,0.1,0.1,0.1
98 : ... PIV
99 : PIV ...
100 : LABEL=Pivd3
101 : PRECISION=1000
102 : NLIST
103 : REF_FILE=Ref3.pdb
104 : PIVATOMS=3
105 : ATOMTYPES=A,B,C
106 : SFACTOR=0.3,0.5,1.0,0.2,0.2,0.2
107 : SORT=1,1,1,1,1,1
108 : SWITCH1={RATIONAL R_0=0.6 MM=12 NN=4}
109 : SWITCH2={RATIONAL R_0=0.4 MM=10 NN=5}
110 : SWITCH3={RATIONAL R_0=0.4 MM=10 NN=5}
111 : SWITCH4={RATIONAL R_0=0.5 MM=12 NN=6}
112 : SWITCH5={RATIONAL R_0=0.5 MM=12 NN=6}
113 : SWITCH6={RATIONAL R_0=0.5 MM=12 NN=6}
114 : NL_CUTOFF=0.8,0.6,0.6,0.7,0.7,0.7
115 : NL_STRIDE=10,10,10,10,10,10
116 : NL_SKIN=0.1,0.1,0.1,0.1,0.1,0.1
117 : ... PIV
118 :
119 : PRINT ARG=Pivd1,Pivd2,Pivd3 FILE=colvar
120 : ````
121 :
122 : WARNING:
123 : Both the "CRYST" and "ATOM" lines of the PDB files must conform precisely to the official pdb format, including the width of each alphanumerical field:
124 :
125 : ````
126 : CRYST1 31.028 36.957 23.143 89.93 92.31 89.99 P 1 1
127 : ATOM 1 OW1 wate 1 15.630 19.750 1.520 1.00 0.00
128 : ````
129 :
130 : In each pdb frame, atoms must be numbered in the same order and with the same element symbol as in the input of the MD program.
131 :
132 : The following example calculates the PIV-distances from two reference configurations Ref1.pdb and Ref2.pdb
133 : and uses PIV-distances to define a Path Collective Variable ([FUNCPATHMSD](FUNCPATHMSD.md)) with only two references (Ref1.pdb and Ref2.pdb).
134 : With the VOLUME keyword one scales the atom-atom distances by the cubic root of the ratio between the specified value and the box volume of the initial step of the trajectory file.
135 :
136 : ```plumed
137 : PIV ...
138 : LABEL=c1
139 : PRECISION=1000
140 : VOLUME=12.15
141 : NLIST
142 : REF_FILE=Ref1.pdb
143 : PIVATOMS=2
144 : ATOMTYPES=A,B
145 : ONLYDIRECT
146 : SFACTOR=1.0,0.2
147 : SORT=1,1
148 : SWITCH1={RATIONAL R_0=0.6 MM=12 NN=4}
149 : SWITCH2={RATIONAL R_0=0.5 MM=10 NN=5}
150 : NL_CUTOFF=1.2,1.2
151 : NL_STRIDE=10,10
152 : NL_SKIN=0.1,0.1
153 : ... PIV
154 : PIV ...
155 : LABEL=c2
156 : PRECISION=1000
157 : VOLUME=12.15
158 : NLIST
159 : REF_FILE=Ref2.pdb
160 : PIVATOMS=2
161 : ATOMTYPES=A,B
162 : ONLYDIRECT
163 : SFACTOR=1.0,0.2
164 : SORT=1,1
165 : SWITCH1={RATIONAL R_0=0.6 MM=12 NN=4}
166 : SWITCH2={RATIONAL R_0=0.4 MM=10 NN=5}
167 : NL_CUTOFF=1.2,1.2
168 : NL_STRIDE=10,10
169 : NL_SKIN=0.1,0.1
170 : ... PIV
171 :
172 : p1: FUNCPATHMSD ARG=c1,c2 LAMBDA=0.180338
173 : METAD ARG=p1.s,p1.z SIGMA=0.01,0.2 HEIGHT=0.8 PACE=500 LABEL=res
174 : PRINT ARG=c1,c2,p1.s,p1.z,res.bias STRIDE=500 FILE=colvar FMT=%15.6f
175 : ```
176 :
177 : When using PIV please cite the papers from the reference section below.
178 :
179 : */
180 : //+ENDPLUMEDOC
181 :
182 : class PIV : public Colvar {
183 : private:
184 : bool pbc, serial, timer;
185 : ForwardDecl<Stopwatch> stopwatch_fwd;
186 : Stopwatch& stopwatch=*stopwatch_fwd;
187 : int updatePIV;
188 : size_t Nprec;
189 : unsigned Natm,Nlist,NLsize;
190 : double Fvol,Vol0,m_PIVdistance;
191 : std::string ref_file;
192 : std::unique_ptr<NeighborList> nlall;
193 : std::vector<SwitchingFunction> sfs;
194 : std::vector<std:: vector<double> > rPIV;
195 : std::vector<double> scaling,r00;
196 : std::vector<double> nl_skin;
197 : std::vector<double> fmass;
198 : std::vector<bool> dosort;
199 : std::vector<Vector> compos;
200 : std::vector<string> sw;
201 : std::vector<std::unique_ptr<NeighborList>> nl;
202 : std::vector<std::unique_ptr<NeighborList>> nlcom;
203 : std::vector<Vector> m_deriv;
204 : Tensor m_virial;
205 : bool Svol,cross,direct,doneigh,test,CompDer,com;
206 :
207 : /// Local structure, used to store data that should be
208 : /// shared across multiple PIV instances
209 6 : struct SharedData {
210 : int prev_stp=-1;
211 : int init_stp=1;
212 : std:: vector<std:: vector<Vector> > prev_pos;
213 : std:: vector<std:: vector<double> > cPIV;
214 : std:: vector<std:: vector<int> > Atom0;
215 : std:: vector<std:: vector<int> > Atom1;
216 : };
217 : /// Owning pointer. Will only be allocated by the first PIV instance
218 : std::unique_ptr<SharedData> sharedData_unique;
219 : /// Raw pointer. Will have the same value for all PIV instances
220 : SharedData* sharedData=nullptr;
221 :
222 : public:
223 : static void registerKeywords( Keywords& keys );
224 : explicit PIV(const ActionOptions&);
225 : // active methods:
226 : virtual void calculate();
227 0 : void checkFieldsAllowed() {}
228 : };
229 :
230 : PLUMED_REGISTER_ACTION(PIV,"PIV")
231 :
232 16 : void PIV::registerKeywords( Keywords& keys ) {
233 16 : Colvar::registerKeywords( keys );
234 16 : keys.add("numbered","SWITCH","The switching functions parameter."
235 : "You should specify a Switching function for all PIV blocks."
236 : "Details of the various switching "
237 : "functions you can use are provided on switchingfunction.");
238 16 : keys.add("compulsory","PRECISION","the precision for approximating reals with integers in sorting.");
239 16 : keys.add("compulsory","REF_FILE","PDB file name that contains the ith reference structure.");
240 16 : keys.add("compulsory","PIVATOMS","Number of atoms to use for PIV.");
241 16 : keys.add("compulsory","SORT","Whether to sort or not the PIV block.");
242 16 : keys.add("compulsory","ATOMTYPES","The atom types to use for PIV.");
243 16 : keys.add("optional","SFACTOR","Scale the PIV-distance by such block-specific factor");
244 16 : keys.add("optional","VOLUME","Scale atom-atom distances by the cubic root of the cell volume. The input volume is used to scale the R_0 value of the switching function. ");
245 16 : keys.add("optional","UPDATEPIV","Frequency (in steps) at which the PIV is updated.");
246 16 : keys.addFlag("TEST",false,"Print the actual and reference PIV and exit");
247 16 : keys.addFlag("COM",false,"Use centers of mass of groups of atoms instead of atoms as specified in the Pdb file");
248 16 : keys.addFlag("ONLYCROSS",false,"Use only cross-terms (A-B, A-C, B-C, ...) in PIV");
249 16 : keys.addFlag("ONLYDIRECT",false,"Use only direct-terms (A-A, B-B, C-C, ...) in PIV");
250 16 : keys.addFlag("DERIVATIVES",false,"Activate the calculation of the PIV for every class (needed for numerical derivatives).");
251 16 : keys.addFlag("NLIST",false,"Use a neighbor list for distance calculations.");
252 16 : keys.addFlag("SERIAL",false,"Perform the calculation in serial - for debug purpose");
253 16 : keys.addFlag("TIMER",false,"Perform timing analysis on heavy loops.");
254 16 : keys.add("optional","NL_CUTOFF","Neighbor lists cutoff.");
255 16 : keys.add("optional","NL_STRIDE","Update neighbor lists every NL_STRIDE steps.");
256 16 : keys.add("optional","NL_SKIN","The maximum atom displacement tolerated for the neighbor lists update.");
257 32 : keys.reset_style("SWITCH","compulsory");
258 32 : keys.setValueDescription("scalar","the PIV-distance");
259 16 : keys.addDOI("10.1063/1.4818005");
260 16 : keys.addDOI("10.1103/PhysRevLett.119.245701");
261 16 : }
262 :
263 12 : PIV::PIV(const ActionOptions&ao):
264 : PLUMED_COLVAR_INIT(ao),
265 12 : pbc(true),
266 12 : serial(false),
267 12 : timer(false),
268 12 : updatePIV(1),
269 12 : Nprec(1000),
270 12 : Natm(1),
271 12 : Nlist(1),
272 12 : NLsize(1),
273 12 : Fvol(1.),
274 12 : Vol0(0.),
275 12 : m_PIVdistance(0.),
276 12 : rPIV(std:: vector<std:: vector<double> >(Nlist)),
277 12 : scaling(std:: vector<double>(Nlist)),
278 12 : r00(std:: vector<double>(Nlist)),
279 12 : nl_skin(std:: vector<double>(Nlist)),
280 12 : fmass(std:: vector<double>(Nlist)),
281 12 : dosort(std:: vector<bool>(Nlist)),
282 12 : compos(std:: vector<Vector>(NLsize)),
283 12 : sw(std:: vector<string>(Nlist)),
284 12 : nl(Nlist),
285 12 : nlcom(NLsize),
286 12 : m_deriv(std:: vector<Vector>(1)),
287 12 : Svol(false),
288 12 : cross(true),
289 12 : direct(true),
290 12 : doneigh(false),
291 12 : test(false),
292 12 : CompDer(false),
293 24 : com(false) {
294 12 : log << "Starting PIV Constructor\n";
295 :
296 : {
297 : // look for another PIV instance previously allocated
298 12 : auto* previous=plumed.getActionSet().selectLatest<PIV*>(this);
299 :
300 : // Uncommenting the following line, it is possible to force
301 : // a separate object per instance of the PIB object.
302 : // Results are unaffected, but performance is worse.
303 : // I think this is the expected behavior. GB
304 : // previous=nullptr;
305 :
306 12 : if(!previous) {
307 : // if not found, allocate the shared data struct
308 6 : sharedData_unique=Tools::make_unique<SharedData>();
309 : // then set the raw pointer
310 6 : sharedData=sharedData_unique.get();
311 : } else {
312 : // if found, use the previous raw pointer
313 6 : sharedData=previous->sharedData;
314 6 : log << "(a previous PIV action was found)\n";
315 : }
316 : }
317 :
318 : // Precision on the real-to-integer transformation for the sorting
319 12 : parse("PRECISION",Nprec);
320 12 : if(Nprec<2) {
321 0 : error("Precision must be => 2");
322 : }
323 :
324 : // PBC
325 12 : bool nopbc=!pbc;
326 12 : parseFlag("NOPBC",nopbc);
327 12 : pbc=!nopbc;
328 12 : if(pbc) {
329 12 : log << "Using Periodic Boundary Conditions\n";
330 : } else {
331 0 : log << "Isolated System (NO PBC)\n";
332 : }
333 :
334 : // SERIAL/PARALLEL
335 12 : parseFlag("SERIAL",serial);
336 12 : if(serial) {
337 0 : log << "Serial PIV construction\n";
338 : } else {
339 12 : log << "Parallel PIV construction\n";
340 : }
341 :
342 : // Derivatives
343 12 : parseFlag("DERIVATIVES",CompDer);
344 12 : if(CompDer) {
345 6 : log << "Computing Derivatives\n";
346 : }
347 :
348 : // Timing
349 12 : parseFlag("TIMER",timer);
350 12 : if(timer) {
351 1 : log << "Timing analysis\n";
352 1 : stopwatch.start();
353 1 : stopwatch.pause();
354 : }
355 :
356 : // Test
357 12 : parseFlag("TEST",test);
358 :
359 : // UPDATEPIV
360 12 : if(keywords.exists("UPDATEPIV")) {
361 24 : parse("UPDATEPIV",updatePIV);
362 : }
363 :
364 : // Test
365 12 : parseFlag("COM",com);
366 12 : if(com) {
367 0 : log << "Building PIV using COMs\n";
368 : }
369 :
370 : // Volume Scaling
371 12 : parse("VOLUME",Vol0);
372 12 : if (Vol0>0) {
373 12 : Svol=true;
374 : }
375 :
376 : // PIV direct and cross blocks
377 12 : bool oc=false,od=false;
378 12 : parseFlag("ONLYCROSS",oc);
379 12 : parseFlag("ONLYDIRECT",od);
380 12 : if (oc&&od) {
381 0 : error("ONLYCROSS and ONLYDIRECT are incompatible options!");
382 : }
383 12 : if(oc) {
384 4 : direct=false;
385 4 : log << "Using only CROSS-PIV blocks\n";
386 : }
387 12 : if(od) {
388 4 : cross=false;
389 4 : log << "Using only DIRECT-PIV blocks\n";
390 : }
391 :
392 : // Atoms for PIV
393 12 : parse("PIVATOMS",Natm);
394 12 : std:: vector<string> atype(Natm);
395 12 : parseVector("ATOMTYPES",atype);
396 :
397 : // Reference PDB file
398 12 : parse("REF_FILE",ref_file);
399 12 : PDB mypdb;
400 12 : FILE* fp=fopen(ref_file.c_str(),"r");
401 12 : if (fp!=NULL) {
402 12 : log<<"Opening PDB file with reference frame: "<<ref_file.c_str()<<"\n";
403 12 : mypdb.readFromFilepointer(fp,usingNaturalUnits(),0.1/getUnits().getLength());
404 12 : fclose (fp);
405 : } else {
406 0 : error("Error in reference PDB file");
407 : }
408 :
409 : // Build COM/Atom lists of AtomNumbers (this might be done in PBC.cpp)
410 : // Atomlist or Plist used to build pair lists
411 12 : std:: vector<std:: vector<AtomNumber> > Plist(Natm);
412 : // Atomlist used to build list of atoms for each COM
413 12 : std:: vector<std:: vector<AtomNumber> > comatm(1);
414 : // NLsize is the number of atoms in the pdb cell
415 12 : NLsize=mypdb.getAtomNumbers().size();
416 : // In the following P stands for Point (either an Atom or a COM)
417 : unsigned resnum=0;
418 : // Presind (array size: number of residues) contains the contains the residue number
419 : // this is because the residue numbers may not always be ordered from 1 to resnum
420 12 : std::vector<int> Presind;
421 : // Build Presind
422 19404 : for (unsigned i=0; i<mypdb.getAtomNumbers().size(); i++) {
423 19392 : const int rind=mypdb.getResidueNumber(mypdb.getAtomNumbers()[i]);
424 : bool oldres=false;
425 7705008 : for (unsigned j=0; j<Presind.size(); j++) {
426 7685616 : if(rind==Presind[j]) {
427 : oldres=true;
428 : }
429 : }
430 19392 : if(!oldres) {
431 4848 : Presind.push_back(rind);
432 : }
433 : }
434 12 : resnum=Presind.size();
435 :
436 : // Pind0 is the atom/COM used in Nlists (for COM Pind0 is the first atom in the pdb belonging to that COM)
437 : unsigned Pind0size;
438 12 : if(com) {
439 : Pind0size=resnum;
440 : } else {
441 12 : Pind0size=NLsize;
442 : }
443 12 : std:: vector<unsigned> Pind0(Pind0size);
444 : // If COM resize important arrays
445 12 : comatm.resize(NLsize);
446 12 : if(com) {
447 0 : nlcom.resize(NLsize);
448 0 : compos.resize(NLsize);
449 0 : fmass.resize(NLsize,0.);
450 : }
451 12 : log << "Total COM/Atoms: " << Natm*resnum << " \n";
452 : // Build lists of Atoms/COMs for NLists
453 : // comatm filled also for non_COM calculation for analysis purposes
454 36 : for (unsigned j=0; j<Natm; j++) {
455 : // first Point of this type, set below when the first atom of type atype[j] is found
456 : unsigned oind=0;
457 38808 : for (unsigned i=0; i<Pind0.size(); i++) {
458 38784 : Pind0[i]=0;
459 : }
460 38808 : for (unsigned i=0; i<mypdb.getAtomNumbers().size(); i++) {
461 : // Residue/Atom AtomNumber: used to build NL for COMS/Atoms pairs.
462 38784 : AtomNumber anum=mypdb.getAtomNumbers()[i];
463 : // ResidueName/Atomname associated to atom
464 38784 : string rname=mypdb.getResidueName(anum);
465 38784 : string aname=mypdb.getAtomName(anum);
466 : // Index associated to residue/atom: used to separate COM-lists
467 38784 : const int rind=mypdb.getResidueNumber(anum);
468 : unsigned aind=anum.index();
469 : // This builds lists for NL
470 : string Pname;
471 : unsigned Pind=0;
472 38784 : if(com) {
473 : Pname=rname;
474 : // Presind holds the residue numbers of all the atoms of mypdb, so rind is always found
475 : bool found=false;
476 0 : for(unsigned l=0; l<resnum; l++) {
477 0 : if(rind==Presind[l]) {
478 : Pind=l;
479 : found=true;
480 : }
481 : }
482 0 : plumed_assert(found);
483 : } else {
484 : Pname=aname;
485 : Pind=aind;
486 : }
487 38784 : if(Pname==atype[j]) {
488 14544 : if(Pind0[Pind]==0) {
489 : // adding the atomnumber to the atom/COM list for pairs
490 14544 : Plist[j].push_back(anum);
491 14544 : Pind0[Pind]=aind+1;
492 : oind=Pind;
493 : }
494 : // adding the atomnumber to list of atoms for every COM/Atoms
495 14544 : comatm[Pind0[Pind]-1].push_back(anum);
496 : }
497 : }
498 : // Output Lists
499 24 : log << " Groups of type " << j << ": " << Plist[j].size() << " \n";
500 24 : if(Plist[j].empty()) {
501 0 : error("no atoms of type " + atype[j] + " found in the reference PDB file");
502 : }
503 : string gname;
504 : unsigned gsize;
505 24 : if(com) {
506 0 : gname=mypdb.getResidueName(comatm[Pind0[oind]-1][0]);
507 0 : gsize=comatm[Pind0[oind]-1].size();
508 : } else {
509 48 : gname=mypdb.getAtomName(comatm[Pind0[oind]-1][0]);
510 : gsize=1;
511 : }
512 24 : log.printf(" %6s %3s %13s %10i %6s\n", "type ", gname.c_str()," containing ",gsize," atoms");
513 : }
514 :
515 : // This is to build the list with all the atoms
516 12 : std:: vector<AtomNumber> listall;
517 19404 : for (unsigned i=0; i<mypdb.getAtomNumbers().size(); i++) {
518 19392 : listall.push_back(mypdb.getAtomNumbers()[i]);
519 : }
520 :
521 : // PIV blocks and Neighbour Lists
522 12 : Nlist=0;
523 : // Direct adds the A-A ad B-B blocks (N)
524 12 : if(direct) {
525 8 : Nlist=Nlist+unsigned(Natm);
526 : }
527 : // Cross adds the A-B blocks (N*(N-1)/2)
528 12 : if(cross) {
529 8 : Nlist=Nlist+unsigned(double(Natm*(Natm-1))/2.);
530 : }
531 : // Resize vectors according to Nlist
532 12 : rPIV.resize(Nlist);
533 :
534 : // PIV scaled option
535 12 : scaling.resize(Nlist);
536 36 : for(unsigned j=0; j<Nlist; j++) {
537 24 : scaling[j]=1.;
538 : }
539 12 : if(keywords.exists("SFACTOR")) {
540 24 : parseVector("SFACTOR",scaling);
541 : }
542 : // Neighbour Lists option
543 12 : parseFlag("NLIST",doneigh);
544 12 : nl.resize(Nlist);
545 12 : nl_skin.resize(Nlist);
546 12 : if(doneigh) {
547 12 : std:: vector<double> nl_cut(Nlist,0.);
548 12 : std:: vector<int> nl_st(Nlist,0);
549 12 : parseVector("NL_CUTOFF",nl_cut);
550 12 : parseVector("NL_STRIDE",nl_st);
551 12 : parseVector("NL_SKIN",nl_skin);
552 36 : for (unsigned j=0; j<Nlist; j++) {
553 24 : if(nl_cut[j]<=0.0) {
554 0 : error("NL_CUTOFF should be explicitly specified and positive");
555 : }
556 24 : if(nl_st[j]<=0) {
557 0 : error("NL_STRIDE should be explicitly specified and positive");
558 : }
559 24 : if(nl_skin[j]<=0.) {
560 0 : error("NL_SKIN should be explicitly specified and positive");
561 : }
562 24 : nl_cut[j]=nl_cut[j]+nl_skin[j];
563 : }
564 12 : log << "Creating Neighbor Lists \n";
565 : // WARNING: is nl_cut meaningful here?
566 24 : nlall= Tools::make_unique<NeighborList>(listall,true,pbc,getPbc(),comm,nl_cut[0],nl_st[0]);
567 12 : if(com) {
568 : //Build lists of Atoms for every COM
569 0 : for (unsigned i=0; i<compos.size(); i++) {
570 : // WARNING: is nl_cut meaningful here?
571 0 : nlcom[i] = Tools::make_unique<NeighborList>(comatm[i],true,pbc,getPbc(),comm,nl_cut[0],nl_st[0]);
572 : }
573 : }
574 : unsigned ncnt=0;
575 : // Direct blocks AA, BB, CC, ...
576 12 : if(direct) {
577 24 : for (unsigned j=0; j<Natm; j++) {
578 16 : nl[ncnt]= Tools::make_unique<NeighborList>(Plist[j],true,pbc,getPbc(),comm,nl_cut[j],nl_st[j]);
579 16 : ncnt+=1;
580 : }
581 : }
582 : // Cross blocks AB, AC, BC, ...
583 12 : if(cross) {
584 24 : for (unsigned j=0; j<Natm; j++) {
585 24 : for (unsigned i=j+1; i<Natm; i++) {
586 16 : nl[ncnt]= Tools::make_unique<NeighborList>(Plist[i],Plist[j],true,false,pbc,getPbc(),comm,nl_cut[ncnt],nl_st[ncnt]);
587 8 : ncnt+=1;
588 : }
589 : }
590 : }
591 : } else {
592 0 : log << "WARNING: Neighbor List not activated this has not been tested!! \n";
593 0 : nlall= Tools::make_unique<NeighborList>(listall,true,pbc,getPbc(),comm);
594 0 : for (unsigned j=0; j<Nlist; j++) {
595 0 : nl[j]= Tools::make_unique<NeighborList>(Plist[j],Plist[j],true,true,pbc,getPbc(),comm);
596 : }
597 : }
598 : // Output Nlist
599 12 : log << "Total Nlists: " << Nlist << " \n";
600 36 : for (unsigned j=0; j<Nlist; j++) {
601 24 : log << " list " << j+1 << " size " << nl[j]->size() << " \n";
602 : }
603 : // Calculate COM masses once and for all from lists
604 12 : if(com) {
605 0 : for(unsigned j=0; j<compos.size(); j++) {
606 : double commass=0.;
607 0 : for(unsigned i=0; i<nlcom[j]->getFullAtomList().size(); i++) {
608 0 : unsigned andx=nlcom[j]->getFullAtomList()[i].index();
609 0 : commass+=mypdb.getOccupancy()[andx];
610 : }
611 0 : for(unsigned i=0; i<nlcom[j]->getFullAtomList().size(); i++) {
612 0 : unsigned andx=nlcom[j]->getFullAtomList()[i].index();
613 0 : if(commass>0.) {
614 0 : fmass[andx]=mypdb.getOccupancy()[andx]/commass;
615 : } else {
616 0 : fmass[andx]=1.;
617 : }
618 : }
619 : }
620 : }
621 :
622 : // Sorting
623 12 : dosort.resize(Nlist);
624 12 : std:: vector<int> ynsort(Nlist);
625 12 : parseVector("SORT",ynsort);
626 36 : for (unsigned i=0; i<Nlist; i++) {
627 24 : if(ynsort[i]==0||CompDer) {
628 : dosort[i]=false;
629 : } else {
630 : dosort[i]=true;
631 : }
632 : }
633 :
634 : //build box vectors and correct for pbc
635 12 : log << "Building the box from PDB data ... \n";
636 12 : Tensor Box=mypdb.getBoxVec();
637 12 : log << " Done! A,B,C vectors in Cartesian space: \n";
638 12 : log.printf(" A: %12.6f%12.6f%12.6f\n", Box[0][0],Box[0][1],Box[0][2]);
639 12 : log.printf(" B: %12.6f%12.6f%12.6f\n", Box[1][0],Box[1][1],Box[1][2]);
640 12 : log.printf(" C: %12.6f%12.6f%12.6f\n", Box[2][0],Box[2][1],Box[2][2]);
641 12 : log << "Changing the PBC according to the new box \n";
642 12 : Pbc mypbc;
643 12 : mypbc.setBox(Box);
644 12 : log << "The box volume is " << mypbc.getBox().determinant() << " \n";
645 :
646 : //Compute scaling factor
647 12 : if(Svol) {
648 12 : Fvol=cbrt(Vol0/mypbc.getBox().determinant());
649 12 : log << "Scaling atom distances by " << Fvol << " \n";
650 : } else {
651 0 : log << "Using unscaled atom distances \n";
652 : }
653 :
654 12 : r00.resize(Nlist);
655 12 : sw.resize(Nlist);
656 36 : for (unsigned j=0; j<Nlist; j++) {
657 48 : if( !parseNumbered( "SWITCH", j+1, sw[j] ) ) {
658 : break;
659 : }
660 : }
661 12 : if(CompDer) {
662 : // Set switching function parameters here only if computing derivatives
663 : // now set at the beginning of the dynamics to solve the r0 issue
664 6 : log << "Switching Function Parameters \n";
665 6 : sfs.resize(Nlist);
666 : std::string errors;
667 18 : for (unsigned j=0; j<Nlist; j++) {
668 12 : if(Svol) {
669 : double r0;
670 : std::string old_r0;
671 12 : vector<string> data=Tools::getWords(sw[j]);
672 : data.erase(data.begin());
673 12 : Tools::parse(data,"R_0",old_r0);
674 12 : Tools::convert(old_r0,r0);
675 12 : r0*=Fvol;
676 : std::string new_r0;
677 12 : Tools::convert(r0,new_r0);
678 12 : std::size_t pos = sw[j].find("R_0");
679 12 : sw[j].replace(pos+4,old_r0.size(),new_r0);
680 12 : }
681 12 : sfs[j].set(sw[j],errors);
682 : std::string num;
683 12 : Tools::convert(j+1, num);
684 12 : if( errors.length()!=0 ) {
685 0 : error("problem reading SWITCH" + num + " keyword : " + errors );
686 : }
687 12 : r00[j]=sfs[j].get_r0();
688 24 : log << " Swf: " << j << " r0=" << (sfs[j].description()).c_str() << " \n";
689 : }
690 : }
691 :
692 : // build COMs from positions if requested
693 12 : if(com) {
694 0 : for(unsigned j=0; j<compos.size(); j++) {
695 0 : compos[j][0]=0.;
696 0 : compos[j][1]=0.;
697 0 : compos[j][2]=0.;
698 0 : for(unsigned i=0; i<nlcom[j]->getFullAtomList().size(); i++) {
699 0 : unsigned andx=nlcom[j]->getFullAtomList()[i].index();
700 0 : compos[j]+=fmass[andx]*mypdb.getPositions()[andx];
701 : }
702 : }
703 : }
704 : // build the rPIV distances (transformation and sorting is done afterwards)
705 12 : if(CompDer) {
706 6 : log << " PIV | block | Size | Zeros | Ones |" << " \n";
707 : }
708 36 : for(unsigned j=0; j<Nlist; j++) {
709 11516328 : for(unsigned i=0; i<nl[j]->size(); i++) {
710 11516304 : unsigned i0=(nl[j]->getClosePairAtomNumber(i).first).index();
711 11516304 : unsigned i1=(nl[j]->getClosePairAtomNumber(i).second).index();
712 : //calculate/get COM position of centers i0 and i1
713 11516304 : Vector Pos0,Pos1;
714 11516304 : if(com) {
715 0 : Pos0=compos[i0];
716 0 : Pos1=compos[i1];
717 : } else {
718 11516304 : Pos0=mypdb.getPositions()[i0];
719 11516304 : Pos1=mypdb.getPositions()[i1];
720 : }
721 11516304 : Vector ddist;
722 11516304 : if(pbc) {
723 11516304 : ddist=mypbc.distance(Pos0,Pos1);
724 : } else {
725 0 : ddist=delta(Pos0,Pos1);
726 : }
727 11516304 : double df=0.;
728 : // Transformation and sorting done at the first timestep to solve the r0 definition issue
729 11516304 : if(CompDer) {
730 1104 : rPIV[j].push_back(sfs[j].calculate(ddist.modulo()*Fvol, df));
731 : } else {
732 11515200 : rPIV[j].push_back(ddist.modulo()*Fvol);
733 : }
734 : }
735 24 : if(CompDer) {
736 12 : if(dosort[j]) {
737 0 : std::sort(rPIV[j].begin(),rPIV[j].end());
738 : }
739 : int lmt0=0;
740 : int lmt1=0;
741 1116 : for(unsigned i=0; i<rPIV[j].size(); i++) {
742 1104 : if(int(rPIV[j][i]*double(Nprec-1))==0) {
743 0 : lmt0+=1;
744 : }
745 1104 : if(int(rPIV[j][i]*double(Nprec-1))==1) {
746 0 : lmt1+=1;
747 : }
748 : }
749 12 : log.printf(" |%10i|%15zu|%15i|%15i|\n", j, rPIV[j].size(), lmt0, lmt1);
750 : }
751 : }
752 :
753 12 : checkRead();
754 : // From the plumed manual on how to build-up a new Colvar
755 12 : addValueWithDerivatives();
756 12 : requestAtoms(nlall->getFullAtomList());
757 12 : setNotPeriodic();
758 : // getValue()->setPeridodicity(false);
759 : // set size of derivative vector
760 12 : m_deriv.resize(getNumberOfAtoms());
761 24 : }
762 :
763 327 : void PIV::calculate() {
764 :
765 : // Local variables
766 :
767 327 : if(sharedData_unique) {
768 : // This is executed by the first PIV instance.
769 : // We initialize variables with the correct Nlist.
770 321 : sharedData_unique->prev_pos.resize(Nlist);
771 321 : sharedData_unique->cPIV.resize(Nlist);
772 321 : sharedData_unique->Atom0.resize(Nlist);
773 321 : sharedData_unique->Atom1.resize(Nlist);
774 : }
775 :
776 : // create references to minimize the impact of the code below
777 327 : auto & prev_stp(sharedData->prev_stp);
778 : auto & init_stp(sharedData->init_stp);
779 : auto & prev_pos(sharedData->prev_pos);
780 : auto & cPIV(sharedData->cPIV);
781 : auto & Atom0(sharedData->Atom0);
782 : auto & Atom1(sharedData->Atom1);
783 :
784 327 : std:: vector<std:: vector<int> > A0(Nprec);
785 327 : std:: vector<std:: vector<int> > A1(Nprec);
786 : size_t stride=1;
787 : unsigned rank=0;
788 :
789 327 : if(!serial) {
790 327 : stride=comm.Get_size();
791 327 : rank=comm.Get_rank();
792 : } else {
793 : stride=1;
794 : rank=0;
795 : }
796 :
797 : // Transform (and sort) the rPIV before starting the dynamics
798 327 : if (((prev_stp==-1) || (init_stp==1)) &&!CompDer) {
799 6 : if(prev_stp!=-1) {
800 3 : init_stp=0;
801 : }
802 : // Calculate the volume scaling factor
803 6 : if(Svol) {
804 6 : Fvol=cbrt(Vol0/getBox().determinant());
805 : }
806 : //Set switching function parameters
807 6 : log << "\n";
808 6 : log << "REFERENCE PDB # " << prev_stp+2 << " \n";
809 : // Set switching function parameters here only if computing derivatives
810 : // now set at the beginning of the dynamics to solve the r0 issue
811 6 : log << "Switching Function Parameters \n";
812 6 : sfs.resize(Nlist);
813 : std::string errors;
814 18 : for (unsigned j=0; j<Nlist; j++) {
815 12 : if(Svol) {
816 : double r0;
817 : std::string old_r0;
818 12 : vector<string> data=Tools::getWords(sw[j]);
819 : data.erase(data.begin());
820 12 : Tools::parse(data,"R_0",old_r0);
821 12 : Tools::convert(old_r0,r0);
822 12 : r0*=Fvol;
823 : std::string new_r0;
824 12 : Tools::convert(r0,new_r0);
825 12 : std::size_t pos = sw[j].find("R_0");
826 12 : sw[j].replace(pos+4,old_r0.size(),new_r0);
827 12 : }
828 12 : sfs[j].set(sw[j],errors);
829 : std::string num;
830 12 : Tools::convert(j+1, num);
831 12 : if( errors.length()!=0 ) {
832 0 : error("problem reading SWITCH" + num + " keyword : " + errors );
833 : }
834 12 : r00[j]=sfs[j].get_r0();
835 24 : log << " Swf: " << j << " r0=" << (sfs[j].description()).c_str() << " \n";
836 : }
837 : //Transform and sort
838 6 : log << "Building Reference PIV Vector \n";
839 6 : log << " PIV | block | Size | Zeros | Ones |" << " \n";
840 6 : double df=0.;
841 18 : for (unsigned j=0; j<Nlist; j++) {
842 11515212 : for (unsigned i=0; i<rPIV[j].size(); i++) {
843 11515200 : rPIV[j][i]=sfs[j].calculate(rPIV[j][i], df);
844 : }
845 12 : if(dosort[j]) {
846 12 : std::sort(rPIV[j].begin(),rPIV[j].end());
847 : }
848 : int lmt0=0;
849 : int lmt1=0;
850 11515212 : for(unsigned i=0; i<rPIV[j].size(); i++) {
851 11515200 : if(int(rPIV[j][i]*double(Nprec-1))==0) {
852 26 : lmt0+=1;
853 : }
854 11515200 : if(int(rPIV[j][i]*double(Nprec-1))==1) {
855 63358 : lmt1+=1;
856 : }
857 : }
858 12 : log.printf(" |%10i|%15zu|%15i|%15i|\n", j, rPIV[j].size(), lmt0, lmt1);
859 : }
860 6 : log << "\n";
861 : }
862 : // Do the sorting only once per timestep to avoid building the PIV N times for N rPIV PDB structures!
863 327 : if ((getStep()>prev_stp&&getStep()%updatePIV==0)||CompDer) {
864 324 : if (CompDer) {
865 321 : log << " Step " << getStep() << " Computing Derivatives NON-SORTED PIV \n";
866 : }
867 : //
868 : // build COMs from positions if requested
869 324 : if(com) {
870 0 : if(pbc) {
871 0 : makeWhole();
872 : }
873 0 : for(unsigned j=0; j<compos.size(); j++) {
874 0 : compos[j][0]=0.;
875 0 : compos[j][1]=0.;
876 0 : compos[j][2]=0.;
877 0 : for(unsigned i=0; i<nlcom[j]->getFullAtomList().size(); i++) {
878 0 : unsigned andx=nlcom[j]->getFullAtomList()[i].index();
879 0 : compos[j]+=fmass[andx]*getPosition(andx);
880 : }
881 : }
882 : }
883 : // update neighbor lists when an atom moves out of the Neighbor list skin
884 324 : if (doneigh) {
885 : bool doupdate=false;
886 : // For the first step build previous positions = actual positions
887 324 : if (prev_stp==-1) {
888 6 : bool docom=com;
889 18 : for (unsigned j=0; j<Nlist; j++) {
890 9708 : for (unsigned i=0; i<nl[j]->getFullAtomList().size(); i++) {
891 : Vector Pos;
892 9696 : if(docom) {
893 0 : Pos=compos[i];
894 : } else {
895 9696 : Pos=getPosition(nl[j]->getFullAtomList()[i].index());
896 : }
897 9696 : prev_pos[j].push_back(Pos);
898 : }
899 : }
900 : doupdate=true;
901 : }
902 : // Decide whether to update lists based on atom displacement, every stride
903 324 : std:: vector<std:: vector<Vector> > tmp_pos(Nlist);
904 324 : if (getStep() % nlall->getStride() ==0) {
905 324 : bool docom=com;
906 972 : for (unsigned j=0; j<Nlist; j++) {
907 20520 : for (unsigned i=0; i<nl[j]->getFullAtomList().size(); i++) {
908 : Vector Pos;
909 19872 : if(docom) {
910 0 : Pos=compos[i];
911 : } else {
912 19872 : Pos=getPosition(nl[j]->getFullAtomList()[i].index());
913 : }
914 19872 : tmp_pos[j].push_back(Pos);
915 19872 : if (pbcDistance(tmp_pos[j][i],prev_pos[j][i]).modulo()>=nl_skin[j]) {
916 : doupdate=true;
917 : }
918 : }
919 : }
920 : }
921 : // Update Nlists if needed
922 324 : if (doupdate==true) {
923 18 : for (unsigned j=0; j<Nlist; j++) {
924 9708 : for (unsigned i=0; i<nl[j]->getFullAtomList().size(); i++) {
925 9696 : prev_pos[j][i]=tmp_pos[j][i];
926 : }
927 12 : nl[j]->update(prev_pos[j]);
928 12 : log << " Step " << getStep() << " Neighbour lists updated " << nl[j]->size() << " \n";
929 : }
930 : }
931 324 : }
932 : // Calculate the volume scaling factor
933 324 : if(Svol) {
934 324 : Fvol=cbrt(Vol0/getBox().determinant());
935 : }
936 324 : Vector ddist;
937 : // Global to local variables
938 324 : bool doserial=serial;
939 : // Build "Nlist" PIV blocks
940 972 : for(unsigned j=0; j<Nlist; j++) {
941 648 : if(dosort[j]) {
942 : // from global to local variables to speedup the for loop with if statements
943 6 : bool docom=com;
944 6 : bool dopbc=pbc;
945 : // Vectors collecting occupancies: OrdVec one rank, OrdVecAll all ranks
946 6 : std:: vector<int> OrdVec(Nprec,0);
947 6 : cPIV[j].resize(0);
948 6 : Atom0[j].resize(0);
949 6 : Atom1[j].resize(0);
950 : // Building distances for the PIV vector at time t
951 6 : if(timer) {
952 3 : stopwatch.start("1 Build cPIV");
953 : }
954 5757606 : for(unsigned i=rank; i<nl[j]->size(); i+=stride) {
955 5757600 : unsigned i0=(nl[j]->getClosePairAtomNumber(i).first).index();
956 5757600 : unsigned i1=(nl[j]->getClosePairAtomNumber(i).second).index();
957 5757600 : Vector Pos0,Pos1;
958 5757600 : if(docom) {
959 0 : Pos0=compos[i0];
960 0 : Pos1=compos[i1];
961 : } else {
962 5757600 : Pos0=getPosition(i0);
963 5757600 : Pos1=getPosition(i1);
964 : }
965 5757600 : if(dopbc) {
966 5757600 : ddist=pbcDistance(Pos0,Pos1);
967 : } else {
968 0 : ddist=delta(Pos0,Pos1);
969 : }
970 5757600 : double df=0.;
971 : //Integer sorting ... faster!
972 : //Transforming distances with the Switching function + real to integer transformation
973 5757600 : int Vint=int(sfs[j].calculate(ddist.modulo()*Fvol, df)*double(Nprec-1)+0.5);
974 : //Integer transformed distance values as index of the Ordering Vector OrdVec
975 5757600 : OrdVec[Vint]+=1;
976 : //Keeps track of atom indices for force and virial calculations
977 5757600 : A0[Vint].push_back(i0);
978 5757600 : A1[Vint].push_back(i1);
979 : }
980 6 : if(timer) {
981 3 : stopwatch.stop("1 Build cPIV");
982 : }
983 6 : if(timer) {
984 3 : stopwatch.start("2 Sort cPIV");
985 : }
986 6 : if(!doserial && comm.initialized()) {
987 : // Vectors keeping track of the dimension and the starting-position of the rank-specific pair vector in the big pair vector.
988 0 : std:: vector<int> Vdim(stride,0);
989 0 : std:: vector<int> Vpos(stride,0);
990 : // Vectors collecting occupancies: OrdVec one rank, OrdVecAll all ranks
991 0 : std:: vector<int> OrdVecAll(stride*Nprec);
992 : // Big vectors containing all Atom indexes for every occupancy (Atom0O(Nprec,n) and Atom1O(Nprec,n) matrices in one vector)
993 0 : std:: vector<int> Atom0F;
994 0 : std:: vector<int> Atom1F;
995 : // Vector used to reconstruct arrays
996 0 : std:: vector<unsigned> k(stride,0);
997 : // Zeros might be many, this slows down a lot due to MPI communication
998 : // Avoid passing the zeros (i=1) for atom indices
999 0 : for(unsigned i=1; i<Nprec; i++) {
1000 : // Building long vectors with all atom indexes for occupancies ordered from i=1 to i=Nprec-1
1001 : // Can this be avoided ???
1002 0 : Atom0F.insert(Atom0F.end(),A0[i].begin(),A0[i].end());
1003 0 : Atom1F.insert(Atom1F.end(),A1[i].begin(),A1[i].end());
1004 0 : A0[i].resize(0);
1005 0 : A1[i].resize(0);
1006 : }
1007 : // Resize partial arrays to fill up for the next PIV block
1008 0 : A0[0].resize(0);
1009 0 : A1[0].resize(0);
1010 0 : A0[Nprec-1].resize(0);
1011 0 : A1[Nprec-1].resize(0);
1012 : // Avoid passing the zeros (i=1) for atom indices
1013 0 : OrdVec[0]=0;
1014 0 : OrdVec[Nprec-1]=0;
1015 :
1016 : // Wait for all ranks before communication of Vectors
1017 0 : comm.Barrier();
1018 :
1019 : // pass the array sizes before passing the arrays
1020 0 : int dim=Atom0F.size();
1021 : // Vdim and Vpos keep track of the dimension and the starting-position of the rank-specific pair vector in the big pair vector.
1022 0 : comm.Allgather(&dim,1,&Vdim[0],1);
1023 :
1024 : // TO BE IMPROVED: the following may be done by the rank 0 (now every rank does it)
1025 : int Fdim=0;
1026 0 : for(unsigned i=1; i<stride; i++) {
1027 0 : Vpos[i]=Vpos[i-1]+Vdim[i-1];
1028 0 : Fdim+=Vdim[i];
1029 : }
1030 0 : Fdim+=Vdim[0];
1031 : // build big vectors for atom pairs on all ranks for all ranks
1032 0 : std:: vector<int> Atom0FAll(Fdim);
1033 0 : std:: vector<int> Atom1FAll(Fdim);
1034 : // TO BE IMPROVED: Allgathers may be substituted by gathers by proc 0
1035 : // Moreover vectors are gathered head-to-tail and assembled later-on in a serial step.
1036 : // Gather the full Ordering Vector (occupancies). This is what we need to build the PIV
1037 0 : comm.Allgather(&OrdVec[0],Nprec,&OrdVecAll[0],Nprec);
1038 : // Gather the vectors of atom pairs to keep track of the idexes for the forces
1039 0 : comm.Allgatherv(Atom0F.data(),Atom0F.size(),&Atom0FAll[0],&Vdim[0],&Vpos[0]);
1040 0 : comm.Allgatherv(Atom1F.data(),Atom1F.size(),&Atom1FAll[0],&Vdim[0],&Vpos[0]);
1041 :
1042 : // Reconstruct the full vectors from collections of Allgathered parts (this is a serial step)
1043 : // This is the tricky serial step, to assemble together PIV and atom-pair info from head-tail big vectors
1044 : // Loop before on l and then on i would be better but the allgather should be modified
1045 : // Loop on blocks
1046 : //for(unsigned m=0;m<Nlist;m++) {
1047 : // Loop on Ordering Vector size excluding zeros (i=1)
1048 0 : if(timer) {
1049 0 : stopwatch.stop("2 Sort cPIV");
1050 : }
1051 0 : if(timer) {
1052 0 : stopwatch.start("3 Reconstruct cPIV");
1053 : }
1054 0 : for(unsigned i=1; i<Nprec; i++) {
1055 : // Loop on the ranks
1056 0 : for(unsigned l=0; l<stride; l++) {
1057 : // Loop on the number of head-to-tail pieces
1058 0 : for(int m=0; m<OrdVecAll[i+l*Nprec]; m++) {
1059 : // cPIV is the current PIV at time t
1060 0 : cPIV[j].push_back(double(i)/double(Nprec-1));
1061 0 : Atom0[j].push_back(Atom0FAll[k[l]+Vpos[l]]);
1062 0 : Atom1[j].push_back(Atom1FAll[k[l]+Vpos[l]]);
1063 0 : k[l]+=1;
1064 : }
1065 : }
1066 : }
1067 0 : if(timer) {
1068 0 : stopwatch.stop("3 Reconstruct cPIV");
1069 : }
1070 0 : } else {
1071 6000000 : for(unsigned i=1; i<Nprec; i++) {
1072 11757594 : for(int m=0; m<OrdVec[i]; m++) {
1073 5757600 : cPIV[j].push_back(double(i)/double(Nprec-1));
1074 5757600 : Atom0[j].push_back(A0[i][m]);
1075 5757600 : Atom1[j].push_back(A1[i][m]);
1076 : }
1077 : }
1078 : }
1079 : }
1080 : }
1081 : }
1082 327 : Vector distance;
1083 327 : double dfunc=0.;
1084 : // Calculate volume scaling factor
1085 327 : if(Svol) {
1086 327 : Fvol=cbrt(Vol0/getBox().determinant());
1087 : }
1088 :
1089 : // This test may be run by specifying the TEST keyword as input, it pritnts rPIV and cPIV and quits
1090 327 : if(test) {
1091 : unsigned limit=0;
1092 0 : for(unsigned j=0; j<Nlist; j++) {
1093 0 : if(dosort[j]) {
1094 0 : limit = cPIV[j].size();
1095 : } else {
1096 0 : limit = rPIV[j].size();
1097 : }
1098 0 : log.printf("PIV Block: %6i %12s %6i \n", j, " Size:", limit);
1099 0 : log.printf("%6s%6s%12s%12s%36s\n"," i"," j", " c-PIV "," r-PIV "," i-j distance vector ");
1100 0 : for(unsigned i=0; i<limit; i++) {
1101 : unsigned i0=0;
1102 : unsigned i1=0;
1103 0 : if(dosort[j]) {
1104 0 : i0=Atom0[j][i];
1105 0 : i1=Atom1[j][i];
1106 : } else {
1107 0 : i0=(nl[j]->getClosePairAtomNumber(i).first).index();
1108 0 : i1=(nl[j]->getClosePairAtomNumber(i).second).index();
1109 : }
1110 0 : Vector Pos0,Pos1;
1111 0 : if(com) {
1112 0 : Pos0=compos[i0];
1113 0 : Pos1=compos[i1];
1114 : } else {
1115 0 : Pos0=getPosition(i0);
1116 0 : Pos1=getPosition(i1);
1117 : }
1118 0 : if(pbc) {
1119 0 : distance=pbcDistance(Pos0,Pos1);
1120 : } else {
1121 0 : distance=delta(Pos0,Pos1);
1122 : }
1123 0 : dfunc=0.;
1124 : double cP,rP;
1125 0 : if(dosort[j]) {
1126 0 : cP = cPIV[j][i];
1127 0 : rP = rPIV[j][rPIV[j].size()-cPIV[j].size()+i];
1128 : } else {
1129 0 : double dm=distance.modulo();
1130 0 : cP = sfs[j].calculate(dm*Fvol, dfunc);
1131 0 : rP = rPIV[j][i];
1132 : }
1133 0 : log.printf("%6i%6i%12.6f%12.6f%12.6f%12.6f%12.6f\n",i0,i1,cP,rP,distance[0],distance[1],distance[2]);
1134 : }
1135 : }
1136 0 : log.printf("This was a test, now exit \n");
1137 0 : exit();
1138 : }
1139 :
1140 327 : if(timer) {
1141 1 : stopwatch.start("4 Build For Derivatives");
1142 : }
1143 : // non-global variables Nder and Scalevol defined to speedup if structures in cycles
1144 327 : bool Nder=CompDer;
1145 327 : bool Scalevol=Svol;
1146 327 : if(getStep()%updatePIV==0) {
1147 : // set to zero PIVdistance, derivatives and virial when they are calculated
1148 29799 : for(unsigned j=0; j<m_deriv.size(); j++) {
1149 117888 : for(unsigned k=0; k<3; k++) {
1150 88416 : m_deriv[j][k]=0.;
1151 : }
1152 : }
1153 1308 : for(unsigned j=0; j<3; j++) {
1154 3924 : for(unsigned k=0; k<3; k++) {
1155 2943 : m_virial[j][k]=0.;
1156 : }
1157 : }
1158 327 : m_PIVdistance=0.;
1159 : // Re-compute atomic distances for derivatives and compute PIV-PIV distance
1160 981 : for(unsigned j=0; j<Nlist; j++) {
1161 : unsigned limit=0;
1162 : // dosorting definition is to speedup if structure in cycles with non-global variables
1163 : bool dosorting=dosort[j];
1164 654 : bool docom=com;
1165 654 : bool dopbc=pbc;
1166 654 : if(dosorting) {
1167 12 : limit = cPIV[j].size();
1168 : } else {
1169 642 : limit = rPIV[j].size();
1170 : }
1171 11574918 : for(unsigned i=rank; i<limit; i+=stride) {
1172 : unsigned i0=0;
1173 : unsigned i1=0;
1174 11574264 : if(dosorting) {
1175 11515200 : i0=Atom0[j][i];
1176 11515200 : i1=Atom1[j][i];
1177 : } else {
1178 59064 : i0=(nl[j]->getClosePairAtomNumber(i).first).index();
1179 59064 : i1=(nl[j]->getClosePairAtomNumber(i).second).index();
1180 : }
1181 11574264 : Vector Pos0,Pos1;
1182 11574264 : if(docom) {
1183 0 : Pos0=compos[i0];
1184 0 : Pos1=compos[i1];
1185 : } else {
1186 11574264 : Pos0=getPosition(i0);
1187 11574264 : Pos1=getPosition(i1);
1188 : }
1189 11574264 : if(dopbc) {
1190 11574264 : distance=pbcDistance(Pos0,Pos1);
1191 : } else {
1192 0 : distance=delta(Pos0,Pos1);
1193 : }
1194 11574264 : dfunc=0.;
1195 : // this is needed for dfunc and dervatives
1196 11574264 : double dm=distance.modulo();
1197 11574264 : double tPIV = sfs[j].calculate(dm*Fvol, dfunc);
1198 : // PIV distance
1199 : double coord=0.;
1200 11574264 : if(!dosorting||Nder) {
1201 59064 : coord = tPIV - rPIV[j][i];
1202 : } else {
1203 11515200 : coord = cPIV[j][i] - rPIV[j][rPIV[j].size()-cPIV[j].size()+i];
1204 : }
1205 : // Calculate derivatives, virial, and variable=sum_j (scaling[j] *(cPIV-rPIV)_j^2)
1206 : // WARNING: dfunc=dswf/(Fvol*dm) (this may change in future Plumed versions)
1207 11574264 : double tmp = 2.*scaling[j]*coord*Fvol*Fvol*dfunc;
1208 : Vector tmpder = tmp*distance;
1209 : // 0.5*(x_i-x_k)*f_ik (force on atom k due to atom i)
1210 11574264 : if(docom) {
1211 0 : Vector dist;
1212 0 : for(unsigned k=0; k<nlcom[i0]->getFullAtomList().size(); k++) {
1213 0 : unsigned x0=nlcom[i0]->getFullAtomList()[k].index();
1214 0 : m_deriv[x0] -= tmpder*fmass[x0];
1215 0 : for(unsigned l=0; l<3; l++) {
1216 0 : dist[l]=0.;
1217 : }
1218 0 : Vector P0=getPosition(x0);
1219 0 : for(unsigned l=0; l<nlcom[i0]->getFullAtomList().size(); l++) {
1220 0 : unsigned x1=nlcom[i0]->getFullAtomList()[l].index();
1221 0 : Vector P1=getPosition(x1);
1222 0 : if(dopbc) {
1223 0 : dist+=pbcDistance(P0,P1);
1224 : } else {
1225 : dist+=delta(P0,P1);
1226 : }
1227 : }
1228 0 : for(unsigned l=0; l<nlcom[i1]->getFullAtomList().size(); l++) {
1229 0 : unsigned x1=nlcom[i1]->getFullAtomList()[l].index();
1230 0 : Vector P1=getPosition(x1);
1231 0 : if(dopbc) {
1232 0 : dist+=pbcDistance(P0,P1);
1233 : } else {
1234 : dist+=delta(P0,P1);
1235 : }
1236 : }
1237 0 : m_virial -= 0.25*fmass[x0]*Tensor(dist,tmpder);
1238 : }
1239 0 : for(unsigned k=0; k<nlcom[i1]->getFullAtomList().size(); k++) {
1240 0 : unsigned x1=nlcom[i1]->getFullAtomList()[k].index();
1241 0 : m_deriv[x1] += tmpder*fmass[x1];
1242 0 : for(unsigned l=0; l<3; l++) {
1243 0 : dist[l]=0.;
1244 : }
1245 0 : Vector P1=getPosition(x1);
1246 0 : for(unsigned l=0; l<nlcom[i1]->getFullAtomList().size(); l++) {
1247 0 : unsigned x0=nlcom[i1]->getFullAtomList()[l].index();
1248 0 : Vector P0=getPosition(x0);
1249 0 : if(dopbc) {
1250 0 : dist+=pbcDistance(P1,P0);
1251 : } else {
1252 : dist+=delta(P1,P0);
1253 : }
1254 : }
1255 0 : for(unsigned l=0; l<nlcom[i0]->getFullAtomList().size(); l++) {
1256 0 : unsigned x0=nlcom[i0]->getFullAtomList()[l].index();
1257 0 : Vector P0=getPosition(x0);
1258 0 : if(dopbc) {
1259 0 : dist+=pbcDistance(P1,P0);
1260 : } else {
1261 : dist+=delta(P1,P0);
1262 : }
1263 : }
1264 0 : m_virial += 0.25*fmass[x1]*Tensor(dist,tmpder);
1265 : }
1266 : } else {
1267 11574264 : m_deriv[i0] -= tmpder;
1268 11574264 : m_deriv[i1] += tmpder;
1269 23148528 : m_virial -= tmp*Tensor(distance,distance);
1270 : }
1271 11574264 : if(Scalevol) {
1272 23148528 : m_virial+=1./3.*tmp*dm*dm*Tensor::identity();
1273 : }
1274 11574264 : m_PIVdistance += scaling[j]*coord*coord;
1275 : }
1276 : }
1277 :
1278 327 : if (!serial && comm.initialized()) {
1279 0 : comm.Barrier();
1280 0 : comm.Sum(&m_PIVdistance,1);
1281 0 : if(!m_deriv.empty()) {
1282 0 : comm.Sum(&m_deriv[0][0],3*m_deriv.size());
1283 : }
1284 0 : comm.Sum(&m_virial[0][0],9);
1285 : }
1286 : }
1287 327 : prev_stp=getStep();
1288 :
1289 : //Timing
1290 327 : if(timer) {
1291 1 : stopwatch.stop("4 Build For Derivatives");
1292 : }
1293 327 : if(timer) {
1294 1 : log.printf("Timings for action %s with label %s \n", getName().c_str(), getLabel().c_str() );
1295 1 : log<<stopwatch;
1296 : }
1297 :
1298 : // Update derivatives, virial, and variable (PIV-distance^2)
1299 29799 : for(unsigned i=0; i<m_deriv.size(); ++i) {
1300 29472 : setAtomsDerivatives(i,m_deriv[i]);
1301 : }
1302 327 : setValue (m_PIVdistance);
1303 327 : setBoxDerivatives (m_virial);
1304 327 : }
1305 : //Close Namespaces at the very beginning
1306 : }
1307 : }
1308 :
|