SimpleFastOpenAtomicVisualiser
Loading...
Searching...
No Matches
structure.h
Go to the documentation of this file.
1#ifndef STRUCTURE_H
2#define STRUCTURE_H
3
4#include <filesystem>
5#include <fstream>
6#include <sstream>
7#include <string>
8#include <array>
9#include <algorithm>
10#include <exception>
11
12#include <vendored/jThread/jThread.h>
13
14#include <atom.h>
15
26{
27public:
28
29 Structure(std::filesystem::path path, bool blocking = false)
30 : path(path),
33 natoms(0),
34 frames(0),
36 timeStep(0),
37 currentFrame(0),
38 linesInFile(0),
39 atomsRead(0),
40 cellA(0),
41 cellB(0),
42 cellC(0)
43 {}
44
50 virtual uint64_t atomCount() const { return atoms.size(); }
51
63 virtual void readFrame(uint64_t frame)
64 {
65 if (atoms.size() != natoms) { atoms.resize(natoms); }
66 frame = frame % frames;
67 if (framePositions.find(frame) != framePositions.cend())
68 {
69 filestream.seekg(framePositions[frame]);
70 }
71 else
72 {
73 if (frame == currentFrame)
74 {
75 framePositions[frame] = filestream.tellg();
76 }
77 else if (frame > currentFrame)
78 {
80 framePositions[frame] = filestream.tellg();
81 }
82 else
83 {
84 beginning();
85 if (frame > 0) { skipFrames(frame-1); }
86 framePositions[frame] = filestream.tellg();
87 }
88 }
89
90 getFrame();
91 currentFrame = frame + 1;
92 }
93
94 virtual ~Structure() = default;
95
101 uint64_t frameCount() const { return frames; }
102
109
126 bool framePositionsLoaded() const { return cacheComplete; }
127
134
141 bool frameReadComplete() const { return atomsRead == atoms.size(); }
142
147 std::vector<Atom> atoms;
148
154 glm::vec3 getCellA() const { return cellA; }
155
161 glm::vec3 getCellB() const { return cellB; }
162
168 glm::vec3 getCellC() const { return cellC; }
169
174 std::map<Element, glm::vec4> colourMap = CPK_COLOURS;
175
176protected:
177
178 std::filesystem::path path;
180 std::ifstream filestream;
188
189 glm::vec3 cellA;
190 glm::vec3 cellB;
191 glm::vec3 cellC;
192
193 bool cacheComplete = false;
194
195 std::map<uint64_t, uint64_t> framePositions;
196
197
198
199 virtual void beginning()
200 {
201 filestream.seekg(std::ios::beg);
202 }
203
204 virtual void getFrame() = 0;
205
206 virtual void getCell() = 0;
207
209 {
210 if (currentFrame == frames-1) { return; }
211 for (uint64_t a = 0; a < linesPerFrame; a++)
212 {
214 }
215 }
216
218 {
219 if (currentFrame == frames-1) { return; }
220 for (uint64_t f = 0; f < count; f++)
221 {
222 skipFrame();
223 }
224 }
225
226 void skipLine(std::ifstream & in)
227 {
228 in.ignore
229 (
230 std::numeric_limits<std::streamsize>::max(),
231 '\n'
232 );
233 }
234
235 virtual void initialise() = 0;
236
238 {
239 if (blockingReads) { cachePositions(); return; }
240 // Non-blocking read of latter frame positions.
241 std::thread io = std::thread
242 (
244 this
245 );
246 io.detach();
247 }
248
250 {
251 cacheComplete = false;
252 std::ifstream scanFileStream(path);
254 uint64_t f = 1;
255 for (uint64_t a = 0; a < linesPerFrame; a++)
256 {
258 }
259 while (scanFileStream.peek() != EOF)
260 {
262 f++;
263 frames = f;
264 for (uint64_t a = 0; a < linesPerFrame; a++)
265 {
267 }
268 }
269 cacheComplete = true;
270 }
271
273 {
274 filestream.seekg(std::ios::beg);
275 linesInFile = 0;
276 std::string line;
277 // Count lines with content, not by POSIX \n.
278 while (std::getline(filestream, line))
279 {
280 linesInFile++;
281 }
282 filestream.clear();
283 filestream.seekg(std::ios::beg);
284 }
285
287 (
288 std::stringstream & ss,
289 std::string lastInput,
290 std::string context
291 )
292 {
293 if (ss.fail())
294 {
295 std::stringstream message;
296 message << "File "
297 << path.c_str()
298 << " failed to read line"
299 << "\n Line reads: \""
300 << lastInput
301 << "\""
302 << "\n Context: "+context;
303 throw std::runtime_error
304 (
305 message.str()
306 );
307 }
308 }
309};
310
311#endif /* STRUCTURE_H */
Specification for the structure file interface.
Definition structure.h:26
uint64_t atomsRead
Definition structure.h:187
void cachePositions()
Definition structure.h:249
glm::vec3 cellA
Definition structure.h:189
virtual void initialise()=0
glm::vec3 cellB
Definition structure.h:190
uint64_t framePosition() const
Get the current frame index.
Definition structure.h:108
virtual void beginning()
Definition structure.h:199
bool blockingReads
Definition structure.h:179
virtual void getFrame()=0
virtual void skipFrames(uint64_t count)
Definition structure.h:217
bool cacheComplete
Definition structure.h:193
bool frameReadComplete() const
If the frame has been fully read into atoms.
Definition structure.h:141
uint64_t currentFrame
Definition structure.h:185
glm::vec3 getCellC() const
Get the c cell vector.
Definition structure.h:168
uint64_t frames
Definition structure.h:182
glm::vec3 getCellB() const
Get the b cell vector.
Definition structure.h:161
void skipFrame()
Definition structure.h:208
std::vector< Atom > atoms
The Atoms read in the current frame.
Definition structure.h:147
virtual void getCell()=0
uint64_t timeStep
Definition structure.h:184
std::map< Element, glm::vec4 > colourMap
Colour map from Element to colour.
Definition structure.h:174
glm::vec3 getCellA() const
Get the a cell vector.
Definition structure.h:154
virtual ~Structure()=default
void scanPositions()
Definition structure.h:237
bool framePositionsLoaded() const
Check if frames start positions have been loaded.
Definition structure.h:126
std::ifstream filestream
Definition structure.h:180
Structure(std::filesystem::path path, bool blocking=false)
Definition structure.h:29
uint64_t linesPerFrame
Definition structure.h:183
virtual void readFrame(uint64_t frame)
Read a single frame at position frame, and increment the current frame.
Definition structure.h:63
std::filesystem::path path
Definition structure.h:178
uint64_t natoms
Definition structure.h:181
void skipLine(std::ifstream &in)
Definition structure.h:226
glm::vec3 cellC
Definition structure.h:191
uint64_t frameCount() const
Get the number of frames.
Definition structure.h:101
uint64_t frameReadProgress() const
Progress of the current frame read.
Definition structure.h:133
void checkRead(std::stringstream &ss, std::string lastInput, std::string context)
Definition structure.h:287
uint64_t linesInFile
Definition structure.h:186
virtual uint64_t atomCount() const
Get the number of atoms in the file.
Definition structure.h:50
void countContentLinesInFile()
Definition structure.h:272
std::map< uint64_t, uint64_t > framePositions
Definition structure.h:195
const std::map< Element, glm::vec4 > CPK_COLOURS
Corey–Pauling–Koltun colourings.
Definition colour.h:18
glm::vec< L, float, glm::qualifier::highp > vec
Definition commandLine.h:214