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 ANALYSIS COLLECT 30 : /* 31 : Collect data from the trajectory for later analysis 32 : 33 : \par Examples 34 : 35 : */ 36 : //+ENDPLUMEDOC 37 : 38 : namespace PLMD { 39 : namespace generic { 40 : 41 : class Collect : 42 : public ActionWithValue, 43 : public ActionWithArguments, 44 : public ActionPilot 45 : { 46 : private: 47 : bool usefirstconf; 48 : unsigned clearstride; 49 : public: 50 : static void registerKeywords( Keywords& keys ); 51 : Collect( const ActionOptions& ); 52 : unsigned getNumberOfDerivatives(); 53 35558 : bool calculateOnUpdate() override { return false; } 54 182 : bool calculateConstantValues( const bool& have_atoms ) override { return false; } 55 17687 : void calculate() override {} 56 17687 : void apply() override {} 57 : void update() override ; 58 : }; 59 : 60 : PLUMED_REGISTER_ACTION(Collect,"COLLECT") 61 : 62 192 : void Collect::registerKeywords( Keywords& keys ) { 63 192 : Action::registerKeywords( keys ); ActionWithValue::registerKeywords( keys ); 64 192 : ActionWithArguments::registerKeywords( keys ); ActionPilot::registerKeywords( keys ); 65 576 : keys.use("ARG"); keys.use("UPDATE_FROM"); keys.use("UPDATE_UNTIL"); 66 384 : keys.add("compulsory","STRIDE","1","the frequency with which the data should be collected and added to the quantity being averaged"); 67 384 : 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 384 : keys.add("compulsory","TYPE","auto","required if you are collecting an object with rank>0. Should be vector/matrix and determines how data is stored. If rank==0 then data has to be stored as a vector"); 70 192 : keys.setValueDescription("the time series for the input quantity"); 71 192 : } 72 : 73 94 : Collect::Collect( const ActionOptions& ao ): 74 : Action(ao), 75 : ActionWithValue(ao), 76 : ActionWithArguments(ao), 77 : ActionPilot(ao), 78 94 : usefirstconf(false) 79 : { 80 94 : if( getNumberOfArguments()!=1 ) error("there should only be one argument to this action"); 81 94 : if( getPntrToArgument(0)->getRank()>0 && getPntrToArgument(0)->hasDerivatives() ) error("input to the collect argument cannot be a grid"); 82 : 83 188 : std::string type; parse("TYPE",type); 84 165 : if( getPntrToArgument(0)->getNumberOfValues()==1 && (type=="auto" || type=="vector") ) type="vector"; 85 24 : else if( getPntrToArgument(0)->getNumberOfValues()==1 && type=="matrix" ) error("invalid type specified. Cannot construct a matrix by collecting scalars"); 86 48 : else if( getPntrToArgument(0)->getNumberOfValues()!=1 && type=="auto" ) error("missing TYPE keyword. TYPE should specify whether data is to be stored as a vector or a matrix"); 87 36 : else if( type!="vector" && type!="matrix" ) error("invalid TYPE specified. Should be matrix/scalar found " + type); 88 : 89 94 : if( type=="vector" ) log.printf(" adding %d elements to stored vector each time we collect\n", getPntrToArgument(0)->getNumberOfValues() ); 90 12 : else log.printf(" constructing matrix with rows of length %d from input data\n", getPntrToArgument(0)->getNumberOfValues() ); 91 : 92 94 : parse("CLEAR",clearstride); unsigned nvals=0; 93 94 : if( clearstride==getStride() ) { 94 6 : nvals=1; usefirstconf=(getStride()==0); 95 88 : } else if( clearstride>0 ) { 96 15 : if( clearstride%getStride()!=0 ) error("CLEAR parameter must be a multiple of STRIDE"); 97 15 : log.printf(" clearing collected data every %u steps \n",clearstride); 98 15 : nvals=(clearstride/getStride()); 99 : } 100 : 101 94 : std::vector<unsigned> shape(1); shape[0]=nvals; getPntrToArgument(0)->buildDataStore(); 102 94 : if( type=="matrix" ) { shape.resize(2); shape[1] = getPntrToArgument(0)->getNumberOfValues(); } 103 94 : if( type=="vector" ) { shape[0] = nvals*getPntrToArgument(0)->getNumberOfValues(); } 104 94 : addValue( shape ); if( shape.size()==2 ) getPntrToComponent(0)->reshapeMatrixStore( shape[1] ); 105 94 : if( getPntrToArgument(0)->isPeriodic() ) { 106 16 : std::string min, max; getPntrToArgument(0)->getDomain( min, max ); 107 16 : setPeriodic( min, max ); 108 78 : } else setNotPeriodic(); 109 94 : } 110 : 111 0 : unsigned Collect::getNumberOfDerivatives() { 112 0 : return 0; 113 : } 114 : 115 17687 : void Collect::update() { 116 17687 : if( getStep()==0 || (!onStep() && !usefirstconf) ) return ; 117 17016 : usefirstconf=false; 118 : 119 : Value* myin=getPntrToArgument(0); 120 17016 : Value* myout=getPntrToComponent(0); 121 17016 : unsigned nargs=myin->getNumberOfValues(); 122 17016 : if( clearstride==getStride() ) { 123 339 : for(unsigned i=0; i<nargs; ++i) myout->set( i, myin->get(i) ); 124 17010 : } else if( clearstride>0 ) { 125 1125 : unsigned step = getStep() - clearstride*std::floor( getStep() / clearstride ); 126 1125 : if( getStep()%clearstride==0 ) step = step + clearstride; 127 1125 : unsigned base = (step/getStride()-1)*nargs; 128 2250 : for(unsigned i=0; i<nargs; ++i) myout->set( base+i, myin->get(i) ); 129 : } else { 130 90800 : for(unsigned i=0; i<nargs; ++i) myout->push_back( myin->get(i) ); 131 15885 : if( myout->getRank()==2 ) myout->reshapeMatrixStore( nargs ); 132 : } 133 : } 134 : 135 : } 136 : }