-
Notifications
You must be signed in to change notification settings - Fork 1
/
crdfile.cpp
61 lines (47 loc) · 1.29 KB
/
crdfile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "crdfile.h"
CRDFile::CRDFile (std::string const crdpath, int const c_size, const bool periodic) :
_file((FILE *)NULL), _size(c_size), _dims(), _frame(0), _eof(true), _set(false), _periodic(periodic)
{
// first load up the file given the path
_file = fopen64 (crdpath.c_str(), "r");
if (_file == (FILE *)NULL) {
printf ("Couldn't load the crdfile %s\n", crdpath.c_str());
exit(1);
}
_eof = false;
char str[1000];
fgets(str, 1000, _file); // first frame's header
this->LoadFirst (); // load the first frame of the file
}
CRDFile::~CRDFile () {
fclose(_file);
}
void CRDFile::LoadNext () {
if (!_set) {
_coords.resize(_size, VecR());
_set = true;
}
double x, y, z;
// grab each coordinate vector for each atom until the size of the system (# of atoms) is processed
for (VecR_it_non_const it = _coords.begin(); it != _coords.end(); it++) {
if (fscanf (_file, " %lf %lf %lf", &x, &y, &z) == EOF) {
// if we've reached the end of the file, then let it be known
_eof = true;
}
else {
it->Set(x,y,z);
}
}
if (_periodic) {
// process the next frame's header line (grab the box dimensions)
fscanf (_file, " %lf %lf %lf", &x, &y, &z);
_dims.Set(x,y,z);
}
++_frame;
return;
}
void CRDFile::LoadFirst() {
rewind (_file);
_set = false;
this->LoadNext();
}