polyTopoChanger.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) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2017-2022 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 "polyTopoChanger.H"
30 #include "polyMesh.H"
31 #include "polyTopoChange.H"
32 #include "Time.H"
33 #include "PtrListOps.H"
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 namespace Foam
38 {
39  defineTypeNameAndDebug(polyTopoChanger, 0);
40 }
41 
42 
43 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
44 
45 bool Foam::polyTopoChanger::readContents()
46 {
47  if (isReadRequired() || (isReadOptional() && headerOk()))
48  {
49  PtrList<polyMeshModifier>& modifiers = *this;
50 
51  // Read modifiers
52  Istream& is = readStream(typeName);
53 
54  PtrList<entry> patchEntries(is);
55  modifiers.setSize(patchEntries.size());
56 
57  forAll(modifiers, modifierI)
58  {
59  modifiers.set
60  (
61  modifierI,
63  (
64  patchEntries[modifierI].keyword(),
65  patchEntries[modifierI].dict(),
66  modifierI,
67  *this
68  )
69  );
70  }
71 
72  is.check(FUNCTION_NAME);
73  close();
74  return true;
75  }
76 
77  return false;
78 }
79 
80 
81 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
82 
83 Foam::polyTopoChanger::polyTopoChanger
84 (
85  const IOobject& io,
86  polyMesh& mesh
87 )
88 :
90  regIOobject(io),
91  mesh_(mesh)
92 {
93  // Warn for MUST_READ_IF_MODIFIED
94  warnNoRereading<polyTopoChanger>();
95 
96  readContents();
97 }
98 
99 
100 Foam::polyTopoChanger::polyTopoChanger
101 (
102  polyMesh& mesh,
104 
105 )
106 :
108  (
109  IOobject
110  (
111  "meshModifiers",
112  mesh.time().findInstance
113  (
114  mesh.meshDir(),
115  "meshModifiers",
116  (
117  // Safety? promote NO_READ to LAZY_READ
118  rOpt == IOobject::NO_READ
119  ? IOobject::LAZY_READ
120  : rOpt
121  )
122  ),
123  polyMesh::meshSubDir,
124  mesh,
125  rOpt,
126  IOobject::NO_WRITE
127  ),
128  mesh
129  )
130 {}
131 
133 Foam::polyTopoChanger::polyTopoChanger(polyMesh& mesh)
134 :
135  polyTopoChanger(mesh, IOobjectOption::READ_IF_PRESENT)
136 {}
137 
140 {
141  return PtrListOps::get<word>(*this, typeOp<polyMeshModifier>());
142 }
143 
146 {
147  return PtrListOps::get<word>(*this, typeOp<polyMeshModifier>());
148 }
149 
150 
152 {
153  // Go through all mesh modifiers and accumulate the morphing information
154  const PtrList<polyMeshModifier>& topoChanges = *this;
155 
156  bool triggerChange = false;
157 
158  forAll(topoChanges, morphI)
159  {
160  if (topoChanges[morphI].active())
161  {
162  bool curTriggerChange = topoChanges[morphI].changeTopology();
163 
164  if (debug)
165  {
166  Info<< "Modifier " << morphI << " named "
167  << topoChanges[morphI].name();
168 
169  if (curTriggerChange)
170  {
171  Info<< " morphing" << endl;
172  }
173  else
174  {
175  Info<< " unchanged" << endl;
176  }
177  }
178 
179  triggerChange = triggerChange || curTriggerChange;
180  }
181  else
182  {
183  if (debug)
184  {
185  Info<< "Modifier " << morphI << " named "
186  << topoChanges[morphI].name() << " inactive" << endl;
187  }
188  }
189 
190  }
191 
192  return triggerChange;
193 }
194 
195 
198 {
199  // Collect changes from all modifiers
200  const PtrList<polyMeshModifier>& topoChanges = *this;
201 
202  auto ptr = autoPtr<polyTopoChange>::New(mesh());
203  polyTopoChange& ref = ptr.ref();
204 
205  forAll(topoChanges, morphI)
206  {
207  if (topoChanges[morphI].active())
208  {
209  topoChanges[morphI].setRefinement(ref);
210  }
211  }
212 
213  return ptr;
214 }
215 
216 
218 {
219  const PtrList<polyMeshModifier>& topoChanges = *this;
220 
221  forAll(topoChanges, morphI)
222  {
223  if (topoChanges[morphI].active())
224  {
225  topoChanges[morphI].modifyMotionPoints(p);
226  }
227  }
228 }
229 
230 
231 void Foam::polyTopoChanger::update(const mapPolyMesh& m)
232 {
233  // Go through all mesh modifiers and accumulate the morphing information
234  PtrList<polyMeshModifier>& topoChanges = *this;
235 
236  forAll(topoChanges, morphI)
237  {
238  topoChanges[morphI].updateMesh(m);
239  }
240 
241  // Force the mesh modifiers to auto-write. This allows us to
242  // preserve the current state of modifiers corresponding with
243  // the mesh.
244  writeOpt(IOobject::AUTO_WRITE);
245  instance() = mesh_.time().timeName();
246 }
247 
248 
250 (
251  const bool inflate,
252  const bool syncParallel,
253  const bool orderCells,
254  const bool orderPoints
255 )
256 {
257  if (changeTopology())
258  {
259  autoPtr<polyTopoChange> ref = topoChangeRequest();
260 
261  autoPtr<mapPolyMesh> topoChangeMap = ref().changeMesh
262  (
263  mesh_,
264  inflate,
265  syncParallel,
266  orderCells,
267  orderPoints
268  );
269 
270  update(topoChangeMap());
271  mesh_.updateMesh(topoChangeMap());
272  return topoChangeMap;
273  }
275  mesh_.topoChanging(false);
276  return nullptr;
277 }
278 
279 
281 (
282  const List<polyMeshModifier*>& tm
283 )
284 {
285  setSize(tm.size());
286 
287  // Copy the patch pointers
288  forAll(tm, tmI)
289  {
290  if (tm[tmI]->topoChanger() != *this)
291  {
293  << "Mesh modifier created with different mesh reference."
294  << abort(FatalError);
295  }
296  set(tmI, tm[tmI]);
297  }
298 
299  writeOpt(IOobject::AUTO_WRITE);
300 }
301 
302 
304 (
305  const word& modName
306 ) const
307 {
308  const PtrList<polyMeshModifier>& topoChanges = *this;
309 
310  forAll(topoChanges, morphI)
311  {
312  if (topoChanges[morphI].name() == modName)
313  {
314  return morphI;
315  }
316  }
317 
318  // Modifier not found
319  if (debug)
320  {
322  << "List of available modifier names: " << names() << endl;
323  }
324 
325  // Not found, return -1
326  return -1;
327 }
328 
329 
330 bool Foam::polyTopoChanger::writeData(Ostream& os) const
331 {
332  os << *this;
333  return os.good();
334 }
335 
336 
337 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
340 {
341  return &me != this;
342 }
343 
344 
346 {
347  return &me == this;
348 }
349 
350 
351 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
352 
353 Foam::Ostream& Foam::operator<<(Ostream& os, const polyTopoChanger& mme)
354 {
355  os << mme.size() << nl << token::BEGIN_LIST;
356 
357  forAll(mme, mmeI)
358  {
359  mme[mmeI].writeDict(os);
360  }
361 
362  os << token::END_LIST;
363 
364  return os;
365 }
366 
367 
368 // ************************************************************************* //
dictionary dict
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
autoPtr< polyTopoChange > topoChangeRequest() const
Return topology change request.
void update(const mapPolyMesh &m)
Force recalculation of locally stored data on topological change.
List< word > names(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
List of names generated by calling name() for each list item and filtered for matches.
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:578
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:49
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
bool headerOk()
Read and check header info. Does not check the headerClassName.
Definition: regIOobject.C:437
Begin list [isseparator].
Definition: token.H:158
bool operator!=(const polyTopoChanger &) const
static autoPtr< polyMeshModifier > New(const word &name, const dictionary &dict, const label index, const polyTopoChanger &mme)
Select constructed from dictionary.
Functions to operate on Pointer Lists.
label findModifierID(const word &modName) const
Find modifier given a name.
const dimensionedScalar me
Electron mass.
autoPtr< mapPolyMesh > changeMesh(const bool inflate, const bool syncParallel=true, const bool orderCells=false, const bool orderPoints=false)
bool isReadOptional() const noexcept
True if (LAZY_READ) bits are set [same as READ_IF_PRESENT].
bool changeTopology() const
Is topology change required.
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:414
points setSize(newPointi)
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:38
dynamicFvMesh & mesh
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:52
void close()
Close Istream.
List of mesh modifiers defining the mesh dynamics.
End list [isseparator].
Definition: token.H:159
Virtual base class for mesh modifiers.
errorManip< error > abort(error &err)
Definition: errorManip.H:139
void modifyMotionPoints(pointField &) const
Modify point motion.
wordList types() const
Return a list of patch types.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
bool writeData(Ostream &) const
writeData member function required by regIOobject
rDeltaT ref()
int debug
Static debugging option.
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
mesh update()
defineTypeNameAndDebug(combustionModel, 0)
bool isReadRequired() const noexcept
True if (MUST_READ | READ_MODIFIED) bits are set.
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:76
#define WarningInFunction
Report a warning using Foam::Warning.
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers...
Definition: List.H:55
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:281
A simple container of IOobject preferences. Can also be used for general handling of read/no-read/rea...
Automatically write from objectRegistry::writeObject()
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:65
messageStream Info
Information stream (stdout output on master, null elsewhere)
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:73
bool operator==(const polyTopoChanger &) const
volScalarField & p
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
static autoPtr< T > New(Args &&... args)
Construct autoPtr with forwarding arguments.
Definition: autoPtr.H:178
void addTopologyModifiers(const List< polyMeshModifier *> &tm)
Add given set of topology modifiers to the topoChanger.
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:171
Istream & readStream(const word &, const bool readOnProc=true)
Return Istream and check object type against that given.
Extract type (as a word) from an object, typically using its type() method.
Definition: word.H:361
Namespace for OpenFOAM.
wordList names() const
Return a list of patch names.
readOption
Enumeration defining read preferences.