From 5ece9477bdc654f87d8698b92e68d18875acd412 Mon Sep 17 00:00:00 2001
From: DennisGlaeser <dennis.glaeser@iws.uni-stuttgart.de>
Date: Thu, 11 Jan 2018 18:27:57 +0100
Subject: [PATCH] [tpfa] use more Dune::ReservedVectors in assembly map

This requires additional info to be supplied by the FluxStencils. For example,
this commit further introduces the property MaxNumBranchesPerScvf with
which the size for these reserved vectors is parameterized for network/surface
grids.
---
 dumux/common/properties.hh                          |  1 +
 .../discretization/cellcentered/connectivitymap.hh  | 11 ++++++++---
 .../cellcentered/tpfa/fvelementgeometry.hh          |  4 ++++
 .../discretization/cellcentered/tpfa/properties.hh  | 13 +++++++++++++
 dumux/discretization/fluxstencil.hh                 | 12 ++++++++----
 5 files changed, 34 insertions(+), 7 deletions(-)

diff --git a/dumux/common/properties.hh b/dumux/common/properties.hh
index 2cbc1b8ed7..4899e913eb 100644
--- a/dumux/common/properties.hh
+++ b/dumux/common/properties.hh
@@ -98,6 +98,7 @@ NEW_PROP_TAG(ElementFluxVariablesCache);           //!< A local vector of flux v
 NEW_PROP_TAG(GridFluxVariablesCache);              //!< The global vector of flux variable containers
 NEW_PROP_TAG(EnableGridFluxVariablesCache);        //!< specifies if data on flux vars should be saved (faster, but more memory consuming)
 NEW_PROP_TAG(GridVariables);                       //!< The grid variables object managing variable data on the grid (volvars/fluxvars cache)
+NEW_PROP_TAG(MaxNumBranchesPerScvf);               //!< The maximum number of branches allowed per scvf (used for static memory allocation)
 
 /////////////////////////////////////////////////////////////////
 // Additional properties used by the cell-centered mpfa schemes:
