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 381 : keys.use("ARG"); keys.use("UPDATE_FROM"); keys.use("UPDATE_UNTIL"); 66 254 : keys.add("compulsory","STRIDE","1","the frequency with which the data should be collected and added to the quantity being averaged"); 67 254 : keys.add("compulsory","CLEAR","0","the frequency with which to clear all the accumulated data. The default value " 68 : "of 0 implies that all the data will be used and that the grid will never be cleared"); 69 127 : keys.setValueDescription("a sum calculated from the time series of the input quantity"); 70 127 : } 71 : 72 63 : Accumulate::Accumulate( const ActionOptions& ao ): 73 : Action(ao), 74 : ActionWithValue(ao), 75 : ActionWithArguments(ao), 76 : ActionPilot(ao), 77 63 : clearnextstep(true) 78 : { 79 63 : if( getNumberOfArguments()!=1 ) error("there should only be one argument to this action"); 80 63 : if( !getPntrToArgument(0)->hasDerivatives() && getPntrToArgument(0)->getRank()!=0 ) error("input to the accumulate action should be a scalar or a grid"); 81 : 82 63 : parse("CLEAR",clearstride); 83 63 : if( clearstride>0 ) { 84 11 : if( clearstride%getStride()!=0 ) error("CLEAR parameter must be a multiple of STRIDE"); 85 11 : log.printf(" clearing average every %u steps \n",clearstride); 86 : } 87 63 : std::vector<unsigned> shape( getPntrToArgument(0)->getShape() ); 88 63 : addValueWithDerivatives( shape ); setNotPeriodic(); 89 63 : if( getPntrToArgument(0)->isPeriodic() ) error("you cannot accumulate a periodic quantity"); 90 63 : } 91 : 92 36 : unsigned Accumulate::getNumberOfDerivatives() { 93 36 : if( getPntrToArgument(0)->getRank()>0 ) return getPntrToArgument(0)->getNumberOfGridDerivatives(); 94 0 : return getPntrToArgument(0)->getNumberOfDerivatives(); 95 : } 96 : 97 2324 : void Accumulate::update() { 98 2324 : if( clearnextstep ) { 99 140 : if( getPntrToComponent(0)->getNumberOfValues()!=getPntrToArgument(0)->getNumberOfValues() ) { 100 0 : getPntrToComponent(0)->setShape( getPntrToArgument(0)->getShape() ); 101 : } 102 70 : clearnextstep=false; getPntrToComponent(0)->set(0,0.0); getPntrToComponent(0)->clearDerivatives(true); 103 : } 104 2324 : if( getStep()==0 ) return; 105 : 106 2262 : Value* myarg=getPntrToArgument(0); Value* myout = getPntrToComponent(0); 107 2262 : if( getPntrToArgument(0)->getRank()>0 ) { 108 118 : unsigned nvals = myarg->getNumberOfValues(), nder = myarg->getNumberOfGridDerivatives(); 109 270581 : for(unsigned i=0; i<nvals; ++i) { 110 270463 : myout->set( i, myout->get(i) + myarg->get(i) ); 111 1353300 : for(unsigned j=0; j<nder; ++j) myout->addGridDerivatives( i, j, myarg->getGridDerivative( i, j ) ); 112 : } 113 2144 : } else getPntrToComponent(0)->add( getPntrToArgument(0)->get() ); 114 : 115 : // Clear if required 116 2262 : if( clearstride>0 && getStep()%clearstride==0 ) clearnextstep=true; 117 : } 118 : 119 : } 120 : }