OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

surfaceDisplacementPointPatchVectorField.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2007 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "surfaceDisplacementPointPatchVectorField.H"
00028 #include "addToRunTimeSelectionTable.H"
00029 #include "Time.H"
00030 #include "transformField.H"
00031 #include "fvMesh.H"
00032 #include "displacementLaplacianFvMotionSolver.H"
00033 
00034 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00035 
00036 namespace Foam
00037 {
00038 
00039 
00040 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00041 
00042 template<>
00043 const char*
00044 NamedEnum<surfaceDisplacementPointPatchVectorField::projectMode, 3>::
00045 names[] =
00046 {
00047     "nearest",
00048     "pointNormal",
00049     "fixedNormal"
00050 };
00051 
00052 const NamedEnum<surfaceDisplacementPointPatchVectorField::projectMode, 3>
00053     surfaceDisplacementPointPatchVectorField::projectModeNames_;
00054 
00055 
00056 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00057 
00058 void surfaceDisplacementPointPatchVectorField::calcProjection
00059 (
00060     vectorField& displacement
00061 ) const
00062 {
00063     const polyMesh& mesh = patch().boundaryMesh().mesh()();
00064     const pointField& localPoints = patch().localPoints();
00065     const labelList& meshPoints = patch().meshPoints();
00066 
00067     //const scalar deltaT = mesh.time().deltaT().value();
00068 
00069     // Construct large enough vector in direction of projectDir so
00070     // we're guaranteed to hit something.
00071 
00072     //- Per point projection vector:
00073     const scalar projectLen = mag(mesh.bounds().max()-mesh.bounds().min());
00074 
00075     // For case of fixed projection vector:
00076     vector projectVec;
00077     if (projectMode_ == FIXEDNORMAL)
00078     {
00079         vector n = projectDir_/mag(projectDir_);
00080         projectVec = projectLen*n;
00081     }
00082 
00083 
00084     // Get fixed points (bit of a hack)
00085     const pointZone* zonePtr = NULL;
00086 
00087     if (frozenPointsZone_.size() > 0)
00088     {
00089         const pointZoneMesh& pZones = mesh.pointZones();
00090 
00091         zonePtr = &pZones[pZones.findZoneID(frozenPointsZone_)];
00092 
00093         Pout<< "surfaceDisplacementPointPatchVectorField : Fixing all "
00094             << zonePtr->size() << " points in pointZone " << zonePtr->name()
00095             << endl;
00096     }
00097 
00098     // Get the starting locations from the motionSolver
00099     const pointField& points0 = mesh.lookupObject<displacementFvMotionSolver>
00100     (
00101         "dynamicMeshDict"
00102     ).points0();
00103 
00104 
00105     pointField start(meshPoints.size());
00106     forAll(start, i)
00107     {
00108         start[i] = points0[meshPoints[i]] + displacement[i];
00109     }
00110 
00111     label nNotProjected = 0;
00112 
00113     if (projectMode_ == NEAREST)
00114     {
00115         List<pointIndexHit> nearest;
00116         labelList hitSurfaces;
00117         surfaces().findNearest
00118         (
00119             start,
00120             scalarField(start.size(), sqr(projectLen)),
00121             hitSurfaces,
00122             nearest
00123         );
00124 
00125         forAll(nearest, i)
00126         {
00127             if (zonePtr && (zonePtr->whichPoint(meshPoints[i]) >= 0))
00128             {
00129                 // Fixed point. Reset to point0 location.
00130                 displacement[i] = points0[meshPoints[i]] - localPoints[i];
00131             }
00132             else if (nearest[i].hit())
00133             {
00134                 displacement[i] =
00135                     nearest[i].hitPoint()
00136                   - points0[meshPoints[i]];
00137             }
00138             else
00139             {
00140                 nNotProjected++;
00141 
00142                 if (debug)
00143                 {
00144                     Pout<< "    point:" << meshPoints[i]
00145                         << " coord:" << localPoints[i]
00146                         << "  did not find any surface within " << projectLen
00147                         << endl;
00148                 }
00149             }
00150         }
00151     }
00152     else
00153     {
00154         // Do tests on all points. Combine later on.
00155 
00156         // 1. Check if already on surface
00157         List<pointIndexHit> nearest;
00158         {
00159             labelList nearestSurface;
00160             surfaces().findNearest
00161             (
00162                 start,
00163                 scalarField(start.size(), sqr(SMALL)),
00164                 nearestSurface,
00165                 nearest
00166             );
00167         }
00168 
00169         // 2. intersection. (combined later on with information from nearest
00170         // above)
00171         vectorField projectVecs(start.size(), projectVec);
00172 
00173         if (projectMode_ == POINTNORMAL)
00174         {
00175             projectVecs = projectLen*patch().pointNormals();
00176         }
00177 
00178         // Knock out any wedge component
00179         scalarField offset(start.size(), 0.0);
00180         if (wedgePlane_ >= 0 && wedgePlane_ <= vector::nComponents)
00181         {
00182             forAll(offset, i)
00183             {
00184                 offset[i] = start[i][wedgePlane_];
00185                 start[i][wedgePlane_] = 0;
00186                 projectVecs[i][wedgePlane_] = 0;
00187             }
00188         }
00189 
00190         List<pointIndexHit> rightHit;
00191         {
00192             labelList rightSurf;
00193             surfaces().findAnyIntersection
00194             (
00195                 start,
00196                 start+projectVecs,
00197                 rightSurf,
00198                 rightHit
00199             );
00200         }
00201         
00202         List<pointIndexHit> leftHit;
00203         {
00204             labelList leftSurf;
00205             surfaces().findAnyIntersection
00206             (
00207                 start,
00208                 start-projectVecs,
00209                 leftSurf,
00210                 leftHit
00211             );
00212         }
00213 
00214         // 3. Choose either -fixed, nearest, right, left.
00215         forAll(displacement, i)
00216         {
00217             if (zonePtr && (zonePtr->whichPoint(meshPoints[i]) >= 0))
00218             {
00219                 // Fixed point. Reset to point0 location.
00220                 displacement[i] = points0[meshPoints[i]] - localPoints[i];
00221             }
00222             else if (nearest[i].hit())
00223             {
00224                 // Found nearest.
00225                 displacement[i] =
00226                     nearest[i].hitPoint()
00227                   - points0[meshPoints[i]];
00228             }
00229             else
00230             {
00231                 pointIndexHit interPt;
00232 
00233                 if (rightHit[i].hit())
00234                 {
00235                     if (leftHit[i].hit())
00236                     {
00237                         if
00238                         (
00239                             magSqr(rightHit[i].hitPoint()-start[i])
00240                           < magSqr(leftHit[i].hitPoint()-start[i])
00241                         )
00242                         {
00243                             interPt = rightHit[i];
00244                         }
00245                         else
00246                         {
00247                             interPt = leftHit[i];
00248                         }
00249                     }
00250                     else
00251                     {
00252                         interPt = rightHit[i];
00253                     }
00254                 }
00255                 else
00256                 {
00257                     if (leftHit[i].hit())
00258                     {
00259                         interPt = leftHit[i];
00260                     }
00261                 }
00262 
00263 
00264                 if (interPt.hit())
00265                 {
00266                     if (wedgePlane_ >= 0 && wedgePlane_ <= vector::nComponents)
00267                     {
00268                         interPt.rawPoint()[wedgePlane_] += offset[i];
00269                     }
00270                     displacement[i] = interPt.rawPoint()-points0[meshPoints[i]];
00271                 }
00272                 else
00273                 {
00274                     nNotProjected++;
00275 
00276                     if (debug)
00277                     {
00278                         Pout<< "    point:" << meshPoints[i]
00279                             << " coord:" << localPoints[i]
00280                             << "  did not find any intersection between"
00281                             << " ray from " << start[i]-projectVecs[i]
00282                             << " to " << start[i]+projectVecs[i] << endl;
00283                     }
00284                 }
00285             }
00286         }
00287     }
00288 
00289     reduce(nNotProjected, sumOp<label>());
00290 
00291     if (nNotProjected > 0)
00292     {
00293         Info<< "surfaceDisplacement :"
00294             << " on patch " << patch().name()
00295             << " did not project " << nNotProjected
00296             << " out of " << returnReduce(localPoints.size(), sumOp<label>())
00297             << " points." << endl;
00298     }
00299 }
00300 
00301 
00302 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00303 
00304 surfaceDisplacementPointPatchVectorField::
00305 surfaceDisplacementPointPatchVectorField
00306 (
00307     const pointPatch& p,
00308     const DimensionedField<vector, pointMesh>& iF
00309 )
00310 :
00311     fixedValuePointPatchVectorField(p, iF),
00312     velocity_(vector::zero),
00313     projectMode_(NEAREST),
00314     projectDir_(vector::zero),
00315     wedgePlane_(-1)
00316 {}
00317 
00318 
00319 surfaceDisplacementPointPatchVectorField::
00320 surfaceDisplacementPointPatchVectorField
00321 (
00322     const pointPatch& p,
00323     const DimensionedField<vector, pointMesh>& iF,
00324     const dictionary& dict
00325 )
00326 :
00327     fixedValuePointPatchVectorField(p, iF, dict),
00328     velocity_(dict.lookup("velocity")),
00329     surfacesDict_(dict.subDict("geometry")),
00330     projectMode_(projectModeNames_.read(dict.lookup("projectMode"))),
00331     projectDir_(dict.lookup("projectDirection")),
00332     wedgePlane_(readLabel(dict.lookup("wedgePlane"))),
00333     frozenPointsZone_(dict.lookupOrDefault("frozenPointsZone", word::null))
00334 {
00335     if (velocity_.x() < 0 || velocity_.y() < 0 || velocity_.z() < 0)
00336     {
00337         FatalErrorIn
00338         (
00339             "surfaceDisplacementPointPatchVectorField::\n"
00340             "surfaceDisplacementPointPatchVectorField\n"
00341             "(\n"
00342             "    const pointPatch& p,\n"
00343             "    const DimensionedField<vector, pointMesh>& iF,\n"
00344             "    const dictionary& dict\n"
00345             ")\n"
00346         )   << "All components of velocity have to be positive : "
00347             << velocity_ << nl
00348             << "Set velocity components to a great value if no clipping"
00349             << " necessary." << exit(FatalError);
00350     }
00351 }
00352 
00353 
00354 surfaceDisplacementPointPatchVectorField::
00355 surfaceDisplacementPointPatchVectorField
00356 (
00357     const surfaceDisplacementPointPatchVectorField& ppf,
00358     const pointPatch& p,
00359     const DimensionedField<vector, pointMesh>& iF,
00360     const pointPatchFieldMapper& mapper
00361 )
00362 :
00363     fixedValuePointPatchVectorField(ppf, p, iF, mapper),
00364     velocity_(ppf.velocity_),
00365     surfacesDict_(ppf.surfacesDict_),
00366     projectMode_(ppf.projectMode_),
00367     projectDir_(ppf.projectDir_),
00368     wedgePlane_(ppf.wedgePlane_),
00369     frozenPointsZone_(ppf.frozenPointsZone_)
00370 {}
00371 
00372 
00373 surfaceDisplacementPointPatchVectorField::
00374 surfaceDisplacementPointPatchVectorField
00375 (
00376     const surfaceDisplacementPointPatchVectorField& ppf
00377 )
00378 :
00379     fixedValuePointPatchVectorField(ppf),
00380     velocity_(ppf.velocity_),
00381     surfacesDict_(ppf.surfacesDict_),
00382     projectMode_(ppf.projectMode_),
00383     projectDir_(ppf.projectDir_),
00384     wedgePlane_(ppf.wedgePlane_),
00385     frozenPointsZone_(ppf.frozenPointsZone_)
00386 {}
00387 
00388 
00389 surfaceDisplacementPointPatchVectorField::
00390 surfaceDisplacementPointPatchVectorField
00391 (
00392     const surfaceDisplacementPointPatchVectorField& ppf,
00393     const DimensionedField<vector, pointMesh>& iF
00394 )
00395 :
00396     fixedValuePointPatchVectorField(ppf, iF),
00397     velocity_(ppf.velocity_),
00398     surfacesDict_(ppf.surfacesDict_),
00399     projectMode_(ppf.projectMode_),
00400     projectDir_(ppf.projectDir_),
00401     wedgePlane_(ppf.wedgePlane_),
00402     frozenPointsZone_(ppf.frozenPointsZone_)
00403 {}
00404 
00405 
00406 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00407 
00408 const searchableSurfaces&
00409 surfaceDisplacementPointPatchVectorField::surfaces() const
00410 {
00411     if (surfacesPtr_.empty())
00412     {
00413         surfacesPtr_.reset
00414         (
00415             new searchableSurfaces
00416             (
00417                 IOobject
00418                 (
00419                     "abc",                              // dummy name
00420                     db().time().constant(),             // directory
00421                     "triSurface",                       // instance
00422                     db().time(),                        // registry
00423                     IOobject::MUST_READ,
00424                     IOobject::NO_WRITE
00425                 ),
00426                 surfacesDict_
00427             )
00428         );
00429     }
00430     return surfacesPtr_();
00431 }
00432 
00433 
00434 void surfaceDisplacementPointPatchVectorField::updateCoeffs()
00435 {
00436     if (this->updated())
00437     {
00438         return;
00439     }
00440 
00441     const polyMesh& mesh = patch().boundaryMesh().mesh()();
00442 
00443     vectorField currentDisplacement = this->patchInternalField();
00444 
00445     // Calculate intersections with surface w.r.t points0.
00446     vectorField displacement(currentDisplacement);
00447     calcProjection(displacement);
00448 
00449     // offset wrt current displacement
00450     vectorField offset = displacement-currentDisplacement;
00451 
00452     // Clip offset to maximum displacement possible: velocity*timestep
00453 
00454     const scalar deltaT = mesh.time().deltaT().value();
00455     const vector clipVelocity = velocity_*deltaT;
00456 
00457     forAll(displacement, i)
00458     {
00459         vector& d = offset[i];
00460 
00461         for (direction cmpt = 0; cmpt < vector::nComponents; cmpt++)
00462         {
00463             if (d[cmpt] < 0)
00464             {
00465                 d[cmpt] = max(d[cmpt], -clipVelocity[cmpt]);
00466             }
00467             else
00468             {
00469                 d[cmpt] = min(d[cmpt], clipVelocity[cmpt]);
00470             }
00471         }
00472     }
00473 
00474     this->operator==(currentDisplacement+offset);
00475 
00476     fixedValuePointPatchVectorField::updateCoeffs();
00477 }
00478 
00479 
00480 void surfaceDisplacementPointPatchVectorField::write(Ostream& os) const
00481 {
00482     fixedValuePointPatchVectorField::write(os);
00483     os.writeKeyword("velocity") << velocity_
00484         << token::END_STATEMENT << nl;
00485     os.writeKeyword("geometry") << surfacesDict_
00486         << token::END_STATEMENT << nl;
00487     os.writeKeyword("projectMode") << projectModeNames_[projectMode_]
00488         << token::END_STATEMENT << nl;
00489     os.writeKeyword("projectDirection") << projectDir_
00490         << token::END_STATEMENT << nl;
00491     os.writeKeyword("wedgePlane") << wedgePlane_
00492         << token::END_STATEMENT << nl;
00493     if (frozenPointsZone_ != word::null)
00494     {
00495         os.writeKeyword("frozenPointsZone") << frozenPointsZone_
00496             << token::END_STATEMENT << nl;
00497     }
00498 }
00499 
00500 
00501 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00502 
00503 makePointPatchTypeField
00504 (
00505     fixedValuePointPatchVectorField,
00506     surfaceDisplacementPointPatchVectorField
00507 );
00508 
00509 
00510 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00511 
00512 } // End namespace Foam
00513 
00514 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd