kspaceFirstOrder3D-OMP  1.1
The C++ implementation of the k-wave toolbox for the time-domain simulation of acoustic wave fields in 3D
 All Classes Files Functions Variables Typedefs Enumerations Friends Pages
RealMatrix.cpp
Go to the documentation of this file.
1 /**
2  * @file RealMatrix.cpp
3  * @author Jiri Jaros \n
4  * Faculty of Information Technology\n
5  * Brno University of Technology \n
6  * jarosjir@fit.vutbr.cz
7  *
8  * @brief The implementation file containing the class for real matrices.
9  *
10  * @version kspaceFirstOrder3D 2.15
11  *
12  * @date 11 July 2011, 10:30 (created) \n
13  * 25 September 2014, 15:13 (revised)
14  *
15  * @section License
16  * This file is part of the C++ extension of the k-Wave Toolbox (http://www.k-wave.org).\n
17  * Copyright (C) 2014 Jiri Jaros and Bradley Treeby
18  *
19  * This file is part of k-Wave. k-Wave is free software: you can redistribute it
20  * and/or modify it under the terms of the GNU Lesser General Public License as
21  * published by the Free Software Foundation, either version 3 of the License,
22  * or (at your option) any later version.
23  *
24  * k-Wave is distributed in the hope that it will be useful, but
25  * WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
27  * See the GNU Lesser General Public License for more details.
28  *
29  * You should have received a copy of the GNU Lesser General Public License
30  * along with k-Wave. If not, see <http://www.gnu.org/licenses/>.
31  */
32 
33 
34 #include <iostream>
35 #include <string.h>
36 
39 
40 #include <Utils/ErrorMessages.h>
41 //----------------------------------------------------------------------------//
42 // Constants //
43 //----------------------------------------------------------------------------//
44 
45 //----------------------------------------------------------------------------//
46 // Definitions //
47 //----------------------------------------------------------------------------//
48 
49 
50 
51 //----------------------------------------------------------------------------//
52 // Implementation //
53 // public methods //
54 //----------------------------------------------------------------------------//
55 
56 /**
57  * Constructor.
58  * @param [in] DimensionSizes - Dimension sizes
59  */
62 {
63  InitDimensions(DimensionSizes);
64 
66 }// end of TRealMatrixData
67 //-----------------------------------------------------------------------------
68 
69 /**
70  * Read data data from HDF5 file (only from the root group).
71  *
72  * @param [in] HDF5_File - HDF5 file
73  * @param [in] MatrixName - HDF5 dataset name
74  *
75  * @throw ios::failure if error occurred.
76  */
78  const char * MatrixName)
79 {
80  // test matrix datatype
81  if (HDF5_File.ReadMatrixDataType(HDF5_File.GetRootGroup(), MatrixName) != THDF5_File::hdf5_mdt_float)
82  {
83  char ErrorMessage[256];
84  sprintf(ErrorMessage, Matrix_ERR_FMT_MatrixNotFloat, MatrixName);
85  throw ios::failure(ErrorMessage);
86  }
87 
88 
89  if (HDF5_File.ReadMatrixDomainType(HDF5_File.GetRootGroup(), MatrixName) != THDF5_File::hdf5_mdt_real)
90  {
91  char ErrorMessage[256];
92  sprintf(ErrorMessage, Matrix_ERR_FMT_MatrixNotReal, MatrixName);
93  throw ios::failure(ErrorMessage);
94  }
95 
96  // Read matrix
97  HDF5_File.ReadCompleteDataset(HDF5_File.GetRootGroup(),
98  MatrixName,
101  );
102 }// end of LoadDataFromMatlabFile
103 //------------------------------------------------------------------------------
104 
105 /**
106  * Write data to HDF5 file (only from the root group)
107  *
108  * @param [in] HDF5_File - HDF5 file
109  * @param [in] MatrixName - HDF5 Matrix name
110  * @param [in] CompressionLevel - Compression level
111  *
112  * @throw ios::failure if an error occurred
113  */
115  const char * MatrixName,
116  const size_t CompressionLevel)
117 {
119  Chunks.Z = 1;
120 
121  //1D matrices
122  if ((pDimensionSizes.Y == 1) && (pDimensionSizes.Z == 1))
123  {
124  // Chunk = 4MB
126  {
127  Chunks.X = ChunkSize_1D_4MB;
128  }
129  else if (pDimensionSizes.X > 4 * ChunkSize_1D_1MB)
130  {
131  Chunks.X = ChunkSize_1D_1MB;
132  }
133  else if (pDimensionSizes.X > 4 * ChunkSize_1D_256KB)
134  {
135  Chunks.X = ChunkSize_1D_256KB;
136  }
137  }
138 
139  hid_t HDF5_Dataset_id = HDF5_File.CreateFloatDataset(HDF5_File.GetRootGroup(),
140  MatrixName,
142  Chunks,
143  CompressionLevel);
144 
145  HDF5_File.WriteHyperSlab(HDF5_Dataset_id,
146  TDimensionSizes(0, 0, 0),
147  pDimensionSizes,
148  pMatrixData);
149 
150  HDF5_File.CloseDataset(HDF5_Dataset_id);
151 
152  // Write data and domain type
153  HDF5_File.WriteMatrixDataType (HDF5_File.GetRootGroup(),
154  MatrixName,
155  THDF5_File::hdf5_mdt_float);
156  HDF5_File.WriteMatrixDomainType(HDF5_File.GetRootGroup(),
157  MatrixName,
158  THDF5_File::hdf5_mdt_real);
159 }// end of WriteDataToHDF5File
160 //------------------------------------------------------------------------------
161 
162 //----------------------------------------------------------------------------//
163 // Implementation //
164 // protected methods //
165 //----------------------------------------------------------------------------//
166 
167 /**
168  * Set necessary dimensions and auxiliary variables.
169  * @param [in] DimensionSizes - 3D Dimension sizes
170  */
171 void TRealMatrix::InitDimensions(const TDimensionSizes & DimensionSizes)
172 {
173  pDimensionSizes = DimensionSizes;
174 
178 
180 
182 
185 }// end of SetDimensions
186 //------------------------------------------------------------------------------/
187 
188 
189 //----------------------------------------------------------------------------//
190 // Implementation //
191 // private methods //
192 //----------------------------------------------------------------------------//
size_t Z
Z dimension size.
Abstract base class for float based matrices defining basic interface. Higher dimensional matrices st...
THDF5_File::THDF5_MatrixDataType ReadMatrixDataType(const hid_t ParentGroup, const char *DatasetName)
Read matrix data type from the dataset.
Definition: HDF5_File.cpp:1155
static const size_t ChunkSize_1D_1MB
Number of elements to get 1MB block of data.
Definition: RealMatrix.h:126
virtual void WriteDataToHDF5File(THDF5_File &HDF5_File, const char *MatrixName, const size_t CompressionLevel)
Write data into the HDF5 file.
Definition: RealMatrix.cpp:114
virtual void AllocateMemory()
Memory allocation.
The header file containing the class for real matrices.
size_t p2DDataSliceSize
Size of a 2D slab (X,Y).
static const size_t ChunkSize_1D_4MB
Number of elements to get 4MB block of data.
Definition: RealMatrix.h:124
virtual void ReadDataFromHDF5File(THDF5_File &HDF5_File, const char *MatrixName)
Read data from the HDF5 file - only from the root group.
Definition: RealMatrix.cpp:77
size_t X
X dimension size.
void WriteHyperSlab(const hid_t HDF5_Dataset_id, const TDimensionSizes &Position, const TDimensionSizes &Size, const float *Data)
Write a hyper-slab into the dataset - float dataset.
Definition: HDF5_File.cpp:467
size_t pTotalAllocatedElementCount
Total number of allocated elements (in terms of floats).
size_t pTotalElementCount
Total number of elements.
virtual void InitDimensions(const TDimensionSizes &DimensionSizes)
Init dimension.
Definition: RealMatrix.cpp:171
void WriteMatrixDataType(const hid_t ParentGroup, const char *DatasetName, const THDF5_MatrixDataType &MatrixDataType)
Write matrix data type into the dataset under a specified group.
Definition: HDF5_File.cpp:1116
void WriteMatrixDomainType(const hid_t ParentGroup, const char *DatasetName, const THDF5_MatrixDomainType &MatrixDomainType)
Write matrix domain type into the dataset under the root group.
Definition: HDF5_File.cpp:1135
struct TDimensionSizes pDimensionSizes
Dimension sizes.
hid_t GetRootGroup() const
Get handle to the root group.
Definition: HDF5_File.h:581
The header file containing all error messages of the project.
hid_t CreateFloatDataset(const hid_t ParentGroup, const char *DatasetName, const TDimensionSizes &DimensionSizes, const TDimensionSizes &ChunkSizes, const size_t CompressionLevel)
Create the HDF5 dataset at a specified place in the file tree (3D/4D).
Definition: HDF5_File.cpp:291
size_t pDataRowSize
Size of a 1D row in X dimension.
The header file with the class for complex matrices.
THDF5_File::THDF5_MatrixDomainType ReadMatrixDomainType(const hid_t ParentGroup, const char *DatasetName)
Read matrix domain type from the dataset under a specified group.
Definition: HDF5_File.cpp:1190
size_t Y
Y dimension size.
void ReadCompleteDataset(const hid_t ParentGroup, const char *DatasetName, const TDimensionSizes &DimensionSizes, float *Data)
Read data from the dataset under a specified group - float dataset.
Definition: HDF5_File.cpp:954
static const size_t ChunkSize_1D_256KB
Number of elements to get 256KB block of data.
Definition: RealMatrix.h:128
const char *const Matrix_ERR_FMT_MatrixNotFloat
Matrix class error message.
Definition: ErrorMessages.h:83
float * pMatrixData
Raw matrix data.
void CloseDataset(const hid_t HDF5_Dataset_id)
Close the HDF5 dataset.
Definition: HDF5_File.cpp:451
const char *const Matrix_ERR_FMT_MatrixNotReal
Matrix class error message.
Definition: ErrorMessages.h:85
TRealMatrix()
Default constructor is not allowed for public.
Definition: RealMatrix.h:110
Class wrapping the HDF5 routines.
Definition: HDF5_File.h:506
Structure with 4D dimension sizes (3 in space and 1 in time).