The finite element method using deal.II - 2021/2022
poisson_tester.cc
Go to the documentation of this file.
1 #include "poisson_tester.h"
2 
3 #include <gtest/gtest.h>
4 
5 #include <fstream>
6 #include <sstream>
7 
8 using namespace dealii;
9 
10 using PoissonTestTypes = ::testing::Types<std::integral_constant<int, 1>,
11  std::integral_constant<int, 2>,
12  std::integral_constant<int, 3>>;
13 
14 
16 
18 
20 {
21  // Output dimension
22  std::cout << "Working on dim " << TypeParam::value << std::endl;
23  this->make_grid();
24 }
25 
26 
27 
28 // Test only two dimensional code
29 TEST_F(Poisson2DTester, TestLinear)
30 {
31  std::stringstream str;
32 
33  str << "subsection Poisson<2>" << std::endl
34  << " set Dirichlet boundary condition expression = x" << std::endl
35  << " set Dirichlet boundary ids = 0" << std::endl
36  << " set Finite element degree = 1" << std::endl
37  << " set Forcing term expression = 0" << std::endl
38  << " set Grid generator arguments = 0: 1: false"
39  << std::endl
40  << " set Grid generator function = hyper_cube"
41  << std::endl
42  << " set Neumann boundary condition expression = 0" << std::endl
43  << " set Neumann boundary ids = " << std::endl
44  << " set Number of global refinements = 4" << std::endl
45  << " set Number of refinement cycles = 1" << std::endl
46  << " set Output filename = poisson" << std::endl
47  << " set Problem constants = pi:3.14" << std::endl
48  << "end" << std::endl;
49 
50  parse_string(str.str());
51  make_grid();
52  setup_system();
53  assemble_system();
54  solve();
55 
56  auto tmp = solution;
57  VectorTools::interpolate(dof_handler, dirichlet_boundary_condition, tmp);
58 
59  tmp -= solution;
60 
61  ASSERT_NEAR(tmp.l2_norm(), 0, 1e-10);
62 }
63 
64 // Test only two dimensional code
65 TEST_F(Poisson2DTester, TestQuadratic)
66 {
67  std::stringstream str;
68 
69  str << "subsection Poisson<2>" << std::endl
70  << " set Dirichlet boundary condition expression = x^2" << std::endl
71  << " set Dirichlet boundary ids = 0" << std::endl
72  << " set Finite element degree = 2" << std::endl
73  << " set Forcing term expression = -2" << std::endl
74  << " set Grid generator arguments = 0: 1: false"
75  << std::endl
76  << " set Grid generator function = hyper_cube"
77  << std::endl
78  << " set Neumann boundary condition expression = 0" << std::endl
79  << " set Neumann boundary ids = " << std::endl
80  << " set Number of global refinements = 4" << std::endl
81  << " set Number of refinement cycles = 1" << std::endl
82  << " set Output filename = quadratic"
83  << std::endl
84  << " set Problem constants = pi:3.14" << std::endl
85  << "end" << std::endl;
86 
87  parse_string(str.str());
88  make_grid();
89  setup_system();
90  assemble_system();
91  solve();
92 
93  auto tmp = solution;
94  VectorTools::interpolate(dof_handler, dirichlet_boundary_condition, tmp);
95 
96  tmp -= solution;
97 
98  ASSERT_NEAR(tmp.l2_norm(), 0, 1e-10);
99 }
100 
101 
102 // Test only two dimensional code
103 TEST_F(Poisson2DTester, TestMixedBC1)
104 {
105  std::stringstream str;
106 
107  str << "subsection Poisson<2>" << std::endl
108  << " set Dirichlet boundary condition expression = x^2" << std::endl
109  << " set Dirichlet boundary ids = 1,2,3" << std::endl
110  << " set Finite element degree = 2" << std::endl
111  << " set Forcing term expression = -2" << std::endl
112  << " set Grid generator arguments = 0: 1: true"
113  << std::endl
114  << " set Grid generator function = hyper_cube"
115  << std::endl
116  << " set Neumann boundary condition expression = 0" << std::endl
117  << " set Neumann boundary ids = 0" << std::endl
118  << " set Number of global refinements = 4" << std::endl
119  << " set Number of refinement cycles = 1" << std::endl
120  << " set Output filename = quadratic"
121  << std::endl
122  << " set Problem constants = pi:3.14" << std::endl
123  << "end" << std::endl;
124 
125  parse_string(str.str());
126  make_grid();
127  setup_system();
128  assemble_system();
129  solve();
130 
131  auto tmp = solution;
132  VectorTools::interpolate(dof_handler, dirichlet_boundary_condition, tmp);
133 
134  tmp -= solution;
135 
136  ASSERT_NEAR(tmp.l2_norm(), 0, 1e-10);
137 }
138 
139 
140 
141 // Test only two dimensional code
142 TEST_F(Poisson2DTester, TestLinearWithHangingNodes)
143 {
144  std::stringstream str;
145 
146  str << "subsection Poisson<2>" << std::endl
147  << " set Dirichlet boundary condition expression = x" << std::endl
148  << " set Dirichlet boundary ids = 0" << std::endl
149  << " set Finite element degree = 1" << std::endl
150  << " set Forcing term expression = 0" << std::endl
151  << " set Grid generator arguments = 0: 1: false"
152  << std::endl
153  << " set Grid generator function = hyper_cube"
154  << std::endl
155  << " set Neumann boundary condition expression = 0" << std::endl
156  << " set Neumann boundary ids = " << std::endl
157  << " set Number of global refinements = 4" << std::endl
158  << " set Number of refinement cycles = 1" << std::endl
159  << " set Output filename = lin_with_handing"
160  << std::endl
161  << " set Problem constants = pi:3.14" << std::endl
162  << "end" << std::endl;
163 
164  parse_string(str.str());
165  make_grid();
166 
167  for (unsigned int i = 0; i < 2; ++i)
168  {
169  for (const auto &cell : triangulation.active_cell_iterators())
170  if (cell->center().square() <= .25)
171  cell->set_refine_flag();
172  triangulation.execute_coarsening_and_refinement();
173  }
174 
175 
176  setup_system();
177  assemble_system();
178  solve();
179  output_results(0);
180 
181  auto tmp = solution;
182  VectorTools::interpolate(dof_handler, dirichlet_boundary_condition, tmp);
183 
184  tmp -= solution;
185 
186  ASSERT_NEAR(tmp.l2_norm(), 0, 1e-10);
187 }
VectorTools::interpolate
void interpolate(const Mapping< dim, spacedim > &mapping, const DoFHandler< dim, spacedim > &dof, const Function< spacedim, typename VectorType::value_type > &function, VectorType &vec, const ComponentMask &component_mask=ComponentMask())
triangulation
const ::parallel::distributed::Triangulation< dim, spacedim > * triangulation
dealii
e
SymmetricTensor< 2, dim, Number > e(const Tensor< 2, dim, Number > &F)
TEST_F
TEST_F(Poisson2DTester, TestLinear)
Definition: poisson_tester.cc:29
value
static constexpr bool const value
make_grid
void make_grid(Triangulation< 2 > &triangulation)
Definition: step-2.cc:41
TYPED_TEST
TYPED_TEST(PoissonTester, MakeGrid)
Definition: poisson_tester.cc:19
TYPED_TEST_CASE
TYPED_TEST_CASE(PoissonTester, PoissonTestTypes)
PoissonTester
Definition: poisson-tester.cc:10
PoissonTestTypes
::testing::Types< std::integral_constant< int, 1 >, std::integral_constant< int, 2 >, std::integral_constant< int, 3 > > PoissonTestTypes
Definition: poisson_tester.cc:12