Chaste  Release::2017.1
AbstractHdf5Access.cpp
1 /*
2 
3 Copyright (c) 2005-2017, 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  // 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  {
85  mUnlimitedDimensionName = "Time";
86  mUnlimitedDimensionUnit = "msec";
87  mUnlimitedDatasetId = H5Dopen(mFileId, mUnlimitedDimensionName.c_str(), H5P_DEFAULT);
88  }
89  else
90  {
92  }
94 }
95 
96 AbstractHdf5Access::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),
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),
125 {
126 }
127 
129 {
130 }
131 
132 
134 {
135  return mIsDataComplete;
136 }
137 
138 
140 {
141  return mIncompleteNodeIndices;
142 }
143 
145 {
147 }
148 
150 {
152 }
153 
155 {
156  // 128 M cache for raw data. 12799 is a prime number which is 100 x larger
157  // than the number of 1 MB chunks (the largest we have tested with) which
158  // could fit into the 128 M cache. 0.75 for the last argument is the default.
159  // See: http://www.hdfgroup.org/HDF5/doc/RM/RM_H5P.html#Property-SetChunkCache
160 
161  hsize_t max_objects_in_chunk_cache = 12799u;
162  hsize_t max_bytes_in_cache = 128u*1024u*1024u;
163 #if H5_VERS_MAJOR>=1 && H5_VERS_MINOR>=8 && H5_VERS_RELEASE>=3 // HDF5 1.8.3+
164  // These methods set the cache on a dataset basis
165  hid_t dapl_id = H5Dget_access_plist( mVariablesDatasetId );
166  H5Pset_chunk_cache( dapl_id,
167  max_objects_in_chunk_cache ,
168  max_bytes_in_cache ,
169  H5D_CHUNK_CACHE_W0_DEFAULT);
170 #else
171  // These older methods set the cache on a file basis
172  hid_t fapl_id = H5Fget_access_plist( mFileId );
173  H5Pset_cache( fapl_id,
174  0, // unused
175  max_objects_in_chunk_cache,
176  max_bytes_in_cache,
177  0.75); // default value
178 #endif
179 }
180 
181 
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()