The finite element method using deal.II - 2021/2022
poisson.cc
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------
2  *
3  * Copyright (C) 1999 - 2020 by the deal.II authors
4  *
5  * This file is part of the deal.II library.
6  *
7  * The deal.II library is free software; you can use it, redistribute
8  * it, and/or modify it under the terms of the GNU Lesser General
9  * Public License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  * The full text of the license can be found in the file LICENSE.md at
12  * the top level directory of deal.II.
13  *
14  * ---------------------------------------------------------------------
15  *
16  * Authors: Wolfgang Bangerth, 1999,
17  * Guido Kanschat, 2011
18  * Luca Heltai, 2021
19  */
20 #include "poisson.h"
21 
22 using namespace dealii;
23 
25  : fe(1)
27 {}
28 
29 
30 
31 void
33 {
36  std::cout << "Number of active cells: " << triangulation.n_active_cells()
37  << std::endl;
38 }
39 
40 
41 
42 void
44 {
46  std::cout << "Number of degrees of freedom: " << dof_handler.n_dofs()
47  << std::endl;
54 }
55 
56 
57 
58 void
60 {
61  QGauss<2> quadrature_formula(fe.degree + 1);
62  FEValues<2> fe_values(fe,
63  quadrature_formula,
65  const unsigned int dofs_per_cell = fe.n_dofs_per_cell();
66  FullMatrix<double> cell_matrix(dofs_per_cell, dofs_per_cell);
67  Vector<double> cell_rhs(dofs_per_cell);
68  std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
69  for (const auto &cell : dof_handler.active_cell_iterators())
70  {
71  fe_values.reinit(cell);
72  cell_matrix = 0;
73  cell_rhs = 0;
74  for (const unsigned int q_index : fe_values.quadrature_point_indices())
75  {
76  for (const unsigned int i : fe_values.dof_indices())
77  for (const unsigned int j : fe_values.dof_indices())
78  cell_matrix(i, j) +=
79  (fe_values.shape_grad(i, q_index) * // grad phi_i(x_q)
80  fe_values.shape_grad(j, q_index) * // grad phi_j(x_q)
81  fe_values.JxW(q_index)); // dx
82  for (const unsigned int i : fe_values.dof_indices())
83  cell_rhs(i) += (fe_values.shape_value(i, q_index) * // phi_i(x_q)
84  1. * // f(x_q)
85  fe_values.JxW(q_index)); // dx
86  }
87  cell->get_dof_indices(local_dof_indices);
88  for (const unsigned int i : fe_values.dof_indices())
89  for (const unsigned int j : fe_values.dof_indices())
92  cell_matrix(i, j));
93  for (const unsigned int i : fe_values.dof_indices())
94  system_rhs(local_dof_indices[i]) += cell_rhs(i);
95  }
96  std::map<types::global_dof_index, double> boundary_values;
98  0,
100  boundary_values);
101  MatrixTools::apply_boundary_values(boundary_values,
103  solution,
104  system_rhs);
105 }
106 
107 
108 
109 void
111 {
112  SolverControl solver_control(1000, 1e-12);
115 }
116 
117 
118 
119 void
121 {
122  DataOut<2> data_out;
124  data_out.add_data_vector(solution, "solution");
125  data_out.build_patches();
126  std::ofstream output("solution.vtk");
127  data_out.write_vtk(output);
128 }
129 
130 
131 
132 void
134 {
135  make_grid();
136  setup_system();
137  assemble_system();
138  solve();
139  output_results();
140 }
FEValues::shape_value
const double & shape_value(const unsigned int function_no, const unsigned int point_no) const
SparseMatrix< double >::add
void add(const size_type i, const size_type j, const double value)
local_dof_indices
std::vector< types::global_dof_index > local_dof_indices
FEValues::reinit
void reinit(const TriaIterator< DoFCellAccessor< dim, spacedim, level_dof_access >> &cell)
Poisson::dof_handler
DoFHandler< 2 > dof_handler
Definition: poisson.h:76
SolverCG
PreconditionIdentity
PreconditionIdentity()
SolverCG::solve
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &preconditioner)
SolverControl
dealii
FEValues::quadrature_point_indices
std_cxx20::ranges::iota_view< unsigned int, unsigned int > quadrature_point_indices() const
Poisson::system_matrix
SparseMatrix< double > system_matrix
Definition: poisson.h:78
Poisson::sparsity_pattern
SparsityPattern sparsity_pattern
Definition: poisson.h:77
Poisson::Poisson
Poisson()
Definition: poisson.cc:24
Triangulation::refine_global
void refine_global(const unsigned int times=1)
MatrixTools::apply_boundary_values
void apply_boundary_values(const std::map< types::global_dof_index, number > &boundary_values, SparseMatrix< number > &matrix, Vector< number > &solution, Vector< number > &right_hand_side, const bool eliminate_columns=true)
Poisson::system_rhs
Vector< double > system_rhs
Definition: poisson.h:80
Poisson::run
void run()
Definition: poisson.cc:133
Poisson::solve
void solve()
Definition: poisson.cc:110
e
SymmetricTensor< 2, dim, Number > e(const Tensor< 2, dim, Number > &F)
Poisson::fe
FE_Q< 2 > fe
Definition: poisson.h:75
DataOut::write_vtk
void write_vtk(std::ostream &out) const
cell_matrix
void cell_matrix(FullMatrix< double > &M, const FEValuesBase< dim > &fe, const FEValuesBase< dim > &fetest, const ArrayView< const std::vector< double >> &velocity, const double factor=1.)
update_values
update_values
DataOut::attach_dof_handler
void attach_dof_handler(const DoFHandler< dim, spacedim > &)
DoFHandler::distribute_dofs
void distribute_dofs(const FiniteElement< dim, spacedim > &fe)
FEValues
FE_Q::degree
const unsigned int degree
FEValues::shape_grad
const Tensor< 1, spacedim > & shape_grad(const unsigned int function_no, const unsigned int quadrature_point) const
DoFTools::make_sparsity_pattern
void make_sparsity_pattern(const DoFHandler< dim, spacedim > &dof_handler, SparsityPatternType &sparsity_pattern, const AffineConstraints< number > &constraints=AffineConstraints< number >(), const bool keep_constrained_dofs=true, const types::subdomain_id subdomain_id=numbers::invalid_subdomain_id)
SparseMatrix< double >::reinit
virtual void reinit(const SparsityPattern &sparsity)
DataOut::build_patches
virtual void build_patches(const unsigned int n_subdivisions=0)
update_gradients
update_gradients
DynamicSparsityPattern
VectorTools::interpolate_boundary_values
void interpolate_boundary_values(const Mapping< dim, spacedim > &mapping, const DoFHandler< dim, spacedim > &dof, const std::map< types::boundary_id, const Function< spacedim, number > * > &function_map, std::map< types::global_dof_index, number > &boundary_values, const ComponentMask &component_mask=ComponentMask())
Poisson::make_grid
void make_grid()
Definition: poisson.cc:32
FEValues::JxW
double JxW(const unsigned int quadrature_point) const
output
output
Vector< double >::reinit
virtual void reinit(const size_type N, const bool omit_zeroing_entries=false)
Poisson::solution
Vector< double > solution
Definition: poisson.h:79
QGauss
update_JxW_values
update_JxW_values
Poisson::setup_system
void setup_system()
Definition: poisson.cc:43
SparsityPattern::copy_from
void copy_from(const size_type n_rows, const size_type n_cols, const ForwardIterator begin, const ForwardIterator end)
GridGenerator::hyper_cube
void hyper_cube(Triangulation< dim, spacedim > &tria, const double left=0., const double right=1., const bool colorize=false)
Poisson::output_results
void output_results() const
Definition: poisson.cc:120
DataOut::add_data_vector
void add_data_vector(const VectorType &data, const std::vector< std::string > &names, const DataVectorType type=type_automatic, const std::vector< DataComponentInterpretation::DataComponentInterpretation > &data_component_interpretation={})
Poisson::triangulation
Triangulation< 2 > triangulation
Definition: poisson.h:74
Triangulation::n_active_cells
unsigned int n_active_cells() const
Functions::ZeroFunction
FEValues::dof_indices
std_cxx20::ranges::iota_view< unsigned int, unsigned int > dof_indices() const
Poisson::solver_control
ParameterAcceptorProxy< ReductionControl > solver_control
Definition: poisson.h:213
FullMatrix< double >
DoFHandler::active_cell_iterators
IteratorRange< active_cell_iterator > active_cell_iterators() const
FE_Q::n_dofs_per_cell
unsigned int n_dofs_per_cell() const
DataOut
Poisson::assemble_system
void assemble_system()
Definition: poisson.cc:59
Vector< double >
DoFHandler::n_dofs
types::global_dof_index n_dofs() const