27typedef std::function<PetscReal(
const PetscInt &,
const PetscInt &,
44 const std::vector<KernelType> &kernels, Mat &M)
46 PetscFunctionBeginUser;
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);
61 ierr = MatSetOption(M, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE); CHKERRQ(ierr);
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];
71 MatStencil self = {k, j, i, 1};
74 ierr = mesh->getPackedGlobalIndex(field, self, idx);
78 ierr = MatSetValue(M, idx, idx, kernels[field](i, j, k),
79 INSERT_VALUES); CHKERRQ(ierr);
83 ierr = MatAssemblyBegin(M, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
84 ierr = MatAssemblyEnd(M, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
86 PetscFunctionReturn(0);
92 PetscFunctionBeginUser;
97 std::vector<KernelType> kernel(3);
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];
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];
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];
116 PetscFunctionReturn(0);
122 PetscFunctionBeginUser;
127 std::vector<KernelType> kernel(3);
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]);
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]);
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]);
146 PetscFunctionReturn(0);
152 PetscFunctionBeginUser;
157 std::vector<KernelType> kernel(3);
160 kernel[0] = [&mesh](
const PetscInt &i,
const PetscInt &j,
161 const PetscInt &k) -> PetscReal {
162 return mesh->dL[0][0][i];
164 kernel[1] = [&mesh](
const PetscInt &i,
const PetscInt &j,
165 const PetscInt &k) -> PetscReal {
166 return mesh->dL[1][1][j];
168 kernel[2] = [&mesh](
const PetscInt &i,
const PetscInt &j,
169 const PetscInt &k) -> PetscReal {
170 return mesh->dL[2][2][k];
176 PetscFunctionReturn(0);
182 PetscFunctionBeginUser;
187 std::vector<KernelType> kernel(3);
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]);
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]);
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]);
206 PetscFunctionReturn(0);
212 PetscFunctionBeginUser;
217 std::vector<KernelType> kernel(3);
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; };
230 PetscFunctionReturn(0);
std::shared_ptr< mesh::MeshBase > Mesh
Type definition of Mesh.
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.