From 96adf3ae8094fc4a80ed43ba3c8a5e65059319c7 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Thu, 2 Sep 2021 16:12:17 +0200
Subject: [PATCH] ENH: add IOobject::objectRelPath() for compact output (#2195)

---
 .../makeFaMesh/findMeshDefinitionDict.H       |  4 ++--
 .../generation/PDRblockMesh/PDRblockMesh.C    |  4 ++--
 .../generation/blockMesh/findBlockMeshDict.H  | 22 +++++++++++++++++-
 .../decomposePar/decomposePar.C               |  2 +-
 .../decomposePar/domainDecompositionDryRun.C  |  2 +-
 .../domainDecompositionDryRunWrite.C          |  2 +-
 .../decomposePar/domainDecompositionWrite.C   |  2 +-
 .../reconstructParMesh/reconstructParMesh.C   |  4 ++--
 src/OpenFOAM/db/IOobject/IOobject.C           | 23 +++++++++++++++++++
 src/OpenFOAM/db/IOobject/IOobject.H           |  3 +++
 10 files changed, 57 insertions(+), 11 deletions(-)

diff --git a/applications/utilities/finiteArea/makeFaMesh/findMeshDefinitionDict.H b/applications/utilities/finiteArea/makeFaMesh/findMeshDefinitionDict.H
index 8a678fdcc8c..5385d38f370 100644
--- a/applications/utilities/finiteArea/makeFaMesh/findMeshDefinitionDict.H
+++ b/applications/utilities/finiteArea/makeFaMesh/findMeshDefinitionDict.H
@@ -11,7 +11,7 @@ License
     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
 
 Description
-    Search for the appropriate faMeshDefinition dictionary....
+    Search for the appropriate faMeshDefinition dictionary...
 
 \*---------------------------------------------------------------------------*/
 
@@ -86,7 +86,7 @@ autoPtr<IOdictionary> meshDictPtr;
     }
 
     Info<< "Creating faMesh from definition: "
-        << runTime.relativePath(meshDictIO.objectPath()) << endl;
+        << meshDictIO.objectRelPath() << endl;
 
     meshDictPtr = autoPtr<IOdictionary>::New(meshDictIO);
 }
diff --git a/applications/utilities/mesh/generation/PDRblockMesh/PDRblockMesh.C b/applications/utilities/mesh/generation/PDRblockMesh/PDRblockMesh.C
index cba7c7d9682..32d304c6b4c 100644
--- a/applications/utilities/mesh/generation/PDRblockMesh/PDRblockMesh.C
+++ b/applications/utilities/mesh/generation/PDRblockMesh/PDRblockMesh.C
@@ -5,7 +5,7 @@
     \\  /    A nd           | www.openfoam.com
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
-    Copyright (C) 2019-2020 OpenCFD Ltd.
+    Copyright (C) 2019-2021 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -165,7 +165,7 @@ int main(int argc, char *argv[])
     IOdictionary meshDict(dictIO);
 
     Info<< "Creating PDRblockMesh from "
-        << runTime.relativePath(dictIO.objectPath()) << endl;
+        << dictIO.objectRelPath() << endl;
 
     // Always start from a PDRblock
     PDRblock blkMesh(meshDict, true);
diff --git a/applications/utilities/mesh/generation/blockMesh/findBlockMeshDict.H b/applications/utilities/mesh/generation/blockMesh/findBlockMeshDict.H
index b05bda22c82..4f81c74ccc9 100644
--- a/applications/utilities/mesh/generation/blockMesh/findBlockMeshDict.H
+++ b/applications/utilities/mesh/generation/blockMesh/findBlockMeshDict.H
@@ -1,3 +1,20 @@
+/*---------------------------------------------------------------------------*\
+  =========                 |
+  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
+   \\    /   O peration     |
+    \\  /    A nd           | www.openfoam.com
+     \\/     M anipulation  |
+-------------------------------------------------------------------------------
+    Copyright (C) 2021 OpenCFD Ltd.
+-------------------------------------------------------------------------------
+License
+    This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
+
+Description
+    Search for the appropriate blockMeshDict dictionary...
+
+\*---------------------------------------------------------------------------*/
+
 // Search for the appropriate blockMesh dictionary....
 const word dictName("blockMeshDict");
 
@@ -64,9 +81,12 @@ autoPtr<IOdictionary> meshDictPtr;
     }
 
     Info<< "Creating block mesh from "
-        << runTime.relativePath(meshDictIO.objectPath()) << endl;
+        << meshDictIO.objectRelPath() << endl;
 
     meshDictPtr = autoPtr<IOdictionary>::New(meshDictIO);
 }
 
 const IOdictionary& meshDict = *meshDictPtr;
+
+
+// ************************************************************************* //
diff --git a/applications/utilities/parallelProcessing/decomposePar/decomposePar.C b/applications/utilities/parallelProcessing/decomposePar/decomposePar.C
index 292e694f71e..55d2410b117 100644
--- a/applications/utilities/parallelProcessing/decomposePar/decomposePar.C
+++ b/applications/utilities/parallelProcessing/decomposePar/decomposePar.C
@@ -647,7 +647,7 @@ int main(int argc, char *argv[])
                 cellDecomposition.write();
 
                 Info<< nl << "Wrote decomposition to "
-                    << runTime.relativePath(cellDecomposition.objectPath())
+                    << cellDecomposition.objectRelPath()
                     << " for use in manual decomposition." << endl;
             }
 
diff --git a/applications/utilities/parallelProcessing/decomposePar/domainDecompositionDryRun.C b/applications/utilities/parallelProcessing/decomposePar/domainDecompositionDryRun.C
index b175c96e4fa..060ad804e87 100644
--- a/applications/utilities/parallelProcessing/decomposePar/domainDecompositionDryRun.C
+++ b/applications/utilities/parallelProcessing/decomposePar/domainDecompositionDryRun.C
@@ -160,7 +160,7 @@ void Foam::domainDecompositionDryRun::execute
 //        cellDecomposition.write();
 //
 //        Info<< nl << "Wrote decomposition to "
-//            << runTime.relativePath(cellDecomposition.objectPath())
+//            << cellDecomposition.objectRelPath()
 //            << " for use in manual decomposition." << endl;
 
         Info<< nl;
diff --git a/applications/utilities/parallelProcessing/decomposePar/domainDecompositionDryRunWrite.C b/applications/utilities/parallelProcessing/decomposePar/domainDecompositionDryRunWrite.C
index 5e02b54f574..e7b4ad0d7de 100644
--- a/applications/utilities/parallelProcessing/decomposePar/domainDecompositionDryRunWrite.C
+++ b/applications/utilities/parallelProcessing/decomposePar/domainDecompositionDryRunWrite.C
@@ -64,7 +64,7 @@ void Foam::domainDecompositionDryRun::writeVolField
     cellDist.write();
 
     Info<< nl << "Wrote decomposition to "
-        << this->mesh().time().relativePath(cellDist.objectPath())
+        << cellDist.objectRelPath()
         << " (volScalarField) for visualization."
         << endl;
 }
diff --git a/applications/utilities/parallelProcessing/decomposePar/domainDecompositionWrite.C b/applications/utilities/parallelProcessing/decomposePar/domainDecompositionWrite.C
index 271be14e4fa..a94e443d758 100644
--- a/applications/utilities/parallelProcessing/decomposePar/domainDecompositionWrite.C
+++ b/applications/utilities/parallelProcessing/decomposePar/domainDecompositionWrite.C
@@ -65,7 +65,7 @@ void Foam::domainDecomposition::writeVolField
     cellDist.write();
 
     Info<< nl << "Wrote decomposition to "
-        << this->mesh().time().relativePath(cellDist.objectPath())
+        << cellDist.objectRelPath()
         << " (volScalarField) for visualization."
         << endl;
 }
diff --git a/applications/utilities/parallelProcessing/reconstructParMesh/reconstructParMesh.C b/applications/utilities/parallelProcessing/reconstructParMesh/reconstructParMesh.C
index 8ba2ff25ad2..b92a5dfcc57 100644
--- a/applications/utilities/parallelProcessing/reconstructParMesh/reconstructParMesh.C
+++ b/applications/utilities/parallelProcessing/reconstructParMesh/reconstructParMesh.C
@@ -396,7 +396,7 @@ void writeDistribution
     cellDecomposition.write();
 
     Info<< nl << "Wrote decomposition to "
-        << runTime.relativePath(cellDecomposition.objectPath())
+        << cellDecomposition.objectRelPath()
         << " for use in manual decomposition." << endl;
 
     // Write as volScalarField for postprocessing. Change time to 0
@@ -434,7 +434,7 @@ void writeDistribution
         cellDist.write();
 
         Info<< nl << "Wrote decomposition to "
-            << runTime.relativePath(cellDist.objectPath())
+            << cellDist.objectRelPath()
             << " (volScalarField) for visualization."
             << endl;
 
diff --git a/src/OpenFOAM/db/IOobject/IOobject.C b/src/OpenFOAM/db/IOobject/IOobject.C
index cd478fea969..ad2be69cbd1 100644
--- a/src/OpenFOAM/db/IOobject/IOobject.C
+++ b/src/OpenFOAM/db/IOobject/IOobject.C
@@ -542,6 +542,29 @@ Foam::fileName Foam::IOobject::path
 }
 
 
+Foam::fileName Foam::IOobject::objectRelPath() const
+{
+    // A file is 'outside' of the case if it has been specified using an
+    // absolute path
+
+    const auto first = instance().find('/');
+
+    if
+    (
+        first == 0
+        #ifdef _WIN32
+     || (first == 2 && instance()[1] == ':')  // Eg, d:/path
+        #endif
+    )
+    {
+        // Absolute path (starts with '/' or 'd:/')
+        return instance()/name();
+    }
+
+    return instance()/db_.dbDir()/local()/name();
+}
+
+
 Foam::fileName Foam::IOobject::localFilePath
 (
     const word& typeName,
diff --git a/src/OpenFOAM/db/IOobject/IOobject.H b/src/OpenFOAM/db/IOobject/IOobject.H
index 41096629a08..25177e5c670 100644
--- a/src/OpenFOAM/db/IOobject/IOobject.H
+++ b/src/OpenFOAM/db/IOobject/IOobject.H
@@ -539,6 +539,9 @@ public:
         //- The complete path + object name
         inline fileName objectPath() const;
 
+        //- The object path relative to the root
+        fileName objectRelPath() const;
+
         //- Helper for filePath that searches locally.
         //  When search is false, simply use the current instance,
         //  otherwise search previous instances.
-- 
GitLab