SSAGES  0.1
A MetaDynamics Package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Groups Pages
Public Member Functions | Static Public Member Functions | List of all members
SSAGES::ReadFile Class Reference

Utility class to read file. More...

#include <ReadFile.h>

Public Member Functions

 ReadFile ()
 Constructor.
 
 ~ReadFile ()
 Deconstructor.
 

Static Public Member Functions

static std::vector< std::array
< double, 4 > > 
ReadXYZ (std::string FileName)
 Read xyz file. More...
 

Detailed Description

Utility class to read file.

Simple utility class to read in a trajectory file.

Supported file types:

Definition at line 44 of file ReadFile.h.

Member Function Documentation

static std::vector<std::array<double,4> > SSAGES::ReadFile::ReadXYZ ( std::string  FileName)
inlinestatic

Read xyz file.

Parameters
FileNameName of xyz file to read in.
Returns
Vector containing information stored in file.

Read in a xyz file. The information will be returned as a vector of 4-element arrays. Each array corresponds to one atom and stores the following information: atom-ID, x-coordinate, y-coordinate, z-coordinate.

Definition at line 70 of file ReadFile.h.

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  }

The documentation for this class was generated from the following file: