22#include "../private/private.h"
30typedef std::function<
StencilVec(
const PetscInt &,
const PetscInt &,
41 : bc(_bc), modifier(bc->dim){};
45PetscErrorCode LCorrectionMult(Mat mat, Vec
x, Vec
y)
47 PetscFunctionBeginUser;
54 ierr = MatShellGetContext(mat, (
void *)&ctx); CHKERRQ(ierr);
57 ierr = VecSet(
y, 0.0); CHKERRQ(ierr);
60 for (PetscInt f = 0; f < ctx->bc->dim; ++f)
61 for (
auto &bd : ctx->bc->bds[f])
63 for (
auto &pt : bd->points)
66 y, ctx->modifier[f][pt.first].row,
67 ctx->modifier[f][pt.first].coeff * pt.second.a1,
68 ADD_VALUES); CHKERRQ(ierr);
74 ierr = VecAssemblyBegin(
y); CHKERRQ(ierr);
75 ierr = VecAssemblyEnd(
y); CHKERRQ(ierr);
77 PetscFunctionReturn(0);
81PetscErrorCode LCorrectionDestroy(Mat mat)
83 PetscFunctionBeginUser;
89 ierr = MatShellGetContext(mat, (
void *)&ctx); CHKERRQ(ierr);
94 PetscFunctionReturn(0);
108inline PetscErrorCode setRowValues(
110 const GetStencilsFunc &getStencils,
const PetscInt &f,
const PetscInt &i,
111 const PetscInt &j,
const PetscInt &k, Mat &L,
112 std::map<MatStencil, petibm::type::RowModifier> &rowModifiers)
114 PetscFunctionBeginUser;
125 for (
unsigned int id = 0;
id < stencils.size(); ++id)
127 ierr = mesh->getPackedGlobalIndex(f, stencils[
id], cols[
id]);
134 for (PetscInt dir = 0; dir < mesh->dim; ++dir)
137 const PetscInt &self = (dir == 0) ? i : (dir == 1) ? j : k;
140 const PetscReal &dLSelf = mesh->dL[f][dir][self];
141 const PetscReal &dLNeg =
142 mesh->coord[f][dir][self] - mesh->coord[f][dir][self - 1];
143 const PetscReal &dLPos =
144 mesh->coord[f][dir][self + 1] - mesh->coord[f][dir][self];
146 values[dir * 2 + 1] = 1.0 / (dLNeg * dLSelf);
147 values[dir * 2 + 2] = 1.0 / (dLPos * dLSelf);
151 values[0] = -std::accumulate(values.begin() + 1, values.end(), 0.0);
154 ierr = MatSetValues(L, 1, &cols[0], stencils.size(), cols.data(),
155 values.data(), INSERT_VALUES); CHKERRQ(ierr);
158 for (
unsigned int id = 0;
id < stencils.size(); ++id)
159 if (cols[
id] == -1) rowModifiers[stencils[id]] = {cols[0], values[id]};
161 PetscFunctionReturn(0);
171 Mat &L, Mat &LCorrection)
173 using namespace std::placeholders;
175 PetscFunctionBeginUser;
179 GetStencilsFunc getStencils;
184 ctx =
new LagrangianCtx(bc);
188 getStencils = [](
const PetscInt &i,
const PetscInt &j,
190 return {{k, j, i, 0},
197 getStencils = [](
const PetscInt &i,
const PetscInt &j,
199 return {{k, j, i, 0}, {k, j, i - 1, 0}, {k, j, i + 1, 0},
200 {k, j - 1, i, 0}, {k, j + 1, i, 0}, {k - 1, j, i, 0},
205 ierr = DMCreateMatrix(mesh->UPack, &L); CHKERRQ(ierr);
206 ierr = MatSetFromOptions(L); CHKERRQ(ierr);
207 ierr = MatSetOption(L, MAT_KEEP_NONZERO_PATTERN, PETSC_FALSE);
209 ierr = MatSetOption(L, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE); CHKERRQ(ierr);
212 for (PetscInt field = 0; field < mesh->dim; ++field)
218 {mesh->bg[field][2], mesh->ed[field][2]},
219 {mesh->bg[field][1], mesh->ed[field][1]},
220 {mesh->bg[field][0], mesh->ed[field][0]},
221 std::bind(setRowValues, std::ref(mesh), std::ref(bc),
222 std::ref(getStencils), std::ref(field), _3, _2, _1,
223 std::ref(L), std::ref(ctx->modifier[field])));
228 ierr = MatAssemblyBegin(L, MAT_FLUSH_ASSEMBLY); CHKERRQ(ierr);
229 ierr = MatAssemblyEnd(L, MAT_FLUSH_ASSEMBLY); CHKERRQ(ierr);
232 for (PetscInt f = 0; f < mesh->dim; ++f)
233 for (
auto &bd : bc->bds[f])
235 for (
auto &pt : bd->points)
237 PetscInt col = pt.second.targetPackedId;
239 ctx->modifier[f][pt.first].coeff * pt.second.a0;
241 ierr = MatSetValue(L, ctx->modifier[f][pt.first].row, col,
242 value, ADD_VALUES); CHKERRQ(ierr);
246 ierr = MatAssemblyBegin(L, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
247 ierr = MatAssemblyEnd(L, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
250 ierr = MatCreateShell(mesh->comm, mesh->UNLocal, mesh->UNLocal,
251 PETSC_DETERMINE, PETSC_DETERMINE, (
void *)ctx,
252 &LCorrection); CHKERRQ(ierr);
254 ierr = MatShellSetOperation(LCorrection, MATOP_MULT,
255 (
void (*)(
void))LCorrectionMult);
258 ierr = MatShellSetOperation(LCorrection, MATOP_DESTROY,
259 (
void (*)(
void))LCorrectionDestroy);
262 PetscFunctionReturn(0);
boundary::BoundaryBase, type::Boundary, and factory function.
std::shared_ptr< boundary::BoundaryBase > Boundary
Type definition of petibm::type::Boundary.
std::shared_ptr< mesh::MeshBase > Mesh
Type definition of Mesh.
PetscErrorCode tripleLoops(const LoopBound &bound1, const LoopBound &bound2, const LoopBound &bound3, const std::function< PetscErrorCode(const PetscInt &, const PetscInt &, const PetscInt &)> &f)
A helper function to carry out a triple loop on a given function.
PetscErrorCode createLaplacian(const type::Mesh &mesh, const type::Boundary &bc, Mat &L, Mat &LCorrection)
Create a Laplacian operator, , and boundary correction, for velocity fields.
std::vector< std::map< MatStencil, RowModifier > > MatrixModifier
A type that holds necessary info for a matrix modifier that modifies matrix coefficient based on BCs.
std::vector< PetscInt > IntVec1D
1D std::vector holding PetscInt.
std::vector< PetscReal > RealVec1D
1D std::vector holding PetscReal.
Prototype of mesh::MeshBase, type::Mesh, and factory function.
Prototypes of some miscellaneous functions.
std::vector< MatStencil > StencilVec
STL vector holding MatStencil.
A toolbox for building flow solvers.
Definition of user-defined types for convenience.