All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Sum.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 "FunctionVessel.h"
23 #include "VesselRegister.h"
24 
25 namespace PLMD {
26 namespace vesselbase{
27 
28 class Sum : public FunctionVessel {
29 private:
30  std::vector<double> df;
31 public:
32  static void registerKeywords( Keywords& keys );
33  static void reserveKeyword( Keywords& keys );
34  Sum( const VesselOptions& da );
35  std::string function_description();
36  bool calculate();
37  void finish();
38 };
39 
41 
42 void Sum::registerKeywords( Keywords& keys ){
44 }
45 
47  keys.reserveFlag("SUM",false,"calculate the sum of all the quantities.",true);
48  keys.addOutputComponent("sum","SUM","the sum of values");
49 }
50 
52 FunctionVessel(da),
53 df(2)
54 {
55 }
56 
58  return "the sum of all the values";
59 }
60 
62  double weight=getAction()->getElementValue(1);
63  plumed_dbg_assert( weight>=getTolerance() );
64 
65  double val=getAction()->getElementValue(0);
66  addValueIgnoringTolerance(0,weight*val);
67  getAction()->chainRuleForElementDerivatives( 0, 0, weight, this );
68  if(diffweight) getAction()->chainRuleForElementDerivatives(0, 1, val, this);
69  return true;
70 }
71 
72 void Sum::finish(){
74  df[0]=1.0; df[1]=0.0;
76 }
77 
78 
79 
80 }
81 }
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.
std::string function_description()
The rest of the description of what we are calculating.
Definition: Sum.cpp:57
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
static void reserveKeyword(Keywords &keys)
Definition: Sum.cpp:46
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)
std::vector< double > df
Definition: Sum.cpp:30
void finish()
Complete the calculation once the loop is finished.
Definition: Sum.cpp:72
bool calculate()
Calculate the part of the vessel that is done in the loop.
Definition: Sum.cpp:61
static void registerKeywords(Keywords &keys)
Definition: Sum.cpp:42
This class is used to pass the input to Vessels.
Definition: Vessel.h:53
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
Sum(const VesselOptions &da)
Definition: Sum.cpp:51
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