10#include <petscviewerhdf5.h>
18 const YAML::Node &node)
28 PetscFunctionBeginUser;
30 ierr = PetscFinalized(&finalized); CHKERRV(ierr);
31 if (finalized)
return;
33 ierr =
destroy(); CHKERRV(ierr);
41 PetscFunctionBeginUser;
44 ierr = ISDestroy(&
isDE[0]); CHKERRQ(ierr);
45 ierr = ISDestroy(&
isDE[1]); CHKERRQ(ierr);
46 ierr = VecResetArray(
P); CHKERRQ(ierr);
47 ierr = VecDestroy(&
P); CHKERRQ(ierr);
51 PetscFunctionReturn(0);
58 PetscFunctionBeginUser;
61 PetscInt dim = node[
"mesh"].size();
63 world, dim, node,
bodies); CHKERRQ(ierr);
71 config[
"output"].as<std::string>() +
72 "/forces-" + std::to_string(
ite) +
".txt",
76 ierr = PetscLogStageRegister(
79 ierr = PetscLogStagePop(); CHKERRQ(ierr);
81 PetscFunctionReturn(0);
89 PetscFunctionBeginUser;
96 PetscFunctionReturn(0);
104 PetscFunctionBeginUser;
106 Mat R, MHat, BN, GH[2], DE[2];
116 mesh, GH[0], PETSC_FALSE); CHKERRQ(ierr);
127 ierr = MatDuplicate(
L, MAT_COPY_VALUES, &
A); CHKERRQ(ierr);
128 ierr = MatScale(
A, -
diffCoeffs->implicitCoeff *
nu); CHKERRQ(ierr);
129 ierr = MatShift(
A, 1.0 /
dt); CHKERRQ(ierr);
133 ierr = MatCreateVecs(R,
nullptr, &RDiag); CHKERRQ(ierr);
134 ierr = MatGetDiagonal(R, RDiag); CHKERRQ(ierr);
138 ierr = MatCreateVecs(MHat,
nullptr, &MHatDiag); CHKERRQ(ierr);
139 ierr = MatGetDiagonal(MHat, MHatDiag); CHKERRQ(ierr);
142 ierr =
bodies->updateMeshIdx(
mesh); CHKERRQ(ierr);
143 const YAML::Node &node =
config[
"parameters"];
144 std::string name = node[
"delta"].as<std::string>(
"ROMA_ET_AL_1999");
149 mesh,
bc,
bodies, kernel, kernelSize, DE[1]); CHKERRQ(ierr);
150 ierr = MatTranspose(DE[1], MAT_INITIAL_MATRIX, &GH[1]); CHKERRQ(ierr);
153 ierr = MatDiagonalScale(DE[1],
nullptr, RDiag); CHKERRQ(ierr);
154 ierr = MatDiagonalScale(DE[1],
nullptr, MHatDiag); CHKERRQ(ierr);
155 ierr = VecDestroy(&RDiag); CHKERRQ(ierr);
156 ierr = VecDestroy(&MHatDiag); CHKERRQ(ierr);
157 ierr = MatDestroy(&R); CHKERRQ(ierr);
158 ierr = MatDestroy(&MHat); CHKERRQ(ierr);
161 ierr = MatScale(GH[1], -1.0); CHKERRQ(ierr);
164 ierr = MatCreateNest(
165 comm, 1,
nullptr, 2,
nullptr, GH, &R); CHKERRQ(ierr);
166 ierr = MatConvert(R, MATAIJ, MAT_INITIAL_MATRIX, &
G); CHKERRQ(ierr);
167 ierr = MatDestroy(&R); CHKERRQ(ierr);
168 ierr = MatDestroy(&GH[0]); CHKERRQ(ierr);
169 ierr = MatDestroy(&GH[1]); CHKERRQ(ierr);
172 ierr = MatCreateNest(
173 comm, 2,
nullptr, 1,
nullptr, DE, &R); CHKERRQ(ierr);
174 ierr = MatConvert(R, MATAIJ, MAT_INITIAL_MATRIX, &
D); CHKERRQ(ierr);
175 ierr = MatNestGetISs(R, is,
nullptr); CHKERRQ(ierr);
176 ierr = ISDuplicate(is[0], &
isDE[0]); CHKERRQ(ierr);
177 ierr = ISDuplicate(is[1], &
isDE[1]); CHKERRQ(ierr);
178 ierr = ISCopy(is[0],
isDE[0]); CHKERRQ(ierr);
179 ierr = ISCopy(is[1],
isDE[1]); CHKERRQ(ierr);
180 ierr = MatDestroy(&R); CHKERRQ(ierr);
181 ierr = MatDestroy(&DE[0]); CHKERRQ(ierr);
182 ierr = MatDestroy(&DE[1]); CHKERRQ(ierr);
186 N =
config[
"parameters"][
"BN"].as<PetscInt>(1);
190 BN,
G, MAT_INITIAL_MATRIX, PETSC_DEFAULT, &
BNG); CHKERRQ(ierr);
194 D,
BNG, MAT_INITIAL_MATRIX, PETSC_DEFAULT, &
DBNG); CHKERRQ(ierr);
200 ierr = MatDestroy(&BN); CHKERRQ(ierr);
202 PetscFunctionReturn(0);
210 PetscFunctionBeginUser;
213 const PetscReal *data;
216 ierr = MatCreateVecs(
G, &
P,
nullptr); CHKERRQ(ierr);
225 ierr = VecReplaceArray(
P,
nullptr); CHKERRQ(ierr);
228 ierr = VecGetSubVector(
solution->pGlobal,
isDE[0], &temp); CHKERRQ(ierr);
229 ierr = VecGetArrayRead(temp, &data); CHKERRQ(ierr);
230 ierr = VecPlaceArray(
P, data); CHKERRQ(ierr);
231 ierr = VecRestoreArrayRead(temp, &data); CHKERRQ(ierr);
232 ierr = VecRestoreSubVector(
238 PetscFunctionReturn(0);
246 PetscFunctionBeginUser;
249 ierr =
pSolver->getType(type); CHKERRQ(ierr);
251 if (type ==
"PETSc KSP")
255 ierr = MatCreateVecs(
DBNG, &n,
nullptr); CHKERRQ(ierr);
256 ierr = VecSet(n, 0.0); CHKERRQ(ierr);
257 ierr = VecGetSubVector(n,
isDE[0], &phiPortion); CHKERRQ(ierr);
258 ierr = VecSet(phiPortion, 1.0 / std::sqrt(
mesh->pN)); CHKERRQ(ierr);
259 ierr = VecRestoreSubVector(n,
isDE[0], &phiPortion); CHKERRQ(ierr);
260 ierr = MatNullSpaceCreate(
261 comm, PETSC_FALSE, 1, &n, &nsp); CHKERRQ(ierr);
262 ierr = MatSetNullSpace(
DBNG, nsp); CHKERRQ(ierr);
263 ierr = MatSetNearNullSpace(
DBNG, nsp); CHKERRQ(ierr);
264 ierr = VecDestroy(&n); CHKERRQ(ierr);
265 ierr = MatNullSpaceDestroy(&nsp); CHKERRQ(ierr);
268 else if (type ==
"NVIDIA AmgX")
270 PetscInt row[1] = {0};
271 ierr = MatZeroRowsColumns(
272 DBNG, 1, row, 1.0,
nullptr,
nullptr); CHKERRQ(ierr);
277 SETERRQ1(PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
278 "Could not recognize the type of linear solver: %s\n",
282 PetscFunctionReturn(0);
290 PetscFunctionBeginUser;
299 ierr = VecGetSubVector(
rhs2,
isDE[0], &bc2); CHKERRQ(ierr);
301 ierr = VecRestoreSubVector(
rhs2,
isDE[0], &bc2); CHKERRQ(ierr);
305 ierr = VecSetValue(
rhs2, 0, 0.0, INSERT_VALUES); CHKERRQ(ierr);
306 ierr = VecAssemblyBegin(
rhs2); CHKERRQ(ierr);
307 ierr = VecAssemblyEnd(
rhs2); CHKERRQ(ierr);
310 ierr = PetscLogStagePop(); CHKERRQ(ierr);
312 PetscFunctionReturn(0);
320 PetscFunctionBeginUser;
322 Vec temp = PETSC_NULL;
334 PetscFunctionReturn(0);
342 PetscFunctionBeginUser;
348 ierr = VecGetSubVector(
solution->pGlobal,
isDE[1], &f); CHKERRQ(ierr);
350 comm, filePath,
"/", {
"force"}, {f}, FILE_MODE_APPEND); CHKERRQ(ierr);
351 ierr = VecRestoreSubVector(
solution->pGlobal,
isDE[1], &f); CHKERRQ(ierr);
353 PetscFunctionReturn(0);
361 PetscFunctionBeginUser;
363 Vec temp = PETSC_NULL;
376 std::vector<Vec> f(1);
377 ierr = VecGetSubVector(
solution->pGlobal,
isDE[1], &f[0]); CHKERRQ(ierr);
379 comm, filePath,
"/", {
"force"}, f); CHKERRQ(ierr);
380 ierr = VecRestoreSubVector(
383 PetscFunctionReturn(0);
392 PetscFunctionBeginUser;
398 ierr = VecGetSubVector(
solution->pGlobal,
isDE[1], &f); CHKERRQ(ierr);
399 ierr =
bodies->calculateAvgForces(f, fAvg); CHKERRQ(ierr);
400 ierr = VecRestoreSubVector(
solution->pGlobal,
isDE[1], &f); CHKERRQ(ierr);
402 ierr = PetscLogStagePop(); CHKERRQ(ierr);
404 ierr = PetscLogStagePush(
stageWrite); CHKERRQ(ierr);
407 ierr = PetscViewerASCIIPrintf(
forcesViewer,
"%10.8e\t",
t); CHKERRQ(ierr);
410 for (
int i = 0; i <
bodies->nBodies; ++i)
412 for (
int d = 0; d <
mesh->dim; ++d)
414 ierr = PetscViewerASCIIPrintf(
418 ierr = PetscViewerASCIIPrintf(
forcesViewer,
"\n"); CHKERRQ(ierr);
420 ierr = PetscLogStagePop(); CHKERRQ(ierr);
422 PetscFunctionReturn(0);
virtual PetscErrorCode writeRestartDataHDF5(const std::string &filePath)
Write data required to restart a simulation into a HDF5 file.
virtual PetscErrorCode createOperators()
Create operators.
PetscErrorCode init(const MPI_Comm &world, const YAML::Node &node)
Initialize the IBPM solver.
~IBPMSolver()
Default destructor.
virtual PetscErrorCode writeForcesASCII()
Write the forces acting on the bodies into an ASCII file.
PetscLogStage stageIntegrateForces
Log stage for integrating the Lagrangian forces.
virtual PetscErrorCode createVectors()
Create vectors.
virtual PetscErrorCode readRestartDataHDF5(const std::string &filePath)
Read data required to restart a simulation from a HDF5 file.
PetscErrorCode write()
Write solution, forces, and solvers info to files.
PetscErrorCode destroy()
Manually destroy data.
IBPMSolver()=default
Default constructor.
virtual PetscErrorCode assembleRHSPoisson()
Assemble the RHS vector of the Poisson system.
virtual PetscErrorCode writeSolutionHDF5(const std::string &filePath)
Write the solution fields into a HDF5 file.
virtual PetscErrorCode setNullSpace()
Set Poisson nullspace or pin pressure at a reference point.
IS isDE[2]
Global index sets for pressure field and Lagrangian forces.
petibm::type::BodyPack bodies
Pack of immersed bodies.
Vec P
PETSc Vec object with pressure field and Lagrangian forces.
PetscViewer forcesViewer
ASCII PetscViewer object to output the forces.
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.
YAML::Node config
YAML configuration settings.
petibm::type::Boundary bc
Information about the domain boundaries.
virtual PetscErrorCode readRestartDataHDF5(const std::string &filePath)
Read data required to restart a simulation from a HDF5 file.
PetscLogStage stageInitialize
Log stage for the initialization phase.
virtual PetscErrorCode writeSolutionHDF5(const std::string &filePath)
Write the solution fields into a HDF5 file.
Mat A
Implicit operator for the velocity solver.
PetscReal dt
Time-step size.
Mat N
Convective operator (matrix-free).
Mat LCorrection
Laplacian correction operator for boundary conditions.
MPI_Comm comm
MPI communicator.
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.
petibm::type::TimeIntegration diffCoeffs
Time scheme for the diffusion terms.
Mat DCorrection
Divergence correction for boundary conditions.
PetscErrorCode write()
Write solution and solver info to files.
PetscErrorCode init(const MPI_Comm &world, const YAML::Node &node)
Initialize the Navier-Stokes solver.
PetscLogStage stageWrite
Log stage when write the solution fields.
PetscInt ite
Time-step index.
petibm::type::Mesh mesh
Structured Cartesian mesh object.
PetscLogStage stageRHSPoisson
Log stage for assembling the RHS of the Poisson system.
Mat BNG
Projection operator.
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.
Prototype of Delta functions.
PetscErrorCode createBodyPack(const MPI_Comm &comm, const PetscInt &dim, const YAML::Node &node, type::BodyPack &bodies)
Factory function to create a pack of bodies.
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 createMHead(const type::Mesh &mesh, Mat &MHead)
Create a matrix of cell widths of velocity points, .
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 createDelta(const type::Mesh &mesh, const type::Boundary &bc, const type::BodyPack &bodies, const delta::DeltaKernel &kernel, const PetscInt &kernelSize, Mat &Op)
Create a Delta operator, .
PetscErrorCode createR(const type::Mesh &mesh, Mat &R)
Create a matrix of flux areas, .
std::vector< RealVec1D > RealVec2D
2D std::vector holding PetscReal.
Definition of the class IBPMSolver.
Prototypes of I/O functions.
PetscErrorCode getKernel(const std::string &name, DeltaKernel &kernel, PetscInt &size)
Get the delta kernel and size providing the name.
std::function< PetscReal(const PetscReal &r, const PetscReal &dr)> DeltaKernel
Typedef to choose the regularized delta kernel to use.