SSAGES  0.1
A MetaDynamics Package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Groups Pages
ReadFile.h
1 
20 #pragma once
21 
22 #include <math.h>
23 #include <array>
24 #include <string>
25 #include <vector>
26 #include <iostream>
27 #include <fstream>
28 
29 // Temp read file class. This class will be integrated/changed
30 // into a larger read file class that uses
31 // readxyz as a function. Can be expanded to include other
32 // file types.
33 
34 namespace SSAGES
35 {
37 
44  class ReadFile
45  {
46  public:
47 
50  {
51 
52  }
53 
56  {
57 
58  }
59 
61 
70  static std::vector<std::array<double,4>> ReadXYZ(std::string FileName)
71  {
72  std::vector<std::array<double,4>> refcoord;
73  int numatoms = 0;
74  std::string comments = "";
75  std::ifstream infile;
76  infile.open(FileName, std::ios::in);
77  if(!infile.is_open())
78  throw std::runtime_error("File " + FileName + " does not exist.");
79 
80  std::string ignore;
81 
82  std::getline(infile, ignore); // Get number of atoms
83 
84  numatoms = std::atoi(ignore.c_str());
85  if(numatoms <= 0)
86  throw std::runtime_error("Must be more than 0 atoms or invalid number atoms defined.");
87 
88  refcoord.resize(numatoms);
89 
90  std::getline(infile, comments); // Get comments
91 
92  int i = 0;
93  std::string line;
94 
95  while (i < numatoms)
96  {
97  std::getline(infile,line);
98  std::istringstream iss(line);
99  if (!(iss >> refcoord[i][0] >> refcoord[i][1] >> refcoord[i][2] >> refcoord[i][3]))
100  throw std::runtime_error("Bad file format for " + FileName + " for atom " + std::to_string(i+1));
101  i++;
102  }
103 
104  if(std::getline(infile, line) && !line.empty())
105  throw std::runtime_error("Bad end line, possibly too many atoms defined.");
106 
107  if(i != numatoms)
108  throw std::runtime_error("Number atoms specified does not match number of atoms defined");
109 
110  return refcoord;
111  }
112  };
113 }
ReadFile()
Constructor.
Definition: ReadFile.h:49
Utility class to read file.
Definition: ReadFile.h:44
static std::vector< std::array< double, 4 > > ReadXYZ(std::string FileName)
Read xyz file.
Definition: ReadFile.h:70
~ReadFile()
Deconstructor.
Definition: ReadFile.h:55