From 3aa78f2bf382b931e159a523e09653853ff52ed7 Mon Sep 17 00:00:00 2001
From: Henry Weller <http://cfd.direct>
Date: Mon, 19 Sep 2016 07:52:42 +0100
Subject: [PATCH] blockMesh: Added block face orientation checks to aid
 debugging

Individual inward-pointing faces are checked and if all faces are
inward-pointing the block is inside-out.  These errors are fatal and the
message indicates which block the error occurs in and where in the
blockMeshDict the block is defined.
---
 .../blockDescriptor/blockDescriptor.C         | 65 ++++++++++++++++++-
 .../blockDescriptor/blockDescriptor.H         |  3 +
 2 files changed, 65 insertions(+), 3 deletions(-)

diff --git a/src/mesh/blockMesh/blockDescriptor/blockDescriptor.C b/src/mesh/blockMesh/blockDescriptor/blockDescriptor.C
index 1bdb4a82d7d..f8f148756dd 100644
--- a/src/mesh/blockMesh/blockDescriptor/blockDescriptor.C
+++ b/src/mesh/blockMesh/blockDescriptor/blockDescriptor.C
@@ -23,9 +23,66 @@ License
 
 \*---------------------------------------------------------------------------*/
 
-#include "error.H"
 #include "blockDescriptor.H"
 
+// * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
+
+void Foam::blockDescriptor::check(const Istream& is)
+{
+    const point blockCentre(blockShape_.centre(blockPointField_));
+    const faceList faces(blockShape_.faces());
+
+    // Check each face is outward-pointing with respect to the block centre
+    label outwardFaceCount = 0;
+    boolList correctFaces(faces.size(), true);
+
+    forAll(faces, i)
+    {
+        point faceCentre(faces[i].centre(blockPointField_));
+        vector faceNormal(faces[i].normal(blockPointField_));
+        if (mag(faceNormal) > SMALL)
+        {
+            if (((faceCentre - blockCentre) & faceNormal) > 0)
+            {
+                outwardFaceCount++;
+            }
+            else
+            {
+                correctFaces[i] = false;
+            }
+        }
+        else
+        {
+            outwardFaceCount++;
+        }
+    }
+
+    // If all faces are inward-pointing the block is inside-out
+    if (outwardFaceCount == 0)
+    {
+        FatalIOErrorInFunction(is)
+            << "Block " << *this << " is inside-out"
+            << exit(FatalIOError);
+    }
+    else if (outwardFaceCount != faces.size())
+    {
+        FatalIOErrorInFunction(is)
+            << "Block " << *this << " has inward-pointing faces"
+            << nl << "    ";
+
+        forAll(correctFaces, i)
+        {
+            if (!correctFaces[i])
+            {
+                FatalIOError<< faces[i] << token::SPACE;
+            }
+        }
+
+        FatalIOError << exit(FatalIOError);
+    }
+}
+
+
 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
 
 Foam::blockDescriptor::blockDescriptor
@@ -156,11 +213,13 @@ Foam::blockDescriptor::blockDescriptor
     }
     else
     {
-        FatalErrorInFunction
+        FatalIOErrorInFunction(is)
             << "Unknown definition of expansion ratios: " << expRatios
-            << exit(FatalError);
+            << exit(FatalIOError);
     }
 
+    check(is);
+
     // Create a list of edges
     makeBlockEdges();
 }
diff --git a/src/mesh/blockMesh/blockDescriptor/blockDescriptor.H b/src/mesh/blockMesh/blockDescriptor/blockDescriptor.H
index c245eefccc9..37bba39bba9 100644
--- a/src/mesh/blockMesh/blockDescriptor/blockDescriptor.H
+++ b/src/mesh/blockMesh/blockDescriptor/blockDescriptor.H
@@ -90,6 +90,9 @@ class blockDescriptor
 
     // Private Member Functions
 
+        //- Check block has outward-pointing faces
+        void check(const Istream& is);
+
         //- Set the points/weights for all edges
         void makeBlockEdges();
 
-- 
GitLab