All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Mean.cpp
Go to the documentation of this file.
1 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2  Copyright (c) 2013 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-code.org for more information.
6 
7  This file is part of plumed, version 2.0.
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 "VesselRegister.h"
23 #include "FunctionVessel.h"
24 #include "ActionWithVessel.h"
25 
26 namespace PLMD {
27 namespace vesselbase {
28 
29 class Mean : public FunctionVessel {
30 private:
31 public:
32  static void registerKeywords( Keywords& keys );
33  static void reserveKeyword( Keywords& keys );
35  std::string function_description();
36  bool calculate();
37  void finish();
38 };
39 
41 
42 void Mean::registerKeywords( Keywords& keys ){
44 }
45 
47  keys.reserveFlag("MEAN",false,"take the mean of these variables.",true);
48  keys.addOutputComponent("mean","MEAN","the mean value. The output component can be refererred to elsewhere in the input "
49  "file by using the label.mean");
50 }
51 
54 {
55  if( getAction()->isPeriodic() ) error("MEAN cannot be used with periodic variables");
56 }
57 
59  return "the mean value";
60 }
61 
63  double weight=getAction()->getElementValue(1);
64  plumed_dbg_assert( weight>=getTolerance() );
65  addValueIgnoringTolerance( 1, weight );
66  double colvar=getAction()->getElementValue(0);
67  addValueIgnoringTolerance( 0, weight*colvar );
68  getAction()->chainRuleForElementDerivatives( 0, 0, weight, this );
69  if(diffweight){
70  getAction()->chainRuleForElementDerivatives( 0, 1, colvar, this );
71  getAction()->chainRuleForElementDerivatives( 1, 1, 1.0, this );
72  }
73  return true;
74 }
75 
76 void Mean::finish(){
78  double denom=getFinalValue(1);
79  std::vector<double> df(2);
80  df[0] = 1.0 / denom;
81  if(diffweight) df[1] = -getFinalValue(0) / (denom*denom);
82  else df[1]=0.0;
84 }
85 
86 }
87 }
bool calculate()
Calculate the part of the vessel that is done in the loop.
Definition: Mean.cpp:62
double getTolerance() const
Return the value of the tolerance.
Definition: Vessel.h:226
double getFinalValue(const unsigned &j)
Get the nth value in the distribution.
bool diffweight
Are the derivatives differentiable.
void chainRuleForElementDerivatives(const unsigned &, const unsigned &, const double &, Vessel *)
Merge the derivatives.
void setOutputValue(const double &val)
Set the final value.
Objects that inherit from FunctionVessel can be used (in tandem with PLMD::ActionWithVessel) to calcu...
static void registerKeywords(Keywords &keys)
void mergeFinalDerivatives(const std::vector< double > &df)
This does a combination of the product and chain rules.
void addOutputComponent(const std::string &name, const std::string &key, const std::string &descr)
Add a potential component which can be output by this particular action.
Definition: Keywords.cpp:565
This class holds the keywords and their documentation.
Definition: Keywords.h:36
void addValueIgnoringTolerance(const unsigned &jval, const double &val)
Add some value to the accumulator and ignore the tolerance.
#define PLUMED_REGISTER_VESSEL(classname, keyword)
Mean(const vesselbase::VesselOptions &da)
Definition: Mean.cpp:52
void error(const std::string &errmsg)
Report an error.
Definition: Vessel.cpp:122
This class is used to pass the input to Vessels.
Definition: Vessel.h:53
std::string function_description()
The rest of the description of what we are calculating.
Definition: Mean.cpp:58
static void registerKeywords(Keywords &keys)
Definition: Mean.cpp:42
static void reserveKeyword(Keywords &keys)
Definition: Mean.cpp:46
void reserveFlag(const std::string &k, const bool def, const std::string &d, const bool isvessel=false)
Reserve a flag.
Definition: Keywords.cpp:140
void int double * da
Definition: Matrix.h:47
void finish()
Complete the calculation once the loop is finished.
Definition: Mean.cpp:76
double getElementValue(const unsigned &ival) const
Get the value of this element.
ActionWithVessel * getAction()
Return a pointer to the action we are working in.
Definition: Vessel.h:236