From 9f27cf04013d0d7a9a13a99daa1ba14f682096b7 Mon Sep 17 00:00:00 2001
From: Alexander Jaust <alexander.jaust@ipvs.uni-stuttgart.de>
Date: Mon, 27 May 2019 13:15:57 +0200
Subject: [PATCH] [coupling-ff-pm] updating preciceAdapter - main_ff and
 main_pm will update preciceDataIDs of the problem after quantities are
 announced. - PreciceAdapter uses its own indexing (always starting at zero)
 instead of preCICE indexing. This allows for easier data access - member
 `dataVectors_` is increased in size as soon as a new quantity is added to the
 adapter

---
 appl/coupling-ff-pm/iterative/main_ff.cc    |  7 ++++---
 appl/coupling-ff-pm/iterative/main_pm.cc    |  9 +++++----
 appl/coupling-ff-pm/monolithic/ffproblem.hh | 21 +++++++++++++++++----
 appl/coupling-ff-pm/monolithic/pmproblem.hh | 21 +++++++++++++++++----
 4 files changed, 43 insertions(+), 15 deletions(-)

diff --git a/appl/coupling-ff-pm/iterative/main_ff.cc b/appl/coupling-ff-pm/iterative/main_ff.cc
index faee282..c7f3942 100644
--- a/appl/coupling-ff-pm/iterative/main_ff.cc
+++ b/appl/coupling-ff-pm/iterative/main_ff.cc
@@ -105,9 +105,6 @@ int main(int argc, char** argv) try
     couplingInterface.announceSolver( "FreeFlow", preciceConfigFilename,
                                       mpiHelper.rank(), mpiHelper.size() );
 
-    const auto velocityId = couplingInterface.announceQuantity( "Velocity" );
-    const auto pressureId = couplingInterface.announceQuantity( "Pressure" );
-
     const int dim = couplingInterface.getDimensions();
     std::cout << dim << "  " << int(FreeFlowFVGridGeometry::GridView::dimension) << std::endl;
     if (dim != int(FreeFlowFVGridGeometry::GridView::dimension))
@@ -146,6 +143,10 @@ int main(int argc, char** argv) try
                                                                      coords,
                                                                      coupledScvfIndices );
 
+    const auto velocityId = couplingInterface.announceQuantity( "Velocity" );
+    const auto pressureId = couplingInterface.announceQuantity( "Pressure" );
+
+    freeFlowProblem->updatePreciceDataIds();
 
     // apply initial solution for instationary problems
     freeFlowProblem->applyInitialSolution(sol);
diff --git a/appl/coupling-ff-pm/iterative/main_pm.cc b/appl/coupling-ff-pm/iterative/main_pm.cc
index 6874f09..0eb1935 100644
--- a/appl/coupling-ff-pm/iterative/main_pm.cc
+++ b/appl/coupling-ff-pm/iterative/main_pm.cc
@@ -166,9 +166,6 @@ int main(int argc, char** argv) try
     couplingInterface.announceSolver( "Darcy", preciceConfigFilename,
                                       mpiHelper.rank(), mpiHelper.size() );
 
-    const auto velocityId = couplingInterface.announceQuantity( "Velocity" );
-    const auto pressureId = couplingInterface.announceQuantity( "Pressure" );
-
     const int dim = couplingInterface.getDimensions();
     std::cout << dim << "  " << int(DarcyFVGridGeometry::GridView::dimension) << std::endl;
     if (dim != int(DarcyFVGridGeometry::GridView::dimension))
@@ -202,11 +199,15 @@ int main(int argc, char** argv) try
     }
 
     const auto numberOfPoints = coords.size() / dim;
