Reference documentation for deal.II version Git 7026f387cc 2019-10-15 14:19:01 -0400
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
scratch_data.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 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 #ifndef dealii_meshworker_scratch_data_h
17 #define dealii_meshworker_scratch_data_h
18 
19 #include <deal.II/base/config.h>
20 
21 #include <deal.II/algorithms/general_data_storage.h>
22 
23 #include <deal.II/differentiation/ad.h>
24 
25 #include <deal.II/fe/fe_values.h>
26 #include <deal.II/fe/mapping_q1.h>
27 
28 #include <boost/any.hpp>
29 
30 #include <algorithm>
31 #include <map>
32 #include <vector>
33 
34 DEAL_II_NAMESPACE_OPEN
35 
36 namespace MeshWorker
37 {
38  namespace internal
39  {
44  template <int dim, int spacedim, typename NumberType, typename Extractor>
45  using OutputType = typename FEValuesViews::View<dim, spacedim, Extractor>::
46  template OutputType<NumberType>;
47  } // namespace internal
48 
231  template <int dim, int spacedim = dim>
233  {
234  public:
254  ScratchData(
255  const Mapping<dim, spacedim> & mapping,
257  const Quadrature<dim> & quadrature,
258  const UpdateFlags & update_flags,
259  const Quadrature<dim - 1> &face_quadrature = Quadrature<dim - 1>(),
260  const UpdateFlags & face_update_flags = update_default);
261 
278  ScratchData(
279  const Mapping<dim, spacedim> & mapping,
281  const Quadrature<dim> & quadrature,
282  const UpdateFlags & update_flags,
283  const UpdateFlags & neighbor_update_flags,
284  const Quadrature<dim - 1> &face_quadrature = Quadrature<dim - 1>(),
285  const UpdateFlags & face_update_flags = update_default,
286  const UpdateFlags & neighbor_face_update_flags = update_default);
287 
300  ScratchData(
302  const Quadrature<dim> & quadrature,
303  const UpdateFlags & update_flags,
304  const Quadrature<dim - 1> &face_quadrature = Quadrature<dim - 1>(),
305  const UpdateFlags & face_update_flags = update_default);
306 
321  ScratchData(
323  const Quadrature<dim> & quadrature,
324  const UpdateFlags & update_flags,
325  const UpdateFlags & neighbor_update_flags,
326  const Quadrature<dim - 1> &face_quadrature = Quadrature<dim - 1>(),
327  const UpdateFlags & face_update_flags = update_default,
328  const UpdateFlags & neighbor_face_update_flags = update_default);
329 
333  ScratchData(const ScratchData<dim, spacedim> &scratch);
334  // CurrentCellMethods
339 
348  reinit(
350 
359  reinit(const typename DoFHandler<dim, spacedim>::active_cell_iterator &cell,
360  const unsigned int face_no);
361 
373  reinit(const typename DoFHandler<dim, spacedim>::active_cell_iterator &cell,
374  const unsigned int face_no,
375  const unsigned int subface_no);
376 
388  get_current_fe_values() const;
389 
393  const std::vector<Point<spacedim>> &
394  get_quadrature_points() const;
395 
399  const std::vector<double> &
400  get_JxW_values() const;
401 
405  const std::vector<Tensor<1, spacedim>> &
406  get_normal_vectors() const;
407 
412  const std::vector<types::global_dof_index> &
413  get_local_dof_indices() const;
414  // CurrentCellMethods
416  // NeighborCellMethods
421 
430  reinit_neighbor(
432 
441  reinit_neighbor(
443  const unsigned int face_no);
444 
456  reinit_neighbor(
458  const unsigned int face_no,
459  const unsigned int subface_no);
460 
473  get_current_neighbor_fe_values() const;
474 
478  const std::vector<double> &
479  get_neighbor_JxW_values() const;
480 
484  const std::vector<Tensor<1, spacedim>> &
485  get_neighbor_normal_vectors();
486 
491  const std::vector<types::global_dof_index> &
492  get_neighbor_dof_indices() const;
493  // NeighborCellMethods
495 
502  get_general_data_storage();
503  // CurrentCellEvaluation
508 
538  template <typename VectorType, typename Number = double>
539  void
540  extract_local_dof_values(const std::string &global_vector_name,
541  const VectorType & input_vector,
542  const Number dummy = Number(0));
543 
552  template <typename Number = double>
553  const std::vector<Number> &
554  get_local_dof_values(const std::string &global_vector_name,
555  Number dummy = Number(0)) const;
556 
578  template <typename Extractor, typename Number = double>
579  const std::vector<
580  typename internal::OutputType<dim, spacedim, Number, Extractor>::
581  value_type> &
582  get_values(const std::string &global_vector_name,
583  const Extractor & variable,
584  const Number dummy = Number(0));
585 
607  template <typename Extractor, typename Number = double>
608  const std::vector<
609  typename internal::OutputType<dim, spacedim, Number, Extractor>::
610  gradient_type> &
611  get_gradients(const std::string &global_vector_name,
612  const Extractor & variable,
613  const Number dummy = Number(0));
614 
637  template <typename Extractor, typename Number = double>
638  const std::vector<
639  typename internal::OutputType<dim, spacedim, Number, Extractor>::
640  symmetric_gradient_type> &
641  get_symmetric_gradients(const std::string &global_vector_name,
642  const Extractor & variable,
643  const Number dummy = Number(0));
644 
666  template <typename Extractor, typename Number = double>
667  const std::vector<
668  typename internal::OutputType<dim, spacedim, Number, Extractor>::
669  divergence_type> &
670  get_divergences(const std::string &global_vector_name,
671  const Extractor & variable,
672  const Number dummy = Number(0));
673 
695  template <typename Extractor, typename Number = double>
696  const std::vector<typename internal::
697  OutputType<dim, spacedim, Number, Extractor>::curl_type>
698  &
699  get_curls(const std::string &global_vector_name,
700  const Extractor & variable,
701  const Number dummy = Number(0));
702 
724  template <typename Extractor, typename Number = double>
725  const std::vector<
726  typename internal::OutputType<dim, spacedim, Number, Extractor>::
727  hessian_type> &
728  get_hessians(const std::string &global_vector_name,
729  const Extractor & variable,
730  const Number dummy = Number(0));
731 
753  template <typename Extractor, typename Number = double>
754  const std::vector<
755  typename internal::OutputType<dim, spacedim, Number, Extractor>::
756  third_derivative_type> &
757  get_third_derivatives(const std::string &global_vector_name,
758  const Extractor & variable,
759  const Number dummy = Number(0));
760  // CurrentCellEvaluation
762 
766  const Mapping<dim, spacedim> &
767  get_mapping() const;
768 
769  private:
774  template <typename Extractor, typename Number = double>
775  std::string
776  get_unique_name(const std::string &global_vector_name,
777  const Extractor & variable,
778  const std::string &object_type,
779  const unsigned int size,
780  const Number & exemplar_number) const;
781 
785  template <typename Number = double>
786  std::string
787  get_unique_dofs_name(const std::string &global_vector_name,
788  const unsigned int size,
789  const Number & exemplar_number) const;
790 
796 
802 
808 
814 
819 
824 
830 
836 
840  std::unique_ptr<FEValues<dim, spacedim>> fe_values;
841 
845  std::unique_ptr<FEFaceValues<dim, spacedim>> fe_face_values;
846 
850  std::unique_ptr<FESubfaceValues<dim, spacedim>> fe_subface_values;
851 
855  std::unique_ptr<FEValues<dim, spacedim>> neighbor_fe_values;
856 
860  std::unique_ptr<FEFaceValues<dim, spacedim>> neighbor_fe_face_values;
861 
865  std::unique_ptr<FESubfaceValues<dim, spacedim>> neighbor_fe_subface_values;
866 
870  std::vector<types::global_dof_index> local_dof_indices;
871 
875  std::vector<types::global_dof_index> neighbor_dof_indices;
876 
881 
886 
892 
898  };
899 
900 #ifndef DOXYGEN
901  template <int dim, int spacedim>
902  template <typename Extractor, typename Number>
903  std::string
905  const std::string &global_vector_name,
906  const Extractor & variable,
907  const std::string &object_type,
908  const unsigned int size,
909  const Number & exemplar_number) const
910  {
911  return global_vector_name + "_" + variable.get_name() + "_" + object_type +
912  "_" + Utilities::int_to_string(size) + "_" +
913  Utilities::type_to_string(exemplar_number);
914  }
915 
916 
917 
918  template <int dim, int spacedim>
919  template <typename Number>
920  std::string
922  const std::string &global_vector_name,
923  const unsigned int size,
924  const Number & exemplar_number) const
925  {
926  return global_vector_name + "_independent_local_dofs_" +
927  Utilities::int_to_string(size) + "_" +
928  Utilities::type_to_string(exemplar_number);
929  }
930 
931 
932 
933  template <int dim, int spacedim>
934  template <typename VectorType, typename Number>
935  void
937  const std::string &global_vector_name,
938  const VectorType & input_vector,
939  const Number dummy)
940  {
941  const unsigned int n_dofs = get_current_fe_values().get_fe().dofs_per_cell;
942 
943  const std::string name =
944  get_unique_dofs_name(global_vector_name, n_dofs, dummy);
945 
946  auto &independent_local_dofs =
947  internal_data_storage
948  .template get_or_add_object_with_name<std::vector<Number>>(name,
949  n_dofs);
950 
951  AssertDimension(independent_local_dofs.size(), n_dofs);
952 
954  for (unsigned int i = 0; i < n_dofs; ++i)
956  input_vector(local_dof_indices[i]),
957  i,
958  n_dofs,
959  independent_local_dofs[i]);
960  else
961  for (unsigned int i = 0; i < n_dofs; ++i)
962  independent_local_dofs[i] = input_vector(local_dof_indices[i]);
963  }
964 
965 
966 
967  template <int dim, int spacedim>
968  template <typename Number>
969  const std::vector<Number> &
971  const std::string &global_vector_name,
972  Number dummy) const
973  {
974  const unsigned int n_dofs = get_current_fe_values().get_fe().dofs_per_cell;
975 
976  const std::string dofs_name =
977  get_unique_dofs_name(global_vector_name, n_dofs, dummy);
978 
979  Assert(
980  internal_data_storage.stores_object_with_name(dofs_name),
981  ExcMessage(
982  "You did not call yet extract_local_dof_values with the right types!"));
983 
984  return internal_data_storage
985  .template get_object_with_name<std::vector<Number>>(dofs_name);
986  }
987 
988 
989 
990  template <int dim, int spacedim>
991  template <typename Extractor, typename Number>
992  const std::vector<
993  typename internal::OutputType<dim, spacedim, Number, Extractor>::value_type>
994  &
996  const std::string &global_vector_name,
997  const Extractor & variable,
998  const Number dummy)
999  {
1000  const std::vector<Number> &independent_local_dofs =
1001  get_local_dof_values(global_vector_name, dummy);
1002 
1003  const FEValuesBase<dim, spacedim> &fev = get_current_fe_values();
1004 
1005  const unsigned int n_q_points = fev.n_quadrature_points;
1006 
1007  const std::string name = get_unique_name(
1008  global_vector_name, variable, "_values_q", n_q_points, dummy);
1009 
1010  // Now build the return type
1011  using RetType =
1012  std::vector<typename internal::
1013  OutputType<dim, spacedim, Number, Extractor>::value_type>;
1014 
1015  RetType &ret =
1016  internal_data_storage.template get_or_add_object_with_name<RetType>(
1017  name, n_q_points);
1018 
1019  AssertDimension(ret.size(), n_q_points);
1020 
1021  fev[variable].get_function_values_from_local_dof_values(
1022  independent_local_dofs, ret);
1023  return ret;
1024  }
1025 
1026 
1027 
1028  template <int dim, int spacedim>
1029  template <typename Extractor, typename Number>
1030  const std::vector<
1031  typename internal::OutputType<dim, spacedim, Number, Extractor>::
1032  gradient_type> &
1034  const std::string &global_vector_name,
1035  const Extractor & variable,
1036  const Number dummy)
1037  {
1038  const std::vector<Number> &independent_local_dofs =
1039  get_local_dof_values(global_vector_name, dummy);
1040 
1041  const FEValuesBase<dim, spacedim> &fev = get_current_fe_values();
1042 
1043  const unsigned int n_q_points = fev.n_quadrature_points;
1044 
1045  const std::string name = get_unique_name(
1046  global_vector_name, variable, "_gradients_q", n_q_points, dummy);
1047 
1048  // Now build the return type
1049  using RetType = std::vector<
1050  typename internal::OutputType<dim, spacedim, Number, Extractor>::
1051  gradient_type>;
1052 
1053  RetType &ret =
1054  internal_data_storage.template get_or_add_object_with_name<RetType>(
1055  name, n_q_points);
1056 
1057  AssertDimension(ret.size(), n_q_points);
1058 
1059  fev[variable].get_function_gradients_from_local_dof_values(
1060  independent_local_dofs, ret);
1061  return ret;
1062  }
1063 
1064 
1065 
1066  template <int dim, int spacedim>
1067  template <typename Extractor, typename Number>
1068  const std::vector<
1069  typename internal::OutputType<dim, spacedim, Number, Extractor>::
1070  hessian_type> &
1072  const std::string &global_vector_name,
1073  const Extractor & variable,
1074  const Number dummy)
1075  {
1076  const std::vector<Number> &independent_local_dofs =
1077  get_local_dof_values(global_vector_name, dummy);
1078 
1079  const FEValuesBase<dim, spacedim> &fev = get_current_fe_values();
1080 
1081  const unsigned int n_q_points = fev.n_quadrature_points;
1082 
1083  const std::string name = get_unique_name(
1084  global_vector_name, variable, "_hessians_q", n_q_points, dummy);
1085 
1086  // Now build the return type
1087  using RetType =
1088  std::vector<typename internal::
1089  OutputType<dim, spacedim, Number, Extractor>::hessian_type>;
1090 
1091  RetType &ret =
1092  internal_data_storage.template get_or_add_object_with_name<RetType>(
1093  name, n_q_points);
1094 
1095 
1096  AssertDimension(ret.size(), n_q_points);
1097 
1098  fev[variable].get_function_hessians_from_local_dof_values(
1099  independent_local_dofs, ret);
1100  return ret;
1101  }
1102 
1103 
1104 
1105  template <int dim, int spacedim>
1106  template <typename Extractor, typename Number>
1107  const std::vector<
1108  typename internal::OutputType<dim, spacedim, Number, Extractor>::
1109  third_derivative_type> &
1111  const std::string &global_vector_name,
1112  const Extractor & variable,
1113  const Number dummy)
1114  {
1115  const std::vector<Number> &independent_local_dofs =
1116  get_local_dof_values(global_vector_name, dummy);
1117 
1118  const FEValuesBase<dim, spacedim> &fev = get_current_fe_values();
1119 
1120  const unsigned int n_q_points = fev.n_quadrature_points;
1121 
1122  const std::string name = get_unique_name(
1123  global_vector_name, variable, "_third_derivatives_q", n_q_points, dummy);
1124 
1125  // Now build the return type
1126  using RetType = std::vector<
1127  typename internal::OutputType<dim, spacedim, Number, Extractor>::
1128  third_derivative_type>;
1129 
1130  RetType &ret =
1131  internal_data_storage.template get_or_add_object_with_name<RetType>(
1132  name, n_q_points);
1133 
1134 
1135  AssertDimension(ret.size(), n_q_points);
1136 
1137  fev[variable].get_function_third_derivatives_from_local_dof_values(
1138  independent_local_dofs, ret);
1139  return ret;
1140  }
1141 
1142 
1143 
1144  template <int dim, int spacedim>
1145  template <typename Extractor, typename Number>
1146  const std::vector<
1147  typename internal::OutputType<dim, spacedim, Number, Extractor>::
1148  symmetric_gradient_type> &
1150  const std::string &global_vector_name,
1151  const Extractor & variable,
1152  const Number dummy)
1153  {
1154  const std::vector<Number> &independent_local_dofs =
1155  get_local_dof_values(global_vector_name, dummy);
1156 
1157  const FEValuesBase<dim, spacedim> &fev = get_current_fe_values();
1158 
1159  const unsigned int n_q_points = fev.n_quadrature_points;
1160 
1161  const std::string name = get_unique_name(
1162  global_vector_name, variable, "_symmetric_gradient_q", n_q_points, dummy);
1163 
1164 
1165  // Now build the return type
1166  using RetType = std::vector<
1167  typename internal::OutputType<dim, spacedim, Number, Extractor>::
1168  symmetric_gradient_type>;
1169 
1170  RetType &ret =
1171  internal_data_storage.template get_or_add_object_with_name<RetType>(
1172  name, n_q_points);
1173 
1174 
1175  AssertDimension(ret.size(), n_q_points);
1176 
1177  fev[variable].get_function_symmetric_gradients_from_local_dof_values(
1178  independent_local_dofs, ret);
1179  return ret;
1180  }
1181 
1182 
1183  template <int dim, int spacedim>
1184  template <typename Extractor, typename Number>
1185  const std::vector<
1186  typename internal::OutputType<dim, spacedim, Number, Extractor>::
1187  divergence_type> &
1189  const std::string &global_vector_name,
1190  const Extractor & variable,
1191  const Number dummy)
1192  {
1193  const std::vector<Number> &independent_local_dofs =
1194  get_local_dof_values(global_vector_name, dummy);
1195 
1196  const FEValuesBase<dim, spacedim> &fev = get_current_fe_values();
1197 
1198  const unsigned int n_q_points = fev.n_quadrature_points;
1199 
1200  const std::string name = get_unique_name(
1201  global_vector_name, variable, "_divergence_q", n_q_points, dummy);
1202 
1203  // Now build the return type
1204  using RetType = std::vector<
1205  typename internal::OutputType<dim, spacedim, Number, Extractor>::
1206  divergence_type>;
1207 
1208  RetType &ret =
1209  internal_data_storage.template get_or_add_object_with_name<RetType>(
1210  name, n_q_points);
1211 
1212 
1213  AssertDimension(ret.size(), n_q_points);
1214 
1215  fev[variable].get_function_divergences_from_local_dof_values(
1216  independent_local_dofs, ret);
1217  return ret;
1218  }
1219 
1220 
1221 
1222  template <int dim, int spacedim>
1223  template <typename Extractor, typename Number>
1224  const std::vector<
1225  typename internal::OutputType<dim, spacedim, Number, Extractor>::curl_type>
1226  &
1227  ScratchData<dim, spacedim>::get_curls(const std::string &global_vector_name,
1228  const Extractor & variable,
1229  const Number dummy)
1230  {
1231  const std::vector<Number> &independent_local_dofs =
1232  get_local_dof_values(global_vector_name, dummy);
1233 
1234  const FEValuesBase<dim, spacedim> &fev = get_current_fe_values();
1235 
1236  const unsigned int n_q_points = fev.n_quadrature_points;
1237 
1238  const std::string name = get_unique_name(
1239  global_vector_name, variable, "_curl_q", n_q_points, dummy);
1240 
1241  // Now build the return type
1242  using RetType =
1243  std::vector<typename internal::
1244  OutputType<dim, spacedim, Number, Extractor>::curl_type>;
1245 
1246  RetType &ret =
1247  internal_data_storage.template get_or_add_object_with_name<RetType>(
1248  name, n_q_points);
1249 
1250  AssertDimension(ret.size(), n_q_points);
1251 
1252  fev[variable].get_function_curls_from_local_dof_values(
1253  independent_local_dofs, ret);
1254  return ret;
1255  }
1256 
1257 #endif
1258 
1259 } // namespace MeshWorker
1260 
1261 DEAL_II_NAMESPACE_CLOSE
1262 
1263 #endif
GeneralDataStorage internal_data_storage
Definition: scratch_data.h:885
SmartPointer< FEValuesBase< dim, spacedim > > current_fe_values
Definition: scratch_data.h:891
std::string type_to_string(const T &t)
Definition: utilities.h:1054
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1567
std::unique_ptr< FEFaceValues< dim, spacedim > > fe_face_values
Definition: scratch_data.h:845
typename ::internal::FEValuesViews::ViewType< dim, spacedim, Extractor >::type View
Definition: fe_values.h:1979
const std::vector< typename internal::OutputType< dim, spacedim, Number, Extractor >::gradient_type > & get_gradients(const std::string &global_vector_name, const Extractor &variable, const Number dummy=Number(0))
Quadrature< dim > cell_quadrature
Definition: scratch_data.h:807
SmartPointer< const Mapping< dim, spacedim > > mapping
Definition: scratch_data.h:795
std::string get_unique_dofs_name(const std::string &global_vector_name, const unsigned int size, const Number &exemplar_number) const
std::unique_ptr< FEValues< dim, spacedim > > neighbor_fe_values
Definition: scratch_data.h:855
UpdateFlags neighbor_face_update_flags
Definition: scratch_data.h:835
const std::vector< typename internal::OutputType< dim, spacedim, Number, Extractor >::value_type > & get_values(const std::string &global_vector_name, const Extractor &variable, const Number dummy=Number(0))
const std::vector< typename internal::OutputType< dim, spacedim, Number, Extractor >::symmetric_gradient_type > & get_symmetric_gradients(const std::string &global_vector_name, const Extractor &variable, const Number dummy=Number(0))
UpdateFlags neighbor_cell_update_flags
Definition: scratch_data.h:823
std::vector< types::global_dof_index > neighbor_dof_indices
Definition: scratch_data.h:875
const std::vector< typename internal::OutputType< dim, spacedim, Number, Extractor >::hessian_type > & get_hessians(const std::string &global_vector_name, const Extractor &variable, const Number dummy=Number(0))
std::unique_ptr< FESubfaceValues< dim, spacedim > > fe_subface_values
Definition: scratch_data.h:850
std::unique_ptr< FESubfaceValues< dim, spacedim > > neighbor_fe_subface_values
Definition: scratch_data.h:865
static ::ExceptionBase & ExcMessage(std::string arg1)
No update.
#define Assert(cond, exc)
Definition: exceptions.h:1407
UpdateFlags
Abstract base class for mapping classes.
Definition: mapping.h:302
SmartPointer< FEValuesBase< dim, spacedim > > current_neighbor_fe_values
Definition: scratch_data.h:897
std::unique_ptr< FEFaceValues< dim, spacedim > > neighbor_fe_face_values
Definition: scratch_data.h:860
UpdateFlags face_update_flags
Definition: scratch_data.h:829
GeneralDataStorage user_data_storage
Definition: scratch_data.h:880
std::string get_unique_name(const std::string &global_vector_name, const Extractor &variable, const std::string &object_type, const unsigned int size, const Number &exemplar_number) const
const std::vector< typename internal::OutputType< dim, spacedim, Number, Extractor >::third_derivative_type > & get_third_derivatives(const std::string &global_vector_name, const Extractor &variable, const Number dummy=Number(0))
const std::vector< typename internal::OutputType< dim, spacedim, Number, Extractor >::curl_type > & get_curls(const std::string &global_vector_name, const Extractor &variable, const Number dummy=Number(0))
std::string int_to_string(const unsigned int value, const unsigned int digits=numbers::invalid_unsigned_int)
Definition: utilities.cc:383
void extract_local_dof_values(const std::string &global_vector_name, const VectorType &input_vector, const Number dummy=Number(0))
UpdateFlags cell_update_flags
Definition: scratch_data.h:818
const unsigned int n_quadrature_points
Definition: fe_values.h:2102
Quadrature< dim - 1 > face_quadrature
Definition: scratch_data.h:813
std::unique_ptr< FEValues< dim, spacedim > > fe_values
Definition: scratch_data.h:840
const std::vector< typename internal::OutputType< dim, spacedim, Number, Extractor >::divergence_type > & get_divergences(const std::string &global_vector_name, const Extractor &variable, const Number dummy=Number(0))
Definition: fe.h:38
typename ActiveSelector::active_cell_iterator active_cell_iterator
Definition: dof_handler.h:328
std::vector< types::global_dof_index > local_dof_indices
Definition: scratch_data.h:870
SmartPointer< const FiniteElement< dim, spacedim > > fe
Definition: scratch_data.h:801
const std::vector< Number > & get_local_dof_values(const std::string &global_vector_name, Number dummy=Number(0)) const