moldyn-loader.h 6.15 KB
Newer Older
Tobias WEBER's avatar
Tobias WEBER committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
 * loads atom dynamics file
 * @author Tobias Weber <tweber@ill.fr>
 * @date Dec-2019
 * @license GPLv3, see 'LICENSE' file
 */

#ifndef __MOLDYN_H__
#define __MOLDYN_H__


#include <fstream>
#include <iostream>
#include <vector>
#include <string>

Tobias WEBER's avatar
Tobias WEBER committed
17
#include "libs/file.h"
Tobias WEBER's avatar
Tobias WEBER committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "libs/str.h"


template<class t_real, class t_vec>
class MolFrame
{
	public:
		MolFrame()
		{
			m_config.reserve(128);
		}


		void AddAtomConfig(std::vector<t_vec>&& config)
		{
			m_config.emplace_back(std::move(config));
		}

		void AddAtomConfig(const std::vector<t_vec>& config)
		{
			m_config.push_back(config);
		}

Tobias WEBER's avatar
Tobias WEBER committed
41
42
43
44
45
46
47
48
49
50
51

		std::size_t GetNumAtoms() const
		{
			return m_config.size();
		}

		const std::vector<t_vec>& GetCoords(std::size_t atomidx) const
		{
			return m_config[atomidx];
		}

Tobias WEBER's avatar
Tobias WEBER committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
	private:
		// atoms -> coordinates
		std::vector<std::vector<t_vec>> m_config;
};



template<class t_real, class t_vec>
class MolDyn
{
	public:
		MolDyn() : m_baseA(3), m_baseB(3), m_baseC(3)
		{
			m_frames.reserve(16384);
		}

		void SetBaseA(t_real x, t_real y, t_real z)
		{
			m_baseA[0] = x;
			m_baseA[1] = y;
			m_baseA[2] = z;
		}

		void SetBaseB(t_real x, t_real y, t_real z)
		{
			m_baseB[0] = x;
			m_baseB[1] = y;
			m_baseB[2] = z;
		}

		void SetBaseC(t_real x, t_real y, t_real z)
		{
			m_baseC[0] = x;
			m_baseC[1] = y;
			m_baseC[2] = z;
		}


Tobias WEBER's avatar
Tobias WEBER committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
		const t_vec& GetBaseA() const { return m_baseA; }
		const t_vec& GetBaseB() const { return m_baseB; }
		const t_vec& GetBaseC() const { return m_baseC; }


		std::size_t GetFrameCount() const
		{ return m_frames.size(); }
	
		const MolFrame<t_real, t_vec>& GetFrame(std::size_t frame) const
		{ return m_frames[frame]; }


		const std::string& GetAtomName(std::size_t idx) const
		{ return m_vecAtoms[idx]; }


Tobias WEBER's avatar
Tobias WEBER committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
		void AddAtomType(const std::string& name, unsigned int number)
		{
			m_vecAtoms.push_back(name);
			m_vecAtomNums.push_back(number);
		}


		void AddFrame(MolFrame<t_real, t_vec>&& frame)
		{
			m_frames.emplace_back(std::move(frame));
		}

		void AddFrame(const MolFrame<t_real, t_vec>& frame)
		{
			m_frames.push_back(frame);
		}



