summaryrefslogtreecommitdiff
path: root/cad/plugins/HDF5_SimResults/include/Nanorex/HDF5_SimResults.h
blob: affde5b5910181387995e04cd0f7f293064afcfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
136
137
138
139
140
141
142
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389

// Copyright 2006-2007 Nanorex, Inc.  See LICENSE file for details. 

#ifndef NE1_HDF5_SIMRESULTS_H
#define NE1_HDF5_SIMRESULTS_H

#ifdef WIN32
#     ifdef _MSC_VER
#             pragma warning(disable:4786)
#     endif
#endif

#include <stdlib.h>
#include <map>

#include "hdf5.h"

#include "Nanorex/SimResultsDataStore.h"

#define HDF5_SIM_RESULT_FILENAME	"sim_results.h5"
#define GROUP_NAME_SIZE_HINT		64

#define USE_CHUNKING				1
//#define USE_SHUFFLING				1
//#define USE_COMPRESSION			1
#define USE_SHUFFLING				0
#define USE_COMPRESSION				0
#define COMPRESSION_LVL				6

#define TOTAL_ENERGY_MSRMT			0
#define IDEAL_TEMPERATURE_MSRMT		1
#define PRESSURE_MSRMT				2

namespace Nanorex {


/* CLASS: FrameSetInfo */
/**
 * Used internally to HDF5_SimResults.
 */
class FrameSetInfo {
	public:
		FrameSetInfo() {
			currentFrameIndex = 0;
			timestampsDatasetId = 0;
			atomIdsDatasetId = 0;
			atomicNumbersDatasetId = 0;
			atomPositionsDatasetId = 0;
			atomVelocitiesDatasetId = 0;
			bondsDatasetId = 0;
			measurementsDatasetId = 0;
		}
	
		int currentFrameIndex;
		hid_t timestampsDatasetId;
		hid_t atomIdsDatasetId;
		hid_t atomicNumbersDatasetId;
		hid_t atomPositionsDatasetId;
		hid_t atomVelocitiesDatasetId;
		hid_t bondsDatasetId;
		hid_t measurementsDatasetId;
};
	

/* CLASS: HDF5_SimResults */
/**
 * HDF5 implementation of SimResultsDataStore.
 *
 * Hierarchy:
 \code
	/
		Name, Description, Notes - attributes
	
		Parameters/ - attributes
			Ints/
				key=int-attribute
			Floats/
				float-attribute
			Strings/
				string-attribute
	
		InputFilePaths/
			key = filePath attributes
	
		Results/ - attributes
			RunResult
			StartTime
			CPU_RunningTime, WallRunningTime
			key=int-attribute, float-attribute, string-attribute
	
			ExtensionData/
				name/
					key=int-attribute, float-attribute
					key=int-array-dataset, float-array-dataset
	
			FrameSets/
				name/
					AggregationMode, StepsPerFrame - attributes
					Timestamps - dataset
					AtomIds - dataset, AtomicNumbers - dataset
					AtomPositions, AtomVelocities - dataset
					Bonds - dataset
					Measurements - dataset
\endcode
 */
class HDF5_SimResults : public SimResultsDataStore {
	public:
		HDF5_SimResults();
		~HDF5_SimResults();
		
		int openDataStore(const char* directory, std::string& message);
		void synchronize();
		void flush();
		
		int getName(std::string& name) const;
		int setName(const std::string& name, std::string& message);
		
		int getDescription(std::string& description) const;
		int setDescription(const std::string& description,
						   std::string& message);
		
		int getNotes(std::string& notes) const;
		int setNotes(const std::string& notes, std::string& message);
		
		std::vector<std::string> getIntParameterKeys() const;
		int getIntParameter(const std::string& key, int& value) const;
		int setIntParameter(const std::string& key, int value,
							std::string& message);
		
		std::vector<std::string> getFloatParameterKeys() const;
		int getFloatParameter(const std::string& key, float& value) const;
		int setFloatParameter(const std::string& key, float value,
							  std::string& message);
		
		std::vector<std::string> getStringParameterKeys() const;
		int getStringParameter(const std::string& key, std::string& value)
			const;
		int setStringParameter(const std::string& key, const std::string& value,
							   std::string& message);

