38 PetscFunctionBeginUser;
43 ierr = PetscFinalized(&finalized); CHKERRV(ierr);
44 if (finalized)
return;
46 std::vector<AO>().swap(
ao);
52 PetscFunctionBeginUser;
58 std::vector<AO>().swap(
ao);
65 PetscFunctionReturn(0);
70 const YAML::Node &node)
72 PetscFunctionBeginUser;
81 ierr = MPI_Comm_size(
comm, &
mpiSize); CHKERRQ(ierr);
82 ierr = MPI_Comm_rank(
comm, &
mpiRank); CHKERRQ(ierr);
96 da = std::vector<DM>(5, PETSC_NULL);
101 ao = std::vector<AO>(4);
121 ierr = MPI_Barrier(
comm); CHKERRQ(ierr);
130 ierr = MPI_Barrier(
comm); CHKERRQ(ierr);
132 PetscFunctionReturn(0);
138 PetscFunctionBeginUser;
141 for (
int i = 0; i <
dim; ++i)
146 std::partial_sum(
dLTrue[3][i].begin(),
dLTrue[3][i].end(),
149 auto f = [i,
this](
double &a,
double &b) ->
double {
150 return a + this->
min[i] - 0.5 * b;
164 pN =
n[3][0] *
n[3][1] *
n[3][2];
173 PetscFunctionReturn(0);
179 PetscFunctionBeginUser;
183 for (
int i = 0; i <
dim; ++i)
186 n[4][i] =
n[3][i] + 1;
196 [i,
this](
double &a) ->
void { a += this->
min[i]; });
205 PetscFunctionReturn(0);
211 PetscFunctionBeginUser;
218 for (
int comp = 0; comp <
dim; comp++)
221 for (
int dir = 0; dir <
dim; ++dir)
227 n[comp][dir] =
n[3][dir] - 1;
230 dLTrue[comp][dir].resize(
n[comp][dir] + 2);
231 coordTrue[comp][dir].resize(
n[comp][dir] + 2);
237 auto f = [](
const PetscReal &
x,
238 const PetscReal &
y) -> PetscReal {
239 return 0.5 * (
x +
y);
245 std::adjacent_difference(
dLTrue[3][dir].begin(),
247 dLTrue[comp][dir].begin(), f);
258 dLTrue[comp][dir].back() =
285 n[comp][dir] =
n[3][dir];
288 dLTrue[comp][dir].resize(
n[comp][dir] + 2);
289 coordTrue[comp][dir].resize(
n[comp][dir] + 2);
296 dLTrue[comp][dir].begin() + 1);
330 dL[comp][dir] = &
dLTrue[comp][dir][1];
335 UN += (
n[comp][0] *
n[comp][1] *
n[comp][2]);
354 PetscFunctionReturn(0);
360 PetscFunctionBeginUser;
364 std::stringstream ss;
368 ss << std::string(80,
'=') << std::endl;
369 ss <<
"Cartesian Staggered Grid:" << std::endl;
370 ss << std::string(80,
'=') << std::endl;
372 ss <<
"\tDimension: " <<
dim << std::endl;
375 ss <<
"\tDomain Range: "
376 <<
"[" <<
min[0] <<
", " <<
max[0] <<
"]; "
377 <<
"[" <<
min[1] <<
", " <<
max[1] <<
"]";
378 if (
dim == 3) ss <<
"; [" <<
min[2] <<
", " <<
max[2] <<
"]";
382 ss <<
"\tNumber of Cells (Nx x Ny" << ((
dim == 2) ?
"" :
" x Nz")
384 ss <<
"\t\tPressure Grid: ";
386 for (
int dir = 1; dir <
dim; ++dir) ss <<
" x " <<
n[3][dir];
389 for (
int comp = 0; comp <
dim; ++comp)
391 ss <<
"\t\t" <<
fd2str[
Field(comp)] <<
"-Velocity Grid: ";
393 for (
int dir = 1; dir <
dim; ++dir) ss <<
" x " <<
n[comp][dir];
398 ss <<
"\tGrid Decomposition:" << std::endl;
399 ss <<
"\t\tMPI Processes: " <<
nProc[0] <<
" x " <<
nProc[1] <<
" x "
400 <<
nProc[2] << std::endl;
408 PetscFunctionReturn(0);
414 PetscFunctionBeginUser;
416 std::string pre(
"\t\t");
418 ss << pre <<
"Rank " <<
mpiRank <<
": " << std::endl;
419 ss << pre <<
"\tPressure Grid: ";
420 ss <<
"[" <<
bg[3][0] <<
", " <<
ed[3][0] <<
"), ";
421 ss <<
"[" <<
bg[3][1] <<
", " <<
ed[3][1] <<
"), ";
422 ss <<
"[" <<
bg[3][2] <<
", " <<
ed[3][2] <<
") ";
425 for (
int comp = 0; comp <
dim; ++comp)
427 ss << pre <<
"\t" <<
fd2str[
Field(comp)] <<
"-Velocity Grid: ";
428 ss <<
"[" <<
bg[comp][0] <<
", " <<
ed[comp][0] <<
"), ";
429 ss <<
"[" <<
bg[comp][1] <<
", " <<
ed[comp][1] <<
"), ";
430 ss <<
"[" <<
bg[comp][2] <<
", " <<
ed[comp][2] <<
") ";
434 PetscFunctionReturn(0);
440 PetscFunctionBeginUser;
448 for (PetscInt f = 0; f <
dim; ++f)
454 ierr = MPI_Barrier(
comm); CHKERRQ(ierr);
456 1, MPIU_INT,
comm); CHKERRQ(ierr);
464 for (PetscInt f = 0; f <
dim; ++f)
466 for (PetscMPIInt r =
mpiSize - 1; r > 0; --r)
469 for (PetscMPIInt r = 1; r <
mpiSize; ++r)
474 for (PetscMPIInt r = 0; r <
mpiSize; ++r)
479 for (PetscMPIInt r =
mpiSize - 1; r > 0; --r)
482 for (PetscMPIInt r = 1; r <
mpiSize; ++r)
488 PetscFunctionReturn(0);
494 PetscFunctionBeginUser;
498 DMDALocalInfo lclInfo;
505 (
periodic[0][0]) ? DM_BOUNDARY_PERIODIC : DM_BOUNDARY_GHOSTED,
506 (
periodic[1][1]) ? DM_BOUNDARY_PERIODIC : DM_BOUNDARY_GHOSTED,
507 DMDA_STENCIL_BOX,
n[i][0],
n[i][1],
nProc[0],
nProc[1], 1, 1,
508 nullptr,
nullptr, &
da[i]); CHKERRQ(ierr);
513 (
periodic[0][0]) ? DM_BOUNDARY_PERIODIC : DM_BOUNDARY_GHOSTED,
514 (
periodic[1][1]) ? DM_BOUNDARY_PERIODIC : DM_BOUNDARY_GHOSTED,
515 (
periodic[2][2]) ? DM_BOUNDARY_PERIODIC : DM_BOUNDARY_GHOSTED,
516 DMDA_STENCIL_BOX,
n[i][0],
n[i][1],
n[i][2],
nProc[0],
nProc[1],
517 nProc[2], 1, 1,
nullptr,
nullptr,
nullptr, &
da[i]);
522 ierr = DMSetUp(
da[i]); CHKERRQ(ierr);
524 ierr = DMDAGetLocalInfo(
da[i], &lclInfo); CHKERRQ(ierr);
526 bg[i][0] = lclInfo.xs;
527 bg[i][1] = lclInfo.ys;
528 bg[i][2] = lclInfo.zs;
530 m[i][0] = lclInfo.xm;
531 m[i][1] = lclInfo.ym;
532 m[i][2] = lclInfo.zm;
534 for (
unsigned int comp = 0; comp < 3; ++comp)
535 ed[i][comp] =
bg[i][comp] +
m[i][comp];
537 PetscFunctionReturn(0);
543 PetscFunctionBeginUser;
549 ierr = DMDAGetAO(
da[3], &
ao[3]); CHKERRQ(ierr);
551 ierr = DMDAGetInfo(
da[3],
nullptr,
nullptr,
nullptr,
nullptr, &
nProc[0],
552 &
nProc[1], &
nProc[2],
nullptr,
nullptr,
nullptr,
nullptr,
553 nullptr,
nullptr); CHKERRQ(ierr);
555 PetscFunctionReturn(0);
561 PetscFunctionBeginUser;
565 ierr = DMCompositeCreate(
comm, &
UPack); CHKERRQ(ierr);
567 for (
int i = 0; i <
dim; ++i)
570 ierr = DMDAGetAO(
da[i], &
ao[i]); CHKERRQ(ierr);
571 ierr = DMCompositeAddDM(
UPack,
da[i]); CHKERRQ(ierr);
574 PetscFunctionReturn(0);
582 PetscFunctionBeginUser;
588 PetscFunctionReturn(0);
598 PetscFunctionBeginUser;
601 if ((i < -1) || (i >
n[f][0]) || (j < -1) || (j >
n[f][1]) || (k < -1) ||
603 SETERRQ4(PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
604 "Stencil (%d, %d, %d) of field %d is out of domain.\n", i, j,
611 idx = (
n[f][0] - 1) + j *
n[f][0] +
612 ((
dim == 3) ? (k *
n[f][1] *
n[f][0]) : 0);
616 PetscFunctionReturn(0);
622 idx = j *
n[f][0] + ((
dim == 3) ? (k *
n[f][1] *
n[f][0]) : 0);
626 PetscFunctionReturn(0);
632 idx = i + (
n[f][1] - 1) *
n[f][0] +
633 ((
dim == 3) ? (k *
n[f][1] *
n[f][0]) : 0);
637 PetscFunctionReturn(0);
643 idx = i + ((
dim == 3) ? (k *
n[f][1] *
n[f][0]) : 0);
647 PetscFunctionReturn(0);
653 idx = i + j *
n[f][0] +
654 ((
dim == 3) ? ((
n[f][2] - 1) *
n[f][1] *
n[f][0]) : 0);
658 PetscFunctionReturn(0);
664 idx = i + j *
n[f][0];
668 PetscFunctionReturn(0);
673 idx = i + j *
n[f][0] + ((
dim == 3) ? (k *
n[f][1] *
n[f][0]) : 0);
675 PetscFunctionReturn(0);
683 PetscFunctionBeginUser;
687 ierr = DMDAConvertToCell(
da[f], s, &idx); CHKERRQ(ierr);
689 PetscFunctionReturn(0);
699 PetscFunctionBeginUser;
705 PetscFunctionReturn(0);
713 PetscFunctionBeginUser;
718 ierr = AOApplicationToPetsc(
ao[f], 1, &idx); CHKERRQ(ierr);
720 PetscFunctionReturn(0);
730 PetscFunctionBeginUser;
736 PetscFunctionReturn(0);
744 PetscFunctionBeginUser;
750 PetscInt unPackedIdx;
757 if ((f == 3) || (unPackedIdx == -1))
760 PetscFunctionReturn(0);
772 for (PetscInt i = 0; i < f; ++i)
778 PetscFunctionReturn(0);
788 PetscFunctionBeginUser;
794 PetscFunctionReturn(0);
800 PetscFunctionBeginUser;
806 PetscFileMode mode = FILE_MODE_WRITE;
807 std::vector<std::string> names{
"x",
"y",
"z"};
809 for (
unsigned int f = 0; f < 5; ++f)
814 n[f],
coord[f], mode); CHKERRQ(ierr);
816 mode = FILE_MODE_APPEND;
820 ierr = MPI_Barrier(
comm); CHKERRQ(ierr);
822 PetscFunctionReturn(0);
828 PetscFunctionBeginUser;
830 for (PetscInt d = 0; d <
dim; ++d)
832 PetscInt start =
bg[field][d], end =
ed[field][d];
833 if (point[d] <
coord[field][d][start] ||
834 point[d] >=
coord[field][d][end])
Definition of class mesh::CartesianMesh.
type::IntVec1D UPackNLocalAllProcs
Number of local packed velocity points (without ghost points) for all processes.
virtual PetscErrorCode getNaturalIndex(const PetscInt &f, const MatStencil &s, PetscInt &idx) const
Get the natural index of a point by providing MatStencil.
type::RealVec3D dLTrue
Underlying data for mesh point spacing.
PetscErrorCode createSingleDMDA(const PetscInt &i)
Function for creating a single DMDA.
virtual PetscErrorCode getGlobalIndex(const PetscInt &f, const MatStencil &s, PetscInt &idx) const
Get the global index of a point in unpacked DM by providing MatStencil.
virtual PetscErrorCode write(const std::string &filePath) const
Write the mesh object into a HDF5 file.
virtual PetscErrorCode getPackedGlobalIndex(const PetscInt &f, const MatStencil &s, PetscInt &idx) const
Get the global index of a point in packed DM by providing MatStencil.
PetscErrorCode initDMDA()
Initialize DMDAs.
type::RealVec3D coordTrue
Underlying data for mesh point coordinates.
type::IntVec1D offsetsPackAllProcs
Offsets of packed velocity points in packed DM.
PetscErrorCode addLocalInfoString(std::stringstream &ss)
Gather info of parallel distribution and add to info string.
virtual ~CartesianMesh()
Default destructor.
PetscErrorCode createVelocityMesh()
Create velocity mesh information.
std::vector< AO > ao
References to underlying AO objects of velocity DMs.
PetscErrorCode createPressureDMDA()
Create DMDA for pressure.
virtual PetscErrorCode getLocalIndex(const PetscInt &f, const MatStencil &s, PetscInt &idx) const
Get the local index of a point by providing MatStencil.
type::IntVec2D UNLocalAllProcs
Number of local velocity points (without ghost points) for all processes and all velocity fields.
virtual PetscErrorCode destroy()
Manually destroy data.
PetscErrorCode init(const MPI_Comm &world, const YAML::Node &node)
Initialization.
PetscErrorCode createPressureMesh()
Create pressure mesh information.
PetscErrorCode createVertexMesh()
Create vertex information.
PetscErrorCode createInfoString()
Create a string of information.
virtual PetscBool isPointOnLocalProc(const type::RealVec1D &point, const type::Field &field)
CartesianMesh(const MPI_Comm &world, const YAML::Node &node)
Constructor.
type::IntVec2D offsetsAllProcs
Offsets of velocity points in unpacked DMs for all processes and all velocity field.
PetscErrorCode createVelocityPack()
Create DMDAs for velocity fields and make a DMComposite.
type::RealVec1D max
Maximum coordinates of boundaries in all directions.
type::IntVec2D n
Total number of points of all fields and in all directions.
virtual PetscErrorCode destroy()
Manually destroy data.
DM UPack
DMComposte of velocity DMs.
MPI_Comm comm
Communicator.
type::IntVec2D ed
The ending index of all fields in all directions of this process.
std::string info
A string for printing information.
PetscMPIInt mpiRank
Rank of this process.
type::IntVec2D bg
The beginning index of all fields in all directions of this process.
PetscInt UNLocal
Total number of velocity points local to this process.
type::IntVec1D nProc
Number of processes in all directions.
type::GhostedVec3D coord
Coordinates of mesh points of all fields and in all directions.
PetscInt UN
Total number of velocity points.
type::GhostedVec3D dL
Spacings of mesh points of all fields and in all directions.
PetscMPIInt mpiSize
Total number of processes.
PetscInt pNLocal
Total number of pressure points local to this process.
std::vector< DM > da
A vector of DMs of all fields.
type::RealVec1D min
Minimum coordinates of boundaries in all directions.
type::BoolVec2D periodic
Bools indicating if any direction is periodic.
PetscInt pN
Total number of pressure points.
type::IntVec2D m
The number of points of all fields in all directions of this process.
PetscErrorCode parseMesh(const YAML::Node &meshNode, PetscInt &dim, type::RealVec1D &bg, type::RealVec1D &ed, type::IntVec1D &nTotal, type::RealVec2D &dL)
Parse a YAML node of cartesianMesh.
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 parseBCs(const YAML::Node &node, type::IntVec2D &bcTypes, type::RealVec2D &bcValues)
Parse boundary conditions from a YAML node.
PetscErrorCode checkPeriodicBC(const type::IntVec2D &bcTypes, type::BoolVec2D &periodic)
Check if there is any periodic boundary condition and if these periodic BCs make sense.
std::vector< IntVec1D > IntVec2D
2D std::vector holding PetscInt.
std::vector< PetscReal * > GhostedVec2D
a vector of pointers to mimic ghosted 1D vectors.
std::map< Field, std::string > fd2str
Mapping between Field and std::string.
std::vector< RealVec1D > RealVec2D
2D std::vector holding PetscReal.
std::vector< PetscInt > IntVec1D
1D std::vector holding PetscInt.
std::vector< PetscReal > RealVec1D
1D std::vector holding PetscReal.
std::vector< RealVec2D > RealVec3D
3D std::vector holding PetscReal.
Prototypes of I/O functions.
Prototypes of some miscellaneous functions.
std::vector< GhostedVec2D > GhostedVec3D
a vector of vector pointers to mimic ghosted 2D vectors. type
A toolbox for building flow solvers.
Prototypes of parser functions.