LCOV - code coverage report
Current view: top level - colvar - PathMSDBase.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 172 175 98.3 %
Date: 2021-11-18 15:22:58 Functions: 6 9 66.7 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2012-2020 The plumed team
       3             :    (see the PEOPLE file at the root of the distribution for a list of names)
       4             : 
       5             :    See http://www.plumed.org for more information.
       6             : 
       7             :    This file is part of plumed, version 2.
       8             : 
       9             :    plumed is free software: you can redistribute it and/or modify
      10             :    it under the terms of the GNU Lesser General Public License as published by
      11             :    the Free Software Foundation, either version 3 of the License, or
      12             :    (at your option) any later version.
      13             : 
      14             :    plumed is distributed in the hope that it will be useful,
      15             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17             :    GNU Lesser General Public License for more details.
      18             : 
      19             :    You should have received a copy of the GNU Lesser General Public License
      20             :    along with plumed.  If not, see <http://www.gnu.org/licenses/>.
      21             : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
      22             : #include "PathMSDBase.h"
      23             : #include "Colvar.h"
      24             : #include "ActionRegister.h"
      25             : #include "core/PlumedMain.h"
      26             : #include "core/Atoms.h"
      27             : #include "tools/PDB.h"
      28             : #include "tools/RMSD.h"
      29             : #include "tools/Tools.h"
      30             : #include <cmath>
      31             : 
      32             : using namespace std;
      33             : 
      34             : namespace PLMD {
      35             : namespace colvar {
      36             : 
      37          27 : void PathMSDBase::registerKeywords(Keywords& keys) {
      38          27 :   Colvar::registerKeywords(keys);
      39         108 :   keys.add("compulsory","LAMBDA","the lambda parameter is needed for smoothing, is in the units of plumed");
      40         108 :   keys.add("compulsory","REFERENCE","the pdb is needed to provide the various milestones");
      41         108 :   keys.add("optional","NEIGH_SIZE","size of the neighbor list");
      42         108 :   keys.add("optional","NEIGH_STRIDE","how often the neighbor list needs to be calculated in time units");
      43         108 :   keys.add("optional", "EPSILON", "(default=-1) the maximum distance between the close and the current structure, the positive value turn on the close structure method");
      44         108 :   keys.add("optional", "LOG-CLOSE", "(default=0) value 1 enables logging regarding the close structure");
      45         108 :   keys.add("optional", "DEBUG-CLOSE", "(default=0) value 1 enables extensive debugging info regarding the close structure, the simulation will run much slower");
      46         108 :   keys.add("optional", "LOG_CLOSE", "same as LOG-CLOSE");
      47         108 :   keys.add("optional", "DEBUG_CLOSE", "same as DEBUG-CLOSE");
      48          27 : }
      49             : 
      50          25 : PathMSDBase::PathMSDBase(const ActionOptions&ao):
      51             :   PLUMED_COLVAR_INIT(ao),
      52             :   nopbc(false),
      53             :   neigh_size(-1),
      54             :   neigh_stride(-1),
      55             :   epsilonClose(-1),
      56             :   debugClose(0),
      57             :   logClose(0),
      58             :   computeRefClose(false),
      59         275 :   nframes(0)
      60             : {
      61          50 :   parse("LAMBDA",lambda);
      62          50 :   parse("NEIGH_SIZE",neigh_size);
      63          50 :   parse("NEIGH_STRIDE",neigh_stride);
      64          50 :   parse("REFERENCE",reference);
      65          50 :   parse("EPSILON", epsilonClose);
      66          50 :   parse("LOG-CLOSE", logClose);
      67          49 :   if(!logClose) parse("LOG_CLOSE", logClose);
      68          50 :   parse("DEBUG-CLOSE", debugClose);
      69          49 :   if(!debugClose) parse("DEBUG_CLOSE",debugClose);
      70          50 :   parseFlag("NOPBC",nopbc);
      71             : 
      72             :   // open the file
      73          50 :   FILE* fp=fopen(reference.c_str(),"r");
      74             :   std::vector<AtomNumber> aaa;
      75          25 :   if (fp!=NULL)
      76             :   {
      77          50 :     log<<"Opening reference file "<<reference.c_str()<<"\n";
      78             :     bool do_read=true;
      79        1107 :     while (do_read) {
      80        2189 :       PDB mypdb;
      81        2189 :       RMSD mymsd;
      82        2214 :       do_read=mypdb.readFromFilepointer(fp,plumed.getAtoms().usingNaturalUnits(),0.1/atoms.getUnits().getLength());
      83        1107 :       if(do_read) {
      84        1082 :         nframes++;
      85        2164 :         if(mypdb.getAtomNumbers().size()==0) error("number of atoms in a frame should be more than zero");
      86        1082 :         unsigned nat=mypdb.getAtomNumbers().size();
      87        2164 :         if(nat!=mypdb.getAtomNumbers().size()) error("frames should have the same number of atoms");
      88        1082 :         if(aaa.empty()) {
      89          25 :           aaa=mypdb.getAtomNumbers();
      90          50 :           log.printf("  found %z atoms in input \n",aaa.size());
      91          25 :           log.printf("  with indices : ");
      92        1013 :           for(unsigned i=0; i<aaa.size(); ++i) {
      93         321 :             if(i%25==0) log<<"\n";
      94         642 :             log.printf("%d ",aaa[i].serial());
      95             :           }
      96          25 :           log.printf("\n");
      97             :         }
      98        2164 :         if(aaa!=mypdb.getAtomNumbers()) error("frames should contain same atoms in same order");
      99        2164 :         log<<"Found PDB: "<<nframes<<" containing  "<<mypdb.getAtomNumbers().size()<<" atoms\n";
     100        1082 :         pdbv.push_back(mypdb);
     101        2164 :         derivs_s.resize(mypdb.getAtomNumbers().size());
     102        2164 :         derivs_z.resize(mypdb.getAtomNumbers().size());
     103        2164 :         mymsd.set(mypdb,"OPTIMAL");
     104        1082 :         msdv.push_back(mymsd); // the vector that stores the frames
     105             :       } else {break ;}
     106             :     }
     107          25 :     fclose (fp);
     108          50 :     log<<"Found TOTAL "<<nframes<< " PDB in the file "<<reference.c_str()<<" \n";
     109          25 :     if(nframes==0) error("at least one frame expected");
     110             :     //set up rmsdRefClose, initialize it to the first structure loaded from reference file
     111          75 :     rmsdPosClose.set(pdbv[0], "OPTIMAL");
     112          25 :     firstPosClose = true;
     113             :   }
     114          25 :   if(neigh_stride>0 || neigh_size>0) {
     115          14 :     if(neigh_size>int(nframes)) {
     116           0 :       log.printf(" List size required ( %d ) is too large: resizing to the maximum number of frames required: %u  \n",neigh_size,nframes);
     117           0 :       neigh_size=nframes;
     118             :     }
     119          14 :     log.printf("  Neighbor list enabled: \n");
     120          14 :     log.printf("                size   :  %d elements\n",neigh_size);
     121          14 :     log.printf("                stride :  %d timesteps \n",neigh_stride);
     122             :   } else {
     123          11 :     log.printf("  Neighbor list NOT enabled \n");
     124             :   }
     125          25 :   if (epsilonClose > 0) {
     126           2 :     log.printf(" Computing with the close structure, epsilon = %lf\n", epsilonClose);
     127           6 :     log << "  Bibliography " << plumed.cite("Pazurikova J, Krenek A, Spiwok V, Simkova M J. Chem. Phys. 146, 115101 (2017)") << "\n";
     128             :   }
     129             :   else {
     130          23 :     debugClose = 0;
     131          23 :     logClose = 0;
     132             :   }
     133          25 :   if (debugClose)
     134           2 :     log.printf(" Extensive debug info regarding close structure turned on\n");
     135             : 
     136          25 :   rotationRefClose.resize(nframes);
     137          50 :   savedIndices = vector<unsigned>(nframes);
     138             : 
     139          25 :   if(nopbc) log.printf("  without periodic boundary conditions\n");
     140          24 :   else      log.printf("  using periodic boundary conditions\n");
     141             : 
     142          25 : }
     143             : 
     144          75 : PathMSDBase::~PathMSDBase() {
     145          25 : }
     146             : 
     147       11179 : void PathMSDBase::calculate() {
     148             : 
     149       11179 :   if(neigh_size>0 && getExchangeStep()) error("Neighbor lists for this collective variable are not compatible with replica exchange, sorry for that!");
     150             : 
     151             :   //log.printf("NOW CALCULATE! \n");
     152             : 
     153       11179 :   if(!nopbc) makeWhole();
     154             : 
     155             : 
     156             :   // resize the list to full
     157       11179 :   if(imgVec.empty()) { // this is the signal that means: recalculate all
     158        7164 :     imgVec.resize(nframes);
     159             :     #pragma omp simd
     160        7164 :     for(unsigned i=0; i<nframes; i++) {
     161      601840 :       imgVec[i].property=indexvec[i];
     162      300920 :       imgVec[i].index=i;
     163             :     }
     164             :   }
     165             : 
     166             : // THIS IS THE HEAVY PART (RMSD STUFF)
     167       11179 :   unsigned stride=comm.Get_size();
     168       11179 :   unsigned rank=comm.Get_rank();
     169       11179 :   unsigned nat=pdbv[0].size();
     170       11179 :   plumed_assert(nat>0);
     171       11179 :   plumed_assert(nframes>0);
     172       11179 :   plumed_assert(imgVec.size()>0);
     173             : 
     174       11179 :   std::vector<Tensor> tmp_rotationRefClose(nframes);
     175             : 
     176       11179 :   if (epsilonClose > 0) {
     177             :     //compute rmsd between positions and close structure, save rotation matrix, drotation_drr01
     178        2184 :     double posclose = rmsdPosClose.calc_Rot_DRotDRr01(getPositions(), rotationPosClose, drotationPosCloseDrr01, true);
     179             :     //if we compute for the first time or the existing close structure is too far from current structure
     180        1092 :     if (firstPosClose || (posclose > epsilonClose)) {
     181             :       //set the current structure as close one for a few next steps
     182          16 :       if (logClose)
     183          16 :         log << "PLUMED-CLOSE: new close structure, rmsd pos close " << posclose << "\n";
     184          16 :       rmsdPosClose.clear();
     185          16 :       rmsdPosClose.setReference(getPositions());
     186             :       //as this is a new close structure, we need to save the rotation matrices fitted to the reference structures
     187             :       // and we need to accurately recalculate for all reference structures
     188          16 :       computeRefClose = true;
     189          16 :       imgVec.resize(nframes);
     190        1360 :       for(unsigned i=0; i<nframes; i++) {
     191        1344 :         imgVec[i].property=indexvec[i];
     192         672 :         imgVec[i].index=i;
     193             :       }
     194          16 :       firstPosClose = false;
     195             :     }
     196             :     else {
     197             :       //the current structure is pretty close to the close structure, so we use saved rotation matrices to decrease the complexity of rmsd comuptation
     198        1076 :       if (debugClose)
     199        1076 :         log << "PLUMED-CLOSE: old close structure, rmsd pos close " << posclose << "\n";
     200        1076 :       computeRefClose = false;
     201             :     }
     202             :   }
     203             : 
     204       22358 :   std::vector<double> tmp_distances(imgVec.size(),0.0);
     205             :   std::vector<Vector> tmp_derivs;
     206             : // this array is a merge of all tmp_derivs, so as to allow a single comm.Sum below
     207       11179 :   std::vector<Vector> tmp_derivs2(imgVec.size()*nat);
     208             : 
     209             : // if imgVec.size() is less than nframes, it means that only some msd will be calculated
     210       11179 :   if (epsilonClose > 0) {
     211        1092 :     if (computeRefClose) {
     212             :       //recompute rotation matrices accurately
     213        2048 :       for(unsigned i=rank; i<imgVec.size(); i+=stride) {
     214        1344 :         tmp_distances[i] = msdv[imgVec[i].index].calc_Rot(getPositions(), tmp_derivs, tmp_rotationRefClose[imgVec[i].index], true);
     215         672 :         plumed_assert(tmp_derivs.size()==nat);
     216             :         #pragma omp simd
     217       26208 :         for(unsigned j=0; j<nat; j++) tmp_derivs2[i*nat+j]=tmp_derivs[j];
     218             :       }
     219             :     }
     220             :     else {
     221             :       //approximate distance with saved rotation matrices
     222       92536 :       for(unsigned i=rank; i<imgVec.size(); i+=stride) {
     223      180768 :         tmp_distances[i] = msdv[imgVec[i].index].calculateWithCloseStructure(getPositions(), tmp_derivs, rotationPosClose, rotationRefClose[imgVec[i].index], drotationPosCloseDrr01, true);
     224       45192 :         plumed_assert(tmp_derivs.size()==nat);
     225             :         #pragma omp simd
     226     1762488 :         for(unsigned j=0; j<nat; j++) tmp_derivs2[i*nat+j]=tmp_derivs[j];
     227       45192 :         if (debugClose) {
     228       90384 :           double withclose = tmp_distances[i];
     229       90384 :           RMSD opt;
     230       90384 :           opt.setType("OPTIMAL");
     231      180768 :           opt.setReference(msdv[imgVec[i].index].getReference());
     232             :           vector<Vector> ders;
     233       45192 :           double withoutclose = opt.calculate(getPositions(), ders, true);
     234       45192 :           float difference = fabs(withoutclose-withclose);
     235       45192 :           log<<"PLUMED-CLOSE: difference original "<<withoutclose;
     236       90384 :           log<<" - with close "<<withclose<<" = "<<difference<<", step "<<getStep()<<", i "<<i<<" imgVec[i].index "<<imgVec[i].index<<"\n";
     237             :         }
     238             :       }
     239             :     }
     240             :   }
     241             :   else {
     242             :     // store temporary local results
     243      883256 :     for(unsigned i=rank; i<imgVec.size(); i+=stride) {
     244      575388 :       tmp_distances[i]=msdv[imgVec[i].index].calculate(getPositions(),tmp_derivs,true);
     245      287694 :       plumed_assert(tmp_derivs.size()==nat);
     246             :       #pragma omp simd
     247    11189466 :       for(unsigned j=0; j<nat; j++) tmp_derivs2[i*nat+j]=tmp_derivs[j];
     248             :     }
     249             :   }
     250             : 
     251             : // reduce over all processors
     252       11179 :   comm.Sum(tmp_distances);
     253       11179 :   comm.Sum(tmp_derivs2);
     254       11179 :   if (epsilonClose > 0 && computeRefClose) {
     255          16 :     comm.Sum(tmp_rotationRefClose);
     256        1360 :     for (unsigned i=0; i<nframes; i++) {
     257        1344 :       rotationRefClose[i] = tmp_rotationRefClose[i];
     258             :     }
     259             :   }
     260             : // assign imgVec[i].distance and imgVec[i].distder
     261     1435808 :   for(unsigned i=0; i<imgVec.size(); i++) {
     262      471150 :     imgVec[i].distance=tmp_distances[i];
     263      942300 :     imgVec[i].distder.assign(&tmp_derivs2[i*nat],nat+&tmp_derivs2[i*nat]);
     264             :   }
     265             : 
     266             : // END OF THE HEAVY PART
     267             : 
     268             :   vector<Value*> val_s_path;
     269       11179 :   if(labels.size()>0) {
     270       72072 :     for(unsigned i=0; i<labels.size(); i++) { val_s_path.push_back(getPntrToComponent(labels[i].c_str()));}
     271             :   } else {
     272       15519 :     val_s_path.push_back(getPntrToComponent("sss"));
     273             :   }
     274       22358 :   Value* val_z_path=getPntrToComponent("zzz");
     275             : 
     276       56728 :   vector<double> s_path(val_s_path.size()); for(unsigned i=0; i<s_path.size(); i++)s_path[i]=0.;
     277             :   double partition=0.;
     278             :   double tmp;
     279             : 
     280             :   // clean vector
     281      457727 :   for(unsigned i=0; i< derivs_z.size(); i++) {derivs_z[i].zero();}
     282             : 
     283      482329 :   for(auto & it : imgVec) {
     284      471150 :     it.similarity=exp(-lambda*(it.distance));
     285             :     //log<<"DISTANCE "<<(*it).distance<<"\n";
     286     3112506 :     for(unsigned i=0; i<s_path.size(); i++) {
     287     1446804 :       s_path[i]+=(it.property[i])*it.similarity;
     288             :     }
     289      471150 :     partition+=it.similarity;
     290             :   }
     291      125468 :   for(unsigned i=0; i<s_path.size(); i++) { s_path[i]/=partition;  val_s_path[i]->set(s_path[i]) ;}
     292       11179 :   val_z_path->set(-(1./lambda)*std::log(partition));
     293       73913 :   for(unsigned j=0; j<s_path.size(); j++) {
     294             :     // clean up
     295             :     #pragma omp simd
     296      240386 :     for(unsigned i=0; i< derivs_s.size(); i++) {derivs_s[i].zero();}
     297             :     // do the derivative
     298      740587 :     for(const auto & it : imgVec) {
     299      723402 :       double expval=it.similarity;
     300     2170206 :       tmp=lambda*expval*(s_path[j]-it.property[j])/partition;
     301             :       #pragma omp simd
     302    19511454 :       for(unsigned i=0; i< derivs_s.size(); i++) { derivs_s[i]+=tmp*it.distder[i] ;}
     303      723402 :       if(j==0) {
     304             :         #pragma omp simd
     305    12700650 :         for(unsigned i=0; i< derivs_z.size(); i++) { derivs_z[i]+=it.distder[i]*expval/partition;}
     306             :       }
     307             :     }
     308      703973 :     for(unsigned i=0; i< derivs_s.size(); i++) {
     309      669603 :       setAtomsDerivatives (val_s_path[j],i,derivs_s[i]);
     310      368324 :       if(j==0) {setAtomsDerivatives (val_z_path,i,derivs_z[i]);}
     311             :     }
     312             :   }
     313       73913 :   for(unsigned i=0; i<val_s_path.size(); ++i) setBoxDerivativesNoPbc(val_s_path[i]);
     314       11179 :   setBoxDerivativesNoPbc(val_z_path);
     315             :   //
     316             :   //  here set next round neighbors
     317             :   //
     318       11179 :   if (neigh_size>0) {
     319             :     //if( int(getStep())%int(neigh_stride/getTimeStep())==0 ){
     320             :     // enforce consistency: the stride is in time steps
     321        7153 :     if( int(getStep())%int(neigh_stride)==0 ) {
     322             : 
     323             :       // next round do it all:empty the vector
     324             :       imgVec.clear();
     325             :     }
     326             :     // time to analyze the results:
     327        7153 :     if(imgVec.size()==nframes) {
     328             :       //sort by msd
     329             :       sort(imgVec.begin(), imgVec.end(), imgOrderByDist());
     330             :       //resize
     331           0 :       imgVec.resize(neigh_size);
     332             :     }
     333             :   }
     334             :   //log.printf("CALCULATION DONE! \n");
     335       11179 : }
     336             : 
     337             : }
     338             : 
     339        5517 : }

Generated by: LCOV version 1.14