coupleGroupIdentifier.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2013-2015 OpenFOAM Foundation
9  Copyright (C) 2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "coupleGroupIdentifier.H"
30 #include "polyMesh.H"
31 #include "Time.H"
32 
33 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
34 
35 Foam::label Foam::coupleGroupIdentifier::findOtherPatchID
36 (
37  const polyMesh& mesh,
38  const polyPatch& thisPatch
39 ) const
40 {
41  const polyBoundaryMesh& pbm = mesh.boundaryMesh();
42 
43  if (!good())
44  {
46  << "Invalid coupleGroup patch group"
47  << " on patch " << thisPatch.name()
48  << " in region " << pbm.mesh().name()
49  << exit(FatalError);
50  }
51 
52  const auto fnd = pbm.groupPatchIDs().cfind(name());
53 
54  if (!fnd.good())
55  {
56  if (&mesh == &thisPatch.boundaryMesh().mesh())
57  {
58  // thisPatch should be in patchGroup
60  << "Patch " << thisPatch.name()
61  << " should be in patchGroup " << name()
62  << " in region " << pbm.mesh().name()
63  << exit(FatalError);
64  }
65 
66  return -1;
67  }
68 
69  // Mesh has patch group
70  const labelList& patchIDs = fnd.val();
71 
72  if (&mesh == &thisPatch.boundaryMesh().mesh())
73  {
74  if (patchIDs.size() > 2 || patchIDs.size() == 0)
75  {
77  << "Couple patchGroup " << name()
78  << " with contents " << patchIDs
79  << " not of size < 2"
80  << " on patch " << thisPatch.name()
81  << " region " << thisPatch.boundaryMesh().mesh().name()
82  << exit(FatalError);
83 
84  return -1;
85  }
86 
87  label index = patchIDs.find(thisPatch.index());
88 
89  if (index == -1)
90  {
92  << "Couple patchGroup " << name()
93  << " with contents " << patchIDs
94  << " does not contain patch " << thisPatch.name()
95  << " in region " << pbm.mesh().name()
96  << exit(FatalError);
97 
98  return -1;
99  }
100 
101 
102  if (patchIDs.size() == 2)
103  {
104  // Return the other patch
105  return patchIDs[1-index];
106  }
107  else // size == 1
108  {
109  return -1;
110  }
111  }
112  else
113  {
114  if (patchIDs.size() != 1)
115  {
117  << "Couple patchGroup " << name()
118  << " with contents " << patchIDs
119  << " in region " << mesh.name()
120  << " should only contain a single patch"
121  << " when matching patch " << thisPatch.name()
122  << " in region " << pbm.mesh().name()
123  << exit(FatalError);
124  }
125 
126  return patchIDs[0];
127  }
128 }
129 
130 
131 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
132 
134 {
135  dict.readIfPresent("coupleGroup", name_);
136 }
137 
138 
139 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
140 
141 Foam::label Foam::coupleGroupIdentifier::findOtherPatchID
142 (
143  const polyPatch& thisPatch
144 ) const
145 {
146  const polyBoundaryMesh& pbm = thisPatch.boundaryMesh();
147 
148  return findOtherPatchID(pbm.mesh(), thisPatch);
149 }
150 
151 
152 Foam::label Foam::coupleGroupIdentifier::findOtherPatchID
153 (
154  const polyPatch& thisPatch,
155  word& otherRegion
156 ) const
157 {
158  const polyBoundaryMesh& pbm = thisPatch.boundaryMesh();
159  const polyMesh& thisMesh = pbm.mesh();
160  const Time& runTime = thisMesh.time();
161 
162 
163  // Loop over all regions to find other patch in coupleGroup
164  label otherPatchID = -1;
165 
166  for (const polyMesh& mesh : runTime.cobjects<polyMesh>())
167  {
168  const label patchID = findOtherPatchID(mesh, thisPatch);
169 
170  if (patchID != -1)
171  {
172  if (otherPatchID != -1)
173  {
175  << "Couple patchGroup " << name()
176  << " should be present on only two patches"
177  << " in any of the meshes in "
178  << runTime.sortedNames<polyMesh>() << nl
179  << " It seems to be present on patch "
180  << thisPatch.name()
181  << " in region " << thisMesh.name()
182  << ", on patch " << otherPatchID
183  << " in region " << otherRegion
184  << " and on patch " << patchID
185  << " in region " << mesh.name() << endl
186  << exit(FatalError);
187  }
188  otherPatchID = patchID;
189  otherRegion = mesh.name();
190  }
191  }
192 
193  if (otherPatchID == -1)
194  {
196  << "Couple patchGroup " << name()
197  << " not found in any of the other meshes "
198  << flatOutput(runTime.sortedNames<polyMesh>())
199  << " on patch " << thisPatch.name()
200  << " region " << thisMesh.name()
202  }
203 
204  return otherPatchID;
205 }
206 
207 
208 void Foam::coupleGroupIdentifier::write(Ostream& os) const
209 {
210  if (!name_.empty())
211  {
212  os.writeEntry("coupleGroup", name_);
213  }
214 }
215 
216 
217 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
218 
219 Foam::Ostream& Foam::operator<<(Ostream& os, const coupleGroupIdentifier& ident)
220 {
221  ident.write(os);
223  return os;
224 }
225 
226 
227 // ************************************************************************* //
const labelList patchIDs(pbm.indices(polyPatchNames, true))
const polyBoundaryMesh & pbm
dictionary dict
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:45
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:195
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
Ostream & writeEntry(const keyType &key, const T &value)
Write a keyword/value entry.
Definition: Ostream.H:321
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
const word & name() const noexcept
Name of patchGroup.
dynamicFvMesh & mesh
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
bool good() const noexcept
The patchGroup has a non-empty name.
const polyBoundaryMesh & boundaryMesh() const noexcept
Return boundaryMesh reference.
Definition: polyPatch.C:309
const polyMesh & mesh() const noexcept
Return the mesh reference.
A class for handling words, derived from Foam::string.
Definition: word.H:63
const Time & time() const noexcept
Return time registry.
wordList sortedNames() const
The sorted names of all objects.
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
A polyBoundaryMesh is a polyPatch list with additional search methods and registered IO...
const word & name() const noexcept
The patch name.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
bool readIfPresent(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX) const
Find an entry if present, and assign to T val. FatalIOError if it is found and the number of tokens i...
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
UPtrList< const Type > cobjects() const
Return unsorted list of objects with a class satisfying isA<Type> or isType<Type> (with Strict) ...
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
const word & name() const
Return reference to name.
Definition: fvMesh.H:387
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
List< label > labelList
A List of labels.
Definition: List.H:62
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:69
void write(Ostream &os) const
Write the coupleGroup dictionary entry.
coupleGroupIdentifier()=default
Default construct.
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:225