From 766f6f783ccdd23faff9b5486d9a1cc3e03192aa Mon Sep 17 00:00:00 2001 From: Daniels Date: Tue, 24 Dec 2024 11:22:48 +0100 Subject: [PATCH] Renamed GeomField to GeomFieldType. Include algorithm header in Adapter.C instead of Adapter.h. Iterate over toRemoveIndices instead of toRemove (field names). --- Adapter.C | 73 +++++++++++++++++++++++++++++-------------------------- Adapter.H | 2 -- 2 files changed, 39 insertions(+), 36 deletions(-) diff --git a/Adapter.C b/Adapter.C index d3807c1b..416d715a 100644 --- a/Adapter.C +++ b/Adapter.C @@ -3,6 +3,7 @@ #include "Utilities.H" #include "IOstreams.H" +#include using namespace Foam; @@ -896,12 +897,12 @@ void preciceAdapter::Adapter::setupCheckpointing() DEBUG(adapterInfo("Adding in checkpointed fields...")); #undef doLocalCode -#define doLocalCode(GeomField) \ - /* Checkpoint registered GeomField objects */ \ - for (const word& obj : mesh_.sortedNames()) \ - { \ - addCheckpointField(mesh_.thisDb().getObjectPtr(obj)); \ - DEBUG(adapterInfo("Checkpoint " + obj + " : " #GeomField)); \ +#define doLocalCode(GeomFieldType) \ + /* Checkpoint registered GeomFieldType objects */ \ + for (const word& obj : mesh_.sortedNames()) \ + { \ + addCheckpointField(mesh_.thisDb().getObjectPtr(obj)); \ + DEBUG(adapterInfo("Checkpoint " + obj + " : " #GeomFieldType)); \ } doLocalCode(volScalarField); @@ -930,37 +931,41 @@ void preciceAdapter::Adapter::pruneCheckpointedFields() // If not, remove them from the checkpointed fields vector word fieldName; + uint index; std::vector regFields; - std::vector toRemove; + std::vector toRemoveIndices; #undef doLocalCode -#define doLocalCode(GeomField, GeomField_, GeomFieldCopies_) \ - regFields.clear(); \ - toRemove.clear(); \ - /* Iterate through fields in OpenFOAM registry */ \ - for (const word& fieldName : mesh_.sortedNames()) \ - { \ - regFields.push_back(fieldName); \ - } \ - /* Iterate through checkpointed fields */ \ - for (GeomField * fieldObj : GeomFieldCopies_) \ - { \ - fieldName = fieldObj->name(); \ - if (std::find(regFields.begin(), regFields.end(), fieldName) == regFields.end()) \ - { \ - toRemove.push_back(fieldName); \ - } \ - } \ - if (!toRemove.empty()) \ - { \ - for (auto it = toRemove.rbegin(); it != toRemove.rend(); ++it) \ - { \ - auto index = std::distance(toRemove.rbegin(), it); \ - GeomField_.erase(GeomField_.begin() + index); \ - delete GeomFieldCopies_.at(index); \ - GeomFieldCopies_.erase(GeomFieldCopies_.begin() + index); \ - DEBUG(adapterInfo("Removed " #GeomField " : " + toRemove[index] + " from the checkpointed fields list.")); \ - } \ +#define doLocalCode(GeomFieldType, GeomField_, GeomFieldCopies_) \ + regFields.clear(); \ + toRemoveIndices.clear(); \ + index = 0; \ + /* Iterate through fields in OpenFOAM registry */ \ + for (const word& fieldName : mesh_.sortedNames()) \ + { \ + regFields.push_back(fieldName); \ + } \ + /* Iterate through checkpointed fields */ \ + for (GeomFieldType * fieldObj : GeomFieldCopies_) \ + { \ + fieldName = fieldObj->name(); \ + if (std::find(regFields.begin(), regFields.end(), fieldName) == regFields.end()) \ + { \ + toRemoveIndices.push_back(index); \ + } \ + index += 1; \ + } \ + if (!toRemoveIndices.empty()) \ + { \ + /* Iterate in reverse to avoid index shifting */ \ + for (auto it = toRemoveIndices.rbegin(); it < toRemoveIndices.rend(); ++it) \ + { \ + index = *it; \ + DEBUG(adapterInfo("Removed " #GeomFieldType " : " + GeomFieldCopies_.at(index)->name() + " from the checkpointed fields list.")); \ + GeomField_.erase(GeomField_.begin() + index); \ + delete GeomFieldCopies_.at(index); \ + GeomFieldCopies_.erase(GeomFieldCopies_.begin() + index); \ + } \ } doLocalCode(volScalarField, volScalarFields_, volScalarFieldCopies_); diff --git a/Adapter.H b/Adapter.H index a360cc31..b82efa20 100644 --- a/Adapter.H +++ b/Adapter.H @@ -24,8 +24,6 @@ // preCICE Solver Interface #include -#include - namespace preciceAdapter {