Reference documentation for deal.II version 9.0.0
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 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:
100  CellsAndFaces();
101 
119  void initialize(AnyData &results, bool separate_faces = true);
120 
128  template <class DOFINFO>
129  void initialize_info(DOFINFO &info, bool face) const;
130 
134  template <class DOFINFO>
135  void assemble(const DOFINFO &info);
136 
140  template <class DOFINFO>
141  void assemble(const DOFINFO &info1,
142  const DOFINFO &info2);
143 
147  number operator() (const unsigned int i) const;
148  private:
149  AnyData results;
150  bool separate_faces;
151  };
152 //----------------------------------------------------------------------//
153 
154  template <typename number>
155  inline void
156  Functional<number>::initialize(const unsigned int n)
157  {
158  results.resize(n);
159  std::fill(results.begin(), results.end(), 0.);
160  }
161 
162 
163  template <typename number>
164  template <class DOFINFO>
165  inline void
167  {
168  info.initialize_numbers(results.size());
169  }
170 
171 
172  template <typename number>
173  template <class DOFINFO>
174  inline void
175  Functional<number>::assemble(const DOFINFO &info)
176  {
177  for (unsigned int i=0; i<results.size(); ++i)
178  results[i] += info.value(i);
179  }
180 
181 
182  template <typename number>
183  template <class DOFINFO>
184  inline void
185  Functional<number>::assemble(const DOFINFO &info1,
186  const DOFINFO &info2)
187  {
188  for (unsigned int i=0; i<results.size(); ++i)
189  {
190  results[i] += info1.value(i);
191  results[i] += info2.value(i);
192  }
193  }
194 
195 
196  template <typename number>
197  inline number
198  Functional<number>::operator() (const unsigned int i) const
199  {
200  AssertIndexRange(i, results.size());
201  return results[i];
202  }
203 
204 //----------------------------------------------------------------------//
205 
206  template <typename number>
207  inline
209  : separate_faces(true)
210  {}
211 
212 
213 
214  template <typename number>
215  inline void
217  {
218  Assert(r.name(0) == "cells", AnyData::ExcNameMismatch(0, "cells"));
219  if (sep)
220  {
221  Assert(r.name(1) == "faces", AnyData::ExcNameMismatch(1, "faces"));
223  r.entry<BlockVector<double>*>(1)->n_blocks());
224  }
225 
226  results = r;
227  separate_faces = sep;
228  }
229 
230  template <typename number>
231  template <class DOFINFO>
232  inline void
233  CellsAndFaces<number>::initialize_info(DOFINFO &info, bool) const
234  {
235  info.initialize_numbers(results.entry<BlockVector<double>*>(0)->n_blocks());
236  }
237 
238 
239  template <typename number>
240  template <class DOFINFO>
241  inline void
242  CellsAndFaces<number>::assemble(const DOFINFO &info)
243  {
245  if (separate_faces &&
246  info.face_number != numbers::invalid_unsigned_int)
247  v = results.entry<BlockVector<double>*>(1);
248  else
249  v = results.entry<BlockVector<double>*>(0);
250 
251  for (unsigned int i=0; i<info.n_values(); ++i)
252  v->block(i)(info.cell->user_index()) += info.value(i);
253  }
254 
255 
256  template <typename number>
257  template <class DOFINFO>
258  inline void
259  CellsAndFaces<number>::assemble(const DOFINFO &info1,
260  const DOFINFO &info2)
261  {
262  for (unsigned int i=0; i<info1.n_values(); ++i)
263  {
264  if (separate_faces)
265  {
266  BlockVector<double> *v1 = results.entry<BlockVector<double>*>(1);
267  const double J = info1.value(i) + info2.value(i);
268  v1->block(i)(info1.face->user_index()) += J;
269  if (info2.face != info1.face)
270  v1->block(i)(info2.face->user_index()) += J;
271  }
272  else
273  {
274  BlockVector<double> *v0 = results.entry<BlockVector<double>*>(0);
275  v0->block(i)(info1.cell->user_index()) += .5*info1.value(i);
276  v0->block(i)(info2.cell->user_index()) += .5*info2.value(i);
277  }
278  }
279  }
280  }
281 }
282 
283 DEAL_II_NAMESPACE_CLOSE
284 
285 #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:338
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1248
void initialize(const unsigned int n)
Definition: functional.h:156
#define AssertIndexRange(index, range)
Definition: exceptions.h:1284
number operator()(const unsigned int i) const
void initialize(AnyData &results, bool separate_faces=true)
Definition: functional.h:216
#define Assert(cond, exc)
Definition: exceptions.h:1142
void initialize_info(DOFINFO &info, bool face)
Definition: functional.h:166
void initialize_info(DOFINFO &info, bool face) const
Definition: functional.h:233
unsigned int n_blocks() const
void assemble(const DOFINFO &info)
Definition: functional.h:175
std::vector< double > results
Definition: functional.h:83
static ::ExceptionBase & ExcNameMismatch(int arg1, std::string arg2)
BlockType & block(const unsigned int i)
number operator()(const unsigned int i) const
Definition: functional.h:198
const std::string & name(const unsigned int i) const
Name of object at index.
Definition: any_data.h:294
void assemble(const DOFINFO &info)
Definition: functional.h:242