diff --git a/dumux/discretization/cellcentered/connectivitymap.hh b/dumux/discretization/cellcentered/connectivitymap.hh
index ad4d8fe314..0f935b288e 100644
--- a/dumux/discretization/cellcentered/connectivitymap.hh
+++ b/dumux/discretization/cellcentered/connectivitymap.hh
@@ -50,6 +50,7 @@ template<class TypeTag>
 class CCSimpleConnectivityMap
 {
     using FVGridGeometry = typename GET_PROP_TYPE(TypeTag, FVGridGeometry);
+    using FVElementGeometry = typename GET_PROP_TYPE(TypeTag, FVElementGeometry);
     using GridView = typename GET_PROP_TYPE(TypeTag, GridView);
     using IndexType = typename GridView::IndexSet::IndexType;
     using FluxStencil = Dumux::FluxStencil<TypeTag>;
@@ -57,10 +58,10 @@ class CCSimpleConnectivityMap
     struct DataJ
     {
         IndexType globalJ;
-        typename FluxStencil::Stencil scvfsJ;
+        Dune::ReservedVector<IndexType, FluxStencil::maxNumScvfJForI> scvfsJ;
         // A list of additional scvfs is needed for compatibility
         // reasons with more complex connectivity maps (see mpfa)
-        std::vector<IndexType> additionalScvfs;
+        Dune::ReservedVector<IndexType, FluxStencil::maxNumScvfJForI> additionalScvfs;
     };
 
     using Map = std::vector<std::vector<DataJ>>;
@@ -76,7 +77,11 @@ public:
     {
         map_.clear();
         map_.resize(fvGridGeometry.gridView().size(0));
-        Dune::ReservedVector<std::pair<IndexType, DataJ>, FluxStencil::maxSize*2*GridView::dimension> dataJForI;
+
+        // container to store for each element J the elements I that appear in J's flux stencils
+        static constexpr int maxNumJ = FluxStencil::maxFluxStencilSize*FVElementGeometry::maxNumElementScvfs;
+        Dune::ReservedVector<std::pair<IndexType, DataJ>, maxNumJ> dataJForI;
+
         for (const auto& element : elements(fvGridGeometry.gridView()))
         {
             // We are looking for the elements I, for which this element J is in the flux stencil
diff --git a/dumux/discretization/cellcentered/tpfa/fvelementgeometry.hh b/dumux/discretization/cellcentered/tpfa/fvelementgeometry.hh
index aa026fdf82..6595e3576c 100644
--- a/dumux/discretization/cellcentered/tpfa/fvelementgeometry.hh
+++ b/dumux/discretization/cellcentered/tpfa/fvelementgeometry.hh
@@ -72,6 +72,8 @@ public:
     using FVGridGeometry = typename GET_PROP_TYPE(TypeTag, FVGridGeometry);
     //! the maximum number of scvs per element
     static constexpr std::size_t maxNumElementScvs = 1;
+    //! the maximum number of scvfs per element (use cubes for maximum)
+    static constexpr std::size_t maxNumElementScvfs = GridView::dimension == 3 ? 6 : 4;
 
     //! Constructor
     CCTpfaFVElementGeometry(const FVGridGeometry& fvGridGeometry)
@@ -187,6 +189,8 @@ public:
     using FVGridGeometry = typename GET_PROP_TYPE(TypeTag, FVGridGeometry);
     //! the maximum number of scvs per element
     static constexpr std::size_t maxNumElementScvs = 1;
+    //! the maximum number of scvfs per element (use cubes for maximum)
+    static constexpr std::size_t maxNumElementScvfs = dim == 3 ? 6 : 4;
 
     //! Constructor
     CCTpfaFVElementGeometry(const FVGridGeometry& fvGridGeometry)
diff --git a/dumux/discretization/cellcentered/tpfa/properties.hh b/dumux/discretization/cellcentered/tpfa/properties.hh
index 8e2bca2a48..4e77be6a2f 100644
--- a/dumux/discretization/cellcentered/tpfa/properties.hh
+++ b/dumux/discretization/cellcentered/tpfa/properties.hh
@@ -82,6 +82,19 @@ SET_TYPE_PROP(CCTpfaModel, ElementFluxVariablesCache, CCTpfaElementFluxVariables
 //! The global current volume variables vector class
 SET_TYPE_PROP(CCTpfaModel, GridVolumeVariables, CCGridVolumeVariables<TypeTag, GET_PROP_VALUE(TypeTag, EnableGridVolumeVariablesCache)>);
 
+//! Set the maximum admissible number of branches per scvf
+SET_PROP(CCTpfaModel, MaxNumBranchesPerScvf)
+{
+private:
+    using GridView = typename GET_PROP_TYPE(TypeTag, GridView);
+    static constexpr int dim = GridView::dimension;
+    static constexpr int dimWorld = GridView::dimensionworld;
+
+public:
+    //! Per default, we allow for 8 neighbors on network/surface grids
+    static const std::size_t value = dim < dimWorld ? 9 : 2;
+};
+
 //! The sub control volume
 SET_PROP(CCTpfaModel, SubControlVolume)
 {
diff --git a/dumux/discretization/fluxstencil.hh b/dumux/discretization/fluxstencil.hh
index db2f631b76..6ee56e89d2 100644
--- a/dumux/discretization/fluxstencil.hh
+++ b/dumux/discretization/fluxstencil.hh
@@ -59,12 +59,16 @@ class FluxStencilImplementation<TypeTag, DiscretizationMethods::CCTpfa>
     using SubControlVolumeFace = typename GET_PROP_TYPE(TypeTag, SubControlVolumeFace);
     using Element = typename GridView::template Codim<0>::Entity;
     using IndexType = typename GridView::IndexSet::IndexType;
-    static constexpr bool isNetwork = int(GridView::dimension) < int(GridView::dimensionworld);
 
 public:
-    // we assume a maxium of 8 neighbors in embedded network grids otherwise max stencil size is 2
-    static constexpr std::size_t maxSize =  isNetwork ? 9 : 2;
-    using Stencil = Dune::ReservedVector<IndexType, maxSize>;
+    //! The maximum number of elements in a flux stencil (equal to max number of branches)
+    static constexpr int maxFluxStencilSize = GET_PROP_VALUE(TypeTag, MaxNumBranchesPerScvf);
+
+    //! States how many scvfs of an element J might have an element I in the flux stencil
+    static constexpr int maxNumScvfJForI = 1;
+
+    //! The flux stencil type
+    using Stencil = Dune::ReservedVector<IndexType, maxFluxStencilSize>;
 
     static Stencil stencil(const Element& element,
                            const FVElementGeometry& fvGeometry,
-- 
GitLab