Reference documentation for deal.II version 8.4.2
functional.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2010 - 2015 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 at
12 // the top level of the deal.II distribution.
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 #include <deal.II/base/smartpointer.h>
22 #include <deal.II/base/mg_level_object.h>
23 #include <deal.II/lac/block_vector.h>
24 #include <deal.II/meshworker/dof_info.h>
25 #include <deal.II/multigrid/mg_constrained_dofs.h>
26 
27 
28 DEAL_II_NAMESPACE_OPEN
29 
30 namespace MeshWorker
31 {
32  namespace Assembler
33  {
43  template <typename number = double>
44  class Functional
45  {
46  public:
51  void initialize(const unsigned int n);
59  template <class DOFINFO>
60  void initialize_info(DOFINFO &info, bool face);
61 
65  template<class DOFINFO>
66  void assemble(const DOFINFO &info);
67 
71  template<class DOFINFO>
72  void assemble(const DOFINFO &info1,
73  const DOFINFO &info2);
74 
78  number operator() (const unsigned int i) const;
79  private:
83  std::vector<double> results;
84  };
85 
93  template <typename number = double>
95  {
96  public:
114  void initialize(AnyData &results, bool separate_faces = true);
115 
123  template <class DOFINFO>
124  void initialize_info(DOFINFO &info, bool face) const;
125 
129  template<class DOFINFO>
130  void assemble(const DOFINFO &info);
131 
135  template<class DOFINFO>
136  void assemble(const DOFINFO &info1,
137  const DOFINFO &info2);
138 
142  number operator() (const unsigned int i) const;
143  private:
145  bool separate_faces;
146  };
147 //----------------------------------------------------------------------//
148 
149  template <typename number>
150  inline void
151  Functional<number>::initialize(const unsigned int n)
152  {
153  results.resize(n);
154  std::fill(results.begin(), results.end(), 0.);
155  }
156 
157 
158  template <typename number>
159  template <class DOFINFO>
160  inline void
162  {
163  info.initialize_numbers(results.size());
164  }
165 
166 
167  template <typename number>
168  template <class DOFINFO>
169  inline void
170  Functional<number>::assemble(const DOFINFO &info)
171  {
172  for (unsigned int i=0; i<results.size(); ++i)
173  results[i] += info.value(i);
174  }
175 
176 
177  template <typename number>
178  template <class DOFINFO>
179  inline void
180  Functional<number>::assemble(const DOFINFO &info1,
181  const DOFINFO &info2)
182  {
183  for (unsigned int i=0; i<results.size(); ++i)
184  {
185  results[i] += info1.value(i);
186  results[i] += info2.value(i);
187  }
188  }
189 
190 
191  template <typename number>
192  inline number
193  Functional<number>::operator() (const unsigned int i) const
194  {
195  AssertIndexRange(i, results.size());
196  return results[i];
197  }
198 
199 //----------------------------------------------------------------------//
200 
201  template <typename number>
202  inline void
204  {
205  Assert(r.name(0) == "cells", AnyData::ExcNameMismatch(0, "cells"));
206  if (sep)
207  {
208  Assert(r.name(1) == "faces", AnyData::ExcNameMismatch(1, "faces"));
210  r.entry<BlockVector<double>*>(1)->n_blocks());
211  }
212 
213  results = r;
214  separate_faces = sep;
215  }
216 
217  template <typename number>
218  template <class DOFINFO>
219  inline void
220  CellsAndFaces<number>::initialize_info(DOFINFO &info, bool) const
221  {
222  info.initialize_numbers(results.entry<BlockVector<double>*>(0)->n_blocks());
223  }
224 
225 
226  template <typename number>
227  template <class DOFINFO>
228  inline void
229  CellsAndFaces<number>::assemble(const DOFINFO &info)
230  {
232  if (separate_faces &&
233  info.face_number != numbers::invalid_unsigned_int)
234  v = results.entry<BlockVector<double>*>(1);
235  else
236  v = results.entry<BlockVector<double>*>(0);
237 
238  for (unsigned int i=0; i<info.n_values(); ++i)
239  v->block(i)(info.cell->user_index()) += info.value(i);
240  }
241 
242 
243  template <typename number>
244  template <class DOFINFO>
245  inline void
246  CellsAndFaces<number>::assemble(const DOFINFO &info1,
247  const DOFINFO &info2)
248  {
249  for (unsigned int i=0; i<info1.n_values(); ++i)
250  {
251  if (separate_faces)
252  {
254  const double J = info1.value(i) + info2.value(i);
255  v1->block(i)(info1.face->user_index()) += J;
256  if (info2.face != info1.face)
257  v1->block(i)(info2.face->user_index()) += J;
258  }
259  else
260  {
262  v0->block(i)(info1.cell->user_index()) += .5*info1.value(i);
263  v0->block(i)(info2.cell->user_index()) += .5*info2.value(i);
264  }
265  }
266  }
267  }
268 }
269 
270 DEAL_II_NAMESPACE_CLOSE
271 
272 #endif
static const unsigned int invalid_unsigned_int
Definition: types.h:164
type entry(const std::string &name)
Access to stored data object by name.
Definition: any_data.h:341
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1052
void initialize(const unsigned int n)
Definition: functional.h:151
#define AssertIndexRange(index, range)
Definition: exceptions.h:1081
void initialize(AnyData &results, bool separate_faces=true)
Definition: functional.h:203
#define Assert(cond, exc)
Definition: exceptions.h:294
void initialize_info(DOFINFO &info, bool face)
Definition: functional.h:161
void initialize_info(DOFINFO &info, bool face) const
Definition: functional.h:220
unsigned int n_blocks() const
void assemble(const DOFINFO &info)
Definition: functional.h:170
std::vector< double > results
Definition: functional.h:83
BlockType & block(const unsigned int i)
number operator()(const unsigned int i) const
Definition: functional.h:193
const std::string & name(const unsigned int i) const
Name of object at index.
Definition: any_data.h:297
void assemble(const DOFINFO &info)
Definition: functional.h:229