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 : { 54 : friend class AveragingVessel; 55 : private: 56 : /// The vessel which is used to compute averages 57 : AveragingVessel* myaverage; 58 : /// The weights we are going to use for reweighting 59 : std::vector<Value*> weights; 60 : /// Are we accumulated the unormalized quantity 61 : bool activated; 62 : /// An object in which analysis data has been stored 63 : analysis::AnalysisBase* my_analysis_object; 64 : enum {t,f,ndata} normalization; 65 : protected: 66 : /// This ensures runAllTasks is used 67 : bool useRunAllTasks; 68 : /// The frequency with which to clear the grid 69 : unsigned clearstride; 70 : /// The current weight and its logarithm 71 : double lweight, cweight; 72 : /// Set the averaging action 73 : void setAveragingAction( std::unique_ptr<AveragingVessel> av_vessel, const bool& usetasks ); 74 : /// Check if we are using the normalization condition when calculating this quantity 75 : bool noNormalization() const ; 76 : /// Are we storing data then averaging 77 : bool storeThenAverage() const ; 78 : public: 79 : static void registerKeywords( Keywords& keys ); 80 : explicit ActionWithAveraging( const ActionOptions& ); 81 : void lockRequests() override; 82 : void unlockRequests() override; 83 : void calculateNumericalDerivatives(PLMD::ActionWithValue*) override; 84 271 : unsigned getNumberOfDerivatives() override { return 0; } 85 : unsigned getNumberOfQuantities() const override; 86 : unsigned getNumberOfArguments() const override; 87 : /// Overwrite ActionWithArguments getArguments() so that we don't return the bias 88 : using ActionWithArguments::getArguments; 89 : std::vector<Value*> getArguments(); 90 : void update() override; 91 : /// This does the clearing of the action 92 : virtual void clearAverage(); 93 : /// This is done before the averaging comences 94 1022 : virtual void prepareForAveraging() {} 95 : /// This does the averaging operation 96 : virtual void performOperations( const bool& from_update ); 97 : /// Does the calculation 98 : void performTask( const unsigned& task_index, const unsigned& current, MultiValue& myvals ) const override; 99 : /// 100 0 : virtual void runTask( const unsigned& current, MultiValue& myvals ) const { plumed_error(); } 101 : /// 102 2108 : virtual void accumulateAverage( MultiValue& myvals ) const {} 103 : /// This is done once the averaging is finished 104 76 : virtual void finishAveraging() {} 105 : /// 106 : void runFinalJobs() override; 107 : /// 108 : virtual bool ignoreNormalization() const ; 109 : }; 110 : 111 : inline 112 145728 : unsigned ActionWithAveraging::getNumberOfArguments() const { 113 226 : return ActionWithArguments::getNumberOfArguments() - weights.size(); 114 : } 115 : 116 : inline 117 58 : std::vector<Value*> ActionWithAveraging::getArguments() { 118 58 : std::vector<Value*> arg_vals( ActionWithArguments::getArguments() ); 119 58 : for(unsigned i=0; i<weights.size(); ++i) arg_vals.erase(arg_vals.end()-1); 120 58 : return arg_vals; 121 : } 122 : 123 : inline 124 : bool ActionWithAveraging::noNormalization() const { 125 36 : return normalization==f; 126 : } 127 : 128 : inline 129 : bool ActionWithAveraging::storeThenAverage() const { 130 105 : if( my_analysis_object ) return true; 131 : return false; 132 : } 133 : 134 : } 135 : } 136 : #endif