AGX Dynamics 2.38.0.2
Loading...
Searching...
No Matches
AgxH5.h
Go to the documentation of this file.
1/*
2Copyright 2007-2024. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16
17#ifndef AGX_AGXH5
18#define AGX_AGXH5
19
20
21#include <agx/config/AGX_USE_HDF5.h>
22#include <agx/String.h>
23#include <agxData/BinaryData.h>
24#include <agx/Vector.h>
26
27#include <iostream>
28#include <string.h>
29#include <typeinfo>
30#include <valarray>
31
32#if AGX_USE_HDF5()
33#include <agx/PushDisableWarnings.h> // Disabling warnings. Include agx/PopDisableWarnings.h below!
34#include <H5Cpp.h>
35#include <H5Exception.h>
36
37#include <H5Tpublic.h>
38#include <agx/PopDisableWarnings.h> // End of disabled warnings.
39
40
41
42#define AGX_HDF5_VERIFY_BUFFER_SIZE 8192
43#define agxHdf5VerifyN(x, node, format, ...) \
44if (!(x)) \
45{ \
46 char __str[AGX_HDF5_VERIFY_BUFFER_SIZE]; \
47 agx_snprintf(__str, AGX_HDF5_VERIFY_BUFFER_SIZE, format, ##__VA_ARGS__); \
48 ssize_t pathLength = H5Iget_name( (node)->getId(), nullptr, 0 ); \
49 agx::String path = "<nameless>"; \
50 if ( pathLength > 0 ) \
51 { \
52 char* pathBuffer = new char[pathLength+1]; \
53 ::memset( pathBuffer, 0, pathLength+1 ); \
54 H5Iget_name( (node)->getId(), pathBuffer, pathLength ); \
55 path = pathBuffer; \
56 } \
57 std::stringstream __stream; \
58 __stream << "[" << __FILE__ << ":" << __LINE__ << "] HDF5 error: " << __str << ". Error at object " << path << std::endl; \
59 __stream << "==============================================================================" << std::endl; \
60 const std::string& __string = __stream.str(); \
61 std::cerr << __string << std::endl; \
62 agxThrow std::runtime_error(__string); \
63}
64
65
66namespace agx {
67 class Component;
68
72 namespace h5 {
86 // HDF5 is not thread safe :/
87 extern ReentrantMutex mutex;
88
89 AGXCORE_EXPORT extern const agx::String agx_format;
90 AGXCORE_EXPORT extern const agx::String agx_problem_type;
91 AGXCORE_EXPORT extern const agx::String agx_problem_mlcp;
92 AGXCORE_EXPORT extern const agx::String agx_problem_nlmcp;
93 AGXCORE_EXPORT extern const agx::String agx_problem_linear;
94 AGXCORE_EXPORT extern const agx::String agx_sparse_matrix;
95 AGXCORE_EXPORT extern const agx::String agx_matrix;
96 AGXCORE_EXPORT extern const agx::String agx_type;
97 AGXCORE_EXPORT extern const agx::String oct_format;
98 AGXCORE_EXPORT extern const agx::String oct_type;
99 AGXCORE_EXPORT extern const agx::String oct_sparse_matrix;
100 AGXCORE_EXPORT extern const agx::String oct_matrix;
101 AGXCORE_EXPORT extern const agx::String oct_scalar;
102 AGXCORE_EXPORT extern const agx::String oct_value;
103
104
105 AGXCORE_EXPORT void dumpComponent(agx::Component *component, const agx::String& filePath);
106
107
108 struct IteratorData {
109 IteratorData() : m_name() {}
110 agx::String m_name;
111 };
112
115 class AGXCORE_EXPORT h2s {
116 public:
117 h2s(hsize_t a, hsize_t b) {
118 m[0]=a;
119 m[1]=b;
120 }
121 h2s(hsize_t a) {
122 m[0]= a;
123 m[1]=0;
124 }
125 h2s() { m[0]=1; m[1]=1;}
126 #if !defined(__APPLE__)
127 #ifndef _WIN32
129 hsize_t& operator[](size_t i) { return m[i]; }
130 hsize_t operator[](size_t i) const { return m[i]; }
131 #endif
132 #endif
133 operator const hsize_t*() const { return m; }
134 operator hsize_t*() { return m; }
135
136 hsize_t m[2];
137 };
138
139
140 // Type information needed by HDF5 functions collected here to allow
141 // extensive polymorphism.
142 struct h5_type {
143 const std::type_info& info;
144 const H5::PredType& predicate;
145 const H5::DataType type;
146 const char* name;
147 h5_type()
148 : info(typeid(void*)), predicate(H5::PredType::NATIVE_UINT64),
149 type(H5::IntType(H5::PredType::NATIVE_UINT64)), name("UNKNOWN")
150 {
151 }
152 h5_type(const std::type_info& newInfo, const H5::PredType& newPredicate,
153 const H5::DataType newType, const char* newName)
154 : info(newInfo), predicate(newPredicate), type(newType), name(newName)
155 {}
156
157 private:
158 // Do not use.
159 h5_type& operator= (const h5_type&)
160 {
161 return *this;
162 }
163 };
164
166 const h5_type& h5_type_find(const std::type_info & type);
167
168
170 void dump_string(H5::CommonFG& g, const agx::String& name, const agx::String& s);
171
173 AGXCORE_EXPORT H5::DataSpace create_dataspace(const h2s& h);
174
175
176 template <typename R>
177 void dump_array(H5::CommonFG& g, const std::string& name, const R* a, size_t m=1, size_t n=1)
178 {
179 try {
180 const h5_type& t = h5_type_find(typeid(a[0]));
181 if ( typeid(a[0]) == t.info ) {
182 H5::DataSet dataset = g.createDataSet( name, t.type, create_dataspace(h2s(m, n)));
183 dataset.write( a, t.predicate);
184 dataset.close();
185 }
186 }catch ( const H5::DataSetIException& exception ) {
187 exception.printError();
188 }
189 }
190
191 template <typename R>
192 void overwrite_scalar(H5::CommonFG& g, const agx::String& name, const R a) {
193 try {
194 const h5_type& t = h5_type_find(typeid(a));
195 if ( typeid(a) == t.info ) {
196 H5::DataSet dataset = g.openDataSet(name);
197 dataset.write(&a, t.predicate);
198 dataset.close();
199 }
200 } catch ( const H5::DataSetIException& exception ) {
201 exception.printError();
202 }
203 }
204
205 template <typename R>
206 void dump_scalar(H5::CommonFG& g, const agx::String& name, const R a){
207 dump_array<R>(g, name, &a, 1, 0);
208 }
209
210 template <>
211 inline void dump_scalar<bool>(H5::CommonFG& g, const agx::String& name, const bool a) {
212 int8_t valueBits = static_cast<int8_t>(a);
213 try {
214 const h5_type& t = h5_type_find(typeid(valueBits));
215 if ( typeid(valueBits) == t.info ) {
216 H5::DataSet dataset = g.createDataSet( name, t.type, create_dataspace(h2s(1,0)) );
217 dataset.write( &valueBits, t.predicate );
218 dataset.close();
219 }
220 } catch ( const H5::DataSetIException& exception ) {
221 exception.printError();
222 }
223 }
224
225 template <typename R>
226 void set_scalar_attribute (H5::H5Object& g, const agx::String& name, const R& val){
227 try {
228 const h5_type& t = h5_type_find(typeid(val));
229 if ( typeid(val) == t.info ) {
230 H5::Attribute attr = g.createAttribute(name, t.type, H5::DataSpace(H5S_SCALAR));
231 attr.write(t.type, &val);
232 } else {
233 }
234 }
235 catch ( const H5::AttributeIException& exception ) {
236 exception.printError();
237 return;
238 }
239 catch ( const H5::DataTypeIException& exception ) {
240 exception.printError();
241 return;
242 }
243 }
244
245 template<>
247 void set_scalar_attribute(H5::H5Object& g, const agx::String &name, const agx::String& val);
248
249
251 void print_attributes( H5::H5Object& loc, const H5std_string attr_name, void* operator_data);
252
253
258 template < typename T >
259 bool get_scalar_attribute(H5::H5Object& g, const agx::String& name, T& result){
260 try {
261 const h5_type& t = h5_type_find(typeid(result));
262 H5::Attribute attribute;
263 try{
264 attribute = g.openAttribute(name);
265 } catch ( const H5::AttributeIException& /*exception*/ ) {
266 return false;
267 }
268 try {
269 if ( attribute.getDataType() == t.predicate ) {
270 attribute.read( attribute.getDataType(), &result );
271 return true;
272 }
273 } catch ( const H5::DataTypeIException& /*exception*/ ) {
274 return false;
275 }
276 } catch ( const H5::DataTypeIException& /*exception*/ ) {
277 return false;
278 }
279 return false;
280 }
281
282
287 template <>
289 bool get_scalar_attribute<agx::String>(H5::H5Object& g, const agx::String& name, agx::String& result);
290
291
293 template <typename R>
294 bool get_scalar(H5::CommonFG& g, const agx::String& name, R& result)
295 {
296 const h5_type & t = h5_type_find(typeid(result));
297 try {
298 H5::DataSet dataset = g.openDataSet(name);
299 if ( dataset.getSpace().getSimpleExtentType() == H5S_SCALAR ) {
300 if ( dataset.getDataType() == t.predicate ) { // need error message or exception if this is false
301 try {
302 dataset.read(&result, dataset.getDataType() );
303 return true;
304 } catch ( const H5::DataSetIException& /*exception*/ ) {
305 return false;
306 }
307 } else {
308 }
309 } else {
310 return false;
311 }
312 } catch ( const H5::DataSetIException& /*exception*/ ) {
313 return false;
314 }
315 return false;
316 }
317
318
319 template<>
321 inline bool get_scalar<bool>(H5::CommonFG& g, const agx::String& name, bool& result)
322 {
323 const h5_type& t = h5_type_find(typeid(result));
324 try {
325 H5::DataSet dataset = g.openDataSet(name);
326 if (dataset.getSpace().getSimpleExtentType() == H5S_SCALAR) {
327 if (dataset.getDataType() == t.predicate) {
328 try {
329 int8_t value = 0;
330 dataset.read(&value, dataset.getDataType());
331 result = (value != 0);
332 return true;
333 } catch (const H5::DataSetIException& ) {
334 return false;
335 }
336 } else if (dataset.getDataType() == H5::IntType(H5::PredType::STD_I8BE)){
337 try {
338 dataset.read(&result, dataset.getDataType());
339 return true;
340 } catch (const H5::DataSetIException& ) {
341 return false;
342 }
343 } else {
344 return false;
345 }
346 } else {
347 // Not scalar.
348 return false;
349 }
350 } catch (const H5::DataSetIException&) {
351 return false;
352 }
353 }
354
355 template <typename R>
357 void dump_array_oct(H5::CommonFG& g, const agx::String& name, const R* a, size_t m=1, size_t n=1)
358 {
359 H5::Group gg = g.createGroup(name);
360 set_scalar_attribute(gg, oct_format, 1);
361 set_scalar_attribute(gg, agx_format, 1);
362 if ( m>1 || n>1 ) {
363 dump_string(gg, oct_type, oct_matrix);
364 }
365 else {
366 dump_string(gg, oct_type, oct_scalar);
367 }
368 if ( m == n && n == 1)
369 n = 0;
370 dump_array<R>(gg, "value", a, m, n);
371 gg.close();
372 }
373
374
376 template <typename R>
377 void dump_scalar_oct(H5::CommonFG& g, const agx::String& name, R a)
378 {
379 dump_array_oct<R>(g, name, &a);
380
381 }
382
386 AGXCORE_EXPORT agxData::BinaryData* get_data(const H5::CommonFG& group, const agx::String& name);
387
388
389
390 AGXCORE_EXPORT H5::H5File* append_or_create(const agx::String& filename);
391 AGXCORE_EXPORT H5::Group append_problem(H5::H5File* file, const agx::String& name = "Problem");
392
396 AGXCORE_EXPORT hsize_t getIndexOf( const H5::CommonFG& parent, const H5std_string& childName );
397 AGXCORE_EXPORT bool hasChildNamed( const H5::H5Object& parent, const H5std_string& childName );
398 AGXCORE_EXPORT bool hasChildNamed( const H5::H5File& parent, const H5std_string& childName );
399
400
404 AGXCORE_EXPORT H5::Group getOrCreateGroup( H5::Group& parent, const H5std_string& childName );
405 AGXCORE_EXPORT H5::Group getOrCreateGroup( H5::H5File* parent, const H5std_string& childName );
406
408 AGXCORE_EXPORT bool check_attr(const H5::H5Object& g, const agx::String & name);
409
412 AGXCORE_EXPORT int get_string(const H5::CommonFG& g, const agx::String& name, agx::String& buff );
413
414
419 template<typename R>
420 int get_array(const H5::CommonFG& g, const agx::String& name, R& v )
421 {
422 int ret = 0;
423 try {
424 H5::Exception::dontPrint();
425 H5::DataSet t = g.openDataSet(name);
426 try {
427 H5::Exception::dontPrint();
428 H5::DataSpace ds = t.getSpace();
429 h2s dims;
430 ds.getSimpleExtentDims(dims);
431 v.resize(size_t(dims[0]));
432 t.read(&(v[0]), t.getFloatType());
433 t.close();
434 ret = 1;
435 } catch ( H5::DataSetIException& /*error*/ ) {
436 }
437 }
438 catch (H5::GroupIException& /*error*/ ) {
439 }
440 return ret;
441 }
442
443
444 template <typename R>
445 int get_array_oct(const H5::CommonFG& g, const agx::String& name, R& v )
446 {
447 return get_array<R>(g, name, v);
448 }
449
450 } /* namespace H5 */
451} /* namespace agx */
452
453extern "C" {
454 herr_t list_nodes (hid_t group_id, const char* member_name, void* operator_data);
455}
456
457
458// AGX_USE_HDF5.
459#endif
460
461// Include guard.
462#endif
#define AGXCORE_EXPORT
A component is an object containing other objects, enabling hierarchical structuring.
Definition Component.h:39
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
std::recursive_mutex ReentrantMutex
Definition Object.h:48