From 2d1946f339f39ddb446cfcb4451a0a49142245d4 Mon Sep 17 00:00:00 2001
From: Daniel Bauer <daniel.j.bauer@fau.de>
Date: Mon, 28 Mar 2022 17:43:50 +0200
Subject: [PATCH] init BoundaryFromMesh from config

---
 src/geometry/initializer/BoundaryFromMesh.h   | 19 ++++++++++--
 .../initializer/BoundaryFromMesh.impl.h       | 16 +++++-----
 src/mesh/boundary/ColorToBoundaryMapper.h     | 29 ++++++++++++++++++-
 3 files changed, 54 insertions(+), 10 deletions(-)

diff --git a/src/geometry/initializer/BoundaryFromMesh.h b/src/geometry/initializer/BoundaryFromMesh.h
index e2b676d79..e0060d328 100644
--- a/src/geometry/initializer/BoundaryFromMesh.h
+++ b/src/geometry/initializer/BoundaryFromMesh.h
@@ -36,6 +36,22 @@ namespace initializer {
 
 //**********************************************************************************************************************
 /*! Initializes the flag field according to a mesh and [color -> boundary] mapping.
+*
+* Configuration file syntax:
+  \verbatim
+     Mesh
+     {
+        fluidFlag fluid;
+        ColorToBoundaryMapper
+        {
+           default NoSlip;
+           0 { color <0.0, 0.5, 1.0>; boundary FreeSlip; }
+           1 { color <1.0, 0.5, 0.0>; boundary Velocity0; }
+        }
+     }
+  \endverbatim
+*
+* \ingroup geometry
 */
 //**********************************************************************************************************************
 template<typename FlagField_T, typename Mesh_T>
@@ -48,7 +64,7 @@ public:
                      shared_ptr<mesh::DistanceOctree<Mesh_T>> distanceOctree,
                      const uint_t numGhostLayers );
 
-   void init( shared_ptr<mesh::ColorToBoundaryMapper<Mesh_T>> colorToBoundaryMapper,
+   void init( mesh::ColorToBoundaryMapper<Mesh_T> & colorToBoundaryMapper,
               FlagUID fluidFlagID );
 
    void init( BlockStorage & blockStorage, const Config::BlockHandle & blockHandle ) override;
@@ -56,7 +72,6 @@ public:
    shared_ptr<mesh::BoundaryLocation<Mesh_T>> getBoundaryLocation() const;
 
 protected:
-   shared_ptr<StructuredBlockStorage> blocks_;
    BlockDataID flagFieldID_;
    BlockDataID flagFieldBoundarHandlingID_;
 
diff --git a/src/geometry/initializer/BoundaryFromMesh.impl.h b/src/geometry/initializer/BoundaryFromMesh.impl.h
index 36e42f125..977aeef23 100644
--- a/src/geometry/initializer/BoundaryFromMesh.impl.h
+++ b/src/geometry/initializer/BoundaryFromMesh.impl.h
@@ -32,6 +32,7 @@ namespace initializer {
 
 namespace internal {
 
+// TODO: move this to central place
 //**********************************************************************************************************************
 /*! A small helper class that makes it possible to use mesh::boundary::BoundarySetup
 *   to operate directly on the flag field instead of on a boundary handling.
@@ -116,8 +117,7 @@ BoundaryFromMesh<FlagField_T, Mesh_T>::BoundaryFromMesh(
    shared_ptr<Mesh_T> mesh,
    shared_ptr<mesh::DistanceOctree<Mesh_T>> distanceOctree,
    const uint_t numGhostLayers )
-   : blocks_(blocks),
-     flagFieldID_(flagFieldID),
+   : flagFieldID_(flagFieldID),
      mesh_(mesh),
      distanceOctree_(distanceOctree),
      boundarySetup_(blocks, makeMeshDistanceFunction(distanceOctree), numGhostLayers)
@@ -141,14 +141,14 @@ BoundaryFromMesh<FlagField_T, Mesh_T>::BoundaryFromMesh(
 //*******************************************************************************************************************
 template<typename FlagField_T, typename Mesh_T>
 void BoundaryFromMesh<FlagField_T, Mesh_T>::init(
-   shared_ptr<mesh::ColorToBoundaryMapper<Mesh_T>> colorToBoundaryMapper,
+   mesh::ColorToBoundaryMapper<Mesh_T> & colorToBoundaryMapper,
    FlagUID fluidFlagID )
 {
    using namespace internal;
 
    boundarySetup_.setFlag<FlagField_T>(flagFieldID_, fluidFlagID, mesh::BoundarySetup::OUTSIDE);
 
-   boundaryLocation_ = colorToBoundaryMapper->addBoundaryInfoToMesh(*mesh_);
+   boundaryLocation_ = colorToBoundaryMapper.addBoundaryInfoToMesh(*mesh_);
 
    boundarySetup_.setBoundaries<FlagFieldBoundaryHandling<FlagField_T>>(
       flagFieldBoundarHandlingID_,
@@ -158,10 +158,12 @@ void BoundaryFromMesh<FlagField_T, Mesh_T>::init(
 
 template<typename FlagField_T, typename Mesh_T>
 void BoundaryFromMesh<FlagField_T, Mesh_T>::init(
-   BlockStorage & blockStorage, const Config::BlockHandle & blockHandle )
+   BlockStorage &, const Config::BlockHandle & blockHandle )
 {
-   // TODO: implement
-   WALBERLA_ABORT("Not implemented")
+   mesh::ColorToBoundaryMapper< Mesh_T > colorToBoundaryMapper{ blockHandle.getBlock( "ColorToBoundaryMapper") };
+   FlagUID fluidFlag = FlagUID{ blockHandle.getParameter< std::string >( "fluidFlag") };
+
+   init( colorToBoundaryMapper, fluidFlag );
 }
 
 //*******************************************************************************************************************
diff --git a/src/mesh/boundary/ColorToBoundaryMapper.h b/src/mesh/boundary/ColorToBoundaryMapper.h
index 1a69b5af5..c6ba36e6f 100644
--- a/src/mesh/boundary/ColorToBoundaryMapper.h
+++ b/src/mesh/boundary/ColorToBoundaryMapper.h
@@ -23,6 +23,7 @@
 
 #include "BoundaryInfo.h"
 
+#include "core/config/Config.h"
 #include "core/DataTypes.h"
 #include "core/debug/CheckFunctions.h"
 
@@ -39,6 +40,32 @@ public:
 
    ColorToBoundaryMapper( const BoundaryInfo & defaultBoundaryInfo ) : defaultBoundaryInfo_(defaultBoundaryInfo) { }
 
+   ColorToBoundaryMapper( const Config::BlockHandle & blockHandle )
+   {
+      if ( not blockHandle.isValid() )
+         return;
+
+      const std::string defaultID = blockHandle.getParameter< std::string >( "default" );
+      defaultBoundaryInfo_ = BoundaryInfo( boundary::BoundaryUID( defaultID) );
+
+      Config::Blocks colorBoundaryMappings;
+      blockHandle.getBlocks( colorBoundaryMappings );
+
+      for ( const auto & mapping : colorBoundaryMappings )
+      {
+         const Vector3< real_t > colorRaw = mapping.getParameter< Vector3< real_t > >( "color" );
+         const std::string id = mapping.getParameter< std::string >( "boundary" );
+
+         // TODO: this conversion makes assumptions on the templated type (Color)
+         const Color color{ std::round( 255 * colorRaw[0]),
+                            std::round( 255 * colorRaw[1]),
+                            std::round( 255 * colorRaw[2]) };
+         const BoundaryInfo info{ boundary::BoundaryUID( id ) };
+
+         set( color, info );
+      }
+   }
+
    void set( const Color & c, const BoundaryInfo & bi )
    {
       boundaryInfoMap_[c] = bi;
@@ -70,4 +97,4 @@ private:
 };
 
 } // namespace walberla
-} // namespace mesh
\ No newline at end of file
+} // namespace mesh
-- 
GitLab