All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Piecewise.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 "core/ActionRegister.h"
23 #include "Function.h"
24 
25 #include <cmath>
26 
27 using namespace std;
28 
29 namespace PLMD{
30 namespace function{
31 
32 //+PLUMEDOC FUNCTION PIECEWISE
33 /*
34 Compute a piecewise straight line through its arguments that passes through
35 a set of ordered control points.
36 
37 For variables less than the first
38 (greater than the last) point, the value of the first (last) point is used.
39 
40 \f[
41 \frac{y_{i+1}-y_i}{x_{i+1}-x_i}(s-x_i)+y_i ; if x_i<s<x_{i+1}
42 \f]
43 \f[
44 y_N ; if x>x_{N-1}
45 \f]
46 \f[
47 y_1 ; if x<x_0
48 \f]
49 
50 Control points are passed using the POINT0=... POINT1=... syntax as in the example below
51 
52 If one argument is supplied, it results in a scalar quantity.
53 If multiple arguments are supplied, it results
54 in a vector of arguments.
55 
56 \par Examples
57 \verbatim
58 dist1: DISTANCE ATOMS=1,10
59 dist2: DISTANCE ATOMS=2,11
60 
61 pw: PIECEWISE POINT0=1,10 POINT1=1,PI POINT2=3,10 ARG=dist1
62 ppww: PIECEWISE POINT0=1,10 POINT1=1,PI POINT2=3,10 ARG=dist1,dist2
63 PRINT ARG=pw,ppww.1,ppww.2
64 \endverbatim
65 (See also \ref PRINT and \ref DISTANCE).
66 
67 
68 */
69 //+ENDPLUMEDOC
70 
71 
72 class Piecewise :
73  public Function
74 {
75  std::vector<std::pair<double,double> > points;
76 public:
77  Piecewise(const ActionOptions&);
78  void calculate();
79  static void registerKeywords(Keywords& keys);
80 };
81 
82 
83 PLUMED_REGISTER_ACTION(Piecewise,"PIECEWISE")
84 
85 void Piecewise::registerKeywords(Keywords& keys){
86  Function::registerKeywords(keys);
87  keys.use("ARG");
88  keys.add("numbered","POINT","This keyword is used to specify the various points in the function above.");
89  keys.reset_style("POINT","compulsory");
90  ActionWithValue::useCustomisableComponents(keys);
91 // componentsAreNotOptional(keys);
92 // keys.addOutputComponent("_pfunc","default","one or multiple instances of this quantity will be referenceable elsewhere "
93 // "in the input file. These quantities will be named with the arguments of the "
94 // "function followed by the character string _pfunc. These quantities tell the "
95 // "user the values of the piecewise functions of each of the arguments.");
96 }
97 
98 Piecewise::Piecewise(const ActionOptions&ao):
99 Action(ao),
100 Function(ao)
101 {
102  for(int i=0;;i++){
103  std::vector<double> pp;
104  if(!parseNumberedVector("POINT",i,pp) ) break;
105  if(pp.size()!=2) error("points should be in x,y format");
106  points.push_back(std::pair<double,double>(pp[0],pp[1]));
107  if(i>0 && points[i].first<=points[i-1].first) error("points abscissas should be monotonously increasing");
108  }
109 
110  for(int i=0;i<getNumberOfArguments();i++)
111  if(getPntrToArgument(i)->isPeriodic())
112  error("Cannot use PIECEWISE on periodic arguments");
113 
114  if(getNumberOfArguments()==1){
116  setNotPeriodic();
117  }else{
118  for(int i=0;i<getNumberOfArguments();i++){
119  string s; Tools::convert(i+1,s);
121 // addComponentWithDerivatives( getPntrToArgument(i)->getName()+"_pfunc" );
123  }
124  }
125  checkRead();
126 
127  log.printf(" on points:");
128  for(unsigned i=0;i<points.size();i++) log.printf(" (%f,%f)",points[i].first,points[i].second);
129  log.printf("\n");
130 }
131 
133  for(int i=0;i<getNumberOfArguments();i++){
134  double val=getArgument(i);
135  int p=0;
136  for(;p<points.size();p++){
137  if(val<points[p].first) break;
138  }
139  double f,d;
140  if(p==0){
141  f=points[0].second;
142  d=0.0;
143  } else if(p==points.size()){
144  f=points[points.size()-1].second;
145  d=0.0;
146  } else {
147  double m=(points[p].second-points[p-1].second) / (points[p].first-points[p-1].first);
148  f=m*(val-points[p-1].first)+points[p-1].second;
149  d=m;
150  }
151  if(getNumberOfArguments()==1) {
152  setValue(f);
153  setDerivative(i,d);
154  } else {
156  v->set(f);
157  v->addDerivative(i,d);
158  }
159  }
160 }
161 
162 }
163 }
164 
165 
void setNotPeriodic()
Set your default value to have no periodicity.
Log & log
Reference to the log stream.
Definition: Action.h:93
A class for holding the value of a function together with its derivatives.
Definition: Value.h:46
static bool convert(const std::string &str, double &t)
Convert a string to a double, reading it.
Definition: Tools.cpp:74
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.
Value * getPntrToArgument(const unsigned n)
Return a pointer to specific argument.
void set(double)
Set the value of the function.
Definition: Value.h:174
Provides the keyword PIECEWISE
Definition: Piecewise.cpp:72
void addComponentWithDerivatives(const std::string &name)
Definition: Function.cpp:58
This class holds the keywords and their documentation.
Definition: Keywords.h:36
void setNotPeriodic()
Set the function not periodic.
Definition: Value.cpp:87
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 calculate()
Calculate an Action.
Definition: Piecewise.cpp:132
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.
void const char const char int double int double double int int double int * m
Definition: Matrix.h:42
bool parseNumberedVector(const std::string &key, const int no, std::vector< T > &t)
Parse a vector with a number.
Definition: Action.h:352
bool isPeriodic() const
Check if the value is periodic.
Definition: Value.cpp:75
std::vector< std::pair< double, double > > points
Definition: Piecewise.cpp:75
This is the abstract base class to use for implementing new CV function, within it there is informati...
Definition: Function.h:37
Value * getPntrToComponent(int i)
Return a pointer to the component by index.
unsigned getNumberOfArguments() const
Returns the number of arguments.
void addDerivative(unsigned i, double d)
Add some derivative to the ith component of the derivatives array.
Definition: Value.h:224
void setDerivative(int, double)
Definition: Function.h:59