All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Combine.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 "Function.h"
23 #include "ActionRegister.h"
24 
25 #include <cmath>
26 
27 using namespace std;
28 
29 namespace PLMD{
30 namespace function{
31 
32 //+PLUMEDOC FUNCTION COMBINE
33 /*
34 Calculate a polynomial combination of a set of other variables.
35 
36 The functional form of this function is
37 \f[
38 C=\sum_{i=1}^{N_{arg}} c_i x_i^{p_i}
39 \f]
40 
41 The coefficients c and powers p are provided as vectors.
42 
43 
44 
45 \par Examples
46 The following input tells plumed to print the distance between atoms 3 and 5
47 its square (as computed from the x,y,z components) and the distance
48 again as computed from the square root of the square.
49 \verbatim
50 DISTANCE LABEL=dist ATOMS=3,5 COMPONENTS
51 COMBINE LABEL=distance2 ARG=dist.x,dist.y,dist.z POWERS=2,2,2 PERIODIC=NO
52 COMBINE LABEL=distance ARG=distance2 POWERS=0.5 PERIODIC=NO
53 PRINT ARG=distance,distance2
54 \endverbatim
55 (See also \ref PRINT and \ref DISTANCE).
56 
57 
58 */
59 //+ENDPLUMEDOC
60 
61 
62 class Combine :
63  public Function
64 {
65  bool normalize;
66  std::vector<double> coefficients;
67  std::vector<double> powers;
68 public:
69  Combine(const ActionOptions&);
70  void calculate();
71  static void registerKeywords(Keywords& keys);
72 };
73 
74 
75 PLUMED_REGISTER_ACTION(Combine,"COMBINE")
76 
77 void Combine::registerKeywords(Keywords& keys){
78  Function::registerKeywords(keys);
79  keys.use("ARG"); keys.use("PERIODIC");
80  keys.add("compulsory","COEFFICIENTS","1.0","the coefficients of the arguments in your function");
81  keys.add("compulsory","POWERS","1.0","the powers to which you are raising each of the arguments in your function");
82  keys.addFlag("NORMALIZE",false,"normalize all the coefficents so that in total they are equal to one");
83 }
84 
85 Combine::Combine(const ActionOptions&ao):
86 Action(ao),
87 Function(ao),
88 normalize(false),
89 coefficients(getNumberOfArguments(),1.0),
90 powers(getNumberOfArguments(),1.0)
91 {
92  parseVector("COEFFICIENTS",coefficients);
93  if(coefficients.size()!=static_cast<unsigned>(getNumberOfArguments()))
94  error("Size of COEFFICIENTS array should be the same as number for arguments");
95 
96  parseVector("POWERS",powers);
97  if(powers.size()!=static_cast<unsigned>(getNumberOfArguments()))
98  error("Size of POWERS array should be the same as number for arguments");
99 
100  parseFlag("NORMALIZE",normalize);
101 
102  if(normalize){
103  double n=0.0;
104  for(unsigned i=0;i<coefficients.size();i++) n+=coefficients[i];
105  for(unsigned i=0;i<coefficients.size();i++) coefficients[i]*=(1.0/n);
106  }
107 
109  checkRead();
110 
111  log.printf(" with coefficients:");
112  for(unsigned i=0;i<coefficients.size();i++) log.printf(" %f",coefficients[i]);
113  log.printf("\n");
114  log.printf(" and powers:");
115  for(unsigned i=0;i<powers.size();i++) log.printf(" %f",powers[i]);
116  log.printf("\n");
117 }
118 
120  double combine=0.0;
121  for(unsigned i=0;i<coefficients.size();++i){
122  combine+=coefficients[i]*pow(getArgument(i),powers[i]);
123  setDerivative(i,coefficients[i]*powers[i]*pow(getArgument(i),powers[i]-1.0));
124  };
125  setValue(combine);
126 }
127 
128 }
129 }
130 
131 
void parseFlag(const std::string &key, bool &t)
Parse one keyword as boolean flag.
Definition: Action.cpp:104
Log & log
Reference to the log stream.
Definition: Action.h:93
void error(const std::string &msg) const
Crash calculation and print documentation.
Definition: Action.cpp:195
void checkRead()
Check if Action was properly read.
Definition: Action.cpp:161
STL namespace.
std::vector< double > powers
Definition: Combine.cpp:67
void const char const char int * n
Definition: Matrix.h:42
This class holds the keywords and their documentation.
Definition: Keywords.h:36
This class is used to bring the relevant information to the Action constructor.
Definition: Action.h:41
int printf(const char *fmt,...)
Formatted output with explicit format - a la printf.
Definition: OFile.cpp:82
Base class for all the input Actions.
Definition: Action.h:60
void parseVector(const std::string &key, std::vector< T > &t)
Parse one keyword as std::vector.
Definition: Action.h:311
void setValue(const double &d)
Set the default value (the one without name)
double getArgument(const unsigned n) const
Returns the value of an argument.
Provides the keyword COMBINE
Definition: Combine.cpp:62
This is the abstract base class to use for implementing new CV function, within it there is informati...
Definition: Function.h:37
std::vector< double > coefficients
Definition: Combine.cpp:66
unsigned getNumberOfArguments() const
Returns the number of arguments.
void setDerivative(int, double)
Definition: Function.h:59
void calculate()
Calculate an Action.
Definition: Combine.cpp:119