14#include <petscviewerhdf5.h>
26 PetscFunctionBeginUser;
28 std::ifstream inFile(file);
35 SETERRQ1(PETSC_COMM_WORLD, PETSC_ERR_FILE_READ,
36 "Opening or reading body file %s failed!", file.c_str());
39 if (std::getline(inFile, line))
41 std::stringstream sline(line);
44 SETERRQ1(PETSC_COMM_WORLD, PETSC_ERR_FILE_READ,
45 "Can't read the total number of points in file %s !\n",
48 if (sline.peek() != EOF)
50 PETSC_COMM_WORLD, PETSC_ERR_FILE_READ,
51 "The first line in file %s contains more than one integer. "
52 "Please check the format.\n",
56 SETERRQ1(PETSC_COMM_WORLD, PETSC_ERR_FILE_READ,
57 "Error while reading the first line in file %s !\n",
62 std::getline(inFile, line);
63 std::stringstream sline(line);
65 while (sline >> temp) dim += 1;
69 PETSC_COMM_WORLD, PETSC_ERR_FILE_READ,
70 "Could not calculate the dimension from the first coordinate "
71 "set in the file %s!\n",
80 for (PetscInt d = 0; d < dim; ++d) sline >> coords[0][d];
87 while (std::getline(inFile, line))
89 std::stringstream sline(line);
91 for (PetscInt d = 0; d < dim; ++d)
92 if (!(sline >> coords[c][d]))
93 SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_FILE_READ,
94 "The number of doubles at line %d in file %s does not "
95 "match the dimension.\n",
98 if (sline.peek() != EOF)
99 SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_FILE_READ,
100 "The number of doubles at line %d in file %s does not "
101 "match the dimension.\n",
102 c + 2, file.c_str());
112 SETERRQ1(PETSC_COMM_WORLD, PETSC_ERR_FILE_READ,
113 "The total number of coordinates read in does not match the "
114 "number specified at the first line in file %s !\n",
117 PetscFunctionReturn(0);
120PetscErrorCode
print(
const std::string &info)
122 PetscFunctionBeginUser;
126 ierr = PetscSynchronizedPrintf(PETSC_COMM_WORLD, info.c_str());
129 ierr = PetscSynchronizedFlush(PETSC_COMM_WORLD, PETSC_STDOUT);
132 ierr = PetscPrintf(PETSC_COMM_WORLD,
"\n"); CHKERRQ(ierr);
134 PetscFunctionReturn(0);
137PetscErrorCode
writeHDF5Vecs(
const MPI_Comm comm,
const std::string &filePath,
138 const std::string &loc,
139 const std::vector<std::string> &names,
140 const std::vector<Vec> &vecs,
141 const PetscFileMode mode)
146 PetscFunctionBeginUser;
149 ierr = PetscViewerCreate(comm, &viewer); CHKERRQ(ierr);
150 ierr = PetscViewerSetType(viewer, PETSCVIEWERHDF5); CHKERRQ(ierr);
151 ierr = PetscViewerFileSetMode(viewer, mode); CHKERRQ(ierr);
152 ierr = PetscViewerFileSetName(viewer, filePath.c_str()); CHKERRQ(ierr);
154 ierr = PetscViewerHDF5PushGroup(viewer, loc.c_str()); CHKERRQ(ierr);
156 for (
unsigned int i = 0; i < vecs.size(); ++i)
158 ierr = PetscObjectSetName((PetscObject)vecs[i], names[i].c_str());
161 ierr = VecView(vecs[i], viewer); CHKERRQ(ierr);
164 ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
166 PetscFunctionReturn(0);
169PetscErrorCode
writeHDF5Vecs(
const MPI_Comm comm,
const std::string &filePath,
170 const std::string &loc,
171 const std::vector<std::string> &names,
172 const std::vector<PetscInt> &n,
173 const std::vector<PetscReal *> &vecs,
174 const PetscFileMode mode)
179 PetscFunctionBeginUser;
181 ierr = PetscViewerCreate(comm, &viewer); CHKERRQ(ierr);
182 ierr = PetscViewerSetType(viewer, PETSCVIEWERHDF5); CHKERRQ(ierr);
183 ierr = PetscViewerFileSetMode(viewer, mode); CHKERRQ(ierr);
184 ierr = PetscViewerFileSetName(viewer, filePath.c_str()); CHKERRQ(ierr);
186 ierr = PetscViewerHDF5PushGroup(viewer, loc.c_str()); CHKERRQ(ierr);
188 for (
unsigned int i = 0; i < vecs.size(); i++)
192 VecCreateMPIWithArray(comm, 1, n[i], PETSC_DECIDE,
nullptr, &temp);
194 ierr = PetscObjectSetName((PetscObject)temp, names[i].c_str());
196 ierr = VecPlaceArray(temp, vecs[i]); CHKERRQ(ierr);
197 ierr = VecView(temp, viewer); CHKERRQ(ierr);
198 ierr = VecResetArray(temp); CHKERRQ(ierr);
199 ierr = VecDestroy(&temp); CHKERRQ(ierr);
202 ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
204 PetscFunctionReturn(0);
207PetscErrorCode
writeHDF5Vecs(
const MPI_Comm comm,
const std::string &filePath,
208 const std::string &loc,
209 const std::vector<std::string> &names,
211 const PetscFileMode mode)
216 PetscFunctionBeginUser;
218 ierr = PetscViewerCreate(comm, &viewer); CHKERRQ(ierr);
219 ierr = PetscViewerSetType(viewer, PETSCVIEWERHDF5); CHKERRQ(ierr);
220 ierr = PetscViewerFileSetMode(viewer, mode); CHKERRQ(ierr);
221 ierr = PetscViewerFileSetName(viewer, filePath.c_str()); CHKERRQ(ierr);
223 ierr = PetscViewerHDF5PushGroup(viewer, loc.c_str()); CHKERRQ(ierr);
225 for (
unsigned int i = 0; i < vecs.size(); i++)
228 ierr = VecCreateMPIWithArray(comm, 1, vecs[i].size(), PETSC_DECIDE,
229 nullptr, &temp); CHKERRQ(ierr);
230 ierr = PetscObjectSetName((PetscObject)temp, names[i].c_str());
232 ierr = VecPlaceArray(temp, vecs[i].data()); CHKERRQ(ierr);
233 ierr = VecView(temp, viewer); CHKERRQ(ierr);
234 ierr = VecResetArray(temp); CHKERRQ(ierr);
235 ierr = VecDestroy(&temp); CHKERRQ(ierr);
238 ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
240 PetscFunctionReturn(0);
243PetscErrorCode
readHDF5Vecs(
const MPI_Comm comm,
const std::string &filePath,
244 const std::string &loc,
245 const std::vector<std::string> &names,
246 std::vector<Vec> &vecs)
251 PetscFunctionBeginUser;
254 ierr = PetscViewerCreate(comm, &viewer); CHKERRQ(ierr);
255 ierr = PetscViewerSetType(viewer, PETSCVIEWERHDF5); CHKERRQ(ierr);
256 ierr = PetscViewerFileSetMode(viewer, FILE_MODE_READ); CHKERRQ(ierr);
257 ierr = PetscViewerFileSetName(viewer, filePath.c_str()); CHKERRQ(ierr);
259 ierr = PetscViewerHDF5PushGroup(viewer, loc.c_str()); CHKERRQ(ierr);
261 for (
unsigned int i = 0; i < vecs.size(); ++i)
263 ierr = PetscObjectSetName((PetscObject)vecs[i], names[i].c_str());
266 ierr = VecLoad(vecs[i], viewer); CHKERRQ(ierr);
269 ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
271 PetscFunctionReturn(0);
274PetscErrorCode
writePetscLog(
const MPI_Comm comm,
const std::string &filePath)
277 PetscViewer viewerLog;
279 PetscFunctionBeginUser;
281 ierr = PetscViewerCreate(comm, &viewerLog); CHKERRQ(ierr);
282 ierr = PetscViewerSetType(viewerLog, PETSCVIEWERASCII); CHKERRQ(ierr);
283 ierr = PetscViewerFileSetMode(viewerLog, FILE_MODE_WRITE); CHKERRQ(ierr);
284 ierr = PetscViewerFileSetName(viewerLog, filePath.c_str()); CHKERRQ(ierr);
285 ierr = PetscLogView(viewerLog); CHKERRQ(ierr);
286 ierr = PetscViewerDestroy(&viewerLog); CHKERRQ(ierr);
288 PetscFunctionReturn(0);
PetscErrorCode writeHDF5Vecs(const MPI_Comm comm, const std::string &filePath, const std::string &loc, const std::vector< std::string > &names, const std::vector< Vec > &vecs, const PetscFileMode mode=FILE_MODE_WRITE)
Write a vector of Vec objects to a HDF5 file.
PetscErrorCode readHDF5Vecs(const MPI_Comm comm, const std::string &filePath, const std::string &loc, const std::vector< std::string > &names, std::vector< Vec > &vecs)
Read a vector of Vec objects from a HDF5 file.
PetscErrorCode readLagrangianPoints(const std::string &file, PetscInt &nPts, type::RealVec2D &coords)
Read the number Lagrangian points and their coordinates from a file.
PetscErrorCode writePetscLog(const MPI_Comm comm, const std::string &filePath)
Write a summary of the PETSc logging into a ASCII file.
PetscErrorCode print(const std::string &info)
Print information of a parallel object to standard output.
std::vector< RealVec1D > RealVec2D
2D std::vector holding PetscReal.
std::vector< PetscReal > RealVec1D
1D std::vector holding PetscReal.
Prototypes of I/O functions.
A toolbox for building flow solvers.