Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gradient data write functionality #233

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions CHT/Temperature.C
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ void preciceAdapter::CHT::Temperature::write(double* buffer, bool meshConnectivi
}
}

void preciceAdapter::CHT::Temperature::writeGradients(std::vector<double>& 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];
}
}
}
}
Comment on lines +59 to +72
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This essentially addresses the same problem as @thesamriel implemented in FF/TemperatureGradient.C in https://github.com/precice/openfoam-adapter/pull/281/files#diff-90c837067b5e748c5bc1af9d24a5ab6f0a5f7801bd91685900c27491c259a830

The main difference is that:

  • This PR gives the gradient of (temperature) to preCICE, as gradient data
  • Updates to the FF module #281 gives the (gradient of temperature) to preCICE, as normal data values

Would these two PRs need to be aligned somehow? @thesamriel I assume that this still cannot be shaped as a replacement for the respective addition of #281.


void preciceAdapter::CHT::Temperature::read(double* buffer, const unsigned int dim)
{
int bufferIndex = 0;
Expand Down
3 changes: 3 additions & 0 deletions CHT/Temperature.H
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>& gradientBuffer, const unsigned int dim) override;

//- Read the temperature values from the buffer
void read(double* buffer, const unsigned int dim);

Expand Down
9 changes: 9 additions & 0 deletions CouplingDataUser.C
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ void preciceAdapter::CouplingDataUser::setLocationsType(LocationType locationsTy
locationType_ = locationsType;
}

void preciceAdapter::CouplingDataUser::writeGradients(std::vector<double>& 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");
}
Comment on lines +39 to +45
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice way to default to a "not implemented" error message!

Only typo: change "a data" to just "data" or make it also "a different data configuration". ("data" is not countable)



void preciceAdapter::CouplingDataUser::checkDataLocation(const bool meshConnectivity) const
{
if (this->isLocationTypeSupported(meshConnectivity) == false)
Expand Down
3 changes: 3 additions & 0 deletions CouplingDataUser.H
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>& gradientBuffer, const unsigned int dim);

//- Read the coupling data from the buffer
virtual void read(double* dataBuffer, const unsigned int dim) = 0;

Expand Down
27 changes: 27 additions & 0 deletions Interface.C
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand All @@ -347,6 +348,10 @@ void preciceAdapter::Interface::createBuffer()
{
needsVectorData = true;
}
if (precice_.isGradientDataRequired(couplingDataWriters_.at(i)->dataID()))
{
requiresGradientData = true;
}
}

// Set the appropriate buffer size
Expand All @@ -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()
Expand Down Expand Up @@ -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());
Comment on lines +443 to +449
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One example for vector data would be velocity. We should also implement that (in this or in a follow-up PR), otherwise this case will be completely unused for now.

}
}
else
{
Expand All @@ -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());
}
}
}
// }
Expand Down
3 changes: 3 additions & 0 deletions Interface.H
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ protected:
//- Buffer for the coupling data
double* dataBuffer_;

//- Buffer for writing gradient data
std::vector<double> gradientBuffer_;

//- Vector of CouplingDataReaders
std::vector<CouplingDataUser*> couplingDataReaders_;

Expand Down