All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Histogram.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 "Analysis.h"
23 #include "core/ActionRegister.h"
24 #include "tools/Grid.h"
25 #include "tools/KernelFunctions.h"
26 #include "tools/IFile.h"
27 #include "tools/OFile.h"
28 
29 namespace PLMD{
30 namespace analysis{
31 
32 //+PLUMEDOC ANALYSIS HISTOGRAM
33 /*
34 Calculate the probability density as a function of a few CVs using kernel density estimation
35 
36 \par Examples
37 
38 The following input monitors two torsional angles during a simulation
39 and outputs a histogram as a function of them at the end of the simulation.
40 \verbatim
41 TORSION ATOMS=1,2,3,4 LABEL=r1
42 TORSION ATOMS=2,3,4,5 LABEL=r2
43 HISTOGRAM ...
44  ARG=r1,r2
45  USE_ALL_DATA
46  GRID_MIN=-3.14,-3.14
47  GRID_MAX=3.14,3.14
48  GRID_BIN=200,200
49  BANDWIDTH=0.05,0.05
50  GRID_WFILE=histo
51 ... HISTOGRAM
52 \endverbatim
53 
54 The following input monitors two torsional angles during a simulation
55 and outputs the histogram accumulated thus far every 100000 steps.
56 \verbatim
57 TORSION ATOMS=1,2,3,4 LABEL=r1
58 TORSION ATOMS=2,3,4,5 LABEL=r2
59 HISTOGRAM ...
60  ARG=r1,r2
61  RUN=100000
62  GRID_MIN=-3.14,-3.14
63  GRID_MAX=3.14,3.14
64  GRID_BIN=200,200
65  BANDWIDTH=0.05,0.05
66  GRID_WFILE=histo
67 ... HISTOGRAM
68 \endverbatim
69 
70 The following input monitors two torsional angles during a simulation
71 and outputs a separate histogram for each 100000 steps worth of trajectory.
72 \verbatim
73 TORSION ATOMS=1,2,3,4 LABEL=r1
74 TORSION ATOMS=2,3,4,5 LABEL=r2
75 HISTOGRAM ...
76  ARG=r1,r2
77  RUN=100000 NOMEMORY
78  GRID_MIN=-3.14,-3.14
79  GRID_MAX=3.14,3.14
80  GRID_BIN=200,200
81  BANDWIDTH=0.05,0.05
82  GRID_WFILE=histo
83 ... HISTOGRAM
84 \endverbatim
85 
86 */
87 //+ENDPLUMEDOC
88 
89 class Histogram : public Analysis {
90 private:
91  std::vector<std::string> gmin, gmax;
92  std::vector<double> point, bw;
93  std::vector<unsigned> gbin;
94  std::string gridfname;
95  std::string kerneltype;
96 public:
97  static void registerKeywords( Keywords& keys );
98  Histogram(const ActionOptions&ao);
99  void performAnalysis();
100 };
101 
102 PLUMED_REGISTER_ACTION(Histogram,"HISTOGRAM")
103 
104 void Histogram::registerKeywords( Keywords& keys ){
106  keys.add("compulsory","GRID_MIN","the lower bounds for the grid");
107  keys.add("compulsory","GRID_MAX","the upper bounds for the grid");
108  keys.add("compulsory","GRID_BIN","the number of bins for the grid");
109  keys.add("compulsory","KERNEL","gaussian","the kernel function you are using. More details on the kernels available in plumed can be found in \\ref kernelfunctions.");
110  keys.add("compulsory","BANDWIDTH","the bandwdith for kernel density estimation");
111  keys.add("compulsory","GRID_WFILE","histogram","the file on which to write the grid");
112  keys.use("NOMEMORY");
113 }
114 
117 gmin(getNumberOfArguments()),
118 gmax(getNumberOfArguments()),
119 point(getNumberOfArguments()),
120 bw(getNumberOfArguments()),
121 gbin(getNumberOfArguments())
122 {
123  // Read stuff for Grid
124  parseVector("GRID_MIN",gmin);
125  parseVector("GRID_MAX",gmax);
126  parseVector("GRID_BIN",gbin);
127  parseOutputFile("GRID_WFILE",gridfname);
128 
129  // Read stuff for window functions
130  parseVector("BANDWIDTH",bw);
131  // Read the type of kernel we are using
132  parse("KERNEL",kerneltype);
133  checkRead();
134 
135  log.printf(" Using %s kernel functions\n",kerneltype.c_str() );
136  log.printf(" Grid min");
137  for(unsigned i=0;i<gmin.size();++i) log.printf(" %s",gmin[i].c_str() );
138  log.printf("\n");
139  log.printf(" Grid max");
140  for(unsigned i=0;i<gmax.size();++i) log.printf(" %s",gmax[i].c_str() );
141  log.printf("\n");
142  log.printf(" Grid bin");
143  for(unsigned i=0;i<gbin.size();++i) log.printf(" %d",gbin[i]);
144  log.printf("\n");
145 }
146 
148  // Back up old histogram files
149 // std::string oldfname=saveResultsFromPreviousAnalyses( gridfname );
150 
151  // Get pbc stuff for grid
152  std::vector<bool> pbc; std::string dmin,dmax;
153  for(unsigned i=0;i<getNumberOfArguments();++i){
154  pbc.push_back( getPeriodicityInformation(i,dmin,dmax) );
155  if(pbc[i]){ Tools::convert(dmin,gmin[i]); Tools::convert(dmax,gmax[i]); }
156  }
157 
158  Grid* gg; IFile oldf; oldf.link(*this);
159  if( usingMemory() && oldf.FileExist(gridfname) ){
160  oldf.open(gridfname);
161  gg = Grid::create( "probs", getArguments(), oldf, gmin, gmax, gbin, false, false, false );
162  oldf.close();
163  } else {
164  gg = new Grid( "probs", getArguments(), gmin, gmax, gbin,false,false);
165  }
166  // Set output format for grid
167  gg->setOutputFmt( getOutputFormat() );
168 
169  // Now build the histogram
170  double weight; std::vector<double> point( getNumberOfArguments() );
171  for(unsigned i=0;i<getNumberOfDataPoints();++i){
172  getDataPoint( i, point, weight );
173  KernelFunctions kernel( point, bw, kerneltype, false, weight, true);
174  gg->addKernel( kernel );
175 
176  }
177  // Normalize the histogram
179 
180  // Write the grid to a file
181  OFile gridfile; gridfile.link(*this); gridfile.setBackupString("analysis");
182  gridfile.open( gridfname ); gg->writeToFile( gridfile );
183  // Close the file
184  gridfile.close(); delete gg;
185 }
186 
187 }
188 }
bool getPeriodicityInformation(const unsigned &i, std::string &dmin, std::string &dmax)
Returns true if argument i is periodic together with the domain.
Definition: Analysis.cpp:337
Log & log
Reference to the log stream.
Definition: Action.h:93
bool usingMemory() const
Are we analyzing each data block separately (if we are not this also returns the old normalization ) ...
Definition: Analysis.h:152
void getDataPoint(const unsigned &idata, std::vector< double > &point, double &weight) const
Retrieve the ith point.
Definition: Analysis.h:141
void parseOutputFile(const std::string &key, std::string &filename)
This is used to read in output file names for analysis methods.
Definition: Analysis.cpp:198
static bool convert(const std::string &str, double &t)
Convert a string to a double, reading it.
Definition: Tools.cpp:74
FileBase & link(FILE *)
Link to an already open filed.
Definition: FileBase.cpp:64
std::vector< double > bw
Definition: Histogram.cpp:92
void checkRead()
Check if Action was properly read.
Definition: Action.cpp:161
double getNormalization() const
Return the normalization constant.
Definition: Analysis.cpp:324
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
IFile & open(const std::string &name)
Opens the file.
Definition: IFile.cpp:90
This class is used to bring the relevant information to the Action constructor.
Definition: Action.h:41
OFile & link(OFile &)
Allows linking this OFile to another one.
Definition: OFile.cpp:71
void addKernel(const KernelFunctions &kernel)
add a kernel function to the grid
Definition: Grid.cpp:325
int printf(const char *fmt,...)
Formatted output with explicit format - a la printf.
Definition: OFile.cpp:82
void close()
Closes the file Should be used only for explicitely opened files.
Definition: FileBase.cpp:140
void parseVector(const std::string &key, std::vector< T > &t)
Parse one keyword as std::vector.
Definition: Action.h:311
std::vector< unsigned > gbin
Definition: Histogram.cpp:93
Class for input files.
Definition: IFile.h:40
std::vector< Value * > getArguments()
Overwrite ActionWithArguments getArguments() so that we don't return the bias.
Definition: Analysis.h:172
unsigned getNumberOfDataPoints() const
Return the number of data points.
Definition: Analysis.h:131
This is the abstract base class to use for implementing new methods for analyzing the trajectory...
Definition: Analysis.h:40
virtual void writeToFile(OFile &)
dump grid on file
Definition: Grid.cpp:531
unsigned getNumberOfArguments() const
Return the number of arguments (this overwrites the one in ActionWithArguments)
Definition: Analysis.h:161
std::vector< std::string > gmin
Definition: Histogram.cpp:91
Class for output files.
Definition: OFile.h:84
virtual void scaleAllValuesAndDerivatives(const double &scalef)
Scale all grid values and derivatives by a constant factor.
Definition: Grid.cpp:500
bool FileExist(const std::string &path)
Check if the file exists.
Definition: FileBase.cpp:118
std::vector< double > point
Definition: Histogram.cpp:92
static void registerKeywords(Keywords &keys)
Definition: Histogram.cpp:104
Histogram(const ActionOptions &ao)
Definition: Histogram.cpp:115
void setOutputFmt(std::string ss)
set output format
Definition: Grid.h:191
void setBackupString(const std::string &)
Set the string name to be used for automatic backup.
Definition: OFile.cpp:218
Provides the keyword HISTOGRAM
Definition: Histogram.cpp:89
static void registerKeywords(Keywords &keys)
Definition: Analysis.cpp:55
OFile & open(const std::string &name)
Opens the file using automatic append/backup.
Definition: OFile.cpp:264
std::vector< std::string > gmax
Definition: Histogram.cpp:91
std::string getOutputFormat() const
Return the format to use for numbers in output files.
Definition: Analysis.h:179
static Grid * create(const std::string &, std::vector< Value * >, IFile &, bool, bool, bool)
read grid from file
Definition: Grid.cpp:573
#define PLUMED_ANALYSIS_INIT(ao)
Definition: Analysis.h:28