From a033b7276ff4f3f9fb1e4242c6d88e8376f9820b Mon Sep 17 00:00:00 2001 From: Will Bainbridge Date: Tue, 25 Jan 2022 09:58:08 +0000 Subject: [PATCH] functionObjects: writeVTK: Fixes The lookup been fixed to prevent failures when a field is looked up with the wrong type, and it now also provides warnings when a field cannot be found for any type. --- .../foamToVTK/foamToVTK/writeVTK/writeVTK.C | 58 ++++++++++++++----- .../foamToVTK/foamToVTK/writeVTK/writeVTK.H | 8 +-- .../foamToVTK/writeVTK/writeVTKTemplates.C | 50 ---------------- 3 files changed, 44 insertions(+), 72 deletions(-) delete mode 100644 applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK/writeVTK/writeVTKTemplates.C diff --git a/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK/writeVTK/writeVTK.C b/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK/writeVTK/writeVTK.C index aa19220476..cab167ad75 100644 --- a/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK/writeVTK/writeVTK.C +++ b/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK/writeVTK/writeVTK.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2016-2021 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2016-2022 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -128,32 +128,60 @@ bool Foam::functionObjects::writeVTK::write() // Write mesh internalWriter writer(vMesh, false, vtkFileName); - UPtrList vsf(lookupFields()); - UPtrList vvf(lookupFields()); - UPtrList vsptf - ( - lookupFields() - ); - UPtrList vstf(lookupFields()); - UPtrList vtf(lookupFields()); + // Declare UPtrLists to the volFields that are to be written + #define DeclareTypeFields(Type, nullArg) \ + UPtrList> Type##Fields; + FOR_ALL_FIELD_TYPES(DeclareTypeFields); + #undef DeclareTypeFields + + // Look up the volFields and store the pointers + label nFields = 0; + forAll(objectNames_, i) + { + bool objectFound = false; + + #define SetTypeFields(Type, nullarg) \ + { \ + if (obr_.foundObject>(objectNames_[i])) \ + { \ + const VolField& field = \ + obr_.lookupObject>(objectNames_[i]); \ + \ + Type##Fields.resize(Type##Fields.size() + 1); \ + Type##Fields.set(Type##Fields.size() - 1, &field); \ + \ + Info<< " Writing " << VolField::typeName \ + << " field " << field.name() << endl; \ + \ + nFields ++; \ + objectFound = true; \ + } \ + } + FOR_ALL_FIELD_TYPES(SetTypeFields); + #undef SetTypeFields + + if (!objectFound) + { + cannotFindObject(objectNames_[i]); + } + } // Write header for cellID and volFields vtkWriteOps::writeCellDataHeader ( writer.os(), vMesh.nFieldCells(), - 1 + vsf.size() + vvf.size() + vsptf.size() + vstf.size() + vtf.size() + 1 + nFields ); // Write cellID field writer.writeCellIDs(); // Write volFields - writer.write(vsf); - writer.write(vvf); - writer.write(vsptf); - writer.write(vstf); - writer.write(vtf); + #define WriteTypeFields(Type, nullArg) \ + writer.write(Type##Fields); + FOR_ALL_FIELD_TYPES(WriteTypeFields); + #undef WriteTypeFields return true; } diff --git a/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK/writeVTK/writeVTK.H b/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK/writeVTK/writeVTK.H index 74912cb141..e0e495d06e 100644 --- a/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK/writeVTK/writeVTK.H +++ b/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK/writeVTK/writeVTK.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2016-2021 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2016-2022 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -145,12 +145,6 @@ public: // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -#ifdef NoRepository - #include "writeVTKTemplates.C" -#endif - -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - #endif // ************************************************************************* // diff --git a/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK/writeVTK/writeVTKTemplates.C b/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK/writeVTK/writeVTKTemplates.C deleted file mode 100644 index 4f135eb555..0000000000 --- a/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK/writeVTK/writeVTKTemplates.C +++ /dev/null @@ -1,50 +0,0 @@ -/*---------------------------------------------------------------------------*\ - ========= | - \\ / F ield | OpenFOAM: The Open Source CFD Toolbox - \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2016-2021 OpenFOAM Foundation - \\/ M anipulation | -------------------------------------------------------------------------------- -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 "writeVTK.H" -#include "objectRegistry.H" -#include "DynamicList.H" - -// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // - -template -Foam::UPtrList -Foam::functionObjects::writeVTK::lookupFields() const -{ - UPtrList fields(objectNames_.size()); - - forAll(objectNames_, i) - { - const GeoField& field = obr_.lookupObject(objectNames_[i]); - Info<< " Writing " << GeoField::typeName - << " field " << field.name() << endl; - fields.set(i, &field); - } - - return fields; -} - - -// ************************************************************************* //