From 0352a224b746c70210f05e69c02c852e82991970 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 15 Dec 2023 11:44:41 +0100 Subject: [PATCH] COMP: access transformedElements by reference, not copy - better code style and seems to avoid triggering a gcc warning about possibly uninitialized values COMP: JSONformatter writeEntry missing a return value STYLE: accept 'json' for checkMesh write format - consistent with caseInfo functionObject --- .../manipulation/checkMesh/writeMeshChecks.H | 8 +-- .../mapPolyMesh/mapDistribute/mapDistribute.C | 66 +++++++++---------- .../mapDistribute/mapDistributeBase.C | 6 +- .../globalIndexAndTransform.H | 11 ++-- src/fileFormats/json/JSONformatter.C | 2 +- src/fileFormats/json/JSONformatter.H | 10 +-- 6 files changed, 49 insertions(+), 54 deletions(-) diff --git a/applications/utilities/mesh/manipulation/checkMesh/writeMeshChecks.H b/applications/utilities/mesh/manipulation/checkMesh/writeMeshChecks.H index aaab11816d..deb0a7c587 100644 --- a/applications/utilities/mesh/manipulation/checkMesh/writeMeshChecks.H +++ b/applications/utilities/mesh/manipulation/checkMesh/writeMeshChecks.H @@ -8,6 +8,7 @@ const Enum writeChecksFormatTypeNames { { writeChecksFormatType::none, "none" }, { writeChecksFormatType::dictionary, "dictionary" }, + { writeChecksFormatType::JSON, "json" }, { writeChecksFormatType::JSON, "JSON" }, }; @@ -15,7 +16,7 @@ writeChecksFormatType writeChecksFormat(writeChecksFormatType::none); auto writeMeshChecks = [](const fvMesh& mesh, const writeChecksFormatType fmt) { - if (Pstream::master()) + if (UPstream::master()) { switch (fmt) { @@ -33,15 +34,14 @@ auto writeMeshChecks = [](const fvMesh& mesh, const writeChecksFormatType fmt) ) ); + Info<< "Writing mesh data to " << os.name() << nl << endl; + data.writeHeader(os); mesh.data().meshDict().write(os, false); IOobject::writeEndDivider(os); - Info<< "Writing mesh data to " << data.objectPath() - << nl << endl; - break; } case writeChecksFormatType::JSON: diff --git a/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapDistribute/mapDistribute.C b/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapDistribute/mapDistribute.C index 4b93907c78..1a8cc8ea39 100644 --- a/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapDistribute/mapDistribute.C +++ b/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapDistribute/mapDistribute.C @@ -154,8 +154,8 @@ Foam::UPtrList Foam::mapDistribute::extractBase UPtrList baseMaps(maps.size()); forAll(maps, i) { - const mapDistributeBase& map = maps[i]; - baseMaps.set(i, &map); + // Implicit cast to + baseMaps.set(i, maps.get(i)); } return baseMaps; } @@ -250,15 +250,13 @@ Foam::mapDistribute::mapDistribute ); // Add all (non-local) transformed elements needed. - forAll(transformedElements, i) + for (const labelPair& elem : transformedElements) { - labelPair elem = transformedElements[i]; label proci = globalTransforms.processor(elem); if (proci != myRank) { label index = globalTransforms.index(elem); - label nCompact = compactMap[proci].size(); - compactMap[proci].insert(index, nCompact); + compactMap[proci].insert(index, compactMap[proci].size()); } } @@ -279,31 +277,32 @@ Foam::mapDistribute::mapDistribute // Renumber the transformed elements // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Count per transformIndex - label nTrafo = globalTransforms.transformPermutations().size(); + const label nTrafo = globalTransforms.transformPermutations().size(); labelList nPerTransform(nTrafo, Zero); - forAll(transformedElements, i) + for (const labelPair& elem : transformedElements) { - labelPair elem = transformedElements[i]; label trafoI = globalTransforms.transformIndex(elem); nPerTransform[trafoI]++; } // Offset per transformIndex - transformStart_.setSize(nTrafo); - transformElements_.setSize(nTrafo); + transformStart_.resize_nocopy(nTrafo); + transformElements_.resize_nocopy(nTrafo); forAll(transformStart_, trafoI) { + const label count = nPerTransform[trafoI]; + transformStart_[trafoI] = constructSize(); - constructSize() += nPerTransform[trafoI]; - transformElements_[trafoI].setSize(nPerTransform[trafoI]); + transformElements_[trafoI].resize_nocopy(count); + constructSize() += count; } // Sort transformed elements into their new slot. nPerTransform = 0; - transformedIndices.setSize(transformedElements.size()); + transformedIndices.resize_nocopy(transformedElements.size()); forAll(transformedElements, i) { - labelPair elem = transformedElements[i]; + const labelPair& elem = transformedElements[i]; label proci = globalTransforms.processor(elem); label index = globalTransforms.index(elem); label trafoI = globalTransforms.transformIndex(elem); @@ -358,18 +357,15 @@ Foam::mapDistribute::mapDistribute ); // Add all (non-local) transformed elements needed. - forAll(transformedElements, celli) + for (const labelPairList& elems : transformedElements) { - const labelPairList& elems = transformedElements[celli]; - - forAll(elems, i) + for (const labelPair& elem : elems) { - label proci = globalTransforms.processor(elems[i]); + label proci = globalTransforms.processor(elem); if (proci != myRank) { - label index = globalTransforms.index(elems[i]); - label nCompact = compactMap[proci].size(); - compactMap[proci].insert(index, nCompact); + label index = globalTransforms.index(elem); + compactMap[proci].insert(index, compactMap[proci].size()); } } } @@ -391,36 +387,36 @@ Foam::mapDistribute::mapDistribute // Renumber the transformed elements // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Count per transformIndex - label nTrafo = globalTransforms.transformPermutations().size(); + const label nTrafo = globalTransforms.transformPermutations().size(); labelList nPerTransform(nTrafo, Zero); - forAll(transformedElements, celli) + for (const labelPairList& elems : transformedElements) { - const labelPairList& elems = transformedElements[celli]; - - forAll(elems, i) + for (const labelPair& elem : elems) { - label trafoI = globalTransforms.transformIndex(elems[i]); + label trafoI = globalTransforms.transformIndex(elem); nPerTransform[trafoI]++; } } // Offset per transformIndex - transformStart_.setSize(nTrafo); - transformElements_.setSize(nTrafo); + transformStart_.resize_nocopy(nTrafo); + transformElements_.resize_nocopy(nTrafo); forAll(transformStart_, trafoI) { + const label count = nPerTransform[trafoI]; + transformStart_[trafoI] = constructSize(); - constructSize() += nPerTransform[trafoI]; - transformElements_[trafoI].setSize(nPerTransform[trafoI]); + transformElements_[trafoI].resize_nocopy(count); + constructSize() += count; } // Sort transformed elements into their new slot. nPerTransform = 0; - transformedIndices.setSize(transformedElements.size()); + transformedIndices.resize_nocopy(transformedElements.size()); forAll(transformedElements, celli) { const labelPairList& elems = transformedElements[celli]; - transformedIndices[celli].setSize(elems.size()); + transformedIndices[celli].resize_nocopy(elems.size()); forAll(elems, i) { diff --git a/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapDistribute/mapDistributeBase.C b/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapDistribute/mapDistributeBase.C index 0f2d4347c0..42905a08d1 100644 --- a/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapDistribute/mapDistributeBase.C +++ b/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapDistribute/mapDistributeBase.C @@ -425,8 +425,7 @@ void Foam::mapDistributeBase::calcCompactAddressing { label proci = globalNumbering.whichProcID(myRank, globalIdx); label index = globalNumbering.toLocal(proci, globalIdx); - label nCompact = compactMap[proci].size(); - compactMap[proci].insert(index, nCompact); + compactMap[proci].insert(index, compactMap[proci].size()); } } } @@ -478,8 +477,7 @@ void Foam::mapDistributeBase::calcCompactAddressing { label proci = globalNumbering.whichProcID(myRank, globalIdx); label index = globalNumbering.toLocal(proci, globalIdx); - label nCompact = compactMap[proci].size(); - compactMap[proci].insert(index, nCompact); + compactMap[proci].insert(index, compactMap[proci].size()); } } } diff --git a/src/OpenFOAM/primitives/globalIndexAndTransform/globalIndexAndTransform.H b/src/OpenFOAM/primitives/globalIndexAndTransform/globalIndexAndTransform.H index 964e414606..36287c7051 100644 --- a/src/OpenFOAM/primitives/globalIndexAndTransform/globalIndexAndTransform.H +++ b/src/OpenFOAM/primitives/globalIndexAndTransform/globalIndexAndTransform.H @@ -43,8 +43,8 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef globalIndexAndTransform_H -#define globalIndexAndTransform_H +#ifndef Foam_globalIndexAndTransform_H +#define Foam_globalIndexAndTransform_H #include "labelPair.H" #include "vectorTensorTransform.H" @@ -55,9 +55,9 @@ SourceFiles namespace Foam { +// Forward Declarations class polyMesh; - /*---------------------------------------------------------------------------*\ Class globalIndexAndTransform Declaration \*---------------------------------------------------------------------------*/ @@ -66,7 +66,7 @@ class globalIndexAndTransform { public: - // Public classes + // Public Classes //- Less function class used in sorting encoded transforms and indices // Minimum of: @@ -168,7 +168,7 @@ public: // Constructors //- Construct from components - globalIndexAndTransform(const polyMesh& mesh); + explicit globalIndexAndTransform(const polyMesh& mesh); // Member Functions @@ -282,7 +282,6 @@ public: const labelHashSet& patchIs, const point& pt ) const; - }; diff --git a/src/fileFormats/json/JSONformatter.C b/src/fileFormats/json/JSONformatter.C index 9b4b9b0ba0..0f936b738e 100644 --- a/src/fileFormats/json/JSONformatter.C +++ b/src/fileFormats/json/JSONformatter.C @@ -130,7 +130,7 @@ Foam::Ostream& Foam::JSONformatter::writeDict(const dictionary& dict) const word& keyword = e.keyword(); os_ << indent; - writeKeyword(keyword) << " : "; + os_.writeQuoted(keyword) << " : "; if (e.isDict()) { diff --git a/src/fileFormats/json/JSONformatter.H b/src/fileFormats/json/JSONformatter.H index d704b035a8..7ba526e991 100644 --- a/src/fileFormats/json/JSONformatter.H +++ b/src/fileFormats/json/JSONformatter.H @@ -82,7 +82,7 @@ public: virtual Ostream& write(const bool val); virtual Ostream& write(const int32_t val); virtual Ostream& write(const int64_t val); - virtual Ostream& write(const float val ); + virtual Ostream& write(const float val); virtual Ostream& write(const double val); virtual Ostream& write(const word& str); virtual Ostream& write(const std::string& str); @@ -94,11 +94,13 @@ public: //- Write OpenFOAM dictionary to JSON dictionary virtual Ostream& writeDict(const dictionary& dict); - //- Write JSON keyword-value pair + //- Write JSON keyword-value pair (for primitive types) template - Ostream& writeEntry(const word& keyword, Type val) + Ostream& writeEntry(const word& keyword, const Type& val) { - writeKeyword(keyword) << " : " << write(val); + os_.writeQuoted(keyword) << " : "; + write(val); + return os_; } };