		bool LoadFile(const std::string& filename, unsigned int frameskip = 0)
		{
			const std::string strDelim{" \t"};

			std::ifstream ifstr{filename};
			if(!ifstr)
			{
				std::cerr << "Cannot open \"" << filename << "\".";
				return 0;
			}

Tobias WEBER's avatar
Tobias WEBER committed
136
137
138
139
			// clear old data
			Clear();


Tobias WEBER's avatar
Tobias WEBER committed
140
141
142
			std::size_t filesize = tl2::get_file_size(ifstr);
			std::cout << "File size: " << filesize / 1024 / 1024 << " MB." << std::endl;

Tobias WEBER's avatar
Tobias WEBER committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
			std::string strSys;
			std::getline(ifstr, strSys);
			tl2::trim(strSys);
			std::cout << "System: " << strSys << std::endl;



			std::string strScale;
			std::getline(ifstr, strScale);
			t_real scale = tl2::str_to_var<t_real>(strScale);
			std::cout << "scale: " << scale << std::endl;



			std::string strVecs1;
			std::string strVecs2;
			std::string strVecs3;
			std::getline(ifstr, strVecs1);
			std::getline(ifstr, strVecs2);
			std::getline(ifstr, strVecs3);

			std::vector<t_real> _vecBase1, _vecBase2, _vecBase3;
			tl2::get_tokens<t_real>(strVecs1, strDelim, _vecBase1);
			tl2::get_tokens<t_real>(strVecs2, strDelim, _vecBase2);
			tl2::get_tokens<t_real>(strVecs3, strDelim, _vecBase3);

			if(_vecBase1.size()!=3 || _vecBase2.size()!=3 || _vecBase3.size()!=3)
			{
				std::cerr << "Invalid base vectors." << std::endl;
				return 0;
			}


			SetBaseA(_vecBase1[0]*scale, _vecBase2[0]*scale, _vecBase3[0]*scale);
			SetBaseB(_vecBase1[1]*scale, _vecBase2[1]*scale, _vecBase3[1]*scale);
			SetBaseC(_vecBase1[2]*scale, _vecBase2[2]*scale, _vecBase3[2]*scale);


			std::string strAtoms;
			std::string strAtomNums;
			std::getline(ifstr, strAtoms);
			std::getline(ifstr, strAtomNums);
			tl2::get_tokens<std::string>(strAtoms, strDelim, m_vecAtoms);
			tl2::get_tokens<unsigned int>(strAtomNums, strDelim, m_vecAtomNums);

			if(m_vecAtoms.size() != m_vecAtomNums.size())
			{
				std::cerr << "Atom size mismatch." << std::endl;
				return 0;
			}

			for(std::size_t i=0; i<m_vecAtoms.size(); ++i)
			{
				std::cout << m_vecAtomNums[i] << " " << m_vecAtoms[i] << " atoms." << std::endl;
			}



			std::size_t iNumConfigs = 0;
Tobias WEBER's avatar
Tobias WEBER committed
202
			t_real percentage = 0;
Tobias WEBER's avatar
Tobias WEBER committed
203
204
205
206
207
208
209
210
211
212
213
			while(true)
			{
				std::string strConfig;
				std::getline(ifstr, strConfig);
				tl2::trim(strConfig);

				if(ifstr.eof())
					break;

				if(frameskip || iNumConfigs % 100)
				{
Tobias WEBER's avatar
Tobias WEBER committed
214
215
					std::cout << "\rReading " << strConfig << ". "
						<< unsigned{percentage} << " %.                ";
Tobias WEBER's avatar
Tobias WEBER committed
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
					std::cout.flush();
				}


				MolFrame<t_real, t_vec> frame;
				for(std::size_t iAtomType=0; iAtomType<m_vecAtoms.size(); ++iAtomType)
				{
					std::vector<t_vec> atomconf;

					for(std::size_t iAtom=0; iAtom<m_vecAtomNums[iAtomType]; ++iAtom)
					{
						std::string strCoords;
						std::getline(ifstr, strCoords);

						t_vec vecCoords;
						vecCoords.reserve(3);
						tl2::get_tokens<t_real>(strCoords, strDelim, vecCoords);


						if(vecCoords.size() != 3)
						{
							std::cerr << "Invalid coordinate." << std::endl;
							return 0;
						}

Tobias WEBER's avatar
Tobias WEBER committed
241
242
243
244
245
						// center cell (in rlu)
						vecCoords[0] -= 0.5;
						vecCoords[1] -= 0.5;
						vecCoords[2] -= 0.5;

Tobias WEBER's avatar
Tobias WEBER committed
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
						atomconf.emplace_back(std::move(vecCoords));
					}

					frame.AddAtomConfig(std::move(atomconf));
				}

				AddFrame(std::move(frame));
				++iNumConfigs;


				// skip frames
				for(unsigned int skipped=0; skipped<frameskip; ++skipped)
				{
					std::string strTmp;
					std::getline(ifstr, strTmp);
					//std::cout << "Skipping " << strTmp << "..." << "        ";

					for(std::size_t iAtomType=0; iAtomType<m_vecAtoms.size(); ++iAtomType)
					{
						for(std::size_t iAtom=0; iAtom<m_vecAtomNums[iAtomType]; ++iAtom)
							std::getline(ifstr, strTmp);
					}
				}

Tobias WEBER's avatar
Tobias WEBER committed
270
271
272
273
274
275

				std::size_t filepos = tl2::get_file_pos(ifstr);
				percentage = t_real{filepos*100} / t_real{filesize};
			}
	
			std::cout << "\rRead " << iNumConfigs << " configurations. " << "                        " << std::endl;
Tobias WEBER's avatar
Tobias WEBER committed
276
277
278
279
			return 1;
		}


Tobias WEBER's avatar
Tobias WEBER committed
280
281
282
283
284
285
286
287
		void Clear()
		{
			m_vecAtoms.clear();
			m_vecAtomNums.clear();
			m_frames.clear();
		}


Tobias WEBER's avatar
Tobias WEBER committed
288
289
290
291
292
293
294
295
296
297
298
299
300
	private:
		t_vec m_baseA;
		t_vec m_baseB;
		t_vec m_baseC;

		std::vector<std::string> m_vecAtoms;
		std::vector<unsigned int> m_vecAtomNums;

		std::vector<MolFrame<t_real, t_vec>> m_frames;
};


#endif