All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Tools.h
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 #ifndef __PLUMED_tools_Tools_h
23 #define __PLUMED_tools_Tools_h
24 
25 #include "AtomNumber.h"
26 #include <vector>
27 #include <string>
28 #include <cstdio>
29 #include <cmath>
30 #include <limits>
31 #include <algorithm>
32 #include <sstream>
33 
34 namespace PLMD{
35 
36 class IFile;
37 
38 /// \ingroup TOOLBOX
39 /// Very small non-zero number
41 
42 /// \ingroup TOOLBOX
43 /// Boltzman constant in kj/K
44 const double kBoltzmann(0.0083144621);
45 
46 /// \ingroup TOOLBOX
47 /// PI
48 const double pi(3.141592653589793238462643383279502884197169399375105820974944592307);
49 
50 /// \ingroup TOOLBOX
51 /// Empty class which just contains several (static) tools
52 class Tools{
53 public:
54 /// Split the line in words using separators.
55 /// It also take into account parenthesis. Outer parenthesis found are removed from
56 /// output, and the text between them is considered as a single word. Only the
57 /// outer parenthesis are processed, to allow nesting them.
58 /// parlevel, if not NULL, is increased or decreased according to the number of opened/closed parenthesis
59  static std::vector<std::string> getWords(const std::string & line,const char* sep=NULL,int* parlevel=NULL,const char* parenthesis="{");
60 /// Get a line from the file pointer ifile
61  static bool getline(FILE*,std::string & line);
62 /// Get a parsed line from the file pointer ifile
63 /// This function already takes care of joining continued lines and splitting the
64 /// resulting line into an array of words
65  static bool getParsedLine(IFile&ifile,std::vector<std::string> & line);
66 /// Convert a string to a double, reading it
67  static bool convert(const std::string & str,double & t);
68 /// Convert a string to a float, reading it
69  static bool convert(const std::string & str,float & t);
70 /// Convert a string to a int, reading it
71  static bool convert(const std::string & str,int & t);
72 /// Convert a string to a long int, reading it
73  static bool convert(const std::string & str,long int & t);
74 /// Convert a string to an unsigned int, reading it
75  static bool convert(const std::string & str,unsigned & t);
76 /// Convert a string to a atom number, reading it
77  static bool convert(const std::string & str,AtomNumber & t);
78 /// Convert a string to a string (i.e. copy)
79  static bool convert(const std::string & str,std::string & t);
80 /// Convert anything into a string
81  template<typename T>
82  static void convert(T i,std::string & str);
83 /// Remove trailing blanks
84  static void trim(std::string & s);
85 /// Remove trailing comments
86  static void trimComments(std::string & s);
87 /// Apply pbc for a unitary cell
88  static double pbc(double);
89 /// Retrieve a key from a vector of options.
90 /// It finds a key starting with "key=" or equal to "key" and copy the
91 /// part after the = on s. E.g.:
92 /// line.push_back("aa=xx");
93 /// getKey(line,"aa",s);
94 /// will set s="xx"
95  static bool getKey(std::vector<std::string>& line,const std::string & key,std::string & s);
96 /// Find a keyword on the input line, eventually deleting it, and saving its value to val
97  template <class T>
98  static bool parse(std::vector<std::string>&line,const std::string&key,T&val);
99 /// Find a keyword on the input line, eventually deleting it, and saving its value to a vector
100  template <class T>
101  static bool parseVector(std::vector<std::string>&line,const std::string&key,std::vector<T>&val);
102 /// Find a keyword without arguments on the input line
103  static bool parseFlag(std::vector<std::string>&line,const std::string&key,bool&val);
104 /// Interpret atom ranges
105  static void interpretRanges(std::vector<std::string>&);
106 /// Remove duplicates from a vector of type T
107  template <typename T>
108  static void removeDuplicates(std::vector<T>& vec);
109 /// interpret ":" syntax for labels
110  static void interpretLabel(std::vector<std::string>&s);
111 /// list files in a directory
112  static std::vector<std::string> ls(const std::string&);
113 /// removes leading and trailing blanks from a string
114  static void stripLeadingAndTrailingBlanks( std::string& str );
115 /// Extract the extensions from a file name.
116 /// E.g.: extension("pippo.xyz")="xyz".
117 /// It only returns extensions with a length between 1 and 4
118 /// E.g.: extension("pippo.12345")="" whereas extenion("pippo.1234")="1234";
119 /// It is also smart enough to detect "/", so that
120 /// extension("pippo/.t")="" whereas extension("pippo/a.t")="t"
121  static std::string extension(const std::string&);
122 /// Fast int power
123  static double fastpow(double base,int exp);
124 };
125 
126 template <class T>
127 bool Tools::parse(std::vector<std::string>&line,const std::string&key,T&val){
128  std::string s;
129  if(!getKey(line,key+"=",s)) return false;
130  if(s.length()>0 && !convert(s,val))return false;
131  return true;
132 }
133 
134 template <class T>
135 bool Tools::parseVector(std::vector<std::string>&line,const std::string&key,std::vector<T>&val){
136  std::string s;
137  if(!getKey(line,key+"=",s)) return false;
138 // if(s.length()==0) return true;
139  val.clear();
140  std::vector<std::string> words=getWords(s,"\t\n ,");
141  for(unsigned i=0;i<words.size();++i){
142  T v;
143  if(!convert(words[i],v))return false;
144  val.push_back(v);
145  }
146  return true;
147 }
148 
149 template<typename T>
150 void Tools::removeDuplicates(std::vector<T>& vec)
151 {
152  std::sort(vec.begin(), vec.end());
153  vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
154 }
155 
156 inline
157 bool Tools::parseFlag(std::vector<std::string>&line,const std::string&key,bool&val){
158  for(std::vector<std::string>::iterator p=line.begin();p!=line.end();++p){
159  if(key==*p){
160  val=true;
161  line.erase(p);
162  return true;
163  }
164  }
165  return false;
166 }
167 
168 /// beware: this brings any number into a pbc that ranges from -0.5 to 0.5
169 inline
170 double Tools::pbc(double x){
171  if(std::numeric_limits<int>::round_style == std::round_toward_zero) {
172  const double offset=100.0;
173  const double y=x+offset;
174  if(y>=0) return y-int(y+0.5);
175  else return y-int(y-0.5);
176  } else if(std::numeric_limits<int>::round_style == std::round_to_nearest) {
177  return x-int(x);
178  } else return x-floor(x+0.5);
179 }
180 
181 template<typename T>
182 void Tools::convert(T i,std::string & str){
183  std::ostringstream ostr;
184  ostr<<i;
185  str=ostr.str();
186 }
187 
188 inline
189 double Tools::fastpow(double base, int exp)
190 {
191  if(exp<0){
192  exp=-exp;
193  base=1.0/base;
194  }
195  double result = 1.0;
196  while (exp)
197  {
198  if (exp & 1)
199  result *= base;
200  exp >>= 1;
201  base *= base;
202  }
203 
204  return result;
205 }
206 
207 }
208 
209 #endif
210 
Simple class to store the index of an atom.
Definition: AtomNumber.h:39
static bool parseVector(std::vector< std::string > &line, const std::string &key, std::vector< T > &val)
Find a keyword on the input line, eventually deleting it, and saving its value to a vector...
Definition: Tools.h:135
const double epsilon
static bool getline(FILE *, std::string &line)
Get a line from the file pointer ifile.
Definition: Tools.cpp:190
Empty class which just contains several (static) tools.
Definition: Tools.h:52
static std::vector< std::string > ls(const std::string &)
list files in a directory
Definition: Tools.cpp:272
static bool parseFlag(std::vector< std::string > &line, const std::string &key, bool &val)
Find a keyword without arguments on the input line.
Definition: Tools.h:157
static bool convert(const std::string &str, double &t)
Convert a string to a double, reading it.
Definition: Tools.cpp:74
static bool parse(std::vector< std::string > &line, const std::string &key, T &val)
Find a keyword on the input line, eventually deleting it, and saving its value to val...
Definition: Tools.h:127
static double pbc(double)
Apply pbc for a unitary cell.
Definition: Tools.h:170
const double epsilon(std::numeric_limits< double >::epsilon())
Very small non-zero number.
static void trim(std::string &s)
Remove trailing blanks.
Definition: Tools.cpp:205
static double fastpow(double base, int exp)
Fast int power.
Definition: Tools.h:189
static std::vector< std::string > getWords(const std::string &line, const char *sep=NULL, int *parlevel=NULL, const char *parenthesis="{")
Split the line in words using separators.
Definition: Tools.cpp:112
static bool getParsedLine(IFile &ifile, std::vector< std::string > &line)
Get a parsed line from the file pointer ifile This function already takes care of joining continued l...
Definition: Tools.cpp:153
const double kBoltzmann(0.0083144621)
Boltzman constant in kj/K.
static std::string extension(const std::string &)
Extract the extensions from a file name.
Definition: Tools.cpp:289
static void interpretRanges(std::vector< std::string > &)
Interpret atom ranges.
Definition: Tools.cpp:231
static void trimComments(std::string &s)
Remove trailing comments.
Definition: Tools.cpp:210
Class for input files.
Definition: IFile.h:40
const double pi(3.141592653589793238462643383279502884197169399375105820974944592307)
PI.
static void stripLeadingAndTrailingBlanks(std::string &str)
removes leading and trailing blanks from a string
Definition: Tools.cpp:283
static void removeDuplicates(std::vector< T > &vec)
Remove duplicates from a vector of type T.
Definition: Tools.h:150
static bool getKey(std::vector< std::string > &line, const std::string &key, std::string &s)
Retrieve a key from a vector of options.
Definition: Tools.cpp:215
static void interpretLabel(std::vector< std::string > &s)
interpret ":" syntax for labels
Definition: Tools.cpp:261