From baf924563e4bb8fc4441c38d03479178e62b9e92 Mon Sep 17 00:00:00 2001
From: Kilian Weishaupt <kilian.weishaupt@iws.uni-stuttgart.de>
Date: Wed, 3 Mar 2021 17:35:49 +0100
Subject: [PATCH] [mpadapter] Store copy of pcKrSw law if called with temporary

---
 .../fluidmatrixinteractions/mp/mpadapter.hh   | 19 ++++++++++++++-----
 .../mpnc/2p2ccomparison/spatialparams.hh      |  3 +--
 .../mpnc/kinetic/spatialparams.hh             |  5 ++---
 .../mpnc/obstacle/spatialparams.hh            |  3 +--
 .../thermalnonequilibrium/spatialparams.hh    |  3 +--
 5 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/dumux/material/fluidmatrixinteractions/mp/mpadapter.hh b/dumux/material/fluidmatrixinteractions/mp/mpadapter.hh
index bb3b4a2c9a..afc9e3e096 100644
--- a/dumux/material/fluidmatrixinteractions/mp/mpadapter.hh
+++ b/dumux/material/fluidmatrixinteractions/mp/mpadapter.hh
@@ -107,7 +107,7 @@ namespace Dumux::FluidMatrix {
  * \ingroup Fluidmatrixinteractions
  * \brief An adapter for mpnc to use the capillary pressure-saturation relationships
  */
-template <class MaterialLaw, int numFluidPhases = MaterialLaw::numFluidPhases()>
+template <class MaterialLaw, int numFluidPhases = std::decay_t<MaterialLaw>::numFluidPhases()>
 class MPAdapter
 {
     static_assert(AlwaysFalse<MaterialLaw>::value, "Adapter not implemented for the specified number of phases");
@@ -119,10 +119,10 @@ class MPAdapter<MaterialLaw, 2>
 : public Adapter<MPAdapter<MaterialLaw, 2>, MultiPhasePcKrSw>
 {
 public:
-    using Scalar = typename MaterialLaw::Scalar;
+    using Scalar = typename std::decay_t<MaterialLaw>::Scalar;
 
-    MPAdapter(const MaterialLaw& pcKrS)
-    : pcKrS_(pcKrS)
+    MPAdapter(MaterialLaw&& pcKrS)
+    : pcKrS_(std::forward<MaterialLaw>(pcKrS))
     {}
 
     /*!
@@ -161,9 +161,18 @@ public:
         return values;
     }
 private:
-    const MaterialLaw& pcKrS_;
+    MaterialLaw pcKrS_;
 };
 
+/*!
+ * \ingroup Fluidmatrixinteractions
+ * \brief Deduction guide for the MPAdapter class.
+ *        Makes sure that MPAdapter stores a copy of T if
+ *        the constructor is called with a temporary object.
+ */
+template<typename T>
+MPAdapter(T&&) -> MPAdapter<T>;
+
 } // end namespace Dumux::FluidMatrix
 
 
diff --git a/test/porousmediumflow/mpnc/2p2ccomparison/spatialparams.hh b/test/porousmediumflow/mpnc/2p2ccomparison/spatialparams.hh
index 2478cbe98f..ab9a55a8ef 100644
--- a/test/porousmediumflow/mpnc/2p2ccomparison/spatialparams.hh
+++ b/test/porousmediumflow/mpnc/2p2ccomparison/spatialparams.hh
@@ -53,7 +53,6 @@ class MPNCComparisonSpatialParams
     using GlobalPosition = typename SubControlVolume::GlobalPosition;
 
     using PcKrSwCurve = FluidMatrix::BrooksCoreyDefault<Scalar>;
-    using MPAdapter = Dumux::FluidMatrix::MPAdapter<PcKrSwCurve, 2>;
 
 public:
     using PermeabilityType = Scalar;
@@ -96,7 +95,7 @@ public:
      */
     auto fluidMatrixInteractionAtPos(const GlobalPosition& globalPos) const
     {
-        return makeFluidMatrixInteraction(MPAdapter(pcKrSw_));
+        return makeFluidMatrixInteraction(FluidMatrix::MPAdapter(pcKrSw_));
     }
 
     /*!
diff --git a/test/porousmediumflow/mpnc/kinetic/spatialparams.hh b/test/porousmediumflow/mpnc/kinetic/spatialparams.hh
index ca8ba66062..cd381c9ab2 100644
--- a/test/porousmediumflow/mpnc/kinetic/spatialparams.hh
+++ b/test/porousmediumflow/mpnc/kinetic/spatialparams.hh
@@ -63,7 +63,6 @@ class EvaporationAtmosphereSpatialParams
     static constexpr auto dimWorld = GridView::dimensionworld;
 
     using PcKrSwCurve = FluidMatrix::BrooksCoreyDefault<Scalar>;
-    using MPAdapter = Dumux::FluidMatrix::MPAdapter<PcKrSwCurve, 2>;
 
     using NonwettingSolidInterfacialArea = FluidMatrix::InterfacialArea<Scalar,
                                                                         FluidMatrix::InterfacialAreaExponentialCubic,
@@ -248,9 +247,9 @@ public:
     auto fluidMatrixInteractionAtPos(const GlobalPosition &globalPos) const
     {
         if (inFF_(globalPos))
-            return makeFluidMatrixInteraction(MPAdapter(*pcKrSwCurveFF_), *aNsFreeFlow_, *aNwFreeFlow_, *aWs_);
+            return makeFluidMatrixInteraction(FluidMatrix::MPAdapter(*pcKrSwCurveFF_), *aNsFreeFlow_, *aNwFreeFlow_, *aWs_);
         else if (inPM_(globalPos))
-            return makeFluidMatrixInteraction(MPAdapter(*pcKrSwCurvePM_), *aNs_, *aNw_, *aWs_);
+            return makeFluidMatrixInteraction(FluidMatrix::MPAdapter(*pcKrSwCurvePM_), *aNs_, *aNw_, *aWs_);
         else DUNE_THROW(Dune::InvalidStateException, "You should not be here: x=" << globalPos[0] << " y= "<< globalPos[dimWorld-1]);
     }
 
diff --git a/test/porousmediumflow/mpnc/obstacle/spatialparams.hh b/test/porousmediumflow/mpnc/obstacle/spatialparams.hh
index 5b60e7488a..dac344e23b 100644
--- a/test/porousmediumflow/mpnc/obstacle/spatialparams.hh
+++ b/test/porousmediumflow/mpnc/obstacle/spatialparams.hh
@@ -54,7 +54,6 @@ class ObstacleSpatialParams
     using GlobalPosition = typename SubControlVolume::GlobalPosition;
 
     using PcKrSwCurve = FluidMatrix::SmoothedLinearLaw<Scalar>;
-    using MPAdapter = Dumux::FluidMatrix::MPAdapter<PcKrSwCurve, 2>;
 
 public:
     //! Export the type used for the permeability
@@ -93,7 +92,7 @@ public:
      */
     auto fluidMatrixInteractionAtPos(const GlobalPosition &globalPos) const
     {
-        return makeFluidMatrixInteraction(MPAdapter(pcKrSwCurve_));
+        return makeFluidMatrixInteraction(FluidMatrix::MPAdapter(pcKrSwCurve_));
     }
 
     /*!
diff --git a/test/porousmediumflow/mpnc/thermalnonequilibrium/spatialparams.hh b/test/porousmediumflow/mpnc/thermalnonequilibrium/spatialparams.hh
index 460d1bbf1a..6aa521c527 100644
--- a/test/porousmediumflow/mpnc/thermalnonequilibrium/spatialparams.hh
+++ b/test/porousmediumflow/mpnc/thermalnonequilibrium/spatialparams.hh
@@ -58,7 +58,6 @@ class CombustionSpatialParams
     using GlobalPosition = typename SubControlVolume::GlobalPosition;
 
     using PcKrSwCurve = FluidMatrix::HeatPipeLaw<Scalar>;
-    using MPAdapter = Dumux::FluidMatrix::MPAdapter<PcKrSwCurve, 2>;
 
 public:
     //! Export the type used for the permeability
@@ -195,7 +194,7 @@ public:
      */
     auto fluidMatrixInteractionAtPos(const GlobalPosition &globalPos) const
     {
-        return makeFluidMatrixInteraction(MPAdapter(*pcKrSwCurve_));
+        return makeFluidMatrixInteraction(FluidMatrix::MPAdapter(*pcKrSwCurve_));
     }
 
 private:
-- 
GitLab