12#include <petscdmcomposite.h>
13#include <petscviewerhdf5.h>
24 const YAML::Node &node,
30 PetscFunctionBeginUser;
38 probe = std::make_shared<ProbeVolume>(comm, node, mesh);
41 probe = std::make_shared<ProbePoint>(comm, node, mesh);
44 SETERRQ(comm, PETSC_ERR_ARG_UNKNOWN_TYPE,
45 "Unknown type of probe. Accepted values are: "
49 PetscFunctionReturn(0);
58 const YAML::Node &node,
63 PetscFunctionBeginUser;
71 name = node[
"name"].as<std::string>(
"unnamed");
73 path = node[
"path"].as<std::string>();
74 n_monitor = node[
"n_monitor"].as<PetscInt>(1);
75 t_start = node[
"t_start"].as<PetscReal>(0.0);
76 t_end = node[
"t_end"].as<PetscReal>(1e12);
80 PetscFunctionReturn(0);
89 PetscFunctionBeginUser;
91 ierr = PetscFinalized(&finalized); CHKERRV(ierr);
92 if (finalized)
return;
94 ierr =
destroy(); CHKERRV(ierr);
102 PetscFunctionBeginUser;
106 if (
viewer != PETSC_NULL) {ierr = PetscViewerDestroy(&
viewer); CHKERRQ(ierr);}
107 comm = MPI_COMM_NULL;
110 PetscFunctionReturn(0);
121 PetscFunctionBeginUser;
126 if (field < mesh->dim)
128 std::vector<Vec> vel(mesh->dim);
129 ierr = DMCompositeGetAccessArray(mesh->UPack, solution->UGlobal,
131 vel.data()); CHKERRQ(ierr);
133 ierr = DMCompositeRestoreAccessArray(mesh->UPack, solution->UGlobal,
135 vel.data()); CHKERRQ(ierr);
139 ierr =
monitorVec(mesh->da[3], solution->pGlobal , n, t); CHKERRQ(ierr);
142 SETERRQ(
comm, PETSC_ERR_SUP,
143 "Unsupported field. Supported fields are:\n"
144 "\t u (0), v (1), w (2), and p (3).");
147 PetscFunctionReturn(0);
156 const YAML::Node &node,
164 const YAML::Node &node,
169 PetscFunctionBeginUser;
174 std::string vtype_str = node[
"viewer"].as<std::string>(
"ascii");
175 if (vtype_str ==
"ascii")
177 else if (vtype_str ==
"hdf5")
180 atol = node[
"atol"].as<PetscReal>(1e-6);
183 n_sum = node[
"n_sum"].as<PetscInt>(0);
191 for (
auto item : node[
"box"])
194 box[dir][0] = item.second[0].as<PetscReal>();
195 box[dir][1] = item.second[1].as<PetscReal>();
209 ierr = PetscViewerCreate(
comm, &
viewer); CHKERRQ(ierr);
213 ierr = PetscViewerFileSetMode(
viewer, FILE_MODE_APPEND); CHKERRQ(ierr);
214 ierr = PetscViewerFileSetName(
viewer,
path.c_str()); CHKERRQ(ierr);
217 ierr =
createIS(mesh); CHKERRQ(ierr);
221 PetscFunctionReturn(0);
229 PetscFunctionBeginUser;
232 if (
isPetsc != PETSC_NULL) {ierr = ISDestroy(&
isPetsc); CHKERRQ(ierr);}
234 if (
dvec != PETSC_NULL) {ierr = VecDestroy(&
dvec); CHKERRQ(ierr);}
236 PetscFunctionReturn(0);
243 std::vector<PetscReal>::iterator low, up;
245 PetscFunctionBeginUser;
249 for (PetscInt d = 0; d < mesh->dim; ++d)
253 low = std::lower_bound(line.begin(), line.end(),
box[d][0] -
atol);
254 up = std::upper_bound(line.begin(), line.end(),
box[d][1] +
atol);
261 1, std::multiplies<PetscInt>());
263 PetscFunctionReturn(0);
271 PetscFunctionBeginUser;
274 ierr = DMDAGetLocalInfo(mesh->da[
field], &info); CHKERRQ(ierr);
275 std::vector<PetscInt> indices(
nPts);
277 for (PetscInt k = info.zs; k < info.zs + info.zm; ++k)
279 for (PetscInt j = info.ys; j < info.ys + info.ym; ++j)
281 for (PetscInt i = info.xs; i < info.xs + info.xm; ++i)
287 indices[
count] = k * (info.my * info.mx) + j * info.mx + i;
293 indices.resize(
count);
298 std::vector<PetscInt> indices2(indices);
301 ierr = DMDAGetAO(mesh->da[
field], &ao); CHKERRQ(ierr);
302 ierr = AOApplicationToPetsc(ao,
count, &indices[0]); CHKERRQ(ierr);
303 ierr = ISCreateGeneral(
comm,
count, &indices[0],
304 PETSC_COPY_VALUES, &
isPetsc); CHKERRQ(ierr);
306 ierr = ISCreateGeneral(
comm,
count, &indices2[0],
307 PETSC_COPY_VALUES, &
isNatural); CHKERRQ(ierr);
309 PetscFunctionReturn(0);
315 PetscFunctionBeginUser;
319 for (PetscInt d = 0; d < mesh->dim; ++d)
323 PetscInt last = first +
nPtsDir[d];
324 for (PetscInt i = first; i < last; ++i)
328 PetscFunctionReturn(0);
336 PetscFunctionBeginUser;
343 else if (std::strcmp(
viewerType,
"hdf5") == 0)
348 SETERRQ(
comm, PETSC_ERR_ARG_UNKNOWN_TYPE,
349 "Unsupported PetscViewerType. Supported types are:\n"
350 "\t PETSCVIEWERASCII and PETSCVIEWERHDF5");
352 PetscFunctionReturn(0);
360 PetscFunctionBeginUser;
368 ierr = PetscViewerCreate(PETSC_COMM_SELF, &viewer2); CHKERRQ(ierr);
369 ierr = PetscViewerSetType(viewer2, PETSCVIEWERHDF5); CHKERRQ(ierr);
370 ierr = PetscViewerFileSetMode(viewer2, FILE_MODE_WRITE); CHKERRQ(ierr);
371 ierr = PetscViewerFileSetName(
372 viewer2, filePath.c_str()); CHKERRQ(ierr);
373 ierr = PetscViewerHDF5PushGroup(viewer2,
"mesh"); CHKERRQ(ierr);
374 std::vector<std::string> dirs{
"x",
"y",
"z"};
375 for (
unsigned int d = 0; d <
coord.size(); ++d)
378 ierr = VecCreateSeqWithArray(PETSC_COMM_SELF, 1,
nPtsDir[d],
379 &
coord[d][0], &tmp); CHKERRQ(ierr);
380 ierr = PetscObjectSetName((PetscObject) tmp,
381 dirs[d].c_str()); CHKERRQ(ierr);
382 ierr = VecView(tmp, viewer2); CHKERRQ(ierr);
383 ierr = VecDestroy(&tmp); CHKERRQ(ierr);
385 ierr = PetscViewerDestroy(&viewer2); CHKERRQ(ierr);
388 PetscFunctionReturn(0);
396 PetscFunctionBeginUser;
404 ierr = PetscViewerCreate(PETSC_COMM_SELF, &viewer2); CHKERRQ(ierr);
405 ierr = PetscViewerSetType(viewer2, PETSCVIEWERASCII); CHKERRQ(ierr);
406 ierr = PetscViewerPushFormat(
407 viewer2, PETSC_VIEWER_ASCII_SYMMODU); CHKERRQ(ierr);
408 ierr = PetscViewerFileSetMode(viewer2, FILE_MODE_WRITE); CHKERRQ(ierr);
409 ierr = PetscViewerFileSetName(
410 viewer2, filePath.c_str()); CHKERRQ(ierr);
411 std::vector<std::string> dirs{
"x",
"y",
"z"};
412 for (
unsigned int d = 0; d <
coord.size(); ++d)
415 ierr = VecCreateSeqWithArray(PETSC_COMM_SELF, 1,
nPtsDir[d],
416 &
coord[d][0], &tmp); CHKERRQ(ierr);
417 ierr = PetscObjectSetName((PetscObject) tmp,
418 dirs[d].c_str()); CHKERRQ(ierr);
419 ierr = VecView(tmp, viewer2); CHKERRQ(ierr);
420 ierr = VecDestroy(&tmp); CHKERRQ(ierr);
422 ierr = PetscViewerPopFormat(viewer2); CHKERRQ(ierr);
423 ierr = PetscViewerDestroy(&viewer2); CHKERRQ(ierr);
425 ierr = MPI_Barrier(
comm); CHKERRQ(ierr);
427 PetscFunctionReturn(0);
435 PetscFunctionBeginUser;
442 else if (std::strcmp(
viewerType,
"hdf5") == 0)
447 SETERRQ(
comm, PETSC_ERR_ARG_UNKNOWN_TYPE,
448 "Unsupported PetscViewerType. Supported types are:\n"
449 "\t PETSCVIEWERASCII and PETSCVIEWERHDF5");
451 PetscFunctionReturn(0);
458 PetscFunctionBeginUser;
460 ierr = PetscViewerFileSetMode(
viewer, FILE_MODE_APPEND); CHKERRQ(ierr);
461 ierr = PetscViewerFileSetName(
viewer, filePath.c_str()); CHKERRQ(ierr);
463 ierr = PetscViewerHDF5PushGroup(
viewer,
"mesh"); CHKERRQ(ierr);
464 ierr = PetscObjectSetName((PetscObject)
isNatural,
"IS"); CHKERRQ(ierr);
467 PetscFunctionReturn(0);
475 PetscFunctionBeginUser;
477 ierr = PetscViewerPushFormat(
478 viewer, PETSC_VIEWER_ASCII_SYMMODU); CHKERRQ(ierr);
479 ierr = PetscObjectSetName((PetscObject)
isNatural,
"IS"); CHKERRQ(ierr);
481 ierr = PetscViewerPopFormat(
viewer); CHKERRQ(ierr);
483 PetscFunctionReturn(0);
494 PetscFunctionBeginUser;
498 ierr = VecGetSubVector(fvec,
isPetsc, &svec); CHKERRQ(ierr);
501 if (
dvec == PETSC_NULL)
503 ierr = VecDuplicate(svec, &
dvec); CHKERRQ(ierr);
504 ierr = VecSet(
dvec, 0.0); CHKERRQ(ierr);
506 ierr = VecAXPY(
dvec, 1.0, svec); CHKERRQ(ierr);
510 ierr = VecScale(
dvec, 1.0 /
count); CHKERRQ(ierr);
513 ierr = VecSet(
dvec, 0.0); CHKERRQ(ierr);
519 ierr =
writeVec(svec, t); CHKERRQ(ierr);
521 ierr = VecRestoreSubVector(fvec,
isPetsc, &svec); CHKERRQ(ierr);
523 PetscFunctionReturn(0);
531 PetscFunctionBeginUser;
538 else if (std::strcmp(
viewerType,
"hdf5") == 0)
543 SETERRQ(
comm, PETSC_ERR_ARG_UNKNOWN_TYPE,
544 "Unsupported PetscViewerType. Supported types are:\n"
545 "\t PETSCVIEWERASCII and PETSCVIEWERHDF5");
547 PetscFunctionReturn(0);
555 PetscFunctionBeginUser;
558 ierr = PetscViewerHDF5PushGroup(
viewer, field_str.c_str()); CHKERRQ(ierr);
559 ierr = PetscObjectSetName((PetscObject) vec,
560 std::to_string(t).c_str()); CHKERRQ(ierr);
561 ierr = VecView(vec,
viewer); CHKERRQ(ierr);
564 ierr = PetscViewerHDF5WriteAttribute(
565 viewer, std::to_string(t).c_str(),
"count", PETSC_INT, &
count);
569 PetscFunctionReturn(0);
577 PetscFunctionBeginUser;
579 ierr = PetscViewerPushFormat(
viewer, PETSC_VIEWER_ASCII_SYMMODU); CHKERRQ(ierr);
580 ierr = PetscViewerASCIIPrintf(
viewer,
"\nt = %e\n", t); CHKERRQ(ierr);
583 ierr = PetscViewerASCIIPrintf(
viewer,
"count = %d\n",
count); CHKERRQ(ierr);
585 ierr = VecView(vec,
viewer); CHKERRQ(ierr);
586 ierr = PetscViewerPopFormat(
viewer); CHKERRQ(ierr);
588 PetscFunctionReturn(0);
597 const YAML::Node &node,
605 const YAML::Node &node,
610 PetscFunctionBeginUser;
619 for (PetscInt d = 0; d < mesh->dim; ++d)
620 loc[d] = node[
"loc"][d].as<PetscReal>();
628 ierr = PetscViewerCreate(PETSC_COMM_SELF, &
viewer); CHKERRQ(ierr);
630 ierr = PetscViewerFileSetMode(
viewer, FILE_MODE_WRITE); CHKERRQ(ierr);
631 ierr = PetscViewerFileSetName(
viewer,
path.c_str()); CHKERRQ(ierr);
639 ierr = DMCreateLocalVector(mesh->da[
field], &
svec); CHKERRQ(ierr);
641 PetscFunctionReturn(0);
649 PetscFunctionBeginUser;
654 ierr =
interp->destroy(); CHKERRQ(ierr);
656 ierr = VecDestroy(&
svec); CHKERRQ(ierr);
658 PetscFunctionReturn(0);
669 PetscFunctionBeginUser;
672 ierr = DMGlobalToLocalBegin(da, fvec, INSERT_VALUES,
svec); CHKERRQ(ierr);
673 ierr = DMGlobalToLocalEnd(da, fvec, INSERT_VALUES,
svec); CHKERRQ(ierr);
679 ierr = PetscViewerASCIIPrintf(
viewer,
"%10.8e\t%10.8e\n", t,
value); CHKERRQ(ierr);
680 ierr = PetscViewerFileSetMode(
viewer, FILE_MODE_APPEND); CHKERRQ(ierr);
683 PetscFunctionReturn(0);
virtual PetscErrorCode destroy()
Manually destroy data in the object.
PetscReal t_end
Monitoring ending time.
std::string path
Path of the file to output the solution.
PetscErrorCode monitor(const type::Solution &solution, const type::Mesh &mesh, const PetscInt &n, const PetscReal &t)
Monitor the field solution and output data to file.
PetscReal t_start
Monitoring starting time.
PetscMPIInt commRank
Rank of the local process in the MPI communicator.
type::Field field
Type of the field to monitor.
virtual PetscErrorCode init(const MPI_Comm &comm, const YAML::Node &node, const type::Mesh &mesh)=0
Initialize the probe.
PetscViewerType viewerType
Type of the PETSc viewer to use.
PetscMPIInt commSize
Number of processes in the MPI communicator.
PetscViewer viewer
PETSc viewer to output the solution.
virtual ~ProbeBase()
Destructor.
PetscInt n_monitor
Frequency of monitoring the solution (number of time steps).
virtual PetscErrorCode monitorVec(const DM &da, const Vec &fvec, const PetscInt &n, const PetscReal &t)=0
Monitor a sub-region of a vector and possibly output data to file.
MPI_Comm comm
MPI communicator.
std::string name
Name of the probe as a string.
PetscReal value
Interpolated value.
PetscErrorCode destroy()
Manually destroy the data.
Vec svec
Local (ghosted) PETSc Vec object with neighboring values.
type::LinInterp interp
Interpolating object to monitor at a single point.
PetscErrorCode monitorVec(const DM &da, const Vec &fvec, const PetscInt &n, const PetscReal &t)
Monitor a sub-region of a vector and possibly output data to file.
PetscErrorCode init(const MPI_Comm &comm, const YAML::Node &node, const type::Mesh &mesh)
Initialize the probe.
ProbePoint(const MPI_Comm &comm, const YAML::Node &node, const type::Mesh &mesh)
Default constructor.
type::RealVec1D loc
Coordinates of the point to monitor around.
PetscBool pointOnLocalProc
True if target point located on local sub-domain.
PetscErrorCode init(const MPI_Comm &comm, const YAML::Node &node, const type::Mesh &mesh)
Initialize the probe.
PetscErrorCode writeGrid_HDF5(const std::string &filePath)
Write the sub mesh into a HDF5 file.
type::RealVec2D coord
Grid point coordinates in the volume.
ProbeVolume(const MPI_Comm &comm, const YAML::Node &node, const type::Mesh &mesh)
Default constructor.
PetscErrorCode writeIS_ASCII(const std::string &filePath)
Write index set (natural ordering) into a HDF5 file.
PetscErrorCode writeGrid(const std::string &filePath)
Write the sub mesh grid points into a file.
PetscErrorCode getInfo(const type::Mesh &mesh, const type::RealVec2D &box)
Get information about the sub-mesh area to monitor.
type::IntVec1D nPtsDir
Number of grid points along each direction in the volume.
PetscInt n_sum
Frequency of saving the data to file.
PetscErrorCode writeIS_HDF5(const std::string &filePath)
Write index set (natural ordering) into a HDF5 file.
PetscErrorCode monitorVec(const DM &da, const Vec &fvec, const PetscInt &n, const PetscReal &t)
Monitor a sub-region of a vector and possibly output data to file.
Vec dvec
Sub-vector of the region to monitor.
PetscErrorCode destroy()
Manually destroy the data.
PetscErrorCode writeGrid_ASCII(const std::string &filePath)
Write the sub mesh into an ASCII file.
PetscInt nPts
Number of grid points in the volume.
PetscErrorCode createIS(const type::Mesh &mesh)
Create the index set for the points to monitor.
PetscErrorCode writeIS(const std::string &filePath)
Write index set (natural ordering) into a file.
IS isPetsc
Index set for the grid points to monitor (PETSc ordering).
PetscErrorCode writeVec_ASCII(const Vec &vec, const PetscReal &t)
Output a PETSc Vec object to an ASCII file.
type::RealVec2D box
Limits of the volume.
PetscReal atol
Absolute tolerance criterion when comparing values.
PetscErrorCode createGrid(const type::Mesh &mesh)
Get the sub-mesh area to monitor.
PetscInt count
Counter to know when to flush to the data to file.
PetscErrorCode writeVec(const Vec &vec, const PetscReal &t)
Output a PETSc Vec object to file.
type::IntVec1D startIdxDir
Index of the first point in the volume in each direction.
PetscErrorCode writeVec_HDF5(const Vec &vec, const PetscReal &t)
Output a PETSc Vec object to a HDF5 file.
IS isNatural
Index set for the grid points to monitor (Natural ordering).
std::shared_ptr< mesh::MeshBase > Mesh
Type definition of Mesh.
std::shared_ptr< misc::ProbeBase > Probe
Type definition of Probe.
PetscErrorCode createLinInterp(const MPI_Comm &comm, const type::RealVec1D &point, const type::Mesh &mesh, const type::Field &field, type::LinInterp &interp)
Factory function to create a linear interpolation object.
PetscErrorCode createProbe(const MPI_Comm &comm, const YAML::Node &node, const type::Mesh &mesh, type::Probe &probe)
Factory function to create a probe to monitor the solution.
std::shared_ptr< solution::SolutionBase > Solution
Type definition of solution object.
Dir
Legal physical directions.
std::map< std::string, ProbeType > str2ProbeType
Mapping between std::string and ProbeType.
ProbeType
Type of probe for monitoring solution.
std::map< Field, std::string > fd2str
Mapping between Field and std::string.
std::vector< RealVec1D > RealVec2D
2D std::vector holding PetscReal.
std::map< std::string, Dir > str2dir
Mapping between std::string and Dir.
std::vector< PetscReal > RealVec1D
1D std::vector holding PetscReal.
std::map< std::string, Field > str2fd
Mapping between std::string and Field.
A toolbox for building flow solvers.
Prototype of probe classes, type::Probe, and factory function.