LCOV - code coverage report
Current view: top level - colvar - ContactMap.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 121 132 91.7 %
Date: 2024-10-11 08:09:47 Functions: 6 8 75.0 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2012-2023 The plumed team
       3             :    (see the PEOPLE file at the root of the distribution for a list of names)
       4             : 
       5             :    See http://www.plumed.org for more information.
       6             : 
       7             :    This file is part of plumed, version 2.
       8             : 
       9             :    plumed is free software: you can redistribute it and/or modify
      10             :    it under the terms of the GNU Lesser General Public License as published by
      11             :    the Free Software Foundation, either version 3 of the License, or
      12             :    (at your option) any later version.
      13             : 
      14             :    plumed is distributed in the hope that it will be useful,
      15             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17             :    GNU Lesser General Public License for more details.
      18             : 
      19             :    You should have received a copy of the GNU Lesser General Public License
      20             :    along with plumed.  If not, see <http://www.gnu.org/licenses/>.
      21             : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
      22             : #include "Colvar.h"
      23             : #include "tools/NeighborList.h"
      24             : #include "ActionRegister.h"
      25             : #include "tools/SwitchingFunction.h"
      26             : 
      27             : namespace PLMD {
      28             : namespace colvar {
      29             : 
      30             : //+PLUMEDOC COLVAR CONTACTMAP
      31             : /*
      32             : Calculate the distances between a number of pairs of atoms and transform each distance by a switching function.
      33             : 
      34             : The transformed distance can be compared with a reference value in order to calculate the squared distance
      35             : between two contact maps. Each distance can also be weighted for a given value. CONTACTMAP can be used together
      36             : with \ref FUNCPATHMSD to define a path in the contactmap space.
      37             : 
      38             : The individual contact map distances related to each contact can be accessed as components
      39             : named `cm.contact-1`, `cm.contact-2`, etc, assuming that the label of the CONTACTMAP is `cm`.
      40             : 
      41             : \par Examples
      42             : 
      43             : The following example calculates switching functions based on the distances between atoms
      44             : 1 and 2, 3 and 4 and 4 and 5. The values of these three switching functions are then output
      45             : to a file named colvar.
      46             : 
      47             : \plumedfile
      48             : CONTACTMAP ATOMS1=1,2 ATOMS2=3,4 ATOMS3=4,5 ATOMS4=5,6 SWITCH={RATIONAL R_0=1.5} LABEL=f1
      49             : PRINT ARG=f1.* FILE=colvar
      50             : \endplumedfile
      51             : 
      52             : The following example calculates the difference of the current contact map with respect
      53             : to a reference provided. In this case REFERENCE is the fraction of contact that is formed
      54             : (i.e. the distance between two atoms transformed with the SWITCH), while R_0 is the contact
      55             : distance. WEIGHT gives the relative weight of each contact to the final distance measure.
      56             : 
      57             : \plumedfile
      58             : CONTACTMAP ...
      59             : ATOMS1=1,2 REFERENCE1=0.1 WEIGHT1=0.5
      60             : ATOMS2=3,4 REFERENCE2=0.5 WEIGHT2=1.0
      61             : ATOMS3=4,5 REFERENCE3=0.25 WEIGHT3=1.0
      62             : ATOMS4=5,6 REFERENCE4=0.0 WEIGHT4=0.5
      63             : SWITCH={RATIONAL R_0=1.5}
      64             : LABEL=cmap
      65             : CMDIST
      66             : ... CONTACTMAP
      67             : 
      68             : PRINT ARG=cmap FILE=colvar
      69             : \endplumedfile
      70             : 
      71             : The next example calculates calculates fraction of native contacts (Q)
      72             : for Trp-cage mini-protein. R_0 is the distance at which the switch function is guaranteed to
      73             : be 1.0 – it doesn't really matter for Q and  should be something very small, like 1 A.
      74             : REF is the reference distance for the contact, e.g. the distance from a crystal structure.
      75             : LAMBDA is the tolerance for the distance – if set to 1.0, the contact would have to have exactly
      76             : the reference value to be formed; instead for lambda values of 1.5–1.8 are usually used to allow some slack.
      77             : BETA is the softness of the switch function, default is 50nm.
      78             : WEIGHT is the 1/(number of contacts) giving equal weight to each contact.
      79             : 
      80             : When using native contact Q switch function, please cite \cite best2013
      81             : 
      82             : \plumedfile
      83             : # The full (much-longer) example available in regtest/basic/rt72/
      84             : 
      85             : CONTACTMAP ...
      86             : ATOMS1=1,67 SWITCH1={Q R_0=0.01 BETA=50.0 LAMBDA=1.5 REF=0.4059} WEIGHT1=0.003597
      87             : ATOMS2=1,68 SWITCH2={Q R_0=0.01 BETA=50.0 LAMBDA=1.5 REF=0.4039} WEIGHT2=0.003597
      88             : ATOMS3=1,69 SWITCH3={Q R_0=0.01 BETA=50.0 LAMBDA=1.5 REF=0.3215} WEIGHT3=0.003597
      89             : ATOMS4=5,61 SWITCH4={Q R_0=0.01 BETA=50.0 LAMBDA=1.5 REF=0.4277} WEIGHT4=0.003597
      90             : ATOMS5=5,67 SWITCH5={Q R_0=0.01 BETA=50.0 LAMBDA=1.5 REF=0.3851} WEIGHT5=0.003597
      91             : ATOMS6=5,68 SWITCH6={Q R_0=0.01 BETA=50.0 LAMBDA=1.5 REF=0.3811} WEIGHT6=0.003597
      92             : ATOMS7=5,69 SWITCH7={Q R_0=0.01 BETA=50.0 LAMBDA=1.5 REF=0.3133} WEIGHT7=0.003597
      93             : LABEL=cmap
      94             : SUM
      95             : ... CONTACTMAP
      96             : 
      97             : PRINT ARG=cmap FILE=colvar
      98             : \endplumedfile
      99             : (See also \ref switchingfunction)
     100             : 
     101             : */
     102             : //+ENDPLUMEDOC
     103             : 
     104             : class ContactMap : public Colvar {
     105             : private:
     106             :   bool pbc, serial, docomp, dosum, docmdist;
     107             :   std::unique_ptr<NeighborList> nl;
     108             :   std::vector<SwitchingFunction> sfs;
     109             :   std::vector<double> reference, weight;
     110             : public:
     111             :   static void registerKeywords( Keywords& keys );
     112             :   explicit ContactMap(const ActionOptions&);
     113             : // active methods:
     114             :   void calculate() override;
     115           0 :   void checkFieldsAllowed() override {}
     116             : };
     117             : 
     118       10435 : PLUMED_REGISTER_ACTION(ContactMap,"CONTACTMAP")
     119             : 
     120           9 : void ContactMap::registerKeywords( Keywords& keys ) {
     121           9 :   Colvar::registerKeywords( keys );
     122          18 :   keys.add("numbered","ATOMS","the atoms involved in each of the contacts you wish to calculate. "
     123             :            "Keywords like ATOMS1, ATOMS2, ATOMS3,... should be listed and one contact will be "
     124             :            "calculated for each ATOM keyword you specify.");
     125          18 :   keys.reset_style("ATOMS","atoms");
     126          18 :   keys.add("numbered","SWITCH","The switching functions to use for each of the contacts in your map. "
     127             :            "You can either specify a global switching function using SWITCH or one "
     128             :            "switching function for each contact. Details of the various switching "
     129             :            "functions you can use are provided on \\ref switchingfunction.");
     130          18 :   keys.add("numbered","REFERENCE","A reference value for a given contact, by default is 0.0 "
     131             :            "You can either specify a global reference value using REFERENCE or one "
     132             :            "reference value for each contact.");
     133          18 :   keys.add("numbered","WEIGHT","A weight value for a given contact, by default is 1.0 "
     134             :            "You can either specify a global weight value using WEIGHT or one "
     135             :            "weight value for each contact.");
     136          18 :   keys.reset_style("SWITCH","compulsory");
     137          18 :   keys.addFlag("SUM",false,"calculate the sum of all the contacts in the input");
     138          18 :   keys.addFlag("CMDIST",false,"calculate the distance with respect to the provided reference contact map");
     139          18 :   keys.addFlag("SERIAL",false,"Perform the calculation in serial - for debug purpose");
     140          18 :   keys.addOutputComponent("contact","default","By not using SUM or CMDIST each contact will be stored in a component");
     141           9 : }
     142             : 
     143           8 : ContactMap::ContactMap(const ActionOptions&ao):
     144             :   PLUMED_COLVAR_INIT(ao),
     145           8 :   pbc(true),
     146           8 :   serial(false),
     147           8 :   docomp(true),
     148           8 :   dosum(false),
     149           8 :   docmdist(false)
     150             : {
     151           8 :   parseFlag("SERIAL",serial);
     152           8 :   parseFlag("SUM",dosum);
     153           8 :   parseFlag("CMDIST",docmdist);
     154           8 :   if(docmdist==true&&dosum==true) error("You cannot use SUM and CMDIST together");
     155           8 :   bool nopbc=!pbc;
     156           8 :   parseFlag("NOPBC",nopbc);
     157           8 :   pbc=!nopbc;
     158             : 
     159             :   // Read in the atoms
     160             :   std::vector<AtomNumber> t, ga_lista, gb_lista;
     161           8 :   for(int i=1;; ++i ) {
     162        1186 :     parseAtomList("ATOMS", i, t );
     163         593 :     if( t.empty() ) break;
     164             : 
     165         585 :     if( t.size()!=2 ) {
     166           0 :       std::string ss; Tools::convert(i,ss);
     167           0 :       error("ATOMS" + ss + " keyword has the wrong number of atoms");
     168             :     }
     169         585 :     ga_lista.push_back(t[0]); gb_lista.push_back(t[1]);
     170         585 :     t.resize(0);
     171             : 
     172             :     // Add a value for this contact
     173         585 :     std::string num; Tools::convert(i,num);
     174         595 :     if(!dosum&&!docmdist) {addComponentWithDerivatives("contact-"+num); componentIsNotPeriodic("contact-"+num);}
     175         585 :   }
     176             :   // Create neighbour lists
     177          16 :   nl=Tools::make_unique<NeighborList>(ga_lista,gb_lista,serial,true,pbc,getPbc(),comm);
     178             : 
     179             :   // Read in switching functions
     180           8 :   std::string errors; sfs.resize( ga_lista.size() ); unsigned nswitch=0;
     181         593 :   for(unsigned i=0; i<ga_lista.size(); ++i) {
     182         585 :     std::string num, sw1; Tools::convert(i+1, num);
     183        1170 :     if( !parseNumbered( "SWITCH", i+1, sw1 ) ) break;
     184         585 :     nswitch++; sfs[i].set(sw1,errors);
     185         585 :     if( errors.length()!=0 ) error("problem reading SWITCH" + num + " keyword : " + errors );
     186             :   }
     187           8 :   if( nswitch==0 ) {
     188           0 :     std::string sw; parse("SWITCH",sw);
     189           0 :     if(sw.length()==0) error("no switching function specified use SWITCH keyword");
     190           0 :     for(unsigned i=0; i<ga_lista.size(); ++i) {
     191           0 :       sfs[i].set(sw,errors);
     192           0 :       if( errors.length()!=0 ) error("problem reading SWITCH keyword : " + errors );
     193             :     }
     194           8 :   } else if( nswitch!=sfs.size()  ) {
     195           0 :     std::string num; Tools::convert(nswitch+1, num);
     196           0 :     error("missing SWITCH" + num + " keyword");
     197             :   }
     198             : 
     199             :   // Read in reference values
     200             :   nswitch=0;
     201           8 :   reference.resize(ga_lista.size(), 0.);
     202          18 :   for(unsigned i=0; i<ga_lista.size(); ++i) {
     203          32 :     if( !parseNumbered( "REFERENCE", i+1, reference[i] ) ) break;
     204          10 :     nswitch++;
     205             :   }
     206           8 :   if( nswitch==0 ) {
     207           6 :     parse("REFERENCE",reference[0]);
     208         575 :     for(unsigned i=1; i<ga_lista.size(); ++i) {
     209         569 :       reference[i]=reference[0];
     210         569 :       nswitch++;
     211             :     }
     212             :   }
     213           8 :   if(nswitch == 0 && docmdist) error("with CMDIST one must use REFERENCE to setup the reference contact map");
     214             : 
     215             :   // Read in weight values
     216             :   nswitch=0;
     217           8 :   weight.resize(ga_lista.size(), 1.0);
     218         588 :   for(unsigned i=0; i<ga_lista.size(); ++i) {
     219        1162 :     if( !parseNumbered( "WEIGHT", i+1, weight[i] ) ) break;
     220         580 :     nswitch++;
     221             :   }
     222           8 :   if( nswitch==0 ) {
     223           1 :     parse("WEIGHT",weight[0]);
     224           5 :     for(unsigned i=1; i<ga_lista.size(); ++i) {
     225           4 :       weight[i]=weight[0];
     226             :     }
     227             :     nswitch = ga_lista.size();
     228             :   }
     229             : 
     230             :   // Output details of all contacts
     231         593 :   for(unsigned i=0; i<sfs.size(); ++i) {
     232         585 :     log.printf("  The %uth contact is calculated from atoms : %d %d. Inflection point of switching function is at %s. Reference contact value is %f\n",
     233        1170 :                i+1, ga_lista[i].serial(), gb_lista[i].serial(), ( sfs[i].description() ).c_str(), reference[i] );
     234             :   }
     235             : 
     236           8 :   if(dosum) {
     237           5 :     addValueWithDerivatives(); setNotPeriodic();
     238           5 :     log.printf("  colvar is sum of all contacts in contact map\n");
     239             :   }
     240           8 :   if(docmdist) {
     241           2 :     addValueWithDerivatives(); setNotPeriodic();
     242           2 :     log.printf("  colvar is distance between the contact map matrix and the provided reference matrix\n");
     243             :   }
     244             : 
     245           8 :   if(dosum || docmdist) {
     246           7 :     docomp=false;
     247             :   } else {
     248           1 :     serial=true;
     249           1 :     docomp=true;
     250             :   }
     251             : 
     252             :   // Set up if it is just a list of contacts
     253           8 :   requestAtoms(nl->getFullAtomList());
     254           8 :   checkRead();
     255           8 : }
     256             : 
     257        2215 : void ContactMap::calculate() {
     258             : 
     259        2215 :   double ncoord=0.;
     260        2215 :   Tensor virial;
     261        2215 :   std::vector<Vector> deriv(getNumberOfAtoms());
     262             : 
     263             :   unsigned stride;
     264             :   unsigned rank;
     265        2215 :   if(serial) {
     266             :     // when using components the parallelisation do not work
     267             :     stride=1;
     268             :     rank=0;
     269             :   } else {
     270        2210 :     stride=comm.Get_size();
     271        2210 :     rank=comm.Get_rank();
     272             :   }
     273             : 
     274             : // sum over close pairs
     275      149835 :   for(unsigned i=rank; i<nl->size(); i+=stride) {
     276      147620 :     Vector distance;
     277      147620 :     unsigned i0=nl->getClosePair(i).first;
     278      147620 :     unsigned i1=nl->getClosePair(i).second;
     279      147620 :     if(pbc) {
     280      147620 :       distance=pbcDistance(getPosition(i0),getPosition(i1));
     281             :     } else {
     282           0 :       distance=delta(getPosition(i0),getPosition(i1));
     283             :     }
     284             : 
     285      147620 :     double dfunc=0.;
     286      147620 :     double coord = weight[i]*(sfs[i].calculate(distance.modulo(), dfunc) - reference[i]);
     287      147620 :     Vector tmpder = weight[i]*dfunc*distance;
     288      147620 :     Tensor tmpvir = weight[i]*dfunc*Tensor(distance,distance);
     289      147620 :     if(!docmdist) {
     290      146595 :       deriv[i0] -= tmpder;
     291      146595 :       deriv[i1] += tmpder;
     292      146595 :       virial    -= tmpvir;
     293      146595 :       ncoord    += coord;
     294             :     } else {
     295        1025 :       tmpder *= 2.*coord;
     296        1025 :       tmpvir *= 2.*coord;
     297        1025 :       deriv[i0] -= tmpder;
     298        1025 :       deriv[i1] += tmpder;
     299        1025 :       virial    -= tmpvir;
     300        1025 :       ncoord    += coord*coord;
     301             :     }
     302             : 
     303      147620 :     if(docomp) {
     304          25 :       Value* val=getPntrToComponent( i );
     305          25 :       setAtomsDerivatives( val, i0, deriv[i0] );
     306          25 :       setAtomsDerivatives( val, i1, deriv[i1] );
     307          25 :       setBoxDerivatives( val, -tmpvir );
     308             :       val->set(coord);
     309             :     }
     310             :   }
     311             : 
     312        2215 :   if(!serial) {
     313        2210 :     comm.Sum(&ncoord,1);
     314        2210 :     if(!deriv.empty()) comm.Sum(&deriv[0][0],3*deriv.size());
     315        2210 :     comm.Sum(&virial[0][0],9);
     316             :   }
     317             : 
     318        2215 :   if( !docomp ) {
     319      297400 :     for(unsigned i=0; i<deriv.size(); ++i) setAtomsDerivatives(i,deriv[i]);
     320        2210 :     setValue           (ncoord);
     321        2210 :     setBoxDerivatives  (virial);
     322             :   }
     323        2215 : }
     324             : 
     325             : }
     326             : }

Generated by: LCOV version 1.15