| kspaceFirstOrder3D-OMP 1.1 The C++ implementation of the k-wave toolbox for the time-domain simulation of acoustic wave fields in 3D

- Main Page
- Related Pages
- Classes
- Files

- File List
- File Members

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
37 #include <MatrixClasses/RealMatrix.h>
38 #include <MatrixClasses/ComplexMatrix.h>
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 */
60 TRealMatrix::TRealMatrix(const TDimensionSizes & DimensionSizes)
61 : TBaseFloatMatrix()
62 {
63 InitDimensions(DimensionSizes);
64
65 AllocateMemory();
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 */
77 void TRealMatrix::ReadDataFromHDF5File(THDF5_File & HDF5_File,
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,
99 pDimensionSizes,
100 pMatrixData
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 */
114 void TRealMatrix::WriteDataToHDF5File(THDF5_File & HDF5_File,
115 const char * MatrixName,
116 const size_t CompressionLevel)
117 {
118 TDimensionSizes Chunks = pDimensionSizes;
119 Chunks.Z = 1;
120
121 //1D matrices
122 if ((pDimensionSizes.Y == 1) && (pDimensionSizes.Z == 1))
123 {
124 // Chunk = 4MB
125 if (pDimensionSizes.X > 4 * ChunkSize_1D_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,
141 pDimensionSizes,
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
175 pTotalElementCount = pDimensionSizes.X *
176 pDimensionSizes.Y *
177 pDimensionSizes.Z;
178
179 pTotalAllocatedElementCount = pTotalElementCount;
180
181 pDataRowSize = pDimensionSizes.X;
182
183 p2DDataSliceSize = pDimensionSizes.X *
184 pDimensionSizes.Y;
185 }// end of SetDimensions
186 //------------------------------------------------------------------------------/
187
188
189 //----------------------------------------------------------------------------//
190 // Implementation //
191 // private methods //
192 //----------------------------------------------------------------------------//
TDimensionSizes::Z
size_t Z
Z dimension size.
Definition: DimensionSizes.h:70
TBaseFloatMatrix
Abstract base class for float based matrices defining basic interface. Higher dimensional matrices st...
Definition: BaseFloatMatrix.h:52
THDF5_File::ReadMatrixDataType
THDF5_File::THDF5_MatrixDataType ReadMatrixDataType(const hid_t ParentGroup, const char *DatasetName)
Read matrix data type from the dataset.
Definition: HDF5_File.cpp:1155
TRealMatrix::ChunkSize_1D_1MB
static const size_t ChunkSize_1D_1MB
Number of elements to get 1MB block of data.
Definition: RealMatrix.h:126
TRealMatrix::WriteDataToHDF5File
virtual void WriteDataToHDF5File(THDF5_File &HDF5_File, const char *MatrixName, const size_t CompressionLevel)
Write data into the HDF5 file.
Definition: RealMatrix.cpp:114
TBaseFloatMatrix::AllocateMemory
virtual void AllocateMemory()
Memory allocation.
Definition: BaseFloatMatrix.cpp:115
RealMatrix.h
The header file containing the class for real matrices.
TBaseFloatMatrix::p2DDataSliceSize
size_t p2DDataSliceSize
Size of a 2D slab (X,Y).
Definition: BaseFloatMatrix.h:119
TRealMatrix::ChunkSize_1D_4MB
static const size_t ChunkSize_1D_4MB
Number of elements to get 4MB block of data.
Definition: RealMatrix.h:124
TRealMatrix::ReadDataFromHDF5File
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
TDimensionSizes::X
size_t X
X dimension size.
Definition: DimensionSizes.h:66
THDF5_File::WriteHyperSlab
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
TBaseFloatMatrix::pTotalAllocatedElementCount
size_t pTotalAllocatedElementCount
Total number of allocated elements (in terms of floats).
Definition: BaseFloatMatrix.h:111
TBaseFloatMatrix::pTotalElementCount
size_t pTotalElementCount
Total number of elements.
Definition: BaseFloatMatrix.h:109
TRealMatrix::InitDimensions
virtual void InitDimensions(const TDimensionSizes &DimensionSizes)
Init dimension.
Definition: RealMatrix.cpp:171
THDF5_File::WriteMatrixDataType
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
THDF5_File::WriteMatrixDomainType
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
TBaseFloatMatrix::pDimensionSizes
struct TDimensionSizes pDimensionSizes
Dimension sizes.
Definition: BaseFloatMatrix.h:114
THDF5_File::GetRootGroup
hid_t GetRootGroup() const
Get handle to the root group.
Definition: HDF5_File.h:581
ErrorMessages.h
The header file containing all error messages of the project.
THDF5_File::CreateFloatDataset
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
TBaseFloatMatrix::pDataRowSize
size_t pDataRowSize
Size of a 1D row in X dimension.
Definition: BaseFloatMatrix.h:117
ComplexMatrix.h
The header file with the class for complex matrices.
THDF5_File::ReadMatrixDomainType
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
TDimensionSizes::Y
size_t Y
Y dimension size.
Definition: DimensionSizes.h:68
THDF5_File::ReadCompleteDataset
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
TRealMatrix::ChunkSize_1D_256KB
static const size_t ChunkSize_1D_256KB
Number of elements to get 256KB block of data.
Definition: RealMatrix.h:128
Matrix_ERR_FMT_MatrixNotFloat
const char *const Matrix_ERR_FMT_MatrixNotFloat
Matrix class error message.
Definition: ErrorMessages.h:83
TBaseFloatMatrix::pMatrixData
float * pMatrixData
Raw matrix data.
Definition: BaseFloatMatrix.h:122
THDF5_File::CloseDataset
void CloseDataset(const hid_t HDF5_Dataset_id)
Close the HDF5 dataset.
Definition: HDF5_File.cpp:451
Matrix_ERR_FMT_MatrixNotReal
const char *const Matrix_ERR_FMT_MatrixNotReal
Matrix class error message.
Definition: ErrorMessages.h:85
TRealMatrix::TRealMatrix
TRealMatrix()
Default constructor is not allowed for public.
Definition: RealMatrix.h:110
THDF5_File
Class wrapping the HDF5 routines.
Definition: HDF5_File.h:506
TDimensionSizes
Structure with 4D dimension sizes (3 in space and 1 in time).
Definition: DimensionSizes.h:63