fixed writing out entries in advective bc
[OpenFOAM-1.6-ext.git] / src / engine / engineTopoChangerMesh / simpleTwoStroke / simpleTwoStrokeMove.C
blob158849d9e544b5f214f65dd76357e2f528959816
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 \*---------------------------------------------------------------------------*/
28 #include "simpleTwoStroke.H"
29 #include "slidingInterface.H"
30 #include "layerAdditionRemoval.H"
31 #include "surfaceFields.H"
32 #include "regionSplit.H"
33 #include "componentMixedTetPolyPatchVectorField.H"
34 #include "mapPolyMesh.H"
36 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
37 void Foam::simpleTwoStroke::makeLayersLive()
39     const polyTopoChanger& morphs = topoChanger_;
41     // Enable layering
42     forAll (morphs, modI)
43     {
44         if (typeid(morphs[modI]) == typeid(layerAdditionRemoval))
45         {
46             morphs[modI].enable();
47         }
48         else if (typeid(morphs[modI]) == typeid(slidingInterface))
49         {
50             morphs[modI].disable();
51         }
52         else
53         {
54             FatalErrorIn("void Foam::simpleTwoStroke::makeLayersLive()")
55                 << "Don't know what to do with mesh modifier "
56                 << modI << " of type " << morphs[modI].type()
57                 << abort(FatalError);
58         }
59     }
62 void Foam::simpleTwoStroke::makeSlidersLive()
64     const polyTopoChanger& morphs = topoChanger_;
66     // Enable sliding interface
67     forAll (morphs, modI)
68     {
69         if (typeid(morphs[modI]) == typeid(layerAdditionRemoval))
70         {
71             morphs[modI].disable();
72         }
73         else if (typeid(morphs[modI]) == typeid(slidingInterface))
74         {
75             morphs[modI].enable();
76         }
77         else
78         {
79             FatalErrorIn("void movingSquaresTM::makeLayersLive()")
80                 << "Don't know what to do with mesh modifier "
81                 << modI << " of type " << morphs[modI].type()
82                 << abort(FatalError);
83         }
84     }
88 bool Foam::simpleTwoStroke::attached() const
90     const polyTopoChanger& morphs = topoChanger_;
92     bool result = false;
94     forAll (morphs, modI)
95     {
96         if (typeid(morphs[modI]) == typeid(slidingInterface))
97         {
98             result =
99                 result
100              || refCast<const slidingInterface>(morphs[modI]).attached();
101         }
102     }
104     // Check thal all sliders are in sync (debug only)
105     forAll (morphs, modI)
106     {
107         if (typeid(morphs[modI]) == typeid(slidingInterface))
108         {
109             if
110             (
111                 result
112              != refCast<const slidingInterface>(morphs[modI]).attached()
113             )
114             {
115                 FatalErrorIn("bool movingSquaresTM::attached() const")
116                     << "Slider " << modI << " named " << morphs[modI].name()
117                     << " out of sync: Should be" << result
118                     << abort(FatalError);
119             }
120         }
121     }
123     return result;
127 bool Foam::simpleTwoStroke::update()
129     // Detaching the interface
130     if (attached())
131     {
132         Info << "Decoupling sliding interfaces" << endl;
133         makeSlidersLive();
134         topoChanger_.changeMesh();
136         Info << "sliding interfaces successfully decoupled!!!" << endl;
137     }
138     else
139     {
140         Info << "Sliding interfaces decoupled" << endl;
141     }
143     Info << "Executing layer action" << endl;
145     // Piston Layering
147     makeLayersLive();
149     // Find piston mesh modifier
150     const label pistonLayerID =
151         topoChanger_.findModifierID("pistonLayer");
153     if (pistonLayerID < 0)
154     {
155         FatalErrorIn("void engineFvMesh::moveAndMorph()")
156             << "Piston modifier not found."
157             << abort(FatalError);
158     }
160     scalar minLayerThickness = piston().minLayer();
161     scalar deltaZ = engTime().pistonDisplacement().value();
162     virtualPistonPosition() += deltaZ;
164     Info << "virtualPistonPosition = " << virtualPistonPosition()
165     << ", deckHeight = " << deckHeight()
166     << ", pistonPosition = " << pistonPosition() << endl;
168     if (realDeformation())
169     {
170         // Dectivate piston layer
171         Info << "Mesh deformation mode" << endl;
172         topoChanger_[pistonLayerID].disable();
173     }
174     else
175     {
176         // Activate piston layer
177         Info << "Piston layering mode" << endl;
178         topoChanger_[pistonLayerID].enable();
179     }
182     // Changing topology by hand
183     autoPtr<mapPolyMesh> topoChangeMap = topoChanger_.changeMesh();
185     // Work array for new points position.
186     pointField newPoints = points();
188     if (topoChangeMap->morphing())
189     {
191         if (topoChangeMap->hasMotionPoints())
192         {
193             Info << "Topology change; executing pre-motion" << endl;
194             movePoints(topoChangeMap->preMotionPoints());
195             newPoints = topoChangeMap->preMotionPoints();
196         }
197         setV0();
198         resetMotion();
199     }
201     // Reset the position of layered interfaces
203     boolList scaleDisp(nPoints(), true);
204     label nScaled = nPoints();
206     List<bool> pistonPoint(newPoints.size(), false);
207     List<bool> headPoint(newPoints.size(), false);
209 //    label pistonPtsIndex = pointZones().findZoneID("pistonPoints");
210 //    const labelList& pistonPoints = pointZones()[pistonPtsIndex];
211     
212     labelList pistonPoints;
214     {
215         label movingCellsIndex = cellZones().findZoneID("movingCells");
217         if (movingCellsIndex < 0)
218         {
219             FatalErrorIn("bool twoStrokeEngine::update()")
220                 << "Cannot find cell zone movingCells"
221                 << abort(FatalError);
222         }
225         const labelList& pistonCells = cellZones()[movingCellsIndex];
227         const labelListList& cp = cellPoints();
229         boolList count(newPoints.size(), false);
231         forAll (pistonCells, cellI)
232         {
233             const labelList& curCellPoints = cp[pistonCells[cellI]];
235             forAll (curCellPoints, i)
236             {
237                 count[curCellPoints[i]] = true;
238             }
239         }
241         // Count the points
242         label nCounted = 0;
243         forAll (count, pointI)
244         {
245             if (count[pointI] == true)
246             {
247                 nCounted++;
248             }
249         }
251         pistonPoints.setSize(nCounted);
253         // Collect the points
254         nCounted = 0;
255         forAll (count, pointI)
256         {
257             if (count[pointI] == true)
258             {
259                 pistonPoints[nCounted] = pointI;
260                 nCounted++;
261             }
262         }
264     }
266     label headPtsIndex = pointZones().findZoneID("headPoints");
267     const labelList& headPoints = pointZones()[headPtsIndex];
269     const scalarField& movingPointsM = movingPointsMask();
271     forAll(pistonPoints, i)
272     {
273         label pointI = pistonPoints[i];
274         pistonPoint[pointI] = true;
275         point& p = newPoints[pointI];
277         if (p.z() < pistonPosition() - 1.0e-6)
278         {
279             scaleDisp[pointI] = false;
280             nScaled--;
281         }
282     }
284     forAll(headPoints, i)
285     {
286         headPoint[headPoints[i]] = true;
287         scaleDisp[headPoints[i]] = false;
288         nScaled--;
289     }
291     if (realDeformation())
292     {
293         forAll(scaleDisp, pointI)
294         {
295             point& p = newPoints[pointI];
297             if (scaleDisp[pointI])
298             {
299                 p.z() += movingPointsM[pointI]*
300                     deltaZ
301                   * (deckHeight() - p.z())/(deckHeight() - pistonPosition());
302             }
303             else
304             {
305                 if (!headPoint[pointI])
306                 {
307                     p.z() += movingPointsM[pointI] * deltaZ;
308                 }
309             }
310         }
311     }
312     else
313     {
314         // Always move piston
315         scalar pistonTopZ = -GREAT;
316         forAll(pistonPoints, i)
317         {
318             point& p = newPoints[pistonPoints[i]];
319             p.z() += deltaZ*movingPointsM[pistonPoints[i]];
320             pistonTopZ = max(pistonTopZ, p.z());
321         }
324         // NN! fix. only needed for compression
325         if (deltaZ > 0.0)
326         {
327             // check if piston-points have moved beyond the layer above
328             forAll(newPoints, i)
329             {
330                 if (!pistonPoint[i])
331                 {
332                     if (virtualPistonPosition() > newPoints[i].z())
333                     {
334                         newPoints[i].z() =
335                             (1.0 - movingPointsM[i])*newPoints[i].z()
336                           + movingPointsM[i]*
337                             (pistonTopZ + 0.9*minLayerThickness);
338                     }
339                 }
340             }
341         }
342     }
345     movePoints(newPoints);
347     deleteDemandDrivenData(movingPointsMaskPtr_);
349     pistonPosition() += deltaZ;
351     {
352         // Attach the interface
353         Info << "Coupling sliding interfaces" << endl;
354         makeSlidersLive();
356         // Changing topology by hand
357         autoPtr<mapPolyMesh> topoChangeMap3 = topoChanger_.changeMesh();
359         Info << "Sliding interfaces coupled: " << attached() << endl;
360     }
362     return true;