PetIBM 0.5.4
Toolbox and applications of the immersed-boundary method for distributed-memory architectures
createbnhead_test.cpp
Go to the documentation of this file.
1
8#include <cmath>
9
10#include <petsc.h>
11
12#include <gtest/gtest.h>
13
14#include <petibm/operators.h>
15
16// Test Mat BNHat up to N=10 when Op is a diagonal Mat
17TEST(operatorsCreateBnHeadTest, testBNHatDiagonalOp)
18{
19 Mat Op; // operator (e.g., Laplacian)
20 PetscReal dt = 2.3, // time-step size
21 c = 0.5, // time-scheme coefficient of the implicit diffusive term
22 val = 2.0 / dt; // value set on the diagonal of the operator
23 PetscInt nx = 10, // number of points in the x-direction
24 ny = 12; // number of points in the y-direction
25 PetscReal ans = nx * ny * dt; // expected sum of all elements of B1Hat
26 // Create and assemble operator
27 MatCreate(PETSC_COMM_WORLD, &Op);
28 MatSetSizes(Op, PETSC_DECIDE, PETSC_DECIDE, nx * ny, nx * ny);
29 MatSetType(Op, MATAIJ);
30 MatSetUp(Op);
31 for (PetscInt i = 0; i < nx * ny; i++)
32 MatSetValues(Op, 1, &i, 1, &i, &val, INSERT_VALUES);
33 MatAssemblyBegin(Op, MAT_FINAL_ASSEMBLY);
34 MatAssemblyEnd(Op, MAT_FINAL_ASSEMBLY);
35 for (PetscInt N = 1; N <= 10; N++)
36 {
37 Mat BNHat; // Nth-order approximation of the inverse of (I/dt - c*Op)
38 // Call function to test
39 petibm::operators::createBnHead(Op, dt, c, N, BNHat);
40 // Check size of Mat BNHat
41 {
42 PetscInt nrows, ncols;
43 MatGetSize(BNHat, &nrows, &ncols);
44 ASSERT_EQ(nx * ny, nrows);
45 ASSERT_EQ(nx * ny, ncols);
46 }
47 // Check sum of elements of BNHat is the expected value
48 if (N > 1) ans += dt * nx * ny * std::pow(c * dt * val, N - 1);
49 {
50 PetscReal sum;
51 Vec v;
52 MatCreateVecs(Op, &v, nullptr);
53 MatGetRowSum(BNHat, v);
54 VecSum(v, &sum);
55 ASSERT_TRUE(std::abs(sum - ans) <= 1.0E-11);
56 VecDestroy(&v);
57 }
58 MatDestroy(&BNHat);
59 }
60 MatDestroy(&Op);
61}
62
63// Run all tests
64int main(int argc, char **argv)
65{
66 PetscErrorCode ierr, status;
67
68 ::testing::InitGoogleTest(&argc, argv);
69 ierr = PetscInitialize(&argc, &argv, nullptr, nullptr); CHKERRQ(ierr);
70 status = RUN_ALL_TESTS();
71 ierr = PetscFinalize(); CHKERRQ(ierr);
72
73 return status;
74} // main
int main(int argc, char **argv)
TEST(operatorsCreateBnHeadTest, testBNHatDiagonalOp)
PetscErrorCode createBnHead(const Mat &Op, const PetscReal &dt, const PetscReal &coeff, const PetscInt &n, Mat &BnHead)
Create non-normalized matrix of approximated , .
Definition: createbn.cpp:19
Prototypes of factory functions for operators.