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 : The way this command can be used is illustrated by the following example: 34 : 35 : ```plumed 36 : d: DISTANCE ATOMS=1,2 37 : c: COLLECT ARG=d STRIDE=1 38 : DUMPVECTOR ARG=c FILE=timeseries 39 : ``` 40 : 41 : The COLLECT command in the input above stores the time series of distances over the whole 42 : trajectory. The STRIDE keyword controls how frequently data is stored and the [DUMPVECTOR](DUMPVECTOR.md) 43 : command then outputs the full time series of `d` values at the end of the calculation. 44 : 45 : The above example is not particularly useful as you can achieve the same result by using simply 46 : a DISTANCE command and the [PRINT](PRINT.md) command. The COLLECT command is useful if you want 47 : use the whole trajectory to perform a analysis such as dimensionality reduction (see [dimred](module_dimred.md)). 48 : The shortcut command [COLLECT_FRAMES](COLLECT_FRAMES.md) uses this action heavily and allows one to easily 49 : deal with the fact that COLLECT can only collect the time series for one PLUMED Value at a time. 50 : 51 : ## Collecting a part of the trajectory 52 : 53 : You can use the CLEAR keyword to collect a subset of the trajectory as illustrated below: 54 : 55 : ```plumed 56 : d: DISTANCE ATOMS=1,2 57 : c: COLLECT ARG=d STRIDE=1 CLEAR=1000 58 : DUMPVECTOR ARG=c FILE=timeseries STRIDE=1000 59 : ``` 60 : 61 : This outputs files contain from this input contain 1000-frame blocks of the trajectory for the distance between atom 1 and 2. 62 : This type of input might proove useful if you wish to perform separate analysis on different parts of the trajectory for 63 : later comparison. 64 : 65 : An alternative way to collect part of the trajectry is to use the UPDATE_FROM and UPDATE_UNTIL arguments as illustrated in the 66 : example shown below: 67 : 68 : ```plumed 69 : d: DISTANCE ATOMS=1,2 70 : c: COLLECT ARG=d STRIDE=1 UPDATE_FROM=100 UPDATE_UNTIL=200 71 : DUMPVECTOR ARG=c FILE=timeseries 72 : ``` 73 : 74 : In this input the COLLECT action only starts collecting the data on step 100 so the first 100 values of the trajectory are discarded. 75 : The COLLECT action then stops collecting data on step 200. These 200 stored distances are then output at the end of the simulation 76 : even if the trajectory contains more than 200 frames. 77 : 78 : ## Collecting vectors 79 : 80 : You can use the collect command even if the input value has a rank that is greater than zero. For example, the following 81 : input collects vectors of distances from the trajectory: 82 : 83 : ```plumed 84 : d: DISTANCE ATOMS1=1,2 ATOMS2=3,4 ATOMS3=5,6 ATOMS4=7,8 85 : c: COLLECT ARG=d TYPE=vector STRIDE=1 CLEAR=100 86 : DUMPVECTOR ARG=c FILE=timeseries STRIDE=100 87 : ``` 88 : 89 : Notice that if the input to the collect action is a value with rank>0 you __must__ use the TYPE keyword to specify whether 90 : the output Value is a vector or matrix. In the above input we are storing a vector so the DUMPVECTOR command outputs 91 : a list of 400 distances - 4 distances for each frame. The assumption in the input above is that the four distances that have 92 : been computed by the DISTANCES command are indistinguishable. 93 : 94 : By using the following input we can ensure the four distances are treated as distinguishable when we do any analysis: 95 : 96 : ```plumed 97 : d: DISTANCE ATOMS1=1,2 ATOMS2=3,4 ATOMS3=5,6 ATOMS4=7,8 98 : c: COLLECT ARG=d TYPE=matrix STRIDE=1 CLEAR=100 99 : DUMPVECTOR ARG=c FILE=timeseries STRIDE=100 100 : ``` 101 : 102 : The value `c` is now a $100\times 4$ matrix. Furthermore, when we use DUMPVECTOR to output the output file will contain 103 : four columns of data. 104 : 105 : ## Collecting matrices 106 : 107 : You can also use this action to collect a matrix as illustrated by the following example: 108 : 109 : ```plumed 110 : d: DISTANCE_MATRIX GROUPA=1,2 GROUPB=3,4,5 111 : c: COLLECT ARG=d TYPE=vector STRIDE=1 CLEAR=100 112 : DUMPVECTOR ARG=c FILE=timeseries STRIDE=100 113 : ``` 114 : 115 : The value `c` here is a vector with 600 elements as the input matrix is converted to a vector. These vectors are then stored in 116 : one contiguous object. 117 : 118 : If by contrast we use `TYPE=matrix` as shown below: 119 : 120 : ```plumed 121 : d: DISTANCE_MATRIX GROUPA=1,2 GROUPB=3,4,5 122 : c: COLLECT ARG=d TYPE=matrix STRIDE=1 CLEAR=100 123 : DUMPVECTOR ARG=c FILE=timeseries STRIDE=100 124 : ``` 125 : 126 : A $100 \times 6$ matrix is stored. Each row of this matrix contains a vectorized version of the input matrix. 127 : There is currently no way to store the collected data in a way that recognises that each of the input PLUMED Value 128 : was a matrix. You also cannot use this action to store functions evaluated on a grid. 129 : 130 : */ 131 : //+ENDPLUMEDOC 132 : 133 : namespace PLMD { 134 : namespace generic { 135 : 136 : class Collect : 137 : public ActionWithValue, 138 : public ActionWithArguments, 139 : public ActionPilot { 140 : private: 141 : bool usefirstconf; 142 : unsigned clearstride; 143 : public: 144 : static void registerKeywords( Keywords& keys ); 145 : Collect( const ActionOptions& ); 146 : unsigned getNumberOfDerivatives() override; 147 35644 : bool calculateOnUpdate() override { 148 35644 : return false; 149 : } 150 206 : bool calculateConstantValues( const bool& have_atoms ) override { 151 206 : return false; 152 : } 153 17718 : void calculate() override {} 154 17718 : void apply() override {} 155 : void update() override ; 156 : }; 157 : 158 : PLUMED_REGISTER_ACTION(Collect,"COLLECT") 159 : 160 218 : void Collect::registerKeywords( Keywords& keys ) { 161 218 : Action::registerKeywords( keys ); 162 218 : ActionWithValue::registerKeywords( keys ); 163 218 : ActionWithArguments::registerKeywords( keys ); 164 218 : ActionPilot::registerKeywords( keys ); 165 218 : keys.use("UPDATE_FROM"); 166 218 : keys.use("UPDATE_UNTIL"); 167 218 : keys.remove("NUMERICAL_DERIVATIVES"); 168 436 : keys.addInputKeyword("compulsory","ARG","scalar/vector/matrix","the label of the value whose time series is being stored for later analysis"); 169 218 : keys.add("compulsory","STRIDE","1","the frequency with which the data should be collected and added to the quantity being averaged"); 170 218 : keys.add("compulsory","CLEAR","0","the frequency with which to clear all the accumulated data. The default value " 171 : "of 0 implies that all the data will be used and that the grid will never be cleared"); 172 218 : 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"); 173 436 : keys.setValueDescription("vector/matrix","the time series for the input quantity"); 174 218 : } 175 : 176 106 : Collect::Collect( const ActionOptions& ao ): 177 : Action(ao), 178 : ActionWithValue(ao), 179 : ActionWithArguments(ao), 180 : ActionPilot(ao), 181 106 : usefirstconf(false) { 182 106 : if( getNumberOfArguments()!=1 ) { 183 0 : error("there should only be one argument to this action"); 184 : } 185 106 : if( getPntrToArgument(0)->getRank()>0 && getPntrToArgument(0)->hasDerivatives() ) { 186 0 : error("input to the collect argument cannot be a grid"); 187 : } 188 : 189 : std::string type; 190 212 : parse("TYPE",type); 191 106 : if( getPntrToArgument(0)->getNumberOfValues()==1 && (type=="auto" || type=="vector") ) { 192 : type="vector"; 193 24 : } else if( getPntrToArgument(0)->getNumberOfValues()==1 && type=="matrix" ) { 194 0 : error("invalid type specified. Cannot construct a matrix by collecting scalars"); 195 24 : } else if( getPntrToArgument(0)->getNumberOfValues()!=1 && type=="auto" ) { 196 0 : error("missing TYPE keyword. TYPE should specify whether data is to be stored as a vector or a matrix"); 197 24 : } else if( type!="vector" && type!="matrix" ) { 198 0 : error("invalid TYPE specified. Should be matrix/scalar found " + type); 199 : } 200 : 201 106 : if( type=="vector" ) { 202 94 : log.printf(" adding %zu elements to stored vector each time we collect\n", getPntrToArgument(0)->getNumberOfValues() ); 203 : } else { 204 12 : log.printf(" constructing matrix with rows of length %zu from input data\n", getPntrToArgument(0)->getNumberOfValues() ); 205 : } 206 : 207 106 : parse("CLEAR",clearstride); 208 : std::size_t nvals=0; 209 106 : if( clearstride==getStride() ) { 210 : nvals=1; 211 6 : usefirstconf=(getStride()==0); 212 100 : } else if( clearstride>0 ) { 213 15 : if( clearstride%getStride()!=0 ) { 214 0 : error("CLEAR parameter must be a multiple of STRIDE"); 215 : } 216 15 : log.printf(" clearing collected data every %u steps \n",clearstride); 217 15 : nvals=(clearstride/getStride()); 218 : } 219 : 220 106 : std::vector<std::size_t> shape(1); 221 106 : shape[0]=nvals; 222 106 : if( type=="matrix" ) { 223 12 : shape.resize(2); 224 12 : shape[1] = getPntrToArgument(0)->getNumberOfValues(); 225 : } 226 106 : if( type=="vector" ) { 227 94 : shape[0] = nvals*getPntrToArgument(0)->getNumberOfValues(); 228 : } 229 106 : addValue( shape ); 230 106 : if( shape.size()==2 ) { 231 12 : getPntrToComponent(0)->reshapeMatrixStore( shape[1] ); 232 : } 233 106 : if( getPntrToArgument(0)->isPeriodic() ) { 234 : std::string min, max; 235 16 : getPntrToArgument(0)->getDomain( min, max ); 236 16 : setPeriodic( min, max ); 237 : } else { 238 90 : setNotPeriodic(); 239 : } 240 212 : } 241 : 242 0 : unsigned Collect::getNumberOfDerivatives() { 243 0 : return 0; 244 : } 245 : 246 17718 : void Collect::update() { 247 17718 : if( getStep()==0 || (!onStep() && !usefirstconf) ) { 248 678 : return ; 249 : } 250 17040 : usefirstconf=false; 251 : 252 : Value* myin=getPntrToArgument(0); 253 17040 : Value* myout=getPntrToComponent(0); 254 17040 : unsigned nargs=myin->getNumberOfValues(); 255 17040 : if( clearstride==getStride() ) { 256 339 : for(unsigned i=0; i<nargs; ++i) { 257 333 : myout->set( i, myin->get(i) ); 258 : } 259 17034 : } else if( clearstride>0 ) { 260 1125 : unsigned step = getStep() - clearstride*std::floor( getStep() / clearstride ); 261 1125 : if( getStep()%clearstride==0 ) { 262 25 : step = step + clearstride; 263 : } 264 1125 : unsigned base = (step/getStride()-1)*nargs; 265 2250 : for(unsigned i=0; i<nargs; ++i) { 266 1125 : myout->set( base+i, myin->get(i) ); 267 : } 268 : } else { 269 90848 : for(unsigned i=0; i<nargs; ++i) { 270 74939 : myout->push_back( myin->get(i) ); 271 : } 272 15909 : if( myout->getRank()==2 ) { 273 589 : myout->reshapeMatrixStore( nargs ); 274 : } 275 : } 276 : } 277 : 278 : } 279 : }