Initial commit for version 2.0.x patch release
[OpenFOAM-2.0.x.git] / applications / utilities / postProcessing / graphics / PV3Readers / PV3FoamReader / vtkPV3Foam / vtkPV3FoamUpdateInfo.C
blobf8f34a01e22025c4b7b8c728ff8f1e61e3a09733
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2004-2010 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by
13     the Free Software Foundation, either version 3 of the License, or
14     (at your option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "vtkPV3Foam.H"
28 // OpenFOAM includes
29 #include "cellSet.H"
30 #include "faceSet.H"
31 #include "pointSet.H"
32 #include "IOobjectList.H"
33 #include "IOPtrList.H"
34 #include "polyBoundaryMeshEntries.H"
35 #include "entry.H"
36 #include "Cloud.H"
37 #include "vtkPV3FoamReader.h"
39 // local headers
40 #include "vtkPV3FoamAddToSelection.H"
41 #include "vtkPV3FoamUpdateInfoFields.H"
43 // VTK includes
44 #include "vtkDataArraySelection.h"
47 // * * * * * * * * * * * * * * * Private Classes * * * * * * * * * * * * * * //
49 namespace Foam
52 //- A class for reading zone information without requiring a mesh
53 class zonesEntries
55     public regIOobject,
56     public PtrList<entry>
59 public:
61     // Constructors
63         explicit zonesEntries(const IOobject& io)
64         :
65             regIOobject(io),
66             PtrList<entry>(readStream("regIOobject"))
67         {
68             close();
69         }
71    // Member functions
73         bool writeData(Ostream&) const
74         {
75             notImplemented("zonesEntries::writeData(Ostream&) const");
76             return false;
77         }
82 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
84 template<class ZoneType>
85 Foam::wordList Foam::vtkPV3Foam::getZoneNames
87     const ZoneMesh<ZoneType, polyMesh>& zmesh
88 ) const
90     wordList names(zmesh.size());
91     label nZone = 0;
93     forAll(zmesh, zoneI)
94     {
95         if (zmesh[zoneI].size())
96         {
97             names[nZone++] = zmesh[zoneI].name();
98         }
99     }
100     names.setSize(nZone);
102     return names;
106 Foam::wordList Foam::vtkPV3Foam::getZoneNames(const word& zoneType) const
108     wordList names;
110     // mesh not loaded - read from file
111     IOobject ioObj
112     (
113         zoneType,
114         dbPtr_().findInstance
115         (
116             meshDir_,
117             zoneType,
118             IOobject::READ_IF_PRESENT
119         ),
120         meshDir_,
121         dbPtr_(),
122         IOobject::READ_IF_PRESENT,
123         IOobject::NO_WRITE,
124         false
125     );
127     if (ioObj.headerOk())
128     {
129         zonesEntries zones(ioObj);
131         names.setSize(zones.size());
132         forAll(zones, zoneI)
133         {
134             names[zoneI] = zones[zoneI].keyword();
135         }
136     }
138     return names;
142 void Foam::vtkPV3Foam::updateInfoInternalMesh
144     vtkDataArraySelection* arraySelection
147     if (debug)
148     {
149         Info<< "<beg> Foam::vtkPV3Foam::updateInfoInternalMesh" << endl;
150     }
152     // Determine mesh parts (internalMesh, patches...)
153     //- Add internal mesh as first entry
154     arrayRangeVolume_.reset(arraySelection->GetNumberOfArrays());
155     arraySelection->AddArray
156     (
157         "internalMesh"
158     );
159     arrayRangeVolume_ += 1;
161     if (debug)
162     {
163         // just for debug info
164         getSelectedArrayEntries(arraySelection);
166         Info<< "<end> Foam::vtkPV3Foam::updateInfoInternalMesh" << endl;
167     }
171 void Foam::vtkPV3Foam::updateInfoLagrangian
173     vtkDataArraySelection* arraySelection
176     if (debug)
177     {
178         Info<< "<beg> Foam::vtkPV3Foam::updateInfoLagrangian" << nl
179             << "    " << dbPtr_->timePath()/cloud::prefix << endl;
180     }
183     // use the db directly since this might be called without a mesh,
184     // but the region must get added back in
185     fileName lagrangianPrefix(cloud::prefix);
186     if (meshRegion_ != polyMesh::defaultRegion)
187     {
188         lagrangianPrefix = meshRegion_/cloud::prefix;
189     }
191     // Search for list of lagrangian objects for this time
192     fileNameList cloudDirs
193     (
194         readDir(dbPtr_->timePath()/lagrangianPrefix, fileName::DIRECTORY)
195     );
197     arrayRangeLagrangian_.reset(arraySelection->GetNumberOfArrays());
199     int nClouds = 0;
200     forAll(cloudDirs, cloudI)
201     {
202         // Add cloud to GUI list
203         arraySelection->AddArray
204         (
205             (cloudDirs[cloudI] + " - lagrangian").c_str()
206         );
208         ++nClouds;
209     }
210     arrayRangeLagrangian_ += nClouds;
212     if (debug)
213     {
214         // just for debug info
215         getSelectedArrayEntries(arraySelection);
217         Info<< "<end> Foam::vtkPV3Foam::updateInfoLagrangian" << endl;
218     }
222 void Foam::vtkPV3Foam::updateInfoPatches
224     vtkDataArraySelection* arraySelection
227     if (debug)
228     {
229         Info<< "<beg> Foam::vtkPV3Foam::updateInfoPatches"
230             << " [meshPtr=" << (meshPtr_ ? "set" : "NULL") << "]" << endl;
231     }
233     arrayRangePatches_.reset(arraySelection->GetNumberOfArrays());
235     int nPatches = 0;
236     if (meshPtr_)
237     {
238         const polyBoundaryMesh& patches = meshPtr_->boundaryMesh();
239         forAll(patches, patchI)
240         {
241             const polyPatch& pp = patches[patchI];
243             if (pp.size())
244             {
245                 // Add patch to GUI list
246                 arraySelection->AddArray
247                 (
248                     (pp.name() + " - patch").c_str()
249                 );
251                 ++nPatches;
252             }
253         }
254     }
255     else
256     {
257         // mesh not loaded - read from file
258         // but this could fail if we've supplied a bad region name
259         IOobject ioObj
260         (
261             "boundary",
262             dbPtr_().findInstance
263             (
264                 meshDir_,
265                 "boundary",
266                 IOobject::READ_IF_PRESENT
267             ),
268             meshDir_,
269             dbPtr_(),
270             IOobject::READ_IF_PRESENT,
271             IOobject::NO_WRITE,
272             false
273         );
275         // this should only ever fail if the mesh region doesn't exist
276         if (ioObj.headerOk())
277         {
278             polyBoundaryMeshEntries patchEntries(ioObj);
280             // Add (non-zero) patches to the list of mesh parts
281             forAll(patchEntries, entryI)
282             {
283                 label nFaces
284                 (
285                     readLabel(patchEntries[entryI].dict().lookup("nFaces"))
286                 );
288                 // Valid patch if nFace > 0 - add patch to GUI list
289                 if (nFaces)
290                 {
291                     arraySelection->AddArray
292                     (
293                         (patchEntries[entryI].keyword() + " - patch").c_str()
294                     );
296                     ++nPatches;
297                 }
298             }
299         }
300     }
301     arrayRangePatches_ += nPatches;
303     if (debug)
304     {
305         // just for debug info
306         getSelectedArrayEntries(arraySelection);
308         Info<< "<end> Foam::vtkPV3Foam::updateInfoPatches" << endl;
309     }
313 void Foam::vtkPV3Foam::updateInfoZones
315     vtkDataArraySelection* arraySelection
318     if (!reader_->GetIncludeZones())
319     {
320         return;
321     }
323     if (debug)
324     {
325         Info<< "<beg> Foam::vtkPV3Foam::updateInfoZones"
326             << " [meshPtr=" << (meshPtr_ ? "set" : "NULL") << "]" << endl;
327     }
329     wordList namesLst;
331     //
332     // cellZones information
333     // ~~~~~~~~~~~~~~~~~~~~~
334     if (meshPtr_)
335     {
336         namesLst = getZoneNames(meshPtr_->cellZones());
337     }
338     else
339     {
340         namesLst = getZoneNames("cellZones");
341     }
343     arrayRangeCellZones_.reset(arraySelection->GetNumberOfArrays());
344     forAll(namesLst, elemI)
345     {
346         arraySelection->AddArray
347         (
348             (namesLst[elemI] + " - cellZone").c_str()
349         );
350     }
351     arrayRangeCellZones_ += namesLst.size();
354     //
355     // faceZones information
356     // ~~~~~~~~~~~~~~~~~~~~~
357     if (meshPtr_)
358     {
359         namesLst = getZoneNames(meshPtr_->faceZones());
360     }
361     else
362     {
363         namesLst = getZoneNames("faceZones");
364     }
366     arrayRangeFaceZones_.reset(arraySelection->GetNumberOfArrays());
367     forAll(namesLst, elemI)
368     {
369         arraySelection->AddArray
370         (
371             (namesLst[elemI] + " - faceZone").c_str()
372         );
373     }
374     arrayRangeFaceZones_ += namesLst.size();
377     //
378     // pointZones information
379     // ~~~~~~~~~~~~~~~~~~~~~~
380     if (meshPtr_)
381     {
382         namesLst = getZoneNames(meshPtr_->pointZones());
383     }
384     else
385     {
386         namesLst = getZoneNames("pointZones");
387     }
389     arrayRangePointZones_.reset(arraySelection->GetNumberOfArrays());
390     forAll(namesLst, elemI)
391     {
392         arraySelection->AddArray
393         (
394             (namesLst[elemI] + " - pointZone").c_str()
395         );
396     }
397     arrayRangePointZones_ += namesLst.size();
399     if (debug)
400     {
401         // just for debug info
402         getSelectedArrayEntries(arraySelection);
404         Info<< "<end> Foam::vtkPV3Foam::updateInfoZones" << endl;
405     }
409 void Foam::vtkPV3Foam::updateInfoSets
411     vtkDataArraySelection* arraySelection
414     if (!reader_->GetIncludeSets())
415     {
416         return;
417     }
419     if (debug)
420     {
421         Info<< "<beg> Foam::vtkPV3Foam::updateInfoSets" << endl;
422     }
424     // Add names of sets. Search for last time directory with a sets
425     // subdirectory. Take care not to search beyond the last mesh.
427     word facesInstance = dbPtr_().findInstance
428     (
429         meshDir_,
430         "faces",
431         IOobject::READ_IF_PRESENT
432     );
434     word setsInstance = dbPtr_().findInstance
435     (
436         meshDir_/"sets",
437         word::null,
438         IOobject::READ_IF_PRESENT,
439         facesInstance
440     );
442     IOobjectList objects(dbPtr_(), setsInstance, meshDir_/"sets");
444     if (debug)
445     {
446         Info<< "     Foam::vtkPV3Foam::updateInfoSets read "
447             << objects.names() << " from " << setsInstance << endl;
448     }
451     arrayRangeCellSets_.reset(arraySelection->GetNumberOfArrays());
452     arrayRangeCellSets_ += addToSelection<cellSet>
453     (
454         arraySelection,
455         objects,
456         " - cellSet"
457     );
459     arrayRangeFaceSets_.reset(arraySelection->GetNumberOfArrays());
460     arrayRangeFaceSets_ += addToSelection<faceSet>
461     (
462         arraySelection,
463         objects,
464         " - faceSet"
465     );
467     arrayRangePointSets_.reset(arraySelection->GetNumberOfArrays());
468     arrayRangePointSets_ += addToSelection<pointSet>
469     (
470         arraySelection,
471         objects,
472         " - pointSet"
473     );
475     if (debug)
476     {
477         // just for debug info
478         getSelectedArrayEntries(arraySelection);
480         Info<< "<end> Foam::vtkPV3Foam::updateInfoSets" << endl;
481     }
485 void Foam::vtkPV3Foam::updateInfoLagrangianFields()
487     if (debug)
488     {
489         Info<< "<beg> Foam::vtkPV3Foam::updateInfoLagrangianFields"
490             << endl;
491     }
493     vtkDataArraySelection* fieldSelection =
494         reader_->GetLagrangianFieldSelection();
496     // preserve the enabled selections
497     stringList enabledEntries = getSelectedArrayEntries(fieldSelection);
498     fieldSelection->RemoveAllArrays();
500     // TODO - currently only get fields from ONE cloud
501     // have to decide if the second set of fields get mixed in
502     // or dealt with separately
504     const arrayRange& range = arrayRangeLagrangian_;
505     if (range.empty())
506     {
507         return;
508     }
510     int partId = range.start();
511     word cloudName = getPartName(partId);
513     // use the db directly since this might be called without a mesh,
514     // but the region must get added back in
515     fileName lagrangianPrefix(cloud::prefix);
516     if (meshRegion_ != polyMesh::defaultRegion)
517     {
518         lagrangianPrefix = meshRegion_/cloud::prefix;
519     }
521     IOobjectList objects
522     (
523         dbPtr_(),
524         dbPtr_().timeName(),
525         lagrangianPrefix/cloudName
526     );
528     addToSelection<IOField<label> >
529     (
530         fieldSelection,
531         objects
532     );
533     addToSelection<IOField<scalar> >
534     (
535         fieldSelection,
536         objects
537     );
538     addToSelection<IOField<vector> >
539     (
540         fieldSelection,
541         objects
542     );
543     addToSelection<IOField<sphericalTensor> >
544     (
545         fieldSelection,
547         objects
548     );
549     addToSelection<IOField<symmTensor> >
550     (
551         fieldSelection,
552         objects
553     );
554     addToSelection<IOField<tensor> >
555     (
556         fieldSelection,
557         objects
558     );
560     // restore the enabled selections
561     setSelectedArrayEntries(fieldSelection, enabledEntries);
563     if (debug)
564     {
565         Info<< "<end> Foam::vtkPV3Foam::updateInfoLagrangianFields - "
566             << "lagrangian objects.size() = " << objects.size() << endl;
567     }
571 // ************************************************************************* //