From f961c13045edc5f0f90b29c4b9f134b1855f4e66 Mon Sep 17 00:00:00 2001 From: David Schneider Date: Mon, 18 Jul 2022 09:46:24 +0200 Subject: [PATCH 1/2] Add basic infrastructure to add conditional gradients --- CouplingDataUser.C | 9 +++++++++ CouplingDataUser.H | 3 +++ Interface.C | 27 +++++++++++++++++++++++++++ Interface.H | 3 +++ 4 files changed, 42 insertions(+) diff --git a/CouplingDataUser.C b/CouplingDataUser.C index 7e84250d..4cffef1c 100644 --- a/CouplingDataUser.C +++ b/CouplingDataUser.C @@ -36,6 +36,15 @@ void preciceAdapter::CouplingDataUser::setLocationsType(LocationType locationsTy locationType_ = locationsType; } +void preciceAdapter::CouplingDataUser::writeGradients(std::vector& gradientBuffer, const unsigned int dim) +{ + adapterInfo("Data \"" + getDataName() + "\" does not support writing gradients. Please select a different " + "data or a different mapping configuration, which does not require " + "additional gradient information.", + "error"); +} + + void preciceAdapter::CouplingDataUser::checkDataLocation(const bool meshConnectivity) const { if (this->isLocationTypeSupported(meshConnectivity) == false) diff --git a/CouplingDataUser.H b/CouplingDataUser.H index 3c994790..f21e2483 100644 --- a/CouplingDataUser.H +++ b/CouplingDataUser.H @@ -69,6 +69,9 @@ public: //- Write the coupling data to the buffer virtual void write(double* dataBuffer, bool meshConnectivity, const unsigned int dim) = 0; + //- Write gradient data to the gradient buffer + virtual void writeGradients(std::vector& gradientBuffer, const unsigned int dim); + //- Read the coupling data from the buffer virtual void read(double* dataBuffer, const unsigned int dim) = 0; diff --git a/Interface.C b/Interface.C index c90ab5bc..246cd843 100644 --- a/Interface.C +++ b/Interface.C @@ -330,6 +330,7 @@ void preciceAdapter::Interface::createBuffer() // Will the interface buffer need to store 3D vector data? bool needsVectorData = false; int dataBufferSize = 0; + bool requiresGradientData = false; // Check all the coupling data readers for (uint i = 0; i < couplingDataReaders_.size(); i++) @@ -347,6 +348,10 @@ void preciceAdapter::Interface::createBuffer() { needsVectorData = true; } + if (precice_.isGradientDataRequired(couplingDataWriters_.at(i)->dataID())) + { + requiresGradientData = true; + } } // Set the appropriate buffer size @@ -367,6 +372,10 @@ void preciceAdapter::Interface::createBuffer() // preCICE implementation, it should work as, when writing scalars, // it should only use the first 1/3 elements of the buffer. dataBuffer_ = new double[dataBufferSize](); + if (requiresGradientData) + { + gradientBuffer_.resize(dim_ * dataBufferSize); + } } void preciceAdapter::Interface::readCouplingData() @@ -430,6 +439,15 @@ void preciceAdapter::Interface::writeCouplingData() numDataLocations_, vertexIDs_, dataBuffer_); + + if (precice_.isGradientDataRequired(couplingDataWriter->dataID())) + { + couplingDataWriter->writeGradients(gradientBuffer_, dim_); + precice_.writeBlockVectorGradientData(couplingDataWriter->dataID(), + numDataLocations_, + vertexIDs_, + gradientBuffer_.data()); + } } else { @@ -438,6 +456,15 @@ void preciceAdapter::Interface::writeCouplingData() numDataLocations_, vertexIDs_, dataBuffer_); + + if (precice_.isGradientDataRequired(couplingDataWriter->dataID())) + { + couplingDataWriter->writeGradients(gradientBuffer_, dim_); + precice_.writeBlockScalarGradientData(couplingDataWriter->dataID(), + numDataLocations_, + vertexIDs_, + gradientBuffer_.data()); + } } } // } diff --git a/Interface.H b/Interface.H index bc18134a..f12cfa89 100644 --- a/Interface.H +++ b/Interface.H @@ -41,6 +41,9 @@ protected: //- Buffer for the coupling data double* dataBuffer_; + //- Buffer for writing gradient data + std::vector gradientBuffer_; + //- Vector of CouplingDataReaders std::vector couplingDataReaders_; From 5a097a4a4bc70455f2fc08276046c1abd0dd7085 Mon Sep 17 00:00:00 2001 From: David Schneider Date: Mon, 18 Jul 2022 11:00:28 +0200 Subject: [PATCH 2/2] Add gradient writer for Temperature data --- CHT/Temperature.C | 15 +++++++++++++++ CHT/Temperature.H | 3 +++ 2 files changed, 18 insertions(+) diff --git a/CHT/Temperature.C b/CHT/Temperature.C index 3756471c..3a3db662 100644 --- a/CHT/Temperature.C +++ b/CHT/Temperature.C @@ -56,6 +56,21 @@ void preciceAdapter::CHT::Temperature::write(double* buffer, bool meshConnectivi } } +void preciceAdapter::CHT::Temperature::writeGradients(std::vector& gradientBuffer, const unsigned int dim) +{ + for (const label patchID : patchIDs_) + { + const auto& TGrad = fvc::grad(*T_); + forAll(TGrad().boundaryField()[patchID], i) + { + for (unsigned int d = 0; d < dim; ++d) + { + gradientBuffer[i * dim + d] = TGrad().boundaryField()[patchID][i][d]; + } + } + } +} + void preciceAdapter::CHT::Temperature::read(double* buffer, const unsigned int dim) { int bufferIndex = 0; diff --git a/CHT/Temperature.H b/CHT/Temperature.H index 0ce85a7c..d749c231 100644 --- a/CHT/Temperature.H +++ b/CHT/Temperature.H @@ -28,6 +28,9 @@ public: //- Write the temperature values into the buffer void write(double* buffer, bool meshConnectivity, const unsigned int dim); + //- Write gradient data + void writeGradients(std::vector& gradientBuffer, const unsigned int dim) override; + //- Read the temperature values from the buffer void read(double* buffer, const unsigned int dim);