LCOV - code coverage report
Current view: top level - generic - Accumulate.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 40 42 95.2 %
Date: 2024-10-18 13:59:31 Functions: 8 9 88.9 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2016-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 "core/ActionWithValue.h"
      23             : #include "core/ActionWithArguments.h"
      24             : #include "core/ActionPilot.h"
      25             : #include "core/ActionRegister.h"
      26             : #include "core/PlumedMain.h"
      27             : #include "core/ActionSet.h"
      28             : 
      29             : //+PLUMEDOC GRIDCALC ACCUMULATE
      30             : /*
      31             : Sum the elements of this value over the course of the trajectory
      32             : 
      33             : \par Examples
      34             : 
      35             : */
      36             : //+ENDPLUMEDOC
      37             : 
      38             : namespace PLMD {
      39             : namespace generic {
      40             : 
      41             : class Accumulate :
      42             :   public ActionWithValue,
      43             :   public ActionWithArguments,
      44             :   public ActionPilot
      45             : {
      46             : private:
      47             :   bool clearnextstep;
      48             :   unsigned clearstride;
      49             : public:
      50             :   static void registerKeywords( Keywords& keys );
      51             :   Accumulate( const ActionOptions& );
      52             :   unsigned getNumberOfDerivatives();
      53        4778 :   bool calculateOnUpdate() override { return false; }
      54         126 :   bool calculateConstantValues( const bool& have_atoms ) override { return false; }
      55        2328 :   void calculate() override {}
      56        2328 :   void apply() override {}
      57             :   void update() override ;
      58             : };
      59             : 
      60             : PLUMED_REGISTER_ACTION(Accumulate,"ACCUMULATE")
      61             : 
      62         127 : void Accumulate::registerKeywords( Keywords& keys ) {
      63         127 :   Action::registerKeywords( keys ); ActionWithValue::registerKeywords( keys );
      64         127 :   ActionWithArguments::registerKeywords( keys ); ActionPilot::registerKeywords( keys );
      65         254 :   keys.use("UPDATE_FROM"); keys.use("UPDATE_UNTIL");
      66         254 :   keys.addInputKeyword("compulsory","ARG","scalar/grid","the label of the argument that is being added to on each timestep");
      67         254 :   keys.add("compulsory","STRIDE","1","the frequency with which the data should be collected and added to the quantity being averaged");
      68         254 :   keys.add("compulsory","CLEAR","0","the frequency with which to clear all the accumulated data.  The default value "
      69             :            "of 0 implies that all the data will be used and that the grid will never be cleared");
      70         254 :   keys.setValueDescription("scalar/grid","a sum calculated from the time series of the input quantity");
      71         127 : }
      72             : 
      73          63 : Accumulate::Accumulate( const ActionOptions& ao ):
      74             :   Action(ao),
      75             :   ActionWithValue(ao),
      76             :   ActionWithArguments(ao),
      77             :   ActionPilot(ao),
      78          63 :   clearnextstep(true)
      79             : {
      80          63 :   if( getNumberOfArguments()!=1 ) error("there should only be one argument to this action");
      81          63 :   if( !getPntrToArgument(0)->hasDerivatives() && getPntrToArgument(0)->getRank()!=0 ) error("input to the accumulate action should be a scalar or a grid");
      82             : 
      83          63 :   parse("CLEAR",clearstride);
      84          63 :   if( clearstride>0 ) {
      85          11 :     if( clearstride%getStride()!=0 ) error("CLEAR parameter must be a multiple of STRIDE");
      86          11 :     log.printf("  clearing average every %u steps \n",clearstride);
      87             :   }
      88          63 :   std::vector<unsigned> shape( getPntrToArgument(0)->getShape() );
      89          63 :   addValueWithDerivatives( shape ); setNotPeriodic();
      90          63 :   if( getPntrToArgument(0)->isPeriodic() ) error("you cannot accumulate a periodic quantity");
      91          63 : }
      92             : 
      93          36 : unsigned Accumulate::getNumberOfDerivatives() {
      94          36 :   if( getPntrToArgument(0)->getRank()>0 ) return getPntrToArgument(0)->getNumberOfGridDerivatives();
      95           0 :   return getPntrToArgument(0)->getNumberOfDerivatives();
      96             : }
      97             : 
      98        2324 : void Accumulate::update() {
      99        2324 :   if( clearnextstep ) {
     100          70 :     if( getPntrToComponent(0)->getNumberOfValues()!=getPntrToArgument(0)->getNumberOfValues() ) {
     101           0 :       getPntrToComponent(0)->setShape( getPntrToArgument(0)->getShape() );
     102             :     }
     103          70 :     clearnextstep=false; getPntrToComponent(0)->set(0,0.0); getPntrToComponent(0)->clearDerivatives(true);
     104             :   }
     105        2324 :   if( getStep()==0 ) return;
     106             : 
     107        2262 :   Value* myarg=getPntrToArgument(0); Value* myout = getPntrToComponent(0);
     108        2262 :   if( getPntrToArgument(0)->getRank()>0 ) {
     109         118 :     unsigned nvals = myarg->getNumberOfValues(), nder = myarg->getNumberOfGridDerivatives();
     110      270581 :     for(unsigned i=0; i<nvals; ++i) {
     111      270463 :       myout->set( i, myout->get(i) + myarg->get(i) );
     112     1353300 :       for(unsigned j=0; j<nder; ++j) myout->addGridDerivatives( i, j, myarg->getGridDerivative( i, j ) );
     113             :     }
     114        2144 :   } else getPntrToComponent(0)->add( getPntrToArgument(0)->get() );
     115             : 
     116             :   // Clear if required
     117        2262 :   if( clearstride>0 && getStep()%clearstride==0 ) clearnextstep=true;
     118             : }
     119             : 
     120             : }
     121             : }

Generated by: LCOV version 1.16