ENH: allow modifiable access to IOobject local(), as per instance()

ENH: simplify code by using fieldTypes::is_xxx() tests
This commit is contained in:
Mark Olesen
2025-08-12 12:34:39 +02:00
parent 891ac808de
commit 1642841868
26 changed files with 95 additions and 128 deletions

View File

@ -42,7 +42,7 @@ using namespace Foam;
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
stringList strings stringList strings
{ ({
"hello", "hello",
"heello", "heello",
"heeello", "heeello",
@ -52,7 +52,7 @@ int main(int argc, char *argv[])
"okey", "okey",
"okkey", "okkey",
"okkkey", "okkkey",
}; });
labelList matches; labelList matches;
wordRes matcher1(ICharStream("( okey \"[hy]e+.*\" )")()); wordRes matcher1(ICharStream("( okey \"[hy]e+.*\" )")());
@ -72,12 +72,14 @@ int main(int argc, char *argv[])
} }
} }
{
regExp matcher(".*ee.*");
Info<< "Match found using ListOps = " Info<< "Match found using ListOps = "
<< ListOps::found(strings, regExp(".*ee.*")) << nl; << ListOps::found_if(strings, matcher) << nl
<< "First index = "
Info<< "First index = " << ListOps::find_if(strings, matcher) << nl;
<< ListOps::find(strings, regExp(".*ee.*")) << nl; }
Info<< endl; Info<< endl;
matches = findMatchingStrings(matcher1, strings); matches = findMatchingStrings(matcher1, strings);

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2023 OpenCFD Ltd. Copyright (C) 2016-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -857,16 +857,8 @@ int main(int argc, char *argv[])
if if
( (
obj.isHeaderClass<volScalarField>() Foam::fieldTypes::is_volume(obj.headerClassName())
|| obj.isHeaderClass<volVectorField>() || Foam::fieldTypes::is_surface(obj.headerClassName())
|| obj.isHeaderClass<volSphericalTensorField>()
|| obj.isHeaderClass<volTensorField>()
|| obj.isHeaderClass<volSymmTensorField>()
|| obj.isHeaderClass<surfaceScalarField>()
|| obj.isHeaderClass<surfaceVectorField>()
|| obj.isHeaderClass<surfaceSphericalTensorField>()
|| obj.isHeaderClass<surfaceSymmTensorField>()
|| obj.isHeaderClass<surfaceTensorField>()
) )
{ {
objects.add(objPtr); objects.add(objPtr);

View File

@ -86,7 +86,7 @@ void constructVolFields(fvMesh& mesh, const vtkUnstructuredReader& reader)
); );
auto& fld = tfld.ref(); auto& fld = tfld.ref();
fld.instance() = mesh.time().timeName(); fld.instance() = mesh.time().timeName();
fld.writeOpt() = IOobject::AUTO_WRITE; fld.writeOpt(IOobject::AUTO_WRITE);
// Fill cell values // Fill cell values
fld.internalFieldRef().field() = fld.internalFieldRef().field() =

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2016-2023 OpenCFD Ltd. Copyright (C) 2016-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -383,29 +383,10 @@ int main(int argc, char *argv[])
if if
( (
io.isHeaderClass<volScalarField>() Foam::fieldTypes::is_volume(io.headerClassName())
|| io.isHeaderClass<volVectorField>() || Foam::fieldTypes::is_internal(io.headerClassName())
|| io.isHeaderClass<volSphericalTensorField>() || Foam::fieldTypes::is_surface(io.headerClassName())
|| io.isHeaderClass<volSymmTensorField>() || Foam::fieldTypes::is_point(io.headerClassName())
|| io.isHeaderClass<volTensorField>()
|| io.isHeaderClass<surfaceScalarField>()
|| io.isHeaderClass<surfaceVectorField>()
|| io.isHeaderClass<surfaceSphericalTensorField>()
|| io.isHeaderClass<surfaceSymmTensorField>()
|| io.isHeaderClass<surfaceTensorField>()
|| io.isHeaderClass<pointScalarField>()
|| io.isHeaderClass<pointVectorField>()
|| io.isHeaderClass<pointSphericalTensorField>()
|| io.isHeaderClass<pointSymmTensorField>()
|| io.isHeaderClass<pointTensorField>()
|| io.isHeaderClass<volScalarField::Internal>()
|| io.isHeaderClass<volVectorField::Internal>()
|| io.isHeaderClass<volSphericalTensorField::Internal>()
|| io.isHeaderClass<volSymmTensorField::Internal>()
|| io.isHeaderClass<volTensorField::Internal>()
) )
{ {
Info<< " Reading " << io.headerClassName() Info<< " Reading " << io.headerClassName()

View File

@ -57,14 +57,7 @@ forAll(meshes, regioni)
if (!doPointValues) if (!doPointValues)
{ {
// Prune point fields if disabled // Prune point fields if disabled
objects.filterClasses objects.filterClasses(Foam::fieldTypes::is_point, true);
(
[](const word& clsName)
{
return fieldTypes::point.found(clsName);
},
true // prune
);
} }
} }

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2018-2023 OpenCFD Ltd. Copyright (C) 2018-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM, distributed under GPL-3.0-or-later. This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
@ -28,8 +28,7 @@ if (doFiniteArea)
autoPtr<faMesh> faMeshPtr; autoPtr<faMesh> faMeshPtr;
const label nAreaFields = const label nAreaFields = faObjects.count(Foam::fieldTypes::is_area);
faObjects.count(stringListOps::foundOp<word>(fieldTypes::area));
if (nAreaFields || withMeshIds) if (nAreaFields || withMeshIds)
{ {

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2018-2022 OpenCFD Ltd. Copyright (C) 2018-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM, distributed under GPL-3.0-or-later. This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
@ -53,10 +53,7 @@ if (doLagrangian)
} }
// Limited to basic IOField types // Limited to basic IOField types
cloudObjs.filterClasses cloudObjs.filterClasses(Foam::fieldTypes::is_basic);
(
stringListOps::foundOp<word>(fieldTypes::basic)
);
// Are there cloud fields (globally)? // Are there cloud fields (globally)?
if (returnReduceAnd(cloudObjs.empty())) if (returnReduceAnd(cloudObjs.empty()))

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2019-2023 OpenCFD Ltd. Copyright (C) 2019-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM, distributed under GPL-3.0-or-later. This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
@ -23,10 +23,7 @@ Description
{ {
using reportFields = foamToVtkReportFields; using reportFields = foamToVtkReportFields;
const label nVolFields = const label nVolFields = objects.count(Foam::fieldTypes::is_volume);
(
objects.count(stringListOps::foundOp<word>(fieldTypes::volume))
);
reportFields::volume(Info, objects); reportFields::volume(Info, objects);

View File

@ -24,21 +24,21 @@ Description
const label nVolFields = const label nVolFields =
( (
(doInternal || doBoundary) (doInternal || doBoundary)
? objects.count(stringListOps::foundOp<word>(fieldTypes::volume)) ? objects.count(Foam::fieldTypes::is_volume)
: 0 : 0
); );
const label nDimFields = const label nDimFields =
( (
(doInternal || doBoundary) (doInternal || doBoundary)
? objects.count(stringListOps::foundOp<word>(fieldTypes::internal)) ? objects.count(Foam::fieldTypes::is_internal)
: 0 : 0
); );
label nPointFields = label nPointFields =
( (
doPointValues doPointValues
? objects.count(stringListOps::foundOp<word>(fieldTypes::point)) ? objects.count(Foam::fieldTypes::is_point)
: 0 : 0
); );

View File

@ -803,14 +803,7 @@ int main(int argc, char *argv[])
if (!doPointValues) if (!doPointValues)
{ {
// Prune point fields if disabled // Prune point fields if disabled
objects.filterClasses objects.filterClasses(Foam::fieldTypes::is_point, true);
(
[](const word& clsName)
{
return fieldTypes::point.found(clsName);
},
true // prune
);
} }
} }

