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> entries(is);
55  modifiers.resize_null(entries.size());
56 
57  forAll(modifiers, modifieri)
58  {
59  modifiers.set
60  (
61  modifieri,
63  (
64  entries[modifieri].keyword(),
65  entries[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 :
107  (
108  IOobject
109  (
110  "meshModifiers",
111  mesh.time().findInstance
112  (
113  mesh.meshDir(),
114  "meshModifiers",
115  (
116  // Safety? promote NO_READ to LAZY_READ
117  rOpt == IOobject::NO_READ
118  ? IOobject::LAZY_READ
119  : rOpt
120  )
121  ),
122  polyMesh::meshSubDir,
123  mesh,
124  rOpt,
125  IOobject::NO_WRITE
126  ),
127  mesh
128  )
129 {}
130 
132 Foam::polyTopoChanger::polyTopoChanger(polyMesh& mesh)
133 :
134  polyTopoChanger(mesh, IOobjectOption::READ_IF_PRESENT)
135 {}
136 
139 {
140  return PtrListOps::get<word>(*this, typeOp<polyMeshModifier>());
141 }
142 
145 {
146  return PtrListOps::get<word>(*this, typeOp<polyMeshModifier>());
147 }
148 
149 
151 {
152  // Go through all mesh modifiers and accumulate the morphing information
153  const PtrList<polyMeshModifier>& topoChanges = *this;
154 
155  bool triggerChange = false;
156 
157  forAll(topoChanges, morphI)
158  {
159  if (topoChanges[morphI].active())
160  {
161  bool curTriggerChange = topoChanges[morphI].changeTopology();
162 
163  if (debug)
164  {
165  Info<< "Modifier " << morphI << " named "
166  << topoChanges[morphI].name();
167 
168  if (curTriggerChange)
169  {
170  Info<< " morphing" << endl;
171  }
172  else
173  {
174  Info<< " unchanged" << endl;
175  }
176  }
177 
178  triggerChange = triggerChange || curTriggerChange;
179  }
180  else
181  {
182  if (debug)
183  {
184  Info<< "Modifier " << morphI << " named "
185  << topoChanges[morphI].name() << " inactive" << endl;
186  }
187  }
188 
189  }
190 
191  return triggerChange;
192 }
193 
194 
197 {
198  // Collect changes from all modifiers
199  const PtrList<polyMeshModifier>& topoChanges = *this;
200 
201  auto ptr = autoPtr<polyTopoChange>::New(mesh());
202  polyTopoChange& ref = ptr.ref();
203 
204  forAll(topoChanges, morphI)
205  {
206  if (topoChanges[morphI].active())
207  {
208  topoChanges[morphI].setRefinement(ref);
209  }
210  }
211 
212  return ptr;
213 }
214 
215 
217 {
218  const PtrList<polyMeshModifier>& topoChanges = *this;
219 
220  forAll(topoChanges, morphI)
221  {
222  if (topoChanges[morphI].active())
223  {
224  topoChanges[morphI].modifyMotionPoints(p);
225  }
226  }
227 }
228 
229 
230 void Foam::polyTopoChanger::update(const mapPolyMesh& m)
231 {
232  // Go through all mesh modifiers and accumulate the morphing information
233  PtrList<polyMeshModifier>& topoChanges = *this;
234 
235  forAll(topoChanges, morphI)
236  {
237  topoChanges[morphI].updateMesh(m);
238  }
239 
240  // Force the mesh modifiers to auto-write. This allows us to
241  // preserve the current state of modifiers corresponding with
242  // the mesh.
243  writeOpt(IOobject::AUTO_WRITE);
244  instance() = mesh_.time().timeName();
245 }
246 
247 
249 (
250  const bool inflate,
251  const bool syncParallel,
252  const bool orderCells,
253  const bool orderPoints
254 )
255 {
256  if (changeTopology())
257  {
258  autoPtr<polyTopoChange> ref = topoChangeRequest();
259 
260  autoPtr<mapPolyMesh> topoChangeMap = ref().changeMesh
261  (
262  mesh_,
263  inflate,
264  syncParallel,
265  orderCells,
266  orderPoints
267  );
268 
269  update(topoChangeMap());
270  mesh_.updateMesh(topoChangeMap());
271  return topoChangeMap;
272  }
274  mesh_.topoChanging(false);
275  return nullptr;
276 }
277 
278 
280 (
281  const List<polyMeshModifier*>& tm
282 )
283 {
284  setSize(tm.size());
285 
286  // Copy the patch pointers
287  forAll(tm, tmI)
288  {
289  if (tm[tmI]->topoChanger() != *this)
290  {
292  << "Mesh modifier created with different mesh reference."
293  << abort(FatalError);
294  }
295  set(tmI, tm[tmI]);
296  }
297 
298  writeOpt(IOobject::AUTO_WRITE);
299 }
300 
301 
303 (
304  const word& modName
305 ) const
306 {
307  const PtrList<polyMeshModifier>& topoChanges = *this;
308 
309  forAll(topoChanges, morphI)
310  {
311  if (topoChanges[morphI].name() == modName)
312  {
313  return morphI;
314  }
315  }
316 
317  // Modifier not found
318  if (debug)
319  {
321  << "List of available modifier names: " << names() << endl;
322  }
323 
324  // Not found, return -1
325  return -1;
326 }
327 
328 
329 bool Foam::polyTopoChanger::writeData(Ostream& os) const
330 {
331  os << *this;
332  return os.good();
333 }
334 
335 
336 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
339 {
340  return &me != this;
341 }
342 
343 
345 {
346  return &me == this;
347 }
348 
349 
350 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
351 
352 Foam::Ostream& Foam::operator<<(Ostream& os, const polyTopoChanger& mme)
353 {
354  os << mme.size() << nl << token::BEGIN_LIST;
355 
356  forAll(mme, mmeI)
357  {
358  mme[mmeI].writeDict(os);
359  }
360 
361  os << token::END_LIST;
362 
363  return os;
364 }
365 
366 
367 // ************************************************************************* //
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:598
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
bool headerOk()
Read and check header info. Does not check the headerClassName.
Definition: regIOobject.C:505
Begin list [isseparator].
Definition: token.H:161
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:421
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 expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
void close()
Close Istream.
List of mesh modifiers defining the mesh dynamics.
End list [isseparator].
Definition: token.H:162
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:56
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:77
#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:66
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:74
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:172
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.