]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Make the elements of FEValuesViews::Cache lazily constructed.
authorWolfgang Bangerth <bangerth@colostate.edu>
Sun, 26 Nov 2023 17:23:44 +0000 (10:23 -0700)
committerWolfgang Bangerth <bangerth@colostate.edu>
Wed, 29 Nov 2023 17:49:11 +0000 (10:49 -0700)
include/deal.II/fe/fe_values_base.h
include/deal.II/fe/fe_values_views.h
source/fe/fe_values_views.cc

index 979b19e74d5b792dd939cb3262fd091606a4bb17..aab684d3422e5e529ccc338588e6e5940b75721b 100644 (file)
@@ -1804,7 +1804,10 @@ FEValuesBase<dim, spacedim>::operator[](
 {
   AssertIndexRange(scalar.component, fe_values_views_cache.scalars.size());
 
-  return fe_values_views_cache.scalars[scalar.component];
+  return fe_values_views_cache.scalars[scalar.component].value_or_initialize(
+    [scalar, this]() {
+      return FEValuesViews::Scalar<dim, spacedim>(*this, scalar.component);
+    });
 }
 
 
@@ -1817,7 +1820,11 @@ FEValuesBase<dim, spacedim>::operator[](
   AssertIndexRange(vector.first_vector_component,
                    fe_values_views_cache.vectors.size());
 
-  return fe_values_views_cache.vectors[vector.first_vector_component];
+  return fe_values_views_cache.vectors[vector.first_vector_component]
+    .value_or_initialize([vector, this]() {
+      return FEValuesViews::Vector<dim, spacedim>(
+        *this, vector.first_vector_component);
+    });
 }
 
 
@@ -1835,7 +1842,11 @@ FEValuesBase<dim, spacedim>::operator[](
                   fe_values_views_cache.symmetric_second_order_tensors.size()));
 
   return fe_values_views_cache
-    .symmetric_second_order_tensors[tensor.first_tensor_component];
+    .symmetric_second_order_tensors[tensor.first_tensor_component]
+    .value_or_initialize([tensor, this]() {
+      return FEValuesViews::SymmetricTensor<2, dim, spacedim>(
+        *this, tensor.first_tensor_component);
+    });
 }
 
 
@@ -1849,7 +1860,11 @@ FEValuesBase<dim, spacedim>::operator[](
                    fe_values_views_cache.second_order_tensors.size());
 
   return fe_values_views_cache
-    .second_order_tensors[tensor.first_tensor_component];
+    .second_order_tensors[tensor.first_tensor_component]
+    .value_or_initialize([tensor, this]() {
+      return FEValuesViews::Tensor<2, dim, spacedim>(
+        *this, tensor.first_tensor_component);
+    });
 }
 
 
index 2de5def53c8a34f9196013aaa698c4651c4c838f..50c4c6e716d367d6ab1b29a59a4563c0ce4562e6 100644 (file)
@@ -19,6 +19,7 @@
 #include <deal.II/base/config.h>
 
 #include <deal.II/base/exceptions.h>
+#include <deal.II/base/lazy.h>
 #include <deal.II/base/smartpointer.h>
 #include <deal.II/base/symmetric_tensor.h>
 #include <deal.II/base/tensor.h>
@@ -2054,11 +2055,12 @@ namespace internal
        * Caches for scalar and vector, and symmetric second-order tensor
        * valued views.
        */
-      std::vector<dealii::FEValuesViews::Scalar<dim, spacedim>> scalars;
-      std::vector<dealii::FEValuesViews::Vector<dim, spacedim>> vectors;
-      std::vector<dealii::FEValuesViews::SymmetricTensor<2, dim, spacedim>>
+      std::vector<Lazy<dealii::FEValuesViews::Scalar<dim, spacedim>>> scalars;
+      std::vector<Lazy<dealii::FEValuesViews::Vector<dim, spacedim>>> vectors;
+      std::vector<
+        Lazy<dealii::FEValuesViews::SymmetricTensor<2, dim, spacedim>>>
         symmetric_second_order_tensors;
-      std::vector<dealii::FEValuesViews::Tensor<2, dim, spacedim>>
+      std::vector<Lazy<dealii::FEValuesViews::Tensor<2, dim, spacedim>>>
         second_order_tensors;
 
       /**
index 383f26608d227f0fbfc1b984156bde355b40fc95..343a56eae60d7e71306750086a285226b9134306 100644 (file)
@@ -1314,9 +1314,7 @@ namespace internal
       const FiniteElement<dim, spacedim> &fe = fe_values.get_fe();
 
       const unsigned int n_scalars = fe.n_components();
-      scalars.reserve(n_scalars);
-      for (unsigned int component = 0; component < n_scalars; ++component)
-        scalars.emplace_back(fe_values, component);
+      scalars.resize(n_scalars);
 
       // compute number of vectors that we can fit into this finite element.
       // note that this is based on the dimensionality 'dim' of the manifold,
@@ -1326,9 +1324,7 @@ namespace internal
            fe.n_components() - Tensor<1, spacedim>::n_independent_components +
              1 :
            0);
-      vectors.reserve(n_vectors);
-      for (unsigned int component = 0; component < n_vectors; ++component)
-        vectors.emplace_back(fe_values, component);
+      vectors.resize(n_vectors);
 
       // compute number of symmetric tensors in the same way as above
       const unsigned int n_symmetric_second_order_tensors =
@@ -1337,12 +1333,7 @@ namespace internal
            fe.n_components() -
              SymmetricTensor<2, spacedim>::n_independent_components + 1 :
            0);
-      symmetric_second_order_tensors.reserve(n_symmetric_second_order_tensors);
-      for (unsigned int component = 0;
-           component < n_symmetric_second_order_tensors;
-           ++component)
-        symmetric_second_order_tensors.emplace_back(fe_values, component);
-
+      symmetric_second_order_tensors.resize(n_symmetric_second_order_tensors);
 
       // compute number of symmetric tensors in the same way as above
       const unsigned int n_second_order_tensors =
@@ -1350,10 +1341,7 @@ namespace internal
            fe.n_components() - Tensor<2, spacedim>::n_independent_components +
              1 :
            0);
-      second_order_tensors.reserve(n_second_order_tensors);
-      for (unsigned int component = 0; component < n_second_order_tensors;
-           ++component)
-        second_order_tensors.emplace_back(fe_values, component);
+      second_order_tensors.resize(n_second_order_tensors);
     }
   } // namespace FEValuesViews
 } // namespace internal

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.