PetIBM 0.5.4
Toolbox and applications of the immersed-boundary method for distributed-memory architectures
creategradient.cpp
Go to the documentation of this file.
1
8//
9#include <functional>
10
11// here goes PETSc headers
12#include <petscmat.h>
13
14// here goes headers from our PetIBM
15#include <petibm/mesh.h>
16#include <petibm/type.h>
17
18namespace petibm
19{
20namespace operators
21{
23typedef std::vector<MatStencil> StencilVec;
24
26typedef std::function<StencilVec(const PetscInt &, const PetscInt &,
27 const PetscInt &)>
29
31typedef std::function<type::RealVec1D(const PetscInt &, const PetscInt &,
32 const PetscInt &)>
34
35// implementation of petibm::operators::createGradient
36PetscErrorCode createGradient(const type::Mesh &mesh, Mat &G,
37 const PetscBool &normalize)
38{
39 PetscFunctionBeginUser;
40
41 PetscErrorCode ierr;
42
43 std::vector<GetNeighborFunc> getNeighbor(3);
44
45 std::vector<Kernel> kernel(3);
46
47 // set up the function used to get the IDs of neighbors
48 getNeighbor[0] = [](const PetscInt &i, const PetscInt &j,
49 const PetscInt &k) -> std::vector<MatStencil> {
50 return {{k, j, i, 0}, {k, j, i + 1, 0}};
51 };
52
53 getNeighbor[1] = [](const PetscInt &i, const PetscInt &j,
54 const PetscInt &k) -> std::vector<MatStencil> {
55 return {{k, j, i, 0}, {k, j + 1, i, 0}};
56 };
57
58 getNeighbor[2] = [](const PetscInt &i, const PetscInt &j,
59 const PetscInt &k) -> std::vector<MatStencil> {
60 return {{k, j, i, 0}, {k + 1, j, i, 0}};
61 };
62
63 // set up the kernel that calculates the entry values
64 if (normalize)
65 kernel[0] = kernel[1] = kernel[2] = std::bind([]() -> type::RealVec1D {
66 return {-1.0, 1.0};
67 });
68 else
69 {
70 kernel[0] = [&mesh](const PetscInt &i, const PetscInt &j,
71 const PetscInt &k) -> type::RealVec1D {
72 PetscReal v = 1.0 / mesh->dL[0][0][i];
73 return {-v, v};
74 };
75
76 kernel[1] = [&mesh](const PetscInt &i, const PetscInt &j,
77 const PetscInt &k) -> type::RealVec1D {
78 PetscReal v = 1.0 / mesh->dL[1][1][j];
79 return {-v, v};
80 };
81
82 kernel[2] = [&mesh](const PetscInt &i, const PetscInt &j,
83 const PetscInt &k) -> type::RealVec1D {
84 PetscReal v = 1.0 / mesh->dL[2][2][k];
85 return {-v, v};
86 };
87 }
88
89 // create matrix
90 ierr = MatCreate(mesh->comm, &G); CHKERRQ(ierr);
91 ierr = MatSetSizes(G, mesh->UNLocal, mesh->pNLocal, PETSC_DETERMINE,
92 PETSC_DETERMINE); CHKERRQ(ierr);
93 ierr = MatSetFromOptions(G); CHKERRQ(ierr);
94 ierr = MatSeqAIJSetPreallocation(G, 2, nullptr); CHKERRQ(ierr);
95 ierr = MatMPIAIJSetPreallocation(G, 2, nullptr, 1, nullptr); CHKERRQ(ierr);
96 ierr = MatSetUp(G); CHKERRQ(ierr);
97 ierr = MatSetOption(G, MAT_KEEP_NONZERO_PATTERN, PETSC_FALSE);
98 CHKERRQ(ierr);
99 ierr = MatSetOption(G, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE); CHKERRQ(ierr);
100
101 // set values to matrix
102 for (PetscInt field = 0; field < mesh->dim; ++field)
103 for (PetscInt k = mesh->bg[field][2]; k < mesh->ed[field][2]; ++k)
104 for (PetscInt j = mesh->bg[field][1]; j < mesh->ed[field][1]; ++j)
105 for (PetscInt i = mesh->bg[field][0]; i < mesh->ed[field][0];
106 ++i)
107 {
108 PetscInt rId, cId;
109
110 StencilVec loc = getNeighbor[field](i, j, k);
111
112 type::RealVec1D values = kernel[field](i, j, k);
113
114 // get packed index of this velocity point
115 ierr = mesh->getPackedGlobalIndex(field, loc[0], rId);
116 CHKERRQ(ierr);
117
118 // loop through columns
119 for (PetscInt n = 0; n < 2; ++n)
120 {
121 ierr = mesh->getPackedGlobalIndex(3, loc[n], cId);
122 CHKERRQ(ierr);
123
124 ierr =
125 MatSetValue(G, rId, cId, values[n], INSERT_VALUES);
126 CHKERRQ(ierr);
127 }
128 }
129
130 // assemble matrix
131 ierr = MatAssemblyBegin(G, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
132 ierr = MatAssemblyEnd(G, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
133
134 PetscFunctionReturn(0);
135} // createGradient
136
137} // end of namespace operators
138} // end of namespace petibm
std::shared_ptr< mesh::MeshBase > Mesh
Type definition of Mesh.
Definition: mesh.h:348
PetscErrorCode createGradient(const type::Mesh &mesh, Mat &G, const PetscBool &normalize=PETSC_TRUE)
Create a gradient operator, , for pressure field.
std::vector< PetscReal > RealVec1D
1D std::vector holding PetscReal.
Definition: type.h:140
Prototype of mesh::MeshBase, type::Mesh, and factory function.
std::function< StencilVec(const PetscInt &, const PetscInt &, const PetscInt &)> GetNeighborFunc
a function type for functions returning neighbors' stencils.
std::function< type::RealVec1D(const PetscInt &, const PetscInt &, const PetscInt &)> Kernel
a function type for functions computing matrix entries' values.
std::vector< MatStencil > StencilVec
STL vector holding MatStencil.
A toolbox for building flow solvers.
Definition: bodypack.h:52
Definition of user-defined types for convenience.