Reference documentation for deal.II version 8.4.1
fe_tools_interpolate.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2000 - 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 #include <deal.II/base/quadrature_lib.h>
18 #include <deal.II/base/qprojector.h>
19 #include <deal.II/base/thread_management.h>
20 #include <deal.II/base/utilities.h>
21 #include <deal.II/lac/vector.h>
22 #include <deal.II/lac/block_vector.h>
23 #include <deal.II/lac/parallel_vector.h>
24 #include <deal.II/lac/parallel_block_vector.h>
25 #include <deal.II/lac/petsc_parallel_vector.h>
26 #include <deal.II/lac/petsc_block_vector.h>
27 #include <deal.II/lac/petsc_parallel_block_vector.h>
28 #include <deal.II/lac/trilinos_vector.h>
29 #include <deal.II/lac/trilinos_block_vector.h>
30 #include <deal.II/lac/constraint_matrix.h>
31 #include <deal.II/grid/tria.h>
32 #include <deal.II/grid/tria_iterator.h>
33 #include <deal.II/grid/grid_generator.h>
34 #include <deal.II/fe/fe_tools.h>
35 #include <deal.II/fe/fe.h>
36 #include <deal.II/fe/fe_values.h>
37 #include <deal.II/dofs/dof_handler.h>
38 #include <deal.II/dofs/dof_accessor.h>
39 #include <deal.II/dofs/dof_tools.h>
40 #include <deal.II/hp/dof_handler.h>
41 
42 #include <deal.II/base/std_cxx11/shared_ptr.h>
43 
44 #include <deal.II/base/index_set.h>
45 
46 #include <iostream>
47 
48 
49 DEAL_II_NAMESPACE_OPEN
50 
51 namespace FETools
52 {
53  template <int dim, int spacedim,
54  template <int, int> class DoFHandlerType1,
55  template <int, int> class DoFHandlerType2,
56  class InVector, class OutVector>
57  void
58  interpolate(const DoFHandlerType1<dim, spacedim> &dof1,
59  const InVector &u1,
60  const DoFHandlerType2<dim, spacedim> &dof2,
61  OutVector &u2)
62  {
63  ConstraintMatrix dummy;
64  dummy.close();
65  interpolate(dof1, u1, dof2, dummy, u2);
66  }
67 
68 
69 
70  template <int dim, int spacedim,
71  template <int, int> class DoFHandlerType1,
72  template <int, int> class DoFHandlerType2,
73  class InVector, class OutVector>
74  void
75  interpolate (const DoFHandlerType1<dim, spacedim> &dof1,
76  const InVector &u1,
77  const DoFHandlerType2<dim, spacedim> &dof2,
78  const ConstraintMatrix &constraints,
79  OutVector &u2)
80  {
81  Assert(&dof1.get_triangulation()==&dof2.get_triangulation(), ExcTriangulationMismatch());
82 
83  Assert(u1.size()==dof1.n_dofs(),
84  ExcDimensionMismatch(u1.size(), dof1.n_dofs()));
85  Assert(u2.size()==dof2.n_dofs(),
86  ExcDimensionMismatch(u2.size(), dof2.n_dofs()));
87 
88 #ifdef DEAL_II_WITH_PETSC
89  if (dynamic_cast<const PETScWrappers::MPI::Vector *>(&u1) != 0)
90  if (dynamic_cast<const DoFHandler<dim>*>(&dof1) != 0)
91  {
92  // if u1 is a parallel distributed
93  // PETSc vector, we check the local
94  // size of u1 for safety
95  Assert(dynamic_cast<const PETScWrappers::MPI::Vector *>(&u1)->local_size() == dof1.n_locally_owned_dofs(),
96  ExcDimensionMismatch(dynamic_cast<const PETScWrappers::MPI::Vector *>(&u1)->local_size(), dof1.n_locally_owned_dofs()));
97  };
98 
99  if (dynamic_cast<PETScWrappers::MPI::Vector *>(&u2) != 0)
100  if (dynamic_cast<const DoFHandler<dim>*>(&dof2) != 0)
101  {
102  Assert(dynamic_cast<PETScWrappers::MPI::Vector *>(&u2)->local_size() == dof2.n_locally_owned_dofs(),
103  ExcDimensionMismatch(dynamic_cast<PETScWrappers::MPI::Vector *>(&u2)->local_size(), dof2.n_locally_owned_dofs()));
104  };
105 #endif
106  // allocate vectors at maximal
107  // size. will be reinited in inner
108  // cell, but Vector makes sure that
109  // this does not lead to
110  // reallocation of memory
111  Vector<typename OutVector::value_type> u1_local(DoFTools::max_dofs_per_cell(dof1));
112  Vector<typename OutVector::value_type> u2_local(DoFTools::max_dofs_per_cell(dof2));
113 
114  // have a map for interpolation
115  // matrices. shared_ptr make sure
116  // that memory is released again
117  std::map<const FiniteElement<dim,spacedim> *,
118  std::map<const FiniteElement<dim,spacedim> *,
119  std_cxx11::shared_ptr<FullMatrix<double> > > >
120  interpolation_matrices;
121 
122  typename DoFHandlerType1<dim,spacedim>::active_cell_iterator cell1 = dof1.begin_active(),
123  endc1 = dof1.end();
124  typename DoFHandlerType2<dim,spacedim>::active_cell_iterator cell2 = dof2.begin_active(),
125  endc2 = dof2.end();
126  (void)endc2;
127 
128  std::vector<types::global_dof_index> dofs;
129  dofs.reserve (DoFTools::max_dofs_per_cell (dof2));
130 
131  u2 = 0;
132  OutVector touch_count(u2);
133  touch_count = 0;
134 
135  // for distributed triangulations,
136  // we can only interpolate u1 on
137  // a cell, which this processor owns,
138  // so we have to know the subdomain_id
139  const types::subdomain_id subdomain_id =
140  dof1.get_triangulation().locally_owned_subdomain();
141 
142  for (; cell1!=endc1; ++cell1, ++cell2)
143  if ((cell1->subdomain_id() == subdomain_id)
144  ||
145  (subdomain_id == numbers::invalid_subdomain_id))
146  {
147  Assert(cell1->get_fe().n_components() == cell2->get_fe().n_components(),
148  ExcDimensionMismatch (cell1->get_fe().n_components(),
149  cell2->get_fe().n_components()));
150 
151  // for continuous elements on
152  // grids with hanging nodes we
153  // need hanging node
154  // constraints. Consequently,
155  // if there are no constraints
156  // then hanging nodes are not
157  // allowed.
158  const bool hanging_nodes_not_allowed
159  = ((cell2->get_fe().dofs_per_vertex != 0) &&
160  (constraints.n_constraints() == 0));
161 
162  if (hanging_nodes_not_allowed)
163  for (unsigned int face=0; face<GeometryInfo<dim>::faces_per_cell; ++face)
164  Assert (cell1->at_boundary(face) ||
165  cell1->neighbor(face)->level() == cell1->level(),
166  ExcHangingNodesNotAllowed(0));
167 
168 
169  const unsigned int dofs_per_cell1 = cell1->get_fe().dofs_per_cell;
170  const unsigned int dofs_per_cell2 = cell2->get_fe().dofs_per_cell;
171  u1_local.reinit (dofs_per_cell1);
172  u2_local.reinit (dofs_per_cell2);
173 
174  // check if interpolation
175  // matrix for this particular
176  // pair of elements is already
177  // there
178  if (interpolation_matrices[&cell1->get_fe()][&cell2->get_fe()].get() == 0)
179  {
180  std_cxx11::shared_ptr<FullMatrix<double> >
181  interpolation_matrix (new FullMatrix<double> (dofs_per_cell2,
182  dofs_per_cell1));
183  interpolation_matrices[&cell1->get_fe()][&cell2->get_fe()]
184  = interpolation_matrix;
185 
186  get_interpolation_matrix(cell1->get_fe(),
187  cell2->get_fe(),
188  *interpolation_matrix);
189  }
190 
191  cell1->get_dof_values(u1, u1_local);
192  interpolation_matrices[&cell1->get_fe()][&cell2->get_fe()]
193  ->vmult(u2_local, u1_local);
194 
195  dofs.resize (dofs_per_cell2);
196  cell2->get_dof_indices(dofs);
197 
198  for (unsigned int i=0; i<dofs_per_cell2; ++i)
199  {
200  u2(dofs[i])+=u2_local(i);
201  touch_count(dofs[i]) += 1;
202  }
203  }
204  // cell1 is at the end, so should
205  // be cell2
206  Assert (cell2 == endc2, ExcInternalError());
207 
208  u2.compress(VectorOperation::add);
209  touch_count.compress(VectorOperation::add);
210 
211  // if we work on parallel distributed
212  // vectors, we have to ensure, that we only
213  // work on dofs this processor owns.
214  IndexSet locally_owned_dofs = dof2.locally_owned_dofs();
215 
216  // when a discontinuous element is
217  // interpolated to a continuous
218  // one, we take the mean values.
219  // for parallel vectors check,
220  // if this component is owned by
221  // this processor.
222  for (types::global_dof_index i=0; i<dof2.n_dofs(); ++i)
223  if (locally_owned_dofs.is_element(i))
224  {
225  Assert(touch_count(i) != typename OutVector::value_type(),
226  ExcInternalError());
227  u2(i) /= touch_count(i);
228  }
229 
230  // finish the work on parallel vectors
231  u2.compress(VectorOperation::insert);
232  // Apply hanging node constraints.
233  constraints.distribute(u2);
234  }
235 
236 
237 
238  template <int dim, class InVector, class OutVector, int spacedim>
239  void
241  const InVector &u1,
242  const FiniteElement<dim,spacedim> &fe2,
243  OutVector &u1_interpolated)
244  {
245  Assert(dof1.get_fe().n_components() == fe2.n_components(),
246  ExcDimensionMismatch(dof1.get_fe().n_components(), fe2.n_components()));
247  Assert(u1.size()==dof1.n_dofs(), ExcDimensionMismatch(u1.size(), dof1.n_dofs()));
248  Assert(u1_interpolated.size()==dof1.n_dofs(),
249  ExcDimensionMismatch(u1_interpolated.size(), dof1.n_dofs()));
250 
251 #ifdef DEAL_II_WITH_PETSC
252  if (dynamic_cast<const PETScWrappers::MPI::Vector *>(&u1) != 0)
253  if (dynamic_cast<const DoFHandler<dim>*>(&dof1) != 0)
254  {
255  // if u1 is a parallel distributed
256  // PETSc vector, we check the local
257  // size of u1 for safety
258  Assert(dynamic_cast<const PETScWrappers::MPI::Vector *>(&u1)->local_size() == dof1.n_locally_owned_dofs(),
259  ExcDimensionMismatch(dynamic_cast<const PETScWrappers::MPI::Vector *>(&u1)->local_size(), dof1.n_locally_owned_dofs()));
260  };
261 
262  if (dynamic_cast<PETScWrappers::MPI::Vector *>(&u1_interpolated) != 0)
263  if (dynamic_cast<const DoFHandler<dim>*>(&dof1) != 0)
264  {
265  Assert(dynamic_cast<PETScWrappers::MPI::Vector *>(&u1_interpolated)->local_size() == dof1.n_locally_owned_dofs(),
266  ExcDimensionMismatch(dynamic_cast<PETScWrappers::MPI::Vector *>(&u1_interpolated)->local_size(), dof1.n_locally_owned_dofs()));
267  };
268 #endif
269 
270  // For continuous elements on grids
271  // with hanging nodes we need
272  // hanging node
273  // constraints. Consequently, when
274  // the elements are continuous no
275  // hanging node constraints are
276  // allowed.
277  const bool hanging_nodes_not_allowed=
278  (dof1.get_fe().dofs_per_vertex != 0) || (fe2.dofs_per_vertex != 0);
279 
280  const unsigned int dofs_per_cell1=dof1.get_fe().dofs_per_cell;
281 
282  Vector<typename OutVector::value_type> u1_local(dofs_per_cell1);
283  Vector<typename OutVector::value_type> u1_int_local(dofs_per_cell1);
284 
285  const types::subdomain_id subdomain_id =
286  dof1.get_triangulation().locally_owned_subdomain();
287 
289  endc = dof1.end();
290 
291  FullMatrix<double> interpolation_matrix(dofs_per_cell1, dofs_per_cell1);
293  interpolation_matrix);
294  for (; cell!=endc; ++cell)
295  if ((cell->subdomain_id() == subdomain_id)
296  ||
297  (subdomain_id == numbers::invalid_subdomain_id))
298  {
299  if (hanging_nodes_not_allowed)
300  for (unsigned int face=0; face<GeometryInfo<dim>::faces_per_cell; ++face)
301  Assert (cell->at_boundary(face) ||
302  cell->neighbor(face)->level() == cell->level(),
303  ExcHangingNodesNotAllowed(0));
304 
305  cell->get_dof_values(u1, u1_local);
306  interpolation_matrix.vmult(u1_int_local, u1_local);
307  cell->set_dof_values(u1_int_local, u1_interpolated);
308  }
309 
310  // if we work on a parallel PETSc vector
311  // we have to finish the work
312  u1_interpolated.compress(VectorOperation::insert);
313  }
314 
315 
316 
317  template <int dim,
318  template <int> class DoFHandlerType,
319  class InVector, class OutVector, int spacedim>
320  void
321  back_interpolate(const DoFHandlerType<dim> &dof1,
322  const InVector &u1,
323  const FiniteElement<dim,spacedim> &fe2,
324  OutVector &u1_interpolated)
325  {
326  Assert(u1.size() == dof1.n_dofs(),
327  ExcDimensionMismatch(u1.size(), dof1.n_dofs()));
328  Assert(u1_interpolated.size() == dof1.n_dofs(),
329  ExcDimensionMismatch(u1_interpolated.size(), dof1.n_dofs()));
330 
331 #ifdef DEAL_II_WITH_PETSC
332  if (dynamic_cast<const PETScWrappers::MPI::Vector *>(&u1) != 0)
333  if (dynamic_cast<const DoFHandler<dim>*>(&dof1) != 0)
334  {
335  // if u1 is a parallel distributed
336  // PETSc vector, we check the local
337  // size of u1 for safety
338  Assert(dynamic_cast<const PETScWrappers::MPI::Vector *>(&u1)->local_size() == dof1.n_locally_owned_dofs(),
339  ExcDimensionMismatch(dynamic_cast<const PETScWrappers::MPI::Vector *>(&u1)->local_size(), dof1.n_locally_owned_dofs()));
340  };
341 
342  if (dynamic_cast<PETScWrappers::MPI::Vector *>(&u1_interpolated) != 0)
343  if (dynamic_cast<const DoFHandler<dim>*>(&dof1) != 0)
344  {
345  Assert(dynamic_cast<PETScWrappers::MPI::Vector *>(&u1_interpolated)->local_size() == dof1.n_locally_owned_dofs(),
346  ExcDimensionMismatch(dynamic_cast<PETScWrappers::MPI::Vector *>(&u1_interpolated)->local_size(), dof1.n_locally_owned_dofs()));
347  };
348 #endif
349 
350  Vector<typename OutVector::value_type> u1_local(DoFTools::max_dofs_per_cell(dof1));
351  Vector<typename OutVector::value_type> u1_int_local(DoFTools::max_dofs_per_cell(dof1));
352 
353  const types::subdomain_id subdomain_id =
354  dof1.get_triangulation().locally_owned_subdomain();
355 
356  typename DoFHandlerType<dim>::active_cell_iterator cell = dof1.begin_active(),
357  endc = dof1.end();
358 
359  // map from possible fe objects in
360  // dof1 to the back_interpolation
361  // matrices
362  std::map<const FiniteElement<dim> *,
363  std_cxx11::shared_ptr<FullMatrix<double> > > interpolation_matrices;
364 
365  for (; cell!=endc; ++cell)
366  if ((cell->subdomain_id() == subdomain_id)
367  ||
368  (subdomain_id == numbers::invalid_subdomain_id))
369  {
370  Assert(cell->get_fe().n_components() == fe2.n_components(),
371  ExcDimensionMismatch(cell->get_fe().n_components(),
372  fe2.n_components()));
373 
374  // For continuous elements on
375  // grids with hanging nodes we
376  // need hanging node
377  // constraints. Consequently,
378  // when the elements are
379  // continuous no hanging node
380  // constraints are allowed.
381  const bool hanging_nodes_not_allowed=
382  (cell->get_fe().dofs_per_vertex != 0) || (fe2.dofs_per_vertex != 0);
383 
384  if (hanging_nodes_not_allowed)
385  for (unsigned int face=0; face<GeometryInfo<dim>::faces_per_cell; ++face)
386  Assert (cell->at_boundary(face) ||
387  cell->neighbor(face)->level() == cell->level(),
388  ExcHangingNodesNotAllowed(0));
389 
390  const unsigned int dofs_per_cell1 = cell->get_fe().dofs_per_cell;
391 
392  // make sure back_interpolation
393  // matrix is available
394  if (interpolation_matrices[&cell->get_fe()] != 0)
395  {
396  interpolation_matrices[&cell->get_fe()] =
397  std_cxx11::shared_ptr<FullMatrix<double> >
398  (new FullMatrix<double>(dofs_per_cell1, dofs_per_cell1));
399  get_back_interpolation_matrix(dof1.get_fe(), fe2,
400  *interpolation_matrices[&cell->get_fe()]);
401  }
402 
403  u1_local.reinit (dofs_per_cell1);
404  u1_int_local.reinit (dofs_per_cell1);
405 
406  cell->get_dof_values(u1, u1_local);
407  interpolation_matrices[&cell->get_fe()]->vmult(u1_int_local, u1_local);
408  cell->set_dof_values(u1_int_local, u1_interpolated);
409  };
410 
411  // if we work on a parallel PETSc vector
412  // we have to finish the work
413  u1_interpolated.compress(VectorOperation::insert);
414  }
415 
416 
417 
418  namespace internal
419  {
420  namespace
421  {
422  template <int dim, int spacedim, class InVector>
423  void back_interpolate (const DoFHandler<dim,spacedim> &dof1,
424  const ConstraintMatrix &constraints1,
425  const InVector &u1,
426  const DoFHandler<dim,spacedim> &dof2,
427  const ConstraintMatrix &constraints2,
428  InVector &u1_interpolated)
429  {
431  interpolate(dof1, u1, dof2, constraints2, u2);
432  interpolate(dof2, u2, dof1, constraints1, u1_interpolated);
433  }
434 
435  // special version for PETSc
436 #ifdef DEAL_II_WITH_PETSC
437  template <int dim, int spacedim>
438  void back_interpolate (const DoFHandler<dim,spacedim> &dof1,
439  const ConstraintMatrix &constraints1,
440  const PETScWrappers::MPI::Vector &u1,
441  const DoFHandler<dim,spacedim> &dof2,
442  const ConstraintMatrix &constraints2,
443  PETScWrappers::MPI::Vector &u1_interpolated)
444  {
445  // if u1 is a parallel distributed PETSc vector, we create a
446  // vector u2 with based on the sets of locally owned and relevant
447  // dofs of dof2
448  IndexSet dof2_locally_owned_dofs = dof2.locally_owned_dofs();
449  IndexSet dof2_locally_relevant_dofs;
451  dof2_locally_relevant_dofs);
452 
453  PETScWrappers::MPI::Vector u2_out (dof2_locally_owned_dofs,
454  u1.get_mpi_communicator());
455  interpolate(dof1, u1, dof2, constraints2, u2_out);
456  PETScWrappers::MPI::Vector u2 (dof2_locally_owned_dofs,
457  dof2_locally_relevant_dofs,
458  u1.get_mpi_communicator());
459  u2 = u2_out;
460  interpolate(dof2, u2, dof1, constraints1, u1_interpolated);
461  }
462 #endif
463 
464  // special version for Trilinos
465 #ifdef DEAL_II_WITH_TRILINOS
466  template <int dim, int spacedim>
467  void back_interpolate (const DoFHandler<dim,spacedim> &dof1,
468  const ConstraintMatrix &constraints1,
470  const DoFHandler<dim,spacedim> &dof2,
471  const ConstraintMatrix &constraints2,
472  TrilinosWrappers::MPI::Vector &u1_interpolated)
473  {
474  // if u1 is a parallel distributed Trilinos vector, we create a
475  // vector u2 with based on the sets of locally owned and relevant
476  // dofs of dof2
477  IndexSet dof2_locally_owned_dofs = dof2.locally_owned_dofs();
478  IndexSet dof2_locally_relevant_dofs;
480  dof2_locally_relevant_dofs);
481 
482  TrilinosWrappers::MPI::Vector u2_out (dof2_locally_owned_dofs,
483  u1.get_mpi_communicator());
484  interpolate(dof1, u1, dof2, constraints2, u2_out);
485  TrilinosWrappers::MPI::Vector u2 (dof2_locally_owned_dofs,
486  dof2_locally_relevant_dofs,
487  u1.get_mpi_communicator());
488  u2 = u2_out;
489  interpolate(dof2, u2, dof1, constraints1, u1_interpolated);
490  }
491 #endif
492 
493  // special version for parallel::distributed::Vector
494  template <int dim, int spacedim, typename Number>
495  void back_interpolate (const DoFHandler<dim,spacedim> &dof1,
496  const ConstraintMatrix &constraints1,
498  const DoFHandler<dim,spacedim> &dof2,
499  const ConstraintMatrix &constraints2,
500  parallel::distributed::Vector<Number> &u1_interpolated)
501  {
502  IndexSet dof2_locally_owned_dofs = dof2.locally_owned_dofs();
503  IndexSet dof2_locally_relevant_dofs;
505  dof2_locally_relevant_dofs);
506 
508  u2 (dof2_locally_owned_dofs,
509  dof2_locally_relevant_dofs,
510  u1.get_mpi_communicator());
511 
512  interpolate(dof1, u1, dof2, constraints2, u2);
513  u2.update_ghost_values ();
514  interpolate(dof2, u2, dof1, constraints1, u1_interpolated);
515  }
516  }
517  }
518 
519 
520  template <int dim, class InVector, class OutVector, int spacedim>
522  const ConstraintMatrix &constraints1,
523  const InVector &u1,
524  const DoFHandler<dim,spacedim> &dof2,
525  const ConstraintMatrix &constraints2,
526  OutVector &u1_interpolated)
527  {
528  // For discontinuous elements without constraints take the simpler version
529  // of the back_interpolate function.
530  if (dof1.get_fe().dofs_per_vertex==0 && dof2.get_fe().dofs_per_vertex==0
531  && constraints1.n_constraints()==0 && constraints2.n_constraints()==0)
532  back_interpolate(dof1, u1, dof2.get_fe(), u1_interpolated);
533  else
534  {
535  Assert(dof1.get_fe().n_components() == dof2.get_fe().n_components(),
536  ExcDimensionMismatch(dof1.get_fe().n_components(), dof2.get_fe().n_components()));
537  Assert(u1.size()==dof1.n_dofs(), ExcDimensionMismatch(u1.size(), dof1.n_dofs()));
538  Assert(u1_interpolated.size()==dof1.n_dofs(),
539  ExcDimensionMismatch(u1_interpolated.size(), dof1.n_dofs()));
540 
541  // For continuous elements first interpolate to dof2, taking into
542  // account constraints2, and then interpolate back to dof1 taking into
543  // account constraints1
544  internal::back_interpolate(dof1, constraints1, u1, dof2, constraints2,
545  u1_interpolated);
546  }
547  }
548 
549 
550 
551  template <int dim, class InVector, class OutVector, int spacedim>
553  const InVector &u1,
554  const FiniteElement<dim,spacedim> &fe2,
555  OutVector &u1_difference)
556  {
557  Assert(dof1.get_fe().n_components() == fe2.n_components(),
558  ExcDimensionMismatch(dof1.get_fe().n_components(), fe2.n_components()));
559  Assert(u1.size()==dof1.n_dofs(), ExcDimensionMismatch(u1.size(), dof1.n_dofs()));
560  Assert(u1_difference.size()==dof1.n_dofs(),
561  ExcDimensionMismatch(u1_difference.size(), dof1.n_dofs()));
562 
563 #ifdef DEAL_II_WITH_PETSC
564  if (dynamic_cast<const PETScWrappers::MPI::Vector *>(&u1) != 0)
565  if (dynamic_cast<const DoFHandler<dim>*>(&dof1) != 0)
566  {
567  // if u1 is a parallel distributed
568  // PETSc vector, we check the local
569  // size of u1 for safety
570  Assert(dynamic_cast<const PETScWrappers::MPI::Vector *>(&u1)->local_size() == dof1.n_locally_owned_dofs(),
571  ExcDimensionMismatch(dynamic_cast<const PETScWrappers::MPI::Vector *>(&u1)->local_size(), dof1.n_locally_owned_dofs()));
572  };
573 
574  if (dynamic_cast<PETScWrappers::MPI::Vector *>(&u1_difference) != 0)
575  if (dynamic_cast<const DoFHandler<dim>*>(&dof1) != 0)
576  {
577  Assert(dynamic_cast<PETScWrappers::MPI::Vector *>(&u1_difference)->local_size() == dof1.n_locally_owned_dofs(),
578  ExcDimensionMismatch(dynamic_cast<PETScWrappers::MPI::Vector *>(&u1_difference)->local_size(), dof1.n_locally_owned_dofs()));
579  };
580 #endif
581 
582  // For continuous elements on grids
583  // with hanging nodes we need
584  // hanging node
585  // constraints. Consequently, when
586  // the elements are continuous no
587  // hanging node constraints are
588  // allowed.
589  const bool hanging_nodes_not_allowed=
590  (dof1.get_fe().dofs_per_vertex != 0) || (fe2.dofs_per_vertex != 0);
591 
592  const unsigned int dofs_per_cell=dof1.get_fe().dofs_per_cell;
593 
594  Vector<typename OutVector::value_type> u1_local(dofs_per_cell);
595  Vector<typename OutVector::value_type> u1_diff_local(dofs_per_cell);
596 
597  const types::subdomain_id subdomain_id =
598  dof1.get_triangulation().locally_owned_subdomain();
599 
600  FullMatrix<double> difference_matrix(dofs_per_cell, dofs_per_cell);
602  difference_matrix);
603 
605  endc = dof1.end();
606 
607  for (; cell!=endc; ++cell)
608  if ((cell->subdomain_id() == subdomain_id)
609  ||
610  (subdomain_id == numbers::invalid_subdomain_id))
611  {
612  if (hanging_nodes_not_allowed)
613  for (unsigned int face=0; face<GeometryInfo<dim>::faces_per_cell; ++face)
614  Assert (cell->at_boundary(face) ||
615  cell->neighbor(face)->level() == cell->level(),
616  ExcHangingNodesNotAllowed(0));
617 
618  cell->get_dof_values(u1, u1_local);
619  difference_matrix.vmult(u1_diff_local, u1_local);
620  cell->set_dof_values(u1_diff_local, u1_difference);
621  }
622 
623  // if we work on a parallel PETSc vector
624  // we have to finish the work and
625  // update ghost values
626  u1_difference.compress(VectorOperation::insert);
627  }
628 
629 
630 
631  namespace internal
632  {
633  namespace
634  {
635  template <int dim, class InVector, class OutVector, int spacedim>
637  const ConstraintMatrix &constraints1,
638  const InVector &u1,
639  const DoFHandler<dim,spacedim> &dof2,
640  const ConstraintMatrix &constraints2,
641  OutVector &u1_difference)
642  {
643  back_interpolate(dof1, constraints1, u1, dof2, constraints2, u1_difference);
644  u1_difference.sadd(-1, u1);
645  }
646 
647  // special version for Trilinos
648 #ifdef DEAL_II_WITH_TRILINOS
649  template <int dim, int spacedim>
651  const ConstraintMatrix &constraints1,
653  const DoFHandler<dim,spacedim> &dof2,
654  const ConstraintMatrix &constraints2,
655  TrilinosWrappers::MPI::Vector &u1_difference)
656  {
657  back_interpolate(dof1, constraints1, u1, dof2, constraints2, u1_difference);
658 
659  // Trilinos vectors with and without ghost entries are very different
660  // and we cannot use the sadd function directly, so we have to create
661  // a completely distributed vector first and copy the local entries
662  // from the vector with ghost entries
663  TrilinosWrappers::MPI::Vector u1_completely_distributed (u1_difference.vector_partitioner ());
664 
665  u1_completely_distributed = u1;
666 
667  u1_difference.sadd(-1, u1_completely_distributed);
668  }
669 #endif
670  }
671  }
672 
673 
674 
675  template <int dim, class InVector, class OutVector, int spacedim>
677  const ConstraintMatrix &constraints1,
678  const InVector &u1,
679  const DoFHandler<dim,spacedim> &dof2,
680  const ConstraintMatrix &constraints2,
681  OutVector &u1_difference)
682  {
683  // For discontinuous elements
684  // without constraints take the
685  // cheaper version of the
686  // interpolation_difference function.
687  if (dof1.get_fe().dofs_per_vertex==0 && dof2.get_fe().dofs_per_vertex==0
688  && constraints1.n_constraints()==0 && constraints2.n_constraints()==0)
689  interpolation_difference(dof1, u1, dof2.get_fe(), u1_difference);
690  else
691  {
692  internal::interpolation_difference(dof1, constraints1, u1, dof2, constraints2, u1_difference);
693  }
694  }
695 
696 
697 
698  template <int dim, class InVector, class OutVector, int spacedim>
700  const InVector &u1,
701  const DoFHandler<dim,spacedim> &dof2,
702  OutVector &u2)
703  {
704  Assert(&dof1.get_triangulation()==&dof2.get_triangulation(), ExcTriangulationMismatch());
705  Assert(dof1.get_fe().n_components() == dof2.get_fe().n_components(),
706  ExcDimensionMismatch(dof1.get_fe().n_components(), dof2.get_fe().n_components()));
707  Assert(u1.size()==dof1.n_dofs(), ExcDimensionMismatch(u1.size(), dof1.n_dofs()));
708  Assert(u2.size()==dof2.n_dofs(), ExcDimensionMismatch(u2.size(), dof2.n_dofs()));
709 
713 
714  const unsigned int n1 = dof1.get_fe().dofs_per_cell;
715  const unsigned int n2 = dof2.get_fe().dofs_per_cell;
716 
717  Vector<double> u1_local(n1);
718  Vector<double> u2_local(n2);
719  std::vector<types::global_dof_index> dofs(n2);
720 
721  FullMatrix<double> matrix(n2,n1);
722  get_projection_matrix(dof1.get_fe(), dof2.get_fe(), matrix);
723 
724  u2 = 0;
725  while (cell2 != end)
726  {
727  cell1->get_dof_values(u1, u1_local);
728  matrix.vmult(u2_local, u1_local);
729  cell2->get_dof_indices(dofs);
730  for (unsigned int i=0; i<n2; ++i)
731  {
732  u2(dofs[i])+=u2_local(i);
733  }
734 
735  ++cell1;
736  ++cell2;
737  }
738  }
739 
740 
741  template <int dim, class InVector, class OutVector, int spacedim>
743  const InVector &u1,
744  const DoFHandler<dim,spacedim> &dof2,
745  OutVector &u2)
746  {
747  ConstraintMatrix dummy;
748  dummy.close();
749  extrapolate(dof1, u1, dof2, dummy, u2);
750  }
751 
752 
753 
754  template <int dim, class InVector, class OutVector, int spacedim>
756  const InVector &u1,
757  const DoFHandler<dim,spacedim> &dof2,
758  const ConstraintMatrix &constraints,
759  OutVector &u2)
760  {
761  Assert(dof1.get_fe().n_components() == dof2.get_fe().n_components(),
762  ExcDimensionMismatch(dof1.get_fe().n_components(), dof2.get_fe().n_components()));
763  Assert(&dof1.get_triangulation()==&dof2.get_triangulation(), ExcTriangulationMismatch());
764  Assert(u1.size()==dof1.n_dofs(), ExcDimensionMismatch(u1.size(), dof1.n_dofs()));
765  Assert(u2.size()==dof2.n_dofs(), ExcDimensionMismatch(u2.size(), dof2.n_dofs()));
766 
767  OutVector u3;
768  u3.reinit(u2);
769  interpolate(dof1, u1, dof2, constraints, u3);
770 
771  const unsigned int dofs_per_cell = dof2.get_fe().dofs_per_cell;
772  Vector<typename OutVector::value_type> dof_values(dofs_per_cell);
773 
774  // make sure that each cell on the
775  // coarsest level is at least once
776  // refined. otherwise, we can't
777  // treat these cells and would
778  // generate a bogus result
779  {
780  typename DoFHandler<dim,spacedim>::cell_iterator cell = dof2.begin(0),
781  endc = dof2.end(0);
782  for (; cell!=endc; ++cell)
783  Assert (cell->has_children(), ExcGridNotRefinedAtLeastOnce());
784  }
785 
786  // then traverse grid bottom up
787  for (unsigned int level=0; level<dof1.get_triangulation().n_levels()-1; ++level)
788  {
789  typename DoFHandler<dim,spacedim>::cell_iterator cell=dof2.begin(level),
790  endc=dof2.end(level);
791 
792  for (; cell!=endc; ++cell)
793  if (!cell->active())
794  {
795  // check whether this
796  // cell has active
797  // children
798  bool active_children=false;
799  for (unsigned int child_n=0; child_n<cell->n_children(); ++child_n)
800  if (cell->child(child_n)->active())
801  {
802  active_children=true;
803  break;
804  }
805 
806  // if there are active
807  // children, the we have
808  // to work on this
809  // cell. get the data
810  // from the one vector
811  // and set it on the
812  // other
813  if (active_children)
814  {
815  cell->get_interpolated_dof_values(u3, dof_values);
816  cell->set_dof_values_by_interpolation(dof_values, u2);
817  }
818  }
819  }
820 
821  // Apply hanging node constraints.
822  constraints.distribute(u2);
823  }
824 
825 } // end of namespace FETools
826 
827 
828 
829 /*-------------- Explicit Instantiations -------------------------------*/
830 #include "fe_tools_interpolate.inst"
831 
832 
833 /*---------------------------- fe_tools.cc ---------------------------*/
834 
835 DEAL_II_NAMESPACE_CLOSE
void project_dg(const DoFHandler< dim, spacedim > &dof1, const InVector &u1, const DoFHandler< dim, spacedim > &dof2, OutVector &u2)
const MPI_Comm & get_mpi_communicator() const
active_cell_iterator begin_active(const unsigned int level=0) const
Definition: dof_handler.cc:845
const types::subdomain_id invalid_subdomain_id
Definition: types.h:239
const MPI_Comm & get_mpi_communicator() const
void sadd(const TrilinosScalar s, const VectorBase &V)
void interpolation_difference(const DoFHandler< dim, spacedim > &dof1, const InVector &z1, const FiniteElement< dim, spacedim > &fe2, OutVector &z1_difference)
const Triangulation< dim, spacedim > & get_triangulation() const
void distribute(VectorType &vec) const
ActiveSelector::active_cell_iterator active_cell_iterator
Definition: dof_handler.h:221
void vmult(Vector< number2 > &w, const Vector< number2 > &v, const bool adding=false) const
void extrapolate(const DoFHandler< dim, spacedim > &dof1, const InVector &z1, const DoFHandler< dim, spacedim > &dof2, OutVector &z2)
void get_projection_matrix(const FiniteElement< dim, spacedim > &fe1, const FiniteElement< dim, spacedim > &fe2, FullMatrix< number > &matrix)
Definition: fe_tools.cc:549
ActiveSelector::cell_iterator cell_iterator
Definition: dof_handler.h:249
void get_interpolation_matrix(const FiniteElement< dim, spacedim > &fe1, const FiniteElement< dim, spacedim > &fe2, FullMatrix< number > &interpolation_matrix)
Definition: fe_tools.cc:433
unsigned int global_dof_index
Definition: types.h:88
#define Assert(cond, exc)
Definition: exceptions.h:294
types::global_dof_index n_dofs() const
void back_interpolate(const DoFHandler< dim, spacedim > &dof1, const InVector &u1, const FiniteElement< dim, spacedim > &fe2, OutVector &u1_interpolated)
unsigned int n_locally_owned_dofs() const
void get_back_interpolation_matrix(const FiniteElement< dim, spacedim > &fe1, const FiniteElement< dim, spacedim > &fe2, FullMatrix< number > &interpolation_matrix)
Definition: fe_tools.cc:497
void extract_locally_relevant_dofs(const DoFHandlerType &dof_handler, IndexSet &dof_set)
Definition: dof_tools.cc:944
unsigned int n_components() const
const Epetra_Map & vector_partitioner() const
unsigned int subdomain_id
Definition: types.h:42
cell_iterator end() const
Definition: dof_handler.cc:861
const MPI_Comm & get_mpi_communicator() const
cell_iterator begin(const unsigned int level=0) const
Definition: dof_handler.cc:833
void get_interpolation_difference_matrix(const FiniteElement< dim, spacedim > &fe1, const FiniteElement< dim, spacedim > &fe2, FullMatrix< number > &difference_matrix)
Definition: fe_tools.cc:523
size_type n_constraints() const
virtual void reinit(const size_type N, const bool omit_zeroing_entries=false)
bool is_element(const size_type index) const
Definition: index_set.h:1317
const unsigned int dofs_per_vertex
Definition: fe_base.h:226
const IndexSet & locally_owned_dofs() const
const FiniteElement< dim, spacedim > & get_fe() const
void interpolate(const DoFHandlerType1< dim, spacedim > &dof1, const InVector &u1, const DoFHandlerType2< dim, spacedim > &dof2, OutVector &u2)