mergeBaffles.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2026 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Application
25  mergeBaffles
26 
27 Description
28  Detects faces that share points (baffles) and merge them into internal
29  faces.
30 
31 \*---------------------------------------------------------------------------*/
32 
33 #include "argList.H"
34 #include "Time.H"
35 #include "syncTools.H"
36 #include "faceSet.H"
37 #include "pointSet.H"
38 #include "meshTools.H"
39 #include "polyTopoChange.H"
40 #include "indirectPrimitivePatch.H"
41 #include "processorPolyPatch.H"
42 #include "localPointRegion.H"
43 #include "duplicatePoints.H"
44 #include "ReadFields.H"
45 #include "volFields.H"
46 #include "surfaceFields.H"
47 #include "pointFields.H"
48 
49 using namespace Foam;
50 
51 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
52 
53 void mergeDuplicateBoundaryFaces
54 (
55  const polyMesh& mesh,
56  polyTopoChange& meshMod
57 )
58 {
59  // Get all duplicate face labels in the boundary
61  (
62  mesh,
64  (
67  )
68  );
69 
70  // Check that none are on processor patches
72  forAll(duplicates, bFacei)
73  {
74  if (duplicates[bFacei] != -1)
75  {
76  label facei = mesh.nInternalFaces() + bFacei;
77  label patchi = patches.whichPatch(facei);
78 
79  if (isA<processorPolyPatch>(patches[patchi]))
80  {
82  << "Duplicate face " << facei
83  << " is on a processorPolyPatch."
84  << "This is not allowed." << nl
85  << "Face:" << facei
86  << " is on patch:" << patches[patchi].name()
87  << abort(FatalError);
88  }
89  }
90  }
91 
92  const faceList& faces = mesh.faces();
93  const labelList& faceOwner = mesh.faceOwner();
94 
95  forAll(duplicates, bFacei)
96  {
97  label otherFacei = duplicates[bFacei];
98 
99  if (otherFacei != -1 && otherFacei > bFacei)
100  {
101  // Two duplicate faces. Merge.
102 
103  label face0 = mesh.nInternalFaces() + bFacei;
104  label face1 = mesh.nInternalFaces() + otherFacei;
105 
106  label own0 = faceOwner[face0];
107  label own1 = faceOwner[face1];
108 
109  if (own0 < own1)
110  {
111  // Use face0 as the new internal face.
112 
113  meshMod.removeFace(face1, -1);
114  meshMod.modifyFace
115  (
116  faces[face0], // modified face
117  face0, // label of face being modified
118  own0, // owner
119  own1, // neighbour
120  false, // face flip
121  -1 // patch for face
122  );
123  }
124  else
125  {
126  // Use face1 as the new internal face.
127 
128  meshMod.removeFace(face0, -1);
129  meshMod.modifyFace
130  (
131  faces[face1], // modified face
132  face1, // label of face being modified
133  own1, // owner
134  own0, // neighbour
135  false, // face flip
136  -1 // patch for face
137  );
138  }
139  }
140  }
141 }
142 
143 
144 int main(int argc, char *argv[])
145 {
147  (
148  "Detect faces that share points (baffles)\n"
149  "and merge them into internal faces."
150  );
151 
152  #include "addNoOverwriteOption.H"
153  #include "addMeshOption.H"
154  #include "addRegionOption.H"
156  (
157  "fields",
158  "update fields"
159  );
160 
164 
165  #include "setNoOverwrite.H"
166  const bool fields = args.optionFound("fields");
167 
168  const word oldInstance = mesh.pointsInstance();
169 
170  // Read objects in time directory
171  IOobjectList objects(mesh, runTime.name());
172 
173  if (fields) Info<< "Reading geometric fields" << nl << endl;
174 
175  #include "readVolFields.H"
176  #include "readSurfaceFields.H"
177  #include "readPointFields.H"
178 
179  // Mesh change engine
180  polyTopoChange meshMod(mesh);
181 
182  // Merge duplicate boundary faces into internal faces.
183  mergeDuplicateBoundaryFaces(mesh, meshMod);
184 
185  if (!overwrite)
186  {
187  runTime++;
188  }
189 
190  // Change the mesh
192 
193  // Update fields
194  mesh.topoChange(map);
195 
196  if (overwrite)
197  {
198  mesh.setInstance(oldInstance);
199  }
200 
201  Info<< "Writing mesh to time " << runTime.name() << endl;
202  mesh.write();
203 
204  Info<< "End\n" << endl;
205 
206  return 0;
207 }
208 
209 
210 // ************************************************************************* //
Field reading functions for post-processing utilities.
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
List of IOobjects with searching and retrieving facilities.
Definition: IOobjectList.H:53
static void addNote(const string &)
Add extra notes for the usage information.
Definition: argList.C:152
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:111
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:114
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
const fvBoundaryMesh & boundary() const
Return reference to boundary mesh.
Definition: fvMesh.C:932
const word & name() const
Return reference to name.
Definition: fvMesh.H:447
virtual void topoChange(const polyTopoChangeMap &map)
Update mesh corresponding to the given map.
Definition: fvMesh.C:1335
static labelList findDuplicateFaces(const primitiveMesh &, const labelList &)
Helper routine to find baffles (two boundary faces using the.
Foam::polyBoundaryMesh.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:78
virtual const faceList & faces() const
Return raw faces.
Definition: polyMesh.C:1308
virtual const labelList & faceOwner() const
Return face owner.
Definition: polyMesh.C:1321
const fileName & pointsInstance() const
Return the current instance directory for points.
Definition: polyMesh.C:1006
void setInstance(const fileName &)
Set the instance for mesh files.
Definition: polyMeshIO.C:102
Direct mesh changes based on v1.3 polyTopoChange syntax.
void removeFace(const label, const label)
Remove face / merge faces.
autoPtr< polyTopoChangeMap > changeMesh(polyMesh &mesh, const bool syncParallel=true, const bool orderCells=false, const bool orderPoints=false)
Inplace changes mesh without change of patches.
void modifyFace(const face &f, const label facei, const label own, const label nei, const bool flipFaceFlux, const label patchID)
Modify vertices or cell of face.
label nInternalFaces() const
label nFaces() const
virtual bool write(const bool write=true) const
Write using setting from DB.
A class for handling words, derived from string.
Definition: word.H:63
Foam::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
int main(int argc, char *argv[])
Definition: financialFoam.C:44
label patchi
const fvPatchList & patches
Info<< "Calculating turbulent flame speed field St\n"<< endl;volScalarField St(IOobject("St", runTime.name(), mesh, IOobject::NO_READ, IOobject::AUTO_WRITE), flameWrinkling->Xi() *Su);multivariateSurfaceInterpolationScheme< scalar >::fieldTable fields
Definition: createFields.H:234
Namespace for OpenFOAM.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:288
errorManip< error > abort(error &err)
Definition: errorManip.H:131
messageStream Info
error FatalError
labelList identityMap(const label len)
Create identity map (map[i] == i) of given length.
Definition: ListOps.C:104
static const char nl
Definition: Ostream.H:297
objects
const bool overwrite
Definition: setNoOverwrite.H:1
Foam::argList args(argc, argv)
Foam::surfaceFields.