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 : private: 46 : bool clearnextstep; 47 : unsigned clearstride; 48 : public: 49 : static void registerKeywords( Keywords& keys ); 50 : Accumulate( const ActionOptions& ); 51 : unsigned getNumberOfDerivatives(); 52 4778 : bool calculateOnUpdate() override { 53 4778 : return false; 54 : } 55 126 : bool calculateConstantValues( const bool& have_atoms ) override { 56 126 : return false; 57 : } 58 2328 : void calculate() override {} 59 2328 : void apply() override {} 60 : void update() override ; 61 : }; 62 : 63 : PLUMED_REGISTER_ACTION(Accumulate,"ACCUMULATE") 64 : 65 129 : void Accumulate::registerKeywords( Keywords& keys ) { 66 129 : Action::registerKeywords( keys ); 67 129 : ActionWithValue::registerKeywords( keys ); 68 129 : ActionWithArguments::registerKeywords( keys ); 69 129 : ActionPilot::registerKeywords( keys ); 70 129 : keys.use("ARG"); 71 129 : keys.use("UPDATE_FROM"); 72 129 : keys.use("UPDATE_UNTIL"); 73 258 : keys.add("compulsory","STRIDE","1","the frequency with which the data should be collected and added to the quantity being averaged"); 74 258 : keys.add("compulsory","CLEAR","0","the frequency with which to clear all the accumulated data. The default value " 75 : "of 0 implies that all the data will be used and that the grid will never be cleared"); 76 129 : keys.setValueDescription("a sum calculated from the time series of the input quantity"); 77 129 : } 78 : 79 63 : Accumulate::Accumulate( const ActionOptions& ao ): 80 : Action(ao), 81 : ActionWithValue(ao), 82 : ActionWithArguments(ao), 83 : ActionPilot(ao), 84 63 : clearnextstep(true) { 85 63 : if( getNumberOfArguments()!=1 ) { 86 0 : error("there should only be one argument to this action"); 87 : } 88 63 : if( !getPntrToArgument(0)->hasDerivatives() && getPntrToArgument(0)->getRank()!=0 ) { 89 0 : error("input to the accumulate action should be a scalar or a grid"); 90 : } 91 : 92 63 : parse("CLEAR",clearstride); 93 63 : if( clearstride>0 ) { 94 11 : if( clearstride%getStride()!=0 ) { 95 0 : error("CLEAR parameter must be a multiple of STRIDE"); 96 : } 97 11 : log.printf(" clearing average every %u steps \n",clearstride); 98 : } 99 63 : std::vector<unsigned> shape( getPntrToArgument(0)->getShape() ); 100 63 : addValueWithDerivatives( shape ); 101 63 : setNotPeriodic(); 102 63 : if( getPntrToArgument(0)->isPeriodic() ) { 103 0 : error("you cannot accumulate a periodic quantity"); 104 : } 105 63 : } 106 : 107 36 : unsigned Accumulate::getNumberOfDerivatives() { 108 36 : if( getPntrToArgument(0)->getRank()>0 ) { 109 36 : return getPntrToArgument(0)->getNumberOfGridDerivatives(); 110 : } 111 0 : return getPntrToArgument(0)->getNumberOfDerivatives(); 112 : } 113 : 114 2324 : void Accumulate::update() { 115 2324 : if( clearnextstep ) { 116 70 : if( getPntrToComponent(0)->getNumberOfValues()!=getPntrToArgument(0)->getNumberOfValues() ) { 117 0 : getPntrToComponent(0)->setShape( getPntrToArgument(0)->getShape() ); 118 : } 119 70 : clearnextstep=false; 120 70 : getPntrToComponent(0)->set(0,0.0); 121 70 : getPntrToComponent(0)->clearDerivatives(true); 122 : } 123 2324 : if( getStep()==0 ) { 124 : return; 125 : } 126 : 127 : Value* myarg=getPntrToArgument(0); 128 2262 : Value* myout = getPntrToComponent(0); 129 2262 : if( getPntrToArgument(0)->getRank()>0 ) { 130 118 : unsigned nvals = myarg->getNumberOfValues(), nder = myarg->getNumberOfGridDerivatives(); 131 270581 : for(unsigned i=0; i<nvals; ++i) { 132 270463 : myout->set( i, myout->get(i) + myarg->get(i) ); 133 1353300 : for(unsigned j=0; j<nder; ++j) { 134 1082837 : myout->addGridDerivatives( i, j, myarg->getGridDerivative( i, j ) ); 135 : } 136 : } 137 : } else { 138 2144 : getPntrToComponent(0)->add( getPntrToArgument(0)->get() ); 139 : } 140 : 141 : // Clear if required 142 2262 : if( clearstride>0 && getStep()%clearstride==0 ) { 143 18 : clearnextstep=true; 144 : } 145 : } 146 : 147 : } 148 : }