12#include <petscviewerhdf5.h>
19 const YAML::Node &node)
29 PetscFunctionBeginUser;
31 ierr = PetscFinalized(&finalized); CHKERRV(ierr);
32 if (finalized)
return;
34 ierr =
destroy(); CHKERRV(ierr);
42 PetscFunctionBeginUser;
48 ierr = VecDestroy(&
dP); CHKERRQ(ierr);
49 ierr = VecDestroy(&
bc1); CHKERRQ(ierr);
50 ierr = VecDestroy(&
rhs1); CHKERRQ(ierr);
51 ierr = VecDestroy(&
rhs2); CHKERRQ(ierr);
52 for (
unsigned int i = 0; i <
conv.size(); ++i)
54 ierr = VecDestroy(&
conv[i]); CHKERRQ(ierr);
56 for (
unsigned int i = 0; i <
diff.size(); ++i)
58 ierr = VecDestroy(&
diff[i]); CHKERRQ(ierr);
62 ierr = MatDestroy(&
A); CHKERRQ(ierr);
63 ierr = MatDestroy(&
DBNG); CHKERRQ(ierr);
64 ierr = MatDestroy(&
BNG); CHKERRQ(ierr);
65 ierr = MatDestroy(&
N); CHKERRQ(ierr);
66 ierr = MatDestroy(&
G); CHKERRQ(ierr);
67 ierr = MatDestroy(&
D); CHKERRQ(ierr);
69 ierr = MatDestroy(&
L); CHKERRQ(ierr);
75 ierr = probe->destroy(); CHKERRQ(ierr);
90 PetscFunctionReturn(0);
97 PetscFunctionBeginUser;
99 ierr = PetscLogStageRegister(
112 dt =
config[
"parameters"][
"dt"].as<PetscReal>();
114 nstart =
config[
"parameters"][
"startStep"].as<PetscInt>(0);
116 t =
config[
"parameters"][
"t"].as<PetscReal>(0.0);
118 nt =
config[
"parameters"][
"nt"].as<PetscInt>();
120 nsave =
config[
"parameters"][
"nsave"].as<PetscInt>();
123 nu =
config[
"flow"][
"nu"].as<PetscReal>();
128 std::string filePath =
config[
"output"].as<std::string>() +
"/grid.h5";
129 ierr =
mesh->write(filePath); CHKERRQ(ierr);
142 ierr =
bc->setGhostICs(
solution); CHKERRQ(ierr);
163 ierr =
vSolver->setMatrix(
A); CHKERRQ(ierr);
168 for (
unsigned int i = 0; i <
probes.size(); ++i)
171 config[
"probes"][i][
"path"] =
172 config[
"output"].as<std::string>() +
"/" +
173 config[
"probes"][i][
"path"].as<std::string>();
181 config[
"output"].as<std::string>() +
182 "/iterations-" + std::to_string(
ite) +
".txt",
186 ierr = PetscLogStageRegister(
188 ierr = PetscLogStageRegister(
190 ierr = PetscLogStageRegister(
192 ierr = PetscLogStageRegister(
194 ierr = PetscLogStageRegister(
196 ierr = PetscLogStageRegister(
198 ierr = PetscLogStageRegister(
201 ierr = PetscLogStagePop(); CHKERRQ(ierr);
203 PetscFunctionReturn(0);
211 PetscFunctionBeginUser;
215 std::stringstream ss;
216 std::string filePath;
217 ss << std::setfill(
'0') << std::setw(7) <<
ite;
218 filePath =
config[
"output"].as<std::string>() +
"/" + ss.str() +
".h5";
219 ierr = PetscPrintf(
comm,
"[time step %d] Writing solution data... ",
222 ierr = PetscPrintf(
comm,
"done\n"); CHKERRQ(ierr);
226 std::stringstream ss;
227 std::string filePath;
228 ss << std::setfill(
'0') << std::setw(7) <<
ite;
229 filePath =
config[
"output"].as<std::string>() +
"/" + ss.str() +
".h5";
230 ierr = PetscPrintf(
comm,
"[time step %d] Reading restart data... ",
233 ierr = PetscPrintf(
comm,
"done\n"); CHKERRQ(ierr);
236 PetscFunctionReturn(0);
244 PetscFunctionBeginUser;
263 ierr =
bc->updateGhostValues(
solution); CHKERRQ(ierr);
265 PetscFunctionReturn(0);
273 PetscFunctionBeginUser;
280 std::stringstream ss;
281 std::string filePath;
282 ss << std::setfill(
'0') << std::setw(7) <<
ite;
283 filePath =
config[
"output"].as<std::string>() +
"/" + ss.str() +
".h5";
284 ierr = PetscPrintf(
comm,
"[time step %d] Writing solution data... ",
287 ierr = PetscPrintf(
comm,
"done\n"); CHKERRQ(ierr);
289 filePath =
config[
"logs"].as<std::string>() +
"/" + ss.str() +
".log";
294 std::string filePath;
295 std::stringstream ss;
296 ss << std::setfill(
'0') << std::setw(7) <<
ite;
297 filePath =
config[
"output"].as<std::string>() +
"/" + ss.str() +
".h5";
298 ierr = PetscPrintf(
comm,
"[time step %d] Writing restart data... ",
301 ierr = PetscPrintf(
comm,
"done\n"); CHKERRQ(ierr);
307 PetscFunctionReturn(0);
321 PetscFunctionBeginUser;
331 mesh,
G, PETSC_FALSE); CHKERRQ(ierr);
342 ierr = MatDuplicate(
L, MAT_COPY_VALUES, &
A); CHKERRQ(ierr);
343 ierr = MatScale(
A, -
diffCoeffs->implicitCoeff *
nu); CHKERRQ(ierr);
344 ierr = MatShift(
A, 1.0 /
dt); CHKERRQ(ierr);
348 N =
config[
"parameters"][
"BN"].as<PetscInt>(1);
352 BN,
G, MAT_INITIAL_MATRIX, PETSC_DEFAULT, &
BNG); CHKERRQ(ierr);
356 D,
BNG, MAT_INITIAL_MATRIX, PETSC_DEFAULT, &
DBNG); CHKERRQ(ierr);
362 ierr = MatDestroy(&BN); CHKERRQ(ierr);
364 PetscFunctionReturn(0);
372 PetscFunctionBeginUser;
374 ierr = VecDuplicate(
solution->pGlobal, &
dP); CHKERRQ(ierr);
375 ierr = VecDuplicate(
solution->UGlobal, &
bc1); CHKERRQ(ierr);
376 ierr = VecDuplicate(
solution->UGlobal, &
rhs1); CHKERRQ(ierr);
377 ierr = VecDuplicate(
solution->pGlobal, &
rhs2); CHKERRQ(ierr);
380 for (
unsigned int i = 0; i <
conv.size(); ++i)
382 ierr = VecDuplicate(
solution->UGlobal, &
conv[i]); CHKERRQ(ierr);
386 for (
unsigned int i = 0; i <
diff.size(); ++i)
388 ierr = VecDuplicate(
solution->UGlobal, &
diff[i]); CHKERRQ(ierr);
391 PetscFunctionReturn(0);
399 PetscFunctionBeginUser;
402 ierr =
pSolver->getType(type); CHKERRQ(ierr);
404 if (type ==
"PETSc KSP")
407 ierr = MatNullSpaceCreate(
408 comm, PETSC_TRUE, 0,
nullptr, &nsp); CHKERRQ(ierr);
409 ierr = MatSetNullSpace(
DBNG, nsp); CHKERRQ(ierr);
410 ierr = MatSetNearNullSpace(
DBNG, nsp); CHKERRQ(ierr);
411 ierr = MatNullSpaceDestroy(&nsp); CHKERRQ(ierr);
414 else if (type ==
"NVIDIA AmgX")
416 PetscInt row[1] = {0};
417 ierr = MatZeroRowsColumns(
418 DBNG, 1, row, 1.0,
nullptr,
nullptr); CHKERRQ(ierr);
423 SETERRQ1(PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
424 "Could not recognize the type of linear solver: %s\n",
428 PetscFunctionReturn(0);
436 PetscFunctionBeginUser;
443 ierr = VecScale(
rhs1, -1.0); CHKERRQ(ierr);
454 for (
int i =
conv.size() - 1; i > 0; i--)
456 ierr = VecSwap(
conv[i],
conv[i - 1]); CHKERRQ(ierr);
462 ierr = MatMult(
N,
solution->UGlobal,
conv[0]); CHKERRQ(ierr);
466 ierr = VecScale(
conv[0], -1.0); CHKERRQ(ierr);
470 for (
unsigned int i = 0; i <
conv.size(); ++i)
482 for (
int i =
diff.size() - 1; i > 0; i--)
484 ierr = VecSwap(
diff[i],
diff[i - 1]); CHKERRQ(ierr);
490 ierr = MatMult(
L,
solution->UGlobal,
diff[0]); CHKERRQ(ierr);
493 ierr = VecScale(
diff[0],
nu); CHKERRQ(ierr);
497 for (
unsigned int i = 0; i <
diff.size(); ++i)
512 ierr = VecScale(
bc1,
nu); CHKERRQ(ierr);
518 ierr = PetscLogStagePop(); CHKERRQ(ierr);
520 PetscFunctionReturn(0);
528 PetscFunctionBeginUser;
534 ierr = PetscLogStagePop(); CHKERRQ(ierr);
536 PetscFunctionReturn(0);
544 PetscFunctionBeginUser;
555 ierr = VecSetValue(
rhs2, 0, 0.0, INSERT_VALUES); CHKERRQ(ierr);
556 ierr = VecAssemblyBegin(
rhs2); CHKERRQ(ierr);
557 ierr = VecAssemblyEnd(
rhs2); CHKERRQ(ierr);
560 ierr = PetscLogStagePop(); CHKERRQ(ierr);
562 PetscFunctionReturn(0);
570 PetscFunctionBeginUser;
577 ierr = PetscLogStagePop(); CHKERRQ(ierr);
579 PetscFunctionReturn(0);
587 PetscFunctionBeginUser;
589 ierr = PetscLogStagePush(
stageUpdate); CHKERRQ(ierr);
592 ierr = MatMult(
BNG,
dP,
rhs1); CHKERRQ(ierr);
593 ierr = VecAXPY(
solution->UGlobal, -1.0,
rhs1); CHKERRQ(ierr);
595 ierr = PetscLogStagePop(); CHKERRQ(ierr);
597 PetscFunctionReturn(0);
605 PetscFunctionBeginUser;
607 ierr = PetscLogStagePush(
stageUpdate); CHKERRQ(ierr);
610 ierr = VecAXPY(
solution->pGlobal, 1.0,
dP); CHKERRQ(ierr);
612 ierr = PetscLogStagePop(); CHKERRQ(ierr);
614 PetscFunctionReturn(0);
622 PetscFunctionBeginUser;
624 ierr = PetscLogStagePush(
stageWrite); CHKERRQ(ierr);
627 ierr =
solution->write(filePath); CHKERRQ(ierr);
631 ierr = PetscLogStagePop(); CHKERRQ(ierr);
633 PetscFunctionReturn(0);
638 const std::string &filePath)
642 PetscBool fileExist = PETSC_FALSE;
644 PetscFunctionBeginUser;
646 ierr = PetscLogStagePush(
stageWrite); CHKERRQ(ierr);
649 ierr = PetscTestFile(filePath.c_str(),
'w', &fileExist); CHKERRQ(ierr);
657 ierr = PetscViewerCreate(
comm, &viewer); CHKERRQ(ierr);
658 ierr = PetscViewerSetType(viewer, PETSCVIEWERHDF5); CHKERRQ(ierr);
659 ierr = PetscViewerFileSetMode(viewer, FILE_MODE_APPEND); CHKERRQ(ierr);
660 ierr = PetscViewerFileSetName(viewer, filePath.c_str()); CHKERRQ(ierr);
663 ierr = PetscViewerHDF5PushGroup(viewer,
"/"); CHKERRQ(ierr);
666 ierr = PetscViewerHDF5PushGroup(viewer,
"/convection"); CHKERRQ(ierr);
667 for (
unsigned int i = 0; i <
conv.size(); ++i)
669 ierr = PetscObjectSetName(
670 (PetscObject)
conv[i], std::to_string(i).c_str()); CHKERRQ(ierr);
671 ierr = VecView(
conv[i], viewer); CHKERRQ(ierr);
674 ierr = PetscViewerHDF5PushGroup(viewer,
"/diffusion"); CHKERRQ(ierr);
675 for (
unsigned int i = 0; i <
diff.size(); ++i)
677 ierr = PetscObjectSetName(
678 (PetscObject)
diff[i], std::to_string(i).c_str()); CHKERRQ(ierr);
679 ierr = VecView(
diff[i], viewer); CHKERRQ(ierr);
683 ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
685 ierr = PetscLogStagePop(); CHKERRQ(ierr);
687 PetscFunctionReturn(0);
692 const std::string &filePath)
696 PetscBool fileExist = PETSC_FALSE;
698 PetscFunctionBeginUser;
701 ierr = PetscTestFile(filePath.c_str(),
'r', &fileExist); CHKERRQ(ierr);
704 SETERRQ1(PETSC_COMM_WORLD, PETSC_ERR_FILE_READ,
705 "Could not find file \"%s\" for restarting.\n",
709 ierr =
solution->read(filePath); CHKERRQ(ierr);
713 ierr = PetscViewerCreate(
comm, &viewer); CHKERRQ(ierr);
714 ierr = PetscViewerSetType(viewer, PETSCVIEWERHDF5); CHKERRQ(ierr);
715 ierr = PetscViewerFileSetMode(viewer, FILE_MODE_READ); CHKERRQ(ierr);
716 ierr = PetscViewerFileSetName(viewer, filePath.c_str()); CHKERRQ(ierr);
719 ierr = PetscViewerHDF5PushGroup(viewer,
"/"); CHKERRQ(ierr);
722 ierr = PetscViewerHDF5PushGroup(viewer,
"/convection"); CHKERRQ(ierr);
723 for (
unsigned int i = 0; i <
conv.size(); ++i)
725 ierr = PetscObjectSetName(
726 (PetscObject)
conv[i], std::to_string(i).c_str()); CHKERRQ(ierr);
727 ierr = VecLoad(
conv[i], viewer); CHKERRQ(ierr);
730 ierr = PetscViewerHDF5PushGroup(viewer,
"/diffusion"); CHKERRQ(ierr);
731 for (
unsigned int i = 0; i <
diff.size(); ++i)
733 ierr = PetscObjectSetName(
734 (PetscObject)
diff[i], std::to_string(i).c_str()); CHKERRQ(ierr);
735 ierr = VecLoad(
diff[i], viewer); CHKERRQ(ierr);
739 ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
743 ierr =
bc->setGhostICs(
solution); CHKERRQ(ierr);
745 PetscFunctionReturn(0);
750 const std::string &filePath,
const PetscFileMode &mode,
755 PetscFunctionBeginUser;
757 ierr = PetscViewerCreate(
comm, &viewer); CHKERRQ(ierr);
758 ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII); CHKERRQ(ierr);
759 ierr = PetscViewerFileSetMode(viewer, mode); CHKERRQ(ierr);
760 ierr = PetscViewerFileSetName(viewer, filePath.c_str()); CHKERRQ(ierr);
762 PetscFunctionReturn(0);
772 PetscFunctionBeginUser;
774 ierr = PetscLogStagePush(
stageWrite); CHKERRQ(ierr);
780 ierr =
vSolver->getIters(nIters); CHKERRQ(ierr);
781 ierr =
vSolver->getResidual(res); CHKERRQ(ierr);
782 ierr = PetscViewerASCIIPrintf(
786 ierr =
pSolver->getIters(nIters); CHKERRQ(ierr);
787 ierr =
pSolver->getResidual(res); CHKERRQ(ierr);
788 ierr = PetscViewerASCIIPrintf(
791 ierr = PetscLogStagePop(); CHKERRQ(ierr);
793 PetscFunctionReturn(0);
798 const std::string &filePath)
803 PetscFunctionBeginUser;
805 ierr = PetscViewerCreate(
comm, &viewer); CHKERRQ(ierr);
806 ierr = PetscViewerSetType(viewer, PETSCVIEWERHDF5); CHKERRQ(ierr);
807 ierr = PetscViewerFileSetMode(viewer, FILE_MODE_APPEND); CHKERRQ(ierr);
808 ierr = PetscViewerFileSetName(viewer, filePath.c_str()); CHKERRQ(ierr);
810 ierr = PetscViewerHDF5WriteAttribute(
811 viewer,
"/p",
"time", PETSC_DOUBLE, &
t); CHKERRQ(ierr);
812 ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
814 PetscFunctionReturn(0);
824 PetscFunctionBeginUser;
826 ierr = PetscViewerCreate(
comm, &viewer); CHKERRQ(ierr);
827 ierr = PetscViewerSetType(viewer, PETSCVIEWERHDF5); CHKERRQ(ierr);
828 ierr = PetscViewerFileSetMode(viewer, FILE_MODE_READ); CHKERRQ(ierr);
829 ierr = PetscViewerFileSetName(viewer, filePath.c_str()); CHKERRQ(ierr);
831 ierr = PetscViewerHDF5ReadAttribute(
832 viewer,
"/p",
"time", PETSC_DOUBLE,
nullptr, &
t);
834 ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
836 PetscFunctionReturn(0);
844 PetscFunctionBeginUser;
853 ierr = PetscLogStagePop(); CHKERRQ(ierr);
855 PetscFunctionReturn(0);
Vec rhs2
Right-hand side vector of the Poisson system.
petibm::type::Solution solution
Data object holding the velocity and pressure fields.
petibm::type::LinSolver pSolver
Poisson linear solver.
PetscReal nu
Viscous diffusion coefficient.
PetscInt nstart
Starting time-step index.
PetscLogStage stageMonitor
Log stage for monitoring user-defined regions of the domain.
Vec bc1
Inhomogeneous boundary terms for the velocity system.
PetscLogStage stageSolvePoisson
Log stage for solving the Poisson system.
PetscErrorCode ioInitialData()
Read or write initial data.
YAML::Node config
YAML configuration settings.
std::vector< petibm::type::Probe > probes
Probes to monitor the solution.
petibm::type::Boundary bc
Information about the domain boundaries.
NavierStokesSolver()=default
Default constructor.
PetscInt nsave
Frequency at which the solution fields are written to files.
virtual PetscErrorCode updatePressure()
virtual PetscErrorCode assembleRHSVelocity()
Assemble the RHS vector of the velocity system.
PetscErrorCode advance()
Advance the solution by one time step.
virtual PetscErrorCode readRestartDataHDF5(const std::string &filePath)
Read data required to restart a simulation from a HDF5 file.
petibm::type::TimeIntegration convCoeffs
Time scheme for the convective terms.
virtual PetscErrorCode writeLinSolversInfo()
Write numbers of iterations and residuals of solvers to file.
PetscLogStage stageInitialize
Log stage for the initialization phase.
virtual PetscErrorCode readTimeHDF5(const std::string &filePath, PetscReal &t)
Read the time value from a HDF5 file.
bool finished()
Evaluate if the simulation is finished.
Vec rhs1
Right-hand side vector of the velocity system.
Vec dP
Pressure-correction vector.
virtual PetscErrorCode writeSolutionHDF5(const std::string &filePath)
Write the solution fields into a HDF5 file.
Mat A
Implicit operator for the velocity solver.
virtual PetscErrorCode createOperators()
Create operators.
petibm::type::LinSolver vSolver
Velocity linear solver.
PetscMPIInt commRank
Rank of the process in the MPI communicator.
PetscReal dt
Time-step size.
Mat N
Convective operator (matrix-free).
Mat LCorrection
Laplacian correction operator for boundary conditions.
std::vector< Vec > conv
Explicit convective terms.
virtual PetscErrorCode assembleRHSPoisson()
Assemble the RHS vector of the Poisson system.
MPI_Comm comm
MPI communicator.
virtual PetscErrorCode solvePoisson()
Solve the Poisson system.
virtual PetscErrorCode writeRestartDataHDF5(const std::string &filePath)
Write data required to restart a simulation into a HDF5 file.
PetscBool isRefP
True if we pin the pressure at a reference point.
~NavierStokesSolver()
Default destructor.
petibm::type::TimeIntegration diffCoeffs
Time scheme for the diffusion terms.
virtual PetscErrorCode applyDivergenceFreeVelocity()
PetscInt nrestart
Frequency at which data to restart are written to files.
Mat DCorrection
Divergence correction for boundary conditions.
PetscMPIInt commSize
Size of the MPI communicator.
PetscLogStage stageRHSVelocity
Log stage for assembling the RHS of the velocity system.
PetscErrorCode write()
Write solution and solver info to files.
PetscErrorCode init(const MPI_Comm &world, const YAML::Node &node)
Initialize the Navier-Stokes solver.
virtual PetscErrorCode setNullSpace()
Set Poisson nullspace or pin pressure at a reference point.
PetscLogStage stageWrite
Log stage when write the solution fields.
std::vector< Vec > diff
Explicit diffusion terms.
PetscLogStage stageUpdate
Log stage for updating field variables.
PetscInt ite
Time-step index.
petibm::type::Mesh mesh
Structured Cartesian mesh object.
PetscLogStage stageSolveVelocity
Log stage for solving the velocity system.
PetscLogStage stageRHSPoisson
Log stage for assembling the RHS of the Poisson system.
virtual PetscErrorCode solveVelocity()
Solve the velocity system.
virtual PetscErrorCode monitorProbes()
Monitor the solution at probes.
Mat BNG
Projection operator.
PetscInt nt
Number of time steps to compute.
virtual PetscErrorCode writeTimeHDF5(const PetscReal &t, const std::string &filePath)
Write the time value into a HDF5 file.
virtual PetscErrorCode createVectors()
Create vectors.
PetscErrorCode destroy()
Manually destroy data.
Mat D
Divergence operator.
virtual PetscErrorCode createPetscViewerASCII(const std::string &filePath, const PetscFileMode &mode, PetscViewer &viewer)
Create an ASCII PetscViewer.
Mat DBNG
Poisson operator.
PetscViewer solversViewer
ASCII PetscViewer object to output solvers info.
PetscErrorCode createBoundary(const type::Mesh &mesh, const YAML::Node &node, type::Boundary &boundary)
Create a Boundary object.
PetscErrorCode createLinSolver(const std::string &solverName, const YAML::Node &node, type::LinSolver &solver)
A factory function for creating a LinSolver.
PetscErrorCode createMesh(const MPI_Comm &comm, const YAML::Node &node, type::Mesh &mesh)
Factory function for creating a Mesh object.
PetscErrorCode writePetscLog(const MPI_Comm comm, const std::string &filePath)
Write a summary of the PETSc logging into a ASCII file.
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.
PetscErrorCode createDivergence(const type::Mesh &mesh, const type::Boundary &bc, Mat &D, Mat &DCorrection, const PetscBool &normalize=PETSC_TRUE)
Create a divergence operator, , and corresponding boundary correction, , for velocity fields.
PetscErrorCode createGradient(const type::Mesh &mesh, Mat &G, const PetscBool &normalize=PETSC_TRUE)
Create a gradient operator, , for pressure field.
PetscErrorCode createLaplacian(const type::Mesh &mesh, const type::Boundary &bc, Mat &L, Mat &LCorrection)
Create a Laplacian operator, , and boundary correction, for velocity fields.
PetscErrorCode createBnHead(const Mat &Op, const PetscReal &dt, const PetscReal &coeff, const PetscInt &n, Mat &BnHead)
Create non-normalized matrix of approximated , .
PetscErrorCode createConvection(const type::Mesh &mesh, const type::Boundary &bc, Mat &H)
Create a matrix-free Mat for convection operator, .
PetscErrorCode createSolution(const type::Mesh &mesh, type::Solution &solution)
Factory function to create a petibm::solution::Solution object.
PetscErrorCode createTimeIntegration(const std::string &name, const YAML::Node &node, type::TimeIntegration &integration)
factory function for type::TimeIntegration.
Prototypes of I/O functions.
Definition of the class NavierStokesSolver.