		std::vector<std::string> getFilePathKeys() const;
		int getFilePath(const char* key, std::string& filePath) const;
		int setFilePath(const char* key, const char* filePath,
						std::string& message);
		
		
		int getRunResult(int& result, std::string& failureDescription) const;
		int setRunResult(const int& code, const char* failureDescription,
						 std::string& message);
		
		int getStartTime(time_t& startTime) const;
		int setStartTime(const time_t& startTime, std::string& message);
		
		int getCPU_RunningTime(float& cpuRunningTime) const;
		int setCPU_RunningTime(const float& cpuRunningTime,
							   std::string& message);
		
		int getWallRunningTime(float& wallRunningTime) const;
		int setWallRunningTime(const float& wallRunningTime,
							   std::string& message);

		std::vector<std::string> getIntResultKeys() const;
		int getIntResult(const std::string& key, int& value) const;
		int setIntResult(const std::string& key, int value,
							std::string& message);
		
		std::vector<std::string> getFloatResultKeys() const;
		int getFloatResult(const std::string& key, float& value) const;
		int setFloatResult(const std::string& key, float value,
							  std::string& message);
		
		std::vector<std::string> getStringResultKeys() const;
		int getStringResult(const std::string& key, std::string& value)
			const;
		int setStringResult(const std::string& key, const std::string& value,
							   std::string& message);

		std::vector<std::string> getFrameSetNames() const;
		int addFrameSet(const char* name, std::string& message);
		
		int getAggregationMode(const char* frameSetName, int& mode) const;
		int setAggregationMode(const char* frameSetName, const int& mode,
							   std::string& message);
		
		int getStepsPerFrame(const char* frameSetName,
							 int& stepsPerFrame) const;
		int setStepsPerFrame(const char* frameSetName,
							 const int& stepsPerFrame,
							 std::string& message);
		
		
		void getFrameCount(const char* frameSetName, int& frameCount);
		int getFrameTimes(const char* frameSetName, float* frameTimes,
						  std::string& message);
		int getFrameTime(const char* frameSetName, const int& frameIndex,
						 float& time, std::string& message);
		int addFrame(const char* frameSetName, const float& time,
					 int& frameIndex, std::string& message);
		
		void getFrameAtomIdsCount(const char* frameSetName,
								  unsigned int& atomIdsCount);
		int getFrameAtomIds(const char* frameSetName,
							unsigned int* atomIds,
							std::string& message);
		int setFrameAtomIds(const char* frameSetName,
							const unsigned int* atomIds,
							const unsigned int& atomIdsCount,
							std::string& message);
		
		int getFrameAtomicNumbers(const char* frameSetName,
								  unsigned int* atomicNumbers,
								  std::string& message);
		int setFrameAtomicNumbers(const char* frameSetName,
								  const unsigned int* atomicNumbers,
								  const unsigned int& atomicNumbersCount,
								  std::string& message);
		
		int getFrameAtomPositions(const char* frameSetName,
								  const int& frameIndex,
								  const unsigned int& atomCount,
								  float* positions,
								  std::string& message);
		int setFrameAtomPositions(const char* frameSetName,
								  const int& frameIndex,
								  const float* positions,
								  const unsigned int& atomCount,
								  std::string& message);
		
		int getFrameAtomVelocities(const char* frameSetName,
								   const int& frameIndex,
								   const unsigned int& atomCount,
								   float* velocities,
								   std::string& message);
		int setFrameAtomVelocities(const char* frameSetName,
								   const int& frameIndex,
								   const float* velocities,
								   const unsigned int& atomCount,
								   std::string& message);
		
		void getFrameBondsCount(const char* frameSetName,
								const int& frameIndex,
								unsigned int& bondCount);
		int getFrameBonds(const char* frameSetName,
						  const int& frameIndex,
						  void* bonds,
						  std::string& message);
		int setFrameBonds(const char* frameSetName,
						  const int& frameIndex,
						  const void* bonds,
						  const unsigned int& bondCount,
						  std::string& message);
		
		int getFrameTotalEnergy(const char* frameSetName,
								const int& frameIndex,
								float& totalEnergy,
								std::string& message);
		int setFrameTotalEnergy(const char* frameSetName,
								const int& frameIndex,
								const float& totalEnergy,
								std::string& message);
		
		int getFrameIdealTemperature(const char* frameSetName,
									 const int& frameIndex,
									 float& idealTemperature,
									 std::string& message);
		int setFrameIdealTemperature(const char* frameSetName,
									 const int& frameIndex,
									 const float& idealTemperature,
									 std::string& message);

		int getFramePressure(const char* frameSetName,
							 const int& frameIndex,
							 float& pressure,
							 std::string& message);
		int setFramePressure(const char* frameSetName,
							 const int& frameIndex,
							 const float& pressure,
							 std::string& message);
		
	private:
		// HDF5 type identifiers
		hid_t bondTypeId;
		hid_t bondsVariableLengthId;
		
		hid_t fileId;	// HDF5 file identifier
		
		std::string dataStoreDirectory;
		std::map<std::string, FrameSetInfo> frameSetInfoMap;
		
		void closeDatasets();
		void openDatasets();
		
		
		int checkFrameExistence(const char* frameSetName,
								const int& frameIndex,
								std::string& message);
		int checkFrameSetDatasetExistence(const char* frameSetName,
										  hid_t& datasetId,
										  const char* datasetName,
										  std::string& message);
		int checkTimestampsExistence(const char* frameSetName,
									 std::string& message);
		int checkFrameSetExistence(const char* frameSetName,
								   std::string& message);
		int createMeasurementsDataset(const char* frameSetName,
									  hid_t& datasetId,
									  std::string& message);			
		int createBondsDataset(const char* frameSetName,
							   const unsigned int& bondCount,
							   hid_t& datasetId,
							   std::string& message);
		int create3D_AtomFloatsDataset(const char* frameSetName,
									   const char* dataSetName,
									   const unsigned int& atomCount,
									   hid_t& datasetId,
									   std::string& message);
		int createTimestampsDataset(const char* frameSetName, hid_t& datasetId,
									std::string& message);
		int writeMeasurement(const char* frameSetName,
							 const int& frameIndex,
							 const int& measurementIndex,
							 const float& value,
							 const hid_t& datasetId,
							 std::string& message);
		int writeBonds(const int& frame, const unsigned int& bondCount,
					   const void* bonds, hid_t datasetId,
					   std::string& message);
		int writeAtomUInts(const char* frameSetName,
						   const char* dataSetName,
						   const unsigned int* atomUInts,
						   const unsigned int& atomUIntsCount,
						   hid_t& datasetId,
						   std::string& message);
		int write3SpaceAtomFloats(const int& frame,
								  const unsigned int& atomCount,
								  const float* data,
								  hid_t datasetId,
								  std::string& message);
		int writeTimestamp(const int& frame, const float& time,
						   hid_t datasetId,
						   std::string& message);
		int readMeasurement(const char* frameSetName,
							const int& frameIndex,
							const int& measurementIndex,
							const hid_t& datasetId,
							float& value,
							std::string& message);
		int read3SpaceAtomFloats(const int& frame,
								 const unsigned int& atomCount,
								 float* data,
								 hid_t datasetId,
								 std::string& message);
		int readTimestamp(const int& frame, float& time, hid_t datasetId,
						  std::string& message) const;
			
		int getStringAttribute(const std::string& groupName,
							   const std::string& attributeName,
							   std::string& attributeValue) const;
		int setStringAttribute(const std::string& groupName,
							   const std::string& attributeName,
							   const std::string& value,
							   std::string& message);
		int getIntAttribute(const std::string& groupName,
							const std::string& attributeName,
							int& attributeValue) const;
		int setIntAttribute(const std::string& groupName,
							const std::string& attributeName,
							const int& value,
							std::string& message);
		int getFloatAttribute(const std::string& groupName,
							  const std::string& attributeName,
							  float& attributeValue) const;
		int setFloatAttribute(const std::string& groupName,
							  const std::string& attributeName,
							  const float& value,
							  std::string& message);
		int getTimeAttribute(const std::string& groupName,
							 const std::string& attributeName,
							 time_t& attributeValue) const;
		int setTimeAttribute(const std::string& groupName,
							 const std::string& attributeName,
							 const time_t& value,
							 std::string& message);
		std::vector<std::string> getGroupKeys(const std::string& group) const;
};

} // Nanorex::

#endif