CEPS  24.01
Cardiac ElectroPhysiology Simulator
CoeffReader.hpp
Go to the documentation of this file.
1 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2  This file is part of CEPS.
3 
4  CEPS is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  CEPS is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with CEPS (see file LICENSE at root of project).
16  If not, see <https://www.gnu.org/licenses/>.
17 
18 
19  Copyright 2019-2024 Inria, Universite de Bordeaux
20 
21  Authors, in alphabetical order:
22 
23  Pierre-Elliott BECUE, Florian CARO, Yves COUDIERE(*), Andjela DAVIDOVIC,
24  Charlie DOUANLA-LONTSI, Marc FUENTES, Mehdi JUHOOR, Michael LEGUEBE(*),
25  Pauline MIGERDITICHAN, Valentin PANNETIER(*), Nejib ZEMZEMI.
26  * : currently active authors
27 
28 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
31 #ifndef _COEFF_READER_HPP_
32 #define _COEFF_READER_HPP_
33 
34 #include "common/CepsCommon.hpp"
35 #include "geometry/Geometry.hpp"
36 #include <vtkCellData.h>
37 #include <vtkDataArray.h>
38 #include <vtkDataSet.h>
39 #include <vtkDataSetReader.h>
40 #include <vtkPointData.h>
41 #include <vtkSmartPointer.h>
42 
43 
62 class CoeffReader : public FileReader
63 {
64 
65 public:
66 
68  CoeffReader (const CepsString& fileName, const CepsLocationFlag& loc, Geometry* geom);
69 
71  ~CoeffReader () override;
72 
74  CepsBool
76 
78  void
80 
82  void
84 
86  void
88 
90  CepsBool
91  hasArray(const CepsString &label, CepsInt dim=-1) const;
92 
93 protected:
94 
96  void
98 
100  template<typename _Result>
101  void
102  read(const CepsString &label, DistributedInfos<_Result>& dest);
103 
105  template<typename _Result>
106  void
107  readVtk(const CepsString &label, DistributedInfos<_Result>& dest);
108 
110  template<typename _Result>
111  void
112  readMsh(const CepsString &label, DistributedInfos<_Result>& dest);
113 
114  // /// @brief Share data that has been read on master
115  // void
116  // broadCastData (const CepsString &label);
117 
118 protected:
119 
122 
127 
128  vtkSmartPointer<vtkDataSet> m_vtkData;
129 };
130 
131 template<typename _Result>
132 void
134 {
136  CEPS_SAYS(" Reading coefficient \"" << label << "\" from file " << m_fileName);
137  if (ext == "vtk")
138  readVtk(label,dest);
139  else
140  readMsh(label,dest);
141 
142  return;
143 }
144 
145 template<typename _Result>
146 void
148 {
149 
150  vtkIdType nbData;
151  vtkDataArray *array = nullptr;
152 
154  {
155  nbData = m_vtkData->GetNumberOfCells();
156  array = m_vtkData->GetCellData()->GetArray(label.c_str());
157  }
158  else
159  {
160  nbData = m_vtkData->GetNumberOfPoints();
161  array = m_vtkData->GetPointData()->GetArray(label.c_str());
162  }
163  // array should be found, as its presence was checked beforehand
164 
165  CepsReal* d = ceps::newArray<CepsReal>(9);
166 
167  for (vtkIdType i = 0; i < nbData; ++i)
168  {
169  array->GetTuple(i,d);
170  // Node data, can be owned or halo
171  // Even if the node is halo, we store the data in the "owned"
172  // section of the distributed info. For a much faster access later on
174  {
176  GeomNode* node = m_geom->getNode(gId);
177  if (ceps::isValidPtr(node))
178  dest.add(ceps::convertReal<_Result>(d),gId,gId,ceps::getRank());
179  }
180  // Cell data, owned only
181  else
182  {
184  GeomCell* cell = m_geom->getCell(gId);
185  if (ceps::isValidPtr(cell))
186  dest.add(ceps::convertReal<_Result>(d),gId,gId,ceps::getRank());
187  }
188  }
190 
191 }
192 
193 template<typename _Result>
194 void
196 {
197  // Check that the file contains an array defined where we want
198  // with the correct name. We build a list of all arrays
199  CepsInt nRealTags,nIntTags,nComp,nToRead;
200  CepsString e = "in file " + m_fileName + ":\n ";
201 
202  // Reader "header" information of data field
203  reset();
204  std::stringstream keyword;
205  keyword << "\"" << label << "\"";
206  lineIndex(keyword.str());
207 
208  nRealTags = ceps::readInt (m_file,e+"cannot read number of real tags");
209  for (CepsInt i = 0U; i < nRealTags; i++)
210  ceps::readReal(m_file,e+"cannot read real tag");
211  nIntTags = ceps::readInt (m_file,e+"cannot read number of int tags");
212  ceps::readInt (m_file,e+"cannot read time step index");
213  nComp = ceps::readInt (m_file,"cannot read number of components of coefficients");
214  nToRead = ceps::readInt (m_file,"cannot read number of data to read");
215  for (CepsInt i = 3U; i < nIntTags; i++)
216  ceps::readInt (m_file,e+"cannot read int tag");
217 
218  // Read the data
219  CepsReal* d = ceps::newArray<CepsReal>(9);
220  CepsInt eId;
221  for (CepsInt n = 0U; n < nToRead; n++)
222  {
223  eId = ceps::readInt(m_file,e+"cannot read entity ID");
224  for (CepsInt i = 0; i < nComp; i++)
225  {
226  d[i] = ceps::readReal(m_file,e+"could not read " + ceps::toString(i+1) + "-th value"
227  +" on node/cell with ID " + ceps::toString(eId));
228 
229  }
230  // Node can be owned or halo
232  {
234  GeomNode* node = m_geom->getNode(gId);
235  if (ceps::isValidPtr(node))
236  {
237  dest.add(ceps::convertReal<_Result>(d),gId,gId,node->getOwner());
238  }
239  }
240  // Cells are only owned
241  else
242  {
244  GeomCell* cell = m_geom->getCell(gId);
245  if (ceps::isValidPtr(cell))
246  {
247  dest.add(ceps::convertReal<_Result>(d),gId,gId,ceps::getRank());
248  }
249  }
250  }
252 
253 }
254 
255 #endif //_COEFF_READER_HPP_
CepsLocationFlag
DataLocation: an enum that will be used by various elements of the code (pde, readers,...
Definition: CepsEnums.hpp:108
@ Cell
Data is defined at cell centers.
@ Point
Data is defined on each point.
#define CEPS_SAYS(message)
Writes a message in the debug log and in the terminal (stdio).
std::basic_string< CepsChar > CepsString
C++ format string.
Definition: CepsTypes.hpp:128
std::array< _Type, _N > CepsArray
C++ arrays.
Definition: CepsTypes.hpp:159
bool CepsBool
Booleans.
Definition: CepsTypes.hpp:124
CepsIndex CepsGlobalIndex
Many uses. Has to be signed for PETSc.
Definition: CepsTypes.hpp:218
float CepsReal
Need single precision floating point.
Definition: CepsTypes.hpp:100
int32_t CepsInt
Need 32 bit integer.
Definition: CepsTypes.hpp:106
Reader used to import point or cell data from files.
Definition: CoeffReader.hpp:63
CepsArray< CepsVector< CepsString >, 3 > m_nodeArrayNames
Names of data arrays defined on nodes (array index: scalar, vector, tensor)
void read(const CepsString &label, DistributedInfos< _Result > &dest)
Read the array and stores the data into instance. The label is needed for vtk format.
~CoeffReader() override
Destructor.
Definition: CoeffReader.cpp:70
vtkSmartPointer< vtkDataSet > m_vtkData
Array in the vtk format.
void readMsh(const CepsString &label, DistributedInfos< _Result > &dest)
Read the array with given label in file, stores the data into instance.
void getData(const CepsString &label, DistributedInfos< CepsMathScalar > &dest)
Extract a distributed data structure of scalars with given label.
Definition: CoeffReader.cpp:81
void buildArrayNamesList()
Stores the names of the arrays for easier access afterwards.
void readVtk(const CepsString &label, DistributedInfos< _Result > &dest)
Read the array with given label in file, stores the data into instance.
CepsLocationFlag m_loc
Used to choose between point or cell data.
Geometry * m_geom
Link to geometry.
CepsArray< CepsVector< CepsString >, 3 > m_cellArrayNames
Names of data arrays defined on cells (array index: scalar, vector, tensor)
CoeffReader(const CepsString &fileName, const CepsLocationFlag &loc, Geometry *geom)
Constructor from file name, and location flag, linked to geometry.
Definition: CoeffReader.cpp:33
CepsBool isValidExtension(CepsString ext)
Tells if extension is compatible with reader.
Definition: CoeffReader.cpp:75
CepsBool hasArray(const CepsString &label, CepsInt dim=-1) const
Tells if file has data array, dim = 0 for scalars, 1 for vector, 2 for tensor.
A class that manages data that is distributed between processors, not only real values (as in Distrib...
void add(const _Type &x, const _Hash &hashValue, const CepsGlobalIndex &globalId, const CepsUInt &pId)
Add entry with global ID to the container, hash must be provided, pId selects owned or halo.
Base class that regroups common reader functionalities.
Definition: FileReader.hpp:43
CepsInt lineIndex(const CepsString &word)
Index of first line starting with word, search starting from the current stream position.
Definition: FileReader.cpp:209
void reset()
Set file stream to the beginning of the file.
Definition: FileReader.cpp:98
CepsString m_fileName
file to open
Definition: FileReader.hpp:145
std::ifstream m_file
stream
Definition: FileReader.hpp:146
Abstract class for geometrical cell. On top of index and attributes managament, the cell has informat...
Definition: GeomCell.hpp:48
Base class for nodes used in meshes.
Definition: GeomNode.hpp:53
Encapsulates all the geometrical data.
Definition: Geometry.hpp:50
GeomNode * getNode(CepsNodeGlobalIndex globalID, CepsBool ownedOnly=false)
Returns a pointer to the node with given global ID. Nullptr if node is not owned or halo.
Definition: Geometry.cpp:459
const CepsVector< CepsNodeGlobalIndex > & getNodePartitionMap()
The node index map from before to after partitioning.
Definition: Geometry.cpp:643
GeomCell * getCell(CepsCellGlobalIndex globalID)
Returns a pointer to the cell with given global ID. Nullptr if cell is not owned.
Definition: Geometry.cpp:478
const CepsVector< CepsCellGlobalIndex > & getCellPartitionMap()
The node index map from before to after partitioning.
Definition: Geometry.cpp:649
const CepsProcId & getOwner() const
Get owner (processus id) of this entity.
CepsUInt getRank()
Returns current processor rank.
CepsString toString(_Tp value)
Convert a given value to a string (input has to be compatible with std::to_string)
Definition: CepsString.hpp:409
void destroyTabular(_Type &)
Destroy[delete] any type.
Definition: CepsMemory.hpp:149
CepsBool isValidPtr(_Type *ptr)
Tells if pointer is not null.
Definition: CepsMemory.hpp:61
CepsInt readInt(std::istream &file, const CepsString &errorMessage="")
Reads an integral number from an istream, aborts if conversion fails advances the stream by 1 word.
Definition: CepsString.cpp:677
CepsString getExtension(const CepsString &str)
Returns the extension of a file, if any.
Definition: CepsString.cpp:580
CepsReal readReal(std::istream &file, const CepsString &errorMessage="")
Reads a floating point number from an istream, aborts if conversion fails advances the stream by 1 wo...
Definition: CepsString.cpp:661