LCOV - code coverage report
Current view: top level - piv - PIV.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 425 595 71.4 %
Date: 2024-10-11 08:09:47 Functions: 8 11 72.7 %

          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 "colvar/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/Stopwatch.h"
      26             : 
      27             : #include <string>
      28             : #include <cmath>
      29             : #include <iostream>
      30             : 
      31             : using namespace std;
      32             : 
      33             : namespace PLMD
      34             : {
      35             : namespace piv
      36             : {
      37             : 
      38             : //+PLUMEDOC PIVMOD_COLVAR PIV
      39             : /*
      40             : Calculates the PIV-distance.
      41             : 
      42             : PIV distance is the squared Cartesian distance between the PIV \cite gallet2013structural \cite pipolo2017navigating
      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 \ref FUNCPATHMSD to define a path in the PIV space.
      46             : 
      47             : \par 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             : \plumedfile
      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             : \endplumedfile
     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             : \verbatim
     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             : \endverbatim
     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 (\ref FUNCPATHMSD) 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             : \plumedfile
     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             : \endplumedfile
     176             : 
     177             : When using PIV please cite \cite pipolo2017navigating .
     178             : 
     179             : (See also \ref PRINT)
     180             : 
     181             : */
     182             : //+ENDPLUMEDOC
     183             : 
     184             : class PIV      : public Colvar
     185             : {
     186             : private:
     187             :   bool pbc, serial, timer;
     188             :   ForwardDecl<Stopwatch> stopwatch_fwd;
     189             :   Stopwatch& stopwatch=*stopwatch_fwd;
     190             :   int updatePIV;
     191             :   size_t Nprec;
     192             :   unsigned Natm,Nlist,NLsize;
     193             :   double Fvol,Vol0,m_PIVdistance;
     194             :   std::string ref_file;
     195             :   NeighborList *nlall;
     196             :   std::vector<SwitchingFunction> sfs;
     197             :   std::vector<std:: vector<double> > rPIV;
     198             :   std::vector<double> scaling,r00;
     199             :   std::vector<double> nl_skin;
     200             :   std::vector<double> fmass;
     201             :   std::vector<bool> dosort;
     202             :   std::vector<Vector> compos;
     203             :   std::vector<string> sw;
     204             :   std::vector<NeighborList *> nl;
     205             :   std::vector<NeighborList *> nlcom;
     206             :   std::vector<Vector> m_deriv;
     207             :   Tensor m_virial;
     208             :   bool Svol,cross,direct,doneigh,test,CompDer,com;
     209             : public:
     210             :   static void registerKeywords( Keywords& keys );
     211             :   explicit PIV(const ActionOptions&);
     212             :   ~PIV();
     213             :   // active methods:
     214             :   virtual void calculate();
     215           0 :   void checkFieldsAllowed() {}
     216             : };
     217             : 
     218       10443 : PLUMED_REGISTER_ACTION(PIV,"PIV")
     219             : 
     220          13 : void PIV::registerKeywords( Keywords& keys )
     221             : {
     222          13 :   Colvar::registerKeywords( keys );
     223          26 :   keys.add("numbered","SWITCH","The switching functions parameter."
     224             :            "You should specify a Switching function for all PIV blocks."
     225             :            "Details of the various switching "
     226             :            "functions you can use are provided on \\ref switchingfunction.");
     227          26 :   keys.add("compulsory","PRECISION","the precision for approximating reals with integers in sorting.");
     228          26 :   keys.add("compulsory","REF_FILE","PDB file name that contains the \\f$i\\f$th reference structure.");
     229          26 :   keys.add("compulsory","PIVATOMS","Number of atoms to use for PIV.");
     230          26 :   keys.add("compulsory","SORT","Whether to sort or not the PIV block.");
     231          26 :   keys.add("compulsory","ATOMTYPES","The atom types to use for PIV.");
     232          26 :   keys.add("optional","SFACTOR","Scale the PIV-distance by such block-specific factor");
     233          26 :   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. ");
     234          26 :   keys.add("optional","UPDATEPIV","Frequency (in steps) at which the PIV is updated.");
     235          26 :   keys.addFlag("TEST",false,"Print the actual and reference PIV and exit");
     236          26 :   keys.addFlag("COM",false,"Use centers of mass of groups of atoms instead of atoms as specified in the Pdb file");
     237          26 :   keys.addFlag("ONLYCROSS",false,"Use only cross-terms (A-B, A-C, B-C, ...) in PIV");
     238          26 :   keys.addFlag("ONLYDIRECT",false,"Use only direct-terms (A-A, B-B, C-C, ...) in PIV");
     239          26 :   keys.addFlag("DERIVATIVES",false,"Activate the calculation of the PIV for every class (needed for numerical derivatives).");
     240          26 :   keys.addFlag("NLIST",false,"Use a neighbor list for distance calculations.");
     241          26 :   keys.addFlag("SERIAL",false,"Perform the calculation in serial - for debug purpose");
     242          26 :   keys.addFlag("TIMER",false,"Perform timing analysis on heavy loops.");
     243          26 :   keys.add("optional","NL_CUTOFF","Neighbor lists cutoff.");
     244          26 :   keys.add("optional","NL_STRIDE","Update neighbor lists every NL_STRIDE steps.");
     245          26 :   keys.add("optional","NL_SKIN","The maximum atom displacement tolerated for the neighbor lists update.");
     246          26 :   keys.reset_style("SWITCH","compulsory");
     247          13 : }
     248             : 
     249          12 : PIV::PIV(const ActionOptions&ao):
     250             :   PLUMED_COLVAR_INIT(ao),
     251          12 :   pbc(true),
     252          12 :   serial(false),
     253          12 :   timer(false),
     254          12 :   updatePIV(1),
     255          12 :   Nprec(1000),
     256          12 :   Natm(1),
     257          12 :   Nlist(1),
     258          12 :   NLsize(1),
     259          12 :   Fvol(1.),
     260          12 :   Vol0(0.),
     261          12 :   m_PIVdistance(0.),
     262          12 :   rPIV(std:: vector<std:: vector<double> >(Nlist)),
     263          12 :   scaling(std:: vector<double>(Nlist)),
     264          12 :   r00(std:: vector<double>(Nlist)),
     265          12 :   nl_skin(std:: vector<double>(Nlist)),
     266          12 :   fmass(std:: vector<double>(Nlist)),
     267          12 :   dosort(std:: vector<bool>(Nlist)),
     268          12 :   compos(std:: vector<Vector>(NLsize)),
     269          12 :   sw(std:: vector<string>(Nlist)),
     270          12 :   nl(std:: vector<NeighborList *>(Nlist)),
     271          12 :   nlcom(std:: vector<NeighborList *>(NLsize)),
     272          12 :   m_deriv(std:: vector<Vector>(1)),
     273          12 :   Svol(false),
     274          12 :   cross(true),
     275          12 :   direct(true),
     276          12 :   doneigh(false),
     277          12 :   test(false),
     278          12 :   CompDer(false),
     279          24 :   com(false)
     280             : {
     281          12 :   log << "Starting PIV Constructor\n";
     282             : 
     283             :   // Precision on the real-to-integer transformation for the sorting
     284          12 :   parse("PRECISION",Nprec);
     285          12 :   if(Nprec<2) error("Precision must be => 2");
     286             : 
     287             :   // PBC
     288          12 :   bool nopbc=!pbc;
     289          12 :   parseFlag("NOPBC",nopbc);
     290          12 :   pbc=!nopbc;
     291          12 :   if(pbc) {
     292          12 :     log << "Using Periodic Boundary Conditions\n";
     293             :   } else  {
     294           0 :     log << "Isolated System (NO PBC)\n";
     295             :   }
     296             : 
     297             :   // SERIAL/PARALLEL
     298          12 :   parseFlag("SERIAL",serial);
     299          12 :   if(serial) {
     300           0 :     log << "Serial PIV construction\n";
     301             :   } else     {
     302          12 :     log << "Parallel PIV construction\n";
     303             :   }
     304             : 
     305             :   // Derivatives
     306          12 :   parseFlag("DERIVATIVES",CompDer);
     307          12 :   if(CompDer) log << "Computing Derivatives\n";
     308             : 
     309             :   // Timing
     310          12 :   parseFlag("TIMER",timer);
     311          12 :   if(timer) {
     312           1 :     log << "Timing analysis\n";
     313           1 :     stopwatch.start();
     314           1 :     stopwatch.pause();
     315             :   }
     316             : 
     317             :   // Test
     318          12 :   parseFlag("TEST",test);
     319             : 
     320             :   // UPDATEPIV
     321          24 :   if(keywords.exists("UPDATEPIV")) {
     322          24 :     parse("UPDATEPIV",updatePIV);
     323             :   }
     324             : 
     325             :   // Test
     326          12 :   parseFlag("COM",com);
     327          12 :   if(com) log << "Building PIV using COMs\n";
     328             : 
     329             :   // Volume Scaling
     330          12 :   parse("VOLUME",Vol0);
     331          12 :   if (Vol0>0) {
     332          12 :     Svol=true;
     333             :   }
     334             : 
     335             :   // PIV direct and cross blocks
     336          12 :   bool oc=false,od=false;
     337          12 :   parseFlag("ONLYCROSS",oc);
     338          12 :   parseFlag("ONLYDIRECT",od);
     339          12 :   if (oc&&od) {
     340           0 :     error("ONLYCROSS and ONLYDIRECT are incompatible options!");
     341             :   }
     342          12 :   if(oc) {
     343           4 :     direct=false;
     344           4 :     log << "Using only CROSS-PIV blocks\n";
     345             :   }
     346          12 :   if(od) {
     347           4 :     cross=false;
     348           4 :     log << "Using only DIRECT-PIV blocks\n";
     349             :   }
     350             : 
     351             :   // Atoms for PIV
     352          12 :   parse("PIVATOMS",Natm);
     353          12 :   std:: vector<string> atype(Natm);
     354          12 :   parseVector("ATOMTYPES",atype);
     355             :   //if(atype.size()!=getNumberOfArguments() && atype.size()!=0) error("not enough values for ATOMTYPES");
     356             : 
     357             :   // Reference PDB file
     358          12 :   parse("REF_FILE",ref_file);
     359          12 :   PDB mypdb;
     360          12 :   FILE* fp=fopen(ref_file.c_str(),"r");
     361          12 :   if (fp!=NULL) {
     362          12 :     log<<"Opening PDB file with reference frame: "<<ref_file.c_str()<<"\n";
     363          24 :     mypdb.readFromFilepointer(fp,plumed.getAtoms().usingNaturalUnits(),0.1/atoms.getUnits().getLength());
     364          12 :     fclose (fp);
     365             :   } else {
     366           0 :     error("Error in reference PDB file");
     367             :   }
     368             : 
     369             :   // Build COM/Atom lists of AtomNumbers (this might be done in PBC.cpp)
     370             :   // Atomlist or Plist used to build pair lists
     371          12 :   std:: vector<std:: vector<AtomNumber> > Plist(Natm);
     372             :   // Atomlist used to build list of atoms for each COM
     373          12 :   std:: vector<std:: vector<AtomNumber> > comatm(1);
     374             :   // NLsize is the number of atoms in the pdb cell
     375          12 :   NLsize=mypdb.getAtomNumbers().size();
     376             :   // In the following P stands for Point (either an Atom or a COM)
     377             :   unsigned resnum=0;
     378             :   // Presind (array size: number of residues) contains the contains the residue number
     379             :   //   this is because the residue numbers may not always be ordered from 1 to resnum
     380             :   std:: vector<unsigned> Presind;
     381             :   // Build Presind
     382       19404 :   for (unsigned i=0; i<mypdb.getAtomNumbers().size(); i++) {
     383       19392 :     unsigned rind=mypdb.getResidueNumber(mypdb.getAtomNumbers()[i]);
     384             :     bool oldres=false;
     385     7705008 :     for (unsigned j=0; j<Presind.size(); j++) {
     386     7685616 :       if(rind==Presind[j]) {
     387             :         oldres=true;
     388             :       }
     389             :     }
     390       19392 :     if(!oldres) {
     391        4848 :       Presind.push_back(rind);
     392             :     }
     393             :   }
     394          12 :   resnum=Presind.size();
     395             : 
     396             :   // Pind0 is the atom/COM used in Nlists (for COM Pind0 is the first atom in the pdb belonging to that COM)
     397             :   unsigned Pind0size;
     398          12 :   if(com) {
     399             :     Pind0size=resnum;
     400             :   } else {
     401          12 :     Pind0size=NLsize;
     402             :   }
     403          12 :   std:: vector<unsigned> Pind0(Pind0size);
     404             :   // If COM resize important arrays
     405          12 :   comatm.resize(NLsize);
     406          12 :   if(com) {
     407           0 :     nlcom.resize(NLsize);
     408           0 :     compos.resize(NLsize);
     409           0 :     fmass.resize(NLsize,0.);
     410             :   }
     411          12 :   log << "Total COM/Atoms: " << Natm*resnum << " \n";
     412             :   // Build lists of Atoms/COMs for NLists
     413             :   //   comatm filled also for non_COM calculation for analysis purposes
     414          36 :   for (unsigned j=0; j<Natm; j++) {
     415             :     unsigned oind;
     416       38808 :     for (unsigned i=0; i<Pind0.size(); i++) {
     417       38784 :       Pind0[i]=0;
     418             :     }
     419       38808 :     for (unsigned i=0; i<mypdb.getAtomNumbers().size(); i++) {
     420             :       // Residue/Atom AtomNumber: used to build NL for COMS/Atoms pairs.
     421       38784 :       AtomNumber anum=mypdb.getAtomNumbers()[i];
     422             :       // ResidueName/Atomname associated to atom
     423       38784 :       string rname=mypdb.getResidueName(anum);
     424       38784 :       string aname=mypdb.getAtomName(anum);
     425             :       // Index associated to residue/atom: used to separate COM-lists
     426       38784 :       unsigned rind=mypdb.getResidueNumber(anum);
     427             :       unsigned aind=anum.index();
     428             :       // This builds lists for NL
     429             :       string Pname;
     430             :       unsigned Pind;
     431       38784 :       if(com) {
     432             :         Pname=rname;
     433           0 :         for(unsigned l=0; l<resnum; l++) {
     434           0 :           if(rind==Presind[l]) {
     435             :             Pind=l;
     436             :           }
     437             :         }
     438             :       } else {
     439             :         Pname=aname;
     440             :         Pind=aind;
     441             :       }
     442       38784 :       if(Pname==atype[j]) {
     443       14544 :         if(Pind0[Pind]==0) {
     444             :           // adding the atomnumber to the atom/COM list for pairs
     445       14544 :           Plist[j].push_back(anum);
     446       14544 :           Pind0[Pind]=aind+1;
     447             :           oind=Pind;
     448             :         }
     449             :         // adding the atomnumber to list of atoms for every COM/Atoms
     450       14544 :         comatm[Pind0[Pind]-1].push_back(anum);
     451             :       }
     452             :     }
     453             :     // Output Lists
     454          24 :     log << "  Groups of type  " << j << ": " << Plist[j].size() << " \n";
     455             :     string gname;
     456             :     unsigned gsize;
     457          24 :     if(com) {
     458           0 :       gname=mypdb.getResidueName(comatm[Pind0[oind]-1][0]);
     459           0 :       gsize=comatm[Pind0[oind]-1].size();
     460             :     } else {
     461          48 :       gname=mypdb.getAtomName(comatm[Pind0[oind]-1][0]);
     462             :       gsize=1;
     463             :     }
     464          24 :     log.printf("    %6s %3s %13s %10i %6s\n", "type  ", gname.c_str(),"   containing ",gsize," atoms");
     465             :   }
     466             : 
     467             :   // This is to build the list with all the atoms
     468             :   std:: vector<AtomNumber> listall;
     469       19404 :   for (unsigned i=0; i<mypdb.getAtomNumbers().size(); i++) {
     470       19392 :     listall.push_back(mypdb.getAtomNumbers()[i]);
     471             :   }
     472             : 
     473             :   // PIV blocks and Neighbour Lists
     474          12 :   Nlist=0;
     475             :   // Direct adds the A-A ad B-B blocks (N)
     476          12 :   if(direct) {
     477           8 :     Nlist=Nlist+unsigned(Natm);
     478             :   }
     479             :   // Cross adds the A-B blocks (N*(N-1)/2)
     480          12 :   if(cross) {
     481           8 :     Nlist=Nlist+unsigned(double(Natm*(Natm-1))/2.);
     482             :   }
     483             :   // Resize vectors according to Nlist
     484          12 :   rPIV.resize(Nlist);
     485             : 
     486             :   // PIV scaled option
     487          12 :   scaling.resize(Nlist);
     488          36 :   for(unsigned j=0; j<Nlist; j++) {
     489          24 :     scaling[j]=1.;
     490             :   }
     491          24 :   if(keywords.exists("SFACTOR")) {
     492          24 :     parseVector("SFACTOR",scaling);
     493             :     //if(scaling.size()!=getNumberOfArguments() && scaling.size()!=0) error("not enough values for SFACTOR");
     494             :   }
     495             :   // Neighbour Lists option
     496          12 :   parseFlag("NLIST",doneigh);
     497          12 :   nl.resize(Nlist);
     498          12 :   nl_skin.resize(Nlist);
     499          12 :   if(doneigh) {
     500          12 :     std:: vector<double> nl_cut(Nlist,0.);
     501          12 :     std:: vector<int> nl_st(Nlist,0);
     502          12 :     parseVector("NL_CUTOFF",nl_cut);
     503             :     //if(nl_cut.size()!=getNumberOfArguments() && nl_cut.size()!=0) error("not enough values for NL_CUTOFF");
     504          12 :     parseVector("NL_STRIDE",nl_st);
     505             :     //if(nl_st.size()!=getNumberOfArguments() && nl_st.size()!=0) error("not enough values for NL_STRIDE");
     506          12 :     parseVector("NL_SKIN",nl_skin);
     507             :     //if(nl_skin.size()!=getNumberOfArguments() && nl_skin.size()!=0) error("not enough values for NL_SKIN");
     508          36 :     for (unsigned j=0; j<Nlist; j++) {
     509          24 :       if(nl_cut[j]<=0.0) error("NL_CUTOFF should be explicitly specified and positive");
     510          24 :       if(nl_st[j]<=0) error("NL_STRIDE should be explicitly specified and positive");
     511          24 :       if(nl_skin[j]<=0.) error("NL_SKIN should be explicitly specified and positive");
     512          24 :       nl_cut[j]=nl_cut[j]+nl_skin[j];
     513             :     }
     514          12 :     log << "Creating Neighbor Lists \n";
     515             :     // WARNING: is nl_cut meaningful here?
     516          12 :     nlall= new NeighborList(listall,true,pbc,getPbc(),comm,nl_cut[0],nl_st[0]);
     517          12 :     if(com) {
     518             :       //Build lists of Atoms for every COM
     519           0 :       for (unsigned i=0; i<compos.size(); i++) {
     520             :         // WARNING: is nl_cut meaningful here?
     521           0 :         nlcom[i]= new NeighborList(comatm[i],true,pbc,getPbc(),comm,nl_cut[0],nl_st[0]);
     522             :       }
     523             :     }
     524             :     unsigned ncnt=0;
     525             :     // Direct blocks AA, BB, CC, ...
     526          12 :     if(direct) {
     527          24 :       for (unsigned j=0; j<Natm; j++) {
     528          16 :         nl[ncnt]= new NeighborList(Plist[j],true,pbc,getPbc(),comm,nl_cut[j],nl_st[j]);
     529          16 :         ncnt+=1;
     530             :       }
     531             :     }
     532             :     // Cross blocks AB, AC, BC, ...
     533          12 :     if(cross) {
     534          24 :       for (unsigned j=0; j<Natm; j++) {
     535          24 :         for (unsigned i=j+1; i<Natm; i++) {
     536           8 :           nl[ncnt]= new NeighborList(Plist[i],Plist[j],true,false,pbc,getPbc(),comm,nl_cut[ncnt],nl_st[ncnt]);
     537           8 :           ncnt+=1;
     538             :         }
     539             :       }
     540             :     }
     541             :   } else {
     542           0 :     log << "WARNING: Neighbor List not activated this has not been tested!!  \n";
     543           0 :     nlall= new NeighborList(listall,true,pbc,getPbc(),comm);
     544           0 :     for (unsigned j=0; j<Nlist; j++) {
     545           0 :       nl[j]= new NeighborList(Plist[j],Plist[j],true,true,pbc,getPbc(),comm);
     546             :     }
     547             :   }
     548             :   // Output Nlist
     549          12 :   log << "Total Nlists: " << Nlist << " \n";
     550          36 :   for (unsigned j=0; j<Nlist; j++) {
     551          24 :     log << "  list " << j+1 << "   size " << nl[j]->size() << " \n";
     552             :   }
     553             :   // Calculate COM masses once and for all from lists
     554          12 :   if(com) {
     555           0 :     for(unsigned j=0; j<compos.size(); j++) {
     556             :       double commass=0.;
     557           0 :       for(unsigned i=0; i<nlcom[j]->getFullAtomList().size(); i++) {
     558           0 :         unsigned andx=nlcom[j]->getFullAtomList()[i].index();
     559           0 :         commass+=mypdb.getOccupancy()[andx];
     560             :       }
     561           0 :       for(unsigned i=0; i<nlcom[j]->getFullAtomList().size(); i++) {
     562           0 :         unsigned andx=nlcom[j]->getFullAtomList()[i].index();
     563           0 :         if(commass>0.) {
     564           0 :           fmass[andx]=mypdb.getOccupancy()[andx]/commass;
     565             :         } else {
     566           0 :           fmass[andx]=1.;
     567             :         }
     568             :       }
     569             :     }
     570             :   }
     571             : 
     572             :   // Sorting
     573          12 :   dosort.resize(Nlist);
     574          12 :   std:: vector<int> ynsort(Nlist);
     575          12 :   parseVector("SORT",ynsort);
     576          36 :   for (unsigned i=0; i<Nlist; i++) {
     577          24 :     if(ynsort[i]==0||CompDer) {
     578             :       dosort[i]=false;
     579             :     } else {
     580             :       dosort[i]=true;
     581             :     }
     582             :   }
     583             : 
     584             :   //build box vectors and correct for pbc
     585          12 :   log << "Building the box from PDB data ... \n";
     586          12 :   Tensor Box=mypdb.getBoxVec();
     587          12 :   log << "  Done! A,B,C vectors in Cartesian space:  \n";
     588          12 :   log.printf("  A:  %12.6f%12.6f%12.6f\n", Box[0][0],Box[0][1],Box[0][2]);
     589          12 :   log.printf("  B:  %12.6f%12.6f%12.6f\n", Box[1][0],Box[1][1],Box[1][2]);
     590          12 :   log.printf("  C:  %12.6f%12.6f%12.6f\n", Box[2][0],Box[2][1],Box[2][2]);
     591          12 :   log << "Changing the PBC according to the new box \n";
     592          12 :   Pbc mypbc;
     593          12 :   mypbc.setBox(Box);
     594          12 :   log << "The box volume is " << mypbc.getBox().determinant() << " \n";
     595             : 
     596             :   //Compute scaling factor
     597          12 :   if(Svol) {
     598          12 :     Fvol=cbrt(Vol0/mypbc.getBox().determinant());
     599          12 :     log << "Scaling atom distances by  " << Fvol << " \n";
     600             :   } else {
     601           0 :     log << "Using unscaled atom distances \n";
     602             :   }
     603             : 
     604          12 :   r00.resize(Nlist);
     605          12 :   sw.resize(Nlist);
     606          36 :   for (unsigned j=0; j<Nlist; j++) {
     607          48 :     if( !parseNumbered( "SWITCH", j+1, sw[j] ) ) break;
     608             :   }
     609          12 :   if(CompDer) {
     610             :     // Set switching function parameters here only if computing derivatives
     611             :     //   now set at the beginning of the dynamics to solve the r0 issue
     612           6 :     log << "Switching Function Parameters \n";
     613           6 :     sfs.resize(Nlist);
     614             :     std::string errors;
     615          18 :     for (unsigned j=0; j<Nlist; j++) {
     616          12 :       if(Svol) {
     617             :         double r0;
     618             :         std::string old_r0;
     619          12 :         vector<string> data=Tools::getWords(sw[j]);
     620             :         data.erase(data.begin());
     621          12 :         Tools::parse(data,"R_0",old_r0);
     622          12 :         Tools::convert(old_r0,r0);
     623          12 :         r0*=Fvol;
     624          12 :         std::string new_r0; Tools::convert(r0,new_r0);
     625          12 :         std::size_t pos = sw[j].find("R_0");
     626          12 :         sw[j].replace(pos+4,old_r0.size(),new_r0);
     627          12 :       }
     628          12 :       sfs[j].set(sw[j],errors);
     629             :       std::string num;
     630          12 :       Tools::convert(j+1, num);
     631          12 :       if( errors.length()!=0 ) error("problem reading SWITCH" + num + " keyword : " + errors );
     632          12 :       r00[j]=sfs[j].get_r0();
     633          24 :       log << "  Swf: " << j << "  r0=" << (sfs[j].description()).c_str() << " \n";
     634             :     }
     635             :   }
     636             : 
     637             :   // build COMs from positions if requested
     638          12 :   if(com) {
     639           0 :     for(unsigned j=0; j<compos.size(); j++) {
     640           0 :       compos[j][0]=0.;
     641           0 :       compos[j][1]=0.;
     642           0 :       compos[j][2]=0.;
     643           0 :       for(unsigned i=0; i<nlcom[j]->getFullAtomList().size(); i++) {
     644           0 :         unsigned andx=nlcom[j]->getFullAtomList()[i].index();
     645           0 :         compos[j]+=fmass[andx]*mypdb.getPositions()[andx];
     646             :       }
     647             :     }
     648             :   }
     649             :   // build the rPIV distances (transformation and sorting is done afterwards)
     650          12 :   if(CompDer) {
     651           6 :     log << "  PIV  |  block   |     Size      |     Zeros     |     Ones      |" << " \n";
     652             :   }
     653          36 :   for(unsigned j=0; j<Nlist; j++) {
     654    11516328 :     for(unsigned i=0; i<nl[j]->size(); i++) {
     655    11516304 :       unsigned i0=(nl[j]->getClosePairAtomNumber(i).first).index();
     656    11516304 :       unsigned i1=(nl[j]->getClosePairAtomNumber(i).second).index();
     657             :       //calculate/get COM position of centers i0 and i1
     658    11516304 :       Vector Pos0,Pos1;
     659    11516304 :       if(com) {
     660             :         //if(pbc) makeWhole();
     661           0 :         Pos0=compos[i0];
     662           0 :         Pos1=compos[i1];
     663             :       } else {
     664    11516304 :         Pos0=mypdb.getPositions()[i0];
     665    11516304 :         Pos1=mypdb.getPositions()[i1];
     666             :       }
     667    11516304 :       Vector ddist;
     668    11516304 :       if(pbc) {
     669    11516304 :         ddist=mypbc.distance(Pos0,Pos1);
     670             :       } else {
     671           0 :         ddist=delta(Pos0,Pos1);
     672             :       }
     673    11516304 :       double df=0.;
     674             :       // Transformation and sorting done at the first timestep to solve the r0 definition issue
     675    11516304 :       if(CompDer) {
     676        1104 :         rPIV[j].push_back(sfs[j].calculate(ddist.modulo()*Fvol, df));
     677             :       } else {
     678    11515200 :         rPIV[j].push_back(ddist.modulo()*Fvol);
     679             :       }
     680             :     }
     681          24 :     if(CompDer) {
     682          12 :       if(dosort[j]) {
     683           0 :         std::sort(rPIV[j].begin(),rPIV[j].end());
     684             :       }
     685             :       int lmt0=0;
     686             :       int lmt1=0;
     687        1116 :       for(unsigned i=0; i<rPIV[j].size(); i++) {
     688        1104 :         if(int(rPIV[j][i]*double(Nprec-1))==0) {
     689           0 :           lmt0+=1;
     690             :         }
     691        1104 :         if(int(rPIV[j][i]*double(Nprec-1))==1) {
     692           0 :           lmt1+=1;
     693             :         }
     694             :       }
     695          12 :       log.printf("       |%10i|%15zu|%15i|%15i|\n", j, rPIV[j].size(), lmt0, lmt1);
     696             :     }
     697             :   }
     698             : 
     699          12 :   checkRead();
     700             :   // From the plumed manual on how to build-up a new Colvar
     701          12 :   addValueWithDerivatives();
     702          12 :   requestAtoms(nlall->getFullAtomList());
     703          12 :   setNotPeriodic();
     704             :   // getValue()->setPeridodicity(false);
     705             :   // set size of derivative vector
     706          12 :   m_deriv.resize(getNumberOfAtoms());
     707          24 : }
     708             : 
     709             : // The following deallocates pointers
     710          24 : PIV::~PIV()
     711             : {
     712          36 :   for (unsigned j=0; j<Nlist; j++) {
     713          24 :     delete nl[j];
     714             :   }
     715          12 :   if(com) {
     716           0 :     for (unsigned j=0; j<NLsize; j++) {
     717           0 :       delete nlcom[j];
     718             :     }
     719             :   }
     720          12 :   delete nlall;
     721          48 : }
     722             : 
     723         327 : void PIV::calculate()
     724             : {
     725             : 
     726             :   // Local variables
     727             :   // The following are probably needed as static arrays
     728             :   static int prev_stp=-1;
     729             :   static int init_stp=1;
     730         327 :   static std:: vector<std:: vector<Vector> > prev_pos(Nlist);
     731         327 :   static std:: vector<std:: vector<double> > cPIV(Nlist);
     732         327 :   static std:: vector<std:: vector<int> > Atom0(Nlist);
     733         327 :   static std:: vector<std:: vector<int> > Atom1(Nlist);
     734         327 :   std:: vector<std:: vector<int> > A0(Nprec);
     735         327 :   std:: vector<std:: vector<int> > A1(Nprec);
     736             :   size_t stride=1;
     737             :   unsigned rank=0;
     738             : 
     739         327 :   if(!serial) {
     740         327 :     stride=comm.Get_size();
     741         327 :     rank=comm.Get_rank();
     742             :   } else {
     743             :     stride=1;
     744             :     rank=0;
     745             :   }
     746             : 
     747             :   // Transform (and sort) the rPIV before starting the dynamics
     748         327 :   if (((prev_stp==-1) || (init_stp==1)) &&!CompDer) {
     749           6 :     if(prev_stp!=-1) {init_stp=0;}
     750             :     // Calculate the volume scaling factor
     751           6 :     if(Svol) {
     752           6 :       Fvol=cbrt(Vol0/getBox().determinant());
     753             :     }
     754             :     //Set switching function parameters
     755           6 :     log << "\n";
     756           6 :     log << "REFERENCE PDB # " << prev_stp+2 << " \n";
     757             :     // Set switching function parameters here only if computing derivatives
     758             :     //   now set at the beginning of the dynamics to solve the r0 issue
     759           6 :     log << "Switching Function Parameters \n";
     760           6 :     sfs.resize(Nlist);
     761             :     std::string errors;
     762          18 :     for (unsigned j=0; j<Nlist; j++) {
     763          12 :       if(Svol) {
     764             :         double r0;
     765             :         std::string old_r0;
     766          12 :         vector<string> data=Tools::getWords(sw[j]);
     767             :         data.erase(data.begin());
     768          12 :         Tools::parse(data,"R_0",old_r0);
     769          12 :         Tools::convert(old_r0,r0);
     770          12 :         r0*=Fvol;
     771          12 :         std::string new_r0; Tools::convert(r0,new_r0);
     772          12 :         std::size_t pos = sw[j].find("R_0");
     773          12 :         sw[j].replace(pos+4,old_r0.size(),new_r0);
     774          12 :       }
     775          12 :       sfs[j].set(sw[j],errors);
     776             :       std::string num;
     777          12 :       Tools::convert(j+1, num);
     778          12 :       if( errors.length()!=0 ) error("problem reading SWITCH" + num + " keyword : " + errors );
     779          12 :       r00[j]=sfs[j].get_r0();
     780          24 :       log << "  Swf: " << j << "  r0=" << (sfs[j].description()).c_str() << " \n";
     781             :     }
     782             :     //Transform and sort
     783           6 :     log << "Building Reference PIV Vector \n";
     784           6 :     log << "  PIV  |  block   |     Size      |     Zeros     |     Ones      |" << " \n";
     785           6 :     double df=0.;
     786          18 :     for (unsigned j=0; j<Nlist; j++) {
     787    11515212 :       for (unsigned i=0; i<rPIV[j].size(); i++) {
     788    11515200 :         rPIV[j][i]=sfs[j].calculate(rPIV[j][i], df);
     789             :       }
     790          12 :       if(dosort[j]) {
     791          12 :         std::sort(rPIV[j].begin(),rPIV[j].end());
     792             :       }
     793             :       int lmt0=0;
     794             :       int lmt1=0;
     795    11515212 :       for(unsigned i=0; i<rPIV[j].size(); i++) {
     796    11515200 :         if(int(rPIV[j][i]*double(Nprec-1))==0) {
     797          26 :           lmt0+=1;
     798             :         }
     799    11515200 :         if(int(rPIV[j][i]*double(Nprec-1))==1) {
     800       63358 :           lmt1+=1;
     801             :         }
     802             :       }
     803          12 :       log.printf("       |%10i|%15zu|%15i|%15i|\n", j, rPIV[j].size(), lmt0, lmt1);
     804             :     }
     805           6 :     log << "\n";
     806             :   }
     807             :   // Do the sorting only once per timestep to avoid building the PIV N times for N rPIV PDB structures!
     808         327 :   if ((getStep()>prev_stp&&getStep()%updatePIV==0)||CompDer) {
     809         324 :     if (CompDer) log << " Step " << getStep() << "  Computing Derivatives NON-SORTED PIV \n";
     810             :     //
     811             :     // build COMs from positions if requested
     812         324 :     if(com) {
     813           0 :       if(pbc) makeWhole();
     814           0 :       for(unsigned j=0; j<compos.size(); j++) {
     815           0 :         compos[j][0]=0.;
     816           0 :         compos[j][1]=0.;
     817           0 :         compos[j][2]=0.;
     818           0 :         for(unsigned i=0; i<nlcom[j]->getFullAtomList().size(); i++) {
     819           0 :           unsigned andx=nlcom[j]->getFullAtomList()[i].index();
     820           0 :           compos[j]+=fmass[andx]*getPosition(andx);
     821             :         }
     822             :       }
     823             :     }
     824             :     // update neighbor lists when an atom moves out of the Neighbor list skin
     825         324 :     if (doneigh) {
     826             :       bool doupdate=false;
     827             :       // For the first step build previous positions = actual positions
     828         324 :       if (prev_stp==-1) {
     829           6 :         bool docom=com;
     830          18 :         for (unsigned j=0; j<Nlist; j++) {
     831        9708 :           for (unsigned i=0; i<nl[j]->getFullAtomList().size(); i++) {
     832        9696 :             Vector Pos;
     833        9696 :             if(docom) {
     834           0 :               Pos=compos[i];
     835             :             } else {
     836        9696 :               Pos=getPosition(nl[j]->getFullAtomList()[i].index());
     837             :             }
     838        9696 :             prev_pos[j].push_back(Pos);
     839             :           }
     840             :         }
     841             :         doupdate=true;
     842             :       }
     843             :       // Decide whether to update lists based on atom displacement, every stride
     844         324 :       std:: vector<std:: vector<Vector> > tmp_pos(Nlist);
     845         324 :       if (getStep() % nlall->getStride() ==0) {
     846         324 :         bool docom=com;
     847         972 :         for (unsigned j=0; j<Nlist; j++) {
     848       20520 :           for (unsigned i=0; i<nl[j]->getFullAtomList().size(); i++) {
     849       19872 :             Vector Pos;
     850       19872 :             if(docom) {
     851           0 :               Pos=compos[i];
     852             :             } else {
     853       19872 :               Pos=getPosition(nl[j]->getFullAtomList()[i].index());
     854             :             }
     855       19872 :             tmp_pos[j].push_back(Pos);
     856       19872 :             if (pbcDistance(tmp_pos[j][i],prev_pos[j][i]).modulo()>=nl_skin[j]) {
     857             :               doupdate=true;
     858             :             }
     859             :           }
     860             :         }
     861             :       }
     862             :       // Update Nlists if needed
     863         324 :       if (doupdate==true) {
     864          18 :         for (unsigned j=0; j<Nlist; j++) {
     865        9708 :           for (unsigned i=0; i<nl[j]->getFullAtomList().size(); i++) {
     866        9696 :             prev_pos[j][i]=tmp_pos[j][i];
     867             :           }
     868          12 :           nl[j]->update(prev_pos[j]);
     869          12 :           log << " Step " << getStep() << "  Neighbour lists updated " << nl[j]->size() << " \n";
     870             :         }
     871             :       }
     872         324 :     }
     873             :     // Calculate the volume scaling factor
     874         324 :     if(Svol) {
     875         324 :       Fvol=cbrt(Vol0/getBox().determinant());
     876             :     }
     877         324 :     Vector ddist;
     878             :     // Global to local variables
     879         324 :     bool doserial=serial;
     880             :     // Build "Nlist" PIV blocks
     881         972 :     for(unsigned j=0; j<Nlist; j++) {
     882         648 :       if(dosort[j]) {
     883             :         // from global to local variables to speedup the for loop with if statements
     884           6 :         bool docom=com;
     885           6 :         bool dopbc=pbc;
     886             :         // Vectors collecting occupancies: OrdVec one rank, OrdVecAll all ranks
     887           6 :         std:: vector<int> OrdVec(Nprec,0);
     888           6 :         cPIV[j].resize(0);
     889           6 :         Atom0[j].resize(0);
     890           6 :         Atom1[j].resize(0);
     891             :         // Building distances for the PIV vector at time t
     892           9 :         if(timer) stopwatch.start("1 Build cPIV");
     893     5757606 :         for(unsigned i=rank; i<nl[j]->size(); i+=stride) {
     894     5757600 :           unsigned i0=(nl[j]->getClosePairAtomNumber(i).first).index();
     895     5757600 :           unsigned i1=(nl[j]->getClosePairAtomNumber(i).second).index();
     896     5757600 :           Vector Pos0,Pos1;
     897     5757600 :           if(docom) {
     898           0 :             Pos0=compos[i0];
     899           0 :             Pos1=compos[i1];
     900             :           } else {
     901     5757600 :             Pos0=getPosition(i0);
     902     5757600 :             Pos1=getPosition(i1);
     903             :           }
     904     5757600 :           if(dopbc) {
     905     5757600 :             ddist=pbcDistance(Pos0,Pos1);
     906             :           } else {
     907           0 :             ddist=delta(Pos0,Pos1);
     908             :           }
     909     5757600 :           double df=0.;
     910             :           //Integer sorting ... faster!
     911             :           //Transforming distances with the Switching function + real to integer transformation
     912     5757600 :           int Vint=int(sfs[j].calculate(ddist.modulo()*Fvol, df)*double(Nprec-1)+0.5);
     913             :           //Integer transformed distance values as index of the Ordering Vector OrdVec
     914     5757600 :           OrdVec[Vint]+=1;
     915             :           //Keeps track of atom indices for force and virial calculations
     916     5757600 :           A0[Vint].push_back(i0);
     917     5757600 :           A1[Vint].push_back(i1);
     918             :         }
     919           9 :         if(timer) stopwatch.stop("1 Build cPIV");
     920           9 :         if(timer) stopwatch.start("2 Sort cPIV");
     921           6 :         if(!doserial && comm.initialized()) {
     922             :           // Vectors keeping track of the dimension and the starting-position of the rank-specific pair vector in the big pair vector.
     923           0 :           std:: vector<int> Vdim(stride,0);
     924           0 :           std:: vector<int> Vpos(stride,0);
     925             :           // Vectors collecting occupancies: OrdVec one rank, OrdVecAll all ranks
     926           0 :           std:: vector<int> OrdVecAll(stride*Nprec);
     927             :           // Big vectors containing all Atom indexes for every occupancy (Atom0O(Nprec,n) and Atom1O(Nprec,n) matrices in one vector)
     928             :           std:: vector<int> Atom0F;
     929             :           std:: vector<int> Atom1F;
     930             :           // Vector used to reconstruct arrays
     931           0 :           std:: vector<unsigned> k(stride,0);
     932             :           // Zeros might be many, this slows down a lot due to MPI communication
     933             :           // Avoid passing the zeros (i=1) for atom indices
     934           0 :           for(unsigned i=1; i<Nprec; i++) {
     935             :             // Building long vectors with all atom indexes for occupancies ordered from i=1 to i=Nprec-1
     936             :             // Can this be avoided ???
     937           0 :             Atom0F.insert(Atom0F.end(),A0[i].begin(),A0[i].end());
     938           0 :             Atom1F.insert(Atom1F.end(),A1[i].begin(),A1[i].end());
     939           0 :             A0[i].resize(0);
     940           0 :             A1[i].resize(0);
     941             :           }
     942             :           // Resize partial arrays to fill up for the next PIV block
     943           0 :           A0[0].resize(0);
     944           0 :           A1[0].resize(0);
     945           0 :           A0[Nprec-1].resize(0);
     946           0 :           A1[Nprec-1].resize(0);
     947             :           // Avoid passing the zeros (i=1) for atom indices
     948           0 :           OrdVec[0]=0;
     949           0 :           OrdVec[Nprec-1]=0;
     950             : 
     951             :           // Wait for all ranks before communication of Vectors
     952           0 :           comm.Barrier();
     953             : 
     954             :           // pass the array sizes before passing the arrays
     955           0 :           int dim=Atom0F.size();
     956             :           // Vdim and Vpos keep track of the dimension and the starting-position of the rank-specific pair vector in the big pair vector.
     957           0 :           comm.Allgather(&dim,1,&Vdim[0],1);
     958             : 
     959             :           // TO BE IMPROVED: the following may be done by the rank 0 (now every rank does it)
     960             :           int Fdim=0;
     961           0 :           for(unsigned i=1; i<stride; i++) {
     962           0 :             Vpos[i]=Vpos[i-1]+Vdim[i-1];
     963           0 :             Fdim+=Vdim[i];
     964             :           }
     965           0 :           Fdim+=Vdim[0];
     966             :           // build big vectors for atom pairs on all ranks for all ranks
     967           0 :           std:: vector<int> Atom0FAll(Fdim);
     968           0 :           std:: vector<int> Atom1FAll(Fdim);
     969             :           // TO BE IMPROVED: Allgathers may be substituted by gathers by proc 0
     970             :           //   Moreover vectors are gathered head-to-tail and assembled later-on in a serial step.
     971             :           // Gather the full Ordering Vector (occupancies). This is what we need to build the PIV
     972           0 :           comm.Allgather(&OrdVec[0],Nprec,&OrdVecAll[0],Nprec);
     973             :           // Gather the vectors of atom pairs to keep track of the idexes for the forces
     974           0 :           comm.Allgatherv(Atom0F.data(),Atom0F.size(),&Atom0FAll[0],&Vdim[0],&Vpos[0]);
     975           0 :           comm.Allgatherv(Atom1F.data(),Atom1F.size(),&Atom1FAll[0],&Vdim[0],&Vpos[0]);
     976             : 
     977             :           // Reconstruct the full vectors from collections of Allgathered parts (this is a serial step)
     978             :           // This is the tricky serial step, to assemble together PIV and atom-pair info from head-tail big vectors
     979             :           // Loop before on l and then on i would be better but the allgather should be modified
     980             :           // Loop on blocks
     981             :           //for(unsigned m=0;m<Nlist;m++) {
     982             :           // Loop on Ordering Vector size excluding zeros (i=1)
     983           0 :           if(timer) stopwatch.stop("2 Sort cPIV");
     984           0 :           if(timer) stopwatch.start("3 Reconstruct cPIV");
     985           0 :           for(unsigned i=1; i<Nprec; i++) {
     986             :             // Loop on the ranks
     987           0 :             for(unsigned l=0; l<stride; l++) {
     988             :               // Loop on the number of head-to-tail pieces
     989           0 :               for(unsigned m=0; m<OrdVecAll[i+l*Nprec]; m++) {
     990             :                 // cPIV is the current PIV at time t
     991           0 :                 cPIV[j].push_back(double(i)/double(Nprec-1));
     992           0 :                 Atom0[j].push_back(Atom0FAll[k[l]+Vpos[l]]);
     993           0 :                 Atom1[j].push_back(Atom1FAll[k[l]+Vpos[l]]);
     994           0 :                 k[l]+=1;
     995             :               }
     996             :             }
     997             :           }
     998           0 :           if(timer) stopwatch.stop("3 Reconstruct cPIV");
     999             :         } else {
    1000     6000000 :           for(unsigned i=1; i<Nprec; i++) {
    1001    11757594 :             for(unsigned m=0; m<OrdVec[i]; m++) {
    1002     5757600 :               cPIV[j].push_back(double(i)/double(Nprec-1));
    1003     5757600 :               Atom0[j].push_back(A0[i][m]);
    1004     5757600 :               Atom1[j].push_back(A1[i][m]);
    1005             :             }
    1006             :           }
    1007             :         }
    1008             :       }
    1009             :     }
    1010             :   }
    1011         327 :   Vector distance;
    1012         327 :   double dfunc=0.;
    1013             :   // Calculate volume scaling factor
    1014         327 :   if(Svol) {
    1015         327 :     Fvol=cbrt(Vol0/getBox().determinant());
    1016             :   }
    1017             : 
    1018             :   // This test may be run by specifying the TEST keyword as input, it pritnts rPIV and cPIV and quits
    1019         327 :   if(test) {
    1020             :     unsigned limit=0;
    1021           0 :     for(unsigned j=0; j<Nlist; j++) {
    1022           0 :       if(dosort[j]) {
    1023           0 :         limit = cPIV[j].size();
    1024             :       } else {
    1025           0 :         limit = rPIV[j].size();
    1026             :       }
    1027           0 :       log.printf("PIV Block:  %6i %12s %6i \n", j, "      Size:", limit);
    1028           0 :       log.printf("%6s%6s%12s%12s%36s\n","     i","     j", "    c-PIV   ","    r-PIV   ","   i-j distance vector       ");
    1029           0 :       for(unsigned i=0; i<limit; i++) {
    1030             :         unsigned i0=0;
    1031             :         unsigned i1=0;
    1032           0 :         if(dosort[j]) {
    1033           0 :           i0=Atom0[j][i];
    1034           0 :           i1=Atom1[j][i];
    1035             :         } else {
    1036           0 :           i0=(nl[j]->getClosePairAtomNumber(i).first).index();
    1037           0 :           i1=(nl[j]->getClosePairAtomNumber(i).second).index();
    1038             :         }
    1039           0 :         Vector Pos0,Pos1;
    1040           0 :         if(com) {
    1041           0 :           Pos0=compos[i0];
    1042           0 :           Pos1=compos[i1];
    1043             :         } else {
    1044           0 :           Pos0=getPosition(i0);
    1045           0 :           Pos1=getPosition(i1);
    1046             :         }
    1047           0 :         if(pbc) {
    1048           0 :           distance=pbcDistance(Pos0,Pos1);
    1049             :         } else {
    1050           0 :           distance=delta(Pos0,Pos1);
    1051             :         }
    1052           0 :         dfunc=0.;
    1053             :         double cP,rP;
    1054           0 :         if(dosort[j]) {
    1055           0 :           cP = cPIV[j][i];
    1056           0 :           rP = rPIV[j][rPIV[j].size()-cPIV[j].size()+i];
    1057             :         } else {
    1058           0 :           double dm=distance.modulo();
    1059           0 :           cP = sfs[j].calculate(dm*Fvol, dfunc);
    1060           0 :           rP = rPIV[j][i];
    1061             :         }
    1062           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]);
    1063             :       }
    1064             :     }
    1065           0 :     log.printf("This was a test, now exit \n");
    1066           0 :     exit();
    1067             :   }
    1068             : 
    1069         328 :   if(timer) stopwatch.start("4 Build For Derivatives");
    1070             :   // non-global variables Nder and Scalevol defined to speedup if structures in cycles
    1071         327 :   bool Nder=CompDer;
    1072         327 :   bool Scalevol=Svol;
    1073         327 :   if(getStep()%updatePIV==0) {
    1074             :     // set to zero PIVdistance, derivatives and virial when they are calculated
    1075       29799 :     for(unsigned j=0; j<m_deriv.size(); j++) {
    1076      117888 :       for(unsigned k=0; k<3; k++) {m_deriv[j][k]=0.;}
    1077             :     }
    1078        1308 :     for(unsigned j=0; j<3; j++) {
    1079        3924 :       for(unsigned k=0; k<3; k++) {
    1080        2943 :         m_virial[j][k]=0.;
    1081             :       }
    1082             :     }
    1083         327 :     m_PIVdistance=0.;
    1084             :     // Re-compute atomic distances for derivatives and compute PIV-PIV distance
    1085         981 :     for(unsigned j=0; j<Nlist; j++) {
    1086             :       unsigned limit=0;
    1087             :       // dosorting definition is to speedup if structure in cycles with non-global variables
    1088         654 :       bool dosorting=dosort[j];
    1089         654 :       bool docom=com;
    1090         654 :       bool dopbc=pbc;
    1091         654 :       if(dosorting) {
    1092          12 :         limit = cPIV[j].size();
    1093             :       } else {
    1094         642 :         limit = rPIV[j].size();
    1095             :       }
    1096    11574918 :       for(unsigned i=rank; i<limit; i+=stride) {
    1097             :         unsigned i0=0;
    1098             :         unsigned i1=0;
    1099    11574264 :         if(dosorting) {
    1100    11515200 :           i0=Atom0[j][i];
    1101    11515200 :           i1=Atom1[j][i];
    1102             :         } else {
    1103       59064 :           i0=(nl[j]->getClosePairAtomNumber(i).first).index();
    1104       59064 :           i1=(nl[j]->getClosePairAtomNumber(i).second).index();
    1105             :         }
    1106    11574264 :         Vector Pos0,Pos1;
    1107    11574264 :         if(docom) {
    1108           0 :           Pos0=compos[i0];
    1109           0 :           Pos1=compos[i1];
    1110             :         } else {
    1111    11574264 :           Pos0=getPosition(i0);
    1112    11574264 :           Pos1=getPosition(i1);
    1113             :         }
    1114    11574264 :         if(dopbc) {
    1115    11574264 :           distance=pbcDistance(Pos0,Pos1);
    1116             :         } else {
    1117           0 :           distance=delta(Pos0,Pos1);
    1118             :         }
    1119    11574264 :         dfunc=0.;
    1120             :         // this is needed for dfunc and dervatives
    1121    11574264 :         double dm=distance.modulo();
    1122    11574264 :         double tPIV = sfs[j].calculate(dm*Fvol, dfunc);
    1123             :         // PIV distance
    1124             :         double coord=0.;
    1125    11574264 :         if(!dosorting||Nder) {
    1126       59064 :           coord = tPIV - rPIV[j][i];
    1127             :         } else {
    1128    11515200 :           coord = cPIV[j][i] - rPIV[j][rPIV[j].size()-cPIV[j].size()+i];
    1129             :         }
    1130             :         // Calculate derivatives, virial, and variable=sum_j (scaling[j] *(cPIV-rPIV)_j^2)
    1131             :         // WARNING: dfunc=dswf/(Fvol*dm)  (this may change in future Plumed versions)
    1132    11574264 :         double tmp = 2.*scaling[j]*coord*Fvol*Fvol*dfunc;
    1133    11574264 :         Vector tmpder = tmp*distance;
    1134             :         // 0.5*(x_i-x_k)*f_ik         (force on atom k due to atom i)
    1135    11574264 :         if(docom) {
    1136           0 :           Vector dist;
    1137           0 :           for(unsigned k=0; k<nlcom[i0]->getFullAtomList().size(); k++) {
    1138           0 :             unsigned x0=nlcom[i0]->getFullAtomList()[k].index();
    1139           0 :             m_deriv[x0] -= tmpder*fmass[x0];
    1140           0 :             for(unsigned l=0; l<3; l++) {
    1141           0 :               dist[l]=0.;
    1142             :             }
    1143           0 :             Vector P0=getPosition(x0);
    1144           0 :             for(unsigned l=0; l<nlcom[i0]->getFullAtomList().size(); l++) {
    1145           0 :               unsigned x1=nlcom[i0]->getFullAtomList()[l].index();
    1146           0 :               Vector P1=getPosition(x1);
    1147           0 :               if(dopbc) {
    1148           0 :                 dist+=pbcDistance(P0,P1);
    1149             :               } else {
    1150           0 :                 dist+=delta(P0,P1);
    1151             :               }
    1152             :             }
    1153           0 :             for(unsigned l=0; l<nlcom[i1]->getFullAtomList().size(); l++) {
    1154           0 :               unsigned x1=nlcom[i1]->getFullAtomList()[l].index();
    1155           0 :               Vector P1=getPosition(x1);
    1156           0 :               if(dopbc) {
    1157           0 :                 dist+=pbcDistance(P0,P1);
    1158             :               } else {
    1159           0 :                 dist+=delta(P0,P1);
    1160             :               }
    1161             :             }
    1162           0 :             m_virial    -= 0.25*fmass[x0]*Tensor(dist,tmpder);
    1163             :           }
    1164           0 :           for(unsigned k=0; k<nlcom[i1]->getFullAtomList().size(); k++) {
    1165           0 :             unsigned x1=nlcom[i1]->getFullAtomList()[k].index();
    1166           0 :             m_deriv[x1] += tmpder*fmass[x1];
    1167           0 :             for(unsigned l=0; l<3; l++) {
    1168           0 :               dist[l]=0.;
    1169             :             }
    1170           0 :             Vector P1=getPosition(x1);
    1171           0 :             for(unsigned l=0; l<nlcom[i1]->getFullAtomList().size(); l++) {
    1172           0 :               unsigned x0=nlcom[i1]->getFullAtomList()[l].index();
    1173           0 :               Vector P0=getPosition(x0);
    1174           0 :               if(dopbc) {
    1175           0 :                 dist+=pbcDistance(P1,P0);
    1176             :               } else {
    1177           0 :                 dist+=delta(P1,P0);
    1178             :               }
    1179             :             }
    1180           0 :             for(unsigned l=0; l<nlcom[i0]->getFullAtomList().size(); l++) {
    1181           0 :               unsigned x0=nlcom[i0]->getFullAtomList()[l].index();
    1182           0 :               Vector P0=getPosition(x0);
    1183           0 :               if(dopbc) {
    1184           0 :                 dist+=pbcDistance(P1,P0);
    1185             :               } else {
    1186           0 :                 dist+=delta(P1,P0);
    1187             :               }
    1188             :             }
    1189           0 :             m_virial    += 0.25*fmass[x1]*Tensor(dist,tmpder);
    1190             :           }
    1191             :         } else {
    1192    11574264 :           m_deriv[i0] -= tmpder;
    1193    11574264 :           m_deriv[i1] += tmpder;
    1194    11574264 :           m_virial    -= tmp*Tensor(distance,distance);
    1195             :         }
    1196    11574264 :         if(Scalevol) {
    1197    11574264 :           m_virial+=1./3.*tmp*dm*dm*Tensor::identity();
    1198             :         }
    1199    11574264 :         m_PIVdistance    += scaling[j]*coord*coord;
    1200             :       }
    1201             :     }
    1202             : 
    1203         327 :     if (!serial && comm.initialized()) {
    1204           0 :       comm.Barrier();
    1205           0 :       comm.Sum(&m_PIVdistance,1);
    1206           0 :       if(!m_deriv.empty()) comm.Sum(&m_deriv[0][0],3*m_deriv.size());
    1207           0 :       comm.Sum(&m_virial[0][0],9);
    1208             :     }
    1209             :   }
    1210         327 :   prev_stp=getStep();
    1211             : 
    1212             :   //Timing
    1213         328 :   if(timer) stopwatch.stop("4 Build For Derivatives");
    1214         327 :   if(timer) {
    1215           1 :     log.printf("Timings for action %s with label %s \n", getName().c_str(), getLabel().c_str() );
    1216           1 :     log<<stopwatch;
    1217             :   }
    1218             : 
    1219             :   // Update derivatives, virial, and variable (PIV-distance^2)
    1220       29799 :   for(unsigned i=0; i<m_deriv.size(); ++i) setAtomsDerivatives(i,m_deriv[i]);
    1221         327 :   setValue           (m_PIVdistance);
    1222         327 :   setBoxDerivatives  (m_virial);
    1223         327 : }
    1224             : //Close Namespaces at the very beginning
    1225             : }
    1226             : }
    1227             : 

Generated by: LCOV version 1.15