Fix tutorials: coupled/conjugateHeatFoam/conjugateCavity: fix Allrun file
[OpenFOAM-1.6-ext.git] / src / topoChangerFvMesh / linearValveLayersFvMesh / linearValveLayersFvMesh.C
blob0e026732b4cc3d9800acfd4e949804f7055b6005
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright held by original author
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 "linearValveLayersFvMesh.H"
28 #include "Time.H"
29 #include "slidingInterface.H"
30 #include "layerAdditionRemoval.H"
31 #include "pointField.H"
32 #include "mapPolyMesh.H"
33 #include "polyTopoChange.H"
34 #include "volMesh.H"
35 #include "addToRunTimeSelectionTable.H"
37 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
39 namespace Foam
41     defineTypeNameAndDebug(linearValveLayersFvMesh, 0);
43     addToRunTimeSelectionTable
44     (
45         topoChangerFvMesh,
46         linearValveLayersFvMesh,
47         IOobject
48     );
52 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
54 void Foam::linearValveLayersFvMesh::addZonesAndModifiers()
56     // Add zones and modifiers for motion action
58     if
59     (
60         pointZones().size() > 0
61      || faceZones().size() > 0
62      || cellZones().size() > 0
63     )
64     {
65         Info<< "void linearValveLayersFvMesh::addZonesAndModifiers() : "
66             << "Zones and modifiers already present.  Skipping."
67             << endl;
69         if (topoChanger_.size() == 0)
70         {
71             FatalErrorIn
72             (
73                 "void linearValveLayersFvMesh::addZonesAndModifiers()"
74             )   << "Mesh modifiers not read properly"
75                 << abort(FatalError);
76         }
78         return;
79     }
81     Info<< "Time = " << time().timeName() << endl
82         << "Adding zones and modifiers to the mesh" << endl;
84     // Add zones
85     List<pointZone*> pz(1);
86     List<faceZone*> fz(4);
87     List<cellZone*> cz(0);
90     // Add an empty zone for cut points
92     pz[0] = new pointZone
93     (
94         "cutPointZone",
95         labelList(0),
96         0,
97         pointZones()
98     );
101     // Do face zones for slider
103     // Inner slider
104     const word innerSliderName(motionDict_.subDict("slider").lookup("inside"));
105     const polyPatch& innerSlider =
106         boundaryMesh()[boundaryMesh().findPatchID(innerSliderName)];
108     labelList isf(innerSlider.size());
110     forAll (isf, i)
111     {
112         isf[i] = innerSlider.start() + i;
113     }
115     fz[0] = new faceZone
116     (
117         "insideSliderZone",
118         isf,
119         boolList(innerSlider.size(), false),
120         0,
121         faceZones()
122     );
124     // Outer slider
125     const word outerSliderName
126     (
127         motionDict_.subDict("slider").lookup("outside")
128     );
130     const polyPatch& outerSlider =
131         boundaryMesh()[boundaryMesh().findPatchID(outerSliderName)];
133     labelList osf(outerSlider.size());
135     forAll (osf, i)
136     {
137         osf[i] = outerSlider.start() + i;
138     }
140     fz[1] = new faceZone
141     (
142         "outsideSliderZone",
143         osf,
144         boolList(outerSlider.size(), false),
145         1,
146         faceZones()
147     );
149     // Add empty zone for cut faces
150     fz[2] = new faceZone
151     (
152         "cutFaceZone",
153         labelList(0),
154         boolList(0, false),
155         2,
156         faceZones()
157     );
159     // Add face zone for layer addition
160     const word layerPatchName
161     (
162         motionDict_.subDict("layer").lookup("patch")
163     );
165     const polyPatch& layerPatch =
166         boundaryMesh()[boundaryMesh().findPatchID(layerPatchName)];
168     labelList lpf(layerPatch.size());
170     forAll (lpf, i)
171     {
172         lpf[i] = layerPatch.start() + i;
173     }
175     fz[3] = new faceZone
176     (
177         "valveLayerZone",
178         lpf,
179         boolList(layerPatch.size(), true),
180         0,
181         faceZones()
182     );
185     Info << "Adding point and face zones" << endl;
186     addZones(pz, fz, cz);
188     // Add a topology modifier
189     topoChanger_.setSize(2);
190     topoChanger_.set
191     (
192         0,
193         new slidingInterface
194         (
195             "valveSlider",
196             0,
197             topoChanger_,
198             outerSliderName + "Zone",
199             innerSliderName + "Zone",
200             "cutPointZone",
201             "cutFaceZone",
202             outerSliderName,
203             innerSliderName,
204             slidingInterface::INTEGRAL,   // Edge matching algorithm
205             true,                         // Attach-detach action
206             intersection::VISIBLE         // Projection algorithm
207         )
208     );
210     topoChanger_.set
211     (
212         1,
213         new layerAdditionRemoval
214         (
215             "valveLayer",
216             1,
217             topoChanger_,
218             "valveLayerZone",
219             readScalar
220             (
221                 motionDict_.subDict("layer").lookup("minThickness")
222             ),
223             readScalar
224             (
225                 motionDict_.subDict("layer").lookup("maxThickness")
226             )
227         )
228     );
230     // Write mesh and modifiers
231     topoChanger_.writeOpt() = IOobject::AUTO_WRITE;
232     topoChanger_.write();
233     write();
237 void Foam::linearValveLayersFvMesh::makeLayersLive()
239     const polyTopoChanger& topoChanges = topoChanger_;
241     // Enable layering
242     forAll (topoChanges, modI)
243     {
244         if (isA<layerAdditionRemoval>(topoChanges[modI]))
245         {
246             topoChanges[modI].enable();
247         }
248         else if (isA<slidingInterface>(topoChanges[modI]))
249         {
250             topoChanges[modI].disable();
251         }
252         else
253         {
254             FatalErrorIn("void linearValveLayersFvMesh::makeLayersLive()")
255                 << "Don't know what to do with mesh modifier "
256                 << modI << " of type " << topoChanges[modI].type()
257                 << abort(FatalError);
258         }
259     }
263 void Foam::linearValveLayersFvMesh::makeSlidersLive()
265     const polyTopoChanger& topoChanges = topoChanger_;
267     // Enable sliding interface
268     forAll (topoChanges, modI)
269     {
270         if (isA<layerAdditionRemoval>(topoChanges[modI]))
271         {
272             topoChanges[modI].disable();
273         }
274         else if (isA<slidingInterface>(topoChanges[modI]))
275         {
276             topoChanges[modI].enable();
277         }
278         else
279         {
280             FatalErrorIn("void linearValveLayersFvMesh::makeLayersLive()")
281                 << "Don't know what to do with mesh modifier "
282                 << modI << " of type " << topoChanges[modI].type()
283                 << abort(FatalError);
284         }
285     }
289 bool Foam::linearValveLayersFvMesh::attached() const
291     const polyTopoChanger& topoChanges = topoChanger_;
293     bool result = false;
295     forAll (topoChanges, modI)
296     {
297         if (isA<slidingInterface>(topoChanges[modI]))
298         {
299             result =
300                 result
301              || refCast<const slidingInterface>(topoChanges[modI]).attached();
302         }
303     }
305     // Check thal all sliders are in sync (debug only)
306     forAll (topoChanges, modI)
307     {
308         if (isA<slidingInterface>(topoChanges[modI]))
309         {
310             if
311             (
312                 result 
313              != refCast<const slidingInterface>(topoChanges[modI]).attached()
314             )
315             {
316                 FatalErrorIn("bool linearValveLayersFvMesh::attached() const")
317                     << "Slider " << modI << " named "
318                     << topoChanges[modI].name()
319                     << " out of sync: Should be" << result
320                     << abort(FatalError);
321             }
322         }
323     }
325     return result;
329 Foam::tmp<Foam::pointField> Foam::linearValveLayersFvMesh::newPoints() const
331     tmp<pointField> tnewPoints
332     (
333         new pointField(points())
334     );
336     pointField& np = tnewPoints();
338     const word layerPatchName
339     (
340         motionDict_.subDict("layer").lookup("patch")
341     );
343     const polyPatch& layerPatch =
344         boundaryMesh()[boundaryMesh().findPatchID(layerPatchName)];
346     const labelList& patchPoints = layerPatch.meshPoints();
348     const vector vel
349     (
350         motionDict_.lookup("pistonVelocity")
351     );
353     forAll (patchPoints, ppI)
354     {
355         np[patchPoints[ppI]] += vel*time().deltaT().value();
356     }
358     return tnewPoints;
363 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
365 // Construct from components
366 Foam::linearValveLayersFvMesh::linearValveLayersFvMesh(const IOobject& io)
368     topoChangerFvMesh(io),
369     motionDict_
370     (
371         IOdictionary
372         (
373             IOobject
374             (
375                 "dynamicMeshDict",
376                 time().constant(),
377                 *this,
378                 IOobject::MUST_READ,
379                 IOobject::NO_WRITE
380             )
381         ).subDict(typeName + "Coeffs")
382     )
384     addZonesAndModifiers();
388 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
390 Foam::linearValveLayersFvMesh::~linearValveLayersFvMesh()
394 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
396 bool Foam::linearValveLayersFvMesh::update()
398     // Detaching the interface
399     if (attached())
400     {
401         Info << "Decoupling sliding interfaces" << endl;
402         makeSlidersLive();
404         // Changing topology by hand
405         topoChanger_.changeMesh();
406     }
407     else
408     {
409         Info << "Sliding interfaces decoupled" << endl;
410     }
412     // Perform layer action and mesh motion
413     makeLayersLive();
415     // Changing topology by hand
416     {
417         autoPtr<mapPolyMesh> topoChangeMap2 = topoChanger_.changeMesh();
419         if (topoChangeMap2->morphing())
420         {
421             if (topoChangeMap2->hasMotionPoints())
422             {
423                 Info << "Topology change; executing pre-motion" << endl;
424                 movePoints(topoChangeMap2->preMotionPoints());
425             }
426         }
427     }
429     // Move points
430     movePoints(newPoints());
432     // Attach the interface
433     Info << "Coupling sliding interfaces" << endl;
434     makeSlidersLive();
436     // Changing topology by hand
437     {
438         // Grab old points to correct the motion
439         pointField oldPointsNew = oldAllPoints();
441         autoPtr<mapPolyMesh> topoChangeMap3 = topoChanger_.changeMesh();
443         if (topoChangeMap3->morphing())
444         {
445             if (debug)
446             {
447                 Info << "Moving points post slider attach" << endl;
448             }
450             pointField newPoints = allPoints();
451             pointField mappedOldPointsNew(newPoints.size());
453             mappedOldPointsNew.map(oldPointsNew, topoChangeMap3->pointMap());
455             // Solve the correct mesh motion to make sure motion fluxes
456             // are solved for and not mapped
457             movePoints(mappedOldPointsNew);
458             resetMotion();
459             setV0();
460             movePoints(newPoints);
461         }
462     }
464     return true;
468 // ************************************************************************* //