View File

@ -104,16 +104,7 @@ IOobjectList preFilterFields
const IOobject& io = *(iter.val()); const IOobject& io = *(iter.val());
if if (Foam::fieldTypes::is_basic(io.headerClassName()))
(
//OR: fieldTypes::basic.found(io.headerClassName())
io.isHeaderClass<IOField<label>>()
|| io.isHeaderClass<IOField<scalar>>()
|| io.isHeaderClass<IOField<vector>>()
|| io.isHeaderClass<IOField<sphericalTensor>>()
|| io.isHeaderClass<IOField<symmTensor>>()
|| io.isHeaderClass<IOField<tensor>>()
)
{ {
// Transfer from cloudObjects -> filteredObjects // Transfer from cloudObjects -> filteredObjects
filteredObjects.add(cloudObjects.remove(fldName)); filteredObjects.add(cloudObjects.remove(fldName));

View File

@ -647,10 +647,10 @@ void Foam::PDRarrays::addBlockage
const word patchName = word::validate(identifier.substr(0, spc)); const word patchName = word::validate(identifier.substr(0, spc));
patchNum = ListOps::find patchNum = ListOps::find_if
( (
patches, patches,
[=](const PDRpatchDef& p){ return patchName == p.patchName; }, [&](const PDRpatchDef& p){ return patchName == p.patchName; },
1 // skip 0 (blocked face) 1 // skip 0 (blocked face)
); );

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2016-2023 OpenCFD Ltd. Copyright (C) 2016-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -505,7 +505,7 @@ public:
//- Return name of the class name read from header //- Return name of the class name read from header
inline const word& headerClassName() const noexcept; inline const word& headerClassName() const noexcept;
//- Return non-constant access to the class name read from header //- Modifiable access to the class name read from header
inline word& headerClassName() noexcept; inline word& headerClassName() noexcept;
//- Return the optional note //- Return the optional note
@ -572,12 +572,15 @@ public:
//- Read access to instance path component //- Read access to instance path component
inline const fileName& instance() const noexcept; inline const fileName& instance() const noexcept;
//- Write access to instance path component //- Modifiable access to instance path component
inline fileName& instance() noexcept; inline fileName& instance() noexcept;
//- Read access to local path component //- Read access to local path component
inline const fileName& local() const noexcept; inline const fileName& local() const noexcept;
//- Modifiable access to the local path component
inline fileName& local() noexcept;
//- The complete path for the object (with instance, local,...). //- The complete path for the object (with instance, local,...).
fileName path() const; fileName path() const;

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2017-2023 OpenCFD Ltd. Copyright (C) 2017-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -288,6 +288,12 @@ inline const Foam::fileName& Foam::IOobject::local() const noexcept
} }
inline Foam::fileName& Foam::IOobject::local() noexcept
{
return local_;
}
inline Foam::fileName Foam::IOobject::objectPath() const inline Foam::fileName Foam::IOobject::objectPath() const
{ {
return path()/name(); return path()/name();

View File

@ -77,7 +77,7 @@ bool Foam::fieldTypes::is_basic(const word& clsName)
return return
( (
clsName.ends_with("Field") clsName.ends_with("Field")
&& fieldTypes::basic.contains(clsName) && Foam::fieldTypes::basic.contains(clsName)
); );
} }

View File

@ -102,8 +102,8 @@ bool Foam::fieldTypes::is_point(const word& clsName)
{ {
return return
( (
clsName.starts_with("point") // && clsName.ends_with("Field") clsName.starts_with("point") && clsName.ends_with("Field")
&& fieldTypes::point.contains(clsName) && Foam::fieldTypes::point.contains(clsName)
); );
} }

View File

@ -335,25 +335,20 @@ namespace Foam
namespace stringListOps namespace stringListOps
{ {
//- Functor to determine if a string is exists in a list of strings. //- Functor to determine if a string exists in a list of strings.
// For example,
//
// \code
// reduce(text, stringListOps::foundOp<word>(myNames));
// \endcode
template<class StringType> template<class StringType>
struct foundOp struct foundOp
{ {
const UList<StringType>& values; const UList<StringType>& values;
foundOp(const UList<StringType>& list) foundOp(const UList<StringType>& list) noexcept
: :
values(list) values(list)
{} {}
bool operator()(const std::string& text) const bool operator()(const std::string& text) const
{ {
return values.found(text); return values.contains(text);
} }
}; };

View File

@ -162,8 +162,18 @@ bool Foam::fieldTypes::is_area(const word& clsName)
{ {
return return
( (
clsName.starts_with("area") // && clsName.ends_with("Field") clsName.starts_with("area") && clsName.ends_with("Field")
&& fieldTypes::area.contains(clsName) && Foam::fieldTypes::area.contains(clsName)
);
}
bool Foam::fieldTypes::is_area_internal(const word& clsName)
{
return
(
clsName.starts_with("area") && clsName.ends_with("::Internal")
&& Foam::fieldTypes::area_internal.contains(clsName)
); );
} }

View File

@ -96,12 +96,15 @@ namespace fieldTypes
//- Standard area field types (scalar, vector, tensor, etc) //- Standard area field types (scalar, vector, tensor, etc)
extern const wordList area; extern const wordList area;
//- Standard dimensioned field types (scalar, vector, tensor, etc) //- Standard area internal field types (scalar, vector, tensor, etc)
extern const wordList area_internal; extern const wordList area_internal;
//- Test if the class name appears to be an area field //- Test if the class name appears to be an area field
bool is_area(const word& clsName); bool is_area(const word& clsName);
//- Test if the class name appears to be an area internal field
bool is_area_internal(const word& clsName);
} // End namespace fieldTypes } // End namespace fieldTypes
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -100,8 +100,8 @@ bool Foam::fieldTypes::is_surface(const word& clsName)
{ {
return return
( (
clsName.starts_with("surface") // && clsName.ends_with("Field") clsName.starts_with("surface") && clsName.ends_with("Field")
&& fieldTypes::surface.contains(clsName) && Foam::fieldTypes::surface.contains(clsName)
); );
} }

View File

@ -90,7 +90,7 @@ namespace fieldTypes
//- Standard surface field types (scalar, vector, tensor, etc) //- Standard surface field types (scalar, vector, tensor, etc)
extern const wordList surface; extern const wordList surface;
//- Test if the class name appears to be an surface field //- Test if the class name appears to be a surface field
bool is_surface(const word& clsName); bool is_surface(const word& clsName);
} // End namespace fieldTypes } // End namespace fieldTypes

View File

@ -159,12 +159,22 @@ const Foam::wordList Foam::fieldTypes::volume
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
bool Foam::fieldTypes::is_internal(const word& clsName)
{
return
(
clsName.starts_with("vol") && clsName.ends_with("::Internal")
&& Foam::fieldTypes::internal.contains(clsName)
);
}
bool Foam::fieldTypes::is_volume(const word& clsName) bool Foam::fieldTypes::is_volume(const word& clsName)
{ {
return return
( (
clsName.starts_with("vol") // && clsName.ends_with("Field") clsName.starts_with("vol") && clsName.ends_with("Field")
&& fieldTypes::volume.contains(clsName) && Foam::fieldTypes::volume.contains(clsName)
); );
} }

View File

@ -93,12 +93,15 @@ typedef
namespace fieldTypes namespace fieldTypes
{ {
//- Standard dimensioned field types (scalar, vector, tensor, etc) //- Standard volume internal field types (scalar, vector, tensor, etc)
extern const wordList internal; extern const wordList internal;
//- Standard volume field types (scalar, vector, tensor, etc) //- Standard volume field types (scalar, vector, tensor, etc)
extern const wordList volume; extern const wordList volume;
//- Test if the class name appears to be a volume internal field
bool is_internal(const word& clsName);
//- Test if the class name appears to be a volume field //- Test if the class name appears to be a volume field
bool is_volume(const word& clsName); bool is_volume(const word& clsName);

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2019-2023 OpenCFD Ltd. Copyright (C) 2019-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -315,8 +315,8 @@ bool Foam::areaWrite::write()
if if
( (
fieldTypes::area.contains(clsName) Foam::fieldTypes::is_area(clsName)
|| fieldTypes::area_internal.contains(clsName) || Foam::fieldTypes::is_area_internal(clsName)
) )
{ {
nAreaFields += n; nAreaFields += n;

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2017-2022 OpenCFD Ltd. Copyright (C) 2017-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -29,7 +29,7 @@ License
#include "dictionary.H" #include "dictionary.H"
#include "Time.H" #include "Time.H"
#include "areaFields.H" #include "areaFields.H"
#include "stringListOps.H" // For stringListOps::foundOp() #include "volFields.H"
#include "foamVtkInternalWriter.H" #include "foamVtkInternalWriter.H"
#include "foamVtkPatchWriter.H" #include "foamVtkPatchWriter.H"
#include "foamVtkSeriesWriter.H" #include "foamVtkSeriesWriter.H"
@ -332,11 +332,7 @@ bool Foam::functionObjects::vtkWrite::write()
const label nVolFields = const label nVolFields =
( (
(doInternal_ || doBoundary_) (doInternal_ || doBoundary_)
? baseMesh.count ? baseMesh.count(Foam::fieldTypes::is_volume, candidateNames)
(
stringListOps::foundOp<word>(fieldTypes::volume),
candidateNames
)
: 0 : 0
); );
@ -345,11 +341,7 @@ bool Foam::functionObjects::vtkWrite::write()
const label nDimFields = 0; const label nDimFields = 0;
// ( // (
// (doInternal_ || doBoundary_) // (doInternal_ || doBoundary_)
// ? baseMesh.count // ? baseMesh.count(Foam::fieldTypes::is_internal, candidateNames)
// (
// stringListOps::foundOp<word>(fieldTypes::internal),
// candidateNames
// )
// : 0 // : 0
// ); // );

View File

@ -127,11 +127,11 @@ Foam::IOobjectList Foam::sampledSurfaces::preCheckFields()
const word& clsName = iter.key(); const word& clsName = iter.key();
const label n = iter.val().size(); const label n = iter.val().size();
if (fieldTypes::volume.contains(clsName)) if (Foam::fieldTypes::is_volume(clsName))
{ {
nVolumeFields += n; nVolumeFields += n;
} }
else if (fieldTypes::surface.contains(clsName)) else if (Foam::fieldTypes::is_surface(clsName))
{ {
nSurfaceFields += n; nSurfaceFields += n;
} }