generic.hpp
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------------
2 // d-SEAMS is free software: you can redistribute it and/or modify
3 // it under the terms of the GNU General Public License as published by
4 // the Free Software Foundation, either version 3 of the License, or
5 // (at your option) any later version.
6 //
7 // A copy of the GNU General Public License is available at
8 // http://www.gnu.org/licenses/
9 //-----------------------------------------------------------------------------------
10 
11 #ifndef __GENERIC_H_
12 #define __GENERIC_H_
13 
14 #include <array>
15 #include <iostream>
16 #include <math.h>
17 #include <mol_sys.hpp>
18 
19 // Boost
20 #include <boost/math/constants/constants.hpp>
21 // Eigen
22 #include <eigen3/Eigen/Core>
23 #include <eigen3/Eigen/Dense>
24 
45 namespace gen {
46 
50 const double pi = boost::math::constants::pi<double>();
51 
57 inline double radDeg(double angle) { return (angle * 180) / gen::pi; }
58 
61 
64 
73 inline double calcMedian(std::vector<double> *input) {
74  int n = (*input).size(); // Number of elements
75  double median; // Output median value
76 
77  // Sort the vector
78  std::sort((*input).begin(), (*input).end());
79 
80  // Calculate the median
81  // For even values, the median is the average of the two middle values
82  if (n % 2 == 0) {
83  median = 0.5 * ((*input)[n / 2] + (*input)[n / 2 - 1]); // n/2+n/2-1
84  } // median is average of middle values
85  else {
86  median = (*input)[(n + 1) / 2 -
87  1]; // middle value of 7 elements is the 4th element
88  } // if odd, it is the middle value
89 
90  return median;
91 }
92 
93 // Generic function for getting the unwrapped distance
103 inline double
105  int iatom, int jatom) {
107  double r2 = 0.0; // Squared absolute distance
108 
109  // Get x1-x2 etc
110  dr[0] = fabs(yCloud->pts[iatom].x - yCloud->pts[jatom].x);
111  dr[1] = fabs(yCloud->pts[iatom].y - yCloud->pts[jatom].y);
112  dr[2] = fabs(yCloud->pts[iatom].z - yCloud->pts[jatom].z);
113 
114  // Get the squared absolute distance
115  for (int k = 0; k < 3; k++) {
116  // Correct for periodicity
117  dr[k] -= yCloud->box[k] * round(dr[k] / yCloud->box[k]);
118  r2 += pow(dr[k], 2.0);
119  }
120 
121  return sqrt(r2);
122 }
123 
135  molSys::PointCloud<molSys::Point<double>, double> *yCloud, int iatom,
136  std::vector<double> singlePoint) {
138  double r2 = 0.0; // Squared absolute distance
139 
140  // Get x1-x2 etc
141  dr[0] = fabs(yCloud->pts[iatom].x - singlePoint[0]);
142  dr[1] = fabs(yCloud->pts[iatom].y - singlePoint[1]);
143  dr[2] = fabs(yCloud->pts[iatom].z - singlePoint[2]);
144 
145  // Get the squared absolute distance
146  for (int k = 0; k < 3; k++) {
147  // Correct for periodicity
148  dr[k] -= yCloud->box[k] * round(dr[k] / yCloud->box[k]);
149  r2 += pow(dr[k], 2.0);
150  }
151 
152  return sqrt(r2);
153 }
154 
155 // Generic function for getting the distance (no PBCs applied)
165 inline double
167  int jatom) {
169  double r2 = 0.0; // Squared absolute distance
170 
171  // Get x1-x2 etc
172  dr[0] = fabs(yCloud->pts[iatom].x - yCloud->pts[jatom].x);
173  dr[1] = fabs(yCloud->pts[iatom].y - yCloud->pts[jatom].y);
174  dr[2] = fabs(yCloud->pts[iatom].z - yCloud->pts[jatom].z);
175 
176  // Get the squared absolute distance
177  for (int k = 0; k < 3; k++) {
178  r2 += pow(dr[k], 2.0);
179  }
180 
181  return sqrt(r2);
182 }
183 
184 // Generic function for getting the relative coordinates
197  int jatom) {
199  std::array<double, 3> box = {yCloud->box[0], yCloud->box[1], yCloud->box[2]};
200  double r2 = 0.0; // Squared absolute distance
201 
202  // Get x1-x2 etc
203  dr[0] = yCloud->pts[iatom].x - yCloud->pts[jatom].x;
204  dr[1] = yCloud->pts[iatom].y - yCloud->pts[jatom].y;
205  dr[2] = yCloud->pts[iatom].z - yCloud->pts[jatom].z;
206 
207  // Get the relative distance
208  for (int k = 0; k < 3; k++) {
209  //
210  if (dr[k] < -box[k] * 0.5) {
211  dr[k] = dr[k] + box[k];
212  }
213  if (dr[k] >= box[k] * 0.5) {
214  dr[k] = dr[k] - box[k];
215  }
216  }
217 
218  return dr;
219 }
220 
221 // Function for sorting according to atom ID
222 // Comparator for std::sort
231  const molSys::Point<double> &b) {
232  return a.atomID < b.atomID;
233 }
234 
237  std::string outFile);
238 
241  molSys::PointCloud<molSys::Point<double>, double> *yCloud, int iatomIndex,
242  int jatomIndex, double *x_i, double *y_i, double *z_i, double *x_j,
243  double *y_j, double *z_j);
244 
248  std::vector<double> quat2);
249 
256  std::istringstream iss(line);
259  return tokens;
260 }
261 
267  std::istringstream iss(line);
268  std::vector<double> tokens;
269  double number; // Each number being read in from the line
270  while (iss >> number) {
271  tokens.push_back(number);
272  }
273  return tokens;
274 }
275 
281  std::istringstream iss(line);
282  std::vector<int> tokens;
283  int number; // Each number being read in from the line
284  while (iss >> number) {
285  tokens.push_back(number);
286  }
287  return tokens;
288 }
289 
294 inline bool file_exists(const std::string &name) {
295  // Replace by boost function later
296  struct stat buffer;
297  return (stat(name.c_str(), &buffer) == 0);
298 }
299 
310  if (neigh == 0) {
311  return v;
312  }
313  for (int m = 0; m < 2 * l + 1; m++) {
314  v[m] = (1.0 / (double)neigh) * v[m];
315  }
316 
317  return v;
318 }
319 
320 } // namespace gen
321 
322 #endif // __NEIGHBOURS_H_
T c_str(T... args)
double distance(molSys::PointCloud< molSys::Point< double >, double > *yCloud, int iatom, int jatom)
Inline generic function for obtaining the wrapped distance between two particles WITHOUT applying PBC...
Definition: generic.hpp:166
std::vector< std::complex< double > > avgVector(std::vector< std::complex< double >> v, int l, int neigh)
Definition: generic.hpp:309
std::array< double, 3 > relDist(molSys::PointCloud< molSys::Point< double >, double > *yCloud, int iatom, int jatom)
Definition: generic.hpp:196
double eigenVecAngle(std::vector< double > OO, std::vector< double > OH)
Eigen function for getting the angle (in radians) between the O–O and O-H vectors.
Definition: generic.cpp:150
double calcMedian(std::vector< double > *input)
Inline generic function for calculating the median given a vector of the values.
Definition: generic.hpp:73
std::vector< int > tokenizerInt(std::string line)
Function for tokenizing line strings into a vector of ints.
Definition: generic.hpp:280
double periodicDist(molSys::PointCloud< molSys::Point< double >, double > *yCloud, int iatom, int jatom)
Inline generic function for obtaining the unwrapped periodic distance between two particles,...
Definition: generic.hpp:104
double unWrappedDistFromPoint(molSys::PointCloud< molSys::Point< double >, double > *yCloud, int iatom, std::vector< double > singlePoint)
Definition: generic.hpp:134
bool file_exists(const std::string &name)
Function for checking if a file exists or not.
Definition: generic.hpp:294
const double pi
Definition: generic.hpp:50
std::vector< std::string > tokenizer(std::string line)
Function for tokenizing line strings into words (strings) delimited by whitespace....
Definition: generic.hpp:255
double angDistDegQuaternions(std::vector< double > quat1, std::vector< double > quat2)
Definition: generic.cpp:262
int unwrappedCoordShift(molSys::PointCloud< molSys::Point< double >, double > *yCloud, int iatomIndex, int jatomIndex, double *x_i, double *y_i, double *z_i, double *x_j, double *y_j, double *z_j)
Shift particles (unwrapped coordinates)
Definition: generic.cpp:66
double radDeg(double angle)
Definition: generic.hpp:57
bool compareByAtomID(const molSys::Point< double > &a, const molSys::Point< double > &b)
Definition: generic.hpp:230
int prettyPrintYoda(molSys::PointCloud< molSys::Point< double >, double > *yCloud, std::string outFile)
Generic function for printing all the struct information.
Definition: generic.cpp:21
double getAverageWithoutOutliers(std::vector< double > inpVec)
Get the average, after excluding the outliers, using quartiles.
Definition: generic.cpp:164
std::vector< double > tokenizerDouble(std::string line)
Function for tokenizing line strings into a vector of doubles.
Definition: generic.hpp:266
The main molecular system handler.
Small generic functions that are shared by all namespaces.
Definition: generic.hpp:45
T push_back(T... args)
T sort(T... args)
This contains a collection of points; contains information for a particular frame.
Definition: mol_sys.hpp:166
This contains per-particle information.
Definition: mol_sys.hpp:145