Skip to content

Commit

Permalink
Add phase variable and flux coupling for interFOAM in the FF module (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
thesamriel authored Jan 24, 2024
1 parent a0e5263 commit 1ed787a
Show file tree
Hide file tree
Showing 14 changed files with 448 additions and 3 deletions.
116 changes: 116 additions & 0 deletions FF/Alpha.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#include "Alpha.H"

using namespace Foam;

preciceAdapter::FF::Alpha::Alpha(
const Foam::fvMesh& mesh,
const std::string nameAlpha)
: Alpha_(
const_cast<volScalarField*>(
&mesh.lookupObject<volScalarField>(nameAlpha)))
{
dataType_ = scalar;
}

std::size_t preciceAdapter::FF::Alpha::write(double* buffer, bool meshConnectivity, const unsigned int dim)
{
int bufferIndex = 0;

if (this->locationType_ == LocationType::volumeCenters)
{
if (cellSetNames_.empty())
{
for (const auto& cell : Alpha_->internalField())
{
buffer[bufferIndex++] = cell;
}
}
else
{
for (const auto& cellSetName : cellSetNames_)
{
cellSet overlapRegion(Alpha_->mesh(), cellSetName);
const labelList& cells = overlapRegion.toc();

for (const auto& currentCell : cells)
{
// Copy the alpha valus into the buffer
buffer[bufferIndex++] = Alpha_->internalField()[currentCell];
}
}
}
}

// For every boundary patch of the interface
for (uint j = 0; j < patchIDs_.size(); j++)
{
int patchID = patchIDs_.at(j);

// For every cell of the patch
forAll(Alpha_->boundaryFieldRef()[patchID], i)
{
// Copy the Alpha into the buffer
buffer[bufferIndex++] =
Alpha_->boundaryFieldRef()[patchID][i];
}
}
return bufferIndex;
}

void preciceAdapter::FF::Alpha::read(double* buffer, const unsigned int dim)
{
int bufferIndex = 0;

if (this->locationType_ == LocationType::volumeCenters)
{
if (cellSetNames_.empty())
{
for (auto& cell : Alpha_->ref())
{
cell = buffer[bufferIndex++];
}
}
else
{
for (const auto& cellSetName : cellSetNames_)
{
cellSet overlapRegion(Alpha_->mesh(), cellSetName);
const labelList& cells = overlapRegion.toc();

for (const auto& currentCell : cells)
{
// Copy the pressure into the buffer
Alpha_->ref()[currentCell] = buffer[bufferIndex++];
}
}
}
}

// For every boundary patch of the interface
for (uint j = 0; j < patchIDs_.size(); j++)
{
int patchID = patchIDs_.at(j);
// For every cell of the patch
forAll(Alpha_->boundaryFieldRef()[patchID], i)
{
Alpha_->boundaryFieldRef()[patchID][i] = buffer[bufferIndex++];
}
}
}

bool preciceAdapter::FF::Alpha::isLocationTypeSupported(const bool meshConnectivity) const
{
if (meshConnectivity)
{
return (this->locationType_ == LocationType::faceCenters);
}
else
{
return (this->locationType_ == LocationType::faceCenters || this->locationType_ == LocationType::volumeCenters);
}
}

std::string preciceAdapter::FF::Alpha::getDataName() const
{
return "Alpha";
}
43 changes: 43 additions & 0 deletions FF/Alpha.H
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef FF_ALPHA_H
#define FF_ALPHA_H

#include "CouplingDataUser.H"

#include "fvCFD.H"
#include "cellSet.H"

namespace preciceAdapter
{
namespace FF
{

//- Class that writes and reads Alpha
class Alpha : public CouplingDataUser
{

private:
//- Alpha field
Foam::volScalarField* Alpha_;

public:
//- Constructor
Alpha(
const Foam::fvMesh& mesh,
const std::string nameAlpha);

//- Write the Alpha values into the buffer
std::size_t write(double* buffer, bool meshConnectivity, const unsigned int dim);

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

bool isLocationTypeSupported(const bool meshConnectivity) const override;

//- Get the name of the current data field
std::string getDataName() const override;
};

}
}

#endif
73 changes: 73 additions & 0 deletions FF/AlphaGradient.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include "AlphaGradient.H"
#include "mixedFvPatchFields.H"

using namespace Foam;

preciceAdapter::FF::AlphaGradient::AlphaGradient(
const Foam::fvMesh& mesh,
const std::string nameAlpha)
: Alpha_(
const_cast<volScalarField*>(
&mesh.lookupObject<volScalarField>(nameAlpha)))
{
dataType_ = scalar;
}

std::size_t preciceAdapter::FF::AlphaGradient::write(double* buffer, bool meshConnectivity, const unsigned int dim)
{
int bufferIndex = 0;

// For every boundary patch of the interface
for (uint j = 0; j < patchIDs_.size(); j++)
{
int patchID = patchIDs_.at(j);

// Get the Alpha gradient boundary patch
const scalarField gradientPatch((Alpha_->boundaryFieldRef()[patchID])
.snGrad());

// For every cell of the patch
forAll(gradientPatch, i)
{
// Copy the Alpha gradient into the buffer
buffer[bufferIndex++] =
-gradientPatch[i];
}
}
return bufferIndex;
}

void preciceAdapter::FF::AlphaGradient::read(double* buffer, const unsigned int dim)
{
int bufferIndex = 0;

// For every boundary patch of the interface
for (uint j = 0; j < patchIDs_.size(); j++)
{
int patchID = patchIDs_.at(j);

// Get the Alpha gradient boundary patch
scalarField& gradientPatch =
refCast<fixedGradientFvPatchScalarField>(
Alpha_->boundaryFieldRef()[patchID])
.gradient();

// For every cell of the patch
forAll(gradientPatch, i)
{
// Set the Alpha gradient as the buffer value
gradientPatch[i] =
buffer[bufferIndex++];
}
}
}

bool preciceAdapter::FF::AlphaGradient::isLocationTypeSupported(const bool meshConnectivity) const
{
return (this->locationType_ == LocationType::faceCenters);
}

std::string preciceAdapter::FF::AlphaGradient::getDataName() const
{
return "AlphaGradient";
}
42 changes: 42 additions & 0 deletions FF/AlphaGradient.H
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef FF_ALPHA_GRADIENT_H
#define FF_ALPHA_GRADIENT_H

#include "CouplingDataUser.H"

#include "fvCFD.H"

namespace preciceAdapter
{
namespace FF
{

//- Class that writes and reads Alpha gradient
class AlphaGradient : public CouplingDataUser
{

private:
//- Alpha field
Foam::volScalarField* Alpha_;

public:
//- Constructor
AlphaGradient(
const Foam::fvMesh& mesh,
const std::string nameAlpha);

//- Write the Alpha gradient values into the buffer
std::size_t write(double* buffer, bool meshConnectivity, const unsigned int dim);

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

bool isLocationTypeSupported(const bool meshConnectivity) const override;

//- Get the name of the current data field
std::string getDataName() const override;
};

}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,4 @@ namespace Foam
makePatchTypeField(
fixedFluxExtrapolatedPressureFvPatchScalarField,
coupledPressureFvPatchField);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,4 @@ namespace Foam
makePatchTypeField(
fvPatchVectorField,
coupledVelocityFvPatchField);
}
}
46 changes: 46 additions & 0 deletions FF/FF.C
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ bool preciceAdapter::FF::FluidFluid::readConfig(const IOdictionary& adapterConfi
nameT_ = FFdict.lookupOrDefault<word>("nameT", "T");
DEBUG(adapterInfo(" temperature field name : " + nameT_));

// Read the name of the phase variable field (if different)
nameAlpha_ = FFdict.lookupOrDefault<word>("nameAlpha", "alpha");
DEBUG(adapterInfo(" phase variable (alpha) field name : " + nameAlpha_));

// Read the name of the face flux field (if different)
namePhi_ = FFdict.lookupOrDefault<word>("namePhi", "phi");
DEBUG(adapterInfo(" face flux field name : " + namePhi_));
Expand Down Expand Up @@ -155,6 +159,27 @@ bool preciceAdapter::FF::FluidFluid::addWriters(std::string dataName, Interface*
new Temperature(mesh_, nameT_));
DEBUG(adapterInfo("Added writer: Flow Temperature."));
}
else if (dataName.find("AlphaGradient") == 0)
{
interface->addCouplingDataWriter(
dataName,
new AlphaGradient(mesh_, nameAlpha_));
DEBUG(adapterInfo("Added writer: Alpha Gradient."));
}
else if (dataName.find("Alpha") == 0)
{
interface->addCouplingDataWriter(
dataName,
new Alpha(mesh_, nameAlpha_));
DEBUG(adapterInfo("Added writer: Alpha."));
}
else if (dataName.find("Phi") == 0)
{
interface->addCouplingDataWriter(
dataName,
new Phi(mesh_, namePhi_));
DEBUG(adapterInfo("Added writer: Phi."));
}
else
{
found = false;
Expand Down Expand Up @@ -215,6 +240,27 @@ bool preciceAdapter::FF::FluidFluid::addReaders(std::string dataName, Interface*
new Temperature(mesh_, nameT_));
DEBUG(adapterInfo("Added reader: Flow Temperature."));
}
else if (dataName.find("AlphaGradient") == 0)
{
interface->addCouplingDataReader(
dataName,
new AlphaGradient(mesh_, nameAlpha_));
DEBUG(adapterInfo("Added reader: Alpha Gradient."));
}
else if (dataName.find("Alpha") == 0)
{
interface->addCouplingDataReader(
dataName,
new Alpha(mesh_, nameAlpha_));
DEBUG(adapterInfo("Added reader: Alpha."));
}
else if (dataName.find("Phi") == 0)
{
interface->addCouplingDataReader(
dataName,
new Phi(mesh_, namePhi_));
DEBUG(adapterInfo("Added reader: Phi."));
}
else
{
found = false;
Expand Down
6 changes: 6 additions & 0 deletions FF/FF.H
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
#include "FF/Velocity.H"
#include "FF/Pressure.H"
#include "FF/Temperature.H"
#include "FF/Alpha.H"
#include "FF/PressureGradient.H"
#include "FF/VelocityGradient.H"
#include "FF/TemperatureGradient.H"
#include "FF/AlphaGradient.H"
#include "FF/Phi.H"

#include "fvCFD.H"

Expand Down Expand Up @@ -38,6 +41,9 @@ protected:
//- Name of the temperature field
std::string nameT_ = "T";

//- Name of the phase variable (alpha) field
std::string nameAlpha_ = "alpha";

//- Name of the face flux field
std::string namePhi_ = "phi";

Expand Down
3 changes: 3 additions & 0 deletions FF/ModuleFF.C
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
#include "PressureGradient.C"
#include "Temperature.C"
#include "TemperatureGradient.C"
#include "Alpha.C"
#include "AlphaGradient.C"
#include "Phi.C"
#include "BoundaryConditions/coupledPressure/coupledPressureFvPatchField.C"
#include "BoundaryConditions/coupledVelocity/coupledVelocityFvPatchField.C"
Loading

0 comments on commit 1ed787a

Please sign in to comment.