Reference documentation for deal.II version 9.1.1
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
functional.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2010 - 2018 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 
17 #ifndef dealii_mesh_worker_functional_h
18 #define dealii_mesh_worker_functional_h
19 
20 #include <deal.II/algorithms/any_data.h>
21 
22 #include <deal.II/base/mg_level_object.h>
23 #include <deal.II/base/smartpointer.h>
24 
25 #include <deal.II/lac/block_vector.h>
26 
27 #include <deal.II/meshworker/dof_info.h>
28 
29 #include <deal.II/multigrid/mg_constrained_dofs.h>
30 
31 
32 DEAL_II_NAMESPACE_OPEN
33 
34 namespace MeshWorker
35 {
36  namespace Assembler
37  {
47  template <typename number = double>
48  class Functional
49  {
50  public:
55  void
56  initialize(const unsigned int n);
64  template <class DOFINFO>
65  void
66  initialize_info(DOFINFO &info, bool face);
67 
71  template <class DOFINFO>
72  void
73  assemble(const DOFINFO &info);
74 
78  template <class DOFINFO>
79  void
80  assemble(const DOFINFO &info1, const DOFINFO &info2);
81 
85  number
86  operator()(const unsigned int i) const;
87 
88  private:
92  std::vector<double> results;
93  };
94 
105  template <typename number = double>
107  {
108  public:
112  CellsAndFaces();
113 
131  void
132  initialize(AnyData &results, bool separate_faces = true);
133 
141  template <class DOFINFO>
142  void
143  initialize_info(DOFINFO &info, bool face) const;
144 
148  template <class DOFINFO>
149  void
150  assemble(const DOFINFO &info);
151 
155  template <class DOFINFO>
156  void
157  assemble(const DOFINFO &info1, const DOFINFO &info2);
158 
162  number
163  operator()(const unsigned int i) const;
164 
165  private:
166  AnyData results;
167  bool separate_faces;
168  };
169  //----------------------------------------------------------------------//
170 
171  template <typename number>
172  inline void
173  Functional<number>::initialize(const unsigned int n)
174  {
175  results.resize(n);
176  std::fill(results.begin(), results.end(), 0.);
177  }
178 
179 
180  template <typename number>
181  template <class DOFINFO>
182  inline void
184  {
185  info.initialize_numbers(results.size());
186  }
187 
188 
189  template <typename number>
190  template <class DOFINFO>
191  inline void
192  Functional<number>::assemble(const DOFINFO &info)
193  {
194  for (unsigned int i = 0; i < results.size(); ++i)
195  results[i] += info.value(i);
196  }
197 
198 
199  template <typename number>
200  template <class DOFINFO>
201  inline void
202  Functional<number>::assemble(const DOFINFO &info1, const DOFINFO &info2)
203  {
204  for (unsigned int i = 0; i < results.size(); ++i)
205  {
206  results[i] += info1.value(i);
207  results[i] += info2.value(i);
208  }
209  }
210 
211 
212  template <typename number>
213  inline number
214  Functional<number>::operator()(const unsigned int i) const
215  {
216  AssertIndexRange(i, results.size());
217  return results[i];
218  }
219 
220  //----------------------------------------------------------------------//
221 
222  template <typename number>
224  : separate_faces(true)
225  {}
226 
227 
228 
229  template <typename number>
230  inline void
232  {
233  Assert(r.name(0) == "cells", AnyData::ExcNameMismatch(0, "cells"));
234  if (sep)
235  {
236  Assert(r.name(1) == "faces", AnyData::ExcNameMismatch(1, "faces"));
238  r.entry<BlockVector<double> *>(1)->n_blocks());
239  }
240 
241  results = r;
242  separate_faces = sep;
243  }
244 
245  template <typename number>
246  template <class DOFINFO>
247  inline void
248  CellsAndFaces<number>::initialize_info(DOFINFO &info, bool) const
249  {
250  info.initialize_numbers(
251  results.entry<BlockVector<double> *>(0)->n_blocks());
252  }
253 
254 
255  template <typename number>
256  template <class DOFINFO>
257  inline void
258  CellsAndFaces<number>::assemble(const DOFINFO &info)
259  {
261  if (separate_faces && info.face_number != numbers::invalid_unsigned_int)
262  v = results.entry<BlockVector<double> *>(1);
263  else
264  v = results.entry<BlockVector<double> *>(0);
265 
266  for (unsigned int i = 0; i < info.n_values(); ++i)
267  v->block(i)(info.cell->user_index()) += info.value(i);
268  }
269 
270 
271  template <typename number>
272  template <class DOFINFO>
273  inline void
274  CellsAndFaces<number>::assemble(const DOFINFO &info1, const DOFINFO &info2)
275  {
276  for (unsigned int i = 0; i < info1.n_values(); ++i)
277  {
278  if (separate_faces)
279  {
280  BlockVector<double> *v1 = results.entry<BlockVector<double> *>(1);
281  const double J = info1.value(i) + info2.value(i);
282  v1->block(i)(info1.face->user_index()) += J;
283  if (info2.face != info1.face)
284  v1->block(i)(info2.face->user_index()) += J;
285  }
286  else
287  {
288  BlockVector<double> *v0 = results.entry<BlockVector<double> *>(0);
289  v0->block(i)(info1.cell->user_index()) += .5 * info1.value(i);
290  v0->block(i)(info2.cell->user_index()) += .5 * info2.value(i);
291  }
292  }
293  }
294  } // namespace Assembler
295 } // namespace MeshWorker
296 
297 DEAL_II_NAMESPACE_CLOSE
298 
299 #endif
static const unsigned int invalid_unsigned_int
Definition: types.h:173
type entry(const std::string &name)
Access to stored data object by name.
Definition: any_data.h:351
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1567
void initialize(const unsigned int n)
Definition: functional.h:173
#define AssertIndexRange(index, range)
Definition: exceptions.h:1637
number operator()(const unsigned int i) const
void initialize(AnyData &results, bool separate_faces=true)
Definition: functional.h:231
#define Assert(cond, exc)
Definition: exceptions.h:1407
void initialize_info(DOFINFO &info, bool face)
Definition: functional.h:183
void initialize_info(DOFINFO &info, bool face) const
Definition: functional.h:248
unsigned int n_blocks() const
void assemble(const DOFINFO &info)
Definition: functional.h:192
std::vector< double > results
Definition: functional.h:92
static ::ExceptionBase & ExcNameMismatch(int arg1, std::string arg2)
BlockType & block(const unsigned int i)
number operator()(const unsigned int i) const
Definition: functional.h:214
const std::string & name(const unsigned int i) const
Name of object at index.
Definition: any_data.h:311
void assemble(const DOFINFO &info)
Definition: functional.h:258