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

isoSurfaceTemplates.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 \*---------------------------------------------------------------------------*/
00026 
00027 #include "isoSurface.H"
00028 #include "polyMesh.H"
00029 #include "syncTools.H"
00030 #include "surfaceFields.H"
00031 #include "OFstream.H"
00032 #include "meshTools.H"
00033 
00034 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00035 
00036 template<class Type>
00037 Foam::tmp<Foam::SlicedGeometricField
00038 <
00039     Type,
00040     Foam::fvPatchField,
00041     Foam::slicedFvPatchField,
00042     Foam::volMesh
00043 > >
00044 Foam::isoSurface::adaptPatchFields
00045 (
00046     const GeometricField<Type, fvPatchField, volMesh>& fld
00047 ) const
00048 {
00049     typedef SlicedGeometricField
00050     <
00051         Type,
00052         fvPatchField,
00053         slicedFvPatchField,
00054         volMesh
00055     > FieldType;
00056 
00057     tmp<FieldType> tsliceFld
00058     (
00059         new FieldType
00060         (
00061             IOobject
00062             (
00063                 fld.name(),
00064                 fld.instance(),
00065                 fld.db(),
00066                 IOobject::NO_READ,
00067                 IOobject::NO_WRITE,
00068                 false
00069             ),
00070             fld,        // internal field
00071             true        // preserveCouples
00072         )
00073     );
00074     FieldType& sliceFld = tsliceFld();
00075 
00076     const fvMesh& mesh = fld.mesh();
00077 
00078     const polyBoundaryMesh& patches = mesh.boundaryMesh();
00079 
00080     forAll(patches, patchI)
00081     {
00082         const polyPatch& pp = patches[patchI];
00083 
00084         if
00085         (
00086             isA<emptyPolyPatch>(pp)
00087          && pp.size() != sliceFld.boundaryField()[patchI].size()
00088         )
00089         {
00090             // Clear old value. Cannot resize it since is a slice.
00091             sliceFld.boundaryField().set(patchI, NULL);
00092 
00093             // Set new value we can change
00094             sliceFld.boundaryField().set
00095             (
00096                 patchI,
00097                 new calculatedFvPatchField<Type>
00098                 (
00099                     mesh.boundary()[patchI],
00100                     sliceFld
00101                 )
00102             );
00103 
00104             // Note: cannot use patchInternalField since uses emptyFvPatch::size
00105             // Do our own internalField instead.
00106             const unallocLabelList& faceCells =
00107                 mesh.boundary()[patchI].patch().faceCells();
00108 
00109             Field<Type>& pfld = sliceFld.boundaryField()[patchI];
00110             pfld.setSize(faceCells.size());
00111             forAll(faceCells, i)
00112             {
00113                 pfld[i] = sliceFld[faceCells[i]];
00114             }
00115         }
00116         else if (isA<cyclicPolyPatch>(pp))
00117         {
00118             // Already has interpolate as value
00119         }
00120         else if (isA<processorPolyPatch>(pp) && !collocatedPatch(pp))
00121         {
00122             fvPatchField<Type>& pfld = const_cast<fvPatchField<Type>&>
00123             (
00124                 fld.boundaryField()[patchI]
00125             );
00126 
00127             const scalarField& w = mesh.weights().boundaryField()[patchI];
00128 
00129             tmp<Field<Type> > f =
00130                 w*pfld.patchInternalField()
00131               + (1.0-w)*pfld.patchNeighbourField();
00132 
00133             PackedBoolList isCollocated
00134             (
00135                 collocatedFaces(refCast<const processorPolyPatch>(pp))
00136             );
00137 
00138             forAll(isCollocated, i)
00139             {
00140                 if (!isCollocated[i])
00141                 {
00142                     pfld[i] = f()[i];
00143                 }
00144             }
00145         }
00146     }
00147     return tsliceFld;
00148 }
00149 
00150 
00151 template<class Type>
00152 Type Foam::isoSurface::generatePoint
00153 (
00154     const scalar s0,
00155     const Type& p0,
00156     const bool hasSnap0,
00157     const Type& snapP0,
00158 
00159     const scalar s1,
00160     const Type& p1,
00161     const bool hasSnap1,
00162     const Type& snapP1
00163 ) const
00164 {
00165     scalar d = s1-s0;
00166 
00167     if (mag(d) > VSMALL)
00168     {
00169         scalar s = (iso_-s0)/d;
00170 
00171         if (hasSnap1 && s >= 0.5 && s <= 1)
00172         {
00173             return snapP1;
00174         }
00175         else if (hasSnap0 && s >= 0.0 && s <= 0.5)
00176         {
00177             return snapP0;
00178         }
00179         else
00180         {
00181             return s*p1 + (1.0-s)*p0;
00182         }
00183     }
00184     else
00185     {
00186         scalar s = 0.4999;
00187 
00188         return s*p1 + (1.0-s)*p0;
00189     }
00190 }
00191 
00192 
00193 template<class Type>
00194 void Foam::isoSurface::generateTriPoints
00195 (
00196     const scalar s0,
00197     const Type& p0,
00198     const bool hasSnap0,
00199     const Type& snapP0,
00200 
00201     const scalar s1,
00202     const Type& p1,
00203     const bool hasSnap1,
00204     const Type& snapP1,
00205 
00206     const scalar s2,
00207     const Type& p2,
00208     const bool hasSnap2,
00209     const Type& snapP2,
00210 
00211     const scalar s3,
00212     const Type& p3,
00213     const bool hasSnap3,
00214     const Type& snapP3,
00215 
00216     DynamicList<Type>& points
00217 ) const
00218 {
00219     int triIndex = 0;
00220     if (s0 < iso_)
00221     {
00222         triIndex |= 1;
00223     }
00224     if (s1 < iso_)
00225     {
00226         triIndex |= 2;
00227     }
00228     if (s2 < iso_)
00229     {
00230         triIndex |= 4;
00231     }
00232     if (s3 < iso_)
00233     {
00234         triIndex |= 8;
00235     }
00236 
00237     /* Form the vertices of the triangles for each case */
00238     switch (triIndex)
00239     {
00240         case 0x00:
00241         case 0x0F:
00242         break;
00243 
00244         case 0x0E:
00245         case 0x01:
00246             points.append
00247             (
00248                 generatePoint(s0,p0,hasSnap0,snapP0,s1,p1,hasSnap1,snapP1)
00249             );
00250             points.append
00251             (
00252                 generatePoint(s0,p0,hasSnap0,snapP0,s2,p2,hasSnap2,snapP2)
00253             );
00254             points.append
00255             (
00256                 generatePoint(s0,p0,hasSnap0,snapP0,s3,p3,hasSnap3,snapP3)
00257             );
00258         break;
00259 
00260         case 0x0D:
00261         case 0x02:
00262             points.append
00263             (
00264                 generatePoint(s1,p1,hasSnap1,snapP1,s0,p0,hasSnap0,snapP0)
00265             );
00266             points.append
00267             (
00268                 generatePoint(s1,p1,hasSnap1,snapP1,s3,p3,hasSnap3,snapP3)
00269             );
00270             points.append
00271             (
00272                 generatePoint(s1,p1,hasSnap1,snapP1,s2,p2,hasSnap2,snapP2)
00273             );
00274         break;
00275 
00276         case 0x0C:
00277         case 0x03:
00278         {
00279             Type tp1 =
00280                 generatePoint(s0,p0,hasSnap0,snapP0,s2,p2,hasSnap2,snapP2);
00281             Type tp2 =
00282                 generatePoint(s1,p1,hasSnap1,snapP1,s3,p3,hasSnap3,snapP3);
00283 
00284             points.append
00285             (
00286                 generatePoint(s0,p0,hasSnap0,snapP0,s3,p3,hasSnap3,snapP3)
00287             );
00288             points.append(tp1);
00289             points.append(tp2);
00290             points.append(tp2);
00291             points.append
00292             (
00293                 generatePoint(s1,p1,hasSnap1,snapP1,s2,p2,hasSnap2,snapP2)
00294             );
00295             points.append(tp1);
00296         }
00297         break;
00298 
00299         case 0x0B:
00300         case 0x04:
00301         {
00302             points.append
00303             (
00304                 generatePoint(s2,p2,hasSnap2,snapP2,s0,p0,hasSnap0,snapP0)
00305             );
00306             points.append
00307             (
00308                 generatePoint(s2,p2,hasSnap2,snapP2,s1,p1,hasSnap1,snapP1)
00309             );
00310             points.append
00311             (
00312                 generatePoint(s2,p2,hasSnap2,snapP2,s3,p3,hasSnap3,snapP3)
00313             );
00314         }
00315         break;
00316 
00317         case 0x0A:
00318         case 0x05:
00319         {
00320             Type tp0 =
00321                 generatePoint(s0,p0,hasSnap0,snapP0,s1,p1,hasSnap1,snapP1);
00322             Type tp1 =
00323                 generatePoint(s2,p2,hasSnap2,snapP2,s3,p3,hasSnap3,snapP3);
00324 
00325             points.append(tp0);
00326             points.append(tp1);
00327             points.append
00328             (
00329                 generatePoint(s0,p0,hasSnap0,snapP0,s3,p3,hasSnap3,snapP3)
00330             );
00331             points.append(tp0);
00332             points.append
00333             (
00334                 generatePoint(s1,p1,hasSnap1,snapP1,s2,p2,hasSnap2,snapP2)
00335             );
00336             points.append(tp1);
00337         }
00338         break;
00339 
00340         case 0x09:
00341         case 0x06:
00342         {
00343             Type tp0 =
00344                 generatePoint(s0,p0,hasSnap0,snapP0,s1,p1,hasSnap1,snapP1);
00345             Type tp1 =
00346                 generatePoint(s2,p2,hasSnap2,snapP2,s3,p3,hasSnap3,snapP3);
00347 
00348             points.append(tp0);
00349             points.append
00350             (
00351                 generatePoint(s1,p1,hasSnap1,snapP1,s3,p3,hasSnap3,snapP3)
00352             );
00353             points.append(tp1);
00354             points.append(tp0);
00355             points.append
00356             (
00357                 generatePoint(s0,p0,hasSnap0,snapP0,s2,p2,hasSnap2,snapP2)
00358             );
00359             points.append(tp1);
00360         }
00361         break;
00362 
00363         case 0x07:
00364         case 0x08:
00365             points.append
00366             (
00367                 generatePoint(s3,p3,hasSnap3,snapP3,s0,p0,hasSnap0,snapP0)
00368             );
00369             points.append
00370             (
00371                 generatePoint(s3,p3,hasSnap3,snapP3,s2,p2,hasSnap2,snapP2)
00372             );
00373             points.append
00374             (
00375                 generatePoint(s3,p3,hasSnap3,snapP3,s1,p1,hasSnap1,snapP1)
00376             );
00377         break;
00378     }
00379 }
00380 
00381 
00382 template<class Type>
00383 Foam::label Foam::isoSurface::generateFaceTriPoints
00384 (
00385     const volScalarField& cVals,
00386     const scalarField& pVals,
00387 
00388     const GeometricField<Type, fvPatchField, volMesh>& cCoords,
00389     const Field<Type>& pCoords,
00390 
00391     const DynamicList<Type>& snappedPoints,
00392     const labelList& snappedCc,
00393     const labelList& snappedPoint,
00394     const label faceI,
00395 
00396     const scalar neiVal,
00397     const Type& neiPt,
00398     const bool hasNeiSnap,
00399     const Type& neiSnapPt,
00400 
00401     DynamicList<Type>& triPoints,
00402     DynamicList<label>& triMeshCells
00403 ) const
00404 {
00405     label own = mesh_.faceOwner()[faceI];
00406 
00407     label oldNPoints = triPoints.size();
00408 
00409     const face& f = mesh_.faces()[faceI];
00410 
00411     forAll(f, fp)
00412     {
00413         label pointI = f[fp];
00414         label nextPointI = f[f.fcIndex(fp)];
00415 
00416         generateTriPoints
00417         (
00418             pVals[pointI],
00419             pCoords[pointI],
00420             snappedPoint[pointI] != -1,
00421             (
00422                 snappedPoint[pointI] != -1
00423               ? snappedPoints[snappedPoint[pointI]]
00424               : pTraits<Type>::zero
00425             ),
00426 
00427             pVals[nextPointI],
00428             pCoords[nextPointI],
00429             snappedPoint[nextPointI] != -1,
00430             (
00431                 snappedPoint[nextPointI] != -1
00432               ? snappedPoints[snappedPoint[nextPointI]]
00433               : pTraits<Type>::zero
00434             ),
00435 
00436             cVals[own],
00437             cCoords[own],
00438             snappedCc[own] != -1,
00439             (
00440                 snappedCc[own] != -1
00441               ? snappedPoints[snappedCc[own]]
00442               : pTraits<Type>::zero
00443             ),
00444 
00445             neiVal,
00446             neiPt,
00447             hasNeiSnap,
00448             neiSnapPt,
00449 
00450             triPoints
00451         );
00452     }
00453 
00454     // Every three triPoints is a triangle
00455     label nTris = (triPoints.size()-oldNPoints)/3;
00456     for (label i = 0; i < nTris; i++)
00457     {
00458         triMeshCells.append(own);
00459     }
00460 
00461     return nTris;
00462 }
00463 
00464 
00465 template<class Type>
00466 void Foam::isoSurface::generateTriPoints
00467 (
00468     const volScalarField& cVals,
00469     const scalarField& pVals,
00470 
00471     const GeometricField<Type, fvPatchField, volMesh>& cCoords,
00472     const Field<Type>& pCoords,
00473 
00474     const DynamicList<Type>& snappedPoints,
00475     const labelList& snappedCc,
00476     const labelList& snappedPoint,
00477 
00478     DynamicList<Type>& triPoints,
00479     DynamicList<label>& triMeshCells
00480 ) const
00481 {
00482     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
00483     const labelList& own = mesh_.faceOwner();
00484     const labelList& nei = mesh_.faceNeighbour();
00485 
00486     if
00487     (
00488         (cVals.size() != mesh_.nCells())
00489      || (pVals.size() != mesh_.nPoints())
00490      || (cCoords.size() != mesh_.nCells())
00491      || (pCoords.size() != mesh_.nPoints())
00492      || (snappedCc.size() != mesh_.nCells())
00493      || (snappedPoint.size() != mesh_.nPoints())
00494     )
00495     {
00496         FatalErrorIn("isoSurface::generateTriPoints(..)")
00497             << "Incorrect size." << endl
00498             << "mesh: nCells:" << mesh_.nCells()
00499             << " points:" << mesh_.nPoints() << endl
00500             << "cVals:" << cVals.size() << endl
00501             << "cCoords:" << cCoords.size() << endl
00502             << "snappedCc:" << snappedCc.size() << endl
00503             << "pVals:" << pVals.size() << endl
00504             << "pCoords:" << pCoords.size() << endl
00505             << "snappedPoint:" << snappedPoint.size() << endl
00506             << abort(FatalError);
00507     }
00508 
00509 
00510     // Generate triangle points
00511 
00512     triPoints.clear();
00513     triMeshCells.clear();
00514 
00515     for (label faceI = 0; faceI < mesh_.nInternalFaces(); faceI++)
00516     {
00517         if (faceCutType_[faceI] != NOTCUT)
00518         {
00519             generateFaceTriPoints
00520             (
00521                 cVals,
00522                 pVals,
00523 
00524                 cCoords,
00525                 pCoords,
00526 
00527                 snappedPoints,
00528                 snappedCc,
00529                 snappedPoint,
00530                 faceI,
00531 
00532                 cVals[nei[faceI]],
00533                 cCoords[nei[faceI]],
00534                 snappedCc[nei[faceI]] != -1,
00535                 (
00536                     snappedCc[nei[faceI]] != -1
00537                   ? snappedPoints[snappedCc[nei[faceI]]]
00538                   : pTraits<Type>::zero
00539                 ),
00540 
00541                 triPoints,
00542                 triMeshCells
00543             );
00544         }
00545     }
00546 
00547 
00548     // Determine neighbouring snap status
00549     boolList neiSnapped(mesh_.nFaces()-mesh_.nInternalFaces(), false);
00550     List<Type> neiSnappedPoint(neiSnapped.size(), pTraits<Type>::zero);
00551     forAll(patches, patchI)
00552     {
00553         const polyPatch& pp = patches[patchI];
00554 
00555         if (pp.coupled())
00556         {
00557             label faceI = pp.start();
00558             forAll(pp, i)
00559             {
00560                 label bFaceI = faceI-mesh_.nInternalFaces();
00561                 label snappedIndex = snappedCc[own[faceI]];
00562 
00563                 if (snappedIndex != -1)
00564                 {
00565                     neiSnapped[bFaceI] = true;
00566                     neiSnappedPoint[bFaceI] = snappedPoints[snappedIndex];
00567                 }
00568                 faceI++;
00569             }
00570         }
00571     }
00572     syncTools::swapBoundaryFaceList(mesh_, neiSnapped, false);
00573     syncTools::swapBoundaryFaceList(mesh_, neiSnappedPoint, false);
00574 
00575 
00576 
00577     forAll(patches, patchI)
00578     {
00579         const polyPatch& pp = patches[patchI];
00580 
00581         if (isA<processorPolyPatch>(pp))
00582         {
00583             const processorPolyPatch& cpp =
00584                 refCast<const processorPolyPatch>(pp);
00585 
00586             PackedBoolList isCollocated(collocatedFaces(cpp));
00587 
00588             forAll(isCollocated, i)
00589             {
00590                 label faceI = pp.start()+i;
00591 
00592                 if (faceCutType_[faceI] != NOTCUT)
00593                 {
00594                     if (isCollocated[i])
00595                     {
00596                         generateFaceTriPoints
00597                         (
00598                             cVals,
00599                             pVals,
00600 
00601                             cCoords,
00602                             pCoords,
00603 
00604                             snappedPoints,
00605                             snappedCc,
00606                             snappedPoint,
00607                             faceI,
00608 
00609                             cVals.boundaryField()[patchI][i],
00610                             cCoords.boundaryField()[patchI][i],
00611                             neiSnapped[faceI-mesh_.nInternalFaces()],
00612                             neiSnappedPoint[faceI-mesh_.nInternalFaces()],
00613 
00614                             triPoints,
00615                             triMeshCells
00616                         );
00617                     }
00618                     else
00619                     {
00620                         generateFaceTriPoints
00621                         (
00622                             cVals,
00623                             pVals,
00624 
00625                             cCoords,
00626                             pCoords,
00627 
00628                             snappedPoints,
00629                             snappedCc,
00630                             snappedPoint,
00631                             faceI,
00632 
00633                             cVals.boundaryField()[patchI][i],
00634                             cCoords.boundaryField()[patchI][i],
00635                             false,
00636                             pTraits<Type>::zero,
00637 
00638                             triPoints,
00639                             triMeshCells
00640                         );
00641                     }
00642                 }
00643             }
00644         }
00645         else
00646         {
00647             label faceI = pp.start();
00648 
00649             forAll(pp, i)
00650             {
00651                 if (faceCutType_[faceI] != NOTCUT)
00652                 {
00653                     generateFaceTriPoints
00654                     (
00655                         cVals,
00656                         pVals,
00657 
00658                         cCoords,
00659                         pCoords,
00660 
00661                         snappedPoints,
00662                         snappedCc,
00663                         snappedPoint,
00664                         faceI,
00665 
00666                         cVals.boundaryField()[patchI][i],
00667                         cCoords.boundaryField()[patchI][i],
00668                         false,  // fc not snapped
00669                         pTraits<Type>::zero,
00670 
00671                         triPoints,
00672                         triMeshCells
00673                     );
00674                 }
00675                 faceI++;
00676             }
00677         }
00678     }
00679 
00680     triPoints.shrink();
00681     triMeshCells.shrink();
00682 }
00683 
00684 
00685 //template <class Type>
00686 //Foam::tmp<Foam::Field<Type> >
00687 //Foam::isoSurface::sample(const Field<Type>& vField) const
00688 //{
00689 //    return tmp<Field<Type> >(new Field<Type>(vField, meshCells()));
00690 //}
00691 
00692 
00693 template <class Type>
00694 Foam::tmp<Foam::Field<Type> >
00695 Foam::isoSurface::interpolate
00696 (
00697     const GeometricField<Type, fvPatchField, volMesh>& cCoords,
00698     const Field<Type>& pCoords
00699 ) const
00700 {
00701     // Recalculate boundary values
00702     tmp<SlicedGeometricField
00703     <
00704         Type,
00705         fvPatchField,
00706         slicedFvPatchField,
00707         volMesh
00708     > > c2(adaptPatchFields(cCoords));
00709 
00710 
00711     DynamicList<Type> triPoints(nCutCells_);
00712     DynamicList<label> triMeshCells(nCutCells_);
00713 
00714     // Dummy snap data
00715     DynamicList<Type> snappedPoints;
00716     labelList snappedCc(mesh_.nCells(), -1);
00717     labelList snappedPoint(mesh_.nPoints(), -1);
00718 
00719     generateTriPoints
00720     (
00721         cValsPtr_(),
00722         pVals_,
00723 
00724         c2(),
00725         pCoords,
00726 
00727         snappedPoints,
00728         snappedCc,
00729         snappedPoint,
00730 
00731         triPoints,
00732         triMeshCells
00733     );
00734 
00735 
00736     // One value per point
00737     tmp<Field<Type> > tvalues
00738     (
00739         new Field<Type>(points().size(), pTraits<Type>::zero)
00740     );
00741     Field<Type>& values = tvalues();
00742     labelList nValues(values.size(), 0);
00743 
00744     forAll(triPoints, i)
00745     {
00746         label mergedPointI = triPointMergeMap_[i];
00747 
00748         if (mergedPointI >= 0)
00749         {
00750             values[mergedPointI] += triPoints[i];
00751             nValues[mergedPointI]++;
00752         }
00753     }
00754 
00755     if (debug)
00756     {
00757         Pout<< "nValues:" << values.size() << endl;
00758         label nMult = 0;
00759         forAll(nValues, i)
00760         {
00761             if (nValues[i] == 0)
00762             {
00763                 FatalErrorIn("isoSurface::interpolate(..)")
00764                     << "point:" << i << " nValues:" << nValues[i]
00765                     << abort(FatalError);
00766             }
00767             else if (nValues[i] > 1)
00768             {
00769                 nMult++;
00770             }
00771         }
00772         Pout<< "Of which mult:" << nMult << endl;
00773     }
00774 
00775     forAll(values, i)
00776     {
00777         values[i] /= scalar(nValues[i]);
00778     }
00779 
00780     return tvalues;
00781 }
00782 
00783 
00784 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd