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

Mesh connectivity for preCICE v3 #297

Merged
merged 15 commits into from
Aug 7, 2023
48 changes: 28 additions & 20 deletions Interface.C
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ void preciceAdapter::Interface::configureMesh(const fvMesh& mesh, const std::str
// Initialize the index of the vertices array
int verticesIndex = 0;

// Map between OpenFOAM vertices and preCICE vertex IDs
std::map<std::tuple<double, double, double>, int> verticesMap;

// Get the locations of the mesh vertices (here: face nodes)
// for all the patches
for (uint j = 0; j < patchIDs_.size(); j++)
Expand Down Expand Up @@ -234,17 +237,30 @@ void preciceAdapter::Interface::configureMesh(const fvMesh& mesh, const std::str
// Assign the (x,y,z) locations to the vertices
// TODO: Ensure consistent order when writing/reading
for (int i = 0; i < faceNodes.size(); i++)
{
for (unsigned int d = 0; d < dim_; ++d)
{
vertices[verticesIndex++] = faceNodes[i][d];
}
if (meshConnectivity_)
{
verticesMap.emplace(std::make_tuple(faceNodes[i][0], faceNodes[i][1], faceNodes[i][2]), -1);
}
}
}

// Pass the mesh vertices information to preCICE
precice_.setMeshVertices(meshName_, vertices, vertexIDs_);

// meshConnectivity for prototype neglected
// Only set the triangles, if necessary
if (meshConnectivity_)
{
// Build the map between OpenFOAM vertices and preCICE vertex IDs
verticesIndex = 0;
for (auto& key : verticesMap)
{
key.second = vertexIDs_[verticesIndex++];
}

for (uint j = 0; j < patchIDs_.size(); j++)
{
// Define triangles
Expand All @@ -263,7 +279,6 @@ void preciceAdapter::Interface::configureMesh(const fvMesh& mesh, const std::str
// Define constants
const int triaPerQuad = 2;
const int nodesPerTria = 3;
const int componentsPerNode = 3;

// Get the list of faces and coordinates at the interface patch
const List<face> faceField = mesh.boundaryMesh()[patchIDs_.at(j)].localFaces();
Expand All @@ -277,41 +292,34 @@ void preciceAdapter::Interface::configureMesh(const fvMesh& mesh, const std::str
pointCoords -= resetField;
}

// Array to store coordinates in preCICE format
double triCoords[faceField.size() * triaPerQuad * nodesPerTria * componentsPerNode];

unsigned int coordIndex = 0;
//Array to store the IDs we get from preCICE
std::vector<int> triVertIDs;
MakisH marked this conversation as resolved.
Show resolved Hide resolved
triVertIDs.reserve(faceField.size() * triaPerQuad * nodesPerTria);

// Iterate over faces
// Triangulate all faces and collect set of nodes that form triangles,
// which are used to set mesh triangles in preCICE.
forAll(faceField, facei)
{
const face& faceQuad = faceField[facei];

// Triangulate the face
faceTriangulation faceTri(pointCoords, faceQuad, false);

// Iterate over all triangles generated out of each (quad) face
for (uint triIndex = 0; triIndex < triaPerQuad; triIndex++)
{
// Get the vertex that corresponds to the x,y,z coordinates of each node of a triangle
for (uint nodeIndex = 0; nodeIndex < nodesPerTria; nodeIndex++)
{
for (uint xyz = 0; xyz < componentsPerNode; xyz++)
triCoords[coordIndex++] = pointCoords[faceTri[triIndex][nodeIndex]][xyz];
triVertIDs.push_back(verticesMap.at(std::make_tuple(pointCoords[faceTri[triIndex][nodeIndex]][0], pointCoords[faceTri[triIndex][nodeIndex]][1], pointCoords[faceTri[triIndex][nodeIndex]][2])));
MakisH marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

//Array to store the IDs we get from preCICE
int triVertIDs[faceField.size() * (triaPerQuad * nodesPerTria)];

//Get preCICE IDs
precice_.getMeshVertexIDsFromPositions(meshID_, faceField.size() * (triaPerQuad * nodesPerTria), triCoords, triVertIDs);

DEBUG(adapterInfo("Number of triangles: " + std::to_string(faceField.size() * triaPerQuad)));

//Set Triangles
for (int facei = 0; facei < faceField.size() * triaPerQuad; facei++)
{
precice_.setMeshTriangleWithEdges(meshID_, triVertIDs[facei * nodesPerTria], triVertIDs[facei * nodesPerTria + 1], triVertIDs[facei * nodesPerTria + 2]);
}
precice_.setMeshTriangles(meshName_, triVertIDs);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions changelog-entries/297.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Changed the way mesh connectivity (face triangles) are provided to preCICE, adapting to preCICE v3 [#297](https://github.com/precice/openfoam-adapter/pull/297)