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

vtkFoam.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 "vtkFoam.H"
00028 
00029 #include "argList.H"
00030 #include "Time.H"
00031 #include "polyBoundaryMeshEntries.H"
00032 #include "IOobjectList.H"
00033 #include "wordList.H"
00034 #include "fvMesh.H"
00035 #include "volFields.H"
00036 #include "pointMesh.H"
00037 #include "volPointInterpolation.H"
00038 
00039 #include "vtkFoamReader.h"
00040 #include "vtkDataArraySelection.h"
00041 #include "vtkUnstructuredGrid.h"
00042 #include "vtkPointData.h"
00043 #include "vtkCellData.h"
00044 #include "vtkFloatArray.h"
00045 #include "vtkCharArray.h"
00046 
00047 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00048 
00049 defineTypeNameAndDebug(Foam::vtkFoam, 0);
00050 
00051 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00052 
00053 #include "vtkFoamConvertFields.H"
00054 
00055 void Foam::vtkFoam::SetName
00056 (
00057     vtkUnstructuredGrid* vtkMesh,
00058     const char* name
00059 )
00060 {
00061     vtkCharArray* nmArray =  vtkCharArray::New();
00062     nmArray->SetName("Name");
00063     size_t len = strlen(name);
00064     nmArray->SetNumberOfTuples(static_cast<vtkIdType>(len)+1);
00065     char* copy = nmArray->GetPointer(0);
00066     memcpy(copy, name, len);
00067     copy[len] = '\0';
00068     vtkMesh->GetFieldData()->AddArray(nmArray);
00069     nmArray->Delete();
00070 }
00071 
00072 
00073 Foam::string Foam::vtkFoam::padTimeString(const string& ts)
00074 {
00075     return ts + string("            ", max(label(12 - ts.size()), 0));
00076 }
00077 
00078 
00079 // Pad the patch name string in order to account for dynamic changes
00080 // in patch names during topological changes
00081 Foam::string Foam::vtkFoam::padPatchString(const string& ps)
00082 {
00083     label n = max(label(50 - ps.size()), 0);
00084     return ps + string("                                                  ", n);
00085 }
00086 
00087 
00088 void Foam::vtkFoam::setSelectedTime
00089 (
00090     Time& runTime,
00091     vtkFoamReader* reader
00092 )
00093 {
00094     // Get times list
00095     instantList Times = runTime.times();
00096     int timeIndex = min(max(reader->GetTimeStep() + 1, 0), Times.size()-1);
00097 
00098     // If this is the first call timeIndex will be 0 ("constant")
00099     // so reset to the first time step if one exists and deselect every
00100     // element of the selection array
00101     if (timeIndex == 0)
00102     {
00103         timeIndex = min(1, Times.size()-1);
00104         reader->GetTimeSelection()->DisableAllArrays();
00105     }
00106 
00107     label selectedTimeIndex = -1;
00108     label nSelectedTimes = reader->GetTimeSelection()->GetNumberOfArrays();
00109 
00110     for (label i=nSelectedTimes-1; i>=0; i--)
00111     {
00112         if(reader->GetTimeSelection()->GetArraySetting(i))
00113         {
00114             word timeName = string::validate<word>
00115             (
00116                 reader->GetTimeSelection()->GetArrayName(i)
00117             );
00118 
00119             forAll(Times, j)
00120             {
00121                 if (Times[j].name() == timeName)
00122                 {
00123                     selectedTimeIndex = j;
00124                     break;
00125                 }
00126             }
00127             break;
00128         }
00129     }
00130 
00131     if (selectedTimeIndex != -1)
00132     {
00133         timeIndex = min(selectedTimeIndex, Times.size()-1);
00134     }
00135 
00136     if (debug)
00137     {
00138         Info<< "Selecting time " << Times[timeIndex].name() << endl;
00139     }
00140 
00141     runTime.setTime(Times[timeIndex], timeIndex);
00142 
00143     Times = runTime.times();
00144 
00145     reader->SetTimeStepRange(0, max(Times.size()-2, 0));
00146 
00147     // reset the time steps ...
00148     reader->GetTimeSelection()->RemoveAllArrays();
00149 
00150     int* TimeStepLimits = reader->GetTimeStepLimits();
00151     label maxStartTimes = min(Times.size(), TimeStepLimits[0]);
00152     label maxNTimes = min(Times.size() - maxStartTimes, TimeStepLimits[1]);
00153 
00154     for (label i=0; i<maxStartTimes; i++)
00155     {
00156         reader->GetTimeSelection()
00157             ->AddArray(padTimeString(Times[i].name()).c_str());
00158     }
00159 
00160     if (Times.size() > TimeStepLimits[0] + TimeStepLimits[1])
00161     {
00162         reader->GetTimeSelection()->AddArray(padTimeString("...").c_str());
00163     }
00164 
00165     for (label i=Times.size() - maxNTimes; i<Times.size(); i++)
00166     {
00167         reader->GetTimeSelection()
00168             ->AddArray(padTimeString(Times[i].name()).c_str());
00169     }
00170 
00171     // Disable all the time selections (which are all selected by default) ...
00172     reader->GetTimeSelection()->DisableAllArrays();
00173 
00174     // But maintain the selections made previously
00175     if (selectedTimeIndex != -1 && selectedTimeIndex < Times.size())
00176     {
00177         reader->GetTimeSelection()->EnableArray
00178             (padTimeString(Times[selectedTimeIndex].name()).c_str());
00179     }
00180 }
00181 
00182 
00183 void Foam::vtkFoam::updateSelectedRegions()
00184 {
00185     if (debug)
00186     {
00187         Info<< "Foam::vtkFoam::updateSelectedRegions()" << endl;
00188     }
00189 
00190     label nRegions = reader_->GetRegionSelection()->GetNumberOfArrays();
00191 
00192     selectedRegions_.setSize(nRegions);
00193 
00194     // Read the selected patches and add to the region list
00195     for (int i=0; i<nRegions; i++)
00196     {
00197         selectedRegions_[i] =
00198             reader_->GetRegionSelection()->GetArraySetting(i);
00199     }
00200 }
00201 
00202 
00203 void Foam::vtkFoam::convertMesh()
00204 {
00205     if (debug)
00206     {
00207         Info<< "Foam::vtkFoam::convertMesh()" << endl;
00208     }
00209 
00210     const fvMesh& mesh = *meshPtr_;
00211 
00212     // Read the internal mesh as region 0 if selected
00213     if (reader_->GetRegionSelection()->GetArraySetting(0))
00214     {
00215         selectedRegions_[0] = true;
00216         addInternalMesh
00217         (
00218             mesh,
00219             vtkUnstructuredGrid::SafeDownCast(reader_->GetOutput(0))
00220         );
00221     }
00222     else
00223     {
00224         selectedRegions_[0] = false;
00225 
00226         vtkUnstructuredGrid *vtkMesh =
00227             vtkUnstructuredGrid::SafeDownCast(reader_->GetOutput(0));
00228 
00229         vtkMesh->Initialize();
00230         SetName(vtkMesh, "(Internal Mesh)");
00231     }
00232 
00233 
00234     // Read the selected patches and add to the region list
00235 
00236     polyBoundaryMeshEntries patchEntries
00237     (
00238         IOobject
00239         (
00240             "boundary",
00241             dbPtr_().findInstance(polyMesh::meshSubDir, "boundary"),
00242             polyMesh::meshSubDir,
00243             dbPtr_(),
00244             IOobject::MUST_READ,
00245             IOobject::NO_WRITE,
00246             false
00247         )
00248     );
00249 
00250     label regioni = 0;
00251     label regioniLast = 0;
00252 
00253     // Read in the number Outputs (patch regions) currently being used
00254     label currNOutputs = reader_->GetNumberOfOutputs();
00255 
00256     // Cycle through all the patches in the boundary file for the relevant
00257     // time step
00258     forAll(patchEntries, entryi)
00259     {
00260         // Number of faces in the current patch (Used to detect dummy patches
00261         // of size zero)
00262         label nFaces(readLabel(patchEntries[entryi].dict().lookup("nFaces")));
00263 
00264         // Check to see if the patch is currently a part of the displayed list
00265         if
00266         (
00267             reader_->GetRegionSelection()->ArrayExists
00268             (
00269                 padPatchString(patchEntries[entryi].keyword()).c_str()
00270             )
00271         )
00272         {
00273             if  (!nFaces)
00274             {
00275                 // Remove patch if it is only a dummy patch in the current
00276                 // time step with zero faces
00277                 reader_->GetRegionSelection()->RemoveArrayByName
00278                 (
00279                     padPatchString(patchEntries[entryi].keyword()).c_str()
00280                 );
00281             }
00282             else
00283             {
00284                 // A patch already existent in the list and which
00285                 // continues to exist found
00286                 regioni++;
00287             }
00288         }
00289         else
00290         {
00291             // A new patch so far not yet included into the list has been found
00292             if  (nFaces)
00293             {
00294                 regioni++;
00295 
00296                 // Add a new entry to the list of regions
00297                 reader_->GetRegionSelection()->AddArray
00298                 (
00299                     padPatchString(patchEntries[entryi].keyword()).c_str()
00300                 );
00301 
00302                 // AddArray automatically enables a new array... disable
00303                 // it manually
00304                 reader_->GetRegionSelection()->DisableArray
00305                 (
00306                     padPatchString(patchEntries[entryi].keyword()).c_str()
00307                 );
00308             }
00309         }
00310 
00311         // Avoid Initialization of the same Output twice
00312         if (regioni != regioniLast)
00313         {
00314             // Only setup an Output if it has not been setup before
00315             if(regioni >= currNOutputs)
00316             {
00317                 vtkUnstructuredGrid* ugrid = vtkUnstructuredGrid::New();
00318                 reader_->SetNthOutput(regioni,ugrid);
00319                 ugrid->Delete();
00320             }
00321             // Initialize -> Delete memory used, and reset to zero state
00322             reader_->GetOutput(regioni)->Initialize();
00323             regioniLast = regioni;
00324         }
00325     }
00326 
00327     // Initialize (reset to zero and free) any outputs which are not used
00328     // anymore
00329     if (regioni < currNOutputs)
00330     {
00331         for(label i = (regioni+1); i < currNOutputs;i++)
00332         {
00333             reader_->GetOutput(i)->Initialize();
00334         }
00335     }
00336 
00337     selectedRegions_.setSize(regioni + 1);
00338 
00339     regioni = 0;
00340 
00341     const polyBoundaryMesh& patches = mesh.boundaryMesh();
00342 
00343     forAll (patches, patchi)
00344     {
00345         if (patches[patchi].size())
00346         {
00347             regioni++;
00348 
00349             if (reader_->GetRegionSelection()->GetArraySetting(regioni))
00350             {
00351                 selectedRegions_[regioni] = true;
00352                 addPatch
00353                 (
00354                     patches[patchi],
00355                     vtkUnstructuredGrid::SafeDownCast
00356                     (
00357                         reader_->GetOutput(regioni)
00358                     )
00359                 );
00360             }
00361             else
00362             {
00363                 selectedRegions_[regioni] = false;
00364 
00365                 vtkUnstructuredGrid *vtkMesh =
00366                     vtkUnstructuredGrid::SafeDownCast
00367                     (
00368                         reader_->GetOutput(regioni)
00369                     );
00370 
00371                 vtkMesh->Initialize();
00372                 SetName
00373                 (
00374                     vtkMesh,
00375                     ('(' + padPatchString(patches[patchi].name()) + ')').c_str()
00376                 );
00377             }
00378         }
00379     }
00380 }
00381 
00382 
00383 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00384 
00385 Foam::vtkFoam::vtkFoam(const char* const FileName, vtkFoamReader* reader)
00386 :
00387     reader_(reader),
00388     argsPtr_(NULL),
00389     dbPtr_(NULL),
00390     meshPtr_(NULL)
00391 {
00392     fileName fullCasePath(fileName(FileName).path());
00393 
00394     if (!isDir(fullCasePath))
00395     {
00396         return;
00397     }
00398 
00399     char* argvStrings[3];
00400     argvStrings[0] = new char[9];
00401     strcpy(argvStrings[0], "/vtkFoam");
00402     argvStrings[1] = new char[6];
00403     strcpy(argvStrings[1], "-case");
00404     argvStrings[2] = new char[fullCasePath.size()+1];
00405     strcpy(argvStrings[2], fullCasePath.c_str());
00406 
00407     int argc = 3;
00408     char** argv = &argvStrings[0];
00409     argsPtr_.reset(new argList(argc, argv));
00410 
00411     for(int i = 0; i < argc; i++)
00412     {
00413         delete[] argvStrings[i];
00414     }
00415 
00416     dbPtr_.reset
00417     (
00418         new Time
00419         (
00420             Time::controlDictName,
00421             argsPtr_().rootPath(),
00422             argsPtr_().caseName()
00423         )
00424     );
00425     dbPtr_().functionObjects().off();
00426     setSelectedTime(dbPtr_(), reader_);
00427 
00428     if (debug)
00429     {
00430         Info<< "vtkFoam::ExecuteInformation: Initialising outputs" << endl;
00431     }
00432 
00433     reader_->GetRegionSelection()->AddArray("Internal Mesh");
00434 
00435     vtkUnstructuredGrid* ugrid = vtkUnstructuredGrid::New();
00436     reader_->SetNthOutput(0, ugrid);
00437     ugrid->Delete();
00438     reader_->GetOutput(0)->Initialize();
00439 
00440     polyBoundaryMeshEntries patchEntries
00441     (
00442         IOobject
00443         (
00444             "boundary",
00445             dbPtr_().findInstance(polyMesh::meshSubDir, "boundary"),
00446             polyMesh::meshSubDir,
00447             dbPtr_(),
00448             IOobject::MUST_READ,
00449             IOobject::NO_WRITE,
00450             false
00451         )
00452     );
00453 
00454     label regioni = 0;
00455     forAll(patchEntries, entryi)
00456     {
00457         label nFaces(readLabel(patchEntries[entryi].dict().lookup("nFaces")));
00458 
00459         if (nFaces)
00460         {
00461             regioni++;
00462 
00463             reader_->GetRegionSelection()->AddArray
00464             (
00465                 padPatchString(patchEntries[entryi].keyword()).c_str()
00466             );
00467 
00468             vtkUnstructuredGrid* ugrid = vtkUnstructuredGrid::New();
00469             reader_->SetNthOutput(regioni, ugrid);
00470             ugrid->Delete();
00471             reader_->GetOutput(regioni)->Initialize();
00472         }
00473     }
00474 
00475     selectedRegions_.setSize(regioni + 1);
00476     selectedRegions_ = true;
00477 
00478     UpdateInformation();
00479 }
00480 
00481 
00482 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00483 
00484 Foam::vtkFoam::~vtkFoam()
00485 {
00486     // Do NOT delete meshPtr_ since still referenced somehow.
00487 }
00488 
00489 
00490 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00491 
00492 #include "vtkFoamAddFields.H"
00493 
00494 void Foam::vtkFoam::UpdateInformation()
00495 {
00496     if (debug)
00497     {
00498         Info<< "TimeStep = " << reader_->GetTimeStep() << endl;
00499     }
00500 
00501     setSelectedTime(dbPtr_(), reader_);
00502 
00503     // Search for list of objects for this time
00504     IOobjectList objects(dbPtr_(), dbPtr_().timeName());
00505 
00506     addFields<volScalarField>(reader_->GetVolFieldSelection(), objects);
00507     addFields<volVectorField>(reader_->GetVolFieldSelection(), objects);
00508     addFields<volSphericalTensorField>(reader_->GetVolFieldSelection(), objects);
00509     addFields<volSymmTensorField>(reader_->GetVolFieldSelection(), objects);
00510     addFields<volTensorField>(reader_->GetVolFieldSelection(), objects);
00511 
00512     addFields<pointScalarField>(reader_->GetPointFieldSelection(), objects);
00513     addFields<pointVectorField>(reader_->GetPointFieldSelection(), objects);
00514     addFields<pointSphericalTensorField>(reader_->GetPointFieldSelection(), objects);
00515     addFields<pointSymmTensorField>(reader_->GetPointFieldSelection(), objects);
00516     addFields<pointTensorField>(reader_->GetPointFieldSelection(), objects);
00517 }
00518 
00519 
00520 void Foam::vtkFoam::Update()
00521 {
00522     if
00523     (
00524         !reader_->GetCacheMesh()
00525      || reader_->GetTimeSelection()->GetArraySetting(0)
00526     )
00527     {
00528         meshPtr_= NULL;
00529     }
00530 
00531     // Clear the current set of selected fields
00532 
00533     for (label i=0; i<reader_->GetNumberOfOutputs(); i++)
00534     {
00535         vtkUnstructuredGrid *vtkMesh =
00536             vtkUnstructuredGrid::SafeDownCast(reader_->GetOutput(i));
00537 
00538         vtkCellData* cellData = vtkMesh->GetCellData();
00539         int numberOfCellArrays = cellData->GetNumberOfArrays();
00540 
00541         wordList cellFieldNames(numberOfCellArrays);
00542         for (int j=0; j<numberOfCellArrays; j++)
00543         {
00544             cellFieldNames[j] = cellData->GetArrayName(j);
00545         }
00546 
00547         for (int j=0; j<numberOfCellArrays; j++)
00548         {
00549             cellData->RemoveArray(cellFieldNames[j].c_str());
00550         }
00551 
00552         vtkPointData* pointData = vtkMesh->GetPointData();
00553         int numberOfPointArrays = pointData->GetNumberOfArrays();
00554 
00555         wordList pointFieldNames(numberOfPointArrays);
00556         for (int j=0; j<numberOfPointArrays; j++)
00557         {
00558             pointFieldNames[j] = pointData->GetArrayName(j);
00559         }
00560 
00561         for (int j=0; j<numberOfPointArrays; j++)
00562         {
00563             pointData->RemoveArray(pointFieldNames[j].c_str());
00564         }
00565     }
00566 
00567     // Check to see if the mesh has been created
00568 
00569     if (!meshPtr_)
00570     {
00571         if (debug)
00572         {
00573             Info<< "Reading Mesh" << endl;
00574         }
00575         meshPtr_ =
00576             new fvMesh
00577             (
00578                 IOobject
00579                 (
00580                     fvMesh::defaultRegion,
00581                     dbPtr_().timeName(),
00582                     dbPtr_()
00583                 )
00584             );
00585         convertMesh();
00586     }
00587     else
00588     {
00589         boolList oldSelectedRegions = selectedRegions_;
00590         updateSelectedRegions();
00591         if
00592         (
00593             meshPtr_->readUpdate() != fvMesh::UNCHANGED
00594          || oldSelectedRegions != selectedRegions_
00595         )
00596         {
00597             convertMesh();
00598         }
00599     }
00600 
00601     if (debug)
00602     {
00603         Info<< "converting fields" << endl;
00604     }
00605 
00606     const fvMesh& mesh = *meshPtr_;
00607 
00608     // Construct interpolation on the raw mesh
00609     Foam::pointMesh pMesh(mesh);
00610 
00611     Foam::volPointInterpolation pInterp(mesh, pMesh);
00612 
00613     // Search for list of objects for this time
00614     Foam::IOobjectList objects(mesh, dbPtr_().timeName());
00615 
00616     convertVolFields<Foam::scalar>
00617     (
00618         mesh, pInterp, objects, reader_->GetVolFieldSelection()
00619     );
00620     convertVolFields<Foam::vector>
00621     (
00622         mesh, pInterp, objects, reader_->GetVolFieldSelection()
00623     );
00624     convertVolFields<Foam::sphericalTensor>
00625     (
00626         mesh, pInterp, objects, reader_->GetVolFieldSelection()
00627     );
00628     convertVolFields<Foam::symmTensor>
00629     (
00630         mesh, pInterp, objects, reader_->GetVolFieldSelection()
00631     );
00632     convertVolFields<Foam::tensor>
00633     (
00634         mesh, pInterp, objects, reader_->GetVolFieldSelection()
00635     );
00636 
00637     convertPointFields<Foam::scalar>
00638     (
00639         mesh, objects, reader_->GetPointFieldSelection()
00640     );
00641     convertPointFields<Foam::vector>
00642     (
00643         mesh, objects, reader_->GetPointFieldSelection()
00644     );
00645     convertPointFields<Foam::sphericalTensor>
00646     (
00647         mesh, objects, reader_->GetPointFieldSelection()
00648     );
00649     convertPointFields<Foam::symmTensor>
00650     (
00651         mesh, objects, reader_->GetPointFieldSelection()
00652     );
00653     convertPointFields<Foam::tensor>
00654     (
00655         mesh, objects, reader_->GetPointFieldSelection()
00656     );
00657 
00658     if (debug)
00659     {
00660         Info<< "done" << endl;
00661     }
00662 }
00663 
00664 
00665 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd