Reference documentation for deal.II version Git f40be01994 2020-04-09 07:13:12 +0200
\(\newcommand{\vcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\vcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
fe_enriched.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2016 - 2019 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 #include <deal.II/base/std_cxx14/memory.h>
18 
19 #include <deal.II/fe/fe_enriched.h>
20 #include <deal.II/fe/fe_tools.h>
21 
22 #include <deal.II/lac/sparsity_tools.h>
23 
24 DEAL_II_NAMESPACE_OPEN
25 
26 namespace internal
27 {
28  namespace FE_Enriched
29  {
30  namespace
31  {
36  template <typename T>
37  std::vector<unsigned int>
38  build_multiplicities(const std::vector<std::vector<T>> &functions)
39  {
40  std::vector<unsigned int> multiplicities;
41  multiplicities.push_back(1); // the first one is non-enriched FE
42  for (unsigned int i = 0; i < functions.size(); i++)
43  multiplicities.push_back(functions[i].size());
44 
45  return multiplicities;
46  }
47 
48 
52  template <int dim, int spacedim>
53  std::vector<const FiniteElement<dim, spacedim> *>
54  build_fes(
55  const FiniteElement<dim, spacedim> * fe_base,
56  const std::vector<const FiniteElement<dim, spacedim> *> &fe_enriched)
57  {
58  std::vector<const FiniteElement<dim, spacedim> *> fes;
59  fes.push_back(fe_base);
60  for (unsigned int i = 0; i < fe_enriched.size(); i++)
61  fes.push_back(fe_enriched[i]);
62 
63  return fes;
64  }
65 
66 
71  template <int dim, int spacedim>
72  bool
73  consistency_check(
74  const std::vector<const FiniteElement<dim, spacedim> *> &fes,
75  const std::vector<unsigned int> & multiplicities,
76  const std::vector<std::vector<std::function<const Function<spacedim> *(
77  const typename ::Triangulation<dim, spacedim>::cell_iterator
78  &)>>> & functions)
79  {
80  AssertThrow(fes.size() > 0, ExcMessage("FEs size should be >=1"));
81  AssertThrow(fes.size() == multiplicities.size(),
82  ExcMessage(
83  "FEs and multiplicities should have the same size"));
84 
85  AssertThrow(functions.size() == fes.size() - 1,
86  ExcDimensionMismatch(functions.size(), fes.size() - 1));
87 
88  AssertThrow(multiplicities[0] == 1,
89  ExcMessage("First multiplicity should be 1"));
90 
91  const unsigned int n_comp_base = fes[0]->n_components();
92 
93  // start from fe=1 as 0th is always non-enriched FE.
94  for (unsigned int fe = 1; fe < fes.size(); fe++)
95  {
96  const FE_Nothing<dim> *fe_nothing =
97  dynamic_cast<const FE_Nothing<dim> *>(fes[fe]);
98  if (fe_nothing)
100  fe_nothing->is_dominating(),
101  ExcMessage(
102  "Only dominating FE_Nothing can be used in FE_Enriched"));
103 
104  AssertThrow(
105  fes[fe]->n_components() == n_comp_base,
106  ExcMessage(
107  "All elements must have the same number of components"));
108  }
109  return true;
110  }
111 
112 
117  template <int dim, int spacedim>
118  bool
119  check_if_enriched(
120  const std::vector<const FiniteElement<dim, spacedim> *> &fes)
121  {
122  // start from fe=1 as 0th is always non-enriched FE.
123  for (unsigned int fe = 1; fe < fes.size(); fe++)
124  if (dynamic_cast<const FE_Nothing<dim> *>(fes[fe]) == nullptr)
125  // this is not FE_Nothing => there will be enrichment
126  return true;
127 
128  return false;
129  }
130  } // namespace
131  } // namespace FE_Enriched
132 } // namespace internal
133 
134 
135 template <int dim, int spacedim>
137  const FiniteElement<dim, spacedim> &fe_base)
138  : FE_Enriched<dim, spacedim>(fe_base,
139  FE_Nothing<dim, spacedim>(fe_base.n_components(),
140  true),
141  nullptr)
142 {}
143 
144 
145 template <int dim, int spacedim>
147  const FiniteElement<dim, spacedim> &fe_base,
148  const FiniteElement<dim, spacedim> &fe_enriched,
149  const Function<spacedim> * enrichment_function)
150  : FE_Enriched<dim, spacedim>(
151  &fe_base,
152  std::vector<const FiniteElement<dim, spacedim> *>(1, &fe_enriched),
153  std::vector<std::vector<std::function<const Function<spacedim> *(
154  const typename Triangulation<dim, spacedim>::cell_iterator &)>>>(
155  1,
156  std::vector<std::function<const Function<spacedim> *(
157  const typename Triangulation<dim, spacedim>::cell_iterator &)>>(
158  1,
159  [=](const typename Triangulation<dim, spacedim>::cell_iterator &)
160  -> const Function<spacedim> * { return enrichment_function; })))
161 {}
162 
163 
164 template <int dim, int spacedim>
166  const FiniteElement<dim, spacedim> * fe_base,
167  const std::vector<const FiniteElement<dim, spacedim> *> &fe_enriched,
168  const std::vector<std::vector<std::function<const Function<spacedim> *(
169  const typename Triangulation<dim, spacedim>::cell_iterator &)>>> &functions)
170  : FE_Enriched<dim, spacedim>(
171  internal::FE_Enriched::build_fes(fe_base, fe_enriched),
172  internal::FE_Enriched::build_multiplicities(functions),
173  functions)
174 {}
175 
176 
177 template <int dim, int spacedim>
179  const std::vector<const FiniteElement<dim, spacedim> *> &fes,
180  const std::vector<unsigned int> & multiplicities,
181  const std::vector<std::vector<std::function<const Function<spacedim> *(
182  const typename Triangulation<dim, spacedim>::cell_iterator &)>>> &functions)
183  : FiniteElement<dim, spacedim>(
184  FETools::Compositing::multiply_dof_numbers(fes, multiplicities, false),
185  FETools::Compositing::compute_restriction_is_additive_flags(
186  fes,
187  multiplicities),
188  FETools::Compositing::compute_nonzero_components(fes,
189  multiplicities,
190  false))
191  , enrichments(functions)
192  , is_enriched(internal::FE_Enriched::check_if_enriched(fes))
193  , fe_system(
194  std_cxx14::make_unique<FESystem<dim, spacedim>>(fes, multiplicities))
195 {
196  // descriptive error are thrown within the function.
197  Assert(internal::FE_Enriched::consistency_check(fes,
198  multiplicities,
199  functions),
200  ExcInternalError());
201 
202  initialize(fes, multiplicities);
203 
204  // resize to be consistent with all FEs used to construct the FE_Enriched,
205  // even though we will never use the 0th element.
206  base_no_mult_local_enriched_dofs.resize(fes.size());
207  for (unsigned int fe = 1; fe < fes.size(); fe++)
208  base_no_mult_local_enriched_dofs[fe].resize(multiplicities[fe]);
209 
212  this->n_base_elements()));
213 
214  // build the map: (base_no, base_m) -> vector of local element DoFs
215  for (unsigned int system_index = 0; system_index < this->dofs_per_cell;
216  ++system_index)
217  {
218  const unsigned int base_no =
219  this->system_to_base_table[system_index].first.first;
220  if (base_no == 0) // 0th is always non-enriched FE
221  continue;
222 
223  const unsigned int base_m =
224  this->system_to_base_table[system_index].first.second;
225 
226  Assert(base_m < base_no_mult_local_enriched_dofs[base_no].size(),
227  ExcMessage(
228  "Size mismatch for base_no_mult_local_enriched_dofs: "
229  "base_index = " +
230  std::to_string(this->system_to_base_table[system_index].second) +
231  "; base_no = " + std::to_string(base_no) +
232  "; base_m = " + std::to_string(base_m) +
233  "; system_index = " + std::to_string(system_index)));
234 
235  Assert(base_m < base_no_mult_local_enriched_dofs[base_no].size(),
237  base_m, base_no_mult_local_enriched_dofs[base_no].size()));
238 
239  base_no_mult_local_enriched_dofs[base_no][base_m].push_back(system_index);
240  }
241 
242  // make sure that local_enriched_dofs.size() is correct, that is equals to
243  // DoFs per cell of the corresponding FE.
244  for (unsigned int base_no = 1;
245  base_no < base_no_mult_local_enriched_dofs.size();
246  base_no++)
247  {
248  for (unsigned int m = 0;
249  m < base_no_mult_local_enriched_dofs[base_no].size();
250  m++)
251  Assert(base_no_mult_local_enriched_dofs[base_no][m].size() ==
252  fes[base_no]->dofs_per_cell,
254  base_no_mult_local_enriched_dofs[base_no][m].size(),
255  fes[base_no]->dofs_per_cell));
256  }
257 }
258 
259 
260 template <int dim, int spacedim>
261 const std::vector<std::vector<std::function<const Function<spacedim> *(
264 {
265  return enrichments;
266 }
267 
268 
269 template <int dim, int spacedim>
270 double
272  const Point<dim> & p) const
273 {
274  Assert(
275  !is_enriched,
276  ExcMessage(
277  "For enriched finite elements shape_value() can not be defined on the reference element."));
278  return fe_system->shape_value(i, p);
279 }
280 
281 
282 template <int dim, int spacedim>
283 std::unique_ptr<FiniteElement<dim, spacedim>>
285 {
286  std::vector<const FiniteElement<dim, spacedim> *> fes;
287  std::vector<unsigned int> multiplicities;
288 
289  for (unsigned int i = 0; i < this->n_base_elements(); i++)
290  {
291  fes.push_back(&base_element(i));
292  multiplicities.push_back(this->element_multiplicity(i));
293  }
294 
295  return std::unique_ptr<FE_Enriched<dim, spacedim>>(
296  new FE_Enriched<dim, spacedim>(fes, multiplicities, get_enrichments()));
297 }
298 
299 
300 template <int dim, int spacedim>
303 {
304  UpdateFlags out = fe_system->requires_update_flags(flags);
305 
306  if (is_enriched)
307  {
308  // if we ask for values or gradients, then we would need quadrature points
309  if (flags & (update_values | update_gradients))
311 
312  // if need gradients, add update_values due to product rule
313  if (out & update_gradients)
314  out |= update_values;
315  }
316 
318 
319  return out;
320 }
321 
322 
323 template <int dim, int spacedim>
324 template <int dim_1>
325 std::unique_ptr<typename FiniteElement<dim, spacedim>::InternalDataBase>
327  std::unique_ptr<typename FESystem<dim, spacedim>::InternalData> fes_data,
328  const UpdateFlags flags,
329  const Quadrature<dim_1> &quadrature) const
330 {
331  // Pass ownership of the FiniteElement::InternalDataBase object
332  // that fes_data points to, to the new InternalData object.
333  auto update_each_flags = fes_data->update_each;
334  std::unique_ptr<typename FiniteElement<dim, spacedim>::InternalDataBase>
335  data_ptr = std_cxx14::make_unique<InternalData>(std::move(fes_data));
336  auto &data = dynamic_cast<InternalData &>(*data_ptr);
337 
338  // copy update_each from FESystem data:
339  data.update_each = update_each_flags;
340 
341  // resize cache array according to requested flags
342  data.enrichment.resize(this->n_base_elements());
343 
344  const unsigned int n_q_points = quadrature.size();
345 
346  for (unsigned int base = 0; base < this->n_base_elements(); ++base)
347  {
348  data.enrichment[base].resize(this->element_multiplicity(base));
349  for (unsigned int m = 0; m < this->element_multiplicity(base); ++m)
350  {
351  if (flags & update_values)
352  data.enrichment[base][m].values.resize(n_q_points);
353 
354  if (flags & update_gradients)
355  data.enrichment[base][m].gradients.resize(n_q_points);
356 
357  if (flags & update_hessians)
358  data.enrichment[base][m].hessians.resize(n_q_points);
359  }
360  }
361 
362  return data_ptr;
363 }
364 
365 
366 template <int dim, int spacedim>
367 std::unique_ptr<typename FiniteElement<dim, spacedim>::InternalDataBase>
369  const UpdateFlags update_flags,
370  const Mapping<dim, spacedim> &mapping,
371  const Quadrature<dim - 1> & quadrature,
373  &output_data) const
374 {
375  auto data =
376  fe_system->get_face_data(update_flags, mapping, quadrature, output_data);
379  std::move(data)),
380  update_flags,
381  quadrature);
382 }
383 
384 
385 template <int dim, int spacedim>
386 std::unique_ptr<typename FiniteElement<dim, spacedim>::InternalDataBase>
388  const UpdateFlags update_flags,
389  const Mapping<dim, spacedim> &mapping,
390  const Quadrature<dim - 1> & quadrature,
392  spacedim>
393  &output_data) const
394 {
395  auto data =
396  fe_system->get_subface_data(update_flags, mapping, quadrature, output_data);
399  std::move(data)),
400  update_flags,
401  quadrature);
402 }
403 
404 
405 template <int dim, int spacedim>
406 std::unique_ptr<typename FiniteElement<dim, spacedim>::InternalDataBase>
408  const UpdateFlags flags,
409  const Mapping<dim, spacedim> &mapping,
410  const Quadrature<dim> & quadrature,
412  &output_data) const
413 {
414  auto data = fe_system->get_data(flags, mapping, quadrature, output_data);
417  std::move(data)),
418  flags,
419  quadrature);
420 }
421 
422 
423 template <int dim, int spacedim>
424 void
426  const std::vector<const FiniteElement<dim, spacedim> *> &fes,
427  const std::vector<unsigned int> & multiplicities)
428 {
429  Assert(fes.size() == multiplicities.size(),
430  ExcDimensionMismatch(fes.size(), multiplicities.size()));
431 
432  // Note that we need to skip every fe with multiplicity 0 in the following
433  // block of code
434  this->base_to_block_indices.reinit(0, 0);
435 
436  for (unsigned int i = 0; i < fes.size(); i++)
437  if (multiplicities[i] > 0)
438  this->base_to_block_indices.push_back(multiplicities[i]);
439 
440  {
441  // If the system is not primitive, these have not been initialized by
442  // FiniteElement
443  this->system_to_component_table.resize(this->dofs_per_cell);
444  this->face_system_to_component_table.resize(this->dofs_per_face);
445 
449  *this,
450  false);
451 
455  *this,
456  false);
457  }
458 
459  // restriction and prolongation matrices are built on demand
460 
461  // now set up the interface constraints for h-refinement.
462  // take them from fe_system:
463  this->interface_constraints = fe_system->interface_constraints;
464 
465  // if we just wrap another FE (i.e. use FE_Nothing as a second FE)
466  // then it makes sense to have support points.
467  // However, functions like interpolate_boundary_values() need all FEs inside
468  // FECollection to be able to provide support points irrespectively whether
469  // this FE sits on the boundary or not. Thus for moment just copy support
470  // points from fe system:
471  {
472  this->unit_support_points = fe_system->unit_support_points;
473  this->unit_face_support_points = fe_system->unit_face_support_points;
474  }
475 
476  // take adjust_quad_dof_index_for_face_orientation_table from FESystem:
477  {
479  fe_system->adjust_line_dof_index_for_line_orientation_table;
480  }
481 }
482 
483 
484 template <int dim, int spacedim>
485 std::string
487 {
488  std::ostringstream namebuf;
489 
490  namebuf << "FE_Enriched<" << Utilities::dim_string(dim, spacedim) << ">[";
491  for (unsigned int i = 0; i < this->n_base_elements(); ++i)
492  {
493  namebuf << base_element(i).get_name();
494  if (this->element_multiplicity(i) != 1)
495  namebuf << '^' << this->element_multiplicity(i);
496  if (i != this->n_base_elements() - 1)
497  namebuf << '-';
498  }
499  namebuf << ']';
500 
501  return namebuf.str();
502 }
503 
504 
505 template <int dim, int spacedim>
507 FE_Enriched<dim, spacedim>::base_element(const unsigned int index) const
508 {
509  return fe_system->base_element(index);
510 }
511 
512 
513 template <int dim, int spacedim>
514 void
516  const typename Triangulation<dim, spacedim>::cell_iterator &cell,
517  const CellSimilarity::Similarity cell_similarity,
518  const Quadrature<dim> & quadrature,
519  const Mapping<dim, spacedim> & mapping,
520  const typename Mapping<dim, spacedim>::InternalDataBase & mapping_internal,
521  const ::internal::FEValuesImplementation::MappingRelatedData<dim,
522  spacedim>
523  & mapping_data,
524  const typename FiniteElement<dim, spacedim>::InternalDataBase &fe_internal,
526  &output_data) const
527 {
528  Assert(dynamic_cast<const InternalData *>(&fe_internal) != nullptr,
529  ExcInternalError());
530  const InternalData &fe_data = static_cast<const InternalData &>(fe_internal);
531 
532  // call FESystem's method to fill everything without enrichment function
533  fe_system->fill_fe_values(cell,
534  cell_similarity,
535  quadrature,
536  mapping,
537  mapping_internal,
538  mapping_data,
539  *fe_data.fesystem_data,
540  output_data);
541 
542  if (is_enriched)
544  quadrature, fe_data, mapping_data, cell, output_data);
545 }
546 
547 
548 template <int dim, int spacedim>
549 void
551  const typename Triangulation<dim, spacedim>::cell_iterator &cell,
552  const unsigned int face_no,
553  const Quadrature<dim - 1> & quadrature,
554  const Mapping<dim, spacedim> & mapping,
555  const typename Mapping<dim, spacedim>::InternalDataBase & mapping_internal,
556  const ::internal::FEValuesImplementation::MappingRelatedData<dim,
557  spacedim>
558  & mapping_data,
559  const typename FiniteElement<dim, spacedim>::InternalDataBase &fe_internal,
561  &output_data) const
562 {
563  Assert(dynamic_cast<const InternalData *>(&fe_internal) != nullptr,
564  ExcInternalError());
565  const InternalData &fe_data = static_cast<const InternalData &>(fe_internal);
566 
567  // call FESystem's method to fill everything without enrichment function
568  fe_system->fill_fe_face_values(cell,
569  face_no,
570  quadrature,
571  mapping,
572  mapping_internal,
573  mapping_data,
574  *fe_data.fesystem_data,
575  output_data);
576 
577  if (is_enriched)
579  quadrature, fe_data, mapping_data, cell, output_data);
580 }
581 
582 
583 template <int dim, int spacedim>
584 void
586  const typename Triangulation<dim, spacedim>::cell_iterator &cell,
587  const unsigned int face_no,
588  const unsigned int sub_no,
589  const Quadrature<dim - 1> & quadrature,
590  const Mapping<dim, spacedim> & mapping,
591  const typename Mapping<dim, spacedim>::InternalDataBase & mapping_internal,
592  const ::internal::FEValuesImplementation::MappingRelatedData<dim,
593  spacedim>
594  & mapping_data,
595  const typename FiniteElement<dim, spacedim>::InternalDataBase &fe_internal,
597  &output_data) const
598 {
599  Assert(dynamic_cast<const InternalData *>(&fe_internal) != nullptr,
600  ExcInternalError());
601  const InternalData &fe_data = static_cast<const InternalData &>(fe_internal);
602 
603  // call FESystem's method to fill everything without enrichment function
604  fe_system->fill_fe_subface_values(cell,
605  face_no,
606  sub_no,
607  quadrature,
608  mapping,
609  mapping_internal,
610  mapping_data,
611  *fe_data.fesystem_data,
612  output_data);
613 
614  if (is_enriched)
616  quadrature, fe_data, mapping_data, cell, output_data);
617 }
618 
619 
620 template <int dim, int spacedim>
621 template <int dim_1>
622 void
624  const Quadrature<dim_1> &quadrature,
625  const InternalData & fe_data,
627  & mapping_data,
628  const typename Triangulation<dim, spacedim>::cell_iterator &cell,
630  &output_data) const
631 {
632  // mapping_data will contain quadrature points on the real element.
633  // fe_internal is needed to get update flags
634  // finally, output_data should store all the results we need.
635 
636  // Either dim_1==dim
637  // (fill_fe_values) or dim_1==dim-1
638  // (fill_fe_(sub)face_values)
639  Assert(dim_1 == dim || dim_1 == dim - 1, ExcInternalError());
640  const UpdateFlags flags = fe_data.update_each;
641 
642  const unsigned int n_q_points = quadrature.size();
643 
644  // First, populate output_data object (that shall hold everything requested
645  // such as shape value, gradients, hessians, etc) from each base element. That
646  // is almost identical to FESystem::compute_fill_one_base(), the difference
647  // being that we do it irrespectively of cell_similarity and use
648  // base_fe_data.update_flags
649 
650  // TODO: do we need it only for dim_1 == dim (i.e. fill_fe_values)?
651  if (dim_1 == dim)
652  for (unsigned int base_no = 1; base_no < this->n_base_elements(); base_no++)
653  {
654  const FiniteElement<dim, spacedim> &base_fe = base_element(base_no);
655  typename FiniteElement<dim, spacedim>::InternalDataBase &base_fe_data =
656  fe_data.get_fe_data(base_no);
658  spacedim>
659  &base_data = fe_data.get_fe_output_object(base_no);
660 
661  const UpdateFlags base_flags = base_fe_data.update_each;
662 
663  for (unsigned int system_index = 0; system_index < this->dofs_per_cell;
664  ++system_index)
665  if (this->system_to_base_table[system_index].first.first == base_no)
666  {
667  const unsigned int base_index =
668  this->system_to_base_table[system_index].second;
669  Assert(base_index < base_fe.dofs_per_cell, ExcInternalError());
670 
671  // now copy. if the shape function is primitive, then there
672  // is only one value to be copied, but for non-primitive
673  // elements, there might be more values to be copied
674  //
675  // so, find out from which index to take this one value, and
676  // to which index to put
677  unsigned int out_index = 0;
678  for (unsigned int i = 0; i < system_index; ++i)
679  out_index += this->n_nonzero_components(i);
680  unsigned int in_index = 0;
681  for (unsigned int i = 0; i < base_index; ++i)
682  in_index += base_fe.n_nonzero_components(i);
683 
684  // then loop over the number of components to be copied
685  Assert(this->n_nonzero_components(system_index) ==
686  base_fe.n_nonzero_components(base_index),
687  ExcInternalError());
688  for (unsigned int s = 0;
689  s < this->n_nonzero_components(system_index);
690  ++s)
691  {
692  if (base_flags & update_values)
693  for (unsigned int q = 0; q < n_q_points; ++q)
694  output_data.shape_values[out_index + s][q] =
695  base_data.shape_values(in_index + s, q);
696 
697  if (base_flags & update_gradients)
698  for (unsigned int q = 0; q < n_q_points; ++q)
699  output_data.shape_gradients[out_index + s][q] =
700  base_data.shape_gradients[in_index + s][q];
701 
702  if (base_flags & update_hessians)
703  for (unsigned int q = 0; q < n_q_points; ++q)
704  output_data.shape_hessians[out_index + s][q] =
705  base_data.shape_hessians[in_index + s][q];
706  }
707  }
708  }
709 
710  Assert(base_no_mult_local_enriched_dofs.size() == fe_data.enrichment.size(),
712  fe_data.enrichment.size()));
713  // calculate hessians, gradients and values for each function
714  for (unsigned int base_no = 1; base_no < this->n_base_elements(); base_no++)
715  {
716  Assert(
717  base_no_mult_local_enriched_dofs[base_no].size() ==
718  fe_data.enrichment[base_no].size(),
720  fe_data.enrichment[base_no].size()));
721  for (unsigned int m = 0;
722  m < base_no_mult_local_enriched_dofs[base_no].size();
723  m++)
724  {
725  // Avoid evaluating quadrature points if no dofs are assigned. This
726  // happens when FE_Nothing is used together with other FE (i.e. FE_Q)
727  // as enrichments.
728  if (base_no_mult_local_enriched_dofs[base_no][m].size() == 0)
729  continue;
730 
731  Assert(enrichments[base_no - 1][m](cell) != nullptr,
732  ExcMessage(
733  "The pointer to the enrichment function is not set"));
734 
735  Assert(enrichments[base_no - 1][m](cell)->n_components == 1,
736  ExcMessage(
737  "Only scalar-valued enrichment functions are allowed"));
738 
739  if (flags & update_hessians)
740  {
741  Assert(fe_data.enrichment[base_no][m].hessians.size() ==
742  n_q_points,
744  fe_data.enrichment[base_no][m].hessians.size(),
745  n_q_points));
746  for (unsigned int q = 0; q < n_q_points; q++)
747  fe_data.enrichment[base_no][m].hessians[q] =
748  enrichments[base_no - 1][m](cell)->hessian(
749  mapping_data.quadrature_points[q]);
750  }
751 
752  if (flags & update_gradients)
753  {
754  Assert(fe_data.enrichment[base_no][m].gradients.size() ==
755  n_q_points,
757  fe_data.enrichment[base_no][m].gradients.size(),
758  n_q_points));
759  for (unsigned int q = 0; q < n_q_points; q++)
760  fe_data.enrichment[base_no][m].gradients[q] =
761  enrichments[base_no - 1][m](cell)->gradient(
762  mapping_data.quadrature_points[q]);
763  }
764 
765  if (flags & update_values)
766  {
767  Assert(fe_data.enrichment[base_no][m].values.size() == n_q_points,
769  fe_data.enrichment[base_no][m].values.size(),
770  n_q_points));
771  for (unsigned int q = 0; q < n_q_points; q++)
772  fe_data.enrichment[base_no][m].values[q] =
773  enrichments[base_no - 1][m](cell)->value(
774  mapping_data.quadrature_points[q]);
775  }
776  }
777  }
778 
779  // Finally, update the standard data stored in output_data
780  // by expanding the product rule for enrichment function.
781  // note that the order if important, namely
782  // output_data.shape_XYZ contains values of standard FEM and we overwrite
783  // it with the updated one in the following order: hessians -> gradients ->
784  // values
785  if (flags & update_hessians)
786  {
787  for (unsigned int base_no = 1; base_no < this->n_base_elements();
788  base_no++)
789  {
790  for (unsigned int m = 0;
791  m < base_no_mult_local_enriched_dofs[base_no].size();
792  m++)
793  for (unsigned int i = 0;
794  i < base_no_mult_local_enriched_dofs[base_no][m].size();
795  i++)
796  {
797  const unsigned int enriched_dof =
798  base_no_mult_local_enriched_dofs[base_no][m][i];
799  for (unsigned int q = 0; q < n_q_points; ++q)
800  {
801  const Tensor<2, spacedim> grad_grad = outer_product(
802  output_data.shape_gradients[enriched_dof][q],
803  fe_data.enrichment[base_no][m].gradients[q]);
804  const Tensor<2, spacedim, double> sym_grad_grad =
805  symmetrize(grad_grad) * 2.0; // symmetrize does [s+s^T]/2
806 
807  output_data.shape_hessians[enriched_dof][q] *=
808  fe_data.enrichment[base_no][m].values[q];
809  output_data.shape_hessians[enriched_dof][q] +=
810  sym_grad_grad +
811  output_data.shape_values[enriched_dof][q] *
812  fe_data.enrichment[base_no][m].hessians[q];
813  }
814  }
815  }
816  }
817 
818  if (flags & update_gradients)
819  for (unsigned int base_no = 1; base_no < this->n_base_elements(); base_no++)
820  {
821  for (unsigned int m = 0;
822  m < base_no_mult_local_enriched_dofs[base_no].size();
823  m++)
824  for (unsigned int i = 0;
825  i < base_no_mult_local_enriched_dofs[base_no][m].size();
826  i++)
827  {
828  const unsigned int enriched_dof =
829  base_no_mult_local_enriched_dofs[base_no][m][i];
830  for (unsigned int q = 0; q < n_q_points; ++q)
831  {
832  output_data.shape_gradients[enriched_dof][q] *=
833  fe_data.enrichment[base_no][m].values[q];
834  output_data.shape_gradients[enriched_dof][q] +=
835  output_data.shape_values[enriched_dof][q] *
836  fe_data.enrichment[base_no][m].gradients[q];
837  }
838  }
839  }
840 
841  if (flags & update_values)
842  for (unsigned int base_no = 1; base_no < this->n_base_elements(); base_no++)
843  {
844  for (unsigned int m = 0;
845  m < base_no_mult_local_enriched_dofs[base_no].size();
846  m++)
847  for (unsigned int i = 0;
848  i < base_no_mult_local_enriched_dofs[base_no][m].size();
849  i++)
850  {
851  const unsigned int enriched_dof =
852  base_no_mult_local_enriched_dofs[base_no][m][i];
853  for (unsigned int q = 0; q < n_q_points; ++q)
854  {
855  output_data.shape_values[enriched_dof][q] *=
856  fe_data.enrichment[base_no][m].values[q];
857  }
858  }
859  }
860 }
861 
862 
863 template <int dim, int spacedim>
866 {
867  return *fe_system;
868 }
869 
870 
871 template <int dim, int spacedim>
872 bool
874 {
875  return true;
876 }
877 
878 
879 template <int dim, int spacedim>
880 void
882  const FiniteElement<dim, spacedim> &source,
883  FullMatrix<double> & matrix) const
884 {
885  if (const FE_Enriched<dim, spacedim> *fe_enr_other =
886  dynamic_cast<const FE_Enriched<dim, spacedim> *>(&source))
887  {
888  fe_system->get_face_interpolation_matrix(fe_enr_other->get_fe_system(),
889  matrix);
890  }
891  else
892  {
893  AssertThrow(
894  false,
895  (typename FiniteElement<dim,
896  spacedim>::ExcInterpolationNotImplemented()));
897  }
898 }
899 
900 
901 template <int dim, int spacedim>
902 void
904  const FiniteElement<dim, spacedim> &source,
905  const unsigned int subface,
906  FullMatrix<double> & matrix) const
907 {
908  if (const FE_Enriched<dim, spacedim> *fe_enr_other =
909  dynamic_cast<const FE_Enriched<dim, spacedim> *>(&source))
910  {
911  fe_system->get_subface_interpolation_matrix(fe_enr_other->get_fe_system(),
912  subface,
913  matrix);
914  }
915  else
916  {
917  AssertThrow(
918  false,
919  (typename FiniteElement<dim,
920  spacedim>::ExcInterpolationNotImplemented()));
921  }
922 }
923 
924 
925 template <int dim, int spacedim>
926 std::vector<std::pair<unsigned int, unsigned int>>
928  const FiniteElement<dim, spacedim> &fe_other) const
929 {
930  if (const FE_Enriched<dim, spacedim> *fe_enr_other =
931  dynamic_cast<const FE_Enriched<dim, spacedim> *>(&fe_other))
932  {
933  return fe_system->hp_vertex_dof_identities(fe_enr_other->get_fe_system());
934  }
935  else
936  {
937  Assert(false, ExcNotImplemented());
938  return std::vector<std::pair<unsigned int, unsigned int>>();
939  }
940 }
941 
942 
943 template <int dim, int spacedim>
944 std::vector<std::pair<unsigned int, unsigned int>>
946  const FiniteElement<dim, spacedim> &fe_other) const
947 {
948  if (const FE_Enriched<dim, spacedim> *fe_enr_other =
949  dynamic_cast<const FE_Enriched<dim, spacedim> *>(&fe_other))
950  {
951  return fe_system->hp_line_dof_identities(fe_enr_other->get_fe_system());
952  }
953  else
954  {
955  Assert(false, ExcNotImplemented());
956  return std::vector<std::pair<unsigned int, unsigned int>>();
957  }
958 }
959 
960 
961 template <int dim, int spacedim>
962 std::vector<std::pair<unsigned int, unsigned int>>
964  const FiniteElement<dim, spacedim> &fe_other) const
965 {
966  if (const FE_Enriched<dim, spacedim> *fe_enr_other =
967  dynamic_cast<const FE_Enriched<dim, spacedim> *>(&fe_other))
968  {
969  return fe_system->hp_quad_dof_identities(fe_enr_other->get_fe_system());
970  }
971  else
972  {
973  Assert(false, ExcNotImplemented());
974  return std::vector<std::pair<unsigned int, unsigned int>>();
975  }
976 }
977 
978 
979 template <int dim, int spacedim>
982  const FiniteElement<dim, spacedim> &fe_other,
983  const unsigned int codim) const
984 {
985  Assert(codim <= dim, ExcImpossibleInDim(dim));
986 
987  // vertex/line/face/cell domination
988  // --------------------------------
989  // need to decide which element constrain another.
990  // for example Q(2) dominate Q(4) and thus some DoFs of Q(4) will be
991  // constrained. If we have Q(2) and Q(4)+POU, then it's clear that Q(2)
992  // dominates, namely our DoFs will be constrained to make field continuous.
993  // However, we need to check for situations like Q(4) vs Q(2)+POU.
994  // In that case the domination for the underlying FEs should be the other way,
995  // but this implies that we can't constrain POU dofs to make the field
996  // continuous. In that case, throw an error
997 
998  // if it's also enriched, do domination based on each one's FESystem
999  if (const FE_Enriched<dim, spacedim> *fe_enr_other =
1000  dynamic_cast<const FE_Enriched<dim, spacedim> *>(&fe_other))
1001  {
1002  return fe_system->compare_for_domination(fe_enr_other->get_fe_system(),
1003  codim);
1004  }
1005  else
1006  {
1007  Assert(false, ExcNotImplemented());
1009  }
1010 }
1011 
1012 
1013 template <int dim, int spacedim>
1014 const FullMatrix<double> &
1016  const unsigned int child,
1017  const RefinementCase<dim> &refinement_case) const
1018 {
1019  return fe_system->get_prolongation_matrix(child, refinement_case);
1020 }
1021 
1022 
1023 template <int dim, int spacedim>
1024 const FullMatrix<double> &
1026  const unsigned int child,
1027  const RefinementCase<dim> &refinement_case) const
1028 {
1029  return fe_system->get_restriction_matrix(child, refinement_case);
1030 }
1031 
1032 
1033 /* ----------------------- FESystem::InternalData ------------------- */
1034 
1035 
1036 template <int dim, int spacedim>
1038  std::unique_ptr<typename FESystem<dim, spacedim>::InternalData> fesystem_data)
1039  : fesystem_data(std::move(fesystem_data))
1040 {}
1041 
1042 
1043 template <int dim, int spacedim>
1046  const unsigned int base_no) const
1047 {
1048  return fesystem_data->get_fe_data(base_no);
1049 }
1050 
1051 
1052 template <int dim, int spacedim>
1055  const unsigned int base_no) const
1056 {
1057  return fesystem_data->get_fe_output_object(base_no);
1058 }
1059 
1060 
1061 namespace ColorEnriched
1062 {
1063  namespace internal
1064  {
1065  template <int dim, int spacedim>
1066  bool
1067  find_connection_between_subdomains(
1068  const hp::DoFHandler<dim, spacedim> & dof_handler,
1069  const predicate_function<dim, spacedim> &predicate_1,
1070  const predicate_function<dim, spacedim> &predicate_2)
1071  {
1072  // Use a vector to mark vertices
1073  std::vector<bool> vertices_subdomain_1(
1074  dof_handler.get_triangulation().n_vertices(), false);
1075 
1076  // Mark vertices that belong to cells in subdomain 1
1077  for (const auto &cell : dof_handler.active_cell_iterators())
1078  if (predicate_1(cell)) // True ==> part of subdomain 1
1079  for (const unsigned int v : GeometryInfo<dim>::vertex_indices())
1080  vertices_subdomain_1[cell->vertex_index(v)] = true;
1081 
1082  // Find if cells in subdomain 2 and subdomain 1 share vertices.
1083  for (const auto &cell : dof_handler.active_cell_iterators())
1084  if (predicate_2(cell)) // True ==> part of subdomain 2
1085  for (const unsigned int v : GeometryInfo<dim>::vertex_indices())
1086  if (vertices_subdomain_1[cell->vertex_index(v)] == true)
1087  {
1088  return true;
1089  }
1090  return false;
1091  }
1092 
1093 
1094 
1095  template <int dim, int spacedim>
1096  unsigned int
1097  color_predicates(
1098  const hp::DoFHandler<dim, spacedim> & mesh,
1099  const std::vector<predicate_function<dim, spacedim>> &predicates,
1100  std::vector<unsigned int> & predicate_colors)
1101  {
1102  const unsigned int num_indices = predicates.size();
1103 
1104  // Use sparsity pattern to represent connections between subdomains.
1105  // Each predicate (i.e a subdomain) is a node in the graph.
1107  dsp.reinit(num_indices, num_indices);
1108 
1109  /*
1110  * Find connections between subdomains taken two at a time.
1111  * If the connection exists, add it to a graph object represented
1112  * by dynamic sparsity pattern.
1113  */
1114  for (unsigned int i = 0; i < num_indices; ++i)
1115  for (unsigned int j = i + 1; j < num_indices; ++j)
1116  if (internal::find_connection_between_subdomains(mesh,
1117  predicates[i],
1118  predicates[j]))
1119  dsp.add(i, j);
1120 
1121  dsp.symmetrize();
1122 
1123  // Copy dynamic sparsity pattern to sparsity pattern needed by
1124  // coloring function
1125  SparsityPattern sp_graph;
1126  sp_graph.copy_from(dsp);
1127  predicate_colors.resize(num_indices);
1128 
1129  // Assign each predicate with a color and return number of colors
1130  return SparsityTools::color_sparsity_pattern(sp_graph, predicate_colors);
1131  }
1132 
1133 
1134 
1135  template <int dim, int spacedim>
1136  void
1137  set_cellwise_color_set_and_fe_index(
1138  hp::DoFHandler<dim, spacedim> & dof_handler,
1139  const std::vector<predicate_function<dim, spacedim>> &predicates,
1140  const std::vector<unsigned int> & predicate_colors,
1141  std::map<unsigned int, std::map<unsigned int, unsigned int>>
1142  & cellwise_color_predicate_map,
1143  std::vector<std::set<unsigned int>> &fe_sets)
1144  {
1145  // clear output variables first
1146  fe_sets.clear();
1147  cellwise_color_predicate_map.clear();
1148 
1149  /*
1150  * Add first element of fe_sets which is empty by default. This means that
1151  * the default, FE index = 0 is associated with an empty set, since no
1152  * predicate is active in these regions.
1153  */
1154  fe_sets.resize(1);
1155 
1156  /*
1157  * Loop through cells and find set of predicate colors associated
1158  * with the cell. As an example, a cell with an FE index associated
1159  * with colors {a,b} means that predicates active in the cell have
1160  * colors a or b.
1161  *
1162  * Create new active FE index in case of the color
1163  * set is not already listed in fe_sets. If the set already exists,
1164  * find index of the set in fe_sets. In either case, use the id in
1165  * fe_sets to modify cell->active_fe_index.
1166  *
1167  * Associate each cell_id with a set of pairs. The pair represents
1168  * predicate color and the active predicate with that color.
1169  * Each color can only correspond to a single predicate since
1170  * predicates with the same color correspond to disjoint domains.
1171  * This is what the graph coloring in color_predicates
1172  * function ensures. The number of pairs is equal to the number
1173  * of predicates active in the given cell.
1174  */
1175  unsigned int map_index = 0;
1176  for (const auto &cell : dof_handler.active_cell_iterators())
1177  {
1178  // set default FE index ==> no enrichment and no active predicates
1179  cell->set_active_fe_index(0);
1180 
1181  // Give each cell a unique id, which the cellwise_color_predicate_map
1182  // can later use to associate a colors of active predicates with
1183  // the actual predicate id.
1184  //
1185  // When the grid is refined, material id is inherited to
1186  // children cells. So, the cellwise_color_predicate_map stays
1187  // relevant.
1188  cell->set_material_id(map_index);
1189  std::set<unsigned int> color_list;
1190 
1191  // loop through active predicates for the cell and insert map.
1192  // Eg: if the cell with material id 100 has active
1193  // predicates 4 (color = 1) and 5 (color = 2), the map will insert
1194  // pairs (1, 4) and (2, 5) at key 100 (i.e unique id of cell is
1195  // mapped with a map which associates color with predicate id)
1196  // Note that color list for the cell would be {1,2}.
1197  std::map<unsigned int, unsigned int> &cell_map =
1198  cellwise_color_predicate_map[map_index];
1199  for (unsigned int i = 0; i < predicates.size(); ++i)
1200  {
1201  if (predicates[i](cell))
1202  {
1203  /*
1204  * create a pair predicate color and predicate id and add it
1205  * to a map associated with each enriched cell
1206  */
1207  auto ret = cell_map.insert(
1208  std::pair<unsigned int, unsigned int>(predicate_colors[i],
1209  i));
1210 
1211  AssertThrow(ret.second == 1,
1212  ExcMessage(
1213  "Only one enrichment function per color"));
1214 
1215  color_list.insert(predicate_colors[i]);
1216  }
1217  }
1218 
1219 
1220  /*
1221  * check if color combination is already added.
1222  * If already added, set the active FE index based on
1223  * its index in the fe_sets. If the combination doesn't
1224  * exist, add the set to fe_sets and once again set the
1225  * active FE index as last index in fe_sets.
1226  *
1227  * Eg: if cell has color list {1,2} associated and
1228  * fe_sets = { {}, {1}, {2} } for now, we need to add a new set {1,2}
1229  * to fe_sets and a new active FE index 3 because 0 to 2 FE indices
1230  * are already taken by existing sets in fe_sets.
1231  */
1232  if (!color_list.empty())
1233  {
1234  const auto it =
1235  std::find(fe_sets.begin(), fe_sets.end(), color_list);
1236  // when entry is not found
1237  if (it == fe_sets.end())
1238  {
1239  fe_sets.push_back(color_list);
1240  cell->set_active_fe_index(fe_sets.size() - 1);
1241  }
1242  // when entry is found
1243  else
1244  {
1245  cell->set_active_fe_index(std::distance(fe_sets.begin(), it));
1246  }
1247  }
1248  /*
1249  * map_index is used to identify cells and should be unique. The
1250  * index is stored in the material_id of the cell and hence
1251  * stays relevant even when the cells are refined (which is
1252  * why cell_id is not used).
1253  * Two distant cells can have the same set of colors but different
1254  * enrichment functions can be associated with any given
1255  * color. So, in order to figure which enrichment function
1256  * belongs to a color, we use a map that uses this index.
1257  */
1258  ++map_index;
1259  }
1260 
1261 
1262  /*
1263  * Treat interface between enriched cells specially,
1264  * until #1496 (https://github.com/dealii/dealii/issues/1496) is resolved.
1265  * Each time we build constraints at the interface between two different
1266  * FE_Enriched, we look for the least dominating FE of their common
1267  * subspace via hp::FECollection::find_dominating_fe_extended().
1268  * If we don't take further actions, we may find a dominating FE that is
1269  * too restrictive, i.e. enriched FE consisting of only FE_Nothing. New
1270  * elements needs to be added to FECollection object to help find the
1271  * correct enriched FE underlying the spaces in the adjacent cells. This
1272  * is done by creating an appropriate set in fe_sets and a call to the
1273  * function make_fe_collection_from_colored_enrichments at a later stage.
1274  *
1275  * Consider a domain with three predicates and hence with three different
1276  * enrichment functions. Let the enriched finite element of a cell with
1277  * enrichment functions 1 and 2 be represented by [1 1 0], with the last
1278  * entry as zero since the 3rd enrichment function is not relevant for
1279  * the cell. If the interface has enriched FE [1 0 1] and [0 1 1]
1280  * on adjacent cells, an enriched FE [0 0 1] should exist and is
1281  * found as the least dominating finite element for the two cells by
1282  * DoFTools::make_hanging_node_constraints, using the above mentioned
1283  * hp::FECollection functions. Denoting the fe set in adjacent cells as
1284  * {1,3} and {2,3}, this implies that an fe set {3} needs to be added!
1285  * Based on the predicate configuration, this may not be automatically
1286  * done without the following special treatment.
1287  */
1288 
1289  // loop through faces
1290  for (const auto &cell : dof_handler.active_cell_iterators())
1291  {
1292  const unsigned int fe_index = cell->active_fe_index();
1293  const std::set<unsigned int> fe_set = fe_sets.at(fe_index);
1294  for (const unsigned int face : GeometryInfo<dim>::face_indices())
1295  {
1296  // cell shouldn't be at the boundary and
1297  // neighboring cell is not already visited (to avoid visiting
1298  // same face twice). Note that the cells' material ids are
1299  // labeled according to their order in dof_handler previously.
1300  if (!cell->at_boundary(face) &&
1301  cell->material_id() < cell->neighbor(face)->material_id())
1302  {
1303  const auto nbr_fe_index =
1304  cell->neighbor(face)->active_fe_index();
1305 
1306  // find corresponding fe set
1307  const auto nbr_fe_set = fe_sets.at(nbr_fe_index);
1308 
1309  // find intersection of the fe sets: fe_set and nbr_fe_set
1310  std::set<unsigned int> intersection_set;
1311  std::set_intersection(
1312  fe_set.begin(),
1313  fe_set.end(),
1314  nbr_fe_set.begin(),
1315  nbr_fe_set.end(),
1316  std::inserter(intersection_set, intersection_set.begin()));
1317 
1318  // add only the new sets
1319  if (!intersection_set.empty())
1320  {
1321  const auto it = std::find(fe_sets.begin(),
1322  fe_sets.end(),
1323  intersection_set);
1324  // add the set if it is not found
1325  if (it == fe_sets.end())
1326  {
1327  fe_sets.push_back(intersection_set);
1328  }
1329  }
1330  }
1331  }
1332  }
1333  }
1334 
1335 
1336 
1337  template <int dim, int spacedim>
1338  void
1339  make_colorwise_enrichment_functions(
1340  const unsigned int n_colors,
1341  const std::vector<std::shared_ptr<Function<spacedim>>> &enrichments,
1342  const std::map<unsigned int, std::map<unsigned int, unsigned int>>
1343  &cellwise_color_predicate_map,
1344  std::vector<std::function<const Function<spacedim> *(
1346  &color_enrichments)
1347  {
1348  color_enrichments.clear();
1349 
1350  // Each color should be associated with a single enrichment function
1351  // called color enrichment function which calls the correct enrichment
1352  // function for a given cell.
1353  //
1354  // Assume that a cell has a active predicates with ids 4 (color = 1) and
1355  // 5 (color = 2). cellwise_color_predicate_map has this information,
1356  // provided we know the material id.
1357  //
1358  // The constructed color_enrichments is such that
1359  // color_enrichments[1](cell) will return return a pointer to
1360  // function with id=4, i.e. enrichments[4].
1361  // In other words, using the previously collected information in
1362  // this function we translate a vector of user provided enrichment
1363  // functions into a vector of functions suitable for FE_Enriched class.
1364  color_enrichments.resize(n_colors);
1365  for (unsigned int i = 0; i < n_colors; ++i)
1366  {
1367  color_enrichments[i] =
1368  [&, i](const typename Triangulation<dim, spacedim>::cell_iterator
1369  &cell) {
1370  const unsigned int id = cell->material_id();
1371 
1372  /*
1373  * i'th color_enrichment function corresponds to (i+1)'th color.
1374  * Since FE_Enriched takes function pointers, we return a
1375  * function pointer.
1376  */
1377  return enrichments[cellwise_color_predicate_map.at(id).at(i + 1)]
1378  .get();
1379  };
1380  }
1381  }
1382 
1383 
1384 
1385  template <int dim, int spacedim>
1386  void
1387  make_fe_collection_from_colored_enrichments(
1388  const unsigned int n_colors,
1389  const std::vector<std::set<unsigned int>> &fe_sets,
1390  const std::vector<std::function<const Function<spacedim> *(
1392  & color_enrichments,
1393  const FiniteElement<dim, spacedim> &fe_base,
1394  const FiniteElement<dim, spacedim> &fe_enriched,
1395  const FE_Nothing<dim, spacedim> & fe_nothing,
1396  hp::FECollection<dim, spacedim> & fe_collection)
1397  {
1398  // define dummy function which is associated with FE_Nothing
1399  const std::function<const Function<spacedim> *(
1401  dummy_function =
1402  [=](const typename Triangulation<dim, spacedim>::cell_iterator &)
1403  -> const Function<spacedim> * {
1404  AssertThrow(false,
1405  ExcMessage("Called enrichment function for FE_Nothing"));
1406  return nullptr;
1407  };
1408 
1409 
1410  // loop through color sets and create FE_enriched element for each
1411  // of them provided before calling this function, we have color
1412  // enrichment function associated with each color.
1413  for (const auto &fe_set : fe_sets)
1414  {
1415  std::vector<const FiniteElement<dim, spacedim> *> vec_fe_enriched(
1416  n_colors, &fe_nothing);
1417  std::vector<std::vector<std::function<const Function<spacedim> *(
1418  const typename Triangulation<dim, spacedim>::cell_iterator &)>>>
1419  functions(n_colors, {dummy_function});
1420 
1421  for (const unsigned int color_id : fe_set)
1422  {
1423  // Given a color id, corresponding color enrichment
1424  // function is at index id-1 because color_enrichments are
1425  // indexed from zero and colors are indexed from 1.
1426  const unsigned int ind = color_id - 1;
1427 
1428  AssertIndexRange(ind, vec_fe_enriched.size());
1429  AssertIndexRange(ind, functions.size());
1430  AssertIndexRange(ind, color_enrichments.size());
1431 
1432  // Assume an active predicate colors {1,2} for a cell.
1433  // We then need to create a vector of FE enriched elements
1434  // with vec_fe_enriched[0] = vec_fe_enriched[1] = &fe_enriched
1435  // which can later be associated with enrichment functions.
1436  vec_fe_enriched[ind] = &fe_enriched;
1437 
1438  // color_set_id'th color function is (color_set_id-1)
1439  // element of color wise enrichments
1440  functions[ind][0] = color_enrichments[ind];
1441  }
1442 
1443  AssertDimension(vec_fe_enriched.size(), functions.size());
1444 
1445  FE_Enriched<dim, spacedim> fe_component(&fe_base,
1446  vec_fe_enriched,
1447  functions);
1448  fe_collection.push_back(fe_component);
1449  }
1450  }
1451  } // namespace internal
1452 
1453 
1454 
1455  template <int dim, int spacedim>
1457  const FiniteElement<dim, spacedim> & fe_base,
1458  const FiniteElement<dim, spacedim> & fe_enriched,
1459  const std::vector<predicate_function<dim, spacedim>> & predicates,
1460  const std::vector<std::shared_ptr<Function<spacedim>>> &enrichments)
1461  : fe_base(fe_base)
1462  , fe_enriched(fe_enriched)
1463  , fe_nothing(fe_base.n_components(), true)
1464  , predicates(predicates)
1466  , n_colors(numbers::invalid_unsigned_int)
1467  {
1468  AssertDimension(predicates.size(), enrichments.size());
1469  AssertDimension(fe_base.n_components(), fe_enriched.n_components());
1470  AssertThrow(predicates.size() > 0,
1471  ExcMessage("Number of predicates should be positive"));
1472  }
1473 
1474 
1475 
1476  template <int dim, int spacedim>
1479  hp::DoFHandler<dim, spacedim> &dof_handler)
1480  {
1481  // color the predicates based on connections between corresponding
1482  // subdomains
1483  n_colors =
1484  internal::color_predicates(dof_handler, predicates, predicate_colors);
1485 
1486  // create color maps and color list for each cell
1487  internal::set_cellwise_color_set_and_fe_index(dof_handler,
1488  predicates,
1491  fe_sets);
1492  // setup color wise enrichment functions
1493  // i'th function corresponds to (i+1) color!
1494  internal::make_colorwise_enrichment_functions<dim, spacedim>(
1496 
1497  // make FE_Collection
1498  internal::make_fe_collection_from_colored_enrichments(n_colors,
1499  fe_sets,
1501  fe_base,
1502  fe_enriched,
1503  fe_nothing,
1504  fe_collection);
1505 
1506  return fe_collection;
1507  }
1508 } // namespace ColorEnriched
1509 
1510 
1511 // explicit instantiations
1512 #include "fe_enriched.inst"
1513 
1514 DEAL_II_NAMESPACE_CLOSE
virtual FiniteElementDomination::Domination compare_for_domination(const FiniteElement< dim, spacedim > &fe_other, const unsigned int codim=0) const override final
Definition: fe_enriched.cc:981
Shape function values.
bool is_dominating() const
Definition: fe_nothing.cc:178
virtual const FiniteElement< dim, spacedim > & base_element(const unsigned int index) const override
Definition: fe_enriched.cc:507
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1579
const std::vector< std::vector< std::function< const Function< spacedim > *(const typename Triangulation< dim, spacedim >::cell_iterator &)> > > get_enrichments() const
Definition: fe_enriched.cc:263
virtual std::vector< std::pair< unsigned int, unsigned int > > hp_line_dof_identities(const FiniteElement< dim, spacedim > &fe_other) const override
Definition: fe_enriched.cc:945
virtual void get_face_interpolation_matrix(const FiniteElement< dim, spacedim > &source, FullMatrix< double > &matrix) const override
Definition: fe_enriched.cc:881
FullMatrix< double > interface_constraints
Definition: fe.h:2468
virtual const FullMatrix< double > & get_restriction_matrix(const unsigned int child, const RefinementCase< dim > &refinement_case=RefinementCase< dim >::isotropic_refinement) const override
InternalData(std::unique_ptr< typename FESystem< dim, spacedim >::InternalData > fesystem_data)
unsigned int n_nonzero_components(const unsigned int i) const
Definition: fe.h:3291
constexpr SymmetricTensor< 2, dim, Number > symmetrize(const Tensor< 2, dim, Number > &t)
FE_Enriched(const FiniteElement< dim, spacedim > &fe_base, const FiniteElement< dim, spacedim > &fe_enriched, const Function< spacedim > *enrichment_function)
Definition: fe_enriched.cc:146
void initialize(const std::vector< const FiniteElement< dim, spacedim > *> &fes, const std::vector< unsigned int > &multiplicities)
Definition: fe_enriched.cc:425
const FiniteElement< dim, spacedim > & fe_enriched
Definition: fe_enriched.h:1144
void add(const size_type i, const size_type j)
internal::FEValuesImplementation::FiniteElementRelatedData< dim, spacedim > & get_fe_output_object(const unsigned int base_no) const
#define AssertIndexRange(index, range)
Definition: exceptions.h:1649
const hp::FECollection< dim, spacedim > & build_fe_collection(hp::DoFHandler< dim, spacedim > &dof_handler)
std::vector< std::pair< std::pair< unsigned int, unsigned int >, unsigned int > > component_to_base_table
Definition: fe.h:2602
std::vector< std::vector< EnrichmentValues > > enrichment
Definition: fe_enriched.h:549
unsigned int n_colors
Definition: fe_enriched.h:1200
std::function< bool(const typename Triangulation< dim, spacedim >::cell_iterator &)> predicate_function
Definition: fe_enriched.h:736
std::vector< Point< dim - 1 > > unit_face_support_points
Definition: fe.h:2487
STL namespace.
Transformed quadrature points.
#define AssertThrow(cond, exc)
Definition: exceptions.h:1531
hp::FECollection< dim, spacedim > fe_collection
Definition: fe_enriched.h:1132
static ::ExceptionBase & ExcInterpolationNotImplemented()
void multiply_by_enrichment(const Quadrature< dim_1 > &quadrature, const InternalData &fe_data, const internal::FEValuesImplementation::MappingRelatedData< dim, spacedim > &mapping_data, const typename Triangulation< dim, spacedim >::cell_iterator &cell, internal::FEValuesImplementation::FiniteElementRelatedData< dim, spacedim > &output_data) const
Definition: fe_enriched.cc:623
const bool is_enriched
Definition: fe_enriched.h:581
std::unique_ptr< To > dynamic_unique_cast(std::unique_ptr< From > &&p)
Definition: utilities.h:779
std::vector< Point< dim > > unit_support_points
Definition: fe.h:2480
virtual std::vector< std::pair< unsigned int, unsigned int > > hp_vertex_dof_identities(const FiniteElement< dim, spacedim > &fe_other) const override
Definition: fe_enriched.cc:927
virtual std::string get_name() const override
Definition: fe_enriched.cc:486
constexpr SymmetricTensor< 4, dim, Number > outer_product(const SymmetricTensor< 2, dim, Number > &t1, const SymmetricTensor< 2, dim, Number > &t2)
const std::vector< std::vector< std::function< const Function< spacedim > *(const typename Triangulation< dim, spacedim >::cell_iterator &)> > > enrichments
Definition: fe_enriched.h:570
const FESystem< dim, spacedim > & get_fe_system() const
Definition: fe_enriched.cc:865
static ::ExceptionBase & ExcMessage(std::string arg1)
static ::ExceptionBase & ExcImpossibleInDim(int arg1)
void push_back(const FiniteElement< dim, spacedim > &new_fe)
void build_cell_tables(std::vector< std::pair< std::pair< unsigned int, unsigned int >, unsigned int >> &system_to_base_table, std::vector< std::pair< unsigned int, unsigned int >> &system_to_component_table, std::vector< std::pair< std::pair< unsigned int, unsigned int >, unsigned int >> &component_to_base_table, const FiniteElement< dim, spacedim > &finite_element, const bool do_tensor_product=true)
std::unique_ptr< typename FESystem< dim, spacedim >::InternalData > fesystem_data
Definition: fe_enriched.h:532
Third derivatives of shape functions.
const std::unique_ptr< const FESystem< dim, spacedim > > fe_system
Definition: fe_enriched.h:694
#define Assert(cond, exc)
Definition: exceptions.h:1419
unsigned int element_multiplicity(const unsigned int index) const
Definition: fe.h:3133
UpdateFlags
IteratorRange< active_cell_iterator > active_cell_iterators() const
FiniteElement< dim, spacedim >::InternalDataBase & get_fe_data(const unsigned int base_no) const
void reinit(const size_type m, const size_type n, const IndexSet &rowset=IndexSet())
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
Abstract base class for mapping classes.
Definition: mapping.h:302
void reinit(const unsigned int n_blocks, const size_type n_elements_per_block)
Definition: fe.h:44
std::vector< std::set< unsigned int > > fe_sets
Definition: fe_enriched.h:1214
std::vector< unsigned int > predicate_colors
Definition: fe_enriched.h:1195
void copy_from(const size_type n_rows, const size_type n_cols, const ForwardIterator begin, const ForwardIterator end)
Second derivatives of shape functions.
virtual std::unique_ptr< typename FiniteElement< dim, spacedim >::InternalDataBase > get_subface_data(const UpdateFlags update_flags, const Mapping< dim, spacedim > &mapping, const Quadrature< dim - 1 > &quadrature, ::internal::FEValuesImplementation::FiniteElementRelatedData< dim, spacedim > &output_data) const override
Definition: fe_enriched.cc:387
virtual double shape_value(const unsigned int i, const Point< dim > &p) const override
Definition: fe_enriched.cc:271
std::string dim_string(const int dim, const int spacedim)
Definition: utilities.cc:557
virtual UpdateFlags requires_update_flags(const UpdateFlags update_flags) const override
Definition: fe_enriched.cc:302
unsigned int size() const
const unsigned int dofs_per_cell
Definition: fe_base.h:282
const std::vector< predicate_function< dim, spacedim > > predicates
Definition: fe_enriched.h:1158
std::vector< Point< spacedim > > quadrature_points
virtual const FullMatrix< double > & get_prolongation_matrix(const unsigned int child, const RefinementCase< dim > &refinement_case=RefinementCase< dim >::isotropic_refinement) const override
unsigned int color_sparsity_pattern(const SparsityPattern &sparsity_pattern, std::vector< unsigned int > &color_indices)
unsigned int n_components() const
virtual void get_subface_interpolation_matrix(const FiniteElement< dim, spacedim > &source, const unsigned int subface, FullMatrix< double > &matrix) const override
Definition: fe_enriched.cc:903
virtual std::unique_ptr< typename FiniteElement< dim, spacedim >::InternalDataBase > get_data(const UpdateFlags flags, const Mapping< dim, spacedim > &mapping, const Quadrature< dim > &quadrature, ::internal::FEValuesImplementation::FiniteElementRelatedData< dim, spacedim > &output_data) const override
Definition: fe_enriched.cc:407
Shape function gradients.
std::vector< std::vector< std::vector< unsigned int > > > base_no_mult_local_enriched_dofs
Definition: fe_enriched.h:559
std::vector< std::pair< unsigned int, unsigned int > > system_to_component_table
Definition: fe.h:2536
const Triangulation< dim, spacedim > & get_triangulation() const
void push_back(const size_type size)
void build_face_tables(std::vector< std::pair< std::pair< unsigned int, unsigned int >, unsigned int >> &face_system_to_base_table, std::vector< std::pair< unsigned int, unsigned int >> &face_system_to_component_table, const FiniteElement< dim, spacedim > &finite_element, const bool do_tensor_product=true)
const unsigned int dofs_per_face
Definition: fe_base.h:275
std::vector< std::pair< std::pair< unsigned int, unsigned int >, unsigned int > > system_to_base_table
Definition: fe.h:2567
static ::ExceptionBase & ExcNotImplemented()
std::vector< std::pair< unsigned int, unsigned int > > face_system_to_component_table
Definition: fe.h:2548
std::vector< cell_iterator_function > color_enrichments
Definition: fe_enriched.h:1188
std::unique_ptr< typename FiniteElement< dim, spacedim >::InternalDataBase > setup_data(std::unique_ptr< typename FESystem< dim, spacedim >::InternalData > fes_data, const UpdateFlags flags, const Quadrature< dim_1 > &quadrature) const
Definition: fe_enriched.cc:326
virtual std::unique_ptr< FiniteElement< dim, spacedim > > clone() const override
Definition: fe_enriched.cc:284
BlockIndices base_to_block_indices
Definition: fe.h:2579
std::map< unsigned int, std::map< unsigned int, unsigned int > > cellwise_color_predicate_map
Definition: fe_enriched.h:1208
std::vector< std::pair< std::pair< unsigned int, unsigned int >, unsigned int > > face_system_to_base_table
Definition: fe.h:2573
virtual std::vector< std::pair< unsigned int, unsigned int > > hp_quad_dof_identities(const FiniteElement< dim, spacedim > &fe_other) const override
Definition: fe_enriched.cc:963
const std::vector< std::shared_ptr< Function< spacedim > > > enrichments
Definition: fe_enriched.h:1165
unsigned int n_base_elements() const
Definition: fe.h:3124
unsigned int n_components(const DoFHandler< dim, spacedim > &dh)
std::vector< int > adjust_line_dof_index_for_line_orientation_table
Definition: fe.h:2531
UpdateFlags update_each
Definition: fe.h:715
virtual bool hp_constraints_are_implemented() const override
Definition: fe_enriched.cc:873
virtual std::unique_ptr< typename FiniteElement< dim, spacedim >::InternalDataBase > get_face_data(const UpdateFlags update_flags, const Mapping< dim, spacedim > &mapping, const Quadrature< dim - 1 > &quadrature, ::internal::FEValuesImplementation::FiniteElementRelatedData< dim, spacedim > &output_data) const override
Definition: fe_enriched.cc:368
static ::ExceptionBase & ExcInternalError()