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

streamFunction.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-2009 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 Application
00026     streamFunction
00027 
00028 Description
00029     Calculates and writes the stream function of velocity field U at each time
00030 
00031 \*---------------------------------------------------------------------------*/
00032 
00033 #include "fvCFD.H"
00034 #include "pointFields.H"
00035 #include "emptyPolyPatch.H"
00036 #include "symmetryPolyPatch.H"
00037 #include "wedgePolyPatch.H"
00038 #include "OSspecific.H"
00039 
00040 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00041 //  Main program:
00042 
00043 int main(int argc, char *argv[])
00044 {
00045     timeSelector::addOptions();
00046 
00047 #   include "setRootCase.H"
00048 #   include "createTime.H"
00049 
00050     instantList timeDirs = timeSelector::select0(runTime, args);
00051 
00052 #   include "createMeshNoClear.H"
00053 
00054     pointMesh pMesh(mesh);
00055 
00056     forAll(timeDirs, timeI)
00057     {
00058         runTime.setTime(timeDirs[timeI], timeI);
00059 
00060         Info<< nl << "Time: " << runTime.timeName() << endl;
00061 
00062         IOobject phiHeader
00063         (
00064             "phi",
00065             runTime.timeName(),
00066             mesh,
00067             IOobject::NO_READ
00068         );
00069 
00070         if (phiHeader.headerOk())
00071         {
00072             mesh.readUpdate();
00073 
00074             Info<< nl << "Reading field phi" << endl;
00075 
00076             surfaceScalarField phi
00077             (
00078                 IOobject
00079                 (
00080                     "phi",
00081                     runTime.timeName(),
00082                     mesh,
00083                     IOobject::MUST_READ,
00084                     IOobject::NO_WRITE
00085                 ),
00086                 mesh
00087             );
00088 
00089             pointScalarField streamFunction
00090             (
00091                 IOobject
00092                 (
00093                     "streamFunction",
00094                     runTime.timeName(),
00095                     mesh,
00096                     IOobject::NO_READ,
00097                     IOobject::NO_WRITE
00098                 ),
00099                 pMesh,
00100                 dimensionedScalar("zero", phi.dimensions(), 0.0)
00101             );
00102 
00103             labelList visitedPoint(mesh.nPoints());
00104             forAll (visitedPoint, pointI)
00105             {
00106                 visitedPoint[pointI] = 0;
00107             }
00108             label nVisited = 0;
00109             label nVisitedOld = 0;
00110 
00111             const unallocFaceList& faces = mesh.faces();
00112             const pointField& points = mesh.points();
00113 
00114             label nInternalFaces = mesh.nInternalFaces();
00115 
00116             vectorField unitAreas = mesh.faceAreas();
00117             unitAreas /= mag(unitAreas);
00118 
00119             const polyPatchList& patches = mesh.boundaryMesh();
00120 
00121             bool finished = true;
00122 
00123             // Find the boundary face with zero flux. set the stream function
00124             // to zero on that face
00125             bool found = false;
00126 
00127             do
00128             {
00129                 found = false;
00130 
00131                 forAll (patches, patchI)
00132                 {
00133                     const primitivePatch& bouFaces = patches[patchI];
00134 
00135                     if (!isType<emptyPolyPatch>(patches[patchI]))
00136                     {
00137                         forAll (bouFaces, faceI)
00138                         {
00139                             if
00140                             (
00141                                 magSqr(phi.boundaryField()[patchI][faceI])
00142                               < SMALL
00143                             )
00144                             {
00145                                 const labelList& zeroPoints = bouFaces[faceI];
00146 
00147                                 // Zero flux face found
00148                                 found = true;
00149 
00150                                 forAll (zeroPoints, pointI)
00151                                 {
00152                                     if (visitedPoint[zeroPoints[pointI]] == 1)
00153                                     {
00154                                         found = false;
00155                                         break;
00156                                     }
00157                                 }
00158 
00159                                 if (found)
00160                                 {
00161                                     Info<< "Zero face: patch: " << patchI
00162                                         << "    face: " << faceI << endl;
00163 
00164                                     forAll (zeroPoints, pointI)
00165                                     {
00166                                         streamFunction[zeroPoints[pointI]] = 0;
00167                                         visitedPoint[zeroPoints[pointI]] = 1;
00168                                         nVisited++;
00169                                     }
00170 
00171                                     break;
00172                                 }
00173                             }
00174                         }
00175                     }
00176 
00177                     if (found) break;
00178                 }
00179 
00180                 if (!found)
00181                 {
00182                     Info<< "zero flux boundary face not found. "
00183                         << "Using cell as a reference."
00184                         << endl;
00185 
00186                     const cellList& c = mesh.cells();
00187 
00188                     forAll (c, cI)
00189                     {
00190                         labelList zeroPoints = c[cI].labels(mesh.faces());
00191 
00192                         bool found = true;
00193 
00194                         forAll (zeroPoints, pointI)
00195                         {
00196                             if (visitedPoint[zeroPoints[pointI]] == 1)
00197                             {
00198                                 found = false;
00199                                 break;
00200                             }
00201                         }
00202 
00203                         if (found)
00204                         {
00205                             forAll (zeroPoints, pointI)
00206                             {
00207                                 streamFunction[zeroPoints[pointI]] = 0.0;
00208                                 visitedPoint[zeroPoints[pointI]] = 1;
00209                                 nVisited++;
00210                             }
00211 
00212                             break;
00213                         }
00214                         else
00215                         {
00216                             FatalErrorIn(args.executable())
00217                                 << "Cannot find initialisation face or a cell."
00218                                 << abort(FatalError);
00219                         }
00220                     }
00221                 }
00222 
00223                 // Loop through all faces. If one of the points on
00224                 // the face has the streamfunction value different
00225                 // from -1, all points with -1 ont that face have the
00226                 // streamfunction value equal to the face flux in
00227                 // that point plus the value in the visited point
00228                 do
00229                 {
00230                      finished = true;
00231 
00232                      for
00233                      (
00234                          label faceI = nInternalFaces;
00235                          faceI<faces.size();
00236                          faceI++
00237                      )
00238                      {
00239                          const labelList& curBPoints = faces[faceI];
00240                          bool bPointFound = false;
00241 
00242                          scalar currentBStream = 0.0;
00243                          vector currentBStreamPoint(0, 0, 0);
00244 
00245                          forAll (curBPoints, pointI)
00246                          {
00247                              // Check if the point has been visited
00248                              if (visitedPoint[curBPoints[pointI]] == 1)
00249                              {
00250                                  // The point has been visited
00251                                  currentBStream =
00252                                      streamFunction[curBPoints[pointI]];
00253                                  currentBStreamPoint =
00254                                      points[curBPoints[pointI]];
00255 
00256                                  bPointFound = true;
00257 
00258                                  break;
00259                              }
00260                          }
00261 
00262                          if (bPointFound)
00263                          {
00264                              // Sort out other points on the face
00265                              forAll (curBPoints, pointI)
00266                              {
00267                                  // Check if the point has been visited
00268                                  if (visitedPoint[curBPoints[pointI]] == 0)
00269                                  {
00270                                      label patchNo =
00271                                          mesh.boundaryMesh().whichPatch(faceI);
00272 
00273                                      if
00274                                      (
00275                                         !isType<emptyPolyPatch>
00276                                          (patches[patchNo])
00277                                      && !isType<symmetryPolyPatch>
00278                                          (patches[patchNo])
00279                                      && !isType<wedgePolyPatch>
00280                                          (patches[patchNo])
00281                                      )
00282                                      {
00283                                          label faceNo =
00284                                              mesh.boundaryMesh()[patchNo]
00285                                              .whichFace(faceI);
00286 
00287                                          vector edgeHat =
00288                                              points[curBPoints[pointI]]
00289                                              - currentBStreamPoint;
00290                                          edgeHat.replace(vector::Z, 0);
00291                                          edgeHat /= mag(edgeHat);
00292 
00293                                          vector nHat = unitAreas[faceI];
00294 
00295                                          if (edgeHat.y() > VSMALL)
00296                                          {
00297                                              visitedPoint[curBPoints[pointI]] =
00298                                                  1;
00299                                              nVisited++;
00300 
00301                                              streamFunction[curBPoints[pointI]]
00302                                                  =
00303                                                  currentBStream
00304                                                + phi.boundaryField()
00305                                                  [patchNo][faceNo]
00306                                                  *sign(nHat.x());
00307                                          }
00308                                          else if (edgeHat.y() < -VSMALL)
00309                                          {
00310                                              visitedPoint[curBPoints[pointI]] =
00311                                                  1;
00312                                              nVisited++;
00313 
00314                                              streamFunction[curBPoints[pointI]]
00315                                                  =
00316                                                  currentBStream
00317                                                - phi.boundaryField()
00318                                                  [patchNo][faceNo]
00319                                                  *sign(nHat.x());
00320                                          }
00321                                          else
00322                                          {
00323                                              if (edgeHat.x() > VSMALL)
00324                                              {
00325                                                  visitedPoint
00326                                                      [curBPoints[pointI]] = 1;
00327                                                  nVisited++;
00328 
00329                                                  streamFunction
00330                                                      [curBPoints[pointI]] =
00331                                                      currentBStream
00332                                                    + phi.boundaryField()
00333                                                      [patchNo][faceNo]
00334                                                      *sign(nHat.y());
00335                                              }
00336                                              else if (edgeHat.x() < -VSMALL)
00337                                              {
00338                                                  visitedPoint
00339                                                      [curBPoints[pointI]] = 1;
00340                                                  nVisited++;
00341 
00342                                                  streamFunction
00343                                                      [curBPoints[pointI]] =
00344                                                      currentBStream
00345                                                    - phi.boundaryField()
00346                                                      [patchNo][faceNo]
00347                                                      *sign(nHat.y());
00348                                              }
00349                                          }
00350                                      }
00351                                  }
00352                              }
00353                          }
00354                          else
00355                          {
00356                              finished = false;
00357                          }
00358                      }
00359 
00360                      for (label faceI=0; faceI<nInternalFaces; faceI++)
00361                      {
00362                          // Get the list of point labels for the face
00363                          const labelList& curPoints = faces[faceI];
00364 
00365                          bool pointFound = false;
00366 
00367                          scalar currentStream = 0.0;
00368                          point currentStreamPoint(0, 0, 0);
00369 
00370                          forAll (curPoints, pointI)
00371                          {
00372                              // Check if the point has been visited
00373                              if (visitedPoint[curPoints[pointI]] == 1)
00374                              {
00375                                  // The point has been visited
00376                                  currentStream =
00377                                      streamFunction[curPoints[pointI]];
00378                                  currentStreamPoint =
00379                                      points[curPoints[pointI]];
00380                                  pointFound = true;
00381 
00382                                  break;
00383                              }
00384                          }
00385 
00386                          if (pointFound)
00387                          {
00388                              // Sort out other points on the face
00389                              forAll (curPoints, pointI)
00390                              {
00391                                  // Check if the point has been visited
00392                                  if (visitedPoint[curPoints[pointI]] == 0)
00393                                  {
00394                                      vector edgeHat =
00395                                          points[curPoints[pointI]]
00396                                        - currentStreamPoint;
00397 
00398                                      edgeHat.replace(vector::Z, 0);
00399                                      edgeHat /= mag(edgeHat);
00400 
00401                                      vector nHat = unitAreas[faceI];
00402 
00403                                      if (edgeHat.y() > VSMALL)
00404                                      {
00405                                          visitedPoint[curPoints[pointI]] = 1;
00406                                          nVisited++;
00407 
00408                                          streamFunction[curPoints[pointI]] =
00409                                              currentStream
00410                                            + phi[faceI]*sign(nHat.x());
00411                                      }
00412                                      else if (edgeHat.y() < -VSMALL)
00413                                      {
00414                                          visitedPoint[curPoints[pointI]] = 1;
00415                                          nVisited++;
00416 
00417                                          streamFunction[curPoints[pointI]] =
00418                                              currentStream
00419                                            - phi[faceI]*sign(nHat.x());
00420                                      }
00421                                  }
00422                              }
00423                          }
00424                          else
00425                          {
00426                              finished = false;
00427                          }
00428                      }
00429 
00430                      Info<< ".";
00431 //                     Info<< "One pass, n visited = " << nVisited << endl;
00432 
00433                      if (nVisited == nVisitedOld)
00434                      {
00435                          // Find new seed.  This must be a
00436                          // multiply connected domain
00437                          Info<< nl << "Exhausted a seed. Looking for new seed "
00438                              << "(this is correct for multiply connected "
00439                              << "domains).";
00440 
00441                          break;
00442                      }
00443                      else
00444                      {
00445                          nVisitedOld = nVisited;
00446                      }
00447                 } while (!finished);
00448 
00449                 Info << endl;
00450             } while (!finished);
00451 
00452             streamFunction.boundaryField() = 0.0;
00453             streamFunction.write();
00454         }
00455         else
00456         {
00457             WarningIn(args.executable())
00458                 << "Flux field does not exist."
00459                 << " Stream function not calculated" << endl;
00460         }
00461     }
00462 
00463     Info<< "\nEnd\n" << endl;
00464 
00465     return 0;
00466 }
00467 
00468 
00469 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd