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: 2026-03-30 11:13:23 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             : \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             : private:
      46             :   bool usefirstconf;
      47             :   unsigned clearstride;
      48             : public:
      49             :   static void registerKeywords( Keywords& keys );
      50             :   Collect( const ActionOptions& );
      51             :   unsigned getNumberOfDerivatives();
      52       35558 :   bool calculateOnUpdate() override {
      53       35558 :     return false;
      54             :   }
      55         182 :   bool calculateConstantValues( const bool& have_atoms ) override {
      56         182 :     return false;
      57             :   }
      58       17687 :   void calculate() override {}
      59       17687 :   void apply() override {}
      60             :   void update() override ;
      61             : };
      62             : 
      63             : PLUMED_REGISTER_ACTION(Collect,"COLLECT")
      64             : 
      65         194 : void Collect::registerKeywords( Keywords& keys ) {
      66         194 :   Action::registerKeywords( keys );
      67         194 :   ActionWithValue::registerKeywords( keys );
      68         194 :   ActionWithArguments::registerKeywords( keys );
      69         194 :   ActionPilot::registerKeywords( keys );
      70         194 :   keys.use("ARG");
      71         194 :   keys.use("UPDATE_FROM");
      72         194 :   keys.use("UPDATE_UNTIL");
      73         388 :   keys.add("compulsory","STRIDE","1","the frequency with which the data should be collected and added to the quantity being averaged");
      74         388 :   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         388 :   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");
      77         194 :   keys.setValueDescription("the time series for the input quantity");
      78         194 : }
      79             : 
      80          94 : Collect::Collect( const ActionOptions& ao ):
      81             :   Action(ao),
      82             :   ActionWithValue(ao),
      83             :   ActionWithArguments(ao),
      84             :   ActionPilot(ao),
      85          94 :   usefirstconf(false) {
      86          94 :   if( getNumberOfArguments()!=1 ) {
      87           0 :     error("there should only be one argument to this action");
      88             :   }
      89          94 :   if( getPntrToArgument(0)->getRank()>0 && getPntrToArgument(0)->hasDerivatives() ) {
      90           0 :     error("input to the collect argument cannot be a grid");
      91             :   }
      92             : 
      93             :   std::string type;
      94         188 :   parse("TYPE",type);
      95         165 :   if( getPntrToArgument(0)->getNumberOfValues()==1 && (type=="auto" || type=="vector") ) {
      96             :     type="vector";
      97          24 :   } else if( getPntrToArgument(0)->getNumberOfValues()==1 && type=="matrix" ) {
      98           0 :     error("invalid type specified. Cannot construct a matrix by collecting scalars");
      99          48 :   } else if(  getPntrToArgument(0)->getNumberOfValues()!=1 && type=="auto" ) {
     100           0 :     error("missing TYPE keyword.  TYPE should specify whether data is to be stored as a vector or a matrix");
     101          36 :   } else if( type!="vector" && type!="matrix" ) {
     102           0 :     error("invalid TYPE specified. Should be matrix/scalar found " + type);
     103             :   }
     104             : 
     105          94 :   if( type=="vector" ) {
     106          82 :     log.printf("  adding %d elements to stored vector each time we collect\n", getPntrToArgument(0)->getNumberOfValues() );
     107             :   } else {
     108          12 :     log.printf("  constructing matrix with rows of length %d from input data\n", getPntrToArgument(0)->getNumberOfValues() );
     109             :   }
     110             : 
     111          94 :   parse("CLEAR",clearstride);
     112             :   unsigned nvals=0;
     113          94 :   if( clearstride==getStride() ) {
     114             :     nvals=1;
     115           6 :     usefirstconf=(getStride()==0);
     116          88 :   } else if( clearstride>0 ) {
     117          15 :     if( clearstride%getStride()!=0 ) {
     118           0 :       error("CLEAR parameter must be a multiple of STRIDE");
     119             :     }
     120          15 :     log.printf("  clearing collected data every %u steps \n",clearstride);
     121          15 :     nvals=(clearstride/getStride());
     122             :   }
     123             : 
     124          94 :   std::vector<unsigned> shape(1);
     125          94 :   shape[0]=nvals;
     126          94 :   getPntrToArgument(0)->buildDataStore();
     127          94 :   if( type=="matrix" ) {
     128          12 :     shape.resize(2);
     129          12 :     shape[1] = getPntrToArgument(0)->getNumberOfValues();
     130             :   }
     131          94 :   if( type=="vector" ) {
     132          82 :     shape[0] = nvals*getPntrToArgument(0)->getNumberOfValues();
     133             :   }
     134          94 :   addValue( shape );
     135          94 :   if( shape.size()==2 ) {
     136          12 :     getPntrToComponent(0)->reshapeMatrixStore( shape[1] );
     137             :   }
     138          94 :   if( getPntrToArgument(0)->isPeriodic() ) {
     139             :     std::string min, max;
     140          16 :     getPntrToArgument(0)->getDomain( min, max );
     141          16 :     setPeriodic( min, max );
     142             :   } else {
     143          78 :     setNotPeriodic();
     144             :   }
     145          94 : }
     146             : 
     147           0 : unsigned Collect::getNumberOfDerivatives() {
     148           0 :   return 0;
     149             : }
     150             : 
     151       17687 : void Collect::update() {
     152       17687 :   if( getStep()==0 || (!onStep() && !usefirstconf) ) {
     153         671 :     return ;
     154             :   }
     155       17016 :   usefirstconf=false;
     156             : 
     157             :   Value* myin=getPntrToArgument(0);
     158       17016 :   Value* myout=getPntrToComponent(0);
     159       17016 :   unsigned nargs=myin->getNumberOfValues();
     160       17016 :   if( clearstride==getStride() ) {
     161         339 :     for(unsigned i=0; i<nargs; ++i) {
     162         333 :       myout->set( i, myin->get(i) );
     163             :     }
     164       17010 :   } else if( clearstride>0 ) {
     165        1125 :     unsigned step = getStep() - clearstride*std::floor( getStep() / clearstride );
     166        1125 :     if( getStep()%clearstride==0 ) {
     167          25 :       step = step + clearstride;
     168             :     }
     169        1125 :     unsigned base = (step/getStride()-1)*nargs;
     170        2250 :     for(unsigned i=0; i<nargs; ++i) {
     171        1125 :       myout->set( base+i, myin->get(i) );
     172             :     }
     173             :   } else {
     174       90800 :     for(unsigned i=0; i<nargs; ++i) {
     175       74915 :       myout->push_back( myin->get(i) );
     176             :     }
     177       15885 :     if( myout->getRank()==2 ) {
     178         589 :       myout->reshapeMatrixStore( nargs );
     179             :     }
     180             :   }
     181             : }
     182             : 
     183             : }
     184             : }

Generated by: LCOV version 1.16