initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / dynamicMesh / polyTopoChange / polyTopoChanger / polyTopoChanger.C
blobc89388e3199023bc705b730902b8f2ea6f7f0b10
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "polyTopoChanger.H"
28 #include "polyMesh.H"
29 #include "polyTopoChange.H"
30 #include "Time.H"
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 namespace Foam
36     defineTypeNameAndDebug(polyTopoChanger, 0);
40 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
42 void Foam::polyTopoChanger::readModifiers()
44     if
45     (
46         readOpt() == IOobject::MUST_READ
47      || (readOpt() == IOobject::READ_IF_PRESENT && headerOk())
48     )
49     {
50         PtrList<polyMeshModifier>& modifiers = *this;
52         // Read modifiers
53         Istream& is = readStream(typeName);
55         PtrList<entry> patchEntries(is);
56         modifiers.setSize(patchEntries.size());
58         forAll(modifiers, modifierI)
59         {
60             modifiers.set
61             (
62                 modifierI,
63                 polyMeshModifier::New
64                 (
65                     patchEntries[modifierI].keyword(),
66                     patchEntries[modifierI].dict(),
67                     modifierI,
68                     *this
69                 )
70             );
71         }
73         // Check state of IOstream
74         is.check
75         (
76             "polyTopoChanger::polyTopoChanger"
77             "(const IOobject&, const polyMesh&)"
78         );
80         close();
81     }
85 // Read constructor given IOobject and a polyMesh reference
86 Foam::polyTopoChanger::polyTopoChanger
88     const IOobject& io,
89     polyMesh& mesh
92     PtrList<polyMeshModifier>(),
93     regIOobject(io),
94     mesh_(mesh)
96     readModifiers();
100 // Read constructor given IOobject and a polyMesh reference
101 Foam::polyTopoChanger::polyTopoChanger(polyMesh& mesh)
103     PtrList<polyMeshModifier>(),
104     regIOobject
105     (
106         IOobject
107         (
108             "meshModifiers",
109             mesh.time().findInstance
110             (
111                 mesh.meshDir(),
112                 "meshModifiers",
113                 IOobject::READ_IF_PRESENT
114             ),
115             mesh.meshSubDir,
116             mesh,
117             IOobject::READ_IF_PRESENT,
118             IOobject::NO_WRITE
119         )
120     ),
121     mesh_(mesh)
123     readModifiers();
127 // Return a list of modifier types
128 Foam::wordList Foam::polyTopoChanger::types() const
130     const PtrList<polyMeshModifier>& modifiers = *this;
132     wordList t(modifiers.size());
134     forAll (modifiers, modifierI)
135     {
136         t[modifierI] = modifiers[modifierI].type();
137     }
139     return t;
143 // Return a list of modifier names
144 Foam::wordList Foam::polyTopoChanger::names() const
146     const PtrList<polyMeshModifier>& modifiers = *this;
148     wordList t(modifiers.size());
150     forAll (modifiers, modifierI)
151     {
152         t[modifierI] = modifiers[modifierI].name();
153     }
155     return t;
159 // Is topology change required
160 bool Foam::polyTopoChanger::changeTopology() const
162     // Go through all mesh modifiers and accumulate the morphing information
163     const PtrList<polyMeshModifier>& topoChanges = *this;
165     bool triggerChange = false;
167     forAll (topoChanges, morphI)
168     {
169         if (topoChanges[morphI].active())
170         {
171             bool curTriggerChange = topoChanges[morphI].changeTopology();
173             if (debug)
174             {
175                 Info<< "Modifier " << morphI << " named "
176                     << topoChanges[morphI].name();
177                 
178                 if (curTriggerChange)
179                 {
180                     Info << " morphing" << endl;
181                 }
182                 else
183                 {
184                     Info << " unchanged" << endl;
185                 }
186             }
188             triggerChange = triggerChange || curTriggerChange;
189         }
190         else
191         {
192             if (debug)
193             {
194                 Info<< "Modifier " << morphI  << " named "
195                     << topoChanges[morphI].name() << " inactive" << endl;
196             }
197         }
198             
199     }
201     return triggerChange;
205 // Return topology change request
206 Foam::autoPtr<Foam::polyTopoChange>
207 Foam::polyTopoChanger::topoChangeRequest() const
209     // Collect changes from all modifiers
210     const PtrList<polyMeshModifier>& topoChanges = *this;
212     polyTopoChange* refPtr(new polyTopoChange(mesh()));
213     polyTopoChange& ref = *refPtr;
215     forAll (topoChanges, morphI)
216     {
217         if (topoChanges[morphI].active())
218         {
219             topoChanges[morphI].setRefinement(ref);
220         }
221     }
223     return autoPtr<polyTopoChange>(refPtr);
227 // Correct polyTopoChanger after moving points
228 void Foam::polyTopoChanger::modifyMotionPoints(pointField& p) const
230     const PtrList<polyMeshModifier>& topoChanges = *this;
232     forAll (topoChanges, morphI)
233     {
234         if (topoChanges[morphI].active())
235         {
236             topoChanges[morphI].modifyMotionPoints(p);
237         }
238     }
242 // Force recalculation of locally stored data on topological change
243 void Foam::polyTopoChanger::update(const mapPolyMesh& m)
245     // Go through all mesh modifiers and accumulate the morphing information
246     PtrList<polyMeshModifier>& topoChanges = *this;
248     forAll (topoChanges, morphI)
249     {
250         topoChanges[morphI].updateMesh(m);
251     }
253     // Force the mesh modifiers to auto-write.  This allows us to
254     // preserve the current state of modifiers corresponding with
255     // the mesh.  
256     writeOpt() = IOobject::AUTO_WRITE;
257     instance() = mesh_.time().timeName();
261 Foam::autoPtr<Foam::mapPolyMesh> Foam::polyTopoChanger::changeMesh
263     const bool inflate,
264     const bool syncParallel,
265     const bool orderCells,
266     const bool orderPoints
269     if (changeTopology())
270     {
271         autoPtr<polyTopoChange> ref = topoChangeRequest();
273         autoPtr<mapPolyMesh> topoChangeMap = ref().changeMesh
274         (
275             mesh_,
276             inflate,
277             syncParallel,
278             orderCells,
279             orderPoints
280         );
282         update(topoChangeMap());
283         mesh_.updateMesh(topoChangeMap());
284         return topoChangeMap;
285     }
286     else
287     {
288         return autoPtr<mapPolyMesh>(NULL);
289     }
293 // Add mesh modifiers to the morph engine
294 void Foam::polyTopoChanger::addTopologyModifiers
296     const List<polyMeshModifier*>& tm
299     setSize(tm.size());
301     // Copy the patch pointers
302     forAll (tm, tmI)
303     {
304         if (tm[tmI]->topoChanger() != *this)
305         {
306             FatalErrorIn
307             (
308                 "void polyTopoChanger::addTopologyModifiers("
309                 "const List<polyMeshModifier*>& tm)"
310             )   << "Mesh modifier created with different mesh reference."
311                 << abort(FatalError);
312         }
313         set(tmI, tm[tmI]);
314     }
316     writeOpt() = IOobject::AUTO_WRITE;
320 Foam::label Foam::polyTopoChanger::findModifierID
322     const word& modName
323 ) const
325     const PtrList<polyMeshModifier>& topoChanges = *this;
327     forAll (topoChanges, morphI)
328     {
329         if (topoChanges[morphI].name() == modName)
330         {
331             return morphI;
332         }
333     }
335     // Modifier not found
336     if (debug)
337     {
338         Info<< "label polyTopoChanger::::findModifierID(const word& "
339             << "modName) const"
340             << "Modifier named " << modName << " not found.  "
341             << "List of available modifier names: " << names() << endl;
342     }
344     // Not found, return -1
345     return -1;
349 // writeData member function required by regIOobject
350 bool Foam::polyTopoChanger::writeData(Ostream& os) const
352     os << *this;
353     return os.good();
357 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
359 bool Foam::polyTopoChanger::operator!=(const polyTopoChanger& me) const
361     return &me != this;
365 bool Foam::polyTopoChanger::operator==(const polyTopoChanger& me) const
367     return &me == this;
371 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
373 Foam::Ostream& Foam::operator<<(Ostream& os, const polyTopoChanger& mme)
375     os  << mme.size() << nl << token::BEGIN_LIST;
377     forAll(mme, mmeI)
378     {
379         mme[mmeI].writeDict(os);
380     }
382     os  << token::END_LIST;
384     return os;
388 // ************************************************************************* //