Fix tutorials: typo in tutorials/viscoelastic/viscoelasticFluidFoam/S-MDCPP/constant...
[OpenFOAM-1.6-ext.git] / src / engine / engineTopoChangerMesh / verticalValves / verticalValvesMove.C
blob60a8143d5bea9d3429857dbb64c2264be15bed44
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2005-2010 Tommaso Lucchini
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 Class
26     verticalValves
28 \*---------------------------------------------------------------------------*/
30 #include "verticalValves.H"
31 #include "slidingInterface.H"
32 #include "layerAdditionRemoval.H"
33 #include "attachDetach.H"
34 #include "surfaceFields.H"
35 #include "regionSplit.H"
36 #include "componentMixedTetPolyPatchVectorField.H"
37 #include "mapPolyMesh.H"
39 #include "tetPolyMesh.H"
40 #include "tetPointFields.H"
41 #include "elementFields.H"
42 #include "fixedValueTetPolyPatchFields.H"
43 #include "slipTetPolyPatchFields.H"
45 #include "tetFem.H"
47 #include "symmetryFvPatch.H"
48 #include "wedgeFvPatch.H"
49 #include "emptyFvPatch.H"
50 #include "zeroGradientTetPolyPatchFields.H"
51 #include "tetDecompositionMotionSolver.H"
53 #include "fixedValueTetPolyPatchFields.H"
54 #include "mixedTetPolyPatchFields.H"
55 #include "slipTetPolyPatchFields.H"
56 #include "zeroGradientTetPolyPatchFields.H"
58 #include "zeroGradientFvPatchFields.H"
59 #include "fvCFD.H"
61 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
63 void Foam::verticalValves::makeLayersLive()
64
65     // Enable layering
66     forAll (topoChanger_, modI)
67     {
68         if (isA<layerAdditionRemoval>(topoChanger_[modI]))
69         {
70             topoChanger_[modI].enable();
71         }
72         else if (isA<slidingInterface>(topoChanger_[modI]))
73         {
74             topoChanger_[modI].disable();
75         }
76         else if (isA<attachDetach>(topoChanger_[modI]))
77         {
78             topoChanger_[modI].enable();
79         }
80         else
81         {
82             FatalErrorIn("void Foam::engineTopoFvMesh::makeLayersLive()")
83                 << "Don't know what to do with mesh modifier "
84                 << modI << " of type " << topoChanger_[modI].type()
85                 << abort(FatalError);
86         }
87     }
90 void Foam::verticalValves::makeSlidersLive()
93     // Enable sliding interface
94     forAll (topoChanger_, modI)
95     {
96         if (isA<layerAdditionRemoval>(topoChanger_[modI]))
97         {
98             topoChanger_[modI].disable();
99         }
100         else if (isA<slidingInterface>(topoChanger_[modI]))
101         {
102             topoChanger_[modI].enable();
103         }
104         else if (isA<attachDetach>(topoChanger_[modI]))
105         {
106             topoChanger_[modI].enable();
107         }
108         else
109         {
110             FatalErrorIn("void Foam::engineTopoFvMesh::makeLayersLive()")
111                 << "Don't know what to do with mesh modifier "
112                 << modI << " of type " << topoChanger_[modI].type()
113                 << abort(FatalError);
114         }
115     }
119 bool Foam::verticalValves::attached() const
121     const polyTopoChanger& morphs = topoChanger_;
123     bool result = false;
125     forAll (morphs, modI)
126     {
127         if (typeid(morphs[modI]) == typeid(slidingInterface))
128         {
129             result =
130                 result
131              || refCast<const slidingInterface>(morphs[modI]).attached();
132         }
133     }
135     // Check thal all sliders are in sync (debug only)
136     forAll (morphs, modI)
137     {
138         if (typeid(morphs[modI]) == typeid(slidingInterface))
139         {
140             if
141             (
142                 result 
143              != refCast<const slidingInterface>(morphs[modI]).attached()
144             )
145             {
146                 FatalErrorIn("bool movingSquaresTM::attached() const")
147                     << "Slider " << modI << " named " << morphs[modI].name()
148                     << " out of sync: Should be" << result
149                     << abort(FatalError);
150             }
151         }
152     }
154     return result;
157 void Foam::verticalValves::valveDetach()
159     // Enable sliding interface
160     forAll (topoChanger_, modI)
161     {
162         if (isA<attachDetach>(topoChanger_[modI]))
163         {
164             const attachDetach& ad =
165                 refCast<const attachDetach>(topoChanger_[modI]);
167             const word masterName = ad.masterPatchID().name();
169             // Find the valve with that name
170             label valveIndex = -1;
172             forAll (valves_, valveI)
173             {
174                 if
175                 (
176                     valves_[valveI].detachInCylinderPatchID().name()
177                  == masterName
178                 )
179                 {
180                     valveIndex = valveI;
181                     break;
182                 }
183             }
185             if (valveIndex < 0)
186             {
187                 FatalErrorIn
188                 (
189                     "void Foam::engineTopoFvMesh::prepareValveDetach()"
190                 )   << "Cannot match patch for attach/detach " << modI
191                     << abort(FatalError);
192             }
194             if (debug)
195             {
196                 Info<< " valveI: " << valveIndex << " attached: "
197                     << ad.attached()
198                     << " valve open: " << valves_[valveIndex].isOpen()
199                     << endl;
200             }
202             ad.setDetach();
203             
204         }
205     }
208 void Foam::verticalValves::valveAttach()
210     // Enable sliding interface
211     forAll (topoChanger_, modI)
212     {
213         if (isA<attachDetach>(topoChanger_[modI]))
214         {
215             const attachDetach& ad =
216                 refCast<const attachDetach>(topoChanger_[modI]);
218             const word masterName = ad.masterPatchID().name();
220             // Find the valve with that name
221             label valveIndex = -1;
223             forAll (valves_, valveI)
224             {
225                 if
226                 (
227                     valves_[valveI].detachInCylinderPatchID().name()
228                  == masterName
229                 )
230                 {
231                     valveIndex = valveI;
232                     break;
233                 }
234             }
236             if (valveIndex < 0)
237             {
238                 FatalErrorIn
239                 (
240                     "void Foam::engineTopoFvMesh::prepareValveDetach()"
241                 )   << "Cannot match patch for attach/detach " << modI
242                     << abort(FatalError);
243             }
245             if (debug)
246             {
247                 Info<< " valveI: " << valveIndex << " attached: "
248                     << ad.attached()
249                     << " valve open: " << valves_[valveIndex].isOpen()
250                     << endl;
251             }
253             ad.setAttach();
254             
255         }
256     }
259 void Foam::verticalValves::prepareValveDetach()
261     // Enable sliding interface
262     forAll (topoChanger_, modI)
263     {
264         if (isA<attachDetach>(topoChanger_[modI]))
265         {
266             const attachDetach& ad =
267                 refCast<const attachDetach>(topoChanger_[modI]);
269             const word masterName = ad.masterPatchID().name();
271             // Find the valve with that name
272             label valveIndex = -1;
274             forAll (valves_, valveI)
275             {
276                 if
277                 (
278                     valves_[valveI].detachInCylinderPatchID().name()
279                  == masterName
280                 )
281                 {
282                     valveIndex = valveI;
283                     break;
284                 }
285             }
287             if (valveIndex < 0)
288             {
289                 FatalErrorIn
290                 (
291                     "void Foam::engineTopoFvMesh::prepareValveDetach()"
292                 )   << "Cannot match patch for attach/detach " << modI
293                     << abort(FatalError);
294             }
296             if (debug)
297             {
298                 Info<< " valveI: " << valveIndex << " attached: "
299                     << ad.attached()
300                     << " valve open: " << valves_[valveIndex].isOpen()
301                     << endl;
302             }
304             if (valves_[valveIndex].isOpen())
305             {
306                 ad.setAttach();
307             }
308             else
309             {
310                 ad.setDetach();
311             }
312         }
313     }
317 bool Foam::verticalValves::update()
320     tetDecompositionMotionSolver& mSolver =
321         refCast<tetDecompositionMotionSolver>(msPtr_());
323     // Detaching the interface
324     if (attached())
325     {
326         Info << "Decoupling sliding interfaces" << endl;
327         makeSlidersLive();
328         valveDetach();
329         autoPtr<mapPolyMesh> topoChangeMap1 = topoChanger_.changeMesh();
331         Info << "sliding interfaces successfully decoupled!!!" << endl;
332         if (topoChangeMap1->morphing())
333         {
334             mSolver.updateMesh(topoChangeMap1());
335         }
336     }
337     else
338     {
339         Info << "Sliding interfaces decoupled" << endl;
340         valveDetach();
341     }
343     Info << "Executing layer action" << endl;
345     // Piston Layering
347     makeLayersLive();
348     scalar deltaZ = engTime().pistonDisplacement().value();
350     // Find piston mesh modifier
351     const label pistonLayerID =
352         topoChanger_.findModifierID("pistonLayer");
354     if (realDeformation())
355     {
356         Info << "Mesh deformation mode" << endl;
357         topoChanger_[pistonLayerID].disable();
358         forAll(valves_, valveI)
359         {
360             // Find piston mesh modifier
361             const label valveLayerID1 =
362                 topoChanger_.findModifierID
363                 (
364                     "valvePistonLayer"
365                   + Foam::name(valveI + 1)
366                 );
368             topoChanger_[valveLayerID1].disable();
370             if(valves_[valveI].bottomPatchID().active())
371             {
372                 // Find piston mesh modifier
373                 const label valveLayerID2 =
374                     topoChanger_.findModifierID
375                     (
376                         "valveBottomLayer"
377                       + Foam::name(valveI + 1)
378                     );
380                 topoChanger_[valveLayerID2].disable();
381             }
382         }
383     }
384     else
385     {
386         // Activate piston layer
387         Info << "Piston layering mode" << endl;
388         topoChanger_[pistonLayerID].enable();
389         forAll(valves_, valveI)
390         {
391             // Find piston mesh modifier
392             const label valveLayerID1 =
393                 topoChanger_.findModifierID
394                 (
395                     "valvePistonLayer"
396                   + Foam::name(valveI + 1)
397                 );
398             topoChanger_[valveLayerID1].enable();
400             if(valves_[valveI].bottomPatchID().active())
401             {
402                 // Find piston mesh modifier
403                 const label valveLayerID2 =
404                     topoChanger_.findModifierID
405                     (
406                         "valveBottomLayer"
407                       + Foam::name(valveI + 1)
408                     );
410                 topoChanger_[valveLayerID2].enable();
411             }
412         }
413     }
415     scalar minLayerThickness = piston().minLayer();
416     virtualPistonPosition() += deltaZ;
418     forAll(valves_,valveI)
419     {
420         if(!realDeformation())
421         {
422             scalar valveDisplacement =
423                 valves_[valveI].curVelocity()*
424                 valves_[valveI].cs().axis().z()*engTime().deltaT().value();
426             valveTopPosition_[valveI] += valveDisplacement;
427             valveBottomPosition_[valveI] += valveDisplacement;
428             valvePistonPosition_[valveI] += deltaZ;
429         }
430     }
432     forAll(valves_,valveI)
433     {
434         if(valves_[valveI].poppetPatchID().active())
435         {
437             // Find valve layer mesh modifier
438             const label valveLayerID =
439                 topoChanger_.findModifierID
440                 (
441                     "valvePoppetLayer"
442                   + Foam::name(valveI + 1)
443                 );
445             if (valveLayerID < 0)
446             {
447                 FatalErrorIn("void engineFvMesh::moveAndMorph()")
448                     << "valve modifier not found."
449                     << abort(FatalError);
450             }
452             if (valves_[valveI].curLift() < valveTopTol_)
453             {
454                 topoChanger_[valveLayerID].disable();
455             }
456             else
457             {
458                 topoChanger_[valveLayerID].enable();
459             }
460         }
462         if(valves_[valveI].isOpen())
463         {
464             isReallyClosed_[valveI] = false;
465         }
467     }
469     // Changing topology by hand
470     autoPtr<mapPolyMesh> topoChangeMap2 = topoChanger_.changeMesh();
472     if (pistonLayerID < 0)
473     {
474         FatalErrorIn("void engineFvMesh::moveAndMorph()")
475             << "Piston modifier not found."
476             << abort(FatalError);
477     }
479     // Work array for new points position.
480     pointField newPoints = points();
482     // Changing topology by hand
483     if (topoChangeMap2->morphing())
484     {
485         mSolver.updateMesh(topoChangeMap2());
487         if (topoChangeMap2->hasMotionPoints())
488         {
489             movePoints(topoChangeMap2->preMotionPoints());
490             newPoints = topoChangeMap2->preMotionPoints();
491         }
492         setV0();
493         resetMotion();
494     }
498 // NEW !
500     // Reset the position of layered interfaces
503     Move the mesh points
504     1) Move all the piston points
507     bool poppetDeformation = false;
509     if(!realDeformation())
510     {
511         Info << "verticalValves::update()::Layering mode" << endl;
513 #       include "movePistonPoints.H"
514 #       include "moveValvePoints.H"
515         movePoints(newPoints);
516 #       include "poppetDeformation.H"
517         newPoints = points();
518         setVirtualPositions();
519     }
520     else
521     {
522         Info << "verticalValves::update()::Deformation mode" << endl;
524 #       include "moveAllTogether.H"
525         movePoints(mSolver.curPoints());
526         setVirtualPositions();
527         newPoints = points();
528 #       include "moveTopOfTheValves.H"
529         movePoints(newPoints);
530 #       include "poppetDeformation.H"
531         newPoints = points();
532         setVirtualPositions();
533     }
535     forAll(valves(), valveI)
536     {
537         Info << "Valve Top Position for valve n. " << valveI + 1 << " = " <<
538         valveTopPosition_[valveI] << endl;
539         Info << "Valve Bottom Position for valve n. " << valveI + 1 << " = " <<
540         valveBottomPosition_[valveI] << endl;
541         Info << "Valve Piston Position for valve n. " << valveI + 1 << " = " <<
542         valvePistonPosition_[valveI] << endl;
543     }
545     Info << "virtualPistonPosition = " << virtualPistonPosition()
546     << ", deckHeight = " << deckHeight()
547     << ", pistonPosition = " << pistonPosition()
548     << endl;
550     pistonPosition() += deltaZ;
552     scalarField Vold = V();
554     volScalarField vMesh
555     (
556         IOobject
557         (
558             "vMesh",
559             time().timeName(),
560             *this,
561             IOobject::NO_READ,
562             IOobject::NO_WRITE
563         ),
564         *this,
565         dimensionedScalar("zero", dimensionSet(0, 0, 0, 0, 0), 0.0),
566         zeroGradientFvPatchScalarField::typeName
567     );
569     vMesh.internalField() = V();
570     vMesh.oldTime().internalField() = V0();
573     // Coupling the interface Again
575     {
576         pointField oldPointsNew = oldPoints();
578         // Attach the interface
579         Info << "Coupling sliding interfaces" << endl;
580         makeSlidersLive();
581         valveAttach();
582         prepareValveDetach();
586         // Changing topology by hand
587         autoPtr<mapPolyMesh> topoChangeMap3 = topoChanger_.changeMesh();
589         Info << "Sliding interfaces coupled: " << attached() << endl;
590         if (topoChangeMap3->morphing())
591         {
592             mSolver.updateMesh(topoChangeMap3());
594             if (topoChangeMap3->hasMotionPoints())
595             {
596 //                movePoints(topoChangeMap3->preMotionPoints());
597 //                resetMotion();
598 //                setV0();
599             }
601             if(correctPointsMotion_)
602             {
603                 // correct the motion after attaching the sliding interface
604                 pointField mappedOldPointsNew(allPoints().size());
606                 mappedOldPointsNew.map
607                 (
608                     oldPointsNew,
609                     topoChangeMap3->pointMap()
610                 );
611                 pointField newPoints = allPoints();
613                 movePoints(mappedOldPointsNew);
615                 resetMotion();
616                 setV0();
617                 movePoints(newPoints);
618             }
619         }
620     }
622     return true;