Skip to content

Commit e055d8f

Browse files
committed
add test, cleanup
1 parent 7d97323 commit e055d8f

File tree

4 files changed

+237
-10
lines changed

4 files changed

+237
-10
lines changed

source/fe/fe_system.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2049,7 +2049,7 @@ FESystem<dim, spacedim>::initialize(
20492049
// This base element has a sparsity pattern
20502050
// where not all DoFs couple. Within this base element are
20512051
// several copies of it (given by multiplicity), each of them
2052-
// couples to each other one only based on this pattern. This
2052+
// couples to the others only based on this pattern. This
20532053
// means we can copy m times m copies of the pattern into ours:
20542054
const unsigned int n_dofs = pattern.size(0);
20552055
for (unsigned int bi = 0; bi < multiplicity; ++bi)

tests/fe/assemble_q_iso_q1_02.cc

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
// ------------------------------------------------------------------------
2+
//
3+
// SPDX-License-Identifier: LGPL-2.1-or-later
4+
// Copyright (C) 2013 - 2024 by the deal.II authors
5+
//
6+
// This file is part of the deal.II library.
7+
//
8+
// Part of the source code is dual licensed under Apache-2.0 WITH
9+
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10+
// governing the source code and code contributions can be found in
11+
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12+
//
13+
// ------------------------------------------------------------------------
14+
15+
16+
// Assemble a 1d,2d,3d vector Poisson problem with FE_Q_iso_Q1 elements to
17+
// check that the sparsity pattern is computed correctly.
18+
19+
#include <deal.II/base/function.h>
20+
21+
#include <deal.II/dofs/dof_handler.h>
22+
#include <deal.II/dofs/dof_tools.h>
23+
24+
#include <deal.II/fe/fe_q.h>
25+
#include <deal.II/fe/fe_q_iso_q1.h>
26+
#include <deal.II/fe/fe_system.h>
27+
#include <deal.II/fe/fe_values.h>
28+
29+
#include <deal.II/grid/grid_generator.h>
30+
#include <deal.II/grid/tria.h>
31+
32+
#include <deal.II/lac/affine_constraints.h>
33+
#include <deal.II/lac/dynamic_sparsity_pattern.h>
34+
#include <deal.II/lac/solver_cg.h>
35+
#include <deal.II/lac/sparse_matrix.h>
36+
#include <deal.II/lac/vector.h>
37+
38+
#include <deal.II/numerics/vector_tools.h>
39+
40+
#include "../tests.h"
41+
42+
43+
44+
template <int dim>
45+
class Step4
46+
{
47+
public:
48+
Step4();
49+
void
50+
run();
51+
52+
private:
53+
void
54+
make_grid();
55+
void
56+
setup_system();
57+
void
58+
assemble_preconditioner();
59+
60+
Triangulation<dim> triangulation;
61+
62+
FESystem<dim> fe_precondition;
63+
DoFHandler<dim> dof_handler_precondition;
64+
65+
AffineConstraints<double> constraints;
66+
67+
SparsityPattern prec_pattern;
68+
SparseMatrix<double> preconditioner_matrix;
69+
70+
Vector<double> system_rhs;
71+
};
72+
73+
74+
template <int dim>
75+
class RightHandSide : public Function<dim>
76+
{
77+
public:
78+
RightHandSide()
79+
: Function<dim>()
80+
{}
81+
82+
virtual double
83+
value(const Point<dim> &p, const unsigned int component = 0) const;
84+
};
85+
86+
87+
88+
template <int dim>
89+
double
90+
RightHandSide<dim>::value(const Point<dim> &p,
91+
const unsigned int /*component*/) const
92+
{
93+
double return_value = 0;
94+
for (unsigned int i = 0; i < dim; ++i)
95+
return_value += 4 * std::pow(p[i], 4);
96+
97+
return return_value;
98+
}
99+
100+
101+
template <int dim>
102+
Step4<dim>::Step4()
103+
: fe_precondition(FE_Q_iso_Q1<dim>(2), dim)
104+
, dof_handler_precondition(triangulation)
105+
{}
106+
107+
108+
template <int dim>
109+
void
110+
Step4<dim>::make_grid()
111+
{
112+
GridGenerator::hyper_cube(triangulation, -1, 1);
113+
triangulation.refine_global(1);
114+
}
115+
116+
117+
118+
template <int dim>
119+
void
120+
Step4<dim>::setup_system()
121+
{
122+
dof_handler_precondition.distribute_dofs(fe_precondition);
123+
124+
constraints.clear();
125+
std::map<unsigned int, double> boundary_values;
126+
VectorTools::interpolate_boundary_values(dof_handler_precondition,
127+
0,
128+
Functions::ZeroFunction<dim>(dim),
129+
constraints);
130+
constraints.close();
131+
132+
{
133+
DynamicSparsityPattern c_sparsity(dof_handler_precondition.n_dofs());
134+
DoFTools::make_sparsity_pattern(dof_handler_precondition,
135+
c_sparsity,
136+
constraints,
137+
false);
138+
prec_pattern.copy_from(c_sparsity);
139+
preconditioner_matrix.reinit(prec_pattern);
140+
}
141+
142+
system_rhs.reinit(dof_handler_precondition.n_dofs());
143+
}
144+
145+
146+
147+
template <int dim>
148+
void
149+
Step4<dim>::assemble_preconditioner()
150+
{
151+
QIterated<dim> quadrature_formula(QGauss<1>(2), 3);
152+
153+
const RightHandSide<dim> right_hand_side;
154+
155+
FEValues<dim> fe_values(fe_precondition,
156+
quadrature_formula,
157+
update_values | update_gradients |
158+
update_quadrature_points | update_JxW_values);
159+
160+
const unsigned int dofs_per_cell = fe_precondition.dofs_per_cell;
161+
const unsigned int n_q_points = quadrature_formula.size();
162+
163+
FullMatrix<double> cell_matrix(dofs_per_cell, dofs_per_cell);
164+
Vector<double> cell_rhs(dofs_per_cell);
165+
166+
std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
167+
168+
typename DoFHandler<dim>::active_cell_iterator
169+
cell = dof_handler_precondition.begin_active(),
170+
endc = dof_handler_precondition.end();
171+
172+
for (; cell != endc; ++cell)
173+
{
174+
fe_values.reinit(cell);
175+
cell_matrix = 0;
176+
cell_rhs = 0;
177+
178+
for (unsigned int q_point = 0; q_point < n_q_points; ++q_point)
179+
for (unsigned int i = 0; i < dofs_per_cell; ++i)
180+
{
181+
for (unsigned int j = 0; j < dofs_per_cell; ++j)
182+
cell_matrix(i, j) +=
183+
(fe_values.shape_grad(i, q_point) *
184+
fe_values.shape_grad(j, q_point) * fe_values.JxW(q_point));
185+
}
186+
187+
cell->get_dof_indices(local_dof_indices);
188+
constraints.distribute_local_to_global(cell_matrix,
189+
local_dof_indices,
190+
preconditioner_matrix);
191+
}
192+
preconditioner_matrix.compress(VectorOperation::add);
193+
}
194+
195+
196+
197+
template <int dim>
198+
void
199+
Step4<dim>::run()
200+
{
201+
deallog.push("dim " + std::to_string(dim));
202+
203+
make_grid();
204+
205+
setup_system();
206+
assemble_preconditioner();
207+
deallog << "nnz: " << preconditioner_matrix.n_nonzero_elements() << std::endl;
208+
209+
deallog.pop();
210+
}
211+
212+
213+
int
214+
main(int argc, char **argv)
215+
{
216+
initlog(true);
217+
218+
{
219+
Step4<1> test;
220+
test.run();
221+
}
222+
{
223+
Step4<2> test;
224+
test.run();
225+
}
226+
{
227+
Step4<3> test;
228+
test.run();
229+
}
230+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
DEAL:dim 1::nnz: 13
3+
DEAL:dim 2::nnz: 228
4+
DEAL:dim 3::nnz: 3381

tests/fe/deformed_projection.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -365,17 +365,10 @@ create_mass_matrix(const Mapping<dim> &mapping,
365365
}
366366
}
367367
}
368-
// transfer everything into the
369-
// global object. lock the
370-
// matrix meanwhile
368+
// transfer everything into the global object
371369
for (unsigned int i = 0; i < dofs_per_cell; ++i)
372370
for (unsigned int j = 0; j < dofs_per_cell; ++j)
373-
if ((n_components == 1) || (cell_matrix(i, j) != 0.0))
374-
/*
375-
(fe.system_to_component_index(i).first ==
376-
fe.system_to_component_index(j).first))
377-
*/
378-
matrix.add(dof_indices[i], dof_indices[j], cell_matrix(i, j));
371+
matrix.add(dof_indices[i], dof_indices[j], cell_matrix(i, j));
379372

380373
for (unsigned int i = 0; i < dofs_per_cell; ++i)
381374
rhs_vector(dof_indices[i]) += cell_vector(i);

0 commit comments

Comments
 (0)