CEPS  24.01
Cardiac ElectroPhysiology Simulator
TimeWriter.cpp
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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
33 
35  const CepsString& fileName,
36  const CepsVector<CepsReal3D>& points,
38 ):
39  FileWriter (fileName),
40  m_discr (discr),
41  m_points (points),
42  m_dofIndices ({}),
43  m_customNames({}),
44  m_customData ({}),
45  m_canAddData (true)
46 {
47  // Initialize 0D unknowns
48  for (Unknown* u: discr->getProblem()->getZeroDUnknowns())
49  {
50  DegreeOfFreedom* dof = discr->getDegreesOfFreedomForUnknown(u)[0];
51  m_dofIndices.push_back(ceps::getRank()==dof->getOwner() ? dof->getGlobalIndex() : -1);
52  }
53 
54  // Initialize points
55  for (CepsReal3D point: points)
56  for (Unknown* u : discr->getProblem()->getSpatialUnknowns())
57  {
58  CepsDofGlobalIndex dofId;
59  CepsProcId owner;
60  discr->findClosestDof(point,u,dofId,owner);
61  m_dofIndices.push_back(owner == ceps::getRank()? dofId : -1);
62  }
63 }
64 
67 {
68  return m_dofIndices.size() + m_customData.size() != 0;
69 }
70 
71 void
73 {
75  "Cannot add custom data to TimeWriter after write() has been called"
76  );
77  m_customData .push_back(data);
78  m_customNames.push_back(name);
79 }
80 
81 void
83 {
84 
85  if (m_canAddData)
86  writeHeader();
87 
88  // Extract data from vector
89  data->getLocalData();
90  CepsUInt n = m_dofIndices.size();
91  CepsVector<CepsReal> myToWrite(n,0.),toWrite(n,0.);
92  for (CepsUInt i=0; i<n; i++)
93  if (m_dofIndices[i]!=-1)
94  {
95  myToWrite[i] = (*data)[m_dofIndices[i]];
96  }
97  data->releaseLocalData();
98 
99  MPI_Allreduce(myToWrite.data(),toWrite.data(),n,CEPS_MPI_REAL,MPI_SUM,ceps::getCommunicator()),
100 
101  m_file << std::setprecision(16);
102  m_file << t;
103  for (CepsReal rdata: toWrite)
104  m_file << " " << rdata;
105  for (CepsReal* data : m_customData)
106  m_file << " " << *data;
107  m_file << std::endl;
108 
109 }
110 
111 
112 void
114 {
115 
116  open();
117 
118  // Write header
119  m_file << "# Time dependant data generated by CEPS\n"
120  << "# Columns are:\n"
121  << "# 1. Time\n";
122  CepsUInt i=1;
124  {
125  i++;
126  m_file << "# " << i << ". " << u->getName() << "\n";
127  }
128  for (CepsReal3D point: m_points)
130  {
131  i++;
132  m_file << "# " << i << ". " << u->getName() << " at closest point to (" << point[0]
133  << "," << point[1] << "," << point[2] << ")" << "\n";
134  }
135  for (CepsString cn : m_customNames)
136  {
137  i++;
138  m_file << "# " << i << ". " << cn << "\n";
139  }
140  m_file << std::endl;
141 
142  m_canAddData = false;
143 }
#define CEPS_ABORT_IF(condition, message)
Stops the execution with a message if condition is true. If testing is enabled, only throws a runtime...
std::basic_string< CepsChar > CepsString
C++ format string.
Definition: CepsTypes.hpp:128
std::vector< _Type, _Alloc > CepsVector
C++ vector.
Definition: CepsTypes.hpp:155
bool CepsBool
Booleans.
Definition: CepsTypes.hpp:124
#define CEPS_MPI_REAL
Definition: CepsTypes.hpp:315
CepsGlobalIndex CepsDofGlobalIndex
Indices of degrees of freedom.
Definition: CepsTypes.hpp:226
std::make_unsigned_t< CepsInt > CepsUInt
Unsigned version on CepsInt.
Definition: CepsTypes.hpp:109
float CepsReal
Need single precision floating point.
Definition: CepsTypes.hpp:100
CepsUInt CepsProcId
For CPU indices.
Definition: CepsTypes.hpp:123
CepsArray3< CepsReal > CepsReal3D
Three real scalars, used like this for compatibility in polynomials.
Definition: CepsTypes.hpp:178
std::shared_ptr< DistributedHaloVector > DHVecPtr
Typedef for pointer on Distributed Halo CepsVector.
Abstract Class for all numerical method (FE, FD, FV etc)
AbstractPdeProblem * getProblem() const
Return link to pde problem.
CepsVector< Unknown * > getSpatialUnknowns() const
A vector of all unknowns of pb defined on cells or points.
CepsVector< Unknown * > getZeroDUnknowns() const
A vector of all zeroD unknowns of the pb.
A degree of freedom for any kind of problem The dof can be associated to a geometrical element or not...
Enables the writing of files.
Definition: FileWriter.hpp:40
void open()
Creates file or cleans previous content.
Definition: FileWriter.cpp:59
std::ofstream m_file
corresponding stream
Definition: FileWriter.hpp:99
CepsVector< CepsString > m_customNames
Custom data name, printed in header.
Definition: TimeWriter.hpp:85
CepsVector< CepsReal3D > m_points
Points location.
Definition: TimeWriter.hpp:82
CepsBool m_canAddData
Prevents addition of data after header is written.
Definition: TimeWriter.hpp:87
CepsBool hasSomethingToWrite() const
Tells if some data must be written. If not, why bother ?
Definition: TimeWriter.cpp:66
AbstractDiscretization * m_discr
Link to descretization (and problem within)
Definition: TimeWriter.hpp:80
void write(CepsReal t, DHVecPtr data)
Writes the content of data at indices set in constructor.
Definition: TimeWriter.cpp:82
TimeWriter(const CepsString &fileName, const CepsVector< CepsReal3D > &points, AbstractDiscretization *discr)
Constructor.
Definition: TimeWriter.cpp:34
CepsVector< CepsDofGlobalIndex > m_dofIndices
Points IDs.
Definition: TimeWriter.hpp:83
void writeHeader()
Writes comment lines to indicate what are the variables.
Definition: TimeWriter.cpp:113
void addCustomData(const CepsString &name, CepsReal *data)
Add a custom data to be written in the outputfile. Custom data cannot be added after header has been ...
Definition: TimeWriter.cpp:72
CepsVector< CepsReal * > m_customData
Custom data, careful with ptr handling.
Definition: TimeWriter.hpp:86
A class used to defined an unknown of a PDE problem The unknown can be defined on a specific region,...
Definition: Unknown.hpp:45
const CepsGlobalIndex & getGlobalIndex() const
Get the index
const CepsProcId & getOwner() const
Get owner (processus id) of this entity.
CepsUInt getRank()
Returns current processor rank.
MPI_Comm getCommunicator()
Get the communicator.