Skip to content

Commit

Permalink
fvMesh: Generalised the handing of old-time fields during mesh motion…
Browse files Browse the repository at this point in the history
… and topology change

Topology change occurs before the time-increment and hence the oldest time
field (old-time in the case of 1st order time schemes, old-old-time in the case
of 2nd-order time schemes) is not actually needed as it is replaced by the
current time-field after time-increment so there is no purpose to mapping this
field.  However, it is necessary to keep track of the existence of the
oldest-time field to ensure the correct number of old-time fields are cached for
the time-scheme.  This development allows fvMesh to delete the redundant
oldest-time fields in such a manner that GeometricField can reinstate them
correctly after time-increment which is more efficient and more reliable than
attempting to map them and done previously.

Additionally fvMesh movement, which occurs after time-increment, now ensure all
old-time fields are up-to-date before NCC stitcher mapping so that both fields
and their old-time values are mapped consistently.  This removes the need for
old-time field caching calls in MapGeometricFields, fvMeshAdder and
fvMeshStitcher, thus simplifying the code and improving maintainability.
  • Loading branch information
Henry Weller committed Oct 10, 2022
1 parent b8ccebb commit 30eb5e2
Show file tree
Hide file tree
Showing 13 changed files with 278 additions and 158 deletions.
28 changes: 26 additions & 2 deletions applications/test/nullObject/Test-nullObject.C
Expand Up @@ -40,10 +40,34 @@ int main()
}


// Test const pointer and const reference to the nullObject

const SimpleClass* constPtrToNull(NullObjectConstPtr<SimpleClass>());
const SimpleClass& constRefToNull(NullObjectRef<SimpleClass>());

if (isNull(constPtrToNull))
{
Info<< "Pass: constPtrToNull is null" << endl;
}
else
{
Info<< "FAIL: constPtrToNull is not null" << endl;
}

if (isNull(constRefToNull))
{
Info<< "Pass: constRefToNull is null" << endl;
}
else
{
Info<< "FAIL: constRefToNull is not null" << endl;
}


// Test pointer and reference to the nullObject

const SimpleClass* ptrToNull(NullObjectPtr<SimpleClass>());
const SimpleClass& refToNull(*ptrToNull);
SimpleClass* ptrToNull(NullObjectPtr<SimpleClass>());
SimpleClass& refToNull(*ptrToNull);

if (isNull(ptrToNull))
{
Expand Down
84 changes: 69 additions & 15 deletions src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricField.C
Expand Up @@ -1069,21 +1069,29 @@ void Foam::GeometricField<Type, PatchField, GeoMesh>::storeOldTime() const
{
if (field0Ptr_)
{
field0Ptr_->storeOldTime();

if (debug)
if (notNull(field0Ptr_))
{
InfoInFunction
<< "Storing old time field for field" << endl
<< this->info() << endl;
field0Ptr_->storeOldTime();

if (debug)
{
InfoInFunction
<< "Storing old time field for field" << endl
<< this->info() << endl;
}

*field0Ptr_ == *this;
field0Ptr_->timeIndex_ = timeIndex_;

if (field0Ptr_->field0Ptr_)
{
field0Ptr_->writeOpt() = this->writeOpt();
}
}

*field0Ptr_ == *this;
field0Ptr_->timeIndex_ = timeIndex_;

if (field0Ptr_->field0Ptr_)
else
{
field0Ptr_->writeOpt() = this->writeOpt();
// Reinstate old-time field
oldTime();
}
}
}
Expand All @@ -1094,7 +1102,14 @@ Foam::label Foam::GeometricField<Type, PatchField, GeoMesh>::nOldTimes() const
{
if (field0Ptr_)
{
return field0Ptr_->nOldTimes() + 1;
if (isNull(field0Ptr_))
{
return 1;
}
else
{
return field0Ptr_->nOldTimes() + 1;
}
}
else
{
Expand All @@ -1107,8 +1122,12 @@ template<class Type, template<class> class PatchField, class GeoMesh>
const Foam::GeometricField<Type, PatchField, GeoMesh>&
Foam::GeometricField<Type, PatchField, GeoMesh>::oldTime() const
{
if (!field0Ptr_)
if (!field0Ptr_ || isNull(field0Ptr_))
{
// Set field0Ptr_ null to ensure the old-time field constructor
// does not construct the old-old-time field
field0Ptr_ = nullptr;

field0Ptr_ = new GeometricField<Type, PatchField, GeoMesh>
(
IOobject
Expand Down Expand Up @@ -1218,11 +1237,46 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::prevIter() const
template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::clearOldTimes()
{
deleteDemandDrivenData(field0Ptr_);
if (field0Ptr_ && notNull(field0Ptr_))
{
deleteDemandDrivenData(field0Ptr_);
}

deleteDemandDrivenData(fieldPrevIterPtr_);
}


template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::nullOldestTime()
{
// Check that the field is not an old-time field
if (!isOldTime())
{
// Search for the oldest old-time field and set to nullObject
nullOldestTimeFound();
}
}


template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::nullOldestTimeFound()
{
if (field0Ptr_ && notNull(field0Ptr_))
{
if (field0Ptr_->field0Ptr_)
{
field0Ptr_->nullOldestTimeFound();
}
else
{
deleteDemandDrivenData(field0Ptr_);
field0Ptr_ =
NullObjectPtr<GeometricField<Type, PatchField, GeoMesh>>();
}
}
}


template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::
correctBoundaryConditions()
Expand Down
Expand Up @@ -130,6 +130,12 @@ private:
//- Read the field - create the field dictionary on-the-fly
void readFields();

//- Store the old-time field
void storeOldTime() const;

//- Find oldest time and set to nullObject
void nullOldestTimeFound();


public:

Expand Down Expand Up @@ -407,9 +413,6 @@ public:
//- Store the old-time fields
void storeOldTimes() const;

//- Store the old-time field
void storeOldTime() const;

//- Return the number of old time fields stored
label nOldTimes() const;

Expand Down Expand Up @@ -439,6 +442,14 @@ public:
//- Delete old time and previous iteration fields
void clearOldTimes();

//- Set oldest time field pointer to nullObjectPtr
// to remove the oldest time field while maintaining a tag
// so that it is reinstated on the next call to oldTime().
//
// This is used to temporarily remove the oldest-time field
// prior to mesh topology change.
void nullOldestTime();

//- Correct boundary field
void correctBoundaryConditions();

Expand Down
Expand Up @@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2021 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2022 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
Expand Down Expand Up @@ -78,31 +78,6 @@ void MapGeometricFields
lookupClass<GeometricField<Type, PatchField, GeoMesh>>()
);

// It is necessary to enforce that all old-time fields are stored
// before the mapping is performed. Otherwise, if the
// old-time-level field is mapped before the field itself, sizes
// will not match.

for
(
typename HashTable<const GeometricField<Type, PatchField, GeoMesh>*>::
iterator fieldIter = fields.begin();
fieldIter != fields.end();
++fieldIter
)
{
GeometricField<Type, PatchField, GeoMesh>& field =
const_cast<GeometricField<Type, PatchField, GeoMesh>&>
(*fieldIter());

// Note: check can be removed once pointFields are actually stored on
// the pointMesh instead of now on the polyMesh!
if (&field.mesh() == &mapper.mesh())
{
field.storeOldTimes();
}
}

for
(
typename HashTable<const GeometricField<Type, PatchField, GeoMesh>*>::
Expand Down
19 changes: 16 additions & 3 deletions src/OpenFOAM/primitives/nullObject/nullObject.H
Expand Up @@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2014-2019 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2014-2022 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
Expand Down Expand Up @@ -68,13 +68,17 @@ public:
extern const NullObject* nullObjectPtr;


//- Return reference to the nullObject of type T
//- Return const reference to the nullObject of type T
template<class T>
inline const T& NullObjectRef();

//- Return const pointer to the nullObject of type T
template<class T>
inline const T* NullObjectConstPtr();

//- Return pointer to the nullObject of type T
template<class T>
inline const T* NullObjectPtr();
inline T* NullObjectPtr();

//- Return reference to the nullObject of type T
template<class T>
Expand All @@ -99,6 +103,15 @@ template<class T>
inline bool notNull(const T* t);


//- Return true if t is a pointer to the nullObject of type T
template<class T>
inline bool isNull(T* t);

//- Return true if t is not a pointer to the nullObject of type T
template<class T>
inline bool notNull(T* t);


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

} // End namespace Foam
Expand Down
23 changes: 21 additions & 2 deletions src/OpenFOAM/primitives/nullObject/nullObjectI.H
Expand Up @@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2014-2019 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2014-2022 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
Expand Down Expand Up @@ -36,11 +36,17 @@ inline T&& Foam::NullObjectMove()
}

template<class T>
inline const T* Foam::NullObjectPtr()
inline const T* Foam::NullObjectConstPtr()
{
return reinterpret_cast<const T*>(nullObjectPtr);
}

template<class T>
inline T* Foam::NullObjectPtr()
{
return const_cast<T*>(reinterpret_cast<const T*>(nullObjectPtr));
}


template<class T>
inline bool Foam::isNull(const T& t)
Expand Down Expand Up @@ -68,4 +74,17 @@ inline bool Foam::notNull(const T* t)
}


template<class T>
inline bool Foam::isNull(T* t)
{
return t == NullObjectPtr<T>();
}

template<class T>
inline bool Foam::notNull(T* t)
{
return t != NullObjectPtr<T>();
}


// ************************************************************************* //

0 comments on commit 30eb5e2

Please sign in to comment.