SimpleFastOpenAtomicVisualiser
Loading...
Searching...
No Matches
config.h
Go to the documentation of this file.
1#ifndef CONFIG_H
2#define CONFIG_H
3
4#include <filesystem>
5#include <fstream>
6#include <sstream>
7#include <string>
8#include <vector>
9#include <algorithm>
10#include <array>
11
12#include <structure.h>
13
24bool ostensiblyCONFIGLike(std::filesystem::path path)
25{
26 const std::array<std::string, 4> names {"config", "revcon", "cfgmin", "history"};
27 std::string ext = path.extension().string();
28 std::string stem = path.stem().string();
29
30 for (std::string * s : {&ext, &stem})
31 {
32 std::transform
33 (
34 s->begin(),
35 s->end(),
36 s->begin(),
37 [](unsigned char c){ return std::tolower(c); }
38 );
39 }
40
41 for (auto name : names)
42 {
43 if (ext.rfind("."+name, 0) == 0)
44 {
45 return true;
46 }
47 if (stem.rfind(name, 0) == 0)
48 {
49 return true;
50 }
51 }
52 return false;
53}
54
69class CONFIG : public Structure
70{
71public:
72
79 CONFIG(std::filesystem::path path, bool blocking = false)
81 {
82 // Check first frame/meta data format.
83 initialise();
85 }
86
87private:
88
89 bool HISTORY = false;
90 uint64_t metaDataLines;
91 uint64_t linesPerAtom;
92 unsigned levcfg;
93 unsigned imcon;
94
95 void initialise()
96 {
97 filestream.seekg(std::ios::beg);
98 std::string line;
100 std::getline(filestream, line);
101 std::stringstream data(line);
102 timeStep = 0;
103 data >> levcfg >> imcon >> natoms;
104 if (data.fail())
105 {
106 std::stringstream message;
107 message << path.c_str()
108 << " does not have an atom count.\n"
109 << "Please add the atom count after imcon.\n";
110 throw std::runtime_error
111 (
112 message.str()
113 );
114 }
115 else
116 {
117 // Could be a HISTORY or REVCON file.
118 data = std::stringstream(line);
119 data >> levcfg >> imcon >> natoms >> frames;
120 if (extractHistoryStepMetaData())
121 {
122 HISTORY = true;
123 }
124 }
125
126 if (HISTORY){ getCell(); }
127 else
128 {
129 filestream.seekg(std::ios::beg);
132 getCell();
133 }
134
135 if (!HISTORY) { metaDataLines = 2+(imcon != 0 ? 3 : 0); }
136 else { metaDataLines = 2; }
137 linesPerAtom = 2+(levcfg > 0 ? 1 : 0)+(levcfg > 1 ? 1 : 0);
138 linesPerFrame = natoms*linesPerAtom+4;
139 beginning();
140 framePositions[0] = filestream.tellg();
141 frames = 1;
142 atoms.resize(natoms);
143 }
144
145 void beginning()
146 {
147 filestream.seekg(std::ios::beg);
148 for (uint64_t l = 0; l < metaDataLines; l++)
149 {
151 }
152 }
153
154 void getAtoms()
155 {
156 std::string line;
157 std::stringstream ss;
158 if (HISTORY)
159 {
161 getCell();
162 }
163 for (uint64_t a = 0; a < atoms.size(); a++)
164 {
165 std::string symbol;
166 uint64_t index;
167 Atom atom;
168
169 std::getline(filestream, line);
170 ss = std::stringstream(line);
171 ss >> symbol >> index;
172 checkRead(ss, line, "CONFIG reading atom "+std::to_string(a));
173
174 std::getline(filestream, line);
175 ss = std::stringstream(line);
176 ss >> atom.position.x
177 >> atom.position.y
178 >> atom.position.z;
179 checkRead(ss, line, "CONFIG reading atom "+std::to_string(a));
180
181 if (levcfg > 0)
182 {
183 std::getline(filestream, line);
184 ss = std::stringstream(line);
185 ss >> atom.velocity.x
186 >> atom.velocity.y
187 >> atom.velocity.z;
188 checkRead(ss, line, "CONFIG reading atom "+std::to_string(a));
189 }
190 if (levcfg > 1)
191 {
192 std::getline(filestream, line);
193 ss = std::stringstream(line);
194 ss >> atom.force.x
195 >> atom.force.y
196 >> atom.force.z;
197 checkRead(ss, line, "CONFIG reading atom "+std::to_string(a));
198 }
199
200 atom.symbol = stringSymbolToElement(symbol);
201 atom.scale = ELEMENT_RADIUS.at(atom.symbol);
202 atom.colour = colourMap.at(atom.symbol);
203 atoms[a] = atom;
204 atomsRead = a+1;
205 }
206 }
207
208 void getFrame()
209 {
210 atomsRead = 0;
211 if (blockingReads) { getAtoms(); return; }
212 std::thread io = std::thread
213 (
214 &CONFIG::getAtoms,
215 this
216 );
217 io.detach();
218 }
219
220 void getCell()
221 {
222 std::string line;
223 std::stringstream data;
224 std::getline(filestream, line);
225 data = std::stringstream(line);
226 data >> cellA.x >> cellA.y >> cellA.z;
227 checkRead(data, line, "getCell");
228
229 std::getline(filestream, line);
230 data = std::stringstream(line);
231 data >> cellB.x >> cellB.y >> cellB.z;
232 checkRead(data, line, "getCell");
233
234 std::getline(filestream, line);
235 data = std::stringstream(line);
236 data >> cellC.x >> cellC.y >> cellC.z;
237 checkRead(data, line, "getCell");
238 }
239
240 bool extractHistoryStepMetaData()
241 {
242 std::string line;
243 std::stringstream data;
244 std::getline(filestream, line);
245 if (line.rfind("timestep", 0) == 0)
246 {
247 data = std::stringstream(line.substr(9));
248 data >> timeStep;
249 return true;
250 }
251 return false;
252 }
253};
254#endif /* CONFIG_H */
An atom structure.
Definition atom.h:20
Read CONFIG files.
Definition config.h:70
CONFIG(std::filesystem::path path, bool blocking=false)
Construct a new CONFIG object to read from path.
Definition config.h:79
Specification for the structure file interface.
Definition structure.h:26
uint64_t atomsRead
Definition structure.h:187
glm::vec3 cellA
Definition structure.h:189
glm::vec3 cellB
Definition structure.h:190
bool blockingReads
Definition structure.h:179
uint64_t frames
Definition structure.h:182
std::vector< Atom > atoms
The Atoms read in the current frame.
Definition structure.h:147
uint64_t timeStep
Definition structure.h:184
std::map< Element, glm::vec4 > colourMap
Colour map from Element to colour.
Definition structure.h:174
void scanPositions()
Definition structure.h:237
std::ifstream filestream
Definition structure.h:180
uint64_t linesPerFrame
Definition structure.h:183
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
void checkRead(std::stringstream &ss, std::string lastInput, std::string context)
Definition structure.h:287
std::map< uint64_t, uint64_t > framePositions
Definition structure.h:195
glm::vec< L, float, glm::qualifier::highp > vec
Definition commandLine.h:196
bool ostensiblyCONFIGLike(std::filesystem::path path)
Check if a path is CONFIG'y.
Definition config.h:24
Element stringSymbolToElement(const std::string &s)
Map a string symbol to an Element.
Definition element.h:681
const std::map< Element, float > ELEMENT_RADIUS
Map Element to a Van der Waals radius in Angstroms.
Definition element.h:461