Chaste  Release::3.4
AbstractHdf5Access.cpp
1 /*
2 
3 Copyright (c) 2005-2016, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, 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 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #include "Exception.hpp"
37 #include "AbstractHdf5Access.hpp"
38 
39 bool AbstractHdf5Access::DoesDatasetExist(const std::string& rDatasetName)
40 {
41 #if H5_VERS_MAJOR>=1 && H5_VERS_MINOR>=8
42  // This is a nice method for testing existence, introduced in HDF5 1.8.0
43  htri_t dataset_status = H5Lexists(mFileId, rDatasetName.c_str(), H5P_DEFAULT);
44  return (dataset_status>0);
45 #else
46  bool result=false;
47  // This is not a nice way of doing it because the error stack produces a load of 'HDF failed' output.
48  // The "TRY" macros are a convenient way to temporarily turn the error stack off.
49  H5E_BEGIN_TRY
50  {
51  hid_t dataset_id = H5Dopen(mFileId, rDatasetName.c_str());
52  if (dataset_id>0)
53  {
54  H5Dclose(dataset_id);
55  result = true;
56  }
57  }
58  H5E_END_TRY;
59 
60  return result;
61 #endif
62 }
63 
65 {
66  // Now deal with the unlimited dimension
67 
68  // In terms of an Unlimited dimension dataset:
69  // * Files pre - r16738 (inc. Release 3.1 and earlier) use simply "Time" for "Data"'s unlimited variable.
70  // * Files generated by r16738 - r18257 used "<DatasetName>_Time" for "<DatasetName>"'s unlimited variable,
71  // - These are not to be used and there is no backwards compatibility for them, since they weren't in a release.
72  // * Files post r18257 (inc. Release 3.2 onwards) use "<DatasetName>_Unlimited" for "<DatasetName>"'s
73  // unlimited variable,
74  // - a new attribute "Name" has been added to the Unlimited Dataset to allow it to assign
75  // any name to the unlimited variable. Which can then be easily read by Hdf5DataReader.
76  // - if this dataset is missing we look for simply "Time" to remain backwards compatible with Releases <= 3.1
77 
78  if (DoesDatasetExist(mDatasetName + "_Unlimited"))
79  {
80  mUnlimitedDatasetId = H5Dopen(mFileId, (mDatasetName + "_Unlimited").c_str());
81  hid_t name_attribute_id = H5Aopen_name(mUnlimitedDatasetId, "Name");
82  hid_t unit_attribute_id = H5Aopen_name(mUnlimitedDatasetId, "Unit");
83 
84  hid_t attribute_type = H5Aget_type(name_attribute_id);
85 
86  // Read into it.
87  char* string_array = (char *)malloc(sizeof(char)*MAX_STRING_SIZE);
88  H5Aread( name_attribute_id, attribute_type, string_array);
89  std::string name_string(&string_array[0]);
90  mUnlimitedDimensionName = name_string;
91 
92  H5Aread( unit_attribute_id, attribute_type, string_array);
93  std::string unit_string(&string_array[0]);
94  mUnlimitedDimensionUnit = unit_string;
95 
96  free(string_array);
97  H5Tclose(attribute_type);
98  H5Aclose(name_attribute_id);
99  H5Aclose(unit_attribute_id);
100  }
101  else if (DoesDatasetExist("Time"))
102  {
103  mUnlimitedDimensionName = "Time";
104  mUnlimitedDimensionUnit = "msec";
106  }
107  else
108  {
110  }
112 }
113 
114 AbstractHdf5Access::AbstractHdf5Access(const std::string& rDirectory,
115  const std::string& rBaseName,
116  const std::string& rDatasetName,
117  bool makeAbsolute)
118  : mBaseName(rBaseName),
119  mDatasetName(rDatasetName),
120  mIsDataComplete(true),
121  mIsUnlimitedDimensionSet(false)
122 {
123  RelativeTo::Value relative_to;
124  if (makeAbsolute)
125  {
126  relative_to = RelativeTo::ChasteTestOutput;
127  }
128  else
129  {
130  relative_to = RelativeTo::Absolute;
131  }
132  mDirectory.SetPath(rDirectory, relative_to);
133 }
134 
136  const std::string& rBaseName,
137  const std::string& rDatasetName)
138  : mBaseName(rBaseName),
139  mDatasetName(rDatasetName),
140  mDirectory(rDirectory),
141  mIsDataComplete(true),
142  mIsUnlimitedDimensionSet(false)
143 {
144 }
145 
147 {
148 }
149 
150 
152 {
153  return mIsDataComplete;
154 }
155 
156 
158 {
159  return mIncompleteNodeIndices;
160 }
161 
163 {
165 }
166 
168 {
170 }
171 
173 {
174  // 128 M cache for raw data. 12799 is a prime number which is 100 x larger
175  // than the number of 1 MB chunks (the largest we have tested with) which
176  // could fit into the 128 M cache. 0.75 for the last argument is the default.
177  // See: http://www.hdfgroup.org/HDF5/doc/RM/RM_H5P.html#Property-SetChunkCache
178 
179  hsize_t max_objects_in_chunk_cache = 12799u;
180  hsize_t max_bytes_in_cache = 128u*1024u*1024u;
181 #if H5_VERS_MAJOR>=1 && H5_VERS_MINOR>=8 && H5_VERS_RELEASE>=3 // HDF5 1.8.3+
182  // These methods set the cache on a dataset basis
183  hid_t dapl_id = H5Dget_access_plist( mVariablesDatasetId );
184  H5Pset_chunk_cache( dapl_id,
185  max_objects_in_chunk_cache ,
186  max_bytes_in_cache ,
187  H5D_CHUNK_CACHE_W0_DEFAULT);
188 #else
189  // These older methods set the cache on a file basis
190  hid_t fapl_id = H5Fget_access_plist( mFileId );
191  H5Pset_cache( fapl_id,
192  0, // unused
193  max_objects_in_chunk_cache,
194  max_bytes_in_cache,
195  0.75); // default value
196 #endif
197 }
198 
199 
std::string GetUnlimitedDimensionUnit()
std::string mUnlimitedDimensionName
std::string mUnlimitedDimensionUnit
#define NEVER_REACHED
Definition: Exception.hpp:206
bool DoesDatasetExist(const std::string &rDatasetName)
std::string GetUnlimitedDimensionName()
AbstractHdf5Access(const std::string &rDirectory, const std::string &rBaseName, const std::string &rDatasetName, bool makeAbsolute=true)
std::vector< unsigned > mIncompleteNodeIndices
virtual void SetPath(const std::string &rPath, RelativeTo::Value relativeTo)
Definition: FileFinder.cpp:99
std::vector< unsigned > GetIncompleteNodeMap()