All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
FuncPathMSD.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 <cmath>
23 
24 #include "Function.h"
25 #include "ActionRegister.h"
26 
27 #include <string>
28 #include <cstring>
29 #include <iostream>
30 
31 using namespace std;
32 
33 namespace PLMD{
34 namespace function{
35 
36 //+PLUMEDOC FUNCTION FUNCPATHMSD
37 /*
38 This function calculates path collective variables.
39 
40 This is the Path Collective Variables implementation
41 ( see \cite brand07 ).
42 This variable computes the progress along a given set of frames that is provided
43 in input ("s" component) and the distance from them ("z" component).
44 It is a function of MSD that are obtained by the joint use of MSD variable and SQUARED flag
45 (see below).
46 
47 \par Examples
48 
49 Here below is a case where you have defined three frames and you want to
50 calculate the progress alng the path and the distance from it in p1
51 
52 \verbatim
53 t1: RMSD REFERENCE=frame_1.dat TYPE=OPTIMAL SQUARED
54 t2: RMSD REFERENCE=frame_21.dat TYPE=OPTIMAL SQUARED
55 t3: RMSD REFERENCE=frame_42.dat TYPE=OPTIMAL SQUARED
56 p1: FUNCPATHMSD ARG=t1,t2,t3 LAMBDA=500.0
57 PRINT ARG=t1,t2,t3,p1.s,p1.z STRIDE=1 FILE=colvar FMT=%8.4f
58 \endverbatim
59 
60 */
61 //+ENDPLUMEDOC
62 
63 class FuncPathMSD : public Function {
64  double lambda;
66  double neigh_stride;
67  vector< pair<Value *,double> > neighpair;
68  map<Value *,double > indexmap; // use double to allow isomaps
69  vector <Value*> allArguments;
70 // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
71 // this below is useful when one wants to sort a vector of double and have back the order
72 // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
73 // create a custom sorter
74 typedef vector<double>::const_iterator myiter;
75 struct ordering {
76  bool operator ()(pair<unsigned , myiter> const& a, pair<unsigned , myiter> const& b) {
77  return *(a.second) < *(b.second);
78  }
79 };
80 // sorting utility
81 vector<int> increasingOrder( vector<double> &v){
82  // make a pair
83  vector< pair<unsigned , myiter> > order(v.size());
84  unsigned n = 0;
85  for (myiter it = v.begin(); it != v.end(); ++it, ++n){
86  order[n] = make_pair(n, it); // note: heere i do not put the values but the addresses that point to the value
87  }
88  // now sort according the second value
89  sort(order.begin(), order.end(), ordering());
90  typedef vector< pair<unsigned , myiter> >::const_iterator pairiter;
91  vector<int> vv(v.size());n=0;
92  for ( pairiter it = order.begin(); it != order.end(); ++it ){
93  vv[n]=(*it).first;n++;
94  }
95  return vv;
96 }
97 // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
98 // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
99 
100 struct pairordering {
101  bool operator ()(pair<Value * , double> const& a, pair<Value* , double> const& b) {
102  return (a).second > (b).second;
103  }
104 };
105 
106 public:
107  FuncPathMSD(const ActionOptions&);
108 // active methods:
109  virtual void calculate();
110  virtual void prepare();
111  static void registerKeywords(Keywords& keys);
112 };
113 
114 PLUMED_REGISTER_ACTION(FuncPathMSD,"FUNCPATHMSD")
115 
116 void FuncPathMSD::registerKeywords(Keywords& keys){
117  Function::registerKeywords(keys);
118  keys.use("ARG");
119  keys.add("compulsory","LAMBDA","all compulsory keywords should be added like this with a description here");
120  keys.add("optional","NEIGH_SIZE","all optional keywords that have input should be added like a description here");
121  keys.add("optional","NEIGH_STRIDE","all optional keywords that have input should be added like a description here");
122  componentsAreNotOptional(keys);
123  keys.addOutputComponent("s","default","the position on the path");
124  keys.addOutputComponent("z","default","the distance from the path");
125 }
126 FuncPathMSD::FuncPathMSD(const ActionOptions&ao):
127 Action(ao),
128 Function(ao),
129 neigh_size(-1),
130 neigh_stride(-1.)
131 {
132 
133  parse("LAMBDA",lambda);
134  parse("NEIGH_SIZE",neigh_size);
135  parse("NEIGH_STRIDE",neigh_stride);
136  checkRead();
137  log.printf(" lambda is %f\n",lambda);
138  // list the action involved and check the type
139  for(unsigned i=0;i<getNumberOfArguments();i++){
140  // for each value get the name and the label of the corresponding action
141  std::string myname=getPntrToArgument(i)->getPntrToAction()->getName();
142  if(myname!="RMSD")plumed_merror("This argument is not of RMSD type!!!");
143  }
144  log.printf(" Consistency check completed! Your path cvs look good!\n");
145  // do some neighbor printout
146  if(neigh_stride>0. || neigh_size>0){
148  log.printf(" List size required ( %d ) is too large: resizing to the maximum number of arg required: %d \n",neigh_size,getNumberOfArguments());
150  }
151  log.printf(" Neighbor list enabled: \n");
152  log.printf(" size : %d elements\n",neigh_size);
153  log.printf(" stride : %f time \n",neigh_stride);
154  }else{
155  log.printf(" Neighbor list NOT enabled \n");
156  }
157 
160 
161  // now backup the arguments
162  for(unsigned i=0;i<getNumberOfArguments();i++)allArguments.push_back(getPntrToArgument(i));
163  double i=1.;
164  for(std::vector<Value*>:: const_iterator it=allArguments.begin();it!=allArguments.end() ;++it){
165  indexmap[(*it)]=i;i+=1.;
166  }
167 
168 }
169 // calculator
171  // log.printf("NOW CALCULATE! \n");
172  double s_path=0.;
173  double partition=0.;
174  double tmp;
175  if(neighpair.empty()){ // at first step, resize it
176  neighpair.resize(allArguments.size());
177  for(unsigned i=0;i<allArguments.size();i++)neighpair[i].first=allArguments[i];
178  }
179 
180  Value* val_s_path=getPntrToComponent("s");
181  Value* val_z_path=getPntrToComponent("z");
182 
183  typedef vector< pair< Value *,double> >::iterator pairiter;
184  for(pairiter it=neighpair.begin();it!=neighpair.end();++it){
185  (*it).second=exp(-lambda*((*it).first->get()));
186  s_path+=(indexmap[(*it).first])*(*it).second;
187  partition+=(*it).second;
188  }
189  s_path/=partition;
190  val_s_path->set(s_path);
191  val_z_path->set(-(1./lambda)*std::log(partition));
192  int n=0;
193  for(pairiter it=neighpair.begin();it!=neighpair.end();++it){
194  double expval=(*it).second;
195  tmp=lambda*expval*(s_path-(indexmap[(*it).first]))/partition;
196  setDerivative(val_s_path,n,tmp);
197  setDerivative(val_z_path,n,expval/partition);
198  n++;
199  }
200 
201 // log.printf("CALCULATION DONE! \n");
202 }
203 ///
204 /// this function updates the needed argument list
205 ///
207 
208  // neighbor list: rank and activate the chain for the next step
209 
210  // neighbor list: if neigh_size<0 never sort and keep the full vector
211  // neighbor list: if neigh_size>0
212  // if the size is full -> sort the vector and decide the dependencies for next step
213  // if the size is not full -> check if next step will need the full dependency otherwise keep this dependencies
214 
215  // here just resize the neighpair. The real resizing of reinit will be done by the prepare stage that will modify the list of arguments
216  if (neigh_size>0){
217  if(neighpair.size()==allArguments.size()){ // I just did the complete round: need to sort, shorten and give it a go
218  // sort the values
219  sort(neighpair.begin(),neighpair.end(),pairordering());
220  // resize the effective list
221  neighpair.resize(neigh_size);
222  log.printf(" NEIGH LIST NOW INCLUDE INDEXES: ");
223  for(unsigned i=0;i<neigh_size;++i)log.printf(" %f ",indexmap[neighpair[i].first]);log.printf(" \n");
224  }else{
225  if( int(getStep())%int(neigh_stride/getTimeStep())==0 ){
226  log.printf(" Time %f : recalculating full neighlist \n",getStep()*getTimeStep());
227  neighpair.resize(allArguments.size());
228  for(unsigned i=0;i<allArguments.size();i++)neighpair[i].first=allArguments[i];
229  }
230  }
231  }else{
232  if( int(getStep())==0){
233  neighpair.resize(allArguments.size());
234  for(unsigned i=0;i<allArguments.size();i++)neighpair[i].first=allArguments[i];
235  }
236  }
237  typedef vector< pair<Value*,double> >::iterator pairiter;
238  vector<Value*> argstocall;
239  //log.printf("PREPARING \n");
240  argstocall.clear();
241  if(!neighpair.empty()){
242  for(pairiter it=neighpair.begin();it!=neighpair.end();++it){
243  argstocall.push_back( (*it).first );
244  // log.printf("CALLING %p %f ",(*it).first ,indexmap[(*it).first] );
245  }
246  }else{
247  for(unsigned i=0;i<allArguments.size();i++){
248  argstocall.push_back(allArguments[i]);
249  }
250  }
251  // now the list of argument changes
252  requestArguments(argstocall);
253  //now resize the derivatives as well
254  //for each value in this action
255  for(unsigned i=0;i< getNumberOfComponents();i++){
256  //resize the derivative to the number the
259  }
260  //log.printf("PREPARING DONE! \n");
261 }
262 
263 }
264 }
265 
266 
virtual void calculate()
Calculate an Action.
Log & log
Reference to the log stream.
Definition: Action.h:93
Provides the keyword FUNCPATHMSD
Definition: FuncPathMSD.cpp:63
void componentIsNotPeriodic(const std::string &name)
Set your value component to have no periodicity.
A class for holding the value of a function together with its derivatives.
Definition: Value.h:46
void requestArguments(const std::vector< Value * > &arg)
Setup the dependencies.
double getTimeStep() const
Return the timestep.
Definition: Action.cpp:177
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 clearDerivatives()
Set all the derivatives to zero.
Definition: Value.h:241
void const char const char int * n
Definition: Matrix.h:42
void set(double)
Set the value of the function.
Definition: Value.h:174
void addComponentWithDerivatives(const std::string &name)
Definition: Function.cpp:58
void parse(const std::string &key, T &t)
Parse one keyword as generic type.
Definition: Action.h:273
This class holds the keywords and their documentation.
Definition: Keywords.h:36
virtual void prepare()
this function updates the needed argument list
This class is used to bring the relevant information to the Action constructor.
Definition: Action.h:41
map< Value *, double > indexmap
Definition: FuncPathMSD.cpp:68
int printf(const char *fmt,...)
Formatted output with explicit format - a la printf.
Definition: OFile.cpp:82
const std::string & getName() const
Returns the name.
Definition: Action.h:268
Base class for all the input Actions.
Definition: Action.h:60
void resizeDerivatives(int n)
Set the number of derivatives.
Definition: Value.h:218
ActionWithValue * getPntrToAction()
This returns the pointer to the action where this value is calculated.
Definition: Value.cpp:154
vector< double >::const_iterator myiter
Definition: FuncPathMSD.cpp:74
vector< int > increasingOrder(vector< double > &v)
Definition: FuncPathMSD.cpp:81
long int getStep() const
Return the present timestep.
Definition: Action.cpp:169
vector< pair< Value *, double > > neighpair
Definition: FuncPathMSD.cpp:67
int getNumberOfComponents() const
Returns the number of values defined.
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.
vector< Value * > allArguments
Definition: FuncPathMSD.cpp:69
void const char const char int double * a
Definition: Matrix.h:42
unsigned getNumberOfArguments() const
Returns the number of arguments.
void setDerivative(int, double)
Definition: Function.h:59