LCOV - code coverage report
Current view: top level - multicolvar - Bridge.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 48 59 81.4 %
Date: 2018-12-19 07:49:13 Functions: 10 12 83.3 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2013-2018 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 "MultiColvar.h"
      23             : #include "tools/SwitchingFunction.h"
      24             : #include "core/ActionRegister.h"
      25             : 
      26             : #include <string>
      27             : #include <cmath>
      28             : 
      29             : using namespace std;
      30             : 
      31             : namespace PLMD {
      32             : namespace multicolvar {
      33             : 
      34             : //+PLUMEDOC MCOLVAR BRIDGE
      35             : /*
      36             : Calculate the number of atoms that bridge two parts of a structure
      37             : 
      38             : This quantity calculates:
      39             : 
      40             : \f[
      41             :  f(x) = \sum_{ijk} s_A(r_{ij})s_B(r_{ik})
      42             : \f]
      43             : 
      44             : where the sum over \f$i\f$ is over all the ``bridging atoms" and
      45             : \f$s_A\f$ and \f$s_B\f$ are \ref switchingfunction.
      46             : 
      47             : \par Examples
      48             : 
      49             : The following example instructs plumed to calculate the number of water molecules
      50             : that are bridging betweeen atoms 1-10 and atoms 11-20 and to print the value
      51             : to a file
      52             : 
      53             : \verbatim
      54             : BRIDGE BRIDGING_ATOMS=100-200 GROUPA=1-10 GROUPB=11-20 LABEL=w1
      55             : PRINT ARG=a1.mean FILE=colvar
      56             : \endverbatim
      57             : 
      58             : */
      59             : //+ENDPLUMEDOC
      60             : 
      61          18 : class Bridge : public MultiColvar {
      62             : private:
      63             :   Vector dij, dik;
      64             :   SwitchingFunction sf1;
      65             :   SwitchingFunction sf2;
      66             : public:
      67             :   static void registerKeywords( Keywords& keys );
      68             :   explicit Bridge(const ActionOptions&);
      69             : // active methods:
      70             :   virtual double compute( const unsigned& tindex, AtomValuePack& myatoms ) const ;
      71           0 :   bool isPeriodic() { return false; }
      72             : };
      73             : 
      74        2532 : PLUMED_REGISTER_ACTION(Bridge,"BRIDGE")
      75             : 
      76          10 : void Bridge::registerKeywords( Keywords& keys ) {
      77          10 :   MultiColvar::registerKeywords( keys );
      78          10 :   keys.use("ATOMS");
      79             :   keys.add("atoms-2","BRIDGING_ATOMS","The list of atoms that can form the bridge between the two interesting parts "
      80          10 :            "of the structure.");
      81          10 :   keys.add("atoms-2","GROUPA","The list of atoms that are in the first interesting part of the structure");
      82          10 :   keys.add("atoms-2","GROUPB","The list of atoms that are in the second interesting part of the structure");
      83          10 :   keys.add("optional","SWITCH","The parameters of the two \\ref switchingfunction in the above formula");
      84             :   keys.add("optional","SWITCHA","The \\ref switchingfunction on the distance between bridging atoms and the atoms in "
      85          10 :            "group A");
      86             :   keys.add("optional","SWITCHB","The \\ref switchingfunction on the distance between the bridging atoms and the atoms in "
      87          10 :            "group B");
      88          10 : }
      89             : 
      90           9 : Bridge::Bridge(const ActionOptions&ao):
      91           9 :   PLUMED_MULTICOLVAR_INIT(ao)
      92             : {
      93             :   // Read in the atoms
      94           9 :   std::vector<AtomNumber> all_atoms;
      95           9 :   readThreeGroups("GROUPA","GROUPB","BRIDGING_ATOMS",false,true,all_atoms);
      96             :   // Setup the multicolvar base
      97           9 :   setupMultiColvarBase( all_atoms );
      98             :   // Setup Central atom atoms
      99          18 :   std::vector<bool> catom_ind(3, false); catom_ind[0]=true;
     100           9 :   setAtomsForCentralAtom( catom_ind );
     101             : 
     102          18 :   std::string sfinput,errors; parse("SWITCH",sfinput);
     103           9 :   if( sfinput.length()>0 ) {
     104           9 :     sf1.set(sfinput,errors);
     105           9 :     if( errors.length()!=0 ) error("problem reading SWITCH keyword : " + errors );
     106           9 :     sf2.set(sfinput,errors);
     107           9 :     if( errors.length()!=0 ) error("problem reading SWITCH keyword : " + errors );
     108             :   } else {
     109           0 :     parse("SWITCHA",sfinput);
     110           0 :     if(sfinput.length()>0) {
     111           0 :       weightHasDerivatives=true;
     112           0 :       sf1.set(sfinput,errors);
     113           0 :       if( errors.length()!=0 ) error("problem reading SWITCHA keyword : " + errors );
     114           0 :       sfinput.clear(); parse("SWITCHB",sfinput);
     115           0 :       if(sfinput.length()==0) error("found SWITCHA keyword without SWITCHB");
     116           0 :       sf2.set(sfinput,errors);
     117           0 :       if( errors.length()!=0 ) error("problem reading SWITCHB keyword : " + errors );
     118             :     } else {
     119           0 :       error("missing definition of switching functions");
     120             :     }
     121             :   }
     122           9 :   log.printf("  distance between bridging atoms and atoms in GROUPA must be less than %s\n",sf1.description().c_str());
     123           9 :   log.printf("  distance between bridging atoms and atoms in GROUPB must be less than %s\n",sf2.description().c_str());
     124             : 
     125             :   // Setup link cells
     126           9 :   setLinkCellCutoff( sf1.get_dmax() + sf2.get_dmax() );
     127             : 
     128             :   // And setup the ActionWithVessel
     129           9 :   if( getNumberOfVessels()!=0 ) error("should not have vessels for this action");
     130          18 :   std::string fake_input;
     131           9 :   addVessel( "SUM", fake_input, -1 );  // -1 here means that this value will be named getLabel()
     132           9 :   readVesselKeywords();
     133             :   // And check everything has been read in correctly
     134          18 :   checkRead();
     135           9 : }
     136             : 
     137       50507 : double Bridge::compute( const unsigned& tindex, AtomValuePack& myatoms ) const {
     138       50507 :   double tot=0;
     139      571240 :   for(unsigned i=2; i<myatoms.getNumberOfAtoms(); ++i) {
     140      520736 :     Vector dij=getSeparation( myatoms.getPosition(i), myatoms.getPosition(0) );
     141      520774 :     double dw1, w1=sf1.calculateSqr( dij.modulo2(), dw1 );
     142      520772 :     Vector dik=getSeparation( myatoms.getPosition(i), myatoms.getPosition(1) );
     143      520791 :     double dw2, w2=sf2.calculateSqr( dik.modulo2(), dw2 );
     144             : 
     145      520802 :     tot += w1*w2;
     146             :     // And finish the calculation
     147      520802 :     addAtomDerivatives( 1, 0,  w2*dw1*dij, myatoms );
     148      520802 :     addAtomDerivatives( 1, 1,  w1*dw2*dik, myatoms );
     149      520777 :     addAtomDerivatives( 1, i, -w1*dw2*dik-w2*dw1*dij, myatoms );
     150      520786 :     myatoms.addBoxDerivatives( 1, w1*(-dw2)*Tensor(dik,dik)+w2*(-dw1)*Tensor(dij,dij) );
     151             :   }
     152       50527 :   return tot;
     153             : }
     154             : 
     155             : }
     156        2523 : }

Generated by: LCOV version 1.13