ENH: use objectRegistry/IOobjectList sorted instead of lookupClass

- in most cases a parallel-consistent order is required.
  Even when the order is not important, it will generally require
  fewer allocations to create a UPtrList of entries instead of a
  HashTable or even a wordList.
This commit is contained in:
Mark Olesen
2023-07-17 18:27:55 +02:00
parent d65e2d89b5
commit db16d80840
70 changed files with 1300 additions and 1758 deletions

View File

@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -105,8 +106,9 @@ int main(int argc, char *argv[])
"foamDataToFluentDict",
runTime.system(),
mesh,
IOobject::MUST_READ_IF_MODIFIED,
IOobject::NO_WRITE
IOobjectOption::MUST_READ,
IOobjectOption::NO_WRITE,
IOobjectOption::NO_REGISTER
)
);
@ -115,44 +117,21 @@ int main(int argc, char *argv[])
IOobjectList objects(mesh, runTime.timeName());
// volScalarField
for (const word& fieldName : objects.sortedNames<volScalarField>())
{
// Lookup field from dictionary and convert field
label unitNumber;
if
(
foamDataToFluentDict.readIfPresent(fieldName, unitNumber)
&& unitNumber > 0
)
{
// Read field
volScalarField field(*(objects[fieldName]), mesh);
readFieldsAndWriteFluent<volScalarField>
(
foamDataToFluentDict,
objects,
mesh,
fluentDataFile
);
Info<< " Converting field " << fieldName << nl;
writeFluentField(field, unitNumber, fluentDataFile);
}
}
// volVectorField
for (const word& fieldName : objects.sortedNames<volVectorField>())
{
// Lookup field from dictionary and convert field
label unitNumber;
if
(
foamDataToFluentDict.readIfPresent(fieldName, unitNumber)
&& unitNumber > 0
)
{
// Read field
volVectorField field(*(objects[fieldName]), mesh);
Info<< " Converting field " << fieldName << nl;
writeFluentField(field, unitNumber, fluentDataFile);
}
}
readFieldsAndWriteFluent<volVectorField>
(
foamDataToFluentDict,
objects,
mesh,
fluentDataFile
);
Info<< endl;
}

View File

@ -28,10 +28,12 @@ InClass
\*---------------------------------------------------------------------------*/
#ifndef writeFluentFields_H
#define writeFluentFields_H
#ifndef Foam_writeFluentFields_H
#define Foam_writeFluentFields_H
#include "volFields.H"
#include "dictionary.H"
#include "IOobjectList.H"
#include "Ostream.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -43,16 +45,48 @@ void writeFluentField
(
const volScalarField& phi,
const label fluentFieldIdentifier,
Ostream& stream
Ostream& os
);
void writeFluentField
(
const volVectorField& phi,
const label fluentFieldIdentifier,
Ostream& stream
Ostream& os
);
template<class GeoField>
void readFieldsAndWriteFluent
(
const dictionary& dict,
const IOobjectList& objects,
const fvMesh& mesh,
Ostream& os
)
{
for (const IOobject& io : objects.csorted<GeoField>())
{
// Lookup field from dictionary and convert field
const word& fieldName = io.name();
label unitNumber;
if
(
dict.readIfPresent(fieldName, unitNumber)
&& unitNumber > 0
)
{
// Read field
GeoField field(io, mesh);
Info<< " Converting field " << fieldName << nl;
writeFluentField(field, unitNumber, os);
}
}
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam

View File

@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -35,12 +36,7 @@ Description
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
void writeFluentField
void Foam::writeFluentField
(
const volScalarField& phi,
const label fluentFieldIdentifier,
@ -58,15 +54,15 @@ void writeFluentField
<< "1 " // Number of components (scalar=1, vector=3)
<< "0 0 " // Unused
<< "1 " << phiInternal.size() // Start and end of list
<< ")(" << endl;
<< ")(" << nl;
forAll(phiInternal, celli)
for (const scalar val : phiInternal)
{
stream << phiInternal[celli] << endl;
stream << val << nl;
}
stream
<< "))" << endl;
<< "))" << nl;
label nWrittenFaces = phiInternal.size();
@ -92,13 +88,13 @@ void writeFluentField
<< "0 0 " // Unused
<< nWrittenFaces + 1 << " "
<< nWrittenFaces + emptyFaceCells.size()// Start and end of list
<< ")(" << endl;
<< ")(" << nl;
nWrittenFaces += emptyFaceCells.size();
forAll(emptyFaceCells, facei)
{
stream << phiInternal[emptyFaceCells[facei]] << endl;
stream << phiInternal[emptyFaceCells[facei]] << nl;
}
stream
@ -120,13 +116,13 @@ void writeFluentField
<< "0 0 " // Unused
<< nWrittenFaces + 1 << " " << nWrittenFaces + patchPhi.size()
// Start and end of list
<< ")(" << endl;
<< ")(" << nl;
nWrittenFaces += patchPhi.size();
forAll(patchPhi, facei)
for (const scalar val : patchPhi)
{
stream << patchPhi[facei] << endl;
stream << val << nl;
}
stream
@ -136,8 +132,4 @@ void writeFluentField
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// ************************************************************************* //

View File

@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -34,12 +35,7 @@ Description
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
void writeFluentField
void Foam::writeFluentField
(
const volVectorField& phi,
const label fluentFieldIdentifier,
@ -57,19 +53,16 @@ void writeFluentField
<< "3 " // Number of components (scalar=1, vector=3)
<< "0 0 " // Unused
<< "1 " << phiInternal.size() // Start and end of list
<< ")(" << endl;
<< ")(" << nl;
forAll(phiInternal, celli)
for (const vector& val : phiInternal)
{
stream
<< phiInternal[celli].x() << " "
<< phiInternal[celli].y() << " "
<< phiInternal[celli].z() << " "
<< endl;
<< val.x() << ' ' << val.y() << ' ' << val.z() << nl;
}
stream
<< "))" << endl;
<< "))" << nl;
label nWrittenFaces = phiInternal.size();
@ -87,17 +80,14 @@ void writeFluentField
<< "0 0 " // Unused
<< nWrittenFaces + 1 << " " << nWrittenFaces + patchPhi.size()
// Start and end of list
<< ")(" << endl;
<< ")(" << nl;
nWrittenFaces += patchPhi.size();
forAll(patchPhi, facei)
for (const vector& val : patchPhi)
{
stream
<< patchPhi[facei].x() << " "
<< patchPhi[facei].y() << " "
<< patchPhi[facei].z() << " "
<< endl;
<< val.x() << ' ' << val.y() << ' ' << val.z() << nl;
}
stream
@ -106,6 +96,4 @@ void writeFluentField
}
} // End namespace Foam
// ************************************************************************* //

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2021-2022 OpenCFD Ltd.
Copyright (C) 2021-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
@ -28,9 +28,9 @@ forAll(meshes, regioni)
IOobjectList objects(0);
if (doConvertFields)
if (doConvertFields && !timeDirs.empty())
{
objects = IOobjectList(mesh, timeDirs.last().name());
objects = IOobjectList(mesh, timeDirs.back().name());
if (fieldSelector && !fieldSelector().empty())
{

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018-2022 OpenCFD Ltd.
Copyright (C) 2018-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -35,18 +35,30 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef readFields_H
#define readFields_H
#ifndef ensight_readFields_H
#define ensight_readFields_H
#include "instantList.H"
#include "IOobjectList.H"
#include "zeroGradientFvPatchFields.H"
#include "fvPatchField.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
//- Get the field or FatalError
template<class GeoField>
tmp<GeoField> getField
(
const IOobject& io,
const typename GeoField::Mesh& mesh
)
{
return tmp<GeoField>::New(io, mesh);
}
//- Get the field or return nullptr
template<class GeoField>
tmp<GeoField> getField

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2021-2022 OpenCFD Ltd.
Copyright (C) 2021-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
@ -72,7 +72,7 @@ label writeAreaFields
label count = 0;
for (const word& fieldName : objects.sortedNames<FieldType>())
for (const IOobject& io : objects.csorted<FieldType>())
{
if
(
@ -80,11 +80,11 @@ label writeAreaFields
(
ensCase,
ensMesh,
getField<FieldType>(objects.findObject(fieldName), mesh)
getField<FieldType>(io, mesh)
)
)
{
Info<< ' ' << fieldName;
Info<< ' ' << io.name();
++count;
}
}

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018-2022 OpenCFD Ltd.
Copyright (C) 2018-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
@ -69,7 +69,7 @@ label writeDimFields
label count = 0;
for (const word& fieldName : objects.sortedNames<FieldType>())
for (const IOobject& io : objects.csorted<FieldType>())
{
if
(
@ -77,11 +77,11 @@ label writeDimFields
(
ensCase,
ensMesh,
getField<FieldType>(objects.findObject(fieldName), mesh)
getField<FieldType>(io, mesh)
)
)
{
Info<< ' ' << fieldName;
Info<< ' ' << io.name();
++count;
}
}

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2020-2022 OpenCFD Ltd.
Copyright (C) 2020-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
@ -70,11 +70,11 @@ label writePointFields
{
typedef PointField<Type> FieldType;
const pointMesh& ptMesh = pointMesh::New(ensMesh.mesh());
const pointMesh& pMesh = pointMesh::New(ensMesh.mesh());
label count = 0;
for (const word& fieldName : objects.sortedNames<FieldType>())
for (const IOobject& io : objects.csorted<FieldType>())
{
if
(
@ -82,11 +82,11 @@ label writePointFields
(
ensCase,
ensMesh,
getField<FieldType>(ptMesh, objects, fieldName)
getField<FieldType>(io, pMesh)
)
)
{
Info<< ' ' << fieldName;
Info<< ' ' << io.name();
++count;
}
}

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018-2022 OpenCFD Ltd.
Copyright (C) 2018-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
@ -92,7 +92,7 @@ label writeVolFields
label count = 0;
for (const word& fieldName : objects.sortedNames<FieldType>())
for (const IOobject& io : objects.csorted<FieldType>())
{
if
(
@ -100,12 +100,12 @@ label writeVolFields
(
ensCase,
ensMesh,
getField<FieldType>(objects.findObject(fieldName), mesh),
getField<FieldType>(io, mesh),
nearCellValue
)
)
{
Info<< ' ' << fieldName;
Info<< ' ' << io.name();
++count;
}
}

View File

@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -58,18 +59,21 @@ void ReadAndMapFields
{
typedef typename MappedGeoField::value_type Type;
// Search list of objects for wanted type
IOobjectList fieldObjects(objects.lookupClass(ReadGeoField::typeName));
// Objects of wanted type
const UPtrList<const IOobject> fieldObjects
(
objects.csorted<ReadGeoField>()
);
tetFields.setSize(fieldObjects.size());
tetFields.resize(fieldObjects.size());
label i = 0;
forAllConstIters(fieldObjects, iter)
for (const IOobject& io : fieldObjects)
{
Info<< "Converting " << ReadGeoField::typeName << ' ' << iter.key()
<< endl;
Info<< "Converting "
<< ReadGeoField::typeName << ' ' << io.name() << endl;
ReadGeoField readField(*iter(), mesh);
ReadGeoField readField(io, mesh);
tetFields.set
(
@ -82,8 +86,8 @@ void ReadAndMapFields
readField.instance(),
readField.local(),
tetDualMesh,
IOobject::NO_READ,
IOobject::AUTO_WRITE,
IOobjectOption::NO_READ,
IOobjectOption::AUTO_WRITE,
readField.registerObject()
),
pointMesh::New(tetDualMesh),

View File

@ -79,8 +79,8 @@ Foam::tmp<GeoField> Foam::getField
{
// Move field to the cache
IOobject newIO(tfield(), *cache);
newIO.readOpt(IOobject::NO_READ);
newIO.writeOpt(IOobject::NO_WRITE);
newIO.readOpt(IOobjectOption::NO_READ);
newIO.writeOpt(IOobjectOption::NO_WRITE);
tfield.ref().checkOut(); // Paranoid
cache->store(new GeoField(newIO, tfield));

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2022 OpenCFD Ltd.
Copyright (C) 2016-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -108,24 +108,12 @@ void Foam::processFields
const IOobjectList& cloudObjects
)
{
for (const word& fldName : cloudObjects.sortedNames<IOField<Type>>())
for (const IOobject& io : cloudObjects.csorted<IOField<Type>>())
{
const IOobject* io = cloudObjects.cfindObject<IOField<Type>>(fldName);
Info<< " reading field " << io.name() << endl;
IOField<Type> field(io);
if (!io)
{
FatalErrorInFunction
<< "Could not read field:" << fldName
<< " type:" << IOField<Type>::typeName
<< abort(FatalError);
}
else
{
Info<< " reading field " << fldName << endl;
IOField<Type> field(*io);
writeVTKField<Type>(os, field, addr);
}
writeVTKField<Type>(os, field, addr);
}
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2015 OpenFOAM Foundation
Copyright (C) 2018-2020 OpenCFD Ltd.
Copyright (C) 2018-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -97,22 +97,24 @@ public:
template<class GeoFieldType>
void fieldInterpolator::interpolate()
{
const word& clsName = GeoFieldType::typeName;
const wordList fieldNames =
label nFields = 0;
for
(
selectedFields_.empty()
? objects_.sortedNames(clsName)
: objects_.sortedNames(clsName, selectedFields_)
);
if (fieldNames.size())
const IOobject& io :
(
selectedFields_.empty()
? objects_.csorted<GeoFieldType>()
: objects_.csorted<GeoFieldType>(selectedFields_)
)
)
{
Info<< " " << clsName << 's';
}
const word& fieldName = io.name();
if (!nFields)
{
Info<< " " << GeoFieldType::typeName << 's';
}
for (const word& fieldName : fieldNames)
{
Info<< ' ' << fieldName << '(';
const scalar deltaT = (ti1_.value() - ti_.value())/(divisions_ + 1);
@ -166,7 +168,7 @@ void fieldInterpolator::interpolate()
(
fieldName,
runTime_.timeName(),
objects_[fieldName]->db(),
io.db(),
IOobject::NO_READ,
IOobject::NO_WRITE,
IOobject::NO_REGISTER
@ -181,9 +183,10 @@ void fieldInterpolator::interpolate()
}
Info<< ')';
++nFields;
}
if (fieldNames.size()) Info<< endl;
if (nFields) Info<< endl;
}