Merge commit 'd3b269b7c6ffa0cd68845adfecdfb849316dba71' into nextRelease
[foam-extend-3.2.git] / src / engine / engineTopoChangerMesh / thoboisMesh / thoboisMeshMove.C
blob343e6a3c112e8cb2250579563b1a6a1a3f15946c
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     |
5     \\  /    A nd           | For copyright notice see file Copyright
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend 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 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "thoboisMesh.H"
27 #include "slidingInterface.H"
28 #include "layerAdditionRemoval.H"
29 #include "attachDetach.H"
30 #include "surfaceFields.H"
31 #include "regionSplit.H"
32 #include "componentMixedTetPolyPatchVectorField.H"
33 #include "mapPolyMesh.H"
35 #include "tetPolyMesh.H"
36 #include "tetPointFields.H"
37 #include "elementFields.H"
38 #include "fixedValueTetPolyPatchFields.H"
39 #include "slipTetPolyPatchFields.H"
40 #include "symmetryTetPolyPatch.H"
42 #include "tetFem.H"
44 #include "symmetryFvPatch.H"
45 #include "wedgeFvPatch.H"
46 #include "emptyFvPatch.H"
47 #include "zeroGradientTetPolyPatchFields.H"
48 #include "tetMotionSolver.H"
50 #include "fixedValueTetPolyPatchFields.H"
51 #include "mixedTetPolyPatchFields.H"
52 #include "slipTetPolyPatchFields.H"
53 #include "zeroGradientTetPolyPatchFields.H"
57 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
58 void Foam::thoboisMesh::makeLayersLive()
60     // Enable layering
61     forAll (topoChanger_, modI)
62     {
63         if (isA<layerAdditionRemoval>(topoChanger_[modI]))
64         {
65             topoChanger_[modI].enable();
66         }
67         else if (isA<slidingInterface>(topoChanger_[modI]))
68         {
69             topoChanger_[modI].disable();
70         }
71         else if (isA<attachDetach>(topoChanger_[modI]))
72         {
73             topoChanger_[modI].enable();
74         }
75         else
76         {
77             FatalErrorIn("void Foam::engineTopoFvMesh::makeLayersLive()")
78                 << "Don't know what to do with mesh modifier "
79                 << modI << " of type " << topoChanger_[modI].type()
80                 << abort(FatalError);
81         }
82     }
87 void Foam::thoboisMesh::valveDetach()
89     // Enable sliding interface
90     forAll (topoChanger_, modI)
91     {
92         if (isA<attachDetach>(topoChanger_[modI]))
93         {
94             const attachDetach& ad =
95                 refCast<const attachDetach>(topoChanger_[modI]);
97             const word masterName = ad.masterPatchID().name();
99             // Find the valve with that name
100             label valveIndex = -1;
102             forAll (valves_, valveI)
103             {
104                 if
105                 (
106                     valves_[valveI].detachInCylinderPatchID().name()
107                  == masterName
108                 )
109                 {
110                     valveIndex = valveI;
111                     break;
112                 }
113             }
115             if (valveIndex < 0)
116             {
117                 FatalErrorIn
118                 (
119                     "void Foam::engineTopoFvMesh::prepareValveDetach()"
120                 )   << "Cannot match patch for attach/detach " << modI
121                     << abort(FatalError);
122             }
124             if (debug)
125             {
126                 Info<< " valveI: " << valveIndex << " attached: "
127                     << ad.attached()
128                     << " valve open: " << valves_[valveIndex].isOpen()
129                     << endl;
130             }
132             ad.setDetach();
134         }
135     }
138 void Foam::thoboisMesh::valveAttach()
140     // Enable sliding interface
141     forAll (topoChanger_, modI)
142     {
143         if (isA<attachDetach>(topoChanger_[modI]))
144         {
145             const attachDetach& ad =
146                 refCast<const attachDetach>(topoChanger_[modI]);
148             const word masterName = ad.masterPatchID().name();
150             // Find the valve with that name
151             label valveIndex = -1;
153             forAll (valves_, valveI)
154             {
155                 if
156                 (
157                     valves_[valveI].detachInCylinderPatchID().name()
158                  == masterName
159                 )
160                 {
161                     valveIndex = valveI;
162                     break;
163                 }
164             }
166             if (valveIndex < 0)
167             {
168                 FatalErrorIn
169                 (
170                     "void Foam::engineTopoFvMesh::prepareValveDetach()"
171                 )   << "Cannot match patch for attach/detach " << modI
172                     << abort(FatalError);
173             }
175             if (debug)
176             {
177                 Info<< " valveI: " << valveIndex << " attached: "
178                     << ad.attached()
179                     << " valve open: " << valves_[valveIndex].isOpen()
180                     << endl;
181             }
183             ad.setAttach();
185         }
186     }
190 void Foam::thoboisMesh::prepareValveDetach()
192     // Enable sliding interface
193     forAll (topoChanger_, modI)
194     {
195         if (isA<attachDetach>(topoChanger_[modI]))
196         {
197             const attachDetach& ad =
198                 refCast<const attachDetach>(topoChanger_[modI]);
200             const word masterName = ad.masterPatchID().name();
202             // Find the valve with that name
203             label valveIndex = -1;
205             forAll (valves_, valveI)
206             {
207                 if
208                 (
209                     valves_[valveI].detachInCylinderPatchID().name()
210                  == masterName
211                 )
212                 {
213                     valveIndex = valveI;
214                     break;
215                 }
216             }
218             if (valveIndex < 0)
219             {
220                 FatalErrorIn
221                 (
222                     "void Foam::engineTopoFvMesh::prepareValveDetach()"
223                 )   << "Cannot match patch for attach/detach " << modI
224                     << abort(FatalError);
225             }
227             if (debug)
228             {
229                 Info<< " valveI: " << valveIndex << " attached: "
230                     << ad.attached()
231                     << " valve open: " << valves_[valveIndex].isOpen()
232                     << endl;
233             }
235             if (valves_[valveIndex].isOpen())
236             {
237                 ad.setAttach();
238             }
239             else
240             {
241                 ad.setDetach();
242             }
243         }
244     }
248 bool Foam::thoboisMesh::update()
250     Info << "bool Foam::layerSmooth::update()" << endl;
251     tetMotionSolver& mSolver =
252         refCast<tetMotionSolver>(msPtr_());
254     tetPointVectorField& motionU = mSolver.motionU();
256     // Find piston mesh modifier
258     const label pistonLayerID =
259         topoChanger_.findModifierID("pistonLayer");
261     {
262         valveDetach();
264         topoChanger_[pistonLayerID].disable();
266         autoPtr<mapPolyMesh> topoChangeMap = topoChanger_.changeMesh();
268         if (topoChangeMap->morphing())
269         {
270             mSolver.updateMesh(topoChangeMap());
271             Info << "mSolver.updateMesh(topoChangeMap())" << endl;
272         }
274     }
276     scalar deltaZ = engTime().pistonDisplacement().value();
278     // deltaZ set to zero, FIXED PISTON POSITION
279     deltaZ = 0.0;
281     Info<< "deltaZ = " << deltaZ << " Piston at:" << pistonPosition()
282         << endl;
283     virtualPistonPosition() += deltaZ;
285     Info << "pistonLayerID: " << pistonLayerID << endl;
287     bool realDeformation = deformation();
289     if
290     (
291         virtualPistonPosition() + deltaZ
292       > deckHeight() - engTime().clearance().value() - SMALL
293     )
294     {
295         realDeformation = true;
296     }
298     if (realDeformation)
299     {
300         // Disable layer addition
301         Info << "**Mesh deformation mode" << endl;
302         topoChanger_[pistonLayerID].disable();
303     }
304     else
305     {
306         // Enable layer addition
307         Info << "**Piston layering mode" << endl;
308         topoChanger_[pistonLayerID].enable();
309     }
311     autoPtr<mapPolyMesh> topoChangeMap = topoChanger_.changeMesh();
313     bool meshChanged = topoChangeMap->morphing();
315     pointField newPoints = allPoints();
317     if (meshChanged)
318     {
319         mSolver.updateMesh(topoChangeMap());
321         if (topoChangeMap().hasMotionPoints())
322         {
323             movePoints(topoChangeMap().preMotionPoints());
324             newPoints = topoChangeMap().preMotionPoints();
325         }
326         setV0();
327         resetMotion();
328     }
330     if (!deformation())
331     {
332 //#       include "movePistonPointsLayering.H"
333         movePoints(newPoints);
335 #       include "setValveMotionBoundaryConditionThobois.H"
337         // Set piston velocity
338         if (piston().patchID().active())
339         {
341             componentMixedTetPolyPatchVectorField& pp =
342                 refCast<componentMixedTetPolyPatchVectorField>
343                 (
344                     motionU.boundaryField()[piston().patchID().index()]
345                 );
347             pp.refValue() = vector::zero;
348         }
350         motionU.correctBoundaryConditions();
353         DynamicList<label> constrainedPoints(mSolver.curPoints()().size()/100);
354         DynamicList<vector> constrainedVelocity
355         (
356             mSolver.curPoints()().size()/100
357         );
359 #       include "setThoboisMeshConstraintsNoDeformation.H"
362         // Set piston velocity
363         if (piston().patchID().active())
364         {
365             componentMixedTetPolyPatchVectorField& pp =
366                 refCast<componentMixedTetPolyPatchVectorField>
367                 (
368                     motionU.boundaryField()[piston().patchID().index()]
369                 );
371             pp.refValue() = vector::zero;
372         }
374         motionU.correctBoundaryConditions();
376         labelList constrainedPointsList(constrainedPoints.shrink());
377         vectorField constrainedVelocityField(constrainedVelocity.shrink());
379         forAll (constrainedPointsList, i)
380         {
381             mSolver.setConstraint
382             (
383                 constrainedPointsList[i],
384                 constrainedVelocityField[i]
385             );
386         }
388 //         mSolver.solve
389 //         (
390 //             labelList(constrainedPoints.shrink()),
391 //             constrainedVelocity.shrink()
392 //         );
394         mSolver.solve();
396         //set to zero the motion U along the x and y directions
398         newPoints = mSolver.curPoints();
399         movePoints(newPoints);
400         setVirtualPositions();
401         mSolver.clearConstraints();
402     }
403     else
404     {
405 #       include "setValveMotionBoundaryConditionThobois.H"
406 #       include "setPistonMotionBoundaryConditionThobois.H"
407         DynamicList<label> constrainedPoints(mSolver.curPoints()().size()/100);
408         DynamicList<vector> constrainedVelocity
409         (
410             mSolver.curPoints()().size()/100
411         );
413 #       include "setThoboisMeshConstraints.H"
415         // Set piston velocity
416         if (piston().patchID().active())
417         {
419             componentMixedTetPolyPatchVectorField& pp =
420                 refCast<componentMixedTetPolyPatchVectorField>
421                 (
422                     motionU.boundaryField()[piston().patchID().index()]
423                 );
425             pp.refValue() = vector::zero;
426         }
428 //        motionU.correctBoundaryConditions();
430         labelList constrainedPointsList(constrainedPoints.shrink());
431         vectorField constrainedVelocityField(constrainedVelocity.shrink());
433         forAll (constrainedPointsList, i)
434         {
435             mSolver.setConstraint
436             (
437                 constrainedPointsList[i],
438                 constrainedVelocityField[i]
439             );
440         }
442 //        mSolver.solve
443 //        (
444 //            labelList(constrainedPoints.shrink()),
445 //            constrainedVelocity.shrink()
446 //        );
448         mSolver.solve();
450         //set to zero the motion U along the x and y directions
452         newPoints = mSolver.curPoints();
453         movePoints(newPoints);
454         setVirtualPositions();
455         mSolver.clearConstraints();
456     }
458     {
459         pointField oldPointsNew = oldPoints();
460         pointField newPointsNew = points();
462         prepareValveDetach();
463         topoChanger_[pistonLayerID].disable();
464         autoPtr<mapPolyMesh> topoChangeMap = topoChanger_.changeMesh();
466 //        Info << motionU << endl;
468         if (topoChangeMap->morphing())
469         {
470             mSolver.updateMesh(topoChangeMap());
472            {
473                // correct the motion after attaching the sliding interface
475                pointField mappedOldPointsNew(allPoints().size());
477                mappedOldPointsNew.map(oldPointsNew, topoChangeMap->pointMap());
478                pointField newPoints = allPoints();
480                movePoints(mappedOldPointsNew);
481                resetMotion();
482                setV0();
483                movePoints(newPoints);
484            }
485         }
486     }
488     Info << "CHANGED LAST" << endl;
491     if(moving() && meshChanged)
492     {
493         Info << "MOOOOOOOOOOVING!!!!!!!" << endl;
494         Info << "min V0() post motion = " << min(V0()) << endl;
495     }
497     Info << "min V() post-motion = " << min(V()) << endl;
498     Info << "max phi() post-motion = " << max(phi()) << endl;
499     Info << "min phi() post-motion = " << min(phi()) << endl;
502     Info<< "Total cylinder volume at CA " << engTime().timeName() << " = "
503         << sum(V()) << endl;
505     return meshChanged;