SSAGES  0.9.3
Software Suite for Advanced General Ensemble Simulations
ReadFile.h
1 
20 #pragma once
21 
22 #include <array>
23 #include <fstream>
24 #include <string>
25 #include <vector>
26 
27 // Temp read file class. This class will be integrated/changed
28 // into a larger read file class that uses
29 // readxyz as a function. Can be expanded to include other
30 // file types.
31 
32 namespace SSAGES
33 {
35 
42  class ReadFile
43  {
44  public:
45 
48  {
49  }
50 
53  {
54  }
55 
57 
66  static std::vector<std::array<double,4>> ReadXYZ(std::string FileName)
67  {
68  std::vector<std::array<double,4>> refcoord;
69  int numatoms = 0;
70  std::string comments = "";
71  std::ifstream infile;
72  infile.open(FileName, std::ios::in);
73  if(!infile.is_open())
74  {
75  throw std::runtime_error("ReadFile.h: File " + FileName + " does not exist.");
76  }
77 
78  std::string line;
79  std::getline(infile, line); // Get number of atoms
80 
81  try
82  {
83  numatoms = std::stoi(line);
84  }
85  catch (const std::invalid_argument&)
86  {
87  throw std::runtime_error("ReadFile.h: Invalid number of atoms defined in File.");
88  }
89  if(numatoms <= 0)
90  {
91  throw std::runtime_error("ReadFile.h: Must be more than 0 atoms defined in File.");
92  }
93 
94  refcoord.resize(numatoms);
95 
96  std::getline(infile, comments); // Get comments
97 
98  int i = 0;
99  std::array<double,4> row;
100  while(infile >> row[0] >> row[1] >> row[2] >> row[3])
101  {
102  if(i < numatoms) refcoord[i] = row;
103  i++;
104  }
105  if(i != numatoms)
106  {
107  throw std::runtime_error(
108  "ReadFile: Header specified " + std::to_string(numatoms) +
109  " atoms, but read in " + std::to_string(i) + ". " +
110  "Check the format of " + FileName + "."
111  );
112  }
113 
114  return refcoord;
115  }
116  };
117 }
Utility class to read file.
Definition: ReadFile.h:43
~ReadFile()
Deconstructor.
Definition: ReadFile.h:52
ReadFile()
Constructor.
Definition: ReadFile.h:47
static std::vector< std::array< double, 4 > > ReadXYZ(std::string FileName)
Read xyz file.
Definition: ReadFile.h:66