-    const double preciceDt = couplingInterface.setMeshAndInitialize( "darcyMesh",
+    const double preciceDt = couplingInterface.setMeshAndInitialize( "DarcyMesh",
                                                                      numberOfPoints,
                                                                      coords,
                                                                      coupledScvfIndices );
 
+    const auto velocityId = couplingInterface.announceQuantity( "Velocity" );
+    const auto pressureId = couplingInterface.announceQuantity( "Pressure" );
+
+    darcyProblem->updatePreciceDataIds();
 
     darcyProblem->applyInitialSolution(sol);
 
diff --git a/appl/coupling-ff-pm/monolithic/ffproblem.hh b/appl/coupling-ff-pm/monolithic/ffproblem.hh
index 7907372..d336848 100644
--- a/appl/coupling-ff-pm/monolithic/ffproblem.hh
+++ b/appl/coupling-ff-pm/monolithic/ffproblem.hh
@@ -125,8 +125,9 @@ public:
       : ParentType(fvGridGeometry, "Stokes"),
         eps_(1e-6),
         couplingInterface_(precice_adapter::PreciceAdapter::getInstance() ),
-        pressureId_(couplingInterface_.getIdFromName( "Pressure" ) ),
-        velocityId_(couplingInterface_.getIdFromName( "Velocity" ) )
+        pressureId_(0),
+        velocityId_(0),
+        dataIdsWereSet_(false)
 #endif
     {
         deltaP_ = getParamFromGroup<Scalar>(this->paramGroup(), "Problem.PressureDifference");
@@ -196,6 +197,7 @@ public:
         }
 #else
     // // TODO do preCICE stuff in analogy to heat transfer
+        assert( dataIdsWereSet_ );
         const auto faceId = scvf.index();
         if ( couplingInterface_.isCoupledEntity(faceId) )
         {
@@ -246,6 +248,7 @@ public:
             values[Indices::momentumYBalanceIdx] = couplingManager().couplingData().momentumCouplingCondition(element, fvGeometry, elemVolVars, elemFaceVars, scvf);
         }
 #else
+        assert( dataIdsWereSet_ );
         const auto faceId = scvf.index();
         if( couplingInterface_.isCoupledEntity( faceId ) )
         {
@@ -355,6 +358,15 @@ public:
         return analyticalVelocityX_;
     }
 
+#if !ENABLEMONOLITHIC
+    void updatePreciceDataIds()
+    {
+      pressureId_ = couplingInterface_.getIdFromName( "Pressure" );
+      velocityId_ = couplingInterface_.getIdFromName( "Velocity" );
+      dataIdsWereSet_ = true;
+    }
+#endif
+
     // \}
 
 private:
@@ -377,8 +389,9 @@ private:
     std::shared_ptr<CouplingManager> couplingManager_;
 #else
    precice_adapter::PreciceAdapter& couplingInterface_;
-   const size_t pressureId_;
-   const size_t velocityId_;
+   size_t pressureId_;
+   size_t velocityId_;
+   bool dataIdsWereSet_;
 #endif
 
     mutable std::vector<Scalar> analyticalVelocityX_;
diff --git a/appl/coupling-ff-pm/monolithic/pmproblem.hh b/appl/coupling-ff-pm/monolithic/pmproblem.hh
index 479bca7..bbec14c 100644
--- a/appl/coupling-ff-pm/monolithic/pmproblem.hh
+++ b/appl/coupling-ff-pm/monolithic/pmproblem.hh
@@ -128,8 +128,9 @@ public:
 DarcySubProblem(std::shared_ptr<const FVGridGeometry> fvGridGeometry)
     : ParentType(fvGridGeometry, "Darcy"), eps_(1e-7),
       couplingInterface_(precice_adapter::PreciceAdapter::getInstance() ),
-      pressureId_(couplingInterface_.getIdFromName( "Pressure" ) ),
-      velocityId_(couplingInterface_.getIdFromName( "Velocity" ) )
+      pressureId_(0),
+      velocityId_(0),
+      dataIdsWereSet_(false)
 #endif
     {}
 
@@ -214,6 +215,7 @@ DarcySubProblem(std::shared_ptr<const FVGridGeometry> fvGridGeometry)
         if (couplingManager().isCoupledEntity(CouplingManager::darcyIdx, scvf))
             values[Indices::conti0EqIdx] = couplingManager().couplingData().massCouplingCondition(element, fvGeometry, elemVolVars, scvf);
 #else
+        assert( dataIdsWereSet_ );
         const auto faceId = scvf.index();
         if ( couplingInterface_.isCoupledEntity(faceId) )
         {
@@ -266,6 +268,15 @@ DarcySubProblem(std::shared_ptr<const FVGridGeometry> fvGridGeometry)
 
     // \}
 
+#if !ENABLEMONOLITHIC
+    void updatePreciceDataIds()
+    {
+      pressureId_ = couplingInterface_.getIdFromName( "Pressure" );
+      velocityId_ = couplingInterface_.getIdFromName( "Velocity" );
+      dataIdsWereSet_ = true;
+    }
+#endif
+
 #if ENABLEMONOLITHIC
     //! Get the coupling manager
     const CouplingManager& couplingManager() const
@@ -291,8 +302,10 @@ private:
     std::shared_ptr<CouplingManager> couplingManager_;
 #else
    precice_adapter::PreciceAdapter& couplingInterface_;
-   const size_t pressureId_;
-   const size_t velocityId_;
+   size_t pressureId_;
+   size_t velocityId_;
+   bool dataIdsWereSet_;
+
 #endif
 };
 } //end namespace
-- 
GitLab