From 730ce92b683ea8dbbecf354ed74d330b967e939f Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Wed, 9 Mar 2022 15:58:14 +0100 Subject: [PATCH] ENH: handle try-construct faMesh (#2399) - a try/catch approach is not really robust enough (or even possible) since read failures likely do not occur on all ranks simultaneously. This leads to situations where the master has thrown an exception (and thus exiting the current routine) while other ranks are still waiting to receive data and the program blocks completely. Since this primarily affects data conversion routines such as foamToEnsight etc, treat similarly to lagrangian: check for the existence of essential files before proceeding or not. This is wrapped into a TryNew factory method: autoPtr faMeshPtr(faMesh::TryNew(mesh)); if (faMeshPtr) ... --- .../foamToEnsight/createMeshAccounting.H | 17 +- .../foamToVTK/convertAreaFields.H | 13 +- src/finiteArea/Make/files | 1 + src/finiteArea/faMesh/faMesh.H | 19 ++- src/finiteArea/faMesh/faMeshNew.C | 157 ++++++++++++++++++ 5 files changed, 180 insertions(+), 27 deletions(-) create mode 100644 src/finiteArea/faMesh/faMeshNew.C diff --git a/applications/utilities/postProcessing/dataConversion/foamToEnsight/createMeshAccounting.H b/applications/utilities/postProcessing/dataConversion/foamToEnsight/createMeshAccounting.H index 909d3dfedf..13c7a6034d 100644 --- a/applications/utilities/postProcessing/dataConversion/foamToEnsight/createMeshAccounting.H +++ b/applications/utilities/postProcessing/dataConversion/foamToEnsight/createMeshAccounting.H @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2021 OpenCFD Ltd. + Copyright (C) 2021-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM, distributed under GPL-3.0-or-later. @@ -65,18 +65,7 @@ PtrList ensightMeshesFa(regionNames.size()); if (doFiniteArea) { - autoPtr faMeshPtr; - - const bool oldThrowingError = FatalError.throwing(true); - try - { - faMeshPtr.reset(new faMesh(mesh)); - } - catch (const Foam::error& err) - { - faMeshPtr.reset(nullptr); - } - FatalError.throwing(oldThrowingError); + autoPtr faMeshPtr(faMesh::TryNew(mesh)); if (faMeshPtr) { @@ -91,7 +80,7 @@ PtrList ensightMeshesFa(regionNames.size()); ) ); - meshesFa.set(regioni, faMeshPtr); + meshesFa.set(regioni, std::move(faMeshPtr)); ensightMeshesFa.set ( diff --git a/applications/utilities/postProcessing/dataConversion/foamToVTK/convertAreaFields.H b/applications/utilities/postProcessing/dataConversion/foamToVTK/convertAreaFields.H index f9f15fb482..ddcebf63e4 100644 --- a/applications/utilities/postProcessing/dataConversion/foamToVTK/convertAreaFields.H +++ b/applications/utilities/postProcessing/dataConversion/foamToVTK/convertAreaFields.H @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2018-2021 OpenCFD Ltd. + Copyright (C) 2018-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -46,16 +46,7 @@ if (doFiniteArea) if (nAreaFields) { - const bool oldThrowingError = FatalError.throwing(true); - try - { - faMeshPtr.reset(new faMesh(meshProxy.baseMesh())); - } - catch (const Foam::error& err) - { - faMeshPtr.clear(); - } - FatalError.throwing(oldThrowingError); + faMeshPtr = faMesh::TryNew(meshProxy.baseMesh()); } if (faMeshPtr && nAreaFields) diff --git a/src/finiteArea/Make/files b/src/finiteArea/Make/files index 2badfb337d..7c2cdd4123 100644 --- a/src/finiteArea/Make/files +++ b/src/finiteArea/Make/files @@ -1,5 +1,6 @@ faMesh/faGlobalMeshData/faGlobalMeshData.C faMesh/faMesh.C +faMesh/faMeshNew.C faMesh/faMeshDemandDrivenData.C faMesh/faMeshPatches.C faMesh/faMeshTopology.C diff --git a/src/finiteArea/faMesh/faMesh.H b/src/finiteArea/faMesh/faMesh.H index 8a3ff124bb..731a2c8208 100644 --- a/src/finiteArea/faMesh/faMesh.H +++ b/src/finiteArea/faMesh/faMesh.H @@ -43,8 +43,8 @@ Author \*---------------------------------------------------------------------------*/ -#ifndef faMesh_H -#define faMesh_H +#ifndef Foam_faMesh_H +#define Foam_faMesh_H #include "MeshObject.H" #include "polyMesh.H" @@ -454,6 +454,15 @@ class faMesh } + // Static Functions + + //- Test if faSchemes/faSolution files are available + static bool hasSystemFiles(const polyMesh& pMesh); + + //- Test if all files needed for read construction are available + static bool hasFiles(const polyMesh& pMesh); + + public: // Public Typedefs @@ -504,6 +513,12 @@ public: virtual ~faMesh(); + // Static Functions + + //- Read construction from polyMesh if all files are available + static autoPtr TryNew(const polyMesh& pMesh); + + // Member Functions // Helpers diff --git a/src/finiteArea/faMesh/faMeshNew.C b/src/finiteArea/faMesh/faMeshNew.C new file mode 100644 index 0000000000..1fcfe24458 --- /dev/null +++ b/src/finiteArea/faMesh/faMeshNew.C @@ -0,0 +1,157 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2022 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "faMesh.H" +#include "polyMesh.H" +#include "fileOperation.H" + +// * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * // + +bool Foam::faMesh::hasSystemFiles(const polyMesh& pMesh) +{ + // Expect + // - system/faSchemes + // - system/faSolution + + const fileOperation& fp = Foam::fileHandler(); + + bool looksValid = true; + + // Global files: system/{faSchemes,faSolution} + for + ( + const word& expect + : List + ({ + {"faSchemes"}, + {"faSolution"} + }) + ) + { + fileName found + ( + fp.filePath + ( + true, // global + IOobject + ( + expect, + pMesh.time().system(), + pMesh, + IOobject::MUST_READ, + IOobject::NO_WRITE, + false + ), + expect // typeName (ununsed?) + ) + ); + + if (found.empty()) + { + looksValid = false; + } + } + + Pstream::broadcast(looksValid); + + return looksValid; +} + + +bool Foam::faMesh::hasFiles(const polyMesh& pMesh) +{ + // As well as system/{faSchemes,faSolution} + // + // expect these: + // - timeValue/faMesh/faBoundary + // - timeValue/instance/faMesh/faceLabels + + bool looksValid = hasSystemFiles(pMesh); + + if (looksValid) + { + const fileOperation& fp = Foam::fileHandler(); + + fileName subDir(pMesh.dbDir()/faMesh::meshSubDir); + + for + ( + const wordPair& expect + : List + ({ + {"faBoundary", "faBoundaryMesh"}, + {"faceLabels", "labelList"} + }) + ) + { + const word& dataFile = expect.first(); + const word& dataClass = expect.second(); + + fileName found + ( + fp.filePath + ( + false, // non-global + IOobject + ( + dataFile, + pMesh.time().findInstance(subDir, dataFile), + faMesh::meshSubDir, + pMesh, + IOobject::MUST_READ, + IOobject::NO_WRITE, + false + ), + dataClass // typeName (ununsed?) + ) + ); + + if (found.empty()) + { + looksValid = false; + } + } + + Pstream::broadcast(looksValid); + } + + return looksValid; +} + + +Foam::autoPtr Foam::faMesh::TryNew(const polyMesh& pMesh) +{ + if (faMesh::hasFiles(pMesh)) + { + return autoPtr::New(pMesh); + } + + return nullptr; +} + + +// ************************************************************************* //