votca 2024.2-dev
Loading...
Searching...
No Matches
h5mdtrajectoryreader.h
Go to the documentation of this file.
1/*
2 * Copyright 2009-2020 The VOTCA Development Team (http://www.votca.org)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18#ifndef VOTCA_CSG_H5MDTRAJECTORYREADER_PRIVATE_H
19#define VOTCA_CSG_H5MDTRAJECTORYREADER_PRIVATE_H
20
21// Standard includes
22#include <cstdio>
23#include <fstream>
24#include <iostream>
25#include <string>
26
27// Third party includes
28#include <hdf5.h>
29
30#include <boost/regex.hpp>
31
32// Local VOTCA includes
35
36namespace votca { // NOLINT
37namespace csg {
38
49 public:
51 ~H5MDTrajectoryReader() override;
52
54 bool Open(const std::string &file) override;
55
57 void Initialize(Topology &top);
58
60 bool FirstFrame(Topology &conf) override;
61
63 bool NextFrame(Topology &conf) override;
64
66 void Close() override;
67
68 private:
70
72 template <typename T1>
73 T1 *ReadVectorData(hid_t ds, hid_t ds_data_type, Index row) {
74 hsize_t offset[3];
75 offset[0] = hsize_t(row);
76 offset[1] = 0;
77 offset[2] = 0;
78 hsize_t chunk_rows[3];
79 chunk_rows[0] = 1;
80 chunk_rows[1] = N_particles_;
81 chunk_rows[2] = vec_components_;
82 hid_t dsp = H5Dget_space(ds);
83 H5Sselect_hyperslab(dsp, H5S_SELECT_SET, offset, nullptr, chunk_rows,
84 nullptr);
85 hid_t mspace1 = H5Screate_simple(vec_components_, chunk_rows, nullptr);
86 T1 *data_out = new T1[N_particles_ * vec_components_];
87 herr_t status =
88 H5Dread(ds, ds_data_type, mspace1, dsp, H5P_DEFAULT, data_out);
89 if (status < 0) {
90 throw std::runtime_error("Error ReadVectorData: " +
91 boost::lexical_cast<std::string>(status));
92 } else {
93 return data_out;
94 }
95 }
96
98 template <typename T1>
99 T1 *ReadScalarData(hid_t ds, hid_t ds_data_type, Index row) {
100 hsize_t offset[2];
101 offset[0] = row;
102 offset[1] = 0;
103 hsize_t ch_rows[2];
104 ch_rows[0] = 1;
105 ch_rows[1] = N_particles_;
106 hid_t dsp = H5Dget_space(ds);
107 H5Sselect_hyperslab(dsp, H5S_SELECT_SET, offset, nullptr, ch_rows, nullptr);
108 hid_t mspace1 = H5Screate_simple(2, ch_rows, nullptr);
109 T1 *data_out = new T1[N_particles_];
110 herr_t status =
111 H5Dread(ds, ds_data_type, mspace1, dsp, H5P_DEFAULT, data_out);
112 if (status < 0) {
113 throw std::runtime_error("Error ReadScalarData: " +
114 boost::lexical_cast<std::string>(status));
115 } else {
116 return data_out;
117 }
118 }
119
120 template <typename T1>
121 void ReadStaticData(hid_t ds, hid_t ds_data_type,
122 std::unique_ptr<T1> &outbuf) {
123 herr_t status =
124 H5Dread(ds, ds_data_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, outbuf.get());
125 if (status < 0) {
126 H5Eprint(H5E_DEFAULT, stderr);
127 }
128 }
129
130 void ReadBox(hid_t ds, hid_t ds_data_type, Index row,
131 std::unique_ptr<double[]> &data_out);
132
133 double ReadScaleFactor(const hid_t &ds, const std::string &unit_type);
134
135 void CheckError(hid_t hid, std::string error_message) {
136 if (hid < 0) {
137 // H5Eprint(H5E_DEFAULT, stderr);
138 throw std::runtime_error(error_message);
139 }
140 }
141
142 bool GroupExists(hid_t file_id, std::string path) {
143 H5G_stat_t info;
144 herr_t status = H5Gget_objinfo(file_id, path.c_str(), 0, &info);
145 if (status < 0) {
146 return false;
147 }
148 return info.type == H5G_GROUP;
149 }
150
151 hid_t file_id_;
157
164
166
167 std::string fname_;
169
170 // Flags about datasets.
175
177
178 // Current frame indicator.
181
182 // Number of particles. This is static among time.
184 //
186
187 // Length scaling - VOTCA internally uses nm as the length unit.
189 double length_scaling_ = 1.0;
190 double velocity_scaling_ = 1.0;
191 double force_scaling_ = 1.0;
192
193 // Convert map from SI prefixes to nm (VOTCA base length unit).
194 std::unordered_map<std::string, double> votca_units_scaling_factors =
195 std::unordered_map<std::string, double>({{"fm", 0.000001},
196 {"pm", 0.001},
197 {"nm", 1.0},
198 {"A", 0.1},
199 {"um", 1000.0}});
200
201 boost::regex suffix_units = boost::regex("^([a-z]+)([0-9+-]+)");
202
203 // Box matrix.
204 Eigen::Matrix3d m;
205};
206
207} // namespace csg
208} // namespace votca
209
210#endif
class for reading H5MD trajectory.
void Close() override
Closes original trajectory file.
double ReadScaleFactor(const hid_t &ds, const std::string &unit_type)
void ReadBox(hid_t ds, hid_t ds_data_type, Index row, std::unique_ptr< double[]> &data_out)
void ReadStaticData(hid_t ds, hid_t ds_data_type, std::unique_ptr< T1 > &outbuf)
bool NextFrame(Topology &conf) override
Reads in the next frame.
T1 * ReadVectorData(hid_t ds, hid_t ds_data_type, Index row)
Reads dataset that contains vectors.
void CheckError(hid_t hid, std::string error_message)
T1 * ReadScalarData(hid_t ds, hid_t ds_data_type, Index row)
Reads dataset with scalar values.
std::unordered_map< std::string, double > votca_units_scaling_factors
bool GroupExists(hid_t file_id, std::string path)
void Initialize(Topology &top)
Initialize data structures.
bool Open(const std::string &file) override
Opens original trajectory file.
bool FirstFrame(Topology &conf) override
Reads in the first frame.
topology of the whole system
Definition topology.h:81
trajectoryreader interface
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26