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

@ -568,7 +568,7 @@ bool Foam::functionObjects::externalCoupled::read(const dictionary& dict)
// Leave trigger intact
// Get names of all fvMeshes (and derived types)
wordList allRegionNames(time_.lookupClass<fvMesh>().sortedToc());
wordList allRegionNames(time_.sortedNames<fvMesh>());
const dictionary& allRegionsDict = dict.subDict("regions");
for (const entry& dEntry : allRegionsDict)

View File

@ -119,14 +119,12 @@ bool Foam::functionObjects::mapFields::mapFieldType() const
const fvMesh& mapRegion = mapRegionPtr_();
wordList fieldNames(this->mesh_.names(VolFieldType::typeName));
wordList fieldNames(this->mesh_.sortedNames<VolFieldType>(fieldNames_));
const labelList selected(fieldNames_.matching(fieldNames));
const bool processed = !fieldNames.empty();
for (const label fieldi : selected)
for (const word& fieldName : fieldNames)
{
const word& fieldName = fieldNames[fieldi];
const VolFieldType& field = lookupObject<VolFieldType>(fieldName);
auto* mapFieldPtr = mapRegion.getObjectPtr<VolFieldType>(fieldName);
@ -159,7 +157,7 @@ bool Foam::functionObjects::mapFields::mapFieldType() const
evaluateConstraintTypes(mappedField);
}
return !selected.empty();
return processed;
}
@ -170,14 +168,12 @@ bool Foam::functionObjects::mapFields::writeFieldType() const
const fvMesh& mapRegion = mapRegionPtr_();
wordList fieldNames(this->mesh_.names(VolFieldType::typeName));
wordList fieldNames(this->mesh_.sortedNames<VolFieldType>(fieldNames_));
const labelList selected(fieldNames_.matching(fieldNames));
const bool processed = !fieldNames.empty();
for (const label fieldi : selected)
for (const word& fieldName : fieldNames)
{
const word& fieldName = fieldNames[fieldi];
const VolFieldType& mappedField =
mapRegion.template lookupObject<VolFieldType>(fieldName);
@ -186,7 +182,7 @@ bool Foam::functionObjects::mapFields::writeFieldType() const
Log << " " << fieldName << ": written";
}
return !selected.empty();
return processed;
}

View File

@ -38,15 +38,13 @@ void Foam::functionObjects::nearWallFields::createFields
{
typedef GeometricField<Type, fvPatchField, volMesh> VolFieldType;
HashTable<const VolFieldType*> flds(obr_.lookupClass<VolFieldType>());
forAllConstIters(flds, iter)
for (const VolFieldType& fld : obr_.csorted<VolFieldType>())
{
const VolFieldType& fld = *(iter.val());
const auto fieldMapIter = fieldMap_.cfind(fld.name());
if (fieldMap_.found(fld.name()))
if (fieldMapIter.good())
{
const word& sampleFldName = fieldMap_[fld.name()];
const word& sampleFldName = fieldMapIter.val();
if (obr_.found(sampleFldName))
{
@ -57,19 +55,18 @@ void Foam::functionObjects::nearWallFields::createFields
}
else
{
label sz = sflds.size();
sflds.setSize(sz+1);
IOobject io(fld);
io.readOpt(IOobject::NO_READ);
io.writeOpt(IOobject::NO_WRITE);
io.readOpt(IOobjectOption::NO_READ);
io.writeOpt(IOobjectOption::NO_WRITE);
io.rename(sampleFldName);
// Override bc to be calculated
const label newFieldi = sflds.size();
sflds.resize(newFieldi+1);
sflds.set
(
sz,
newFieldi,
new VolFieldType
(
io,
@ -79,7 +76,7 @@ void Foam::functionObjects::nearWallFields::createFields
)
);
Log << " created " << sflds[sz].name()
Log << " created " << io.name()
<< " to sample " << fld.name() << endl;
}
}

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2022 OpenCFD Ltd.
Copyright (C) 2016-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -107,7 +107,9 @@ bool Foam::functionObjects::particleDistribution::write()
{
Log << type() << " " << name() << " output:" << endl;
if (!mesh_.foundObject<cloud>(cloudName_))
const cloud* cloudPtr = mesh_.cfindObject<cloud>(cloudName_);
if (!cloudPtr)
{
WarningInFunction
<< "Unable to find cloud " << cloudName_
@ -117,7 +119,7 @@ bool Foam::functionObjects::particleDistribution::write()
return false;
}
const cloud& c = mesh_.lookupObject<cloud>(cloudName_);
const cloud& c = *cloudPtr;
objectRegistry cloudObr
(

View File

@ -907,16 +907,15 @@ bool Foam::functionObjects::regionSizeDistribution::write()
{
for
(
const word& fldName
: obr_.sortedNames<volScalarField>(fields_)
const volScalarField& vfield
: obr_.csorted<volScalarField>(fields_)
)
{
const word& fldName = vfield.name();
Log << " Scalar field " << fldName << endl;
tmp<Field<scalar>> tfld
(
obr_.lookupObject<volScalarField>(fldName).primitiveField()
);
tmp<Field<scalar>> tfld(vfield.primitiveField());
const auto& fld = tfld();
writeGraphs
@ -938,23 +937,21 @@ bool Foam::functionObjects::regionSizeDistribution::write()
{
for
(
const word& fldName
: obr_.sortedNames<volVectorField>(fields_)
const volVectorField& vfield
: obr_.csorted<volVectorField>(fields_)
)
{
const word& fldName = vfield.name();
Log << " Vector field " << fldName << endl;
tmp<Field<vector>> tfld
(
obr_.lookupObject<volVectorField>(fldName).primitiveField()
);
tmp<Field<vector>> tfld(vfield.primitiveField());
if (csysPtr_)
{
Log << "Transforming vector field " << fldName
<< " with coordinate system "
<< csysPtr_->name()
<< endl;
<< csysPtr_->name() << endl;
tfld = csysPtr_->localVector(tfld());
}

View File

@ -84,7 +84,7 @@ void Foam::functionObjects::setFlow::setPhi(const volVectorField& U)
FatalErrorInFunction
<< "Unable to find rho field'" << rhoName_
<< "' in the mesh database. Available fields are:"
<< mesh_.names<volScalarField>()
<< flatOutput(mesh_.sortedNames<volScalarField>())
<< exit(FatalError);
}
}

View File

@ -45,12 +45,8 @@ void Foam::functionObjects::surfaceInterpolate::interpolateFields()
}
HashTable<const VolFieldType*> flds(obr_.lookupClass<VolFieldType>());
forAllConstIters(flds, iter)
for (const VolFieldType& fld : obr_.csorted<VolFieldType>())
{
const VolFieldType& fld = *iter();
if (fieldMap.found(fld.name()))
{
// const word sName = "interpolate(" + fld.name() + ')';
@ -62,8 +58,8 @@ void Foam::functionObjects::surfaceInterpolate::interpolateFields()
}
else
{
Log << " interpolating " << fld.name() << " to create "
<< sName << endl;
Log << " interpolating "
<< fld.name() << " to create " << sName << endl;
}
store(sName, linearInterpolate(fld));

View File

@ -756,7 +756,10 @@ void Foam::functionObjects::forces::calcForcesMoments()
const volScalarField rho(this->rho());
const volScalarField mu(this->mu());
const auto models = obr_.lookupClass<porosityModel>();
const UPtrList<const porosityModel> models
(
obr_.csorted<porosityModel>()
);
if (models.empty())
{
@ -766,10 +769,10 @@ void Foam::functionObjects::forces::calcForcesMoments()
<< endl;
}
forAllConstIters(models, iter)
for (const porosityModel& mdl : models)
{
// Non-const access required if mesh is changing
auto& pm = const_cast<porosityModel&>(*iter());
auto& pm = const_cast<porosityModel&>(mdl);
const vectorField fPTot(pm.force(U, rho, mu));

View File

@ -220,7 +220,7 @@ const Foam::volVectorField& Foam::functionObjects::propellerInfo::U() const
FatalErrorInFunction
<< "Unable to find velocity field " << UName_
<< " . Available vector fields are: "
<< mesh_.names<volVectorField>()
<< flatOutput(mesh_.sortedNames<volVectorField>())
<< exit(FatalError);
}