LCOV - code coverage report
Current view: top level - adjmat - MatrixColumnSums.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 34 34 100.0 %
Date: 2018-12-19 07:49:13 Functions: 10 11 90.9 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2015-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 "ActionWithInputMatrix.h"
      23             : #include "multicolvar/AtomValuePack.h"
      24             : #include "AdjacencyMatrixVessel.h"
      25             : #include "AdjacencyMatrixBase.h"
      26             : #include "core/ActionRegister.h"
      27             : #include "core/PlumedMain.h"
      28             : #include "core/ActionSet.h"
      29             : 
      30             : //+PLUMEDOC MATRIXF COLUMNSUMS
      31             : /*
      32             : Sum the columns of a contact matrix
      33             : 
      34             : As discussed in the section of the manual on \ref contactmatrix a useful tool for developing complex collective variables is the notion of the
      35             : so called adjacency matrix.  An adjacency matrix is an \f$N \times N\f$ matrix in which the \f$i\f$th, \f$j\f$th element tells you whether
      36             : or not the \f$i\f$th and \f$j\f$th atoms/molecules from a set of \f$N\f$ atoms/molecules are adjacent or not.  This action allows you to calculate
      37             : the sum of the columns in this adjacency matrix and to then calculate further functions of these quantities.
      38             : 
      39             : \par Examples
      40             : 
      41             : The first instruction in the following input file tells PLUMED to compute a \f$10 \times 10\f$ matrix in which the \f$ij\f$-element
      42             : tells you whether atoms \f$i\f$ and \f$j\f$ are within 1.0 nm of each other.  The numbers in each of this rows are then added together
      43             : and the average value is computed.  As such the following input provides an alternative method for calculating the coordination numbers
      44             : of atoms 1 to 10.
      45             : 
      46             : \verbatim
      47             : mat: CONTACT_MATRIX ATOMS=1-10 SWITCH={RATIONAL R_0=1.0}
      48             : rsums: COLUMNSUMS MATRIX=mat MEAN
      49             : PRINT ARG=rsums.* FILE=colvar
      50             : \endverbatim
      51             : 
      52             : The following input demonstrates another way that an average coordination number can be computed.  This input calculates the number of atoms
      53             : with indices between 1 and 5 that are within the first coordination spheres of each of the atoms within indices between 6 and 15.  The average
      54             : coordination number is then calculated from these fifteen coordination numbers and this quantity is output to a file.
      55             : 
      56             : \verbatim
      57             : mat2: CONTACT_MATRIX ATOMSA=1-5 ATOMSB=6-15 SWITCH={RATIONAL R_0=1.0}
      58             : rsums: COLUMNSUMS MATRIX=mat2 MEAN
      59             : PRINT ARG=rsums.* FILE=colvar
      60             : \endverbatim
      61             : 
      62             : */
      63             : //+ENDPLUMEDOC
      64             : 
      65             : namespace PLMD {
      66             : namespace adjmat {
      67             : 
      68           6 : class MatrixColumnSums : public ActionWithInputMatrix {
      69             : public:
      70             :   static void registerKeywords( Keywords& keys );
      71             :   explicit MatrixColumnSums(const ActionOptions&);
      72             :   double compute( const unsigned& tinded, multicolvar::AtomValuePack& myatoms ) const ;
      73             : };
      74             : 
      75        2526 : PLUMED_REGISTER_ACTION(MatrixColumnSums,"COLUMNSUMS")
      76             : 
      77           4 : void MatrixColumnSums::registerKeywords( Keywords& keys ) {
      78           4 :   ActionWithInputMatrix::registerKeywords( keys );
      79           4 :   keys.use("ALT_MIN"); keys.use("LOWEST"); keys.use("HIGHEST"); keys.use("MEAN");
      80           4 :   keys.use("MEAN"); keys.use("MIN"); keys.use("MAX"); keys.use("LESS_THAN");
      81           4 :   keys.use("MORE_THAN"); keys.use("BETWEEN"); keys.use("HISTOGRAM"); keys.use("MOMENTS");
      82           4 : }
      83             : 
      84           3 : MatrixColumnSums::MatrixColumnSums(const ActionOptions& ao):
      85             :   Action(ao),
      86           3 :   ActionWithInputMatrix(ao)
      87             : {
      88           3 :   if( (mymatrix->getMatrixAction())->mybasemulticolvars.size()>0 ) error("matrix row sums should only be calculated when inputs are atoms");
      89             :   // Setup the tasks
      90           3 :   unsigned ncols = mymatrix->getNumberOfColumns();
      91           3 :   ablocks.resize(1); ablocks[0].resize( ncols );
      92           3 :   for(unsigned i=0; i<ncols; ++i) addTaskToList( i );
      93             :   // Set the positions - this is only used when getting positions for central atoms
      94           3 :   if( mymatrix->undirectedGraph() ) {
      95           2 :     for(unsigned i=0; i<ncols; ++i) ablocks[0][i]=i;
      96             :   } else {
      97           1 :     for(unsigned i=0; i<ncols; ++i) ablocks[0][i]=mymatrix->getNumberOfRows() + i;
      98             :   }
      99           3 :   std::vector<AtomNumber> fake_atoms; setupMultiColvarBase( fake_atoms );
     100           3 : }
     101             : 
     102         164 : double MatrixColumnSums::compute( const unsigned& tinded, multicolvar::AtomValuePack& myatoms ) const {
     103         164 :   double sum=0.0; std::vector<double> tvals( mymatrix->getNumberOfComponents() );
     104         164 :   unsigned nrows = mymatrix->getNumberOfRows();
     105        5010 :   for(unsigned i=0; i<nrows; ++i) {
     106        4846 :     if( mymatrix->undirectedGraph() && tinded==i ) continue;
     107        4732 :     sum+=retrieveConnectionValue( i, tinded, tvals );
     108             :   }
     109             : 
     110         164 :   if( !doNotCalculateDerivatives() ) {
     111         100 :     MultiValue myvals( mymatrix->getNumberOfComponents(), myatoms.getNumberOfDerivatives() );
     112         100 :     MultiValue& myvout=myatoms.getUnderlyingMultiValue();
     113         850 :     for(unsigned i=0; i<nrows; ++i) {
     114         750 :       if( mymatrix->isSymmetric() && tinded==i ) continue ;
     115         700 :       addConnectionDerivatives( i, tinded, tvals, myvals, myvout );
     116         100 :     }
     117             :   }
     118         164 :   return sum;
     119             : }
     120             : 
     121             : }
     122        2523 : }

Generated by: LCOV version 1.13