LCOV - code coverage report
Current view: top level - vesselbase - ActionWithAveraging.h (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 13 15 86.7 %
Date: 2026-03-30 13:16:06 Functions: 6 7 85.7 %

          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             : #ifndef __PLUMED_vesselbase_ActionWithAveraging_h
      23             : #define __PLUMED_vesselbase_ActionWithAveraging_h
      24             : 
      25             : #include "core/ActionPilot.h"
      26             : #include "core/ActionWithValue.h"
      27             : #include "core/ActionAtomistic.h"
      28             : #include "core/ActionWithValue.h"
      29             : #include "core/ActionWithArguments.h"
      30             : #include "tools/MultiValue.h"
      31             : #include "analysis/AnalysisBase.h"
      32             : #include "ActionWithVessel.h"
      33             : #include "AveragingVessel.h"
      34             : 
      35             : namespace PLMD {
      36             : namespace vesselbase {
      37             : 
      38             : /**
      39             : \ingroup INHERIT
      40             : This abstract base class should be used if you are writing some method that calculates an "average" from a set of
      41             : trajectory frames.  Notice that we use the word average very broadly here and state that even dimensionality
      42             : reduction algorithms calculate an "average."  In other words, what we mean by average is that the method is going
      43             : to take in data from each trajectory frame and only calculate the final quantity once a certain amount of data has
      44             : been collected.
      45             : */
      46             : 
      47             : class ActionWithAveraging :
      48             :   public ActionPilot,
      49             :   public ActionAtomistic,
      50             :   public ActionWithArguments,
      51             :   public ActionWithValue,
      52             :   public ActionWithVessel {
      53             :   friend class AveragingVessel;
      54             : private:
      55             : /// The vessel which is used to compute averages
      56             :   AveragingVessel* myaverage;
      57             : /// The weights we are going to use for reweighting
      58             :   std::vector<Value*> weights;
      59             : /// Are we accumulated the unormalized quantity
      60             :   bool activated;
      61             : /// An object in which analysis data has been stored
      62             :   analysis::AnalysisBase* my_analysis_object;
      63             :   enum {t,f,ndata} normalization;
      64             : protected:
      65             : /// This ensures runAllTasks is used
      66             :   bool useRunAllTasks;
      67             : /// The frequency with which to clear the grid
      68             :   unsigned clearstride;
      69             : /// The current weight and its logarithm
      70             :   double lweight, cweight;
      71             : /// Set the averaging action
      72             :   void setAveragingAction( std::unique_ptr<AveragingVessel> av_vessel, const bool& usetasks );
      73             : /// Check if we are using the normalization condition when calculating this quantity
      74             :   bool noNormalization() const ;
      75             : /// Are we storing data then averaging
      76             :   bool storeThenAverage() const ;
      77             : public:
      78             :   static void registerKeywords( Keywords& keys );
      79             :   explicit ActionWithAveraging( const ActionOptions& );
      80             :   void lockRequests() override;
      81             :   void unlockRequests() override;
      82             :   void calculateNumericalDerivatives(PLMD::ActionWithValue*) override;
      83         271 :   unsigned getNumberOfDerivatives() override {
      84         271 :     return 0;
      85             :   }
      86             :   unsigned getNumberOfQuantities() const override;
      87             :   unsigned getNumberOfArguments() const override;
      88             : /// Overwrite ActionWithArguments getArguments() so that we don't return the bias
      89             :   using ActionWithArguments::getArguments;
      90             :   std::vector<Value*> getArguments();
      91             :   void update() override;
      92             : /// This does the clearing of the action
      93             :   virtual void clearAverage();
      94             : /// This is done before the averaging comences
      95        1022 :   virtual void prepareForAveraging() {}
      96             : /// This does the averaging operation
      97             :   virtual void performOperations( const bool& from_update );
      98             : /// Does the calculation
      99             :   void performTask( const unsigned& task_index, const unsigned& current, MultiValue& myvals ) const override;
     100             : ///
     101           0 :   virtual void runTask( const unsigned& current, MultiValue& myvals ) const {
     102           0 :     plumed_error();
     103             :   }
     104             : ///
     105        2108 :   virtual void accumulateAverage( MultiValue& myvals ) const {}
     106             : /// This is done once the averaging is finished
     107          76 :   virtual void finishAveraging() {}
     108             : ///
     109             :   void runFinalJobs() override;
     110             : ///
     111             :   virtual bool ignoreNormalization() const ;
     112             : };
     113             : 
     114             : inline
     115      145728 : unsigned ActionWithAveraging::getNumberOfArguments() const {
     116         226 :   return ActionWithArguments::getNumberOfArguments() - weights.size();
     117             : }
     118             : 
     119             : inline
     120          58 : std::vector<Value*> ActionWithAveraging::getArguments() {
     121          58 :   std::vector<Value*> arg_vals( ActionWithArguments::getArguments() );
     122          58 :   for(unsigned i=0; i<weights.size(); ++i) {
     123             :     arg_vals.erase(arg_vals.end()-1);
     124             :   }
     125          58 :   return arg_vals;
     126             : }
     127             : 
     128             : inline
     129             : bool ActionWithAveraging::noNormalization() const {
     130          36 :   return normalization==f;
     131             : }
     132             : 
     133             : inline
     134             : bool ActionWithAveraging::storeThenAverage() const {
     135         105 :   if( my_analysis_object ) {
     136             :     return true;
     137             :   }
     138             :   return false;
     139             : }
     140             : 
     141             : }
     142             : }
     143             : #endif

Generated by: LCOV version 1.16