LifeV
HeartUtility.hpp
Go to the documentation of this file.
1 //@HEADER
2 /*
3  *******************************************************************************
4 
5  Copyright (C) 2004, 2005, 2007 EPFL, Politecnico di Milano, INRIA
6  Copyright (C) 2010 EPFL, Politecnico di Milano, Emory University
7 
8  This file is part of LifeV.
9 
10  LifeV is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  LifeV is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with LifeV. If not, see <http://www.gnu.org/licenses/>.
22 
23  *******************************************************************************
24  */
25 //@HEADER
26 
27 /*!
28  @file
29  @brief Utilities
30 
31  @contributor Simone Rossi <simone.rossi@epfl.ch>
32  @maintainer Simone Rossi <simone.rossi@epfl.ch>
33 
34  This file contains a set of base utilities used to read vectorial data (mainly fiber
35  and sheet directions) from different formats to VectorEpetra objects.
36  Also other useful functions are collected here.
37  */
38 
39 #ifndef HEARTUTILITY_H
40 #define HEARTUTILITY_H 1
41 
42 #include <lifev/core/LifeV.hpp>
43 #include <lifev/core/array/VectorEpetra.hpp>
44 #include <lifev/core/array/MatrixEpetra.hpp>
45 #include <lifev/core/array/MapEpetra.hpp>
46 #include <lifev/core/filter/ExporterEnsight.hpp>
47 #include <lifev/core/filter/ExporterHDF5.hpp>
48 #include <lifev/core/filter/ExporterEmpty.hpp>
49 #include <lifev/core/filter/Exporter.hpp>
50 #include <lifev/core/mesh/RegionMesh.hpp>
51 #include <lifev/core/fem/FESpace.hpp>
52 
53 namespace LifeV
54 {
55 
56 // Predeclaration
57 
59 {
60 
61 //! HeartUtility
62 /*!
63  * @author(s) Simone Rossi
64  *
65  * \c HeartUtility contains methods for reading vectorial data from different formats to VectorEpetra objects.
66  *
67  */
68 
69 //! @name Methods
70 //@{
71 
72 //! Read fiber vector field from HDF5 file
73 /*!
74  * @param fiberVector VectorEpetra object for storing the vector field
75  * @param fileName Name of the HDF5 file to read from
76  * @param localMesh Pointer to the mesh
77  */
78 template<typename Mesh> inline void importFibers ( std::shared_ptr<VectorEpetra> fiberVector, const std::string& fileName, std::shared_ptr< Mesh > localMesh, const std::string& filePath = "./" )
79 {
80  typedef Mesh mesh_Type;
85 
86 
89 
92 
98  importer -> closeFile();
99 
100 }
101 
102 //! Read scalar field from HDF5 file
103 /*!
104  * @param vector VectorEpetra object for storing the scalar field
105  * @param fileName Name of the HDF5 file to read from
106  * @param fieldName Name of the scalar field in the HDF5 file
107  * @param localMesh Pointer to the mesh
108  */
110 {
111  typedef Mesh mesh_Type;
116 
117 
120 
123 
128  importer -> closeFile();
129 
130 }
131 
132 //! Read vector field from HDF5 file
133 /*!
134  * @param vector VectorEpetra object for storing the scalar field
135  * @param fileName Name of the HDF5 file to read from
136  * @param fieldName Name of the vector field in the HDF5 file
137  * @param localMesh Pointer to the mesh
138  */
139 template<typename Mesh> inline void importVectorField ( std::shared_ptr<VectorEpetra> vector, const std::string& fileName, const std::string& fieldName, std::shared_ptr< Mesh > localMesh , const std::string& postDir = "./")
140 {
141  typedef Mesh mesh_Type;
146 
147 
150 
153 
159  importer -> closeFile();
160 
161 }
162 
163 //! Read fiber field from text file
164 /*!
165  * @param fiberVector VectorEpetra object for storing the vector field
166  * @param fileName Name of the HDF5 file to read from
167  * @param filePath Path of the HDF5 file to read from
168  * @param format The fibers can be in the text file in two different formats:
169  *
170  * format 0 = fibers saved as (fx, fy, fz) in each row
171  *
172  * format 1 = fibers saved as fx in each row for all the mesh
173  * fy in each row for all the mesh
174  * fz in each row for all the mesh
175  */
177 {
178  typedef VectorEpetra vector_Type;
180 
182 
185 
186  // Importing fibers
187  for ( UInt i = 0; i < NumGlobalElements; ++i)
188  {
190  if ( fiber_global_vector[i] == 0 )
191  {
192  std::cout << "\nzero component!!!! \t";
193  std::cout << "in: " << filePath + fileName << "\n";
194 
195  }
196  }
197  int n = (*fiberVector).epetraVector().MyLength();
198  int d = n / 3;
199  int i (0);
200  int j (0);
201  int k (0);
202  int offset = (*fiberVector).size() / 3;
203 
204  for (int l = 0; l < d; ++l)
205  {
206  i = (*fiberVector).blockMap().GID (l);
207  j = (*fiberVector).blockMap().GID (l + d);
208  k = (*fiberVector).blockMap().GID (l + 2 * d);
209  if ( format == 0 )
210  {
211  (*fiberVector) [i] = fiber_global_vector[3 * i];
212  (*fiberVector) [j] = fiber_global_vector[3 * i + 1];
213  (*fiberVector) [k] = fiber_global_vector[3 * i + 2];
214  }
215  else
216  {
217 
220  (*fiberVector) [k] = fiber_global_vector[ i + 2 * offset ];
221  }
222 
223  //normalizing
224  Real norm = std::sqrt ( (*fiberVector) [i] * (*fiberVector) [i] + (*fiberVector) [j] * (*fiberVector) [j] + (*fiberVector) [k] * (*fiberVector) [k] );
225  if ( norm != 0 )
226  {
227  (*fiberVector) [i] = (*fiberVector) [i] / norm;
228  (*fiberVector) [j] = (*fiberVector) [j] / norm;
229  (*fiberVector) [k] = (*fiberVector) [k] / norm;
230  }
231  else
232  {
233  std::cout << "\n\nThe fiber vector in the node: " << i << " has component:";
234  std::cout << "\nx: " << fiber_global_vector [i];
235  std::cout << "\ny: " << fiber_global_vector [i + offset];
236  std::cout << "\nz: " << fiber_global_vector [i + 2 * offset];
237  std::cout << "\nI will put it to: (f_x, f_y, f_z) = (1, 0, 0)\n\n";
238 
239  (*fiberVector) [i] = 1.;
240  (*fiberVector) [j] = 0.;
241  (*fiberVector) [k] = 0.;
242  }
243 
244 
245 
246  }
247 
249 
250 
251 }
252 
253 //! Setup fiber field from unidirectional VectorSmall object
254 /*!
255  * @param fiberVector VectorEpetra object for storing the vector field
256  * @param fiberDirection Direction of fiber vectors as a VectorSmall
257  */
259 {
261  int d1 = n1 / 3;
262  fiberVector *= 0;
263  int i (0);
264  int j (0);
265  int k (0);
266 
267  for ( int l (0); l < d1; l++)
268  {
269  i = fiberVector.blockMap().GID (l);
270  j = fiberVector.blockMap().GID (l + d1);
271  k = fiberVector.blockMap().GID (l + 2 * d1);
275  }
276 
277 }
278 
279 //! Setup fiber field from unidirectional std::vector object
280 /*!
281  * @param fiberVector VectorEpetra object for storing the vector field
282  * @param fiberDirection Direction of fiber vectors as a VectorSmall
283  */
285 {
291 }
292 
293 //! Setup fiber field from three real components
294 /*!
295  * @param fiberVector VectorEpetra object for storing the vector field
296  * @param fx First component of vector
297  * @param fy Second component of vector
298  * @param fz Third component of vector
299  */
301 {
303  fiberVectorSmall[0] = fx;
304  fiberVectorSmall[1] = fy;
305  fiberVectorSmall[2] = fz;
307 }
308 
309 
310 //! On the boundary with the specified flag, set the wanted value
311 /*!
312  * @param vec VectorEpetra object where we want to impose the boundary value
313  * @param fullMesh Pointer to the non partitioned mesh
314  * @param value value to set on that boundary
315  * @param flag flag of the boundary
316  */
318 {
319 
320  for ( Int j (0); j < vec.epetraVector().MyLength() ; ++j )
321  {
322  if ( fullMesh -> point ( vec.blockMap().GID (j) ).markerID() == flag )
323  {
324  if ( vec.blockMap().LID ( vec.blockMap().GID (j) ) != -1 )
325  {
326  (vec) ( vec.blockMap().GID (j) ) = value;
327  }
328  }
329  }
330 }
331 
332 
333 //! On the boundary with the specified flags, set the wanted value
334 /*!
335  * @param vec VectorEpetra object where we want to impose the boundary value
336  * @param fullMesh Pointer to the non partitioned mesh
337  * @param value value to set on that boundary
338  * @param flags flags of the boundary
339  */
341 {
342  for ( int j (0); j < vec.epetraVector().MyLength() ; ++j )
343  {
344  for ( UInt k (0); k < flags.size(); k++ )
345  {
346  if ( fullMesh -> point ( vec.blockMap().GID (j) ).markerID() == flags.at (k) )
347  {
348  if ( vec.blockMap().LID ( vec.blockMap().GID (j) ) != -1 )
349  {
350  (vec) ( vec.blockMap().GID (j) ) = value;
351  }
352  }
353  }
354  }
355 }
356 
357 //! Rescale a scalar field to be between requested bounds
358 /*!
359  * @param vector VectorEpetra object that contains the scalar field
360  * @param minValue Minimum value
361  * @param maxValue Maximum value
362  * @param scaleFactor Additional scaling factor (defaults to 1)
363  */
365 {
366  vector -= minValue;
367  vector *= ( scaleFactor / ( maxValue - minValue ) );
368 }
369 
370 //! Rescale a scalar field by a constant factor
371 /*!
372  * @param vector VectorEpetra object that contains the scalar field
373  * @param scaleFactor Additional scaling factor (defaults to 1)
374  */
376 {
377  Real max = vector.maxValue();
378  Real min = vector.minValue();
380 }
381 
382 //! Rescale a scalar field by a constant factor on a boundary
383 /*!
384  * @param vector VectorEpetra object that contains the scalar field
385  * @param fullMesh pointer to the non partitioned mesh
386  * @param flag flag of the boundary
387  * @param scaleFactor Additional scaling factor (defaults to 1)
388  */
390 {
391  for ( Int j (0); j < vector.epetraVector().MyLength() ; ++j )
392  {
393  if ( fullMesh -> point ( vector.blockMap().GID (j) ).markerID() == flag )
394  {
395  if ( vector.blockMap().LID ( vector.blockMap().GID (j) ) != -1 )
396  {
397  (vector) ( vector.blockMap().GID (j) ) *= scaleFactor;
398  }
399  }
400  }
401 }
402 
403 //! Normalizes a vector field to unit length
404 /*!
405  * @param vector VectorEpetra object that contains the vector field
406  */
407 inline void normalize ( VectorEpetra& vector )
408 {
409  int n1 = vector.epetraVector().MyLength();
410  int d1 = n1 / 3;
411  int i (0);
412  int j (0);
413  int k (0);
414 
415  for ( int l (0); l < d1; l++)
416  {
417  i = vector.blockMap().GID (l);
418  j = vector.blockMap().GID (l + d1);
419  k = vector.blockMap().GID (l + 2 * d1);
420  Real norm = std::sqrt ( vector[i] * vector[i] + vector[j] * vector[j] + vector[k] * vector[k] );
421  if ( norm != 0 )
422  {
423  (vector) [i] = (vector) [i] / norm;
424  (vector) [j] = (vector) [j] / norm;
425  (vector) [k] = (vector) [k] / norm;
426  }
427  else
428  {
429  std::cout << "\n\nThe vector in the node: " << i << " has component:";
430  std::cout << "\nx: " << vector[i];
431  std::cout << "\ny: " << vector[j];
432  std::cout << "\nz: " << vector[k];
433  std::cout << "\nI will put it to: (v_x, v_y, v_z) = (1, 0, 0)\n\n";
434  }
435 
436  }
437 }
438 
439 
440 //! Add random component to the fibers
441 /*!
442  * @param fiberVector VectorEpetra object for storing the vector field
443  * @param fiberDirection Direction of fiber vectors as a VectorSmall
444  */
445 inline void addNoiseToFibers ( VectorEpetra& fiberVector, Real magnitude = 0.01, std::vector<bool> component = std::vector<bool> (3, true) )
446 {
448  int d1 = n1 / 3;
449  int i (0);
450  int j (0);
451  int k (0);
452 
453  for ( int l (0); l < d1; l++)
454  {
455  if (component[0])
456  {
457  i = fiberVector.blockMap().GID (l);
458  fiberVector [i] += magnitude * (0.01 * ( (std::rand() % 100 ) - 50.0 ) );
459  }
460  if (component[1])
461  {
462  j = fiberVector.blockMap().GID (l + d1);
463  fiberVector [j] += magnitude * (0.01 * ( (std::rand() % 100 ) - 50.0 ) );
464  }
465  if (component[2])
466  {
467  k = fiberVector.blockMap().GID (l + 2 * d1);
468  fiberVector [k] += magnitude * (0.01 * ( (std::rand() % 100 ) - 50.0 ) );
469  }
470  }
471 
473 }
474 
475 
476 
477 typedef std::function < Real (const Real& t,
478  const Real& x,
479  const Real& y,
480  const Real& z,
481  const ID& i ) > function_Type;
483 {
484  typedef Mesh mesh_Type;
489 
490 
493 
496  *vector, 0.0);
497 }
498 //@}
499 
500 } // namespace HeartUtility
501 
502 } // namespace LifeV
503 
504 #endif /* HEARTUTILITY_H */
void setFibersFromFunction(std::shared_ptr< VectorEpetra > vector, std::shared_ptr< Mesh > localMesh, function_Type f)
void addNoiseToFibers(VectorEpetra &fiberVector, Real magnitude=0.01, std::vector< bool > component=std::vector< bool >(3, true))
Add random component to the fibers.
void importVectorField(std::shared_ptr< VectorEpetra > vector, const std::string &fileName, const std::string &fieldName, std::shared_ptr< Mesh > localMesh, const std::string &postDir="./")
Read vector field from HDF5 file.
void importFibers(std::shared_ptr< VectorEpetra > fiberVector, const std::string &fileName, std::shared_ptr< Mesh > localMesh, const std::string &filePath="./")
Read fiber vector field from HDF5 file.
void importScalarField(std::shared_ptr< VectorEpetra > vector, const std::string &fileName, const std::string &fieldName, std::shared_ptr< Mesh > localMesh)
Read scalar field from HDF5 file.
void importFibersFromTextFile(std::shared_ptr< VectorEpetra > fiberVector, std::string fileName, std::string filePath, int format=0)
Read fiber field from text file.