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
ComplexMatrix.cpp
Go to the documentation of this file.
1 /**
2  * @file ComplexMatrix.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 with the class for complex matrices.
9  *
10  * @version kspaceFirstOrder3D 2.15
11  *
12  * @date 11 July 2011, 14:02 (created) \n
13  * 25 September 2014, 12:35 (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 #include <iostream>
34 
36 
37 #include <Utils/ErrorMessages.h>
38 
39 //----------------------------------------------------------------------------//
40 // Constants //
41 //----------------------------------------------------------------------------//
42 
43 //----------------------------------------------------------------------------//
44 // Definitions //
45 //----------------------------------------------------------------------------//
46 
47 
48 //----------------------------------------------------------------------------//
49 // Implementation //
50 // public methods //
51 //----------------------------------------------------------------------------//
52 
53 /**
54  * Constructor.
55  * @param [in] DimensionSizes - Dimension sizes
56  */
57 
60 {
61  InitDimensions(DimensionSizes);
62 
64 } // end of TComplexMatrixData
65 //-----------------------------------------------------------------------------
66 
67 
68 /**
69  * Read data from HDF5 file (do some basic checks). Only from the root group.
70  * \throw ios::failure when there is a problem
71  *
72  * @param [in] HDF5_File - HDF5 file
73  * @param [in] MatrixName - HDF5 dataset name
74  */
76  const char * MatrixName)
77 {
78  // check data type
79  if (HDF5_File.ReadMatrixDataType(HDF5_File.GetRootGroup(), MatrixName) != THDF5_File::hdf5_mdt_float)
80  {
81  char ErrorMessage[256];
82  sprintf(ErrorMessage, Matrix_ERR_FMT_MatrixNotFloat, MatrixName);
83  throw ios::failure(ErrorMessage);
84  }
85 
86  // check domain type
87  if (HDF5_File.ReadMatrixDomainType(HDF5_File.GetRootGroup(), MatrixName) != THDF5_File::hdf5_mdt_complex)
88  {
89  char ErrorMessage[256];
90  sprintf(ErrorMessage, Matrix_ERR_FMT_MatrixNotComplex, MatrixName);
91  throw ios::failure(ErrorMessage);
92  }
93 
94  // Initialise dimensions
95  TDimensionSizes ComplexDims = pDimensionSizes;
96  ComplexDims.X = 2 * ComplexDims.X;
97 
98  // Read data from the file
99  HDF5_File.ReadCompleteDataset(HDF5_File.GetRootGroup(),
100  MatrixName,
101  ComplexDims,
102  pMatrixData);
103 }// end of LoadDataFromMatlabFile
104 //------------------------------------------------------------------------------
105 
106 /**
107  * Write data to HDF5 file (only from the root group).
108  * \throw ios::failure an exception what the operation fails
109  *
110  * @param [in] HDF5_File - HDF5 file handle
111  * @param [in] MatrixName - HDF5 dataset name
112  * @param [in] CompressionLevel - Compression level for the dataset
113  */
115  const char * MatrixName,
116  const size_t CompressionLevel)
117 {
118  // set dimensions and chunks
119  TDimensionSizes ComplexDims = pDimensionSizes;
120  ComplexDims.X = 2 * ComplexDims.X;
121 
122  TDimensionSizes Chunks = ComplexDims;
123  ComplexDims.Z = 1;
124 
125  // create a dataset
126  hid_t HDF5_Dataset_id = HDF5_File.CreateFloatDataset(HDF5_File.GetRootGroup(),
127  MatrixName,
128  ComplexDims,
129  Chunks,
130  CompressionLevel);
131  // Write write the matrix at once.
132  HDF5_File.WriteHyperSlab(HDF5_Dataset_id,
133  TDimensionSizes(0, 0, 0),
135  pMatrixData);
136  HDF5_File.CloseDataset(HDF5_Dataset_id);
137 
138  // Write data and domain type
139  HDF5_File.WriteMatrixDataType(HDF5_File.GetRootGroup(),
140  MatrixName,
141  THDF5_File::hdf5_mdt_float);
142 
143  HDF5_File.WriteMatrixDomainType(HDF5_File.GetRootGroup(),
144  MatrixName,
145  THDF5_File::hdf5_mdt_complex);
146 }// end of WriteDataToHDF5File
147 //---------------------------------------------------------------------------
148 
149 
150 
151 
152 //----------------------------------------------------------------------------//
153 // Implementation //
154 // protected methods //
155 //----------------------------------------------------------------------------//
156 /**
157  * Initialize matrix dimension sizes.
158  * @param [in] DimensionSizes
159  */
161 {
162 
163  pDimensionSizes = DimensionSizes;
164 
168 
169  pDataRowSize = (pDimensionSizes.X << 1);
170 
172  pDimensionSizes.Y) << 1;
173 
174  // compute actual necessary memory sizes
176 
177 }// end of InitDimensions
178 //------------------------------------------------------------------------------
179 
180 //----------------------------------------------------------------------------//
181 // Implementation //
182 // private methods //
183 //----------------------------------------------------------------------------//
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
virtual void WriteDataToHDF5File(THDF5_File &HDF5_File, const char *MatrixName, const size_t CompressionLevel)
Write data into the HDF5_File.
virtual void ReadDataFromHDF5File(THDF5_File &HDF5_File, const char *MatrixName)
Load data from the HDF5_File.
virtual void AllocateMemory()
Memory allocation.
size_t p2DDataSliceSize
Size of a 2D slab (X,Y).
virtual void InitDimensions(const TDimensionSizes &DimensionSizes)
Initialize dimension sizes and related structures.
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.
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
TComplexMatrix()
Default constructor not allowed for public.
TDimensionSizes()
Default constructor.
const char *const Matrix_ERR_FMT_MatrixNotComplex
Matrix class error message.
Definition: ErrorMessages.h:87
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
Class wrapping the HDF5 routines.
Definition: HDF5_File.h:506
Structure with 4D dimension sizes (3 in space and 1 in time).