All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Function.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 
24 using namespace std;
25 namespace PLMD{
26 namespace function{
27 
28 void Function::registerKeywords(Keywords& keys){
29  Action::registerKeywords(keys);
30  ActionWithValue::registerKeywords(keys);
31  ActionWithArguments::registerKeywords(keys);
32  keys.reserve("compulsory","PERIODIC","if the output of your function is periodic then you should specify the periodicity of the function. If the output is not periodic you must state this using PERIODIC=NO");
33 }
34 
35 Function::Function(const ActionOptions&ao):
36 Action(ao),
37 ActionWithValue(ao),
39 {
40 }
41 
43  plumed_massert( getNumberOfArguments()!=0, "for functions you must requestArguments before adding values");
46 
47  if( keywords.exists("PERIODIC") ){
48  std::vector<std::string> period;
49  parseVector("PERIODIC",period);
50  if(period.size()==1 && period[0]=="NO"){
52  } else if(period.size()==2){
53  setPeriodic(period[0],period[1]);
54  } else error("missing PERIODIC keyword");
55  }
56 }
57 
58 void Function::addComponentWithDerivatives( const std::string& name ){
59  plumed_massert( getNumberOfArguments()!=0, "for functions you must requestArguments before adding values");
62 }
63 
65 
66  vector<double> f(getNumberOfArguments(),0.0);
67  bool at_least_one_forced=false;
68 
69  std::vector<double> forces( getNumberOfArguments() );
70  for(int i=0;i<getNumberOfComponents();++i){
71  if( getPntrToComponent(i)->applyForce( forces ) ){
72  at_least_one_forced=true;
73  for(unsigned j=0;j<forces.size();j++){ f[j]+=forces[j]; }
74  }
75  }
76 
77  if(at_least_one_forced) for(unsigned i=0;i<getNumberOfArguments();++i){
78  getPntrToArgument(i)->addForce(f[i]);
79  }
80 }
81 }
82 }
void addForce(double f)
Add some force on this value.
Definition: Value.h:247
void setNotPeriodic()
Set your default value to have no periodicity.
bool applyForce(std::vector< double > &forces) const
Apply the forces to the derivatives using the chain rule (if there are no forces this routine returns...
Definition: Value.cpp:80
void error(const std::string &msg) const
Crash calculation and print documentation.
Definition: Action.cpp:195
Value * getPntrToValue()
Get a pointer to the default value.
STL namespace.
Value * getPntrToArgument(const unsigned n)
Return a pointer to specific argument.
void addValueWithDerivatives()
Add a value with the name label that has derivatives.
This is used to create PLMD::Action objects that take the output from some other Action as input...
Used to create a PLMD::Action that has some scalar or vectorial output that may or may not have some ...
void addComponentWithDerivatives(const std::string &name)
Definition: Function.cpp:58
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
void setPeriodic(const std::string &min, const std::string &max)
Set the value to be periodic with a particular domain.
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 resizeDerivatives(int n)
Set the number of derivatives.
Definition: Value.h:218
void reserve(const std::string &t, const std::string &k, const std::string &d, const bool isvessel=false)
Reserve a keyword.
Definition: Keywords.cpp:110
void apply()
Apply an Action.
Definition: Function.cpp:64
bool exists(const std::string &k) const
Check if there is a keyword with name k.
Definition: Keywords.cpp:239
void addComponentWithDerivatives(const std::string &name)
Add a value with a name like label.name that has derivatives.
int getNumberOfComponents() const
Returns the number of values defined.
Value * getPntrToComponent(int i)
Return a pointer to the component by index.
unsigned getNumberOfArguments() const
Returns the number of arguments.
const Keywords & keywords
Definition: Action.h:161