stitchMesh.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-2023 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  stitchMesh
26 
27 Description
28  'Stitches' a mesh.
29 
30  Takes a mesh and two patches and merges the faces on the two patches
31  (if geometrically possible) so the faces become internal.
32 
33  Can do
34  - 'perfect' match: faces and points on patches align exactly. Order might
35  be different though.
36  - 'integral' match: where the surfaces on both patches exactly
37  match but the individual faces not
38  - 'partial' match: where the non-overlapping part of the surface remains
39  in the respective patch.
40 
41  Note : Is just a front-end to perfectInterface/slidingInterface.
42 
43  Comparable to running a meshModifier of the form
44  (if masterPatch is called "M" and slavePatch "S"):
45 
46  \verbatim
47  couple
48  {
49  type slidingInterface;
50  masterFaceZoneName MSMasterZone
51  slaveFaceZoneName MSSlaveZone
52  cutPointZoneName MSCutPointZone
53  cutFaceZoneName MSCutFaceZone
54  masterPatchName M;
55  slavePatchName S;
56  typeOfMatch partial or integral
57  }
58  \endverbatim
59 
60 
61 \*---------------------------------------------------------------------------*/
62 
63 #include "argList.H"
64 #include "polyTopoChanger.H"
65 #include "polyTopoChangeMap.H"
66 #include "slidingInterface.H"
67 #include "perfectInterface.H"
68 #include "ReadFields.H"
69 #include "volFields.H"
70 #include "surfaceFields.H"
71 #include "pointFields.H"
72 
73 using namespace Foam;
74 
75 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
76 
77 label addPointZone(const polyMesh& mesh, const word& name)
78 {
79  label zoneID = mesh.pointZones().findZoneID(name);
80 
81  if (zoneID != -1)
82  {
83  Info<< "Reusing existing pointZone "
84  << mesh.pointZones()[zoneID].name()
85  << " at index " << zoneID << endl;
86  }
87  else
88  {
89  meshPointZones& pointZones = const_cast<polyMesh&>(mesh).pointZones();
90  zoneID = pointZones.size();
91  Info<< "Adding pointZone " << name << " at index " << zoneID << endl;
92 
93  pointZones.setSize(zoneID+1);
94  pointZones.set
95  (
96  zoneID,
97  new pointZone
98  (
99  name,
100  labelList(0),
101  zoneID,
102  pointZones
103  )
104  );
105  }
106  return zoneID;
107 }
108 
109 
110 label addFaceZone(const polyMesh& mesh, const word& name)
111 {
112  label zoneID = mesh.faceZones().findZoneID(name);
113 
114  if (zoneID != -1)
115  {
116  Info<< "Reusing existing faceZone " << mesh.faceZones()[zoneID].name()
117  << " at index " << zoneID << endl;
118  }
119  else
120  {
121  meshFaceZones& faceZones = const_cast<polyMesh&>(mesh).faceZones();
122  zoneID = faceZones.size();
123  Info<< "Adding faceZone " << name << " at index " << zoneID << endl;
124 
125  faceZones.setSize(zoneID+1);
126  faceZones.set
127  (
128  zoneID,
129  new faceZone
130  (
131  name,
132  labelList(0),
133  boolList(),
134  zoneID,
135  faceZones
136  )
137  );
138  }
139  return zoneID;
140 }
141 
142 
143 label addCellZone(const polyMesh& mesh, const word& name)
144 {
145  label zoneID = mesh.cellZones().findZoneID(name);
146 
147  if (zoneID != -1)
148  {
149  Info<< "Reusing existing cellZone " << mesh.cellZones()[zoneID].name()
150  << " at index " << zoneID << endl;
151  }
152  else
153  {
154  meshCellZones& cellZones = const_cast<polyMesh&>(mesh).cellZones();
155  zoneID = cellZones.size();
156  Info<< "Adding cellZone " << name << " at index " << zoneID << endl;
157 
158  cellZones.setSize(zoneID+1);
159  cellZones.set
160  (
161  zoneID,
162  new cellZone
163  (
164  name,
165  labelList(0),
166  zoneID,
167  cellZones
168  )
169  );
170  }
171  return zoneID;
172 }
173 
174 
175 // Checks whether patch present
176 void checkPatch(const polyBoundaryMesh& bMesh, const word& name)
177 {
178  const label patchi = bMesh.findPatchID(name);
179 
180  if (patchi == -1)
181  {
183  << "Cannot find patch " << name << endl
184  << "It should be present and of non-zero size" << endl
185  << "Valid patches are " << bMesh.names()
186  << exit(FatalError);
187  }
188 
189  if (bMesh[patchi].empty())
190  {
192  << "Patch " << name << " is present but zero size"
193  << exit(FatalError);
194  }
195 }
196 
197 
198 
199 int main(int argc, char *argv[])
200 {
202  (
203  "Merge the faces on the specified patches (if geometrically possible)\n"
204  "so the faces become internal.\n"
205  "Integral matching is used when the options -partial and -perfect are "
206  "omitted.\n"
207  );
208 
210  #include "addOverwriteOption.H"
211  #include "addRegionOption.H"
213  (
214  "noFields",
215  "do not update fields"
216  );
217 
218  argList::validArgs.append("masterPatch");
219  argList::validArgs.append("slavePatch");
220 
222  (
223  "partial",
224  "couple partially overlapping patches (optional)"
225  );
227  (
228  "perfect",
229  "couple perfectly aligned patches (optional)"
230  );
232  (
233  "toleranceDict",
234  "file",
235  "dictionary file with tolerances"
236  );
237 
238  #include "setRootCase.H"
240  #include "createNamedMesh.H"
241 
242  const word oldInstance = mesh.pointsInstance();
243 
244  const word masterPatchName = args[1];
245  const word slavePatchName = args[2];
246 
247  const bool partialCover = args.optionFound("partial");
248  const bool perfectCover = args.optionFound("perfect");
249  const bool overwrite = args.optionFound("overwrite");
250  const bool fields = !args.optionFound("noFields");
251 
252  if (partialCover && perfectCover)
253  {
255  << "Cannot supply both partial and perfect." << endl
256  << "Use perfect match option if the patches perfectly align"
257  << " (both vertex positions and face centres)" << endl
258  << exit(FatalError);
259  }
260 
261 
262  const word mergePatchName(masterPatchName + slavePatchName);
263  const word cutZoneName(mergePatchName + "CutFaceZone");
264 
266 
267  if (partialCover)
268  {
269  Info<< "Coupling partially overlapping patches "
270  << masterPatchName << " and " << slavePatchName << nl
271  << "Resulting internal faces will be in faceZone " << cutZoneName
272  << nl
273  << "Any uncovered faces will remain in their patch"
274  << endl;
275 
277  }
278  else if (perfectCover)
279  {
280  Info<< "Coupling perfectly aligned patches "
281  << masterPatchName << " and " << slavePatchName << nl
282  << "Resulting (internal) faces will be in faceZone " << cutZoneName
283  << nl << nl
284  << "Note: both patches need to align perfectly." << nl
285  << "Both the vertex"
286  << " positions and the face centres need to align to within" << nl
287  << "a tolerance given by the minimum edge length on the patch"
288  << endl;
289  }
290  else
291  {
292  Info<< "Coupling patches " << masterPatchName << " and "
293  << slavePatchName << nl
294  << "Resulting (internal) faces will be in faceZone " << cutZoneName
295  << nl << nl
296  << "Note: the overall area covered by both patches should be"
297  << " identical (\"integral\" interface)." << endl
298  << "If this is not the case use the -partial option" << nl << endl;
299  }
300 
301  // set up the tolerances for the sliding mesh
302  dictionary slidingTolerances;
303  if (args.options().found("toleranceDict"))
304  {
305  IOdictionary toleranceFile
306  (
307  IOobject
308  (
309  args.options()["toleranceDict"],
310  runTime.constant(),
311  mesh,
314  )
315  );
316  slidingTolerances += toleranceFile;
317  }
318 
319  // Check for non-empty master and slave patches
320  checkPatch(mesh.boundaryMesh(), masterPatchName);
321  checkPatch(mesh.boundaryMesh(), slavePatchName);
322 
323  // Create and add face zones and mesh modifiers
324 
325  // Master patch
326  const polyPatch& masterPatch = mesh.boundaryMesh()[masterPatchName];
327 
328  // Make list of masterPatch faces
329  labelList isf(masterPatch.size());
330 
331  forAll(isf, i)
332  {
333  isf[i] = masterPatch.start() + i;
334  }
335 
336  polyTopoChanger stitcher(mesh);
337  stitcher.setSize(1);
338 
339  mesh.pointZones().clearAddressing();
340  mesh.faceZones().clearAddressing();
341  mesh.cellZones().clearAddressing();
342 
343  if (perfectCover)
344  {
345  // Add empty zone for resulting internal faces
346  label cutZoneID = addFaceZone(mesh, cutZoneName);
347 
348  mesh.faceZones()[cutZoneID].resetAddressing
349  (
350  isf,
351  boolList(masterPatch.size(), false)
352  );
353 
354  // Add the perfect interface mesh modifier
355  stitcher.set
356  (
357  0,
358  new perfectInterface
359  (
360  "couple",
361  0,
362  stitcher,
363  cutZoneName,
364  masterPatchName,
365  slavePatchName
366  )
367  );
368  }
369  else
370  {
371  label pointZoneID = addPointZone(mesh, mergePatchName + "CutPointZone");
372  mesh.pointZones()[pointZoneID] = labelList(0);
373 
374  label masterZoneID = addFaceZone(mesh, mergePatchName + "MasterZone");
375 
376  mesh.faceZones()[masterZoneID].resetAddressing
377  (
378  isf,
379  boolList(masterPatch.size(), false)
380  );
381 
382  // Slave patch
383  const polyPatch& slavePatch = mesh.boundaryMesh()[slavePatchName];
384 
385  labelList osf(slavePatch.size());
386 
387  forAll(osf, i)
388  {
389  osf[i] = slavePatch.start() + i;
390  }
391 
392  label slaveZoneID = addFaceZone(mesh, mergePatchName + "SlaveZone");
393  mesh.faceZones()[slaveZoneID].resetAddressing
394  (
395  osf,
396  boolList(slavePatch.size(), false)
397  );
398 
399  // Add empty zone for cut faces
400  label cutZoneID = addFaceZone(mesh, cutZoneName);
401  mesh.faceZones()[cutZoneID].resetAddressing
402  (
403  labelList(0),
404  boolList(0, false)
405  );
406 
407 
408  // Add the sliding interface mesh modifier
409  stitcher.set
410  (
411  0,
412  new slidingInterface
413  (
414  "couple",
415  0,
416  stitcher,
417  mergePatchName + "MasterZone",
418  mergePatchName + "SlaveZone",
419  mergePatchName + "CutPointZone",
420  cutZoneName,
421  masterPatchName,
422  slavePatchName,
423  tom, // integral or partial
424  true // couple/decouple mode
425  )
426  );
427  static_cast<slidingInterface&>(stitcher[0]).setTolerances
428  (
429  slidingTolerances,
430  true
431  );
432  }
433 
434 
435  // Search for list of objects for this time
436  IOobjectList objects(mesh, runTime.name());
437 
438  if (fields) Info<< "Reading geometric fields" << nl << endl;
439 
440  #include "readVolFields.H"
441  // #include "readSurfaceFields.H"
442  #include "readPointFields.H"
443 
444  if (!overwrite)
445  {
446  runTime++;
447  }
448 
449  // Execute all polyMeshModifiers
450  autoPtr<polyTopoChangeMap> map = stitcher.changeMesh(true);
451 
452  mesh.setPoints(map->preMotionPoints());
453 
454  // Write mesh
455  if (overwrite)
456  {
457  mesh.setInstance(oldInstance);
458  stitcher.instance() = oldInstance;
459  stitcher.writeOpt() = IOobject::NO_WRITE;
460  }
461  Info<< nl << "Writing polyMesh to time " << runTime.name() << endl;
462 
464 
465  // Bypass runTime write (since only writes at writeTime)
466  if
467  (
468  !runTime.objectRegistry::writeObject
469  (
470  runTime.writeFormat(),
472  runTime.writeCompression(),
473  true
474  )
475  )
476  {
478  << "Failed writing polyMesh."
479  << exit(FatalError);
480  }
481 
482  mesh.faceZones().write();
483  mesh.pointZones().write();
484  mesh.cellZones().write();
485 
486  // Write fields
487  runTime.write();
488 
489  Info<< "End\n" << endl;
490 
491  return 0;
492 }
493 
494 
495 // ************************************************************************* //
Field reading functions for post-processing utilities.
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:57
List of IOobjects with searching and retrieving facilities.
Definition: IOobjectList.H:53
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
@ MUST_READ_IF_MODIFIED
Definition: IOobject.H:119
const word & name() const
Return name.
Definition: IOobject.H:310
static const versionNumber currentVersion
Current version number.
Definition: IOstream.H:203
static unsigned int defaultPrecision()
Return the default precision.
Definition: IOstream.H:458
label findZoneID(const word &zoneName) const
Find zone index given a name.
Definition: MeshZones.C:341
void clearAddressing()
Clear addressing.
Definition: MeshZones.C:387
bool set(const label) const
Is element set.
Definition: PtrListI.H:65
void setSize(const label)
Reset size of PtrList. If extending the PtrList, new entries are.
Definition: PtrList.C:131
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:128
static void addNote(const string &)
Add extra notes for the usage information.
Definition: argList.C:159
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:118
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:114
static void noParallel()
Remove the parallel options.
Definition: argList.C:175
const Foam::HashTable< string > & options() const
Return options.
Definition: argListI.H:96
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:153
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
A subset of mesh cells.
Definition: cellZone.H:64
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:160
A subset of mesh faces organised as a primitive patch.
Definition: faceZone.H:68
Hack of attachDetach to couple patches when they perfectly align. Does not decouple....
A subset of mesh points. The labels of points in the zone can be obtained from the addressing() list.
Definition: pointZone.H:65
Foam::polyBoundaryMesh.
label findPatchID(const word &patchName) const
Find patch index given a name.
wordList names() const
Return a list of patch names.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:80
const meshFaceZones & faceZones() const
Return face zones.
Definition: polyMesh.H:447
const fileName & pointsInstance() const
Return the current instance directory for points.
Definition: polyMesh.C:1019
const meshPointZones & pointZones() const
Return point zones.
Definition: polyMesh.H:441
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:405
void setInstance(const fileName &)
Set the instance for mesh files.
Definition: polyMeshIO.C:78
const meshCellZones & cellZones() const
Return cell zones.
Definition: polyMesh.H:453
virtual void setPoints(const pointField &)
Reset the points.
Definition: polyMesh.C:1448
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:70
label start() const
Return start label of this patch in the polyMesh face list.
Definition: polyPatch.H:280
List of mesh modifiers defining the mesh dynamics.
virtual bool write(const bool write=true) const
Write using setting from DB.
Sliding interface mesh modifier. Given two face zones, couple the master and slave side using a cutti...
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
int main(int argc, char *argv[])
Definition: financialFoam.C:44
const polyBoundaryMesh & bMesh
label patchi
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:230
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
List< label > labelList
A List of labels.
Definition: labelList.H:56
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:251
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
messageStream Info
List< bool > boolList
Bool container classes.
Definition: boolList.H:50
layerAndWeight max(const layerAndWeight &a, const layerAndWeight &b)
error FatalError
static const char nl
Definition: Ostream.H:260
DynamicID< meshPointZones > pointZoneID
Foam::pointZoneID.
Definition: ZoneIDs.H:47
objects
Foam::argList args(argc, argv)
Foam::surfaceFields.