LCOV - code coverage report
Current view: top level - function - Sum.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 18 19 94.7 %
Date: 2025-03-25 09:33:27 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2011-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 "Sum.h"
      23             : #include "FunctionShortcut.h"
      24             : #include "FunctionOfScalar.h"
      25             : #include "FunctionOfVector.h"
      26             : #include "FunctionOfMatrix.h"
      27             : #include "core/ActionRegister.h"
      28             : 
      29             : //+PLUMEDOC COLVAR SUM
      30             : /*
      31             : Calculate the sum of the arguments
      32             : 
      33             : This action takes a single vector or a single matrix in input. The output is a scalar
      34             : that contains the sum of all the elements in the input vector/matrix. This action is
      35             : very useful if you want to do calculations like the one illustrated in this example:
      36             : 
      37             : ```plumed
      38             : d: DISTANCE ATOMS1=1,2 ATOMS2=3,4 ATOMS3=5,6 ATOMS4=7,8
      39             : b: LESS_THAN ARG=d SWITCH={RATIONAL R_0=0.1}
      40             : s: SUM ARG=b PERIODIC=NO
      41             : PRINT ARG=s FILE=colvar
      42             : ```
      43             : 
      44             : This example calculates and outputs the number of distances that are less than 0.1 nm.
      45             : 
      46             : You can do something similar by summing the elements of a matrix as shown below:
      47             : 
      48             : ```plumed
      49             : c: CONTACT_MATRIX GROUP=1-100 SWITCH={RATIONAL R_0=0.1}
      50             : s: SUM ARG=c PERIODIC=NO
      51             : PRINT ARG=s FILE=colvar
      52             : ```
      53             : 
      54             : */
      55             : //+ENDPLUMEDOC
      56             : 
      57             : //+PLUMEDOC COLVAR SUM_VECTOR
      58             : /*
      59             : Calculate the sum of the elements in a vector
      60             : 
      61             : \par Examples
      62             : 
      63             : */
      64             : //+ENDPLUMEDOC
      65             : 
      66             : //+PLUMEDOC COLVAR SUM_SCALAR
      67             : /*
      68             : Calculate the SUM of the set of input scalars
      69             : 
      70             : \par Examples
      71             : 
      72             : */
      73             : //+ENDPLUMEDOC
      74             : 
      75             : //+PLUMEDOC COLVAR MEAN
      76             : /*
      77             : Calculate the arithmetic mean of the elements in a vector
      78             : 
      79             : This action takes a single vector or a single matrix in input.  The output is a scalar
      80             : that contains the mean of all the elements in the input vector/matrix. This action is
      81             : useful if you want do to calculations like the one illustrated in this example:
      82             : 
      83             : ```plumed
      84             : d: DISTANCE ATOMS1=1,2 ATOMS2=3,4 ATOMS3=5,6 ATOMS4=7,8
      85             : m: MEAN ARG=d PERIODIC=NO
      86             : PRINT ARG=m FILE=colvar
      87             : ```
      88             : 
      89             : The output from the MEAN action here is the average over the four distances that are evaluated by the
      90             : DISTANCE action.  Notice that you can also do the calculation above using:
      91             : 
      92             : ```plumed
      93             : d: DISTANCE ATOMS1=1,2 ATOMS2=3,4 ATOMS3=5,6 ATOMS4=7,8
      94             : s: SUM ARG=d PERIODIC=NO
      95             : m: CUSTOM ARG=s FUNC=x/4 PERIODIC=NO
      96             : PRINT ARG=m FILE=colvar
      97             : ```
      98             : 
      99             : */
     100             : //+ENDPLUMEDOC
     101             : 
     102             : //+PLUMEDOC COLVAR MEAN_SCALAR
     103             : /*
     104             : Calculate the arithmetic mean of the set of input scalars
     105             : 
     106             : \par Examples
     107             : 
     108             : */
     109             : //+ENDPLUMEDOC
     110             : 
     111             : //+PLUMEDOC COLVAR MEAN_VECTOR
     112             : /*
     113             : Calculate the arithmetic mean of the elements in a vector
     114             : 
     115             : \par Examples
     116             : 
     117             : */
     118             : //+ENDPLUMEDOC
     119             : 
     120             : //+PLUMEDOC COLVAR SUM_MATRIX
     121             : /*
     122             : Sum all the elements in a matrix
     123             : 
     124             : \par Examples
     125             : 
     126             : */
     127             : //+ENDPLUMEDOC
     128             : 
     129             : 
     130             : namespace PLMD {
     131             : namespace function {
     132             : 
     133             : typedef FunctionShortcut<Sum> SumShortcut;
     134             : PLUMED_REGISTER_ACTION(SumShortcut,"SUM")
     135             : PLUMED_REGISTER_ACTION(SumShortcut,"MEAN")
     136             : typedef FunctionOfScalar<Sum> ScalarSum;
     137             : PLUMED_REGISTER_ACTION(ScalarSum,"SUM_SCALAR")
     138             : PLUMED_REGISTER_ACTION(ScalarSum,"MEAN_SCALAR")
     139             : typedef FunctionOfVector<Sum> VectorSum;
     140             : PLUMED_REGISTER_ACTION(VectorSum,"SUM_VECTOR")
     141             : PLUMED_REGISTER_ACTION(VectorSum,"MEAN_VECTOR")
     142             : typedef FunctionOfMatrix<Sum> MatrixSum;
     143             : PLUMED_REGISTER_ACTION(MatrixSum,"SUM_MATRIX")
     144             : 
     145        3318 : void Sum::registerKeywords( Keywords& keys ) {
     146        3318 :   keys.use("PERIODIC");
     147        6636 :   keys.setValueDescription("scalar","the sum");
     148        3318 : }
     149             : 
     150         914 : void Sum::read( ActionWithArguments* action ) {
     151         914 :   if( action->getNumberOfArguments()!=1 ) {
     152           0 :     action->error("should only be one argument to sum actions");
     153             :   }
     154         914 : }
     155             : 
     156        1004 : void Sum::setPrefactor( ActionWithArguments* action, const double pref ) {
     157        1004 :   if(action->getName().find("MEAN")!=std::string::npos) {
     158         177 :     prefactor = pref / (action->getPntrToArgument(0))->getNumberOfValues();
     159             :   } else {
     160         827 :     prefactor = pref;
     161             :   }
     162        1004 : }
     163             : 
     164       25628 : bool Sum::zeroRank() const {
     165       25628 :   return true;
     166             : }
     167             : 
     168     1916368 : void Sum::calc( const ActionWithArguments* action, const std::vector<double>& args, std::vector<double>& vals, Matrix<double>& derivatives ) const {
     169     1916368 :   vals[0]=prefactor*args[0];
     170     1916368 :   derivatives(0,0)=prefactor;
     171     1916368 : }
     172             : 
     173             : }
     174             : }

Generated by: LCOV version 1.16