From d2b1b1cdc0736eb4c79ceb0c8e02d70df918c47d Mon Sep 17 00:00:00 2001 From: mattijs Date: Thu, 12 Apr 2018 15:12:04 +0100 Subject: [PATCH] ENH: paraFoam: catch read errors. Fixes #798. --- .../vtkPVFoam/vtkPVFoamFieldTemplates.C | 373 ++++++++++++------ src/Allwmake | 1 - src/OpenFOAM/db/error/IOerror.C | 41 +- src/OpenFOAM/db/error/error.H | 5 +- 4 files changed, 271 insertions(+), 149 deletions(-) diff --git a/applications/utilities/postProcessing/graphics/PVReaders/vtkPVFoam/vtkPVFoamFieldTemplates.C b/applications/utilities/postProcessing/graphics/PVReaders/vtkPVFoam/vtkPVFoamFieldTemplates.C index 1b8b94735a..40802fdb7c 100644 --- a/applications/utilities/postProcessing/graphics/PVReaders/vtkPVFoam/vtkPVFoamFieldTemplates.C +++ b/applications/utilities/postProcessing/graphics/PVReaders/vtkPVFoam/vtkPVFoamFieldTemplates.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2017 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -276,11 +276,33 @@ void Foam::vtkPVFoam::convertVolFields if (ioobj.headerClassName() == FieldType::typeName) { - // Load field - FieldType fld(ioobj, mesh); + // Throw FatalError, FatalIOError as exceptions + const bool throwingError = FatalError.throwExceptions(); + const bool throwingIOerr = FatalIOError.throwExceptions(); - // Convert - convertVolField(patchInterpList, fld); + try + { + // Load field + FieldType fld(ioobj, mesh); + + // Convert + convertVolField(patchInterpList, fld); + } + catch (Foam::IOerror& ioErr) + { + ioErr.write(Warning, false); + Info << nl << endl; + } + catch (Foam::error& err) + { + // Bit of trickery to get the original message + err.write(Warning, false); + Info << nl << endl; + } + + // Restore previous exception throwing state + FatalError.throwExceptions(throwingError); + FatalIOError.throwExceptions(throwingIOerr); } } } @@ -307,40 +329,62 @@ void Foam::vtkPVFoam::convertDimFields continue; } - // Load field - FieldType dimFld(ioobj, mesh); + // Throw FatalError, FatalIOError as exceptions + const bool throwingError = FatalError.throwExceptions(); + const bool throwingIOerr = FatalIOError.throwExceptions(); - // Construct volField with zero-gradient patch fields - - IOobject io(dimFld); - io.readOpt() = IOobject::NO_READ; - - PtrList> patchFields(mesh.boundary().size()); - forAll(patchFields, patchI) + try { - patchFields.set - ( - patchI, - fvPatchField::New + // Load field + FieldType dimFld(ioobj, mesh); + + // Construct volField with zero-gradient patch fields + + IOobject io(dimFld); + io.readOpt() = IOobject::NO_READ; + + PtrList> patchFields(mesh.boundary().size()); + forAll(patchFields, patchI) + { + patchFields.set ( - zeroGradientFvPatchField::typeName, - mesh.boundary()[patchI], - dimFld - ) + patchI, + fvPatchField::New + ( + zeroGradientFvPatchField::typeName, + mesh.boundary()[patchI], + dimFld + ) + ); + } + + VolFieldType volFld + ( + io, + dimFld.mesh(), + dimFld.dimensions(), + dimFld, + patchFields ); + volFld.correctBoundaryConditions(); + + convertVolField(patchInterpList, volFld); + } + catch (Foam::IOerror& ioErr) + { + ioErr.write(Warning, false); + Info << nl << endl; + } + catch (Foam::error& err) + { + // Bit of trickery to get the original message + err.write(Warning, false); + Info << nl << endl; } - VolFieldType volFld - ( - io, - dimFld.mesh(), - dimFld.dimensions(), - dimFld, - patchFields - ); - volFld.correctBoundaryConditions(); - - convertVolField(patchInterpList, volFld); + // Restore previous exception throwing state + FatalError.throwExceptions(throwingError); + FatalIOError.throwExceptions(throwingIOerr); } } @@ -421,31 +465,54 @@ void Foam::vtkPVFoam::convertAreaFields if (ioobj.headerClassName() == FieldType::typeName) { - // Load field - FieldType fld(ioobj, mesh); + // Throw FatalError, FatalIOError as exceptions + const bool throwingError = FatalError.throwExceptions(); + const bool throwingIOerr = FatalIOError.throwExceptions(); - // Convert - for (const auto partId : partIds) + try { - const auto& longName = selectedPartIds_[partId]; + // Load field + FieldType fld(ioobj, mesh); - auto iter = cachedVtp_.find(longName); - if (!iter.found() || !iter.object().dataset) + // Convert + for (const auto partId : partIds) { - // Should not happen, but for safety require a vtk geometry - continue; + const auto& longName = selectedPartIds_[partId]; + + auto iter = cachedVtp_.find(longName); + if (!iter.found() || !iter.object().dataset) + { + // Should not happen, but for safety require a vtk + // geometry + continue; + } + + foamVtpData& vtpData = iter.object(); + auto dataset = vtpData.dataset; + + vtkSmartPointer cdata = convertFieldToVTK + ( + fld.name(), + fld + ); + dataset->GetCellData()->AddArray(cdata); } - - foamVtpData& vtpData = iter.object(); - auto dataset = vtpData.dataset; - - vtkSmartPointer cdata = convertFieldToVTK - ( - fld.name(), - fld - ); - dataset->GetCellData()->AddArray(cdata); } + catch (Foam::IOerror& ioErr) + { + ioErr.write(Warning, false); + Info << nl << endl; + } + catch (Foam::error& err) + { + // Bit of trickery to get the original message + err.write(Warning, false); + Info << nl << endl; + } + + // Restore previous exception throwing state + FatalError.throwExceptions(throwingError); + FatalIOError.throwExceptions(throwingIOerr); } } } @@ -483,91 +550,113 @@ void Foam::vtkPVFoam::convertPointFields Info<< "convertPointFields : " << fieldName << nl; } - FieldType pfld(ioobj, pMesh); + // Throw FatalError, FatalIOError as exceptions + const bool throwingError = FatalError.throwExceptions(); + const bool throwingIOerr = FatalIOError.throwExceptions(); - convertPointFieldBlock(pfld, rangeVolume_); // internalMesh - convertPointFieldBlock(pfld, rangeCellZones_); // cellZones - convertPointFieldBlock(pfld, rangeCellSets_); // cellSets - - // Patches - currently skip field conversion for groups - for - ( - const auto partId - : rangePatches_.intersection(selectedPartIds_) - ) + try { - const auto& longName = selectedPartIds_[partId]; + FieldType pfld(ioobj, pMesh); - auto iter = cachedVtp_.find(longName); - if (!iter.found() || !iter.object().dataset) - { - // Should not happen, but for safety require a vtk geometry - continue; - } + convertPointFieldBlock(pfld, rangeVolume_); // internalMesh + convertPointFieldBlock(pfld, rangeCellZones_); // cellZones + convertPointFieldBlock(pfld, rangeCellSets_); // cellSets - foamVtpData& vtpData = iter.object(); - auto dataset = vtpData.dataset; - - const labelList& patchIds = vtpData.additionalIds(); - if (patchIds.size() != 1) - { - continue; - } - - const label patchId = patchIds[0]; - - vtkSmartPointer pdata = convertFieldToVTK + // Patches - currently skip field conversion for groups + for ( - fieldName, - pfld.boundaryField()[patchId].patchInternalField()() - ); - - dataset->GetPointData()->AddArray(pdata); - } - - // Face Zones - for - ( - const auto partId - : rangeFaceZones_.intersection(selectedPartIds_) - ) - { - const auto& longName = selectedPartIds_[partId]; - const word zoneName = getFoamName(longName); - - auto iter = cachedVtp_.find(longName); - if (!iter.found() || !iter.object().dataset) + const auto partId + : rangePatches_.intersection(selectedPartIds_) + ) { - // Should not happen, but for safety require a vtk geometry - continue; - } + const auto& longName = selectedPartIds_[partId]; - foamVtpData& vtpData = iter.object(); - auto dataset = vtpData.dataset; + auto iter = cachedVtp_.find(longName); + if (!iter.found() || !iter.object().dataset) + { + // Should not happen, but for safety require a vtk geometry + continue; + } - const label zoneId = mesh.faceZones().findZoneID(zoneName); + foamVtpData& vtpData = iter.object(); + auto dataset = vtpData.dataset; - if (zoneId < 0) - { - continue; - } + const labelList& patchIds = vtpData.additionalIds(); + if (patchIds.size() != 1) + { + continue; + } - // Extract the field on the zone - Field znfld - ( - pfld.primitiveField(), - mesh.faceZones()[zoneId]().meshPoints() - ); + const label patchId = patchIds[0]; - vtkSmartPointer pdata = - convertFieldToVTK + vtkSmartPointer pdata = convertFieldToVTK ( fieldName, - znfld + pfld.boundaryField()[patchId].patchInternalField()() ); - dataset->GetPointData()->AddArray(pdata); + dataset->GetPointData()->AddArray(pdata); + } + + // Face Zones + for + ( + const auto partId + : rangeFaceZones_.intersection(selectedPartIds_) + ) + { + const auto& longName = selectedPartIds_[partId]; + const word zoneName = getFoamName(longName); + + auto iter = cachedVtp_.find(longName); + if (!iter.found() || !iter.object().dataset) + { + // Should not happen, but for safety require a vtk geometry + continue; + } + + foamVtpData& vtpData = iter.object(); + auto dataset = vtpData.dataset; + + const label zoneId = mesh.faceZones().findZoneID(zoneName); + + if (zoneId < 0) + { + continue; + } + + // Extract the field on the zone + Field znfld + ( + pfld.primitiveField(), + mesh.faceZones()[zoneId]().meshPoints() + ); + + vtkSmartPointer pdata = + convertFieldToVTK + ( + fieldName, + znfld + ); + + dataset->GetPointData()->AddArray(pdata); + } } + catch (Foam::IOerror& ioErr) + { + ioErr.write(Warning, false); + Info << nl << endl; + } + catch (Foam::error& err) + { + // Bit of trickery to get the original message + err.write(Warning, false); + Info << nl << endl; + } + + // Restore previous exception throwing state + FatalError.throwExceptions(throwingError); + FatalIOError.throwExceptions(throwingIOerr); } } @@ -735,18 +824,40 @@ void Foam::vtkPVFoam::convertLagrangianFields if (ioobj.headerClassName() == IOField::typeName) { - IOField fld(ioobj); + // Throw FatalError, FatalIOError as exceptions + const bool throwingError = FatalError.throwExceptions(); + const bool throwingIOerr = FatalIOError.throwExceptions(); - vtkSmartPointer data = - convertFieldToVTK - ( - ioobj.name(), - fld - ); + try + { + IOField fld(ioobj); - // Provide identical data as cell and as point data - vtkmesh->GetCellData()->AddArray(data); - vtkmesh->GetPointData()->AddArray(data); + vtkSmartPointer data = + convertFieldToVTK + ( + ioobj.name(), + fld + ); + + // Provide identical data as cell and as point data + vtkmesh->GetCellData()->AddArray(data); + vtkmesh->GetPointData()->AddArray(data); + } + catch (Foam::IOerror& ioErr) + { + ioErr.write(Warning, false); + Info << nl << endl; + } + catch (Foam::error& err) + { + // Bit of trickery to get the original message + err.write(Warning, false); + Info << nl << endl; + } + + // Restore previous exception throwing state + FatalError.throwExceptions(throwingError); + FatalIOError.throwExceptions(throwingIOerr); } } } diff --git a/src/Allwmake b/src/Allwmake index 66e9b561fd..259d52a55d 100755 --- a/src/Allwmake +++ b/src/Allwmake @@ -46,7 +46,6 @@ wmake $targetType genericPatchFields conversion/Allwmake $targetType $* wmake $targetType mesh/extrudeModel wmake $targetType dynamicMesh -wmake $targetType sampling wmake $targetType dynamicFvMesh wmake $targetType sampling wmake $targetType topoChangerFvMesh diff --git a/src/OpenFOAM/db/error/IOerror.C b/src/OpenFOAM/db/error/IOerror.C index 4ddaa2473f..d200c578ab 100644 --- a/src/OpenFOAM/db/error/IOerror.C +++ b/src/OpenFOAM/db/error/IOerror.C @@ -260,34 +260,43 @@ void Foam::IOerror::abort() } -Foam::Ostream& Foam::operator<<(Ostream& os, const IOerror& err) +void Foam::IOerror::write(Ostream& os, const bool includeTitle) const { if (!os.bad()) { - os << nl - << err.title().c_str() << nl - << err.message().c_str() << nl << endl; - - os << "file: " << err.ioFileName().c_str(); - - if (err.ioStartLineNumber() >= 0 && err.ioEndLineNumber() >= 0) + os << nl; + if (includeTitle) { - os << " from line " << err.ioStartLineNumber() - << " to line " << err.ioEndLineNumber() << '.'; + os << title().c_str() << nl; } - else if (err.ioStartLineNumber() >= 0) + os << message().c_str() << nl << endl; + + os << "file: " << ioFileName().c_str(); + + if (ioStartLineNumber() >= 0 && ioEndLineNumber() >= 0) { - os << " at line " << err.ioStartLineNumber() << '.'; + os << " from line " << ioStartLineNumber() + << " to line " << ioEndLineNumber() << '.'; + } + else if (ioStartLineNumber() >= 0) + { + os << " at line " << ioStartLineNumber() << '.'; } - if (IOerror::level >= 2 && err.sourceFileLineNumber()) + if (IOerror::level >= 2 && sourceFileLineNumber()) { os << nl << nl - << " From function " << err.functionName().c_str() << endl - << " in file " << err.sourceFileName().c_str() - << " at line " << err.sourceFileLineNumber() << '.'; + << " From function " << functionName().c_str() << endl + << " in file " << sourceFileName().c_str() + << " at line " << sourceFileLineNumber() << '.'; } } +} + + +Foam::Ostream& Foam::operator<<(Ostream& os, const IOerror& err) +{ + err.write(os); return os; } diff --git a/src/OpenFOAM/db/error/error.H b/src/OpenFOAM/db/error/error.H index 5674df7a69..efb4cefe32 100644 --- a/src/OpenFOAM/db/error/error.H +++ b/src/OpenFOAM/db/error/error.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2015-2017 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2015-2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -320,6 +320,9 @@ public: //- Abort : used to stop code for fatal errors void abort(); + //- Print error message + void write(Ostream& os, const bool includeTitle = true) const; + // Ostream operator