20 const PetscReal &coeff,
const PetscInt &n,
23 PetscFunctionBeginUser;
28 SETERRQ(PETSC_COMM_WORLD, 56,
29 "The order of Bn can not be smaller than 1.");
33 ierr = MatDuplicate(Op, MAT_DO_NOT_COPY_VALUES, &BnHead); CHKERRQ(ierr);
34 ierr = MatSetOption(BnHead, MAT_KEEP_NONZERO_PATTERN, PETSC_FALSE);
36 ierr = MatSetOption(BnHead, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE);
41 ierr = MatSeqAIJSetPreallocation(BnHead, 1,
nullptr); CHKERRQ(ierr);
42 ierr = MatMPIAIJSetPreallocation(BnHead, 1,
nullptr, 0,
nullptr);
47 ierr = MatAssemblyBegin(BnHead, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
48 ierr = MatAssemblyEnd(BnHead, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
49 ierr = MatShift(BnHead, dt); CHKERRQ(ierr);
53 if (n == 1) PetscFunctionReturn(0);
56 Mat rightMat, tempResult;
59 for (PetscInt term = 2; term <= n; ++term)
62 ierr = MatDuplicate(Op, MAT_COPY_VALUES, &rightMat); CHKERRQ(ierr);
65 for (PetscInt c = 2; c < term; ++c)
68 ierr = MatMatMult(Op, rightMat, MAT_INITIAL_MATRIX, PETSC_DEFAULT,
69 &tempResult); CHKERRQ(ierr);
72 ierr = MatDestroy(&rightMat); CHKERRQ(ierr);
76 rightMat = tempResult;
80 PetscReal a = std::pow(dt, term) * std::pow(coeff, term - 1);
83 ierr = MatAXPY(BnHead, a, rightMat, DIFFERENT_NONZERO_PATTERN);
87 ierr = MatAssemblyBegin(BnHead, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
88 ierr = MatAssemblyEnd(BnHead, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
91 ierr = MatDestroy(&rightMat); CHKERRQ(ierr);
94 PetscFunctionReturn(0);
98PetscErrorCode
createBn(
const Mat &Op,
const Mat &R,
const Mat &MHead,
99 const PetscReal &dt,
const PetscReal &coeff,
100 const PetscInt &n, Mat &Bn)
102 PetscFunctionBeginUser;
107 ierr =
createBnHead(Op, dt, coeff, n, Bn); CHKERRQ(ierr);
109 Vec RDiag, MHeadDiagInv;
112 ierr = MatCreateVecs(MHead,
nullptr, &MHeadDiagInv); CHKERRQ(ierr);
113 ierr = MatGetDiagonal(MHead, MHeadDiagInv); CHKERRQ(ierr);
114 ierr = VecReciprocal(MHeadDiagInv); CHKERRQ(ierr);
117 ierr = MatCreateVecs(R,
nullptr, &RDiag); CHKERRQ(ierr);
118 ierr = MatGetDiagonal(R, RDiag); CHKERRQ(ierr);
121 ierr = MatDiagonalScale(Bn, RDiag, MHeadDiagInv); CHKERRQ(ierr);
124 ierr = VecDestroy(&RDiag); CHKERRQ(ierr);
125 ierr = VecDestroy(&MHeadDiagInv); CHKERRQ(ierr);
127 PetscFunctionReturn(0);
131PetscErrorCode
createBn(
const Mat &Op,
const Mat &M,
const PetscReal &dt,
132 const PetscReal &coeff,
const PetscInt &n, Mat &Bn)
134 PetscFunctionBeginUser;
141 SETERRQ(PETSC_COMM_WORLD, 56,
142 "The order of Bn can not be smaller than 1.");
146 ierr = MatDuplicate(Op, MAT_DO_NOT_COPY_VALUES, &Bn); CHKERRQ(ierr);
147 ierr = MatSetOption(Bn, MAT_KEEP_NONZERO_PATTERN, PETSC_FALSE);
149 ierr = MatSetOption(Bn, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE);
154 ierr = MatSeqAIJSetPreallocation(Bn, 1,
nullptr); CHKERRQ(ierr);
155 ierr = MatMPIAIJSetPreallocation(Bn, 1,
nullptr, 0,
nullptr);
160 ierr = MatCreateVecs(M,
nullptr, &MDiagInv); CHKERRQ(ierr);
161 ierr = MatGetDiagonal(M, MDiagInv); CHKERRQ(ierr);
162 ierr = VecReciprocal(MDiagInv); CHKERRQ(ierr);
163 ierr = MatDiagonalSet(Bn, MDiagInv, INSERT_VALUES); CHKERRQ(ierr);
164 ierr = MatScale(Bn, dt); CHKERRQ(ierr);
168 if (n == 1) PetscFunctionReturn(0);
171 Mat leftMat, rightMat, tempResult;
174 ierr = MatDuplicate(Op, MAT_COPY_VALUES, &leftMat); CHKERRQ(ierr);
175 ierr = MatDiagonalScale(leftMat, MDiagInv,
nullptr); CHKERRQ(ierr);
177 for (PetscInt term = 2; term <= n; ++term)
180 ierr = MatDuplicate(leftMat, MAT_COPY_VALUES, &rightMat);
184 for (PetscInt c = 2; c < term; ++c)
187 ierr = MatMatMult(leftMat, rightMat, MAT_INITIAL_MATRIX,
188 PETSC_DEFAULT, &tempResult); CHKERRQ(ierr);
191 ierr = MatDestroy(&rightMat); CHKERRQ(ierr);
195 rightMat = tempResult;
199 ierr = MatDiagonalScale(rightMat,
nullptr, MDiagInv); CHKERRQ(ierr);
202 PetscReal a = std::pow(dt, term) * std::pow(coeff, term - 1);
205 ierr = MatAXPY(Bn, a, rightMat, DIFFERENT_NONZERO_PATTERN);
209 ierr = MatAssemblyBegin(Bn, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
210 ierr = MatAssemblyEnd(Bn, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
213 ierr = MatDestroy(&rightMat); CHKERRQ(ierr);
217 ierr = MatDestroy(&leftMat); CHKERRQ(ierr);
219 PetscFunctionReturn(0);
PetscErrorCode createBn(const Mat &Op, const Mat &R, const Mat &MHead, const PetscReal &dt, const PetscReal &coeff, const PetscInt &n, Mat &Bn)
Create normalized matrix of approximated , .
PetscErrorCode createBnHead(const Mat &Op, const PetscReal &dt, const PetscReal &coeff, const PetscInt &n, Mat &BnHead)
Create non-normalized matrix of approximated , .
A toolbox for building flow solvers.