PetIBM 0.5.4
Toolbox and applications of the immersed-boundary method for distributed-memory architectures
creatediagmatrix.cpp
Go to the documentation of this file.
1
8// STL
9#include <functional>
10#include <vector>
11
12// here goes PETSc headers
13#include <petscmat.h>
14
15// here goes headers from our PetIBM
16#include <petibm/mesh.h>
17
18namespace petibm
19{
20namespace operators
21{
27typedef std::function<PetscReal(const PetscInt &, const PetscInt &,
28 const PetscInt &)>
30
43PetscErrorCode createDiagMatrix(const type::Mesh &mesh,
44 const std::vector<KernelType> &kernels, Mat &M)
45{
46 PetscFunctionBeginUser;
47
48 PetscErrorCode ierr;
49
50 // create matrix
51 ierr = MatCreate(mesh->comm, &M); CHKERRQ(ierr);
52 ierr = MatSetSizes(M, mesh->UNLocal, mesh->UNLocal, PETSC_DETERMINE,
53 PETSC_DETERMINE); CHKERRQ(ierr);
54 ierr = MatSetFromOptions(M); CHKERRQ(ierr);
55 ierr = MatSeqAIJSetPreallocation(M, 1, nullptr); CHKERRQ(ierr);
56 ierr = MatMPIAIJSetPreallocation(M, 1, nullptr, 0, nullptr); CHKERRQ(ierr);
57 ierr = MatSetUp(M); CHKERRQ(ierr);
58 ierr = MatSetOption(M, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE); CHKERRQ(ierr);
59 ierr = MatSetOption(M, MAT_KEEP_NONZERO_PATTERN, PETSC_FALSE);
60 CHKERRQ(ierr);
61 ierr = MatSetOption(M, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE); CHKERRQ(ierr);
62
63 // set values to matrix
64 for (PetscInt field = 0; field < mesh->dim; ++field)
65 for (PetscInt k = mesh->bg[field][2]; k < mesh->ed[field][2]; ++k)
66 for (PetscInt j = mesh->bg[field][1]; j < mesh->ed[field][1]; ++j)
67 for (PetscInt i = mesh->bg[field][0]; i < mesh->ed[field][0];
68 ++i)
69 {
70 PetscInt idx;
71 MatStencil self = {k, j, i, 1};
72
73 // get packed index of this velocity point
74 ierr = mesh->getPackedGlobalIndex(field, self, idx);
75 CHKERRQ(ierr);
76
77 // set value
78 ierr = MatSetValue(M, idx, idx, kernels[field](i, j, k),
79 INSERT_VALUES); CHKERRQ(ierr);
80 }
81
82 // assemble matrix
83 ierr = MatAssemblyBegin(M, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
84 ierr = MatAssemblyEnd(M, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
85
86 PetscFunctionReturn(0);
87} // createDiagMatrix
88
89// implementation of petibm::operators::createR
90PetscErrorCode createR(const type::Mesh &mesh, Mat &R)
91{
92 PetscFunctionBeginUser;
93
94 PetscErrorCode ierr;
95
96 // kernels are kernel functions to calculate entry values
97 std::vector<KernelType> kernel(3);
98
99 // set kernels
100 kernel[0] = [&mesh](const PetscInt &i, const PetscInt &j,
101 const PetscInt &k) -> PetscReal {
102 return mesh->dL[0][1][j] * mesh->dL[0][2][k];
103 };
104 kernel[1] = [&mesh](const PetscInt &i, const PetscInt &j,
105 const PetscInt &k) -> PetscReal {
106 return mesh->dL[1][0][i] * mesh->dL[1][2][k];
107 };
108 kernel[2] = [&mesh](const PetscInt &i, const PetscInt &j,
109 const PetscInt &k) -> PetscReal {
110 return mesh->dL[2][0][i] * mesh->dL[2][1][j];
111 };
112
113 // call the function to create matrix
114 ierr = createDiagMatrix(mesh, kernel, R); CHKERRQ(ierr);
115
116 PetscFunctionReturn(0);
117} // createR
118
119// implementation of petibm::operators::createRInv
120PetscErrorCode createRInv(const type::Mesh &mesh, Mat &RInv)
121{
122 PetscFunctionBeginUser;
123
124 PetscErrorCode ierr;
125
126 // kernels are kernel functions to calculate entry values
127 std::vector<KernelType> kernel(3);
128
129 // set kernels
130 kernel[0] = [&mesh](const PetscInt &i, const PetscInt &j,
131 const PetscInt &k) -> PetscReal {
132 return 1.0 / (mesh->dL[0][1][j] * mesh->dL[0][2][k]);
133 };
134 kernel[1] = [&mesh](const PetscInt &i, const PetscInt &j,
135 const PetscInt &k) -> PetscReal {
136 return 1.0 / (mesh->dL[1][0][i] * mesh->dL[1][2][k]);
137 };
138 kernel[2] = [&mesh](const PetscInt &i, const PetscInt &j,
139 const PetscInt &k) -> PetscReal {
140 return 1.0 / (mesh->dL[2][0][i] * mesh->dL[2][1][j]);
141 };
142
143 // call the function to create matrix
144 ierr = createDiagMatrix(mesh, kernel, RInv); CHKERRQ(ierr);
145
146 PetscFunctionReturn(0);
147} // createRInv
148
149// implementation of petibm::operators::createMHead
150PetscErrorCode createMHead(const type::Mesh &mesh, Mat &MHead)
151{
152 PetscFunctionBeginUser;
153
154 PetscErrorCode ierr;
155
156 // kernels are kernel functions to calculate entry values
157 std::vector<KernelType> kernel(3);
158
159 // set kernels
160 kernel[0] = [&mesh](const PetscInt &i, const PetscInt &j,
161 const PetscInt &k) -> PetscReal {
162 return mesh->dL[0][0][i];
163 };
164 kernel[1] = [&mesh](const PetscInt &i, const PetscInt &j,
165 const PetscInt &k) -> PetscReal {
166 return mesh->dL[1][1][j];
167 };
168 kernel[2] = [&mesh](const PetscInt &i, const PetscInt &j,
169 const PetscInt &k) -> PetscReal {
170 return mesh->dL[2][2][k];
171 };
172
173 // call the function to create matrix
174 ierr = createDiagMatrix(mesh, kernel, MHead); CHKERRQ(ierr);
175
176 PetscFunctionReturn(0);
177} // createMHead
178
179// implementation of petibm::operators::createM
180PetscErrorCode createM(const type::Mesh &mesh, Mat &M)
181{
182 PetscFunctionBeginUser;
183
184 PetscErrorCode ierr;
185
186 // kernels are kernel functions to calculate entry values
187 std::vector<KernelType> kernel(3);
188
189 // set kernels
190 kernel[0] = [&mesh](const PetscInt &i, const PetscInt &j,
191 const PetscInt &k) -> PetscReal {
192 return mesh->dL[0][0][i] / (mesh->dL[0][1][j] * mesh->dL[0][2][k]);
193 };
194 kernel[1] = [&mesh](const PetscInt &i, const PetscInt &j,
195 const PetscInt &k) -> PetscReal {
196 return mesh->dL[1][1][j] / (mesh->dL[1][0][i] * mesh->dL[1][2][k]);
197 };
198 kernel[2] = [&mesh](const PetscInt &i, const PetscInt &j,
199 const PetscInt &k) -> PetscReal {
200 return mesh->dL[2][2][k] / (mesh->dL[2][0][i] * mesh->dL[2][1][j]);
201 };
202
203 // call the function to create matrix
204 ierr = createDiagMatrix(mesh, kernel, M); CHKERRQ(ierr);
205
206 PetscFunctionReturn(0);
207} // createM
208
209// implementation of petibm::operators::createIdentity
210PetscErrorCode createIdentity(const type::Mesh &mesh, Mat &I)
211{
212 PetscFunctionBeginUser;
213
214 PetscErrorCode ierr;
215
216 // kernels are kernel functions to calculate entry values
217 std::vector<KernelType> kernel(3);
218
219 // set kernels
220 kernel[0] = [&mesh](const PetscInt &i, const PetscInt &j,
221 const PetscInt &k) -> PetscReal { return 1.0; };
222 kernel[1] = [&mesh](const PetscInt &i, const PetscInt &j,
223 const PetscInt &k) -> PetscReal { return 1.0; };
224 kernel[2] = [&mesh](const PetscInt &i, const PetscInt &j,
225 const PetscInt &k) -> PetscReal { return 1.0; };
226
227 // call the function to create matrix
228 ierr = createDiagMatrix(mesh, kernel, I); CHKERRQ(ierr);
229
230 PetscFunctionReturn(0);
231} // createIdentity
232
233} // end of namespace operators
234} // end of namespace petibm
std::shared_ptr< mesh::MeshBase > Mesh
Type definition of Mesh.
Definition: mesh.h:348
PetscErrorCode createRInv(const type::Mesh &mesh, Mat &RInv)
Create a matrix of inversed flux areas, .
PetscErrorCode createMHead(const type::Mesh &mesh, Mat &MHead)
Create a matrix of cell widths of velocity points, .
PetscErrorCode createIdentity(const type::Mesh &mesh, Mat &I)
Create an identity matrix for velocity fields.
PetscErrorCode createM(const type::Mesh &mesh, Mat &M)
Create a matrix .
PetscErrorCode createR(const type::Mesh &mesh, Mat &R)
Create a matrix of flux areas, .
Prototype of mesh::MeshBase, type::Mesh, and factory function.
std::function< PetscReal(const PetscInt &, const PetscInt &, const PetscInt &)> KernelType
a short name for the signatures of kernels.
PetscErrorCode createDiagMatrix(const type::Mesh &mesh, const std::vector< KernelType > &kernels, Mat &M)
a function to create diagonal matrix according to input kernel.
A toolbox for building flow solvers.
Definition: bodypack.h:52