From 4495db0302a7122860d65d778d4bf3822f5d7b77 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Wed, 16 Mar 2022 14:06:46 +0100 Subject: [PATCH] ENH: avoid redundant IOobjectList use - areaWrite and fileFieldSelection --- .../fileFieldSelection/fileFieldSelection.C | 100 ++++++++++++++---- .../fileFieldSelection/fileFieldSelection.H | 71 +++++++------ .../fileFieldSelectionTemplates.C | 83 --------------- .../utilities/areaWrite/areaWrite.C | 5 +- 4 files changed, 119 insertions(+), 140 deletions(-) delete mode 100644 src/finiteVolume/functionObjects/fieldSelections/fileFieldSelection/fileFieldSelectionTemplates.C diff --git a/src/finiteVolume/functionObjects/fieldSelections/fileFieldSelection/fileFieldSelection.C b/src/finiteVolume/functionObjects/fieldSelections/fileFieldSelection/fileFieldSelection.C index cc6a45699c..308f4c802a 100644 --- a/src/finiteVolume/functionObjects/fieldSelections/fileFieldSelection/fileFieldSelection.C +++ b/src/finiteVolume/functionObjects/fieldSelections/fileFieldSelection/fileFieldSelection.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2017-2019 OpenCFD Ltd. + Copyright (C) 2017-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -27,45 +27,98 @@ License #include "fileFieldSelection.H" #include "objectRegistry.H" +#include "IOobjectList.H" +#include "fvMesh.H" #include "volMesh.H" #include "fvPatchField.H" #include "surfaceMesh.H" #include "fvsPatchField.H" #include "pointMesh.H" #include "pointPatchField.H" +#include "GeometricField.H" #include "UniformDimensionedField.H" -void Foam::functionObjects::fileFieldSelection::addInternalFieldTypes +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // + +template +void Foam::functionObjects::fileFieldSelection::addFromFile ( + const IOobjectList& objects, DynamicList& set ) const { - const fvMesh& mesh = static_cast(obr_); + for (const fieldInfo& fi : *this) + { + const wordList names(objects.sortedNames(fi.name())); - const IOobjectList allObjects(mesh, mesh.time().timeName()); + if (names.size()) + { + for (const word& name : names) + { + set.append(fieldInfo(wordRe(name))); + } - addFromFile>(allObjects, set); - addFromFile>(allObjects, set); - addFromFile>(allObjects, set); - addFromFile>(allObjects, set); - addFromFile>(allObjects, set); + fi.found() = true; + } + } +} + + +template class PatchType, class MeshType> +void Foam::functionObjects::fileFieldSelection::addGeoFieldTypes +( + const IOobjectList& objects, + DynamicList& set +) const +{ + #undef doLocalCode + #define doLocalCode(DataType) \ + addFromFile>(objects, set); + + doLocalCode(scalar); + doLocalCode(vector); + doLocalCode(sphericalTensor); + doLocalCode(symmTensor); + doLocalCode(tensor); + #undef doLocalCode +} + + +void Foam::functionObjects::fileFieldSelection::addInternalFieldTypes +( + const IOobjectList& objects, + DynamicList& set +) const +{ + #undef doLocalCode + #define doLocalCode(DataType) \ + addFromFile>(objects, set); + + doLocalCode(scalar); + doLocalCode(vector); + doLocalCode(sphericalTensor); + doLocalCode(symmTensor); + doLocalCode(tensor); + #undef doLocalCode } void Foam::functionObjects::fileFieldSelection::addUniformFieldTypes ( + const IOobjectList& objects, DynamicList& set ) const { - const fvMesh& mesh = static_cast(obr_); + #undef doLocalCode + #define doLocalCode(DataType) \ + addFromFile>(objects, set); - const IOobjectList allObjects(mesh, mesh.time().timeName()); - - addFromFile>(allObjects, set); - addFromFile>(allObjects, set); - addFromFile>(allObjects, set); - addFromFile>(allObjects, set); - addFromFile>(allObjects, set); + doLocalCode(scalar); + doLocalCode(vector); + doLocalCode(sphericalTensor); + doLocalCode(symmTensor); + doLocalCode(tensor); + #undef doLocalCode } @@ -85,20 +138,23 @@ Foam::functionObjects::fileFieldSelection::fileFieldSelection bool Foam::functionObjects::fileFieldSelection::updateSelection() { + const fvMesh& mesh = static_cast(obr_); + const IOobjectList objects(mesh, mesh.time().timeName()); + List oldSet(std::move(selection_)); DynamicList newSelection(oldSet.size()); // Geometric fields - addGeoFieldTypes(newSelection); - addGeoFieldTypes(newSelection); - addGeoFieldTypes(newSelection); + addGeoFieldTypes(objects, newSelection); + addGeoFieldTypes(objects, newSelection); + addGeoFieldTypes(objects, newSelection); // Internal fields - addInternalFieldTypes(newSelection); + addInternalFieldTypes(objects, newSelection); // Uniform fields - addUniformFieldTypes(newSelection); + addUniformFieldTypes(objects, newSelection); selection_.transfer(newSelection); diff --git a/src/finiteVolume/functionObjects/fieldSelections/fileFieldSelection/fileFieldSelection.H b/src/finiteVolume/functionObjects/fieldSelections/fileFieldSelection/fileFieldSelection.H index 28b0756d21..50d4d09df3 100644 --- a/src/finiteVolume/functionObjects/fieldSelections/fileFieldSelection/fileFieldSelection.H +++ b/src/finiteVolume/functionObjects/fieldSelections/fileFieldSelection/fileFieldSelection.H @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2017-2019 OpenCFD Ltd. + Copyright (C) 2017-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -44,6 +44,7 @@ SourceFiles namespace Foam { +// Forward Declarations class IOobjectList; namespace functionObjects @@ -57,45 +58,53 @@ class fileFieldSelection : public fieldSelection { -private: - // Private Member Functions - //- No copy construct - fileFieldSelection(const fileFieldSelection&) = delete; - - -protected: - - // Protected Member Functions - - //- Add registered GeometricField types to selection - template class PatchType, class MeshType> - void addGeoFieldTypes(DynamicList& set) const; - - //- Add registered Internal types to selection - void addInternalFieldTypes(DynamicList& set) const; - - //- Add registered uniform types to selection - void addUniformFieldTypes(DynamicList& set) const; - //- Add objects of a given type template void addFromFile ( - const IOobjectList& allFileObjects, + const IOobjectList& objects, + DynamicList& set + ) const; + + //- Add registered GeometricField types to selection + template class PatchType, class MeshType> + void addGeoFieldTypes + ( + const IOobjectList& objects, + DynamicList& set + ) const; + + //- Add registered Internal types to selection + void addInternalFieldTypes + ( + const IOobjectList& objects, + DynamicList& set + ) const; + + //- Add registered uniform types to selection + void addUniformFieldTypes + ( + const IOobjectList& objects, DynamicList& set ) const; + //- No copy construct + fileFieldSelection(const fileFieldSelection&) = delete; + + public: - //- Construct from object registry - fileFieldSelection - ( - const objectRegistry& obr, - const bool includeComponents = false - ); + // Constructors + + //- Construct from object registry + explicit fileFieldSelection + ( + const objectRegistry& obr, + const bool includeComponents = false + ); //- Destructor @@ -116,12 +125,6 @@ public: // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -#ifdef NoRepository - #include "fileFieldSelectionTemplates.C" -#endif - -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - #endif // ************************************************************************* // diff --git a/src/finiteVolume/functionObjects/fieldSelections/fileFieldSelection/fileFieldSelectionTemplates.C b/src/finiteVolume/functionObjects/fieldSelections/fileFieldSelection/fileFieldSelectionTemplates.C deleted file mode 100644 index 12801b87b2..0000000000 --- a/src/finiteVolume/functionObjects/fieldSelections/fileFieldSelection/fileFieldSelectionTemplates.C +++ /dev/null @@ -1,83 +0,0 @@ -/*---------------------------------------------------------------------------*\ - ========= | - \\ / F ield | OpenFOAM: The Open Source CFD Toolbox - \\ / O peration | - \\ / A nd | www.openfoam.com - \\/ M anipulation | -------------------------------------------------------------------------------- - Copyright (C) 2017 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 "IOobjectList.H" -#include "GeometricField.H" -#include "fvMesh.H" - -// * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * // - -template -void Foam::functionObjects::fileFieldSelection::addFromFile -( - const IOobjectList& allFileObjects, - DynamicList& set -) const -{ - for (const fieldInfo& fi : *this) - { - const wordList names(allFileObjects.names(Type::typeName, fi.name())); - if (names.size()) - { - for (const word& name : names) - { - set.append(fieldInfo(wordRe(name))); - } - - fi.found() = true; - } - } -} - - -template class PatchType, class MeshType> -void Foam::functionObjects::fileFieldSelection::addGeoFieldTypes -( - DynamicList& set -) const -{ - const fvMesh& mesh = static_cast(obr_); - - const IOobjectList allObjects(mesh, mesh.time().timeName()); - - addFromFile>(allObjects, set); - addFromFile>(allObjects, set); - addFromFile> - ( - allObjects, - set - ); - addFromFile> - ( - allObjects, - set - ); - addFromFile>(allObjects, set); -} - - -// ************************************************************************* // diff --git a/src/functionObjects/utilities/areaWrite/areaWrite.C b/src/functionObjects/utilities/areaWrite/areaWrite.C index 50824c226f..6c4a7bdaf3 100644 --- a/src/functionObjects/utilities/areaWrite/areaWrite.C +++ b/src/functionObjects/utilities/areaWrite/areaWrite.C @@ -249,10 +249,13 @@ bool Foam::areaWrite::write() selected.clear(); - const IOobjectList objects(areaMesh.thisDb(), obr_.time().timeName()); + IOobjectList objects(0); if (loadFromFiles_) { + // Check files for a particular time + objects = IOobjectList(areaMesh.thisDb(), obr_.time().timeName()); + allFields = objects.names(); selected = objects.classes(fieldSelection_); }