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

mapLagrangian.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 "MapLagrangianFields.H"
00028 #include "Cloud.H"
00029 #include "passiveParticle.H"
00030 #include "meshSearch.H"
00031 
00032 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00033 
00034 namespace Foam
00035 {
00036 
00037 static const scalar perturbFactor = 1E-6;
00038 
00039 
00040 // Special version of findCell that generates a cell guaranteed to be
00041 // compatible with tracking.
00042 static label findCell(const meshSearch& meshSearcher, const point& pt)
00043 {
00044     const polyMesh& mesh = meshSearcher.mesh();
00045 
00046     // Use tracking to find cell containing pt
00047     label cellI = meshSearcher.findCell(pt);
00048 
00049     if (cellI >= 0)
00050     {
00051         return cellI;
00052     }
00053     else
00054     {
00055         // See if particle on face by finding nearest face and shifting
00056         // particle.
00057 
00058         label faceI = meshSearcher.findNearestBoundaryFace(pt);
00059 
00060         if (faceI >= 0)
00061         {
00062             const point& cc = mesh.cellCentres()[mesh.faceOwner()[faceI]];
00063             const point perturbPt = (1-perturbFactor)*pt+perturbFactor*cc;
00064 
00065             return meshSearcher.findCell(perturbPt);
00066         }
00067     }
00068     return -1;
00069 }
00070 
00071 
00072 void mapLagrangian(const meshToMesh& meshToMeshInterp)
00073 {
00074     // Determine which particles are in meshTarget
00075     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00076 
00077     // target to source cell map
00078     const labelList& cellAddressing = meshToMeshInterp.cellAddressing();
00079 
00080     // Invert celladdressing to get source to target(s).
00081     // Note: could use sparse addressing but that is too storage inefficient
00082     // (Map<labelList>)
00083     labelListList sourceToTargets
00084     (
00085         invertOneToMany(meshToMeshInterp.fromMesh().nCells(), cellAddressing)
00086     );
00087 
00088     const fvMesh& meshSource = meshToMeshInterp.fromMesh();
00089     const fvMesh& meshTarget = meshToMeshInterp.toMesh();
00090     const pointField& targetCc = meshTarget.cellCentres();
00091 
00092 
00093     fileNameList cloudDirs
00094     (
00095         readDir
00096         (
00097             meshSource.time().timePath()/cloud::prefix,
00098             fileName::DIRECTORY
00099         )
00100     );
00101 
00102     forAll(cloudDirs, cloudI)
00103     {
00104         // Search for list of lagrangian objects for this time
00105         IOobjectList objects
00106         (
00107             meshSource,
00108             meshSource.time().timeName(),
00109             cloud::prefix/cloudDirs[cloudI]
00110         );
00111 
00112         IOobject* positionsPtr = objects.lookup("positions");
00113 
00114         if (positionsPtr)
00115         {
00116             Info<< nl << "    processing cloud " << cloudDirs[cloudI] << endl;
00117 
00118             // Read positions & cell
00119             Cloud<passiveParticle> sourceParcels
00120             (
00121                 meshSource,
00122                 cloudDirs[cloudI],
00123                 false
00124             );
00125             Info<< "    read " << sourceParcels.size()
00126                 << " parcels from source mesh." << endl;
00127 
00128             // Construct empty target cloud
00129             Cloud<passiveParticle> targetParcels
00130             (
00131                 meshTarget,
00132                 cloudDirs[cloudI],
00133                 IDLList<passiveParticle>()
00134             );
00135 
00136             label sourceParticleI = 0;
00137 
00138             // Indices of source particles that get added to targetParcels
00139             DynamicList<label> addParticles(sourceParcels.size());
00140 
00141             // Unmapped particles
00142             labelHashSet unmappedSource(sourceParcels.size());
00143 
00144 
00145             // Initial: track from fine-mesh cell centre to particle position
00146             // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00147             // This requires there to be no boundary in the way.
00148 
00149 
00150             forAllConstIter(Cloud<passiveParticle>, sourceParcels, iter)
00151             {
00152                 bool foundCell = false;
00153 
00154                 // Assume that cell from read parcel is the correct one...
00155                 if (iter().cell() >= 0)
00156                 {
00157                     const labelList& targetCells =
00158                         sourceToTargets[iter().cell()];
00159 
00160                     // Particle probably in one of the targetcells. Try
00161                     // all by tracking from their cell centre to the parcel
00162                     // position.
00163 
00164                     forAll(targetCells, i)
00165                     {
00166                         // Track from its cellcentre to position to make sure.
00167                         autoPtr<passiveParticle> newPtr
00168                         (
00169                             new passiveParticle
00170                             (
00171                                 targetParcels,
00172                                 targetCc[targetCells[i]],
00173                                 targetCells[i]
00174                             )
00175                         );
00176                         passiveParticle& newP = newPtr();
00177 
00178                         scalar fraction = 0;
00179                         label faceI = newP.track(iter().position(), fraction);
00180 
00181                         if (faceI < 0 && newP.cell() >= 0)
00182                         {
00183                             // Hit position.
00184                             foundCell = true;
00185                             addParticles.append(sourceParticleI);
00186                             targetParcels.addParticle(newPtr.ptr());
00187                             break;
00188                         }
00189                     }
00190                 }
00191 
00192                 if (!foundCell)
00193                 {
00194                     // Store for closer analysis
00195                     unmappedSource.insert(sourceParticleI);
00196                 }
00197 
00198                 sourceParticleI++;
00199             }
00200 
00201             Info<< "    after meshToMesh addressing found "
00202                 << targetParcels.size()
00203                 << " parcels in target mesh." << endl;
00204 
00205 
00206             // Do closer inspection for unmapped particles
00207             // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00208 
00209             if (unmappedSource.size())
00210             {
00211                 meshSearch targetSearcher(meshTarget, false);
00212 
00213                 sourceParticleI = 0;
00214 
00215                 forAllIter(Cloud<passiveParticle>, sourceParcels, iter)
00216                 {
00217                     if (unmappedSource.found(sourceParticleI))
00218                     {
00219                         label targetCell =
00220                             findCell(targetSearcher, iter().position());
00221 
00222                         if (targetCell >= 0)
00223                         {
00224                             unmappedSource.erase(sourceParticleI);
00225                             addParticles.append(sourceParticleI);
00226                 iter().cell()=targetCell;
00227                             targetParcels.addParticle
00228                             (
00229                                 sourceParcels.remove(&iter())
00230                             );
00231                         }
00232                     }
00233                     sourceParticleI++;
00234                 }
00235             }
00236             addParticles.shrink();
00237 
00238             Info<< "    after additional mesh searching found "
00239                 << targetParcels.size() << " parcels in target mesh." << endl;
00240 
00241             if (addParticles.size())
00242             {
00243                 IOPosition<passiveParticle>(targetParcels).write();
00244 
00245                 // addParticles now contains the indices of the sourceMesh
00246                 // particles that were appended to the target mesh.
00247 
00248                 // Map lagrangian fields
00249                 // ~~~~~~~~~~~~~~~~~~~~~
00250 
00251                 MapLagrangianFields<label>
00252                 (cloudDirs[cloudI], objects, meshToMeshInterp, addParticles);
00253                 MapLagrangianFields<scalar>
00254                 (cloudDirs[cloudI], objects, meshToMeshInterp, addParticles);
00255                 MapLagrangianFields<vector>
00256                 (cloudDirs[cloudI], objects, meshToMeshInterp, addParticles);
00257                 MapLagrangianFields<sphericalTensor>
00258                 (cloudDirs[cloudI], objects, meshToMeshInterp, addParticles);
00259                 MapLagrangianFields<symmTensor>
00260                 (cloudDirs[cloudI], objects, meshToMeshInterp, addParticles);
00261                 MapLagrangianFields<tensor>
00262                 (cloudDirs[cloudI], objects, meshToMeshInterp, addParticles);
00263             }
00264         }
00265     }
00266 }
00267 
00268 
00269 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00270 
00271 } // End namespace Foam
00272 
00273 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd