English
OpenFOAM Programming Tips
Keywords:
• OpenFOAM
• findPatchID
• gSum
Fumiya Nozaki
• faceCells
Last Updated: 1 June 2014
1. How to get patch’s label from patch’s name
label patchID = mesh.boundaryMesh().findPatchID("NAME_OF_PATCH");
Info << "patchID = " << patchID << endl;
Example
5
(
0 inlet
{
type patch; label patchID = mesh.boundaryMesh().findPatchID(“upperWall");
nFaces 30;
startFace 24170;
} Info << "patchID = " << patchID << endl;
1 outlet
{
type patch;
nFaces 57; ⇒ patchID = 2
startFace 24200;
}
2 upperWall
{
type wall;
inGroups 1(wall);
nFaces 223;
startFace 24257;
}
3 lowerWall
{
type wall;
inGroups 1(wall);
nFaces 250;
startFace 24480;
}
…
)
2
2. How to calculate the sum over the specified patch
We can calculate the total outlet flux
by summing the field phi over the patch named outlet:
label outletPatchID = mesh.boundaryMesh().findPatchID(“outlet");
scalar outFlux = gSum(phi.boundaryField()[outletPatchID]);
Info << “Volumetric flux = " << outFlux << “ [m^3/s]” << endl;
gSum() sums over all the processors in a parallel run
If you calculate the total “inlet” flux using the above code, it takes the
negative value because the face normal vectors point in the opposite
direction from the inlet velocities.
inlet outlet
mesh.Sf()
3
3. How to get a boundary value of a variable
We can get the velocity on the outlet patch using the following code:
label patchID = mesh.boundaryMesh().findPatchID(“outlet");
forAll(mesh.boundary()[patchID], faceI)
{
Info<< U.boundaryField()[patchID][faceI] << endl;
}
outlet
faceI=2
U.boundaryField()[patchID][2]
faceI=1
U.boundaryField()[patchID][1]
faceI=0
U.boundaryField()[patchID][0]
4
4. How to get variable values in the cells adjacent to a patch
We can get the label list of cells adjacent to patch using faceCells():
label patchID = mesh.boundaryMesh().findPatchID(“outlet");
forAll(mesh.boundary()[patchID], faceI)
{
Info<< U[mesh.boundary()[patchID].faceCells()[faceI]] << endl;
}
outlet
faceI=2
U[mesh.boundary()[patchID].faceCells()[1]]
faceI=1
faceI=0
5
I will continuously update this slide in the future.
Kindly let me know
if you have any ideas about what topics to cover.
6
Thank
You!
7