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

particleTracks.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) 2008-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     particleTracks
00027 
00028 Description
00029     Generates a VTK file of particle tracks for cases that were computed using
00030     a tracked-parcel-type cloud
00031 
00032 \*---------------------------------------------------------------------------*/
00033 
00034 #include "argList.H"
00035 #include "Cloud.H"
00036 #include "IOdictionary.H"
00037 #include "fvMesh.H"
00038 #include "Time.H"
00039 #include "timeSelector.H"
00040 #include "OFstream.H"
00041 #include "passiveParticleCloud.H"
00042 
00043 using namespace Foam;
00044 
00045 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00046 
00047 int main(int argc, char *argv[])
00048 {
00049     timeSelector::addOptions();
00050     #include "addRegionOption.H"
00051 
00052     #include "setRootCase.H"
00053 
00054     #include "createTime.H"
00055     instantList timeDirs = timeSelector::select0(runTime, args);
00056     #include "createNamedMesh.H"
00057     #include "createFields.H"
00058 
00059     // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00060 
00061     fileName vtkPath(runTime.path()/"VTK");
00062     mkDir(vtkPath);
00063 
00064     Info<< "Scanning times to determine track data for cloud " << cloudName
00065         << nl << endl;
00066 
00067     labelList maxIds(Pstream::nProcs(), -1);
00068     forAll(timeDirs, timeI)
00069     {
00070         runTime.setTime(timeDirs[timeI], timeI);
00071         Info<< "Time = " << runTime.timeName() << endl;
00072 
00073         Info<< "    Reading particle positions" << endl;
00074         passiveParticleCloud myCloud(mesh, cloudName);
00075         Info<< "    Read " << returnReduce(myCloud.size(), sumOp<label>())
00076             << " particles" << endl;
00077 
00078         forAllConstIter(passiveParticleCloud, myCloud, iter)
00079         {
00080             label origId = iter().origId();
00081             label origProc = iter().origProc();
00082 
00083             maxIds[origProc] = max(maxIds[origProc], origId);
00084         }
00085     }
00086     Pstream::listCombineGather(maxIds, maxEqOp<label>());
00087     Pstream::listCombineScatter(maxIds);
00088 
00089     labelList numIds = maxIds + 1;
00090 
00091     Info<< nl << "Particle statistics:" << endl;
00092     forAll(maxIds, procI)
00093     {
00094         Info<< "    Found " << numIds[procI] << " particles originating"
00095             << " from processor " << procI << endl;
00096     }
00097     Info<< nl << endl;
00098 
00099 
00100     // calc starting ids for particles on each processor
00101     List<label> startIds(numIds.size(), 0);
00102     for (label i = 0; i < numIds.size()-1; i++)
00103     {
00104         startIds[i+1] += startIds[i] + numIds[i];
00105     }
00106     label nParticle = startIds[startIds.size()-1] + numIds[startIds.size()-1];
00107 
00108 
00109 
00110     // number of tracks to generate
00111     label nTracks = nParticle/sampleFrequency;
00112 
00113     // storage for all particle tracks
00114     List<DynamicList<vector> > allTracks(nTracks);
00115 
00116     Info<< "\nGenerating " << nTracks << " particle tracks for cloud "
00117         << cloudName << nl << endl;
00118 
00119     forAll(timeDirs, timeI)
00120     {
00121         runTime.setTime(timeDirs[timeI], timeI);
00122         Info<< "Time = " << runTime.timeName() << endl;
00123 
00124         List<pointField> allPositions(Pstream::nProcs());
00125         List<labelField> allOrigIds(Pstream::nProcs());
00126         List<labelField> allOrigProcs(Pstream::nProcs());
00127 
00128         // Read particles. Will be size 0 if no particles.
00129         Info<< "    Reading particle positions" << endl;
00130         passiveParticleCloud myCloud(mesh, cloudName);
00131 
00132         // collect the track data on all processors that have positions
00133         allPositions[Pstream::myProcNo()].setSize
00134         (
00135             myCloud.size(),
00136             point::zero
00137         );
00138         allOrigIds[Pstream::myProcNo()].setSize(myCloud.size(), 0);
00139         allOrigProcs[Pstream::myProcNo()].setSize(myCloud.size(), 0);
00140 
00141         label i = 0;
00142         forAllConstIter(passiveParticleCloud, myCloud, iter)
00143         {
00144             allPositions[Pstream::myProcNo()][i] = iter().position();
00145             allOrigIds[Pstream::myProcNo()][i] = iter().origId();
00146             allOrigProcs[Pstream::myProcNo()][i] = iter().origProc();
00147             i++;
00148         }
00149 
00150         // collect the track data on the master processor
00151         Pstream::gatherList(allPositions);
00152         Pstream::gatherList(allOrigIds);
00153         Pstream::gatherList(allOrigProcs);
00154 
00155         Info<< "    Constructing tracks" << nl << endl;
00156         if (Pstream::master())
00157         {
00158             forAll(allPositions, procI)
00159             {
00160                 forAll(allPositions[procI], i)
00161                 {
00162                     label globalId =
00163                         startIds[allOrigProcs[procI][i]]
00164                       + allOrigIds[procI][i];
00165 
00166                     if (globalId % sampleFrequency == 0)
00167                     {
00168                         label trackId = globalId/sampleFrequency;
00169                         if (allTracks[trackId].size() < maxPositions)
00170                         {
00171                             allTracks[trackId].append
00172                             (
00173                                 allPositions[procI][i]
00174                             );
00175                         }
00176                     }
00177                 }
00178             }
00179         }
00180     }
00181 
00182     if (Pstream::master())
00183     {
00184         OFstream vtkTracks(vtkPath/"particleTracks.vtk");
00185 
00186         Info<< "\nWriting particle tracks to " << vtkTracks.name()
00187             << nl << endl;
00188 
00189         // Total number of points in tracks + 1 per track
00190         label nPoints = 0;
00191         forAll(allTracks, trackI)
00192         {
00193             nPoints += allTracks[trackI].size();
00194         }
00195 
00196         vtkTracks
00197             << "# vtk DataFile Version 2.0" << nl
00198             << "particleTracks" << nl
00199             << "ASCII" << nl
00200             << "DATASET POLYDATA" << nl
00201             << "POINTS " << nPoints << " float" << nl;
00202 
00203         // Write track points to file
00204         forAll(allTracks, trackI)
00205         {
00206             forAll(allTracks[trackI], i)
00207             {
00208                 const vector& pt = allTracks[trackI][i];
00209                 vtkTracks << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
00210             }
00211         }
00212 
00213         // write track (line) connectivity to file
00214         vtkTracks << "LINES " << nTracks << ' ' << nPoints+nTracks << nl;
00215 
00216         // Write ids of track points to file
00217         label globalPtI = 0;
00218         forAll(allTracks, trackI)
00219         {
00220             vtkTracks << allTracks[trackI].size();
00221 
00222             forAll(allTracks[trackI], i)
00223             {
00224                 vtkTracks << ' ' << globalPtI;
00225                 globalPtI++;
00226             }
00227 
00228             vtkTracks << nl;
00229         }
00230 
00231         Info<< "end" << endl;
00232     }
00233 
00234     return 0;
00235 }
00236 
00237 
00238 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd