SimpleFastOpenAtomicVisualiser
Loading...
Searching...
No Matches
xyz.h
Go to the documentation of this file.
1#ifndef XYZ_H
2#define XYZ_H
3
4#include <filesystem>
5#include <fstream>
6#include <sstream>
7#include <string>
8#include <vector>
9#include <algorithm>
10
11#include <structure.h>
12#include <util.h>
13
21bool ostensiblyXYZLike(std::filesystem::path path)
22{
23 std::string ext = path.extension().string();
24 std::transform
25 (
26 ext.begin(),
27 ext.end(),
28 ext.begin(),
29 [](unsigned char c){ return std::tolower(c); }
30 );
31 if (ext.rfind(".xyz", 0) == 0 || ext.rfind(".extxyz", 0) == 0)
32 {
33 return true;
34 }
35 return false;
36}
37
49class XYZ : public Structure
50{
51public:
52
59 XYZ(std::filesystem::path path, bool blocking = false)
61 {
62 // Check first frame/meta data format.
63 initialise();
65 }
66
67private:
68
69 std::map<std::string, std::string> metaData;
70
71 void initialise()
72 {
73 beginning();
74 std::string line;
75 std::getline(filestream, line);
76 std::stringstream count(line);
77 count >> natoms;
78 checkRead(count, line, "XYZ readAtomCount");
79 parseMetaData();
80 getCell();
81 beginning();
82 framePositions[0] = filestream.tellg();
83 frames = 1;
85 atoms.resize(natoms);
86 }
87
88 void parseMetaData()
89 {
90 beginning();
92 std::string line;
93 std::getline(filestream, line);
94 std::vector<std::string> comment = split(line, std::regex("\\w*="));
95 if (comment.size() > 0)
96 {
97 for (auto s : comment)
98 {
99 auto pos = s.find("=");
100 metaData.insert(std::pair(s.substr(0, pos), s.substr(pos+1)));
101 }
102 }
103 else
104 {
105 metaData["comment"] = line;
106 }
107 }
108
109 void getAtoms()
110 {
111 std::string line;
112 std::stringstream ss;
113 std::getline(filestream, line);
114 std::getline(filestream, line);
115 for (uint64_t a = 0; a < atoms.size(); a++)
116 {
117 std::getline(filestream, line);
118 std::string symbol;
119 Atom atom;
120 ss = std::stringstream(line);
121 ss >> symbol
122 >> atom.position.x
123 >> atom.position.y
124 >> atom.position.z;
125 checkRead(ss, line, "XYZ reading atom "+std::to_string(a));
126 atom.symbol = stringSymbolToElement(symbol);
127 atom.scale = ELEMENT_RADIUS.at(atom.symbol);
128 atom.colour = colourMap.at(atom.symbol);
129 atoms[a] = atom;
130 atomsRead = a+1;
131 }
132 }
133
134 void getFrame()
135 {
136 atomsRead = 0;
137 if (blockingReads) { getAtoms(); return; }
138 std::thread io = std::thread
139 (
140 &XYZ::getAtoms,
141 this
142 );
143 io.detach();
144 }
145
146 void getCell()
147 {
148 if (metaData.find("Lattice") != metaData.end())
149 {
150 auto values = split(metaData["Lattice"], std::regex(" "));
151 for (auto & v : values)
152 {
153 v = std::regex_replace(v, std::regex("[^0-9\\.]"), "");
154 }
155 if (values.size() == 9)
156 {
157 cellA = glm::vec3(std::stof(values[0]), std::stof(values[1]), std::stof(values[2]));
158 cellB = glm::vec3(std::stof(values[3]), std::stof(values[4]), std::stof(values[5]));
159 cellC = glm::vec3(std::stof(values[6]), std::stof(values[7]), std::stof(values[8]));
160 }
161 }
162 }
163};
164
165#endif /* XYZ_H */
An atom structure.
Definition atom.h:20
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
virtual void beginning()
Definition structure.h:199
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
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
Read XYZ and EXTXYZ files.
Definition xyz.h:50
XYZ(std::filesystem::path path, bool blocking=false)
Construct a new XYZ object to read from path.
Definition xyz.h:59
glm::vec< L, float, glm::qualifier::highp > vec
Definition commandLine.h:214
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
std::vector< std::string > split(std::string str, std::regex delim)
Split a std::string by a std::regex token.
Definition util.h:108
bool ostensiblyXYZLike(std::filesystem::path path)
Check if a path is XYZ'y.
Definition xyz.h:21