Reference documentation for deal.II version 9.1.0-pre
dof_accessor_get.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2017 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 #include <deal.II/dofs/dof_accessor.h>
17 #include <deal.II/dofs/dof_handler.h>
18 #include <deal.II/dofs/dof_levels.h>
19 
20 #include <deal.II/fe/fe.h>
21 
22 #include <deal.II/grid/tria_iterator.h>
23 #include <deal.II/grid/tria_iterator.templates.h>
24 
25 #include <deal.II/hp/dof_handler.h>
26 
27 #include <deal.II/lac/block_vector.h>
28 #include <deal.II/lac/la_parallel_block_vector.h>
29 #include <deal.II/lac/la_parallel_vector.h>
30 #include <deal.II/lac/la_vector.h>
31 #include <deal.II/lac/petsc_block_vector.h>
32 #include <deal.II/lac/petsc_vector.h>
33 #include <deal.II/lac/sparse_matrix.h>
34 #include <deal.II/lac/trilinos_parallel_block_vector.h>
35 #include <deal.II/lac/trilinos_vector.h>
36 #include <deal.II/lac/vector.h>
37 
38 #include <vector>
39 
40 DEAL_II_NAMESPACE_OPEN
41 
42 
43 template <typename DoFHandlerType, bool lda>
44 template <class InputVector, typename number>
45 void
47  const InputVector &values,
48  Vector<number> & interpolated_values,
49  const unsigned int fe_index) const
50 {
51  if (!this->has_children())
52  // if this cell has no children: simply return the exact values on this
53  // cell unless the finite element we need to interpolate to is different
54  // than the one we have on the current cell
55  {
56  if ((dynamic_cast<DoFHandler<DoFHandlerType::dimension,
57  DoFHandlerType::space_dimension> *>(
58  this->dof_handler) != nullptr) ||
59  // for hp-DoFHandlers, we need to require that on
60  // active cells, you either don't specify an fe_index,
61  // or that you specify the correct one
62  (fe_index == this->active_fe_index()) ||
63  (fe_index == DoFHandlerType::default_fe_index))
64  this->get_dof_values(values, interpolated_values);
65  else
66  {
67  // well, here we need to first get the values from the current
68  // cell and then interpolate it to the element requested. this
69  // can clearly only happen for hp::DoFHandler objects
70  Vector<number> tmp(this->get_fe().dofs_per_cell);
71  this->get_dof_values(values, tmp);
72 
73  FullMatrix<double> interpolation(
74  this->dof_handler->get_fe(fe_index).dofs_per_cell,
75  this->get_fe().dofs_per_cell);
76  this->dof_handler->get_fe(fe_index).get_interpolation_matrix(
77  this->get_fe(), interpolation);
78  interpolation.vmult(interpolated_values, tmp);
79  }
80  }
81  else
82  // otherwise obtain them from the children
83  {
84  // we are on a non-active cell. these do not have any finite
85  // element associated with them in the hp context (in the non-hp
86  // context, we can simply assume that the FE space to which we
87  // want to interpolate is the same as for all elements in the
88  // mesh). consequently, we cannot interpolate from children's FE
89  // space to this cell's (unknown) FE space unless an explicit
90  // fe_index is given
91  Assert((dynamic_cast<DoFHandler<DoFHandlerType::dimension,
92  DoFHandlerType::space_dimension> *>(
93  this->dof_handler) != nullptr) ||
94  (fe_index != DoFHandlerType::default_fe_index),
95  ExcMessage(
96  "You cannot call this function on non-active cells "
97  "of hp::DoFHandler objects unless you provide an explicit "
98  "finite element index because they do not have naturally "
99  "associated finite element spaces associated: degrees "
100  "of freedom are only distributed on active cells for which "
101  "the active_fe_index has been set."));
102 
103  const FiniteElement<dim, spacedim> &fe =
104  this->get_dof_handler().get_fe(fe_index);
105  const unsigned int dofs_per_cell = fe.dofs_per_cell;
106 
107  Assert(this->dof_handler != nullptr,
108  typename BaseClass::ExcInvalidObject());
109  Assert(interpolated_values.size() == dofs_per_cell,
110  typename BaseClass::ExcVectorDoesNotMatch());
111  Assert(values.size() == this->dof_handler->n_dofs(),
112  typename BaseClass::ExcVectorDoesNotMatch());
113 
114 
115  // see if the finite element we have on the current cell has any
116  // degrees of freedom to begin with; if not (e.g., when
117  // interpolating FE_Nothing), then simply skip all of the
118  // following since the output vector would be of size zero
119  // anyway (and in fact is of size zero, see the assertion above)
120  if (fe.dofs_per_cell > 0)
121  {
122  Vector<number> tmp1(dofs_per_cell);
123  Vector<number> tmp2(dofs_per_cell);
124 
125  interpolated_values = 0;
126 
127  // later on we will have to push the values interpolated from the
128  // child to the mother cell into the output vector. unfortunately,
129  // there are two types of elements: ones where you add up the
130  // contributions from the different child cells, and ones where you
131  // overwrite.
132  //
133  // an example for the first is piecewise constant (and discontinuous)
134  // elements, where we build the value on the coarse cell by averaging
135  // the values from the cell (i.e. by adding up a fraction of the
136  // values of their values)
137  //
138  // an example for the latter are the usual continuous elements. the
139  // value on a vertex of a coarse cell must there be the same,
140  // irrespective of the adjacent cell we are presently on. so we always
141  // overwrite. in fact, we must, since we cannot know in advance how
142  // many neighbors there will be, so there is no way to compute the
143  // average with fixed factors
144  //
145  // so we have to find out to which type this element belongs. the
146  // difficulty is: the finite element may be a composed one, so we can
147  // only hope to do this for each shape function individually. in fact,
148  // there are even weird finite elements (for example the
149  // Raviart-Thomas element) which have shape functions that are
150  // additive (interior ones) and others that are overwriting (face
151  // degrees of freedom that need to be continuous across the face).
152  for (unsigned int child = 0; child < this->n_children(); ++child)
153  {
154  // get the values from the present child, if necessary by
155  // interpolation itself either from its own children or
156  // by interpolating from the finite element on an active
157  // child to the finite element space requested here
158  this->child(child)->get_interpolated_dof_values(values,
159  tmp1,
160  fe_index);
161  // interpolate these to the mother cell
162  fe.get_restriction_matrix(child, this->refinement_case())
163  .vmult(tmp2, tmp1);
164 
165  // and add up or set them in the output vector
166  for (unsigned int i = 0; i < dofs_per_cell; ++i)
167  if (fe.restriction_is_additive(i))
168  interpolated_values(i) += tmp2(i);
169  else if (tmp2(i) != number())
170  interpolated_values(i) = tmp2(i);
171  }
172  }
173  }
174 }
175 
176 
177 // --------------------------------------------------------------------------
178 // explicit instantiations
179 #include "dof_accessor_get.inst"
180 
181 DEAL_II_NAMESPACE_CLOSE
void get_interpolated_dof_values(const InputVector &values, Vector< number > &interpolated_values, const unsigned int fe_index=DoFHandlerType::default_fe_index) const
size_type size() const
void vmult(Vector< number2 > &w, const Vector< number2 > &v, const bool adding=false) const
static::ExceptionBase & ExcMessage(std::string arg1)
#define Assert(cond, exc)
Definition: exceptions.h:1227
const unsigned int dofs_per_cell
Definition: fe_base.h:297
bool restriction_is_additive(const unsigned int index) const
Definition: fe.h:3253
virtual const FullMatrix< double > & get_restriction_matrix(const unsigned int child, const RefinementCase< dim > &refinement_case=RefinementCase< dim >::isotropic_refinement) const
Definition: fe.cc:306