From e6717b6e72b80005058dd2190e5d4748757d1b84 Mon Sep 17 00:00:00 2001
From: Henry Weller <http://openfoam.org>
Date: Fri, 14 Jul 2017 12:38:21 +0100
Subject: [PATCH] triSurfaceMesh: corrected parallel operation of 'file'
 specification

Patch contributed by Mattijs Janssens
Resolves bug-report https://bugs.openfoam.org/view.php?id=2614
---
 .../triSurfaceMesh/triSurfaceMesh.C           | 54 ++++++++++++++++---
 .../triSurfaceMesh/triSurfaceMesh.H           | 11 +++-
 2 files changed, 56 insertions(+), 9 deletions(-)

diff --git a/src/meshTools/searchableSurfaces/triSurfaceMesh/triSurfaceMesh.C b/src/meshTools/searchableSurfaces/triSurfaceMesh/triSurfaceMesh.C
index 64af5e5298d..2aff7124af2 100644
--- a/src/meshTools/searchableSurfaces/triSurfaceMesh/triSurfaceMesh.C
+++ b/src/meshTools/searchableSurfaces/triSurfaceMesh/triSurfaceMesh.C
@@ -65,9 +65,34 @@ Foam::fileName Foam::triSurfaceMesh::checkFile
 }
 
 
+Foam::fileName Foam::triSurfaceMesh::relativeFilePath
+(
+    const regIOobject& io,
+    const fileName& f,
+    const bool isGlobal
+)
+{
+    fileName fName(f);
+    fName.expand();
+    if (!fName.isAbsolute())
+    {
+        // Is the specified file:
+        // - local to the cwd?
+        // - local to the case dir?
+        // - or just another name?
+        fName = fileHandler().filePath
+        (
+            isGlobal,
+            IOobject(io, fName),
+            word::null
+        );
+    }
+    return fName;
+}
+
 Foam::fileName Foam::triSurfaceMesh::checkFile
 (
-    const IOobject& io,
+    const regIOobject& io,
     const dictionary& dict,
     const bool isGlobal
 )
@@ -75,11 +100,8 @@ Foam::fileName Foam::triSurfaceMesh::checkFile
     fileName fName;
     if (dict.readIfPresent("file", fName, false, false))
     {
-        fName.expand();
-        if (!fName.isAbsolute())
-        {
-            fName = io.objectPath().path()/fName;
-        }
+        fName = relativeFilePath(io, fName, isGlobal);
+
         if (!exists(fName))
         {
             FatalErrorInFunction
@@ -299,7 +321,15 @@ Foam::triSurfaceMesh::triSurfaceMesh
     outsideVolType_(volumeType::UNKNOWN)
 {
     // Reading from supplied file name instead of objectPath/filePath
-    dict.readIfPresent("file", fName_, false, false);
+    if (dict.readIfPresent("file", fName_, false, false))
+    {
+        fName_ = relativeFilePath
+        (
+            static_cast<const searchableSurface&>(*this),
+            fName_,
+            true
+        );
+    }
 
     scalar scaleFactor = 0;
 
@@ -391,7 +421,15 @@ Foam::triSurfaceMesh::triSurfaceMesh
     outsideVolType_(volumeType::UNKNOWN)
 {
     // Reading from supplied file name instead of objectPath/filePath
-    dict.readIfPresent("file", fName_, false, false);
+    if (dict.readIfPresent("file", fName_, false, false))
+    {
+        fName_ = relativeFilePath
+        (
+            static_cast<const searchableSurface&>(*this),
+            fName_,
+            isGlobal
+        );
+    }
 
     scalar scaleFactor = 0;
 
diff --git a/src/meshTools/searchableSurfaces/triSurfaceMesh/triSurfaceMesh.H b/src/meshTools/searchableSurfaces/triSurfaceMesh/triSurfaceMesh.H
index d35abbad2eb..110ee7944b2 100644
--- a/src/meshTools/searchableSurfaces/triSurfaceMesh/triSurfaceMesh.H
+++ b/src/meshTools/searchableSurfaces/triSurfaceMesh/triSurfaceMesh.H
@@ -95,10 +95,19 @@ class triSurfaceMesh
         //- Return fileName to load IOobject from
         static fileName checkFile(const IOobject& io, const bool isGlobal);
 
+        //- Return fileName. If fileName is relative gets treated local to
+        //  IOobject
+        static fileName relativeFilePath
+        (
+            const regIOobject&,
+            const fileName&,
+            const bool isGlobal
+        );
+
         //- Return fileName to load IOobject from. Optional override of fileName
         static fileName checkFile
         (
-            const IOobject&,
+            const regIOobject&,
             const dictionary&,
             const bool isGlobal
         );
-- 
GitLab