Skip to content

Commit 7832d7b

Browse files
authored
Fix different dimensions error / Checkpointing issue #158 (#344)
1 parent 20b4617 commit 7832d7b

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed

Adapter.C

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "Utilities.H"
44

55
#include "IOstreams.H"
6+
#include <algorithm>
67

78
using namespace Foam;
89

@@ -437,6 +438,7 @@ void preciceAdapter::Adapter::execute()
437438
// Read checkpoint if required
438439
if (requiresReadingCheckpoint())
439440
{
441+
pruneCheckpointedFields();
440442
readCheckpoint();
441443
}
442444

@@ -895,12 +897,12 @@ void preciceAdapter::Adapter::setupCheckpointing()
895897
DEBUG(adapterInfo("Adding in checkpointed fields..."));
896898

897899
#undef doLocalCode
898-
#define doLocalCode(GeomField) \
899-
/* Checkpoint registered GeomField objects */ \
900-
for (const word& obj : mesh_.sortedNames<GeomField>()) \
901-
{ \
902-
addCheckpointField(mesh_.thisDb().getObjectPtr<GeomField>(obj)); \
903-
DEBUG(adapterInfo("Checkpoint " + obj + " : " #GeomField)); \
900+
#define doLocalCode(GeomFieldType) \
901+
/* Checkpoint registered GeomFieldType objects */ \
902+
for (const word& obj : mesh_.sortedNames<GeomFieldType>()) \
903+
{ \
904+
addCheckpointField(mesh_.thisDb().getObjectPtr<GeomFieldType>(obj)); \
905+
DEBUG(adapterInfo("Checkpoint " + obj + " : " #GeomFieldType)); \
904906
}
905907

906908
doLocalCode(volScalarField);
@@ -923,6 +925,64 @@ void preciceAdapter::Adapter::setupCheckpointing()
923925
ACCUMULATE_TIMER(timeInCheckpointingSetup_);
924926
}
925927

928+
void preciceAdapter::Adapter::pruneCheckpointedFields()
929+
{
930+
// Check if checkpointed fields exist in OpenFOAM registry
931+
// If not, remove them from the checkpointed fields vector
932+
933+
word fieldName;
934+
uint index;
935+
std::vector<word> regFields;
936+
std::vector<uint> toRemoveIndices;
937+
938+
#undef doLocalCode
939+
#define doLocalCode(GeomFieldType, GeomField_, GeomFieldCopies_) \
940+
regFields.clear(); \
941+
toRemoveIndices.clear(); \
942+
index = 0; \
943+
/* Iterate through fields in OpenFOAM registry */ \
944+
for (const word& fieldName : mesh_.sortedNames<GeomFieldType>()) \
945+
{ \
946+
regFields.push_back(fieldName); \
947+
} \
948+
/* Iterate through checkpointed fields */ \
949+
for (GeomFieldType * fieldObj : GeomFieldCopies_) \
950+
{ \
951+
fieldName = fieldObj->name(); \
952+
if (std::find(regFields.begin(), regFields.end(), fieldName) == regFields.end()) \
953+
{ \
954+
toRemoveIndices.push_back(index); \
955+
} \
956+
index += 1; \
957+
} \
958+
if (!toRemoveIndices.empty()) \
959+
{ \
960+
/* Iterate in reverse to avoid index shifting */ \
961+
for (auto it = toRemoveIndices.rbegin(); it != toRemoveIndices.rend(); ++it) \
962+
{ \
963+
index = *it; \
964+
DEBUG(adapterInfo("Removed " #GeomFieldType " : " + GeomFieldCopies_.at(index)->name() + " from the checkpointed fields list.")); \
965+
GeomField_.erase(GeomField_.begin() + index); \
966+
delete GeomFieldCopies_.at(index); \
967+
GeomFieldCopies_.erase(GeomFieldCopies_.begin() + index); \
968+
} \
969+
}
970+
971+
doLocalCode(volScalarField, volScalarFields_, volScalarFieldCopies_);
972+
doLocalCode(volVectorField, volVectorFields_, volVectorFieldCopies_);
973+
doLocalCode(volTensorField, volTensorFields_, volTensorFieldCopies_);
974+
doLocalCode(volSymmTensorField, volSymmTensorFields_, volSymmTensorFieldCopies_);
975+
976+
doLocalCode(surfaceScalarField, surfaceScalarFields_, surfaceScalarFieldCopies_);
977+
doLocalCode(surfaceVectorField, surfaceVectorFields_, surfaceVectorFieldCopies_);
978+
doLocalCode(surfaceTensorField, surfaceTensorFields_, surfaceTensorFieldCopies_);
979+
980+
doLocalCode(pointScalarField, pointScalarFields_, pointScalarFieldCopies_);
981+
doLocalCode(pointVectorField, pointVectorFields_, pointVectorFieldCopies_);
982+
doLocalCode(pointTensorField, pointTensorFields_, pointTensorFieldCopies_);
983+
984+
#undef doLocalCode
985+
}
926986

927987
// All mesh checkpointed fields
928988

Adapter.H

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,10 @@ private:
294294
//- Configure the checkpointing
295295
void setupCheckpointing();
296296

297+
//- Remove checkpointed fields which are not used by OpenFOAM anymore
298+
void pruneCheckpointedFields();
299+
300+
297301
//- Make a copy of the runTime object
298302
void storeCheckpointTime();
299303

changelog-entries/344.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fixed a bug where a field was checkpointed, but doesn't exist anymore in OpenFOAM registry, causing a crash when reloading last timestep into OpenFOAM. This is done by pruning checkpointed fields that do not (anymore) appear in the registry of objects at that timestep before reading the checkpoint. [#344](https://github.com/precice/openfoam-adapter/pull/344)

0 commit comments

Comments
 (0)