kspaceFirstOrder3D-OMP  1.2
The C++ implementation of the k-wave toolbox for the time-domain simulation of acoustic wave fields in 3D
OutputStreamContainer.cpp
Go to the documentation of this file.
1 /**
2  * @file OutputStreamContainer.cpp
3  *
4  * @author Jiri Jaros \n
5  * Faculty of Information Technology\n
6  * Brno University of Technology \n
7  * jarosjir@fit.vutbr.cz
8  *
9  * @brief The implementation file for the output stream container.
10  *
11  * @version kspaceFirstOrder3D 2.16
12  *
13  * @date 27 August 2017, 08:59 (created) \n
14  * 04 September 2017, 10:54 (revised)
15  *
16  * @copyright Copyright (C) 2017 Jiri Jaros and Bradley Treeby.
17  *
18  * This file is part of the C++ extension of the [k-Wave Toolbox](http://www.k-wave.org).
19  *
20  * This file is part of the k-Wave. k-Wave is free software: you can redistribute it and/or modify it under the terms
21  * of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the
22  * License, or (at your option) any later version.
23  *
24  * k-Wave is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
25  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
26  * more details.
27  *
28  * You should have received a copy of the GNU Lesser General Public License along with k-Wave.
29  * If not, see [http://www.gnu.org/licenses/](http://www.gnu.org/licenses/).
30  */
31 
32 
33 #include <Parameters/Parameters.h>
35 
40 
41 //--------------------------------------------------------------------------------------------------------------------//
42 //---------------------------------------------------- Constants -----------------------------------------------------//
43 //--------------------------------------------------------------------------------------------------------------------//
44 
45 
46 //--------------------------------------------------------------------------------------------------------------------//
47 //-------------------------------------------------- Public methods --------------------------------------------------//
48 //--------------------------------------------------------------------------------------------------------------------//
49 
50 /**
51  * Default constructor.
52  */
54  : mContainer()
55 {
56 
57 }// end of OutputStreamContainer
58 //----------------------------------------------------------------------------------------------------------------------
59 
60 
61 /**
62  * Destructor
63  */
65 {
66  mContainer.clear();
67 }// end of Destructor.
68 //----------------------------------------------------------------------------------------------------------------------
69 
70 /**
71  * Add all streams in simulation in the container, set all streams records here!
72  */
74 {
75 
77 
78  // shortcuts for long data types
79  using OI = OutputStreamIdx;
80  using MI = MatrixContainer::MatrixIdx;
82 
83 
84  float* tempBuffX = matrixContainer.getMatrix<RealMatrix>(MI::kTemp1Real3D).getData();
85  float* tempBuffY = matrixContainer.getMatrix<RealMatrix>(MI::kTemp2Real3D).getData();
86  float* tempBuffZ = matrixContainer.getMatrix<RealMatrix>(MI::kTemp3Real3D).getData();
87 
88  //-------------------------------------------------- pressure ------------------------------------------------------//
89  if (params.getStorePressureRawFlag())
90  {
91  mContainer[OI::kPressureRaw] = createOutputStream(matrixContainer, MI::kP, kPressureRawName, RO::kNone, tempBuffX);
92  }// IsStore_p_raw
93 
94  if (params.getStorePressureRmsFlag())
95  {
96  mContainer[OI::kPressureRms] = createOutputStream(matrixContainer, MI::kP, kPressureRmsName, RO::kRms);
97  }
98 
99  if (params.getStorePressureMaxFlag())
100  {
101  mContainer[OI::kPressureMax] = createOutputStream(matrixContainer, MI::kP, kPressureMaxName, RO::kMax);
102  }
103 
104  if (params.getStorePressureMinFlag())
105  {
106  mContainer[OI::kPressureMin] = createOutputStream(matrixContainer, MI::kP, kPressureMinName, RO::kMin);
107  }
108 
109  if (params.getStorePressureMaxAllFlag())
110  {
111  mContainer[OI::kPressureMaxAll] = new WholeDomainOutputStream(params.getOutputFile(),
113  matrixContainer.getMatrix<RealMatrix>(MI::kP),
114  RO::kMax);
115  }
116 
117  if (params.getStorePressureMinAllFlag())
118  {
119  mContainer[OI::kPressureMinAll] = new WholeDomainOutputStream(params.getOutputFile(),
121  matrixContainer.getMatrix<RealMatrix>(MI::kP),
122  RO::kMin);
123  }
124 
125  //-------------------------------------------------- velocity ------------------------------------------------------//
126  if (params.getStoreVelocityRawFlag())
127  {
128  mContainer[OI::kVelocityXRaw] = createOutputStream(matrixContainer, MI::kUxSgx, kUxName, RO::kNone, tempBuffX);
129  mContainer[OI::kVelocityYRaw] = createOutputStream(matrixContainer, MI::kUySgy, kUyName, RO::kNone, tempBuffY);
130  mContainer[OI::kVelocityZRaw] = createOutputStream(matrixContainer, MI::kUzSgz, kUzName, RO::kNone, tempBuffZ);
131  }
132 
134  {
135  mContainer[OI::kVelocityXNonStaggeredRaw] = createOutputStream(matrixContainer,
136  MI::kUxShifted,
138  RO::kNone,
139  tempBuffX);
140  mContainer[OI::kVelocityYNonStaggeredRaw] = createOutputStream(matrixContainer,
141  MI::kUyShifted,
143  RO::kNone,
144  tempBuffY);
145  mContainer[OI::kVelocityZNonStaggeredRaw] = createOutputStream(matrixContainer,
146  MI::kUzShifted,
148  RO::kNone,
149  tempBuffZ);
150  }
151 
152  if (params.getStoreVelocityRmsFlag())
153  {
154  mContainer[OI::kVelocityXRms] = createOutputStream(matrixContainer, MI::kUxSgx, kUxRmsName, RO::kRms);
155  mContainer[OI::kVelocityYRms] = createOutputStream(matrixContainer, MI::kUySgy, kUyRmsName, RO::kRms);
156  mContainer[OI::kVelocityZRms] = createOutputStream(matrixContainer, MI::kUzSgz, kUzRmsName, RO::kRms);
157  }
158 
159  if (params.getStoreVelocityMaxFlag())
160  {
161  mContainer[OI::kVelocityXMax] = createOutputStream(matrixContainer, MI::kUxSgx, kUxMaxName, RO::kMax);
162  mContainer[OI::kVelocityYMax] = createOutputStream(matrixContainer, MI::kUySgy, kUyMaxName, RO::kMax);
163  mContainer[OI::kVelocityZMax] = createOutputStream(matrixContainer, MI::kUzSgz, kUzMaxName, RO::kMax);
164  }
165 
166  if (params.getStoreVelocityMinFlag())
167  {
168  mContainer[OI::kVelocityXMin] = createOutputStream(matrixContainer, MI::kUxSgx, kUxMinName, RO::kMin);
169  mContainer[OI::kVelocityYMin] = createOutputStream(matrixContainer, MI::kUySgy, kUyMinName, RO::kMin);
170  mContainer[OI::kVelocityZMin] = createOutputStream(matrixContainer, MI::kUzSgz, kUzMinName, RO::kMin);
171  }
172 
173  if (params.getStoreVelocityMaxAllFlag())
174  {
175  mContainer[OI::kVelocityXMaxAll] =
178  matrixContainer.getMatrix<RealMatrix>(MI::kUxSgx),
179  RO::kMax);
180  mContainer[OI::kVelocityYMaxAll] =
183  matrixContainer.getMatrix<RealMatrix>(MI::kUySgy),
184  RO::kMax);
185  mContainer[OI::kVelocityZMaxAll] =
188  matrixContainer.getMatrix<RealMatrix>(MI::kUzSgz),
189  RO::kMax);
190  }
191 
192  if (params.getStoreVelocityMinAllFlag())
193  {
194  mContainer[OI::kVelocityXMinAll] =
197  matrixContainer.getMatrix<RealMatrix>(MI::kUxSgx),
198  RO::kMin);
199  mContainer[OI::kVelocityYMinAll] =
202  matrixContainer.getMatrix<RealMatrix>(MI::kUySgy),
203  RO::kMin);
204  mContainer[OI::kVelocityZMinAll] =
207  matrixContainer.getMatrix<RealMatrix>(MI::kUzSgz),
208  RO::kMin);
209  }
210 }// end of addStreams
211 //----------------------------------------------------------------------------------------------------------------------
212 
213 /**
214  * Create all streams.
215  */
217 {
218  for (const auto& it : mContainer)
219  {
220  if (it.second)
221  {
222  it.second->create();
223  }
224  }
225 }// end of createStreams
226 //----------------------------------------------------------------------------------------------------------------------
227 
228 /**
229  * Reopen all streams after restarting from checkpoint.
230  */
232 {
233  for (const auto& it : mContainer)
234  {
235  if (it.second)
236  {
237  it.second->reopen();
238  }
239  }
240 }// end of reopenStreams
241 //----------------------------------------------------------------------------------------------------------------------
242 
243 /**
244  * Sample all streams.
245  */
247 {
248  for (const auto& it : mContainer)
249  {
250  if (it.second)
251  {
252  it.second->sample();
253  }
254  }
255 }// end of sampleStreams
256 //----------------------------------------------------------------------------------------------------------------------
257 
258 /**
259  * Checkpoint streams without post-processing (flush to the file).
260  */
262 {
263  for (const auto& it : mContainer)
264  {
265  if (it.second)
266  {
267  it.second->checkpoint();
268  }
269  }
270 }// end of checkpointStreams
271 //----------------------------------------------------------------------------------------------------------------------
272 
273 /**
274  * Post-process all streams and flush them to the file.
275  */
277 {
278  for (const auto& it : mContainer)
279  {
280  if (it.second)
281  {
282  it.second->postProcess();
283  }
284  }
285 }// end of postProcessStreams
286 //----------------------------------------------------------------------------------------------------------------------
287 
288 /**
289  * Close all streams (apply post-processing if necessary, flush data and close).
290  */
292 {
293  for (const auto& it : mContainer)
294  {
295  if (it.second)
296  {
297  it.second->close();
298  }
299  }
300 }// end of closeStreams
301 //----------------------------------------------------------------------------------------------------------------------
302 
303 /**
304  * Free all streams - destroy them.
305  */
307 {
308  for (auto& it : mContainer)
309  {
310  if (it.second)
311  {
312  delete it.second;
313  it.second = nullptr;
314  }
315  }
316  mContainer.clear();
317 }// end of freeStreams
318 //----------------------------------------------------------------------------------------------------------------------
319 
320 //--------------------------------------------------------------------------------------------------------------------//
321 //------------------------------------------------ Protected methods -------------------------------------------------//
322 //--------------------------------------------------------------------------------------------------------------------//
323 
324 /**
325  * Create a new output stream.
326  */
328  const MatrixContainer::MatrixIdx sampledMatrixIdx,
329  const MatrixName& fileObjectName,
330  const BaseOutputStream::ReduceOperator reduceOp,
331  float* bufferToReuse)
332 {
333  using MatrixIdx = MatrixContainer::MatrixIdx;
334 
336 
337  BaseOutputStream* stream = nullptr;
338 
340  {
341  stream = new IndexOutputStream(params.getOutputFile(),
342  fileObjectName,
343  matrixContainer.getMatrix<RealMatrix>(sampledMatrixIdx),
344  matrixContainer.getMatrix<IndexMatrix>(MatrixIdx::kSensorMaskIndex),
345  reduceOp,
346  bufferToReuse);
347  }
348  else
349  {
350  stream = new CuboidOutputStream(params.getOutputFile(),
351  fileObjectName,
352  matrixContainer.getMatrix<RealMatrix>(sampledMatrixIdx),
353  matrixContainer.getMatrix<IndexMatrix>(MatrixIdx::kSensorMaskCorners),
354  reduceOp,
355  bufferToReuse);
356  }
357 
358  return stream;
359 }// end of createNewOutputStream
360 //----------------------------------------------------------------------------------------------------------------------
361 
362 //--------------------------------------------------------------------------------------------------------------------//
363 //------------------------------------------------- Private methods --------------------------------------------------//
364 //--------------------------------------------------------------------------------------------------------------------//
MatrixName kUzName
uz variable name
Definition: MatrixNames.h:198
Output stream for quantities sampled by an index sensor mask.
The class for real matrices.
Definition: RealMatrix.h:47
void postProcessStreams()
Post-process all streams and flush them to the file.
MatrixName kPressureMinName
p_min variable name
Definition: MatrixNames.h:274
MatrixName kUzMinName
uz_min variable name
Definition: MatrixNames.h:300
Class implementing the matrix container.
OutputStreamIdx
Output streams identifiers in k-Wave.
MatrixName kUxRmsName
ux_rms variable name
Definition: MatrixNames.h:283
MatrixName kUxMinName
ux_min variable name
Definition: MatrixNames.h:296
MatrixName kUyMaxName
uy_max variable name
Definition: MatrixNames.h:292
MatrixName kUzNonStaggeredName
uz_non_staggered variable name
Definition: MatrixNames.h:212
bool getStoreVelocityMinAllFlag() const
Is –u_min set?
Definition: Parameters.h:575
Class storing all parameters of the simulation.
Definition: Parameters.h:50
MatrixName kPressureMaxAllName
p_max_all variable name
Definition: MatrixNames.h:276
Hdf5File & getOutputFile()
Get output file handle.
Definition: Parameters.h:158
bool getStoreVelocityMinFlag() const
Is –u_min set?
Definition: Parameters.h:565
bool getStoreVelocityRawFlag() const
Is -u or –u_raw specified at the command line?
Definition: Parameters.h:542
The header file containing the parameters of the simulation.
bool getStorePressureMinAllFlag() const
Is –p_min_all set?
Definition: Parameters.h:530
void reopenStreams()
Reopen streams after checkpoint file (datasets).
MatrixName kUyName
uy variable name
Definition: MatrixNames.h:196
void addStreams(MatrixContainer &matrixContainer)
Add all streams in simulation in the container, set all streams records here!
MatrixName kUzMinAllName
uz_min_all variable name
Definition: MatrixNames.h:313
bool getStoreVelocityNonStaggeredRawFlag() const
Is –u_non_staggered_raw set?
Definition: Parameters.h:547
void freeStreams()
Free all streams - destroy them.
OutputStreamContainer()
Constructor.
Output stream for quantities sampled in the whole domain.
MatrixName kPressureRawName
p variable name in the output file
Definition: MatrixNames.h:268
MatrixName kUxNonStaggeredName
ux_non_staggered variable name
Definition: MatrixNames.h:208
The header file of classes responsible for storing output quantities based on the cuboid sensor mask ...
MatrixName kUxMaxAllName
ux_max_all variable name
Definition: MatrixNames.h:303
void sampleStreams()
Sample all streams.
bool getStoreVelocityRmsFlag() const
Is –u_rms set?
Definition: Parameters.h:555
void createStreams()
Create all streams - opens the datasets.
bool getStoreVelocityMaxFlag() const
Is –u_max set?
Definition: Parameters.h:560
MatrixName kUxMaxName
ux_max variable name
Definition: MatrixNames.h:290
const std::string MatrixName
Datatype for matrix names.
Definition: MatrixNames.h:39
MatrixName kUzRmsName
uz_rms variable name
Definition: MatrixNames.h:287
MatrixIdx
Matrix identifers of all matrices in the k-space code.
bool getStorePressureMaxAllFlag() const
Is –p_max_all set?
Definition: Parameters.h:525
SensorMaskType getSensorMaskType() const
Get sensor mask type (linear or corners).
Definition: Parameters.h:484
MatrixName kUxName
ux variable name
Definition: MatrixNames.h:194
MatrixName kUyMinAllName
uy_min_all variable name
Definition: MatrixNames.h:311
MatrixName kUyMaxAllName
uy_max_all variable name
Definition: MatrixNames.h:305
bool getStorePressureRawFlag() const
Is -p or –p_raw specified at the command line?
Definition: Parameters.h:505
ReduceOperator
How to aggregate data.
T & getMatrix(const MatrixIdx matrixIdx)
Get the matrix with a specific type from the container.
std::map< OutputStreamIdx, BaseOutputStream * > mContainer
Map with output streams.
MatrixName kPressureMaxName
p_max variable name
Definition: MatrixNames.h:272
The header file of the class saving RealMatrix data into the output HDF5 file.
MatrixName kUzMaxName
uz_max variable name
Definition: MatrixNames.h:294
MatrixName kPressureMinAllName
p_min_all variable name
Definition: MatrixNames.h:278
MatrixName kUyMinName
uy_min variable name
Definition: MatrixNames.h:298
MatrixName kPressureRmsName
p_rms variable name
Definition: MatrixNames.h:270
bool getStoreVelocityMaxAllFlag() const
Is –u_max_all set?
Definition: Parameters.h:570
The header file defining the output stream container.
bool getStorePressureMaxFlag() const
Is –p_max set?
Definition: Parameters.h:515
static Parameters & getInstance()
Get instance of the singleton class.
Definition: Parameters.cpp:84
bool getStorePressureMinFlag() const
Is –p_min set?
Definition: Parameters.h:520
The header file of the class saving whole RealMatrix into the output HDF5 file, e.g. p_max_all.
MatrixName kUxMinAllName
ux_min_all variable name
Definition: MatrixNames.h:309
void closeStreams()
Close all streams.
void checkpointStreams()
Checkpoint streams.
The class for 64b unsigned integers (indices). It is used for linear and cuboid corners masks to get ...
Definition: IndexMatrix.h:47
MatrixName kUzMaxAllName
uz_max_all variable name
Definition: MatrixNames.h:307
MatrixName kUyNonStaggeredName
uy_non_staggered variable name
Definition: MatrixNames.h:210
Abstract base class for output data streams (sampled data).
The header file of the class saving data based on the index senor mask into the output HDF5 file...
MatrixName kUyRmsName
uy_rms variable name
Definition: MatrixNames.h:285
BaseOutputStream * createOutputStream(MatrixContainer &matrixContainer, const MatrixContainer::MatrixIdx sampledMatrixIdx, const MatrixName &fileObjectName, const BaseOutputStream::ReduceOperator reduceOp, float *bufferToReuse=nullptr)
Create a new output stream.
bool getStorePressureRmsFlag() const
Is –p_rms set?
Definition: Parameters.h:510
Output stream for quantities sampled by a cuboid corner sensor mask.