LCOV - code coverage report
Current view: top level - generic - Collect.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 55 57 96.5 %
Date: 2024-10-18 13:59:31 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             : {
      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         384 :   keys.use("UPDATE_FROM"); keys.use("UPDATE_UNTIL");
      66         384 :   keys.addInputKeyword("compulsory","ARG","scalar/vector/matrix","the label of the value whose time series is being stored for later analysis");
      67         384 :   keys.add("compulsory","STRIDE","1","the frequency with which the data should be collected and added to the quantity being averaged");
      68         384 :   keys.add("compulsory","CLEAR","0","the frequency with which to clear all the accumulated data.  The default value "
      69             :            "of 0 implies that all the data will be used and that the grid will never be cleared");
      70         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");
      71         384 :   keys.setValueDescription("vector/matrix","the time series for the input quantity");
      72         192 : }
      73             : 
      74          94 : Collect::Collect( const ActionOptions& ao ):
      75             :   Action(ao),
      76             :   ActionWithValue(ao),
      77             :   ActionWithArguments(ao),
      78             :   ActionPilot(ao),
      79          94 :   usefirstconf(false)
      80             : {
      81          94 :   if( getNumberOfArguments()!=1 ) error("there should only be one argument to this action");
      82          94 :   if( getPntrToArgument(0)->getRank()>0 && getPntrToArgument(0)->hasDerivatives() ) error("input to the collect argument cannot be a grid");
      83             : 
      84         188 :   std::string type; parse("TYPE",type);
      85         165 :   if( getPntrToArgument(0)->getNumberOfValues()==1 && (type=="auto" || type=="vector") ) type="vector";
      86          24 :   else if( getPntrToArgument(0)->getNumberOfValues()==1 && type=="matrix" ) error("invalid type specified. Cannot construct a matrix by collecting scalars");
      87          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");
      88          36 :   else if( type!="vector" && type!="matrix" ) error("invalid TYPE specified. Should be matrix/scalar found " + type);
      89             : 
      90          94 :   if( type=="vector" ) log.printf("  adding %d elements to stored vector each time we collect\n", getPntrToArgument(0)->getNumberOfValues() );
      91          12 :   else log.printf("  constructing matrix with rows of length %d from input data\n", getPntrToArgument(0)->getNumberOfValues() );
      92             : 
      93          94 :   parse("CLEAR",clearstride); unsigned nvals=0;
      94          94 :   if( clearstride==getStride() ) {
      95           6 :     nvals=1; usefirstconf=(getStride()==0);
      96          88 :   } else if( clearstride>0 ) {
      97          15 :     if( clearstride%getStride()!=0 ) error("CLEAR parameter must be a multiple of STRIDE");
      98          15 :     log.printf("  clearing collected data every %u steps \n",clearstride);
      99          15 :     nvals=(clearstride/getStride());
     100             :   }
     101             : 
     102          94 :   std::vector<unsigned> shape(1); shape[0]=nvals; getPntrToArgument(0)->buildDataStore();
     103          94 :   if( type=="matrix" ) { shape.resize(2); shape[1] = getPntrToArgument(0)->getNumberOfValues(); }
     104          94 :   if( type=="vector" ) { shape[0] = nvals*getPntrToArgument(0)->getNumberOfValues(); }
     105          94 :   addValue( shape ); if( shape.size()==2 ) getPntrToComponent(0)->reshapeMatrixStore( shape[1] );
     106          94 :   if( getPntrToArgument(0)->isPeriodic() ) {
     107          16 :     std::string min, max; getPntrToArgument(0)->getDomain( min, max );
     108          16 :     setPeriodic( min, max );
     109          78 :   } else setNotPeriodic();
     110          94 : }
     111             : 
     112           0 : unsigned Collect::getNumberOfDerivatives() {
     113           0 :   return 0;
     114             : }
     115             : 
     116       17687 : void Collect::update() {
     117       17687 :   if( getStep()==0 || (!onStep() && !usefirstconf) ) return ;
     118       17016 :   usefirstconf=false;
     119             : 
     120             :   Value* myin=getPntrToArgument(0);
     121       17016 :   Value* myout=getPntrToComponent(0);
     122       17016 :   unsigned nargs=myin->getNumberOfValues();
     123       17016 :   if( clearstride==getStride() ) {
     124         339 :     for(unsigned i=0; i<nargs; ++i) myout->set( i, myin->get(i) );
     125       17010 :   } else if( clearstride>0 ) {
     126        1125 :     unsigned step = getStep() - clearstride*std::floor( getStep() / clearstride );
     127        1125 :     if( getStep()%clearstride==0 ) step = step + clearstride;
     128        1125 :     unsigned base = (step/getStride()-1)*nargs;
     129        2250 :     for(unsigned i=0; i<nargs; ++i) myout->set( base+i, myin->get(i) );
     130             :   } else {
     131       90800 :     for(unsigned i=0; i<nargs; ++i) myout->push_back( myin->get(i) );
     132       15885 :     if( myout->getRank()==2 ) myout->reshapeMatrixStore( nargs );
     133             :   }
     134             : }
     135             : 
     136             : }
     137             : }

Generated by: LCOV version 1.16