From 374594398f86bbbacb43244d48b2d84ca8c70799 Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Sun, 26 Nov 2023 10:23:44 -0700 Subject: [PATCH] Make the elements of FEValuesViews::Cache lazily constructed. --- include/deal.II/fe/fe_values_base.h | 23 +++++++++++++++++++---- include/deal.II/fe/fe_values_views.h | 10 ++++++---- source/fe/fe_values_views.cc | 20 ++++---------------- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/include/deal.II/fe/fe_values_base.h b/include/deal.II/fe/fe_values_base.h index 979b19e74d..aab684d342 100644 --- a/include/deal.II/fe/fe_values_base.h +++ b/include/deal.II/fe/fe_values_base.h @@ -1804,7 +1804,10 @@ FEValuesBase::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(*this, scalar.component); + }); } @@ -1817,7 +1820,11 @@ FEValuesBase::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( + *this, vector.first_vector_component); + }); } @@ -1835,7 +1842,11 @@ FEValuesBase::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::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); + }); } diff --git a/include/deal.II/fe/fe_values_views.h b/include/deal.II/fe/fe_values_views.h index 2de5def53c..50c4c6e716 100644 --- a/include/deal.II/fe/fe_values_views.h +++ b/include/deal.II/fe/fe_values_views.h @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -2054,11 +2055,12 @@ namespace internal * Caches for scalar and vector, and symmetric second-order tensor * valued views. */ - std::vector> scalars; - std::vector> vectors; - std::vector> + std::vector>> scalars; + std::vector>> vectors; + std::vector< + Lazy>> symmetric_second_order_tensors; - std::vector> + std::vector>> second_order_tensors; /** diff --git a/source/fe/fe_values_views.cc b/source/fe/fe_values_views.cc index 383f26608d..343a56eae6 100644 --- a/source/fe/fe_values_views.cc +++ b/source/fe/fe_values_views.cc @@ -1314,9 +1314,7 @@ namespace internal const FiniteElement &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 -- 2.39.5