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
181 std::string getPath() { return path.string(); }
182
183 bool updated = false;
184
185protected:
186
187 std::filesystem::path path;
189 std::ifstream filestream;
197
198 glm::vec3 cellA;
199 glm::vec3 cellB;
200 glm::vec3 cellC;
201
202 bool cacheComplete = false;
203
204 std::map<uint64_t, uint64_t> framePositions;
205
206
207
208 virtual void beginning()
209 {
210 filestream.seekg(std::ios::beg);
211 }
212
213 virtual void getFrame() = 0;
214
215 virtual void getCell() = 0;
216
218 {
219 if (currentFrame == frames-1) { return; }
220 for (uint64_t a = 0; a < linesPerFrame; a++)
221 {
223 }
224 }
225
227 {
228 if (currentFrame == frames-1) { return; }
229 for (uint64_t f = 0; f < count; f++)
230 {
231 skipFrame();
232 }
233 }
234
235 void skipLine(std::ifstream & in)
236 {
237 in.ignore
238 (
239 std::numeric_limits<std::streamsize>::max(),
240 '\n'
241 );
242 }
243
244 virtual void initialise() = 0;
245
247 {
248 if (blockingReads) { cachePositions(); return; }
249 // Non-blocking read of latter frame positions.
250 std::thread io = std::thread
251 (
253 this
254 );
255 io.detach();
256 }
257
259 {
260 cacheComplete = false;
261 std::ifstream scanFileStream(path);
263 uint64_t f = 1;
264 for (uint64_t a = 0; a < linesPerFrame; a++)
265 {
267 }
268 while (scanFileStream.peek() != EOF)
269 {
271 f++;
272 frames = f;
273 for (uint64_t a = 0; a < linesPerFrame; a++)
274 {
276 }
277 }
278 cacheComplete = true;
279 }
280
282 {
283 filestream.seekg(std::ios::beg);
284 linesInFile = 0;
285 std::string line;
286 // Count lines with content, not by POSIX \n.
287 while (std::getline(filestream, line))
288 {
289 linesInFile++;
290 }
291 filestream.clear();
292 filestream.seekg(std::ios::beg);
293 }
294
296 (
297 std::stringstream & ss,
298 std::string lastInput,
299 std::string context
300 )
301 {
302 if (ss.fail())
303 {
304 std::stringstream message;
305 message << "File "
306 << path.c_str()
307 << " failed to read line"
308 << "\n Line reads: \""
309 << lastInput
310 << "\""
311 << "\n Context: "+context;
312 throw std::runtime_error
313 (
314 message.str()
315 );
316 }
317 }
318};
319
320#endif /* STRUCTURE_H */
Specification for the structure file interface.
Definition structure.h:26
uint64_t atomsRead
Definition structure.h:196
void cachePositions()
Definition structure.h:258
glm::vec3 cellA
Definition structure.h:198
virtual void initialise()=0
glm::vec3 cellB
Definition structure.h:199
uint64_t framePosition() const
Get the current frame index.
Definition structure.h:108
virtual void beginning()
Definition structure.h:208
bool blockingReads
Definition structure.h:188
virtual void getFrame()=0
virtual void skipFrames(uint64_t count)
Definition structure.h:226
bool cacheComplete
Definition structure.h:202
bool frameReadComplete() const
If the frame has been fully read into atoms.
Definition structure.h:141
uint64_t currentFrame
Definition structure.h:194
glm::vec3 getCellC() const
Get the c cell vector.
Definition structure.h:168
uint64_t frames
Definition structure.h:191
glm::vec3 getCellB() const
Get the b cell vector.
Definition structure.h:161
void skipFrame()
Definition structure.h:217
std::vector< Atom > atoms
The Atoms read in the current frame.
Definition structure.h:147
std::string getPath()
Get the Path of the structure.
Definition structure.h:181
virtual void getCell()=0
uint64_t timeStep
Definition structure.h:193
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:246
bool framePositionsLoaded() const
Check if frames start positions have been loaded.
Definition structure.h:126
std::ifstream filestream
Definition structure.h:189
bool updated
Definition structure.h:183
Structure(std::filesystem::path path, bool blocking=false)
Definition structure.h:29
uint64_t linesPerFrame
Definition structure.h:192
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:187
uint64_t natoms
Definition structure.h:190
void skipLine(std::ifstream &in)
Definition structure.h:235
glm::vec3 cellC
Definition structure.h:200
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:296
uint64_t linesInFile
Definition structure.h:195
virtual uint64_t atomCount() const
Get the number of atoms in the file.
Definition structure.h:50
void countContentLinesInFile()
Definition structure.h:281
std::map< uint64_t, uint64_t > framePositions
Definition structure.h:204
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