Chaste Commit::baa90ac2819b962188b7562f2326be23c47859a7
AbstractHdf5Access.cpp
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, are permitted provided that the following conditions are met:
14 * Redistributions of source code must retain the above copyright notice,
15 this list of conditions and the following disclaimer.
16 * Redistributions in binary form must reproduce the above copyright notice,
17 this list of conditions and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
19 * Neither the name of the University of Oxford nor the names of its
20 contributors may be used to endorse or promote products derived from this
21 software without specific prior written permission.
22
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
36#include "AbstractHdf5Access.hpp"
37#include "Exception.hpp"
38
39bool AbstractHdf5Access::DoesDatasetExist(const std::string& rDatasetName)
40{
41 // This is a nice method for testing existence, introduced in HDF5 1.8.0
42 htri_t dataset_status = H5Lexists(mFileId, rDatasetName.c_str(), H5P_DEFAULT);
43 return (dataset_status > 0);
44}
45
47{
48 // Now deal with the unlimited dimension
49
50 // In terms of an Unlimited dimension dataset:
51 // * Files pre - r16738 (inc. Release 3.1 and earlier) use simply "Time" for "Data"'s unlimited variable.
52 // * Files generated by r16738 - r18257 used "<DatasetName>_Time" for "<DatasetName>"'s unlimited variable,
53 // - These are not to be used and there is no backwards compatibility for them, since they weren't in a release.
54 // * Files post r18257 (inc. Release 3.2 onwards) use "<DatasetName>_Unlimited" for "<DatasetName>"'s
55 // unlimited variable,
56 // - a new attribute "Name" has been added to the Unlimited Dataset to allow it to assign
57 // any name to the unlimited variable. Which can then be easily read by Hdf5DataReader.
58 // - if this dataset is missing we look for simply "Time" to remain backwards compatible with Releases <= 3.1
59
60 if (DoesDatasetExist(mDatasetName + "_Unlimited"))
61 {
62 mUnlimitedDatasetId = H5Dopen(mFileId, (mDatasetName + "_Unlimited").c_str(), H5P_DEFAULT);
63 hid_t name_attribute_id = H5Aopen_name(mUnlimitedDatasetId, "Name");
64 hid_t unit_attribute_id = H5Aopen_name(mUnlimitedDatasetId, "Unit");
65
66 hid_t attribute_type = H5Aget_type(name_attribute_id);
67
68 // Read into it.
69 char* string_array = (char*)malloc(sizeof(char) * MAX_STRING_SIZE);
70 H5Aread(name_attribute_id, attribute_type, string_array);
71 std::string name_string(&string_array[0]);
72 mUnlimitedDimensionName = name_string;
73
74 H5Aread(unit_attribute_id, attribute_type, string_array);
75 std::string unit_string(&string_array[0]);
76 mUnlimitedDimensionUnit = unit_string;
77
78 free(string_array);
79 H5Tclose(attribute_type);
80 H5Aclose(name_attribute_id);
81 H5Aclose(unit_attribute_id);
82 }
83 else if (DoesDatasetExist("Time"))
84 {
87 mUnlimitedDatasetId = H5Dopen(mFileId, mUnlimitedDimensionName.c_str(), H5P_DEFAULT);
88 }
89 else
90 {
92 }
94}
95
96AbstractHdf5Access::AbstractHdf5Access(const std::string& rDirectory,
97 const std::string& rBaseName,
98 const std::string& rDatasetName,
99 bool makeAbsolute)
100 : mBaseName(rBaseName),
101 mDatasetName(rDatasetName),
102 mIsDataComplete(true),
103 mIsUnlimitedDimensionSet(false)
104{
105 RelativeTo::Value relative_to;
106 if (makeAbsolute)
107 {
108 relative_to = RelativeTo::ChasteTestOutput;
109 }
110 else
111 {
112 relative_to = RelativeTo::Absolute;
113 }
114 mDirectory.SetPath(rDirectory, relative_to);
115}
116
118 const std::string& rBaseName,
119 const std::string& rDatasetName)
120 : mBaseName(rBaseName),
121 mDatasetName(rDatasetName),
122 mDirectory(rDirectory),
123 mIsDataComplete(true),
124 mIsUnlimitedDimensionSet(false)
125{
126}
127
131
136
138{
140}
141
146
151
153{
154 // 128 M cache for raw data. 12799 is a prime number which is 100 x larger
155 // than the number of 1 MB chunks (the largest we have tested with) which
156 // could fit into the 128 M cache. 0.75 for the last argument is the default.
157 // See: http://www.hdfgroup.org/HDF5/doc/RM/RM_H5P.html#Property-SetChunkCache
158
159 hsize_t max_objects_in_chunk_cache = 12799u;
160 hsize_t max_bytes_in_cache = 128u * 1024u * 1024u;
161#if H5_VERS_MAJOR >= 1 && H5_VERS_MINOR >= 8 && H5_VERS_RELEASE >= 3 // HDF5 1.8.3+
162 // These methods set the cache on a dataset basis
163 hid_t dapl_id = H5Dget_access_plist(mVariablesDatasetId);
164 H5Pset_chunk_cache(dapl_id,
165 max_objects_in_chunk_cache,
166 max_bytes_in_cache,
167 H5D_CHUNK_CACHE_W0_DEFAULT);
168#else
169 // These older methods set the cache on a file basis
170 hid_t fapl_id = H5Fget_access_plist(mFileId);
171 H5Pset_cache(fapl_id,
172 0, // unused
173 max_objects_in_chunk_cache,
174 max_bytes_in_cache,
175 0.75); // default value
176#endif
177}
#define NEVER_REACHED
std::string GetUnlimitedDimensionName()
bool DoesDatasetExist(const std::string &rDatasetName)
std::vector< unsigned > GetIncompleteNodeMap()
AbstractHdf5Access(const std::string &rDirectory, const std::string &rBaseName, const std::string &rDatasetName, bool makeAbsolute=true)
std::vector< unsigned > mIncompleteNodeIndices
std::string mUnlimitedDimensionUnit
std::string GetUnlimitedDimensionUnit()
std::string mUnlimitedDimensionName
virtual void SetPath(const std::string &rPath, RelativeTo::Value relativeTo)