LCOV - code coverage report
Current view: top level - generic - Collect.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 74 82 90.2 %
Date: 2025-04-08 18:07:56 Functions: 7 9 77.8 %

          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             : ## Collecting vectors
      66             : 
      67             : You can use the collect command even if the input value has a rank that is greater than zero.  For example, the following
      68             : input collects vectors of distances from the trajectory:
      69             : 
      70             : ```plumed
      71             : d: DISTANCE ATOMS1=1,2 ATOMS2=3,4 ATOMS3=5,6 ATOMS4=7,8
      72             : c: COLLECT ARG=d TYPE=vector STRIDE=1 CLEAR=100
      73             : DUMPVECTOR ARG=c FILE=timeseries STRIDE=100
      74             : ```
      75             : 
      76             : Notice that if the input to the collect action is a value with rank>0 you __must__ use the TYPE keyword to specify whether
      77             : the output Value is a vector or matrix.  In the above input we are storing a vector so the DUMPVECTOR command outputs
      78             : a list of 400 distances - 4 distances for each frame.  The assumption in the input above is that the four distances that have
      79             : been computed by the DISTANCES command are indistinguishable.
      80             : 
      81             : By using the following input we can ensure the the four distances are treated as distinguishable when we do any analysis:
      82             : 
      83             : ```plumed
      84             : d: DISTANCE ATOMS1=1,2 ATOMS2=3,4 ATOMS3=5,6 ATOMS4=7,8
      85             : c: COLLECT ARG=d TYPE=matrix STRIDE=1 CLEAR=100
      86             : DUMPVECTOR ARG=c FILE=timeseries STRIDE=100
      87             : ```
      88             : 
      89             : The value `c` is now a $100\times 4$ matrix.  Furthermore, when we use DUMPVECTOR to output the output file will contain
      90             : four columns of data.
      91             : 
      92             : ## Collecting matrices
      93             : 
      94             : You can also use this action to collect a matrix as illustrated by the following example:
      95             : 
      96             : ```plumed
      97             : d: DISTANCE_MATRIX GROUPA=1,2 GROUPB=3,4,5
      98             : c: COLLECT ARG=d TYPE=vector STRIDE=1 CLEAR=100
      99             : DUMPVECTOR ARG=c FILE=timeseries STRIDE=100
     100             : ```
     101             : 
     102             : 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
     103             : one contiguous object.
     104             : 
     105             : If by contrast we use `TYPE=matrix` as shown below:
     106             : 
     107             : ```plumed
     108             : d: DISTANCE_MATRIX GROUPA=1,2 GROUPB=3,4,5
     109             : c: COLLECT ARG=d TYPE=matrix STRIDE=1 CLEAR=100
     110             : DUMPVECTOR ARG=c FILE=timeseries STRIDE=100
     111             : ```
     112             : 
     113             : A $100 \times 6$ matrix is stored.  Each row of this matrix contains a vectorized version of the input matrix.
     114             : There is currently no way to store the collected data in a way that recognises that each of the input PLUMED Value
     115             : was a matrix. You also cannot use this action to store functions evaluated on a grid.
     116             : 
     117             : */
     118             : //+ENDPLUMEDOC
     119             : 
     120             : namespace PLMD {
     121             : namespace generic {
     122             : 
     123             : class Collect :
     124             :   public ActionWithValue,
     125             :   public ActionWithArguments,
     126             :   public ActionPilot {
     127             : private:
     128             :   bool usefirstconf;
     129             :   unsigned clearstride;
     130             : public:
     131             :   static void registerKeywords( Keywords& keys );
     132             :   Collect( const ActionOptions& );
     133             :   unsigned getNumberOfDerivatives();
     134       35558 :   bool calculateOnUpdate() override {
     135       35558 :     return false;
     136             :   }
     137         182 :   bool calculateConstantValues( const bool& have_atoms ) override {
     138         182 :     return false;
     139             :   }
     140       17687 :   void calculate() override {}
     141       17687 :   void apply() override {}
     142             :   void update() override ;
     143             : };
     144             : 
     145             : PLUMED_REGISTER_ACTION(Collect,"COLLECT")
     146             : 
     147         192 : void Collect::registerKeywords( Keywords& keys ) {
     148         192 :   Action::registerKeywords( keys );
     149         192 :   ActionWithValue::registerKeywords( keys );
     150         192 :   ActionWithArguments::registerKeywords( keys );
     151         192 :   ActionPilot::registerKeywords( keys );
     152         192 :   keys.use("UPDATE_FROM");
     153         192 :   keys.use("UPDATE_UNTIL");
     154         384 :   keys.addInputKeyword("compulsory","ARG","scalar/vector/matrix","the label of the value whose time series is being stored for later analysis");
     155         192 :   keys.add("compulsory","STRIDE","1","the frequency with which the data should be collected and added to the quantity being averaged");
     156         192 :   keys.add("compulsory","CLEAR","0","the frequency with which to clear all the accumulated data.  The default value "
     157             :            "of 0 implies that all the data will be used and that the grid will never be cleared");
     158         192 :   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");
     159         384 :   keys.setValueDescription("vector/matrix","the time series for the input quantity");
     160         192 : }
     161             : 
     162          94 : Collect::Collect( const ActionOptions& ao ):
     163             :   Action(ao),
     164             :   ActionWithValue(ao),
     165             :   ActionWithArguments(ao),
     166             :   ActionPilot(ao),
     167          94 :   usefirstconf(false) {
     168          94 :   if( getNumberOfArguments()!=1 ) {
     169           0 :     error("there should only be one argument to this action");
     170             :   }
     171          94 :   if( getPntrToArgument(0)->getRank()>0 && getPntrToArgument(0)->hasDerivatives() ) {
     172           0 :     error("input to the collect argument cannot be a grid");
     173             :   }
     174             : 
     175             :   std::string type;
     176         188 :   parse("TYPE",type);
     177         165 :   if( getPntrToArgument(0)->getNumberOfValues()==1 && (type=="auto" || type=="vector") ) {
     178             :     type="vector";
     179          24 :   } else if( getPntrToArgument(0)->getNumberOfValues()==1 && type=="matrix" ) {
     180           0 :     error("invalid type specified. Cannot construct a matrix by collecting scalars");
     181          48 :   } else if(  getPntrToArgument(0)->getNumberOfValues()!=1 && type=="auto" ) {
     182           0 :     error("missing TYPE keyword.  TYPE should specify whether data is to be stored as a vector or a matrix");
     183          36 :   } else if( type!="vector" && type!="matrix" ) {
     184           0 :     error("invalid TYPE specified. Should be matrix/scalar found " + type);
     185             :   }
     186             : 
     187          94 :   if( type=="vector" ) {
     188          82 :     log.printf("  adding %d elements to stored vector each time we collect\n", getPntrToArgument(0)->getNumberOfValues() );
     189             :   } else {
     190          12 :     log.printf("  constructing matrix with rows of length %d from input data\n", getPntrToArgument(0)->getNumberOfValues() );
     191             :   }
     192             : 
     193          94 :   parse("CLEAR",clearstride);
     194             :   unsigned nvals=0;
     195          94 :   if( clearstride==getStride() ) {
     196             :     nvals=1;
     197           6 :     usefirstconf=(getStride()==0);
     198          88 :   } else if( clearstride>0 ) {
     199          15 :     if( clearstride%getStride()!=0 ) {
     200           0 :       error("CLEAR parameter must be a multiple of STRIDE");
     201             :     }
     202          15 :     log.printf("  clearing collected data every %u steps \n",clearstride);
     203          15 :     nvals=(clearstride/getStride());
     204             :   }
     205             : 
     206          94 :   std::vector<unsigned> shape(1);
     207          94 :   shape[0]=nvals;
     208          94 :   getPntrToArgument(0)->buildDataStore();
     209          94 :   if( type=="matrix" ) {
     210          12 :     shape.resize(2);
     211          12 :     shape[1] = getPntrToArgument(0)->getNumberOfValues();
     212             :   }
     213          94 :   if( type=="vector" ) {
     214          82 :     shape[0] = nvals*getPntrToArgument(0)->getNumberOfValues();
     215             :   }
     216          94 :   addValue( shape );
     217          94 :   if( shape.size()==2 ) {
     218          12 :     getPntrToComponent(0)->reshapeMatrixStore( shape[1] );
     219             :   }
     220          94 :   if( getPntrToArgument(0)->isPeriodic() ) {
     221             :     std::string min, max;
     222          16 :     getPntrToArgument(0)->getDomain( min, max );
     223          16 :     setPeriodic( min, max );
     224             :   } else {
     225          78 :     setNotPeriodic();
     226             :   }
     227          94 : }
     228             : 
     229           0 : unsigned Collect::getNumberOfDerivatives() {
     230           0 :   return 0;
     231             : }
     232             : 
     233       17687 : void Collect::update() {
     234       17687 :   if( getStep()==0 || (!onStep() && !usefirstconf) ) {
     235         671 :     return ;
     236             :   }
     237       17016 :   usefirstconf=false;
     238             : 
     239             :   Value* myin=getPntrToArgument(0);
     240       17016 :   Value* myout=getPntrToComponent(0);
     241       17016 :   unsigned nargs=myin->getNumberOfValues();
     242       17016 :   if( clearstride==getStride() ) {
     243         339 :     for(unsigned i=0; i<nargs; ++i) {
     244         333 :       myout->set( i, myin->get(i) );
     245             :     }
     246       17010 :   } else if( clearstride>0 ) {
     247        1125 :     unsigned step = getStep() - clearstride*std::floor( getStep() / clearstride );
     248        1125 :     if( getStep()%clearstride==0 ) {
     249          25 :       step = step + clearstride;
     250             :     }
     251        1125 :     unsigned base = (step/getStride()-1)*nargs;
     252        2250 :     for(unsigned i=0; i<nargs; ++i) {
     253        1125 :       myout->set( base+i, myin->get(i) );
     254             :     }
     255             :   } else {
     256       90800 :     for(unsigned i=0; i<nargs; ++i) {
     257       74915 :       myout->push_back( myin->get(i) );
     258             :     }
     259       15885 :     if( myout->getRank()==2 ) {
     260         589 :       myout->reshapeMatrixStore( nargs );
     261             :     }
     262             :   }
     263             : }
     264             : 
     265             : }
     266             : }

Generated by: LCOV version 1.16