Lattice Microbes 2.5
This is for whole cell modeling
Loading...
Searching...
No Matches
ArbitraryH5.h
Go to the documentation of this file.
1/*
2 * University of Illinois Open Source License
3 * Copyright 2018-2018 Luthey-Schulten Group,
4 * All rights reserved.
5 *
6 * Developed by: Luthey-Schulten Group
7 * University of Illinois at Urbana-Champaign
8 * http://www.scs.uiuc.edu/~schulten
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy of
11 * this software and associated documentation files (the Software), to deal with
12 * the Software without restriction, including without limitation the rights to
13 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
14 * of the Software, and to permit persons to whom the Software is furnished to
15 * do so, subject to the following conditions:
16 *
17 * - Redistributions of source code must retain the above copyright notice,
18 * this list of conditions and the following disclaimers.
19 *
20 * - Redistributions in binary form must reproduce the above copyright notice,
21 * this list of conditions and the following disclaimers in the documentation
22 * and/or other materials provided with the distribution.
23 *
24 * - Neither the names of the Luthey-Schulten Group, University of Illinois at
25 * Urbana-Champaign, nor the names of its contributors may be used to endorse or
26 * promote products derived from this Software without specific prior written
27 * permission.
28 *
29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
32 * THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
33 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
34 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35 * OTHER DEALINGS WITH THE SOFTWARE.
36 *
37 * Author(s): Tyler M. Earnest
38 */
39
40#ifndef _ARBITRARYH5_H
41#define _ARBITRARYH5_H
42
43#include <hdf5.h>
44#include <algorithm>
45#include <vector>
46#include <cstddef>
47#include <string>
48#include <functional>
49#include <numeric>
50#include "core/Exceptions.h"
51#include "core/Print.h"
52
53#define MAX_H5_NAME 64
54#define MAX_H5L_PATH 64
55
56struct H5Lookup {
57 enum Mode {
60 };
61 unsigned int type; // expected by LocalDataOutputWorker::run()
62 unsigned int replicate; // expected by LocalDataOutputWorker::run()
63 size_t messageSize; // expected by LocalDataOutputWorker::run()
64 Mode mode; // "message" data
65 char path[MAX_H5L_PATH]; // "message" data
66 char attr[MAX_H5L_PATH]; // "message" data
67 hid_t h5type; // "message" data
68 size_t payloadSize; // expected by LocalDataOutputWorker::run()
69};
70
71
72struct H5MetaData {
78 unsigned int type; // expected by LocalDataOutputWorker::run()
79 unsigned int replicate; // expected by LocalDataOutputWorker::run()
80 size_t messageSize; // expected by LocalDataOutputWorker::run()
81 Mode mode; // "message" data
82 hid_t h5type; // "message" data
83 size_t ndim; // "message" data
84 size_t shape[H5S_MAX_RANK]; // "message" data
85 char name[MAX_H5_NAME]; // "message" data
86 size_t payloadSize; // expected by LocalDataOutputWorker::run()
87};
88
89template <typename T> hid_t get_h5_type_id();
90
91template <> inline hid_t get_h5_type_id<char>() { return H5T_NATIVE_CHAR; }
92template <> inline hid_t get_h5_type_id<unsigned char>() { return H5T_NATIVE_UCHAR; }
93template <> inline hid_t get_h5_type_id<short>() { return H5T_NATIVE_SHORT; }
94template <> inline hid_t get_h5_type_id<unsigned short>() { return H5T_NATIVE_USHORT; }
95template <> inline hid_t get_h5_type_id<int>() { return H5T_NATIVE_INT; }
96template <> inline hid_t get_h5_type_id<unsigned int>() { return H5T_NATIVE_UINT; }
97template <> inline hid_t get_h5_type_id<long>() { return H5T_NATIVE_LONG; }
98template <> inline hid_t get_h5_type_id<unsigned long>() { return H5T_NATIVE_ULONG; }
99template <> inline hid_t get_h5_type_id<long long>() { return H5T_NATIVE_LLONG; }
100template <> inline hid_t get_h5_type_id<unsigned long long>() { return H5T_NATIVE_ULLONG; }
101template <> inline hid_t get_h5_type_id<float>() { return H5T_NATIVE_FLOAT; }
102template <> inline hid_t get_h5_type_id<double>() { return H5T_NATIVE_DOUBLE; }
103template <> inline hid_t get_h5_type_id<long double>() { return H5T_NATIVE_LDOUBLE; }
104
105template <typename T>
107make_H5_meta(H5MetaData::Mode mode, const std::vector<size_t> shape, const std::string name)
108{
109 if (shape.size() > H5S_MAX_RANK) {
110 throw lm::Exception("Dimension exceeds HDF5 limits");
111 }
112
113 if (name.size() > MAX_H5_NAME-1) {
114 throw lm::Exception("Name length too long");
115 }
116
117 H5MetaData md;
119 md.ndim = shape.size();
120 md.payloadSize = sizeof(T)*std::accumulate(shape.begin(), shape.end(), 1, std::multiplies<size_t>());
121 md.messageSize = offsetof(H5MetaData, payloadSize) - offsetof(H5MetaData, mode);
122
123 md.mode = mode;
124 std::copy(shape.begin(), shape.end(), md.shape);
125 std::copy(name.begin(), name.end(), md.name);
126 md.name[name.size()] = 0;
127 return md;
128}
129
130
131inline H5Lookup
132make_H5_lookup(H5Lookup::Mode mode, const std::string path, const std::string attr="")
133{
134 if (path.size() > MAX_H5L_PATH-1) {
135 throw lm::Exception("Path length too long");
136 }
137 if (attr.size() > MAX_H5L_PATH-1) {
138 throw lm::Exception("Attr length too long");
139 }
140
141 H5Lookup md;
142 md.payloadSize = 0;
143 md.messageSize = offsetof(H5Lookup, payloadSize) - offsetof(H5Lookup, mode);
144
145 md.mode = mode;
146
147 std::copy(attr.begin(), attr.end(), md.attr);
148 md.attr[attr.size()] = 0;
149
150 std::copy(path.begin(), path.end(), md.path);
151 md.path[path.size()] = 0;
152 return md;
153}
154
155
156
157#undef MAX_H5_NAME
158#undef MAX_H5L_PATH
159
160#endif /* _ARBITRARYH5_H */
hid_t get_h5_type_id()
hid_t get_h5_type_id< unsigned long long >()
Definition ArbitraryH5.h:100
H5Lookup make_H5_lookup(H5Lookup::Mode mode, const std::string path, const std::string attr="")
Definition ArbitraryH5.h:132
hid_t get_h5_type_id< double >()
Definition ArbitraryH5.h:102
hid_t get_h5_type_id< long double >()
Definition ArbitraryH5.h:103
hid_t get_h5_type_id< long long >()
Definition ArbitraryH5.h:99
hid_t get_h5_type_id< short >()
Definition ArbitraryH5.h:93
hid_t get_h5_type_id< unsigned short >()
Definition ArbitraryH5.h:94
hid_t get_h5_type_id< long >()
Definition ArbitraryH5.h:97
hid_t get_h5_type_id< float >()
Definition ArbitraryH5.h:101
hid_t get_h5_type_id< char >()
Definition ArbitraryH5.h:91
H5MetaData make_H5_meta(H5MetaData::Mode mode, const std::vector< size_t > shape, const std::string name)
Definition ArbitraryH5.h:107
hid_t get_h5_type_id< int >()
Definition ArbitraryH5.h:95
hid_t get_h5_type_id< unsigned char >()
Definition ArbitraryH5.h:92
#define MAX_H5_NAME
Definition ArbitraryH5.h:53
hid_t get_h5_type_id< unsigned int >()
Definition ArbitraryH5.h:96
hid_t get_h5_type_id< unsigned long >()
Definition ArbitraryH5.h:98
#define MAX_H5L_PATH
Definition ArbitraryH5.h:54
Base class for exceptions.
Definition Exceptions.h:55
Definition ArbitraryH5.h:56
Mode
Definition ArbitraryH5.h:57
@ ATTR
Definition ArbitraryH5.h:59
@ DATASET
Definition ArbitraryH5.h:58
hid_t h5type
Definition ArbitraryH5.h:67
Mode mode
Definition ArbitraryH5.h:64
char attr[MAX_H5L_PATH]
Definition ArbitraryH5.h:66
unsigned int replicate
Definition ArbitraryH5.h:62
size_t payloadSize
Definition ArbitraryH5.h:68
size_t messageSize
Definition ArbitraryH5.h:63
unsigned int type
Definition ArbitraryH5.h:61
char path[MAX_H5L_PATH]
Definition ArbitraryH5.h:65
Definition ArbitraryH5.h:72
Mode
Definition ArbitraryH5.h:73
@ APPEND_TO_DATASET
Definition ArbitraryH5.h:75
@ NEW_DATASET
Definition ArbitraryH5.h:76
@ APPEND_TO_GROUP
Definition ArbitraryH5.h:74
size_t payloadSize
Definition ArbitraryH5.h:86
size_t ndim
Definition ArbitraryH5.h:83
size_t shape[H5S_MAX_RANK]
Definition ArbitraryH5.h:84
unsigned int type
Definition ArbitraryH5.h:78
Mode mode
Definition ArbitraryH5.h:81
char name[MAX_H5_NAME]
Definition ArbitraryH5.h:85
size_t messageSize
Definition ArbitraryH5.h:80
hid_t h5type
Definition ArbitraryH5.h:82
unsigned int replicate
Definition ArbitraryH